Debian Codegraph @

extern/prep-nihocc.go

raw
1 // prep-nihocc.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 "strconv"
13 "strings"
14 "unicode"
15 )
16
17 // copied from EDirect's eutils library
18
19 // ANSI escape codes for terminal color, highlight, and reverse
20 const (
21 RED = "\033[31m"
22 BLUE = "\033[34m"
23 BOLD = "\033[1m"
24 RVRS = "\033[7m"
25 INIT = "\033[0m"
26 LOUD = INIT + RED + BOLD
27 INVT = LOUD + RVRS
28 )
29
30 func displayError(format string, params ...any) {
31
32 str := fmt.Sprintf(format, params...)
33 fmt.Fprintf(os.Stderr, "\n%s ERROR: %s %s%s\n", INVT, LOUD, str, INIT)
34 }
35
36 func isAllDigits(str string) bool {
37
38 for _, ch := range str {
39 if !unicode.IsDigit(ch) {
40 return false
41 }
42 }
43
44 return true
45 }
46
47 func padNumericID(id string) string {
48
49 if len(id) > 64 {
50 return id
51 }
52
53 str := id
54
55 if isAllDigits(str) {
56
57 // pad numeric identifier to 8 characters with leading zeros
58 ln := len(str)
59 if ln < 8 {
60 zeros := "00000000"
61 str = zeros[ln:] + str
62 }
63 }
64
65 return str
66 }
67
68 func createNIHOCC(hasMaxPMID bool, maxPMID string) {
69
70 var buffer strings.Builder
71 count := 0
72 okay := false
73
74 wrtr := bufio.NewWriter(os.Stdout)
75
76 scanr := bufio.NewScanner(os.Stdin)
77
78 // skip first line with column heading names
79 for scanr.Scan() {
80
81 line := scanr.Text()
82 if line != "citing,referenced" {
83 displayError("Unrecognized header '%s' in open_citation_collection.csv", line)
84 os.Exit(1)
85 }
86 break
87 }
88
89 // read lines of PMID link information
90 for scanr.Scan() {
91
92 line := scanr.Text()
93
94 cols := strings.Split(line, ",")
95 if len(cols) != 2 {
96 continue
97 }
98
99 fst := cols[0]
100 scd := cols[1]
101
102 if fst == "0" || scd == "0" {
103 continue
104 }
105
106 pdFst := padNumericID(fst)
107 pdScd := padNumericID(scd)
108
109 // string comparison works as integer comparison on left zero-padded numbers
110 if hasMaxPMID {
111 if len(pdFst) > 8 || len(pdScd) > 8 {
112 continue
113 }
114 if pdFst > maxPMID || pdScd > maxPMID {
115 continue
116 }
117 }
118
119 buffer.WriteString(fst + "\tCITED\t" + pdScd + "\n")
120 buffer.WriteString(scd + "\tCITES\t" + pdFst + "\n")
121
122 count++
123
124 if count >= 1000 {
125 count = 0
126 txt := buffer.String()
127 if txt != "" {
128 // print current buffer
129 wrtr.WriteString(txt[:])
130 }
131 buffer.Reset()
132 }
133
134 okay = true
135 }
136
137 if okay {
138 txt := buffer.String()
139 if txt != "" {
140 // print current buffer
141 wrtr.WriteString(txt[:])
142 }
143 }
144 buffer.Reset()
145
146 wrtr.Flush()
147 }
148
149 func main() {
150
151 // skip past executable name
152 args := os.Args[1:]
153
154 // e.g., -nihocc 37939422
155 hasMaxPMID := false
156 maxPMID := "0"
157 if len(args) > 0 {
158 val, err := strconv.Atoi(args[0])
159 if err == nil && val > 0 {
160 hasMaxPMID = true
161 maxPMID = args[0]
162 }
163 }
164 maxPMID = padNumericID(maxPMID)
165
166 createNIHOCC(hasMaxPMID, maxPMID)
167 }