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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
package shared
import (
"fmt"
"io/ioutil"
"log"
"strings"
. "github.com/brentp/vcfanno/api"
"github.com/shenwei356/xopen"
)
type Config struct {
Annotation []Annotation
PostAnnotation []PostAnnotation
// base path to prepend to all files.
Base string
}
// Annotation holds information about the annotation files parsed from the toml config.
type Annotation struct {
File string
Ops []string
Fields []string
Columns []int
// the names in the output.
Names []string
}
// Flatten turns an annotation into a slice of Sources. Pass in the index of the file.
// having it as a Source makes the code cleaner, but it's simpler for the user to
// specify multiple ops per file in the toml config.
func (a *Annotation) Flatten(index int, basepath string) ([]*Source, error) {
if len(a.Ops) == 0 {
if !strings.HasSuffix(a.File, ".bam") {
return nil, fmt.Errorf("no ops specified for %s\n", a.File)
}
// auto-fill bam to count.
a.Ops = make([]string, len(a.Names))
for i := range a.Names {
a.Ops[i] = "sum"
}
}
if len(a.Columns) == 0 && len(a.Fields) == 0 {
if !strings.HasSuffix(a.File, ".bam") {
return nil, fmt.Errorf("no columns or fields specified for %s\n", a.File)
}
if len(a.Fields) == 0 {
a.Columns = make([]int, len(a.Names))
for i := range a.Names {
a.Columns[i] = 1
}
}
}
if !(xopen.Exists(a.File) || a.File == "-") {
if basepath != "" {
a.File = basepath + "/" + a.File
}
if !(xopen.Exists(a.File) || a.File == "-") {
return nil, fmt.Errorf("[Flatten] unable to open file: %s in %s\n", a.File, basepath)
}
}
n := len(a.Ops)
sources := make([]*Source, n)
for i := 0; i < n; i++ {
isLua := strings.HasPrefix(a.Ops[i], "lua:")
if !isLua {
if len(a.Fields) > i && a.Fields[i] == "DP2" {
if !strings.HasSuffix(a.File, ".bam") {
log.Fatal("DP2 only valid for bams")
}
// always set Op to DP2 whne Field is DP2
a.Ops[i] = "DP2"
}
if _, ok := Reducers[a.Ops[i]]; !ok {
return nil, fmt.Errorf("requested op not found: %s for %s\n", a.Ops[i], a.File)
}
}
op := a.Ops[i]
if len(a.Names) == 0 {
a.Names = a.Fields
}
sources[i] = &Source{File: a.File, Op: op, Name: a.Names[i], Index: index}
if nil != a.Fields {
sources[i].Field = a.Fields[i]
sources[i].Column = -1
} else {
sources[i].Column = a.Columns[i]
}
}
return sources, nil
}
func (c Config) Sources() ([]*Source, error) {
annos := c.Annotation
for i, a := range annos {
if !xopen.Exists(a.File) && a.File != "-" {
a.File = c.Base + "/" + a.File
annos[i] = a
}
}
var s []*Source
for i, a := range annos {
flats, err := a.Flatten(i, c.Base)
if err != nil {
return nil, err
}
s = append(s, flats...)
}
return s, nil
}
func CheckPostAnno(p *PostAnnotation) error {
if len(p.Fields) == 0 {
log.Println("warning: no specified 'fields' for postannotation:", p.Name)
}
if p.Op == "" {
return fmt.Errorf("must specify an 'op' for postannotation")
}
if p.Name == "" {
if p.Op != "delete" {
return fmt.Errorf("must specify a 'name' for postannotation")
}
}
if !(p.Type == "Float" || p.Type == "String" || p.Type == "Integer" || p.Type == "Flag") {
if p.Op != "delete" {
return fmt.Errorf("must specify a type for postannotation that is 'Flag', 'Float', 'Integer' or 'String'")
}
}
return nil
}
func CheckAnno(a *Annotation) error {
if strings.HasSuffix(a.File, ".bam") {
if nil == a.Columns && nil == a.Fields {
a.Columns = []int{1}
}
if nil == a.Ops {
a.Ops = []string{"count"}
}
}
if a.Fields == nil {
// Columns: BED/BAM
if a.Columns == nil {
return fmt.Errorf("must specify either 'fields' or 'columns' for %s", a.File)
}
if len(a.Ops) != len(a.Columns) && !strings.HasSuffix(a.File, ".bam") {
return fmt.Errorf("must specify same # of 'columns' as 'ops' for %s", a.File)
}
if len(a.Names) != len(a.Columns) && !strings.HasSuffix(a.File, ".bam") {
return fmt.Errorf("must specify same # of 'names' as 'ops' for %s", a.File)
}
} else {
// Fields: VCF
if a.Columns != nil {
if strings.HasSuffix(a.File, ".bam") {
a.Columns = make([]int, len(a.Ops))
} else {
return fmt.Errorf("specify only 'fields' or 'columns' not both %s", a.File)
}
}
if len(a.Ops) != len(a.Fields) {
return fmt.Errorf("must specify same # of 'fields' as 'ops' for %s", a.File)
}
}
if len(a.Names) == 0 {
a.Names = a.Fields
}
return nil
}
func ReadLua(lua string) string {
var luaString string
if lua != "" {
luaReader, err := xopen.Ropen(lua)
if err != nil {
log.Fatal(err)
}
luaBytes, err := ioutil.ReadAll(luaReader)
if err != nil {
log.Fatal(err)
}
luaString = string(luaBytes)
} else {
luaString = ""
}
return luaString
}
|