| 1 |
package vcfgo |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/csv" |
| 5 |
"errors" |
| 6 |
"fmt" |
| 7 |
"math" |
| 8 |
"regexp" |
| 9 |
"strconv" |
| 10 |
"strings" |
| 11 |
"sync" |
| 12 |
) |
| 13 |
|
| 14 |
var typeRe = `String|Integer|Float|Flag|Character|Unknown` |
| 15 |
var infoRegexp = regexp.MustCompile(fmt.Sprintf(`##INFO=<ID=(.+),Number=([\dAGR\.]*),Type=(%s),Description="(.*)">`, typeRe)) |
| 16 |
var formatRegexp = regexp.MustCompile(fmt.Sprintf(`##FORMAT=<ID=(.+),Number=([\dAGR\.]*),Type=(%s),Description="(.*)">`, typeRe)) |
| 17 |
var filterRegexp = regexp.MustCompile(`##FILTER=<ID=(.+),Description="(.*)">`) |
| 18 |
var contigRegexp = regexp.MustCompile(`contig=<.*((\w+)=([^,>]+))`) |
| 19 |
var sampleRegexp = regexp.MustCompile(`SAMPLE=<ID=([^,>]+)`) |
| 20 |
var pedRegexp = regexp.MustCompile(`PEDIGREE=<=([^,>]+)`) |
| 21 |
|
| 22 |
// var headerIdRegexp = regexp.MustCompile(`##([^=]+)=<ID=([^,]+)`) |
| 23 |
var fileVersionRegexp = regexp.MustCompile(`##fileformat=VCFv(.+)`) |
| 24 |
|
| 25 |
// Info holds the Info and Format fields |
| 26 |
type Info struct { |
| 27 |
Id string |
| 28 |
Description string |
| 29 |
Number string // A G R . '' |
| 30 |
Type string // STRING INTEGER FLOAT FLAG CHARACTER UNKONWN |
| 31 |
} |
| 32 |
|
| 33 |
// SampleFormat holds the type info for Format fields. |
| 34 |
type SampleFormat Info |
| 35 |
|
| 36 |
// Header holds all the type and format information for the variants. |
| 37 |
type Header struct { |
| 38 |
sync.RWMutex |
| 39 |
|
| 40 |
SampleNames []string |
| 41 |
Infos map[string]*Info |
| 42 |
SampleFormats map[string]*SampleFormat |
| 43 |
Filters map[string]string |
| 44 |
Extras []string |
| 45 |
FileFormat string |
| 46 |
// Contigs is a list of maps of length, URL, etc. |
| 47 |
Contigs []map[string]string |
| 48 |
// ##SAMPLE |
| 49 |
Samples map[string]string |
| 50 |
Pedigrees []string |
| 51 |
} |
| 52 |
|
| 53 |
// String returns a string representation. |
| 54 |
func (i *Info) String() string { |
| 55 |
return fmt.Sprintf("##INFO=<ID=%s,Number=%s,Type=%s,Description=\"%s\">", i.Id, i.Number, i.Type, i.Description) |
| 56 |
} |
| 57 |
|
| 58 |
// String returns a string representation. |
| 59 |
func (i *SampleFormat) String() string { |
| 60 |
return fmt.Sprintf("##FORMAT=<ID=%s,Number=%s,Type=%s,Description=\"%s\">", i.Id, i.Number, i.Type, i.Description) |
| 61 |
} |
| 62 |
|
| 63 |
/* |
| 64 |
func (h *Header) Validate(verr *VCFError) []error { |
| 65 |
var errs []error |
| 66 |
return errs |
| 67 |
}*/ |
| 68 |
|
| 69 |
func (h *Header) parseSample(format []string, s string) (*SampleGenotype, []error) { |
| 70 |
values := strings.Split(s, ":") |
| 71 |
if len(format) != len(values) { |
| 72 |
return NewSampleGenotype(), []error{fmt.Errorf("bad sample string: %s", s)} |
| 73 |
} |
| 74 |
//if geno == nil { |
| 75 |
var value string |
| 76 |
var geno = NewSampleGenotype() |
| 77 |
var errs []error |
| 78 |
//} |
| 79 |
var e error |
| 80 |
|
| 81 |
for i, field := range format { |
| 82 |
value = values[i] |
| 83 |
switch field { |
| 84 |
case "GT": |
| 85 |
e = h.setSampleGT(geno, value) |
| 86 |
case "DP": |
| 87 |
e = h.setSampleDP(geno, value) |
| 88 |
case "GL": |
| 89 |
e = h.setSampleGL(geno, value, false) |
| 90 |
case "PL": |
| 91 |
e = h.setSampleGL(geno, value, true) |
| 92 |
case "GQ": |
| 93 |
if format, ok := h.SampleFormats[field]; ok { |
| 94 |
e = h.setSampleGQ(geno, value, format.Type) |
| 95 |
} |
| 96 |
} |
| 97 |
geno.Fields[field] = value |
| 98 |
if e != nil { |
| 99 |
errs = append(errs, e) |
| 100 |
} |
| 101 |
} |
| 102 |
return geno, errs |
| 103 |
} |
| 104 |
|
| 105 |
func (h *Header) setSampleDP(geno *SampleGenotype, value string) error { |
| 106 |
var err error |
| 107 |
geno.DP, err = strconv.Atoi(value) |
| 108 |
if err != nil && (value == "" || value == ".") { |
| 109 |
return nil |
| 110 |
} |
| 111 |
return err |
| 112 |
} |
| 113 |
|
| 114 |
func (h *Header) setSampleGQ(geno *SampleGenotype, value string, Type string) error { |
| 115 |
var err error |
| 116 |
if Type == "Integer" { |
| 117 |
geno.GQ, err = strconv.Atoi(value) |
| 118 |
} else if Type == "Float" { |
| 119 |
var v float64 |
| 120 |
v, err = strconv.ParseFloat(value, 32) |
| 121 |
if err == nil { |
| 122 |
err = errors.New("setSampleGQ: GQ reported as float. rounding to int") |
| 123 |
geno.GQ = int(math.Floor(v + 0.5)) |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
if err != nil && (value == "" || value == ".") { |
| 128 |
return nil |
| 129 |
} |
| 130 |
return err |
| 131 |
} |
| 132 |
|
| 133 |
func (h *Header) setSampleGL(geno *SampleGenotype, value string, isPL bool) error { |
| 134 |
var err error |
| 135 |
if len(geno.GL) != 0 { |
| 136 |
geno.GL = geno.GL[:0] |
| 137 |
} |
| 138 |
if value == "." || value == "" { |
| 139 |
return nil |
| 140 |
} |
| 141 |
vals := strings.Split(value, ",") |
| 142 |
var v float64 |
| 143 |
for _, val := range vals { |
| 144 |
v, err = strconv.ParseFloat(val, 64) |
| 145 |
if isPL { |
| 146 |
v /= -10.0 |
| 147 |
} |
| 148 |
geno.GL = append(geno.GL, v) |
| 149 |
} |
| 150 |
return err |
| 151 |
} |
| 152 |
|
| 153 |
func (h *Header) setSampleGT(geno *SampleGenotype, value string) error { |
| 154 |
geno.Phased = strings.Contains(value, "|") |
| 155 |
splitString := "/" |
| 156 |
if geno.Phased { |
| 157 |
splitString = "|" |
| 158 |
} |
| 159 |
|
| 160 |
alleles := strings.Split(value, splitString) |
| 161 |
for _, allele := range alleles { |
| 162 |
switch allele { |
| 163 |
case ".": |
| 164 |
geno.GT = append(geno.GT, -1) |
| 165 |
case "1": |
| 166 |
geno.GT = append(geno.GT, 1) |
| 167 |
case "0": |
| 168 |
geno.GT = append(geno.GT, 0) |
| 169 |
default: |
| 170 |
v, err := strconv.Atoi(allele) |
| 171 |
if err != nil { |
| 172 |
return err |
| 173 |
} |
| 174 |
geno.GT = append(geno.GT, v) |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
return nil |
| 179 |
} |
| 180 |
|
| 181 |
// NewHeader returns a Header with the requisite allocations. |
| 182 |
func NewHeader() *Header { |
| 183 |
var h Header |
| 184 |
h.Filters = make(map[string]string) |
| 185 |
h.Infos = make(map[string]*Info) |
| 186 |
h.SampleFormats = make(map[string]*SampleFormat) |
| 187 |
h.SampleNames = make([]string, 0) |
| 188 |
h.Pedigrees = make([]string, 0) |
| 189 |
h.Samples = make(map[string]string) |
| 190 |
h.Extras = make([]string, 0) |
| 191 |
h.Contigs = make([]map[string]string, 0, 64) |
| 192 |
return &h |
| 193 |
} |
| 194 |
|
| 195 |
func parseHeaderInfo(info string) (*Info, error) { |
| 196 |
res := infoRegexp.FindStringSubmatch(info) |
| 197 |
if len(res) != 5 { |
| 198 |
return nil, fmt.Errorf("INFO error: %s, %v", info, res) |
| 199 |
} |
| 200 |
var i Info |
| 201 |
i.Id = res[1] |
| 202 |
i.Number = res[2] |
| 203 |
i.Type = res[3] |
| 204 |
i.Description = res[4] |
| 205 |
return &i, nil |
| 206 |
} |
| 207 |
|
| 208 |
func parseHeaderContig(contig string) (map[string]string, error) { |
| 209 |
vmap := make(map[string]string) |
| 210 |
contig = strings.TrimSuffix(strings.TrimPrefix(contig, "##contig=<"), ">") |
| 211 |
rdr := csv.NewReader(strings.NewReader(contig)) |
| 212 |
rdr.LazyQuotes = true |
| 213 |
rdr.TrimLeadingSpace = true |
| 214 |
contigs, err := rdr.Read() |
| 215 |
|
| 216 |
for _, pair := range contigs { |
| 217 |
kv := strings.SplitN(pair, "=", 2) |
| 218 |
vmap[kv[0]] = kv[1] |
| 219 |
} |
| 220 |
return vmap, err |
| 221 |
} |
| 222 |
|
| 223 |
func parseHeaderExtraKV(kv string) ([]string, error) { |
| 224 |
kv = strings.TrimLeft(kv, "##") |
| 225 |
kv = strings.TrimLeft(kv, " ") |
| 226 |
kvpair := strings.SplitN(kv, "=", 2) |
| 227 |
|
| 228 |
if len(kvpair) != 2 { |
| 229 |
kvpair = append(kvpair, "") |
| 230 |
return kvpair, fmt.Errorf("header error in extra field: %s", kv) |
| 231 |
} |
| 232 |
return kvpair, nil |
| 233 |
} |
| 234 |
|
| 235 |
func parseHeaderFormat(info string) (*SampleFormat, error) { |
| 236 |
res := formatRegexp.FindStringSubmatch(info) |
| 237 |
if len(res) != 5 { |
| 238 |
return nil, fmt.Errorf("FORMAT error: %s", info) |
| 239 |
} |
| 240 |
var i SampleFormat |
| 241 |
i.Id = res[1] |
| 242 |
i.Number = res[2] |
| 243 |
i.Type = res[3] |
| 244 |
i.Description = res[4] |
| 245 |
return &i, nil |
| 246 |
} |
| 247 |
|
| 248 |
func parseHeaderFilter(info string) ([]string, error) { |
| 249 |
res := filterRegexp.FindStringSubmatch(info) |
| 250 |
if len(res) != 3 { |
| 251 |
return nil, fmt.Errorf("FILTER error: %s", info) |
| 252 |
} |
| 253 |
return res[1:3], nil |
| 254 |
} |
| 255 |
|
| 256 |
// return just the sample id. |
| 257 |
func parseHeaderSample(line string) (string, error) { |
| 258 |
res := sampleRegexp.FindStringSubmatch(line) |
| 259 |
if len(res) != 2 { |
| 260 |
return "", fmt.Errorf("error parsing ##SAMPLE") |
| 261 |
} |
| 262 |
return res[1], nil |
| 263 |
} |
| 264 |
|
| 265 |
func parseHeaderFileVersion(format string) (string, error) { |
| 266 |
res := fileVersionRegexp.FindStringSubmatch(format) |
| 267 |
if len(res) != 2 { |
| 268 |
return "-1", fmt.Errorf("file format error: %s", format) |
| 269 |
} |
| 270 |
|
| 271 |
return res[1], nil |
| 272 |
} |
| 273 |
|
| 274 |
func parseSampleLine(line string) ([]string, error) { |
| 275 |
fields := strings.Split(line, "\t") |
| 276 |
var samples []string |
| 277 |
if len(fields) > 9 { |
| 278 |
samples = fields[9:] |
| 279 |
} else { |
| 280 |
samples = []string{} |
| 281 |
} |
| 282 |
return samples, nil |
| 283 |
} |
| 284 |
|
| 285 |
func parseOne(key, val, itype string) (interface{}, error) { |
| 286 |
var v interface{} |
| 287 |
var err error |
| 288 |
switch itype { |
| 289 |
case "Integer", "INTEGER": |
| 290 |
v, err = strconv.Atoi(val) |
| 291 |
case "Float", "FLOAT": |
| 292 |
v, err = strconv.ParseFloat(val, 32) |
| 293 |
case "Flag", "FLAG": |
| 294 |
if val != "" { |
| 295 |
err = fmt.Errorf("Info Error: flag field (%s) had value", key) |
| 296 |
} |
| 297 |
v = true |
| 298 |
default: |
| 299 |
v = val |
| 300 |
} |
| 301 |
return v, err |
| 302 |
} |