Skip to content

Commit

Permalink
allow postannotation to set the ID column.
Browse files Browse the repository at this point in the history
closes #51
  • Loading branch information
brentp committed Feb 24, 2017
1 parent db733d2 commit b8ce754
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 12 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,18 @@ indicate the fields (from the INFO) that will be available to the op, and the *o
powerful. For an extensive example that demonstrates the utility of this type of approach, see
[docs/examples/clinvar_exac.md](http://brentp.github.io/vcfanno/examples/clinvar_exac/).

A user can set the ID field of the VCF in a `[[postannotation]]` block by using `name=ID`. For example:

```
[[postannotation]]
name="ID"
fields=["other_field", "ID"]
op="lua:other_field .. ';' .. ID"
type="String"
```

will take the value in `other_field`, concatenate it with the existing ID, and set the ID to that value.


Binaries
========
Expand Down
32 changes: 23 additions & 9 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,12 +423,13 @@ func (s *Source) UpdateHeader(r HeaderUpdater, ends bool, htype string, number s
}

// PostAnnotate happens after everything is done.
func (a *Annotator) PostAnnotate(chrom string, start int, end int, info interfaces.Info, prefix string) error {
func (a *Annotator) PostAnnotate(chrom string, start int, end int, info interfaces.Info, prefix string, id string) (error, string) {
var e, err error
vals := make([]interface{}, 0, 2)
fields := make([]string, 0, 2)
missing := make([]string, 0, 2)
var val interface{}
newid := ""
for i := range a.PostAnnos {
post := a.PostAnnos[i]
vals = vals[:0]
Expand All @@ -437,7 +438,11 @@ func (a *Annotator) PostAnnotate(chrom string, start int, end int, info interfac
// lua code
if post.code != "" {
for _, field := range post.Fields {
val, e = info.Get(field)
if field == "ID" {
val = id
} else {
val, e = info.Get(field)
}
if val != nil {
vals = append(vals, val)
fields = append(fields, field)
Expand Down Expand Up @@ -516,7 +521,9 @@ func (a *Annotator) PostAnnotate(chrom string, start int, end int, info interfac
}

} else {
if e := info.Set(prefix+post.Name, val); e != nil {
if post.Name == "ID" && prefix == "" {
newid = val
} else if e := info.Set(prefix+post.Name, val); e != nil {
err = e
}
}
Expand All @@ -539,12 +546,15 @@ func (a *Annotator) PostAnnotate(chrom string, start int, end int, info interfac
// run this as long as we found any of the values.
if len(vals) != 0 {
fn := Reducers[post.Op]
info.Set(prefix+post.Name, fn(vals))
if post.Name == "ID" && prefix == "" {
newid = fmt.Sprintf("%s", fn(vals))
} else {
info.Set(prefix+post.Name, fn(vals))
}
}
}

}
return err
return err, newid
}

// Setup reads all the tabix indexes and setups up the Queryables
Expand Down Expand Up @@ -624,11 +634,12 @@ func (a *Annotator) AnnotateEnds(v interfaces.Relatable, ends string) error {

var err error
// if Both, call the interval, left, and right version to annotate.
id := v.(*parsers.Variant).IVariant.(*vcfgo.Variant).Id()
if ends == BOTH {
if e := a.AnnotateOne(v, a.Strict); e != nil {
err = e
}
if e := a.PostAnnotate(v.Chrom(), int(v.Start()), int(v.End()), v.(interfaces.IVariant).Info(), ""); e != nil {
if e, _ := a.PostAnnotate(v.Chrom(), int(v.Start()), int(v.End()), v.(interfaces.IVariant).Info(), "", id); e != nil {
err = e
}
if e := a.AnnotateEnds(v, LEFT); e != nil {
Expand All @@ -640,10 +651,13 @@ func (a *Annotator) AnnotateEnds(v interfaces.Relatable, ends string) error {
}
if ends == INTERVAL {
err := a.AnnotateOne(v, a.Strict)
err2 := a.PostAnnotate(v.Chrom(), int(v.Start()), int(v.End()), v.(interfaces.IVariant).Info(), "")
err2, newid := a.PostAnnotate(v.Chrom(), int(v.Start()), int(v.End()), v.(interfaces.IVariant).Info(), "", id)
if err != nil {
return err
}
if err2 == nil && newid != "" {
v.(*parsers.Variant).IVariant.(*vcfgo.Variant).Id_ = newid
}
return err2
}
// hack:
Expand Down Expand Up @@ -677,7 +691,7 @@ func (a *Annotator) AnnotateEnds(v interfaces.Relatable, ends string) error {
val, err = v2.Info().Get(key)
variant.Info().Set(key, val)
}
err2 := a.PostAnnotate(v.Chrom(), int(l), int(r), variant.Info(), ends)
err2, _ := a.PostAnnotate(v.Chrom(), int(l), int(r), variant.Info(), ends, id)
if err2 != nil {
err = err2
}
Expand Down
Binary file modified tests/dbnsfp/Calls_for_dbNSFP_example.vcf.gz
Binary file not shown.
6 changes: 6 additions & 0 deletions tests/dbnsfp/conf.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ file="dbNSFP_ex.txt.gz"
columns=[1,2,4,5]
names=["nschrom", "nspos", "nsref", "nsalt"]
ops=["first", "max", "uniq", "uniq"]

[[postannotation]]
name="ID"
fields=["culprit", "ID"]
op="lua:culprit .. ';' .. ID"
type="String"
Binary file added tests/dbnsfp/dbNSFP_example.txt.gz
Binary file not shown.
Binary file added tests/dbnsfp/dbNSFP_example.txt.gz.tbi
Binary file not shown.
6 changes: 4 additions & 2 deletions tests/functional-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,20 @@ assert_equal 3 $(grep -c ";right_ref_alt=A" $STDOUT_FILE)
rm e.lua
irefalt() {
vcfanno -permissive-overlap -base-path tests/dbnsfp/ tests/dbnsfp/conf.toml tests/dbnsfp/Calls_for_dbNSFP_example.vcf.gz | grep -v ^#
vcfanno -lua <(echo "") -permissive-overlap -base-path tests/dbnsfp/ tests/dbnsfp/conf.toml tests/dbnsfp/Calls_for_dbNSFP_example.vcf.gz | grep -v ^#
}
run check_iref_alt irefalt
assert_in_stdout "nsalt=A,G,T"
assert_exit_code 0
irefalt_strict() {
vcfanno -base-path tests/dbnsfp/ tests/dbnsfp/conf.toml tests/dbnsfp/Calls_for_dbNSFP_example.vcf.gz | grep -v ^#
vcfanno -lua <(echo "") -base-path tests/dbnsfp/ tests/dbnsfp/conf.toml tests/dbnsfp/Calls_for_dbNSFP_example.vcf.gz | grep -v ^#
}
run check_iref_alt_strict irefalt_strict
assert_in_stdout $'nsalt=T\t'
# check that ID was set.
assert_in_stdout $'\tReadPosRankSum;ORIGID\t'
assert_exit_code 0
Expand Down
2 changes: 1 addition & 1 deletion vcfanno.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"github.com/brentp/xopen"
)

var VERSION = "0.1.1"
var VERSION = "0.1.2-dev"

func envGet(name string, vdefault int) int {
sval := os.Getenv(name)
Expand Down

0 comments on commit b8ce754

Please sign in to comment.