| 1 |
// prep-generif.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 |
// ANSI escape codes for terminal color, highlight, and reverse |
| 19 |
const ( |
| 20 |
RED = "\033[31m" |
| 21 |
BLUE = "\033[34m" |
| 22 |
BOLD = "\033[1m" |
| 23 |
RVRS = "\033[7m" |
| 24 |
INIT = "\033[0m" |
| 25 |
LOUD = INIT + RED + BOLD |
| 26 |
INVT = LOUD + RVRS |
| 27 |
) |
| 28 |
|
| 29 |
func displayError(format string, params ...any) { |
| 30 |
|
| 31 |
str := fmt.Sprintf(format, params...) |
| 32 |
fmt.Fprintf(os.Stderr, "\n%s ERROR: %s %s%s\n", INVT, LOUD, str, INIT) |
| 33 |
} |
| 34 |
|
| 35 |
func isAllDigits(str string) bool { |
| 36 |
|
| 37 |
for _, ch := range str { |
| 38 |
if !unicode.IsDigit(ch) { |
| 39 |
return false |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
return true |
| 44 |
} |
| 45 |
|
| 46 |
func padNumericID(id string) string { |
| 47 |
|
| 48 |
if len(id) > 64 { |
| 49 |
return id |
| 50 |
} |
| 51 |
|
| 52 |
str := id |
| 53 |
|
| 54 |
if isAllDigits(str) { |
| 55 |
|
| 56 |
// pad numeric identifier to 8 characters with leading zeros |
| 57 |
ln := len(str) |
| 58 |
if ln < 8 { |
| 59 |
zeros := "00000000" |
| 60 |
str = zeros[ln:] + str |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
return str |
| 65 |
} |
| 66 |
|
| 67 |
func splitInTwoLeft(str, chr string) (string, string) { |
| 68 |
|
| 69 |
slash := strings.SplitN(str, chr, 2) |
| 70 |
if len(slash) > 1 { |
| 71 |
return slash[0], slash[1] |
| 72 |
} |
| 73 |
|
| 74 |
return str, "" |
| 75 |
} |
| 76 |
|
| 77 |
func createGeneRIF(tf, sn string) { |
| 78 |
|
| 79 |
transform := make(map[string]string) |
| 80 |
|
| 81 |
synonyms := make(map[string]string) |
| 82 |
|
| 83 |
readMappingTable := func(tf string, tbl map[string]string) { |
| 84 |
|
| 85 |
inFile, err := os.Open(tf) |
| 86 |
if err != nil { |
| 87 |
displayError("Unable to open transformation file %s - %s", tf, err.Error()) |
| 88 |
os.Exit(1) |
| 89 |
} |
| 90 |
|
| 91 |
defer inFile.Close() |
| 92 |
|
| 93 |
scanr := bufio.NewScanner(inFile) |
| 94 |
|
| 95 |
// populate transformation map |
| 96 |
for scanr.Scan() { |
| 97 |
|
| 98 |
line := scanr.Text() |
| 99 |
frst, scnd := splitInTwoLeft(line, "\t") |
| 100 |
|
| 101 |
tbl[frst] = scnd |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
readMappingTable(tf, transform) |
| 106 |
|
| 107 |
if sn != "" { |
| 108 |
// read optional gene synonym file |
| 109 |
readMappingTable(sn, synonyms) |
| 110 |
} |
| 111 |
|
| 112 |
var buffer strings.Builder |
| 113 |
count := 0 |
| 114 |
okay := false |
| 115 |
|
| 116 |
wrtr := bufio.NewWriter(os.Stdout) |
| 117 |
|
| 118 |
scanr := bufio.NewScanner(os.Stdin) |
| 119 |
|
| 120 |
currpmid := "" |
| 121 |
|
| 122 |
// skip first line with column heading names |
| 123 |
for scanr.Scan() { |
| 124 |
|
| 125 |
line := scanr.Text() |
| 126 |
cols := strings.Split(line, "\t") |
| 127 |
if len(cols) != 5 { |
| 128 |
displayError("Unexpected number of columns (%d) in generifs_basic.gz", len(cols)) |
| 129 |
os.Exit(1) |
| 130 |
} |
| 131 |
if len(cols) != 5 || cols[0] != "#Tax ID" { |
| 132 |
displayError("Unrecognized contents in generifs_basic.gz") |
| 133 |
os.Exit(1) |
| 134 |
} |
| 135 |
break |
| 136 |
} |
| 137 |
|
| 138 |
// read lines of PMIDs and gene references |
| 139 |
for scanr.Scan() { |
| 140 |
|
| 141 |
line := scanr.Text() |
| 142 |
|
| 143 |
cols := strings.Split(line, "\t") |
| 144 |
if len(cols) != 5 { |
| 145 |
continue |
| 146 |
} |
| 147 |
|
| 148 |
val := cols[2] |
| 149 |
pmids := strings.Split(val, ",") |
| 150 |
for _, pmid := range pmids { |
| 151 |
if currpmid != pmid { |
| 152 |
// end current block |
| 153 |
currpmid = pmid |
| 154 |
|
| 155 |
if pmid == "" { |
| 156 |
continue |
| 157 |
} |
| 158 |
|
| 159 |
count++ |
| 160 |
|
| 161 |
if count >= 1000 { |
| 162 |
count = 0 |
| 163 |
txt := buffer.String() |
| 164 |
if txt != "" { |
| 165 |
// print current buffer |
| 166 |
wrtr.WriteString(txt[:]) |
| 167 |
} |
| 168 |
buffer.Reset() |
| 169 |
} |
| 170 |
|
| 171 |
okay = true |
| 172 |
} |
| 173 |
|
| 174 |
addItemtoIndex := func(fld, val string) { |
| 175 |
|
| 176 |
buffer.WriteString(pmid) |
| 177 |
buffer.WriteString("\t") |
| 178 |
buffer.WriteString(fld) |
| 179 |
buffer.WriteString("\t") |
| 180 |
buffer.WriteString(val) |
| 181 |
buffer.WriteString("\n") |
| 182 |
} |
| 183 |
|
| 184 |
gene := cols[1] |
| 185 |
addItemtoIndex("GENE", gene) |
| 186 |
gn, ok := transform[gene] |
| 187 |
if ok && gn != "" { |
| 188 |
addItemtoIndex("GRIF", gn) |
| 189 |
addItemtoIndex("PREF", gn) |
| 190 |
addItemtoIndex("GENE", gn) |
| 191 |
} |
| 192 |
sn, ok := synonyms[gene] |
| 193 |
if ok && sn != "" { |
| 194 |
syns := strings.Split(sn, "|") |
| 195 |
for _, syn := range syns { |
| 196 |
addItemtoIndex("GSYN", syn) |
| 197 |
addItemtoIndex("GENE", syn) |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
if okay { |
| 204 |
txt := buffer.String() |
| 205 |
if txt != "" { |
| 206 |
// print current buffer |
| 207 |
wrtr.WriteString(txt[:]) |
| 208 |
} |
| 209 |
} |
| 210 |
buffer.Reset() |
| 211 |
|
| 212 |
wrtr.Flush() |
| 213 |
} |
| 214 |
|
| 215 |
func main() { |
| 216 |
|
| 217 |
// skip past executable name |
| 218 |
args := os.Args[1:] |
| 219 |
|
| 220 |
if len(args) < 1 { |
| 221 |
displayError("Insufficient arguments for -generif") |
| 222 |
os.Exit(1) |
| 223 |
} |
| 224 |
|
| 225 |
tf := args[0] |
| 226 |
|
| 227 |
if tf == "" { |
| 228 |
displayError("Empty transformation table for -generif") |
| 229 |
os.Exit(1) |
| 230 |
} |
| 231 |
|
| 232 |
sn := "" |
| 233 |
if len(args) > 1 { |
| 234 |
sn = args[1] |
| 235 |
} |
| 236 |
|
| 237 |
createGeneRIF(tf, sn) |
| 238 |
} |