forked from brentp/vcfanno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vcfanno.go
251 lines (223 loc) · 6.18 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
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
// vcfanno is a command-line application and an api for annotating intervals (bed or vcf).
package main
import (
"flag"
"fmt"
"io"
"log"
//_ "net/http/pprof"
"os"
"runtime"
"strconv"
"strings"
"sync"
"time"
"github.com/BurntSushi/toml"
"github.com/biogo/hts/bgzf"
"github.com/brentp/irelate"
"github.com/brentp/irelate/interfaces"
"github.com/brentp/irelate/parsers"
. "github.com/brentp/vcfanno/api"
. "github.com/brentp/vcfanno/shared"
"github.com/brentp/vcfgo"
"github.com/brentp/xopen"
)
const VERSION = "0.3.5"
func envGet(name string, vdefault int) int {
sval := os.Getenv(name)
var err error
if sval != "" {
vdefault, err = strconv.Atoi(sval)
if err != nil {
log.Printf("couldn't parse %s using %d\n", name, vdefault)
} else {
log.Printf("using %s of %d\n", name, vdefault)
}
}
return vdefault
}
func init() {
log.SetFlags(log.Lshortfile)
}
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.")
lua := flag.String("lua", "", "optional path to a file containing custom lua 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.")
flag.Parse()
inFiles := flag.Args()
if len(inFiles) != 2 {
fmt.Printf(`Usage:
%s config.toml input.vcf > annotated.vcf
`, os.Args[0])
flag.PrintDefaults()
os.Exit(2)
}
queryFile := inFiles[1]
if !(xopen.Exists(queryFile) || queryFile == "") {
fmt.Fprintf(os.Stderr, "\nERROR: can't find query file: %s\n", queryFile)
os.Exit(2)
}
runtime.GOMAXPROCS(*procs)
var config Config
if _, err := toml.DecodeFile(inFiles[0], &config); err != nil {
if strings.Contains(err.Error(), "Expected value but found") {
fmt.Fprintln(os.Stderr, "\nNOTE: you must quote values in the conf file, e.g. fields=['AC', 'AN'] instead of fields=[AC, AN]")
}
panic(err)
}
config.Base = *base
for _, a := range config.Annotation {
err := CheckAnno(&a)
if err != nil {
log.Fatal("CheckAnno err:", err)
}
for _, op := range a.Ops {
if len(op) > 4 && op[:4] == "lua:" && *lua == "" {
log.Fatal("ERROR: requested lua op without specifying -lua flag")
}
}
}
for i := range config.PostAnnotation {
r := config.PostAnnotation[i]
err := CheckPostAnno(&r)
if err != nil {
log.Fatal(fmt.Sprintf("error in postannotation section %s err: %s", r.Name, err))
}
if len(r.Op) > 4 && r.Op[:4] == "lua:" && *lua == "" {
log.Fatal("ERROR: requested lua op without specifying -lua flag")
}
}
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))
}()
*/
luaString := ReadLua(*lua)
strict := !*notstrict
var a = NewAnnotator(sources, luaString, *ends, strict, config.PostAnnotation)
var out io.Writer = os.Stdout
defer os.Stdout.Close()
var err error
var qrdr io.Reader
// try to parallelize reading if we have plenty of CPUs and it's (possibly)
// a bgzf file.
if len(config.Annotation) < runtime.GOMAXPROCS(0) && strings.HasSuffix(queryFile, ".gz") || strings.HasSuffix(queryFile, ".bgz") {
if rdr, err := os.Open(queryFile); err == nil {
if st, err := rdr.Stat(); err == nil && st.Size() > 2320303098 {
qrdr, err = bgzf.NewReader(rdr, 4)
if err == nil {
log.Printf("using 4 worker threads to decompress bgzip file")
} else {
qrdr = nil
}
} else {
qrdr, err = bgzf.NewReader(rdr, 2)
if err == nil {
log.Printf("using 2 worker threads to decompress bgzip file")
} else {
qrdr = nil
}
}
} else {
log.Fatal(err)
}
}
if qrdr == nil {
qrdr, err = xopen.Ropen(queryFile)
log.Printf("falling back to non-bgzip")
}
if err != nil {
log.Fatal(fmt.Errorf("error opening query file %s: %s", queryFile, err))
}
qstream, query, err := parsers.VCFIterator(qrdr)
if err != nil {
log.Fatal(fmt.Errorf("error parsing VCF query file %s: %s", queryFile, err))
}
queryables, err := a.Setup(query)
if err != nil {
log.Fatal(err)
}
aends := INTERVAL
if *ends {
aends = BOTH
}
lastMsg := struct {
sync.RWMutex
s [10]string
i int
}{}
fn := func(v interfaces.Relatable) {
e := a.AnnotateEnds(v, aends)
if e != nil {
lastMsg.RLock()
em := e.Error()
found := false
for i := len(lastMsg.s) - 1; i >= 0; i-- {
if em == lastMsg.s[i] {
found = true
break
}
}
if !found {
log.Println(e, ">> this error/warning may occur many times. reporting once here...")
lastMsg.RUnlock()
lastMsg.Lock()
lastMsg.s[lastMsg.i] = em
if lastMsg.i == len(lastMsg.s)-1 {
lastMsg.i = -1
}
lastMsg.i++
lastMsg.Unlock()
} else {
lastMsg.RUnlock()
}
}
}
maxGap := envGet("IRELATE_MAX_GAP", 20000)
maxChunk := envGet("IRELATE_MAX_CHUNK", 8000)
// make a new writer from the string header.
query.Header.Extras = append(query.Header.Extras, fmt.Sprintf("##vcfanno=%s", VERSION))
out, err = vcfgo.NewWriter(out, query.Header)
stream := irelate.PIRelate(maxChunk, maxGap, qstream, *ends, fn, queryables...)
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 {
//log.Printf("%v\n", interval)
fmt.Fprintln(out, interval)
n++
}
printTime(start, n)
}
func printTime(start time.Time, n int) {
dur := time.Since(start)
duri, duru := dur.Seconds(), "second"
log.Printf("annotated %d variants in %.2f %ss (%.1f / %s)", n, duri, duru, float64(n)/duri, duru)
}