-
Notifications
You must be signed in to change notification settings - Fork 3
/
biobtree.go
executable file
·511 lines (415 loc) · 12.7 KB
/
biobtree.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
package main
import (
"biobtree/configs"
"biobtree/generate"
"biobtree/service"
"biobtree/update"
"fmt"
"log"
"os"
"runtime"
"runtime/pprof"
// "runtime"
// "runtime/pprof"
"strconv"
"strings"
"time"
"github.com/urfave/cli"
)
const version = "1.8.0"
const versionTag = "v1.8.0"
var config *configs.Conf
var allDatasets = []string{"uniprot", "go", "eco", "efo", "hgnc", "chebi", "taxonomy",
"interpro", "hmdb", "literature_mappings", "ensembl",
"uniparc", "uniref50", "uniref90", "uniref100", "my_data", "uniprot_unreviewed",
"ensembl_bacteria", "ensembl_fungi", "ensembl_metazoa", "ensembl_plants", "ensembl_protists",
"chembl_document", "chembl_assay", "chembl_activity", "chembl_molecule", "chembl_target", "chembl_target_component", "chembl_cell_line"}
func main() {
app := cli.NewApp()
app.Name = "biobtree"
app.Version = version
app.Usage = "A tool to search and map bioinformatics identifiers and special keywords"
app.Copyright = ""
app.Authors = []cli.Author{
cli.Author{
Name: "Tamer Gür",
Email: "[email protected]",
},
}
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "uniprot-ftp,f",
Value: "UK",
Usage: "uniprot ftp mirrors. Switzerland, USA or UK",
},
cli.StringFlag{
Name: "datasets,d",
//Value: "uniprot_reviewed,taxonomy,hgnc,chebi,interpro,uniprot_unreviewed,uniparc,uniref100,uniref50,uniref90,my_data,literature_mappings,hmdb,ensembl,ensembl_bacteria,ensembl_fungi,ensembl_metazoa,ensembl_plants,ensembl_protists",
Usage: "change default source datasets if value starts with + sign it means default dataset and given dataset. List of datasets are uniprot,taxonomy,hgnc,chebi,interpro,uniparc,uniref50,uniref90,uniref100,my_data,uniprot_unreviewed,literature_mappings,hmdb,ensembl,ensembl_bacteria,ensembl_fungi,ensembl_metazoa,ensembl_plants,ensembl_protists,go",
},
cli.StringFlag{
Name: "target-datasets,t",
Usage: "specify target datasets. By default all datasets are included. Speeds up all process. Useful if only certain mappings are interested.",
},
cli.StringFlag{
Name: "idx,i",
Usage: "for indexing in multiple machine. Set unique number or text for each process. No need to specify for single process",
},
cli.StringFlag{
Name: "out-dir",
Usage: "change the output directory by default it is out folder in the same directory",
},
cli.StringFlag{
Name: "confdir",
Hidden: true,
Usage: "to change default config directory while developing",
},
cli.StringFlag{
Name: "lmdb-alloc-size,lmdbsize",
Hidden: true,
Usage: "specify lmdb alloc size",
},
cli.StringFlag{
Name: "pre-built,p",
Usage: "With this command pre built data automatically installed. Please check README for values of this parameter with data that are included for each value",
},
cli.BoolFlag{
Name: "keep",
Usage: "Keep existing data from update command",
},
cli.BoolFlag{
Name: "include-optionals",
Usage: "when this flag sets optional dataset which defined in the optional.dataset.json file includes for mapping. by default it is false",
},
cli.BoolFlag{
Name: "ensembl-orthologs,eo",
Usage: "For ensembls by default only gff3 files are processed. For orthologs and other mappings this parameter must be set",
},
cli.StringFlag{
Name: "ensembl-orthologs-taxids,otaxids",
Usage: "This param has same affect as 'ensembl-orthologs' but in addition fetched given comma seperated taxid for orthologs",
},
cli.BoolFlag{
Name: "ensembl-orthologs-all,eoa",
Usage: "When ensembl-orthologs is set only given taxonomies orthologs are processed if this parameter is set all orthologs and some extra mappings are included",
},
cli.BoolFlag{
Name: "no-timezone-check",
Usage: "By default from user timezone it is checked that if user using the tool from US (between gmt -4 and gmt -9) if this is true then US ftp mirrors are used for applicable datasets e.g uniprot.",
Hidden: true,
},
cli.IntFlag{
Name: "maxcpu",
Hidden: true,
Usage: "sets the maximum number of CPUs that can be executing simultaneously. By default biobtree uses all the CPUs when applicable.",
},
cli.StringFlag{
Name: "genome,s",
Usage: "Genome names for ensembl datasets",
},
cli.StringFlag{
Name: "genome-pattern,sp",
//Value: "homo_sapiens",
Usage: "Genome names pattern for ensembl datasets. e.g 'salmonella' which gets all genomes of salmonella species in ensembl",
},
cli.StringFlag{
Name: "genome-taxids,tax",
Usage: "Process all the genomes belongs to given taxonomy ids seperated by comma",
},
cli.BoolFlag{
Name: "no-web-popup,np",
Usage: "When opening the web application don't trigger opening popup",
},
cli.BoolFlag{
Name: "skip-ensembl,se",
Usage: "During uniprot data update when taxids selected this paramater is used to just index uniprot",
},
}
// add dataset local flags
for _, dataset := range allDatasets {
app.Flags = append(app.Flags, cli.StringFlag{
Name: dataset + ".file",
Hidden: true,
})
}
app.Commands = []cli.Command{
{
Name: "start",
Usage: "Shortcut command to execute update, generate and web",
Action: func(c *cli.Context) error {
return runStartCommand(c)
},
},
{
Name: "build",
Usage: "Shortcut command to execute update and generate. ",
Action: func(c *cli.Context) error {
return runBuildCommand(c)
},
},
{
Name: "update",
Usage: "Fetch selected datsets and produce chunk files",
Action: func(c *cli.Context) error {
return runUpdateCommand(c)
},
},
{
Name: "generate",
Usage: "Merge chunks from update command and generate LMDB database",
Action: func(c *cli.Context) error {
return runGenerateCommand(c)
},
},
{
Name: "web",
Usage: "Start web server for ws, ui and grpc",
Action: func(c *cli.Context) error {
return runWebCommand(c)
},
},
{
Name: "install",
Usage: "Install configuration files. Used for genomes and datasets listing",
Action: func(c *cli.Context) error {
return runInstallCommand(c)
},
},
{
Name: "alias",
Usage: "Recreates alias db this is used if new aliases wants to added while keeping existing state",
Hidden: true,
Action: func(c *cli.Context) error {
return runAliasCommand(c)
},
},
{
Name: "profile",
Hidden: true,
Usage: "profile the",
Action: func(c *cli.Context) error {
return runProfileCommand(c)
},
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
func runAliasCommand(c *cli.Context) error {
log.Println("Alias running...")
start := time.Now()
confdir := c.GlobalString("confdir")
outDir := c.GlobalString("out-dir")
includeOptionals := c.GlobalBool("include-optionals")
config = &configs.Conf{}
config.Init(confdir, versionTag, outDir, includeOptionals)
var ali = update.Alias{}
ali.Merge(config)
elapsed := time.Since(start)
log.Printf("Alias took %s", elapsed)
return nil
}
func runStartCommand(c *cli.Context) error {
err := runUpdateCommand(c)
err = runGenerateCommand(c)
err = runWebCommand(c)
return err
}
func runBuildCommand(c *cli.Context) error {
err := runUpdateCommand(c)
err = runGenerateCommand(c)
return err
}
func runUpdateCommand(c *cli.Context) error {
start := time.Now()
confdir := c.GlobalString("confdir")
outDir := c.GlobalString("out-dir")
includeOptionals := c.GlobalBool("include-optionals")
config = &configs.Conf{}
config.Init(confdir, versionTag, outDir, includeOptionals)
indataset := c.GlobalString("datasets")
d := map[string]bool{}
if len(indataset) > 0 {
for _, dt := range strings.Split(indataset, ",") {
d[dt] = true
}
}
genomeSelectionWay := 0
s := c.GlobalString("genome")
var sp []string
if len(s) > 0 {
genomeSelectionWay++
sp = strings.Split(s, ",")
}
spattern := c.GlobalString("genome-pattern")
var spatterns []string
if len(spattern) > 0 {
genomeSelectionWay++
spatterns = strings.Split(spattern, ",")
}
genometaxidsStr := c.GlobalString("genome-taxids")
var genometaxids []int
if len(genometaxidsStr) > 0 {
genomeSelectionWay++
for _, s := range strings.Split(genometaxidsStr, ",") {
taxid, err := strconv.Atoi(s)
if err != nil {
log.Fatalf("Invalid taxonomy id %s", s)
}
genometaxids = append(genometaxids, taxid)
}
}
if genomeSelectionWay > 1 {
log.Fatal("Genome can be selected with one way among 3 selection paramters.")
return nil
}
if len(d) == 0 && len(sp) == 0 && len(spatterns) == 0 && len(genometaxids) == 0 {
log.Fatal("Datasets or genome must be selected.")
return nil
}
keep := c.GlobalBool("keep")
if !keep {
config.CleanOutDirs()
}
config.Appconf["uniprot_ftp"] = c.GlobalString("uniprot-ftp")
noZoneCheck := c.GlobalBool("no-timezone-check")
if len(config.Appconf["uniprot_ftp"]) == 0 && !noZoneCheck {
t := time.Now()
_, offset := t.Zone()
if offset <= -14400 && offset >= -32400 {
log.Println("Uniprot USA ftp mirror set")
config.Appconf["uniprot_ftp"] = "USA"
}
}
t := c.GlobalString("target-datasets")
var ts []string
if len(t) > 0 {
ts = strings.Split(t, ",")
}
chunkIdxx := c.GlobalString("idx")
if len(chunkIdxx) == 0 {
chunkIdxx = strconv.Itoa(time.Now().Nanosecond())
}
cpu := c.GlobalInt("maxcpu")
if cpu > 1 {
runtime.GOMAXPROCS(cpu)
}
// check local file path settings
for _, dset := range allDatasets {
if len(c.GlobalString(dset+".file")) > 0 {
config.Dataconf[dset]["path"] = c.GlobalString(dset + ".file")
config.Dataconf[dset]["useLocalFile"] = "yes"
}
}
eo := c.GlobalBool("ensembl-orthologs")
orthologIDs := map[int]bool{}
if len(c.GlobalString("ensembl-orthologs-taxids")) > 0 {
eo = true
for _, tax := range strings.Split(c.GlobalString("ensembl-orthologs-taxids"), ",") {
taxid, err := strconv.Atoi(tax)
if err != nil {
log.Fatalf("Invalid taxonomy id %s", s)
}
orthologIDs[taxid] = true
}
}
update.NewDataUpdate(d, ts, sp, spatterns, genometaxids, c.GlobalBool("skip-ensembl"), orthologIDs, eo, c.GlobalBool("ensembl-orthologs-all"), config, chunkIdxx).Update()
elapsed := time.Since(start)
log.Printf("Update took %s", elapsed)
return nil
}
func runGenerateCommand(c *cli.Context) error {
log.Println("Generate running please wait...")
start := time.Now()
confdir := c.GlobalString("confdir")
outDir := c.GlobalString("out-dir")
lmdbAllocSize := c.GlobalString("lmdb-alloc-size")
config = &configs.Conf{}
config.Init(confdir, versionTag, outDir, true)
if len(lmdbAllocSize) > 0 {
config.Appconf["lmdbAllocSize"] = lmdbAllocSize
}
cpu := c.GlobalInt(" maxcpu")
if cpu > 1 {
runtime.GOMAXPROCS(cpu)
}
keep := c.GlobalBool("keep")
var ali = update.Alias{}
ali.Merge(config)
var d = generate.Merge{}
d.Merge(config, keep)
elapsed := time.Since(start)
log.Printf("Generate took %s", elapsed)
return nil
}
func runWebCommand(c *cli.Context) error {
confdir := c.GlobalString("confdir")
outDir := c.GlobalString("out-dir")
config = &configs.Conf{}
config.Init(confdir, versionTag, outDir, true)
cpu := c.GlobalInt(" maxcpu")
if cpu > 1 {
runtime.GOMAXPROCS(cpu)
}
web := service.Web{}
web.Start(config, c.GlobalBool("no-web-popup"))
return nil
}
func runInstallCommand(c *cli.Context) error {
confdir := c.GlobalString("confdir")
outDir := c.GlobalString("out-dir")
preBuildSet := c.GlobalString("pre-built")
config = &configs.Conf{}
config.Install(confdir, versionTag, outDir, preBuildSet, true)
return nil
}
func runProfileCommand(c *cli.Context) error {
confdir := c.GlobalString("confdir")
outDir := c.GlobalString("out-dir")
config = &configs.Conf{}
config.Init(confdir, versionTag, outDir, true)
os.Remove("memprof.out")
os.Remove("cpuprof.out")
start := time.Now()
d := strings.Split(c.GlobalString("datasets"), ",")
config.Appconf["uniprot_ftp"] = c.GlobalString("uniprot-ftp")
if len(d) == 0 {
log.Println("Error:datasets must be specified")
return nil
}
f, err := os.Create("cpuprof.out")
if err != nil {
log.Fatal("could not create CPU profile: ", err)
}
if err := pprof.StartCPUProfile(f); err != nil {
log.Fatal("could not start CPU profile: ", err)
}
defer pprof.StopCPUProfile()
chunkIdxx := c.GlobalString("idx")
if len(chunkIdxx) == 0 {
chunkIdxx = strconv.Itoa(time.Now().Nanosecond())
}
//fmt.Println("chunkindex", chunkIdx)
///updateData(d, []string{})
elapsed := time.Since(start)
log.Printf("finished took %s", elapsed)
//mergeData()
f2, err := os.Create("memprof.out")
if err != nil {
log.Fatal("could not create memory profile: ", err)
}
runtime.GC() // get up-to-date statistics
if err := pprof.WriteHeapProfile(f2); err != nil {
log.Fatal("could not write memory profile: ", err)
}
f2.Close()
return nil
}
func check(err error) {
if err != nil {
fmt.Println("Error: ", err)
panic(err)
}
}