Debian Codegraph @

extern/prep-finish.go

raw
1 // prep-finish.go
2
3 // Public domain notice for all NCBI EDirect scripts is located at:
4 // https://www.ncbi.nlm.nih.gov/books/NBK179288/#chapter6.Public_Domain_Notice
5
6 package main
7
8 import (
9 "bufio"
10 "compress/gzip"
11 "fmt"
12 "html"
13 "io"
14 "maps"
15 "os"
16 "slices"
17 "strconv"
18 "strings"
19 )
20
21 // copied from EDirect's eutils library
22
23 // ANSI escape codes for terminal color, highlight, and reverse
24 const (
25 RED = "\033[31m"
26 BLUE = "\033[34m"
27 BOLD = "\033[1m"
28 RVRS = "\033[7m"
29 INIT = "\033[0m"
30 LOUD = INIT + RED + BOLD
31 INVT = LOUD + RVRS
32 )
33
34 func displayError(format string, params ...any) {
35
36 str := fmt.Sprintf(format, params...)
37 fmt.Fprintf(os.Stderr, "\n%s ERROR: %s %s%s\n", INVT, LOUD, str, INIT)
38 }
39
40 func displayWarning(format string, params ...any) {
41
42 str := fmt.Sprintf(format, params...)
43 fmt.Fprintf(os.Stderr, "\n%s WARNING: %s %s%s\n", INVT, LOUD, str, INIT)
44 }
45
46 func prepareIndices(chunk int, target, prefix string) {
47
48 zipp := true
49
50 suffix := "e2x"
51 sfx := suffix
52 if zipp {
53 sfx += ".gz"
54 }
55
56 fnum := 0
57
58 scanr := bufio.NewScanner(os.Stdin)
59
60 processChunk := func() bool {
61
62 // map for combined index
63 indexed := make(map[string][]string)
64
65 writeChunk := func() {
66
67 var (
68 fl *os.File
69 wrtr *bufio.Writer
70 zpr *gzip.Writer
71 err error
72 )
73
74 fnum++
75 fpath := fmt.Sprintf("%s/%s%03d.%s", target, prefix, fnum, sfx)
76 fl, err = os.Create(fpath)
77 if err != nil {
78 fmt.Fprintf(os.Stderr, "%s\n", err.Error())
79 return
80 }
81 defer fl.Close()
82
83 pth := fmt.Sprintf("%s%03d.%s", prefix, fnum, suffix)
84 os.Stderr.WriteString(pth + "\n")
85
86 var out io.Writer
87
88 out = fl
89
90 if zipp {
91
92 zpr, err = gzip.NewWriterLevel(fl, gzip.BestSpeed)
93 if err != nil {
94 fmt.Fprintf(os.Stderr, "%s\n", err.Error())
95 return
96 }
97
98 out = zpr
99 }
100
101 wrtr = bufio.NewWriter(out)
102 if wrtr == nil {
103 displayWarning("Unable to create bufio.NewWriter")
104 return
105 }
106
107 var buffer strings.Builder
108 count := 0
109
110 buffer.WriteString("<IdxDocumentSet>\n")
111
112 // sort fields in alphabetical order
113 keys := slices.Sorted(maps.Keys(indexed))
114
115 for _, idx := range keys {
116
117 item, ok := indexed[idx]
118 if !ok {
119 continue
120 }
121
122 uid := item[0]
123 data := item[1:]
124
125 if uid == "" || len(data) < 1 {
126 continue
127 }
128
129 // do not sort data now that it has field and value pairs
130
131 buffer.WriteString(" <IdxDocument>\n")
132 buffer.WriteString(" <IdxUid>")
133 buffer.WriteString(uid)
134 buffer.WriteString("</IdxUid>\n")
135 buffer.WriteString(" <IdxSearchFields>\n")
136
137 prevf := ""
138 prevv := ""
139 for len(data) > 0 {
140 fld := data[0]
141 val := data[1]
142 data = data[2:]
143
144 if fld == prevf && val == prevv {
145 continue
146 }
147
148 buffer.WriteString(" <")
149 buffer.WriteString(fld)
150 buffer.WriteString(">")
151 buffer.WriteString(val)
152 buffer.WriteString("</")
153 buffer.WriteString(fld)
154 buffer.WriteString(">\n")
155
156 prevf = fld
157 prevv = val
158 }
159
160 buffer.WriteString(" </IdxSearchFields>\n")
161 buffer.WriteString(" </IdxDocument>\n")
162
163 count++
164
165 if count >= 1000 {
166 count = 0
167 txt := buffer.String()
168 if txt != "" {
169 // print current buffer
170 wrtr.WriteString(txt[:])
171 }
172 buffer.Reset()
173 }
174 }
175
176 buffer.WriteString("</IdxDocumentSet>\n")
177
178 txt := buffer.String()
179 if txt != "" {
180 // print current buffer
181 wrtr.WriteString(txt[:])
182 }
183 buffer.Reset()
184
185 wrtr.Flush()
186
187 if zpr != nil {
188 zpr.Close()
189 }
190 }
191
192 lineCount := 0
193 okay := false
194
195 // read lines of dependency paths, scores for each theme
196 for scanr.Scan() {
197
198 line := scanr.Text()
199
200 cols := strings.Split(line, "\t")
201 if len(cols) != 3 {
202 displayWarning("Mismatched -thesis columns in '%s'", line)
203 continue
204 }
205
206 uid := cols[0]
207 fd := cols[1]
208 val := cols[2]
209 if uid == "" || fd == "" || val == "" {
210 continue
211 }
212
213 val = strings.ToLower(val)
214 // convert angle brackets in chemical names
215 val = html.EscapeString(val)
216
217 data, ok := indexed[uid]
218 if !ok {
219 data = make([]string, 0, 3)
220 // first entry on new slice is uid
221 data = append(data, uid)
222 }
223 data = append(data, fd)
224 data = append(data, val)
225 // always need to update indexed, since data may be reallocated
226 indexed[uid] = data
227
228 okay = true
229
230 lineCount++
231 if lineCount > chunk {
232 break
233 }
234 }
235
236 if okay {
237 writeChunk()
238 return true
239 }
240
241 return false
242 }
243
244 for processChunk() {
245 // loop until scanner runs out of lines
246 }
247 }
248
249 func main() {
250
251 // skip past executable name
252 args := os.Args[1:]
253
254 if len(args) < 3 {
255 displayError("Insufficient arguments for -thesis")
256 os.Exit(1)
257 }
258
259 // e.g., 250000 "$target" "biocchem"
260 chunk, err := strconv.Atoi(args[0])
261 if err != nil {
262 displayError("Unrecognized count - '%s'", err.Error())
263 os.Exit(1)
264 }
265 target := strings.TrimSuffix(args[1], "/")
266 prefix := args[2]
267
268 prepareIndices(chunk, target, prefix)
269 }