| 1 |
// prep-geneinfo.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 |
"html" |
| 12 |
"os" |
| 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 createGeneInfo() { |
| 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 |
cols := strings.Split(line, "\t") |
| 83 |
if len(cols) != 16 { |
| 84 |
displayError("Unexpected number of columns (%d) in gene_info.gz", len(cols)) |
| 85 |
os.Exit(1) |
| 86 |
} |
| 87 |
if len(cols) != 16 || cols[0] != "#tax_id" { |
| 88 |
displayError("Unrecognized contents in gene_info.gz") |
| 89 |
os.Exit(1) |
| 90 |
} |
| 91 |
break |
| 92 |
} |
| 93 |
|
| 94 |
buffer.WriteString("<Set>\n") |
| 95 |
|
| 96 |
// read lines of gene information |
| 97 |
for scanr.Scan() { |
| 98 |
|
| 99 |
line := scanr.Text() |
| 100 |
|
| 101 |
cols := strings.Split(line, "\t") |
| 102 |
if len(cols) != 16 { |
| 103 |
continue |
| 104 |
} |
| 105 |
|
| 106 |
gene := cols[2] |
| 107 |
// skip NEWLINE entries |
| 108 |
if gene == "NEWENTRY" { |
| 109 |
continue |
| 110 |
} |
| 111 |
|
| 112 |
id := cols[1] |
| 113 |
ltag := cols[3] |
| 114 |
syns := cols[4] |
| 115 |
desc := cols[8] |
| 116 |
auth := cols[10] |
| 117 |
|
| 118 |
buffer.WriteString(" <Rec>\n") |
| 119 |
|
| 120 |
buffer.WriteString(" <Id>" + id + "</Id>\n") |
| 121 |
buffer.WriteString(" <Gene>" + html.EscapeString(gene) + "</Gene>\n") |
| 122 |
|
| 123 |
if ltag != "-" { |
| 124 |
buffer.WriteString(" <Ltag>" + html.EscapeString(ltag) + "</Ltag>\n") |
| 125 |
} |
| 126 |
if syns != "-" { |
| 127 |
buffer.WriteString(" <Syns>" + html.EscapeString(syns) + "</Syns>\n") |
| 128 |
} |
| 129 |
if desc != "-" { |
| 130 |
buffer.WriteString(" <Desc>" + html.EscapeString(desc) + "</Desc>\n") |
| 131 |
} |
| 132 |
if auth != "-" { |
| 133 |
buffer.WriteString(" <Auth>" + html.EscapeString(auth) + "</Auth>\n") |
| 134 |
} |
| 135 |
|
| 136 |
buffer.WriteString(" </Rec>\n") |
| 137 |
|
| 138 |
count++ |
| 139 |
|
| 140 |
if count >= 1000 { |
| 141 |
count = 0 |
| 142 |
txt := buffer.String() |
| 143 |
if txt != "" { |
| 144 |
// print current buffer |
| 145 |
wrtr.WriteString(txt[:]) |
| 146 |
} |
| 147 |
buffer.Reset() |
| 148 |
} |
| 149 |
|
| 150 |
okay = true |
| 151 |
} |
| 152 |
|
| 153 |
buffer.WriteString("</Set>\n") |
| 154 |
|
| 155 |
if okay { |
| 156 |
txt := buffer.String() |
| 157 |
if txt != "" { |
| 158 |
// print current buffer |
| 159 |
wrtr.WriteString(txt[:]) |
| 160 |
} |
| 161 |
} |
| 162 |
buffer.Reset() |
| 163 |
|
| 164 |
wrtr.Flush() |
| 165 |
} |
| 166 |
|
| 167 |
func main() { |
| 168 |
|
| 169 |
createGeneInfo() |
| 170 |
} |