| 1 | package parsers |
| 2 | |
| 3 | import ( |
| 4 | "io" |
| 5 | |
| 6 | "github.com/brentp/irelate/interfaces" |
| 7 | "github.com/brentp/vcfgo" |
| 8 | ) |
| 9 | |
| 10 | type Variant struct { |
| 11 | interfaces.IVariant |
| 12 | source uint32 |
| 13 | related []interfaces.Relatable |
| 14 | } |
| 15 | |
| 16 | func (v *Variant) String() string { |
| 17 | return v.IVariant.String() |
| 18 | } |
| 19 | |
| 20 | func NewVariant(v interfaces.IVariant, source uint32, related []interfaces.Relatable) *Variant { |
| 21 | return &Variant{v, source, related} |
| 22 | } |
| 23 | |
| 24 | func (v *Variant) AddRelated(r interfaces.Relatable) { |
| 25 | if len(v.related) == 0 { |
| 26 | v.related = make([]interfaces.Relatable, 0, 2) |
| 27 | } |
| 28 | v.related = append(v.related, r) |
| 29 | } |
| 30 | |
| 31 | func (v *Variant) Related() []interfaces.Relatable { |
| 32 | return v.related |
| 33 | } |
| 34 | |
| 35 | func (v *Variant) SetSource(src uint32) { v.source = src } |
| 36 | func (v *Variant) Source() uint32 { return v.source } |
| 37 | |
| 38 | func Vopen(rdr io.Reader, hdr *vcfgo.Header) (*vcfgo.Reader, error) { |
| 39 | if hdr == nil { |
| 40 | return vcfgo.NewReader(rdr, true) |
| 41 | } |
| 42 | return vcfgo.NewWithHeader(rdr, hdr, true) |
| 43 | } |
| 44 | |
| 45 | func StreamVCF(vcf *vcfgo.Reader) interfaces.RelatableChannel { |
| 46 | ch := make(interfaces.RelatableChannel, 256) |
| 47 | go func() { |
| 48 | j := 0 |
| 49 | for { |
| 50 | v := vcf.Read() |
| 51 | if v == nil { |
| 52 | break |
| 53 | } |
| 54 | ch <- &Variant{v, 0, nil} |
| 55 | j++ |
| 56 | if j < 1000 { |
| 57 | vcf.Clear() |
| 58 | j = 0 |
| 59 | } |
| 60 | } |
| 61 | close(ch) |
| 62 | }() |
| 63 | return ch |
| 64 | } |
| 65 | |
| 66 | type vWrapper struct { |
| 67 | *vcfgo.Reader |
| 68 | } |
| 69 | |
| 70 | func (v vWrapper) Next() (interfaces.Relatable, error) { |
| 71 | r := v.Read() |
| 72 | if r == nil { |
| 73 | return nil, io.EOF |
| 74 | } |
| 75 | return &Variant{r, 0, nil}, nil |
| 76 | } |
| 77 | |
| 78 | func (v vWrapper) AddInfoToHeader(id, itype, number, description string) { |
| 79 | v.Reader.AddInfoToHeader(id, itype, number, description) |
| 80 | } |
| 81 | |
| 82 | func VCFIterator(buf io.Reader) (interfaces.RelatableIterator, *vcfgo.Reader, error) { |
| 83 | v, err := Vopen(buf, nil) |
| 84 | if err != nil { |
| 85 | return nil, v, err |
| 86 | } |
| 87 | return vWrapper{v}, v, nil |
| 88 | } |