forked from brentp/vcfanno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vcfanno.go
171 lines (145 loc) · 3.92 KB
/
vcfanno.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
// vcfanno is a command-line application and an api for annotating intervals (bed or vcf).
package main
import (
"flag"
"fmt"
"io"
"log"
"net/http"
_ "net/http/pprof"
"os"
"runtime"
"runtime/pprof"
"time"
"github.com/BurntSushi/toml"
"github.com/brentp/bix"
"github.com/brentp/irelate"
"github.com/brentp/irelate/interfaces"
. "github.com/brentp/vcfanno/api"
. "github.com/brentp/vcfanno/shared"
"github.com/brentp/vcfgo"
"github.com/brentp/xopen"
)
const VERSION = "0.0.8"
func main() {
fmt.Fprintf(os.Stderr, `
=============================================
vcfanno version %s [built with %s]
see: https://github.com/brentp/vcfanno
=============================================
`, VERSION, runtime.Version())
ends := flag.Bool("ends", false, "annotate the start and end as well as the interval itself.")
notstrict := flag.Bool("permissive-overlap", false, "annotate with an overlapping variant even it doesn't"+
" share the same ref and alt alleles. Default is to require exact match between variants.")
js := flag.String("js", "", "optional path to a file containing custom javascript functions to be used as ops")
base := flag.String("base-path", "", "optional base-path to prepend to annotation files in the config")
procs := flag.Int("p", 2, "number of processes to use. default is 2")
flag.Parse()
inFiles := flag.Args()
if len(inFiles) != 2 {
fmt.Printf(`Usage:
%s config.toml intput.vcf > annotated.vcf
To run a server:
%s server
`, os.Args[0], os.Args[0])
flag.PrintDefaults()
return
}
queryFile := inFiles[1]
if !(xopen.Exists(queryFile)) {
fmt.Fprintf(os.Stderr, "\nERROR: can't find query file: %s\n", queryFile)
os.Exit(2)
}
if !(xopen.Exists(queryFile + ".tbi")) {
fmt.Fprintf(os.Stderr, "\nERROR: can't find index for query file: %s\n", queryFile)
os.Exit(2)
}
runtime.GOMAXPROCS(*procs)
var config Config
if _, err := toml.DecodeFile(inFiles[0], &config); err != nil {
panic(err)
}
config.Base = *base
for _, a := range config.Annotation {
err := CheckAnno(&a)
if err != nil {
log.Fatal("CheckAnno err:", err)
}
}
sources, e := config.Sources()
if e != nil {
log.Fatal(e)
}
log.Printf("found %d sources from %d files\n", len(sources), len(config.Annotation))
go func() {
log.Println(http.ListenAndServe("localhost:6060", nil))
}()
jsString := ReadJs(*js)
strict := !*notstrict
var a = NewAnnotator(sources, jsString, *ends, strict)
var out io.Writer = os.Stdout
defer os.Stdout.Close()
var err error
var bx interfaces.RelatableIterator
b, err := bix.New(queryFile, 1)
if err != nil {
log.Fatal(fmt.Errorf("error opening query file and/or index: %s ... %s", queryFile, err))
}
a.UpdateHeader(b)
if err != nil {
log.Fatal(err)
}
bx, err = b.Query(nil)
if err != nil {
log.Fatal(err)
}
files, err := a.SetupStreams()
if err != nil {
log.Fatal(err)
}
aends := INTERVAL
if *ends {
aends = BOTH
}
fn := func(v interfaces.Relatable) {
a.AnnotateEnds(v, aends)
}
queryables := make([]interfaces.Queryable, len(files))
for i, f := range files {
q, err := bix.New(f, 1)
if err != nil {
log.Fatal(err)
}
queryables[i] = q
}
stream := irelate.PIRelate(4000, 20000, bx, *ends, fn, queryables...)
// make a new writer from the string header.
out, err = vcfgo.NewWriter(out, b.VReader.Header)
if err != nil {
log.Fatal(err)
}
start := time.Now()
n := 0
if os.Getenv("IRELATE_PROFILE") == "TRUE" {
log.Println("profiling to: irelate.pprof")
f, err := os.Create("irelate.pprof")
if err != nil {
panic(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
for interval := range stream {
fmt.Fprintf(out, "%s\n", interval)
n++
}
printTime(start, n)
}
func printTime(start time.Time, n int) {
dur := time.Since(start)
duri, duru := dur.Seconds(), "second"
if duri > float64(600) {
duri, duru = dur.Minutes(), "minute"
}
log.Printf("annotated %d variants in %.2f %ss (%.1f / %s)", n, duri, duru, float64(n)/duri, duru)
}