This repository has been archived by the owner on Feb 9, 2021. It is now read-only.
forked from brentp/vcfgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriter.go
93 lines (79 loc) · 1.74 KB
/
writer.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
package vcfgo
import (
"fmt"
"io"
"sort"
"strings"
)
// Writer allows writing VCF files.
type Writer struct {
io.Writer
Header *Header
}
// NewWriter returns a writer after writing the header.
func NewWriter(w io.Writer, h *Header) (*Writer, error) {
fmt.Fprintf(w, "##fileformat=VCFv%s\n", h.FileFormat)
for _, imap := range h.Contigs {
fmt.Fprintf(w, "##contig=<ID=%s", imap["ID"])
for k, v := range imap {
if k == "ID" {
continue
}
fmt.Fprintf(w, ",%s=%s", k, v)
}
fmt.Fprintln(w, ">")
}
// Samples
keys := make([]string, 0, len(h.Samples))
for sampleId := range h.Samples {
keys = append(keys, sampleId)
}
sort.Strings(keys)
for _, sampleId := range keys {
fmt.Fprintln(w, h.Samples[sampleId])
}
for i := range h.Pedigrees {
fmt.Fprintln(w, h.Pedigrees[i])
}
// Filters
keys = keys[:0]
for k := range h.Filters {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
fmt.Fprintf(w, "##FILTER=<ID=%s,Description=\"%s\">\n", k, h.Filters[k])
}
// Infos
keys = keys[:0]
for k := range h.Infos {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
fmt.Fprintf(w, "%s\n", h.Infos[k])
}
// SampleFormats
keys = keys[:0]
for k := range h.SampleFormats {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
fmt.Fprintf(w, "%s\n", h.SampleFormats[k])
}
for _, line := range h.Extras {
fmt.Fprintf(w, "%s\n", line)
}
fmt.Fprint(w, "#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\tFORMAT")
var s string
if len(h.SampleNames) > 0 {
s = "\t" + strings.Join(h.SampleNames, "\t")
}
fmt.Fprint(w, s+"\n")
return &Writer{w, h}, nil
}
// WriteVariant writes a single variant
func (w *Writer) WriteVariant(v *Variant) {
fmt.Fprintln(w, v)
}