/
main.go
191 lines (168 loc) · 3.79 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package main
import (
"flag"
"fmt"
"log"
_ "net/http/pprof"
"os"
"path/filepath"
"regexp"
"runtime/pprof"
"strings"
"github.com/liserjrqlxue/goUtil/fmtUtil"
"github.com/liserjrqlxue/goUtil/osUtil"
"github.com/liserjrqlxue/goUtil/simpleUtil"
"github.com/liserjrqlxue/goUtil/textUtil"
"github.com/liserjrqlxue/simple-util"
"github.com/liserjrqlxue/version"
"github.com/pelletier/go-toml"
"github.com/liserjrqlxue/anno2xlsx/v2/anno"
)
// os
var (
ex, _ = os.Executable()
exPath = filepath.Dir(ex)
etcPath = filepath.Join(exPath, "..", "..", "etc")
dbPath = filepath.Join(exPath, "..", "..", "db")
)
// flag
var (
cfg = flag.String(
"cfg",
filepath.Join(etcPath, "config.toml"),
"toml config document",
)
input = flag.String(
"input",
"",
"input tsv",
)
output = flag.String(
"output",
"",
"output, default is -input.tsv",
)
cnvType = flag.String(
"cnvType",
"",
"cnvType[exon_cnv|large_cnv]",
)
title = flag.String(
"title",
filepath.Join(exPath, "title.txt"),
"output title",
)
geneID = flag.String(
"geneId",
filepath.Join(dbPath, "gene.id.txt"),
"gene symbol and ncbi id list",
)
cpuprofile = flag.String(
"cpuprofile",
"",
"cpu profile",
)
memprofile = flag.String(
"memprofile",
"",
"mem profile",
)
warn = flag.Bool(
"warn",
false,
"warn gene id lost rather than fatal",
)
)
// database
var (
aesCode = "c3d112d6a47a0a04aad2b9d2d2cad266"
gene2id map[string]string
// 突变频谱
spectrumDb anno.EncodeDb
// 基因-疾病
diseaseDb anno.EncodeDb
chpo anno.AnnoDb
)
// \n -> <br/>
var isLF = regexp.MustCompile(`\n`)
func init() {
version.LogVersion()
flag.Parse()
if *cpuprofile != "" {
var f = osUtil.Create(*cpuprofile)
simpleUtil.CheckErr(pprof.StartCPUProfile(f))
defer pprof.StopCPUProfile()
}
if *input == "" {
flag.Usage()
fmt.Println("\n-input is required")
os.Exit(0)
}
if *output == "" {
*output = *input + ".tsv"
}
var tomlCfg = simpleUtil.HandleError(toml.LoadFile(*cfg)).(*toml.Tree)
// CHPO
chpo.Load(
tomlCfg.Get("annotation.hpo").(*toml.Tree),
dbPath,
)
// 突变频谱
spectrumDb.Load(
tomlCfg.Get("annotation.Gene.spectrum").(*toml.Tree),
dbPath,
[]byte(aesCode),
)
// 基因-疾病
diseaseDb.Load(
tomlCfg.Get("annotation.Gene.disease").(*toml.Tree),
dbPath,
[]byte(aesCode),
)
anno.LoadGeneTrans(anno.GuessPath(tomlCfg.Get("annotation.Gene.transcript").(string), dbPath))
}
func main() {
var out = osUtil.Create(*output)
defer simpleUtil.DeferClose(out)
gene2id = simpleUtil.HandleError(textUtil.File2Map(*geneID, "\t", false)).(map[string]string)
cnvDb, _ := simple_util.LongFile2MapArray(*input, "\t", nil)
titles := textUtil.File2Array(*title)
fmtUtil.Fprintln(out, strings.Join(titles, "\t"))
for _, item := range cnvDb {
// Primer
item["Primer"] = anno.CnvPrimer(item, *cnvType)
var gene = item["OMIM_Gene"]
var geneIDs []string
for _, g := range strings.Split(gene, ";") {
var id, ok = gene2id[g]
if !ok {
if g != "-" && g != "." {
if *warn {
log.Printf("can not find gene id of [%s]:[%s]\n", g, gene)
} else {
log.Fatalf("can not find gene id of [%s]:[%s]\n", g, gene)
}
}
}
geneIDs = append(geneIDs, id)
}
// CHPO
chpo.Annos(item, "<br/>", geneIDs)
// 基因-疾病
diseaseDb.Annos(item, "<br/>", geneIDs)
// 突变频谱
spectrumDb.Annos(item, "<br/>", geneIDs)
item["OMIM"] = item["OMIM_Phenotype_ID"]
anno.UpdateCnvAnnot(gene, *cnvType, item, gene2id, diseaseDb.Db)
var array []string
for _, key := range titles {
array = append(array, isLF.ReplaceAllString(item[key], "<br/>"))
}
fmtUtil.FprintStringArray(out, array, "\t")
}
if *memprofile != "" {
var f = osUtil.Create(*memprofile)
defer simpleUtil.DeferClose(f)
simpleUtil.CheckErr(pprof.WriteHeapProfile(f))
}
}