/
vcf-filter.go
150 lines (125 loc) · 2.78 KB
/
vcf-filter.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
package main
import (
"bufio"
"errors"
"fmt"
"io"
"os"
"regexp"
"strconv"
"strings"
"github.com/codegangsta/cli"
"github.com/knmkr/go-vcf-tools/lib"
)
func doFilter(c *cli.Context) {
arg_keep_ids := c.String("keep-ids")
arg_keep_pos := c.String("keep-pos")
arg_keep_only_pass := c.Bool("keep-only-pass")
pattern := regexp.MustCompile(`rs(\d+)`)
keep_ids := make(map[int]bool)
keep_pos := make(map[int64]bool)
// Get SNP IDs to be kept if exists
if arg_keep_ids != "" {
var ids_fp *os.File
var err error
ids_fp, err = os.Open(arg_keep_ids)
if err != nil {
panic(err)
}
defer ids_fp.Close()
ids_reader := bufio.NewReaderSize(ids_fp, 128*1024)
ids_line, err := lib.Readln(ids_reader)
for err == nil {
id_found := pattern.FindStringSubmatch(ids_line)
if id_found != nil {
keep_id, _ := strconv.Atoi(id_found[1])
keep_ids[keep_id] = true
}
ids_line, err = lib.Readln(ids_reader)
}
if err != nil && err != io.EOF {
panic(err)
}
}
// Get loci to be kept if exists
if arg_keep_pos != "" {
var pos_fp *os.File
var err error
pos_fp, err = os.Open(arg_keep_pos)
if err != nil {
panic(err)
}
defer pos_fp.Close()
pos_reader := bufio.NewReaderSize(pos_fp, 128*1024)
pos_line, err := lib.Readln(pos_reader)
for err == nil {
records := strings.Split(pos_line, "\t")
chrom := records[0]
pos, _ := strconv.ParseInt(records[1], 10, 64)
chrpos := lib.ChrPos(chrom, pos)
keep_pos[chrpos] = true
pos_line, err = lib.Readln(pos_reader)
}
if err != nil && err != io.EOF {
panic(err)
}
}
// Parse header lines
reader := bufio.NewReaderSize(os.Stdin, 128*1024)
line, err := lib.Readln(reader)
for err == nil {
if strings.HasPrefix(line, "##") {
fmt.Println(line)
} else if strings.HasPrefix(line, "#CHROM") {
fmt.Println(line)
break
} else {
err = errors.New("Invalid VCF header")
break
}
line, err = lib.Readln(reader)
}
if err != nil && err != io.EOF {
panic(err)
}
// Parse body lines
line, err = lib.Readln(reader)
for err == nil {
records := strings.Split(line, "\t")
var is_pass bool
// Filter by id
if arg_keep_ids != "" {
id_found := pattern.FindStringSubmatch(records[2])
if id_found != nil {
id, _ := strconv.Atoi(id_found[1])
if keep_ids[id] {
is_pass = true
}
}
}
// Filter by loci
if arg_keep_pos != "" {
chrom := records[0]
pos, _ := strconv.ParseInt(records[1], 10, 64)
chrpos := lib.ChrPos(chrom, pos)
if keep_pos[chrpos] {
is_pass = true
}
}
// Filter by FILTER = PASS
if arg_keep_only_pass {
if records[6] == "PASS" {
is_pass = true
} else {
is_pass = false
}
}
if is_pass {
fmt.Println(line)
}
line, err = lib.Readln(reader)
}
if err != nil && err != io.EOF {
panic(err)
}
}