-
Notifications
You must be signed in to change notification settings - Fork 0
/
vcfHeader.go
180 lines (159 loc) · 4.71 KB
/
vcfHeader.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
package vcfio
import (
"encoding/csv"
"fmt"
"regexp"
"strconv"
"strings"
"sync"
)
var typeRe = `String|Integer|Float|Flag|Character|Unknown`
var infoRegexp = regexp.MustCompile(fmt.Sprintf(`##INFO=<ID=(.+),Number=([\dAGR\.]*),Type=(%s),Description="(.*)">`, typeRe))
var formatRegexp = regexp.MustCompile(fmt.Sprintf(`##FORMAT=<ID=(.+),Number=([\dAGR\.]*),Type=(%s),Description="(.*)">`, typeRe))
var filterRegexp = regexp.MustCompile(`##FILTER=<ID=(.+),Description="(.*)">`)
var sampleRegexp = regexp.MustCompile(`SAMPLE=<ID=([^,>]+)`)
var fileVersionRegexp = regexp.MustCompile(`##fileformat=VCFv(.+)`)
// InfoVcf holds the Info and Format fields
type InfoVcf struct {
Id string
Description string
Number string // A G R . ''
Type string // STRING INTEGER FLOAT FLAG CHARACTER UNKONWN
}
// SampleFormat holds the type info for Format fields.
type SampleFormat InfoVcf
// Header holds all the type and format information for the variants.
type Header struct {
sync.RWMutex
SampleNames []string
Infos map[string]*InfoVcf
SampleFormats map[string]*SampleFormat
Filters map[string]string
Extras []string
FileFormat string
// Contigs is a list of maps of length, URL, etc.
Contigs []map[string]string
// ##SAMPLE
Samples map[string]string
Pedigrees []string
Platform string
}
// String returns a string representation.
func (i *InfoVcf) String() string {
return fmt.Sprintf("##INFO=<ID=%s,Number=%s,Type=%s,Description=\"%s\">", i.Id, i.Number, i.Type, i.Description)
}
// String returns a string representation.
func (i *SampleFormat) String() string {
return fmt.Sprintf("##FORMAT=<ID=%s,Number=%s,Type=%s,Description=\"%s\">", i.Id, i.Number, i.Type, i.Description)
}
// NewHeader returns a Header with the requisite allocations.
func NewHeader() *Header {
var h Header
h.Filters = make(map[string]string)
h.Infos = make(map[string]*InfoVcf)
h.SampleFormats = make(map[string]*SampleFormat)
h.SampleNames = make([]string, 0)
h.Pedigrees = make([]string, 0)
h.Samples = make(map[string]string)
h.Extras = make([]string, 0)
h.Contigs = make([]map[string]string, 0, 64)
return &h
}
func parseHeaderInfo(info string) (*InfoVcf, error) {
res := infoRegexp.FindStringSubmatch(info)
if len(res) != 5 {
return nil, fmt.Errorf("INFO error: %s, %v", info, res)
}
var i InfoVcf
i.Id = res[1]
i.Number = res[2]
i.Type = res[3]
i.Description = res[4]
return &i, nil
}
func parseHeaderContig(contig string) (map[string]string, error) {
vmap := make(map[string]string)
contig = strings.TrimSuffix(strings.TrimPrefix(contig, "##contig=<"), ">")
rdr := csv.NewReader(strings.NewReader(contig))
rdr.LazyQuotes = true
rdr.TrimLeadingSpace = true
contigs, err := rdr.Read()
for _, pair := range contigs {
kv := strings.SplitN(pair, "=", 2)
vmap[kv[0]] = kv[1]
}
return vmap, err
}
func parseHeaderExtraKV(kv string) ([]string, error) {
// This is repeated bc go-staticheck alert didn't like '##'
kv = strings.TrimLeft(kv, "#")
kv = strings.TrimLeft(kv, "#") // Keep second repeated line
kv = strings.TrimLeft(kv, " ")
kvpair := strings.SplitN(kv, "=", 2)
if len(kvpair) != 2 {
return nil, fmt.Errorf("header error in extra field: %s", kv)
}
return kvpair, nil
}
func parseHeaderFormat(info string) (*SampleFormat, error) {
res := formatRegexp.FindStringSubmatch(info)
if len(res) != 5 {
return nil, fmt.Errorf("FORMAT error: %s", info)
}
var i SampleFormat
i.Id = res[1]
i.Number = res[2]
i.Type = res[3]
i.Description = res[4]
return &i, nil
}
func parseHeaderFilter(info string) ([]string, error) {
res := filterRegexp.FindStringSubmatch(info)
if len(res) != 3 {
return nil, fmt.Errorf("FILTER error: %s", info)
}
return res[1:3], nil
}
// return just the sample id.
func parseHeaderSample(line string) (string, error) {
res := sampleRegexp.FindStringSubmatch(line)
if len(res) != 2 {
return "", fmt.Errorf("error parsing ##SAMPLE")
}
return res[1], nil
}
func parseHeaderFileVersion(format string) (string, error) {
res := fileVersionRegexp.FindStringSubmatch(format)
if len(res) != 2 {
return "-1", fmt.Errorf("file format error: %s", format)
}
return res[1], nil
}
func parseSampleLine(line string) ([]string, error) {
fields := strings.Split(line, "\t")
var samples []string
if len(fields) > 9 {
samples = fields[9:]
} else {
samples = []string{}
}
return samples, nil
}
func parseOne(key, val, itype string) (interface{}, error) {
var v interface{}
var err error
switch itype {
case "Integer", "INTEGER":
v, err = strconv.Atoi(val)
case "Float", "FLOAT":
v, err = strconv.ParseFloat(val, 32)
case "Flag", "FLAG":
if val != "" {
err = fmt.Errorf("Info Error: flag field (%s) had value", key)
}
v = true
default:
v = val
}
return v, err
}