Debian Codegraph @

extern/prep-nlmnlp.go

raw
1 // prep-nlmnlp.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 "fmt"
11 "os"
12 "strings"
13 "unicode"
14 )
15
16 // copied from EDirect's eutils library
17
18 // parser character type lookup tables
19 var inBlank [256]bool
20
21 // ANSI escape codes for terminal color, highlight, and reverse
22 const (
23 RED = "\033[31m"
24 BLUE = "\033[34m"
25 BOLD = "\033[1m"
26 RVRS = "\033[7m"
27 INIT = "\033[0m"
28 LOUD = INIT + RED + BOLD
29 INVT = LOUD + RVRS
30 )
31
32 func displayError(format string, params ...any) {
33
34 str := fmt.Sprintf(format, params...)
35 fmt.Fprintf(os.Stderr, "\n%s ERROR: %s %s%s\n", INVT, LOUD, str, INIT)
36 }
37
38 func isAllDigits(str string) bool {
39
40 for _, ch := range str {
41 if !unicode.IsDigit(ch) {
42 return false
43 }
44 }
45
46 return true
47 }
48
49 func compressRunsOfSpaces(str string) string {
50
51 whiteSpace := false
52 var buffer strings.Builder
53
54 for _, ch := range str {
55 if ch < 127 && inBlank[ch] {
56 if !whiteSpace {
57 buffer.WriteRune(' ')
58 }
59 whiteSpace = true
60 } else {
61 buffer.WriteRune(ch)
62 whiteSpace = false
63 }
64 }
65
66 return buffer.String()
67 }
68
69 func splitInTwoLeft(str, chr string) (string, string) {
70
71 slash := strings.SplitN(str, chr, 2)
72 if len(slash) > 1 {
73 return slash[0], slash[1]
74 }
75
76 return str, ""
77 }
78
79 // initialize tables
80 func init() {
81
82 for i := range inBlank {
83 inBlank[i] = false
84 }
85 inBlank[' '] = true
86 inBlank['\t'] = true
87 inBlank['\n'] = true
88 inBlank['\r'] = true
89 inBlank['\f'] = true
90 }
91
92 func createNLMNLP(tf string) {
93
94 transform := make(map[string]string)
95
96 readMappingTable := func(tf string, tbl map[string]string) {
97
98 inFile, err := os.Open(tf)
99 if err != nil {
100 displayError("Unable to open transformation file %s - %s", tf, err.Error())
101 os.Exit(1)
102 }
103
104 defer inFile.Close()
105
106 scanr := bufio.NewScanner(inFile)
107
108 // populate transformation map
109 for scanr.Scan() {
110
111 line := scanr.Text()
112 frst, scnd := splitInTwoLeft(line, "\t")
113
114 tbl[frst] = scnd
115 }
116 }
117
118 readMappingTable(tf, transform)
119
120 var buffer strings.Builder
121 count := 0
122 okay := false
123
124 wrtr := bufio.NewWriter(os.Stdout)
125
126 scanr := bufio.NewScanner(os.Stdin)
127
128 currpmid := ""
129
130 // read lines of PMIDs and extracted concepts
131 for scanr.Scan() {
132
133 line := scanr.Text()
134
135 cols := strings.Split(line, "\t")
136 if len(cols) != 5 {
137 continue
138 }
139
140 pmid := cols[0]
141 if currpmid != pmid {
142 // end current block
143 currpmid = pmid
144
145 if pmid == "" {
146 continue
147 }
148
149 count++
150
151 if count >= 1000 {
152 count = 0
153 txt := buffer.String()
154 if txt != "" {
155 // print current buffer
156 wrtr.WriteString(txt[:])
157 }
158 buffer.Reset()
159 }
160
161 okay = true
162 }
163
164 addItemtoIndex := func(fld, val string) {
165
166 buffer.WriteString(pmid)
167 buffer.WriteString("\t")
168 buffer.WriteString(fld)
169 buffer.WriteString("\t")
170 buffer.WriteString(val)
171 buffer.WriteString("\n")
172 }
173
174 typ := cols[1]
175 val := cols[2]
176 switch typ {
177 case "Gene":
178 genes := strings.Split(val, ";")
179 for _, gene := range genes {
180 if gene == "None" {
181 continue
182 }
183 addItemtoIndex("GENE", gene)
184 gn, ok := transform[gene]
185 if !ok || gn == "" {
186 continue
187 }
188 addItemtoIndex("PREF", gn)
189 addItemtoIndex("GENE", gn)
190 }
191 case "Disease":
192 if strings.HasPrefix(val, "MESH:") {
193 diszs := strings.Split(val[5:], "|")
194 for _, disz := range diszs {
195 addItemtoIndex("DISZ", disz)
196 dn, ok := transform[disz]
197 if !ok || dn == "" {
198 continue
199 }
200 addItemtoIndex("DISZ", dn)
201 }
202 } else if strings.HasPrefix(val, "OMIM:") {
203 omims := strings.Split(val[5:], "|")
204 for _, omim := range omims {
205 // was OMIM, now fused with DISZ, tag OMIM identifiers with M prefix
206 addItemtoIndex("DISZ", "M"+omim)
207 }
208 }
209 case "Chemical":
210 if strings.HasPrefix(val, "MESH:") {
211 chems := strings.Split(val[5:], "|")
212 for _, chem := range chems {
213 addItemtoIndex("CHEM", chem)
214 ch, ok := transform[chem]
215 if !ok || ch == "" {
216 continue
217 }
218 addItemtoIndex("CHEM", ch)
219 }
220 } else if strings.HasPrefix(val, "CHEBI:") {
221 chebs := strings.Split(val[6:], "|")
222 for _, cheb := range chebs {
223 // was CEBI, now fused with CHEM, tag CHEBI identifiers with H prefix
224 addItemtoIndex("CHEM", "H"+cheb)
225 }
226 }
227 case "Species":
228 case "Mutation":
229 case "CellLine":
230 default:
231 }
232 }
233
234 if okay {
235 txt := buffer.String()
236 if txt != "" {
237 // print current buffer
238 wrtr.WriteString(txt[:])
239 }
240 }
241 buffer.Reset()
242
243 wrtr.Flush()
244 }
245
246 func main() {
247
248 // skip past executable name
249 args := os.Args[1:]
250
251 if len(args) < 1 {
252 displayError("Insufficient arguments for -nlmnlp")
253 os.Exit(1)
254 }
255
256 tf := args[0]
257
258 if tf == "" {
259 displayError("Empty transformation table for -nlmnlp")
260 os.Exit(1)
261 }
262
263 createNLMNLP(tf)
264 }