| 1 |
[](https://godoc.org/github.com/brentp/vcfgo) |
| 2 |
[](https://github.com/brentp/vcfgo/actions/workflows/tests.yml) |
| 3 |
[](https://coveralls.io/r/brentp/vcfgo) |
| 4 |
|
| 5 |
vcfgo is a golang library to read, write and manipulate files in the variant call format. |
| 6 |
|
| 7 |
# vcfgo |
| 8 |
-- |
| 9 |
import "github.com/brentp/vcfgo" |
| 10 |
|
| 11 |
Package vcfgo implements a Reader and Writer for variant call format. It eases |
| 12 |
reading, filtering modifying VCF's even if they are not to spec. Example: |
| 13 |
|
| 14 |
## Usage |
| 15 |
|
| 16 |
```go |
| 17 |
f, _ := os.Open("examples/test.auto_dom.no_parents.vcf") |
| 18 |
rdr, err := vcfgo.NewReader(f, false) |
| 19 |
if err != nil { |
| 20 |
panic(err) |
| 21 |
} |
| 22 |
for { |
| 23 |
variant := rdr.Read() |
| 24 |
if variant == nil { |
| 25 |
break |
| 26 |
} |
| 27 |
fmt.Printf("%s\t%d\t%s\t%v\n", variant.Chromosome, variant.Pos, variant.Ref(), variant.Alt()) |
| 28 |
dp, err := variant.Info().Get("DP") |
| 29 |
fmt.Printf("depth: %v\n", dp.(int)) |
| 30 |
sample := variant.Samples[0] |
| 31 |
// we can get the PL field as a list (-1 is default in case of missing value) |
| 32 |
PL, err := variant.GetGenotypeField(sample, "PL", -1) |
| 33 |
if err != nil { |
| 34 |
panic(err) |
| 35 |
} |
| 36 |
fmt.Printf("%v\n", PL) |
| 37 |
_ = sample.DP |
| 38 |
} |
| 39 |
fmt.Fprintln(os.Stderr, rdr.Error()) |
| 40 |
|
| 41 |
``` |
| 42 |
|
| 43 |
## Status |
| 44 |
|
| 45 |
`vcfgo` is well-tested, but still in development. It tries to tolerate, but report |
| 46 |
errors; after every `rdr.Read()` call, the caller can check `rdr.Error()` |
| 47 |
and get feedback on the errors without stopping execution unless it is explicitly |
| 48 |
requested to do so. |
| 49 |
|
| 50 |
Info and sample fields are pre-parsed and stored as `map[string]interface{}` so |
| 51 |
callers will have to cast to the appropriate type upon retrieval. |
| 52 |
|
| 53 |
#### type Header |
| 54 |
|
| 55 |
```go |
| 56 |
type Header struct { |
| 57 |
SampleNames []string |
| 58 |
Infos map[string]*Info |
| 59 |
SampleFormats map[string]*SampleFormat |
| 60 |
Filters map[string]string |
| 61 |
Extras map[string]string |
| 62 |
FileFormat string |
| 63 |
// contid id maps to a map of length, URL, etc. |
| 64 |
Contigs map[string]map[string]string |
| 65 |
} |
| 66 |
``` |
| 67 |
|
| 68 |
Header holds all the type and format information for the variants. |
| 69 |
|
| 70 |
#### func NewHeader |
| 71 |
|
| 72 |
```go |
| 73 |
func NewHeader() *Header |
| 74 |
``` |
| 75 |
NewHeader returns a Header with the requisite allocations. |
| 76 |
|
| 77 |
#### type Info |
| 78 |
|
| 79 |
```go |
| 80 |
type Info struct { |
| 81 |
Id string |
| 82 |
Description string |
| 83 |
Number string // A G R . '' |
| 84 |
Type string // STRING INTEGER FLOAT FLAG CHARACTER UNKONWN |
| 85 |
} |
| 86 |
``` |
| 87 |
|
| 88 |
Info holds the Info and Format fields |
| 89 |
|
| 90 |
#### func (*Info) String |
| 91 |
|
| 92 |
```go |
| 93 |
func (i *Info) String() string |
| 94 |
``` |
| 95 |
String returns a string representation. |
| 96 |
|
| 97 |
#### type InfoMap |
| 98 |
|
| 99 |
```go |
| 100 |
type InfoMap map[string]interface{} |
| 101 |
``` |
| 102 |
|
| 103 |
InfoMap holds the parsed Info field which can contain floats, ints and lists |
| 104 |
thereof. |
| 105 |
|
| 106 |
#### func (InfoMap) String |
| 107 |
|
| 108 |
```go |
| 109 |
func (m InfoMap) String() string |
| 110 |
``` |
| 111 |
String returns a string that matches the original info field. |
| 112 |
|
| 113 |
#### type Reader |
| 114 |
|
| 115 |
```go |
| 116 |
type Reader struct { |
| 117 |
Header *Header |
| 118 |
|
| 119 |
LineNumber int64 |
| 120 |
} |
| 121 |
``` |
| 122 |
|
| 123 |
Reader holds information about the current line number (for errors) and The VCF |
| 124 |
header that indicates the structure of records. |
| 125 |
|
| 126 |
#### func NewReader |
| 127 |
|
| 128 |
```go |
| 129 |
func NewReader(r io.Reader, lazySamples bool) (*Reader, error) |
| 130 |
``` |
| 131 |
NewReader returns a Reader. |
| 132 |
|
| 133 |
#### func (*Reader) Clear |
| 134 |
|
| 135 |
```go |
| 136 |
func (vr *Reader) Clear() |
| 137 |
``` |
| 138 |
Clear empties the cache of errors. |
| 139 |
|
| 140 |
#### func (*Reader) Error |
| 141 |
|
| 142 |
```go |
| 143 |
func (vr *Reader) Error() error |
| 144 |
``` |
| 145 |
Error() aggregates the multiple errors that can occur into a single object. |
| 146 |
|
| 147 |
#### func (*Reader) Read |
| 148 |
|
| 149 |
```go |
| 150 |
func (vr *Reader) Read() *Variant |
| 151 |
``` |
| 152 |
Read returns a pointer to a Variant. Upon reading the caller is assumed to check |
| 153 |
Reader.Err() |
| 154 |
|
| 155 |
#### type SampleFormat |
| 156 |
|
| 157 |
```go |
| 158 |
type SampleFormat Info |
| 159 |
``` |
| 160 |
|
| 161 |
SampleFormat holds the type info for Format fields. |
| 162 |
|
| 163 |
#### func (*SampleFormat) String |
| 164 |
|
| 165 |
```go |
| 166 |
func (i *SampleFormat) String() string |
| 167 |
``` |
| 168 |
String returns a string representation. |
| 169 |
|
| 170 |
#### type SampleGenotype |
| 171 |
|
| 172 |
```go |
| 173 |
type SampleGenotype struct { |
| 174 |
Phased bool |
| 175 |
GT []int |
| 176 |
DP int |
| 177 |
GL []float32 |
| 178 |
GQ int |
| 179 |
MQ int |
| 180 |
Fields map[string]string |
| 181 |
} |
| 182 |
``` |
| 183 |
|
| 184 |
SampleGenotype holds the information about a sample. Several fields are |
| 185 |
pre-parsed, but all fields are kept in Fields as well. |
| 186 |
|
| 187 |
#### func NewSampleGenotype |
| 188 |
|
| 189 |
```go |
| 190 |
func NewSampleGenotype() *SampleGenotype |
| 191 |
``` |
| 192 |
NewSampleGenotype allocates the internals and returns a SampleGenotype |
| 193 |
|
| 194 |
#### func (*SampleGenotype) String |
| 195 |
|
| 196 |
```go |
| 197 |
func (sg *SampleGenotype) String(fields []string) string |
| 198 |
``` |
| 199 |
String returns the string representation of the sample field. |
| 200 |
|
| 201 |
#### type VCFError |
| 202 |
|
| 203 |
```go |
| 204 |
type VCFError struct { |
| 205 |
Msgs []string |
| 206 |
Lines []int64 |
| 207 |
} |
| 208 |
``` |
| 209 |
|
| 210 |
VCFError satisfies the error interface and allows multiple errors. This is |
| 211 |
useful because, for example, on a single line, every sample may have a field |
| 212 |
that doesn't match the description in the header. We want to keep parsing but |
| 213 |
also let the caller know about the error. |
| 214 |
|
| 215 |
#### func NewVCFError |
| 216 |
|
| 217 |
```go |
| 218 |
func NewVCFError() *VCFError |
| 219 |
``` |
| 220 |
NewVCFError allocates the needed ingredients. |
| 221 |
|
| 222 |
#### func (*VCFError) Add |
| 223 |
|
| 224 |
```go |
| 225 |
func (e *VCFError) Add(err error, line int64) |
| 226 |
``` |
| 227 |
Add adds an error and the line number within the vcf where the error took place. |
| 228 |
|
| 229 |
#### func (*VCFError) Clear |
| 230 |
|
| 231 |
```go |
| 232 |
func (e *VCFError) Clear() |
| 233 |
``` |
| 234 |
Clear empties the Messages |
| 235 |
|
| 236 |
#### func (*VCFError) Error |
| 237 |
|
| 238 |
```go |
| 239 |
func (e *VCFError) Error() string |
| 240 |
``` |
| 241 |
Error returns a string with all errors delimited by newlines. |
| 242 |
|
| 243 |
#### func (*VCFError) IsEmpty |
| 244 |
|
| 245 |
```go |
| 246 |
func (e *VCFError) IsEmpty() bool |
| 247 |
``` |
| 248 |
IsEmpty returns true if there no errors stored. |
| 249 |
|
| 250 |
#### type Variant |
| 251 |
|
| 252 |
```go |
| 253 |
type Variant struct { |
| 254 |
Chromosome string |
| 255 |
Pos uint64 |
| 256 |
Id string |
| 257 |
Ref string |
| 258 |
Alt []string |
| 259 |
Quality float32 |
| 260 |
Filter string |
| 261 |
Info InfoMap |
| 262 |
Format []string |
| 263 |
Samples []*SampleGenotype |
| 264 |
Header *Header |
| 265 |
LineNumber int64 |
| 266 |
} |
| 267 |
``` |
| 268 |
|
| 269 |
Variant holds the information about a single site. It is analagous to a row in a |
| 270 |
VCF file. |
| 271 |
|
| 272 |
#### func (*Variant) GetGenotypeField |
| 273 |
|
| 274 |
```go |
| 275 |
func (v *Variant) GetGenotypeField(g *SampleGenotype, field string, missing interface{}) (interface{}, error) |
| 276 |
``` |
| 277 |
GetGenotypeField uses the information from the header to parse the correct time |
| 278 |
from a genotype field. It returns an interface that can be asserted to the |
| 279 |
expected type. |
| 280 |
|
| 281 |
#### func (*Variant) String |
| 282 |
|
| 283 |
```go |
| 284 |
func (v *Variant) String() string |
| 285 |
``` |
| 286 |
String gives a string representation of a variant |
| 287 |
|
| 288 |
#### type Writer |
| 289 |
|
| 290 |
```go |
| 291 |
type Writer struct { |
| 292 |
io.Writer |
| 293 |
Header *Header |
| 294 |
} |
| 295 |
``` |
| 296 |
|
| 297 |
Writer allows writing VCF files. |
| 298 |
|
| 299 |
#### func NewWriter |
| 300 |
|
| 301 |
```go |
| 302 |
func NewWriter(w io.Writer, h *Header) (*Writer, error) |
| 303 |
``` |
| 304 |
NewWriter returns a writer after writing the header. |
| 305 |
|
| 306 |
#### func (*Writer) WriteVariant |
| 307 |
|
| 308 |
```go |
| 309 |
func (w *Writer) WriteVariant(v *Variant) |
| 310 |
``` |
| 311 |
WriteVariant writes a single variant |