Skip to content

Commit

Permalink
add test for #20
Browse files Browse the repository at this point in the history
  • Loading branch information
brentp committed Sep 30, 2024
1 parent b340b4d commit 9b35bf3
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 23 deletions.
4 changes: 2 additions & 2 deletions header.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var contigRegexp = regexp.MustCompile(`contig=<.*((\w+)=([^,>]+))`)
var sampleRegexp = regexp.MustCompile(`SAMPLE=<ID=([^,>]+)`)
var pedRegexp = regexp.MustCompile(`PEDIGREE=<=([^,>]+)`)

//var headerIdRegexp = regexp.MustCompile(`##([^=]+)=<ID=([^,]+)`)
// var headerIdRegexp = regexp.MustCompile(`##([^=]+)=<ID=([^,]+)`)
var fileVersionRegexp = regexp.MustCompile(`##fileformat=VCFv(.+)`)

// Info holds the Info and Format fields
Expand Down Expand Up @@ -226,7 +226,7 @@ func parseHeaderExtraKV(kv string) ([]string, error) {
kvpair := strings.SplitN(kv, "=", 2)

if len(kvpair) != 2 {
kvpair = append(kvpair, "")
kvpair = append(kvpair, "")
return kvpair, fmt.Errorf("header error in extra field: %s", kv)
}
return kvpair, nil
Expand Down
37 changes: 19 additions & 18 deletions reader.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
// Package vcfgo implements a Reader and Writer for variant call format.
// It eases reading, filtering modifying VCF's even if they are not to spec.
// Example:
// f, _ := os.Open("examples/test.auto_dom.no_parents.vcf")
// rdr, err := vcfgo.NewReader(f)
// if err != nil {
// panic(err)
// }
// for {
// variant := rdr.Read()
// if variant == nil {
// break
// }
// fmt.Printf("%s\t%d\t%s\t%s\n", variant.Chromosome, variant.Pos, variant.Ref, variant.Alt)
// fmt.Printf("%s", variant.Info["DP"].(int) > 10)
// sample := variant.Samples[0]
// // we can get the PL field as a list (-1 is default in case of missing value)
// fmt.Println("%s", variant.GetGenotypeField(sample, "PL", -1))
// _ = sample.DP
// }
// fmt.Fprintln(os.Stderr, rdr.Error())
//
// f, _ := os.Open("examples/test.auto_dom.no_parents.vcf")
// rdr, err := vcfgo.NewReader(f)
// if err != nil {
// panic(err)
// }
// for {
// variant := rdr.Read()
// if variant == nil {
// break
// }
// fmt.Printf("%s\t%d\t%s\t%s\n", variant.Chromosome, variant.Pos, variant.Ref, variant.Alt)
// fmt.Printf("%s", variant.Info["DP"].(int) > 10)
// sample := variant.Samples[0]
// // we can get the PL field as a list (-1 is default in case of missing value)
// fmt.Println("%s", variant.GetGenotypeField(sample, "PL", -1))
// _ = sample.DP
// }
// fmt.Fprintln(os.Stderr, rdr.Error())
package vcfgo

import (
Expand Down
11 changes: 8 additions & 3 deletions variant.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,11 @@ type SampleGenotype struct {
Fields map[string]string
}

// String returns the string representation of the sample field.
func (s *SampleGenotype) String() string {
return fmt.Sprintf("GT: %v, DP: %d, GL: %v, GQ: %d, MQ: %d, Fields: %v", s.GT, s.DP, s.GL, s.GQ, s.MQ, s.Fields)
}

// RefDepth returns the depths of the alternates for this sample
func (s *SampleGenotype) RefDepth() (int, error) {
if ad, ok := s.Fields["AD"]; ok {
Expand Down Expand Up @@ -257,8 +262,8 @@ func (s *SampleGenotype) AltDepths() ([]int, error) {
return vals, nil
}

// String returns the string representation of the sample field.
func (sg *SampleGenotype) String(fields []string) string {
// ToString returns the string representation of the sample field.
func (sg *SampleGenotype) ToString(fields []string) string {
if len(fields) == 0 {
return "."
}
Expand Down Expand Up @@ -291,7 +296,7 @@ func (v *Variant) String() string {
if len(v.Samples) > 0 {
samps := make([]string, len(v.Samples))
for i, s := range v.Samples {
samps[i] = s.String(v.Format)
samps[i] = s.ToString(v.Format)
}
s += fmt.Sprintf("\t%s\t%s", strings.Join(v.Format, ":"), strings.Join(samps, "\t"))
} else if v.sampleString != "" {
Expand Down
23 changes: 23 additions & 0 deletions vcf_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package vcfgo

import (
"fmt"
"os"
"testing"

Expand Down Expand Up @@ -180,3 +181,25 @@ func (s *VCFSuite) TestWriterWithSamples(c *C) {
c.Assert(strings.HasPrefix(lines[len(lines)-1], "#CHROM"), Equals, true)

}
func (s *VCFSuite) TestIssue20SampleGenotypes(c *C) {
fname := "test-issue-20.vcf"
rdr, err := os.Open(fname)
c.Assert(err, IsNil)
defer rdr.Close()

vcf, err := NewReader(rdr, false)
c.Assert(err, IsNil)

variant := vcf.Read()
c.Assert(variant, NotNil)

samples := variant.Samples
c.Assert(len(samples), Equals, 3)
// print samples
fmt.Printf("%+v\n", samples)

// Check genotypes for each sample
c.Assert(samples[0].GT, DeepEquals, []int{1, 1})
c.Assert(samples[1].GT, DeepEquals, []int{0, 1})
c.Assert(samples[2].GT, DeepEquals, []int{-1, -1})
}

0 comments on commit 9b35bf3

Please sign in to comment.