| 1 |
package parsers |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"strconv" |
| 6 |
"strings" |
| 7 |
"unsafe" |
| 8 |
|
| 9 |
"github.com/brentp/irelate/interfaces" |
| 10 |
) |
| 11 |
|
| 12 |
const empty = "" |
| 13 |
|
| 14 |
|
| 15 |
type Interval struct { |
| 16 |
|
| 17 |
chrom string |
| 18 |
start uint32 |
| 19 |
end uint32 |
| 20 |
source uint32 |
| 21 |
Fields [][]byte |
| 22 |
related []interfaces.Relatable |
| 23 |
} |
| 24 |
|
| 25 |
func NewInterval(chrom string, start uint32, end uint32, fields [][]byte, source uint32, related []interfaces.Relatable) *Interval { |
| 26 |
return &Interval{chrom: chrom, start: start, end: end, Fields: fields, source: source, related: related} |
| 27 |
} |
| 28 |
|
| 29 |
func (i *Interval) Chrom() string { return i.chrom } |
| 30 |
func (i *Interval) Start() uint32 { return i.start } |
| 31 |
func (i *Interval) End() uint32 { return i.end } |
| 32 |
func (i *Interval) Related() []interfaces.Relatable { return i.related } |
| 33 |
func (i *Interval) AddRelated(b interfaces.Relatable) { |
| 34 |
i.related = append(i.related, b) |
| 35 |
} |
| 36 |
|
| 37 |
func (i *Interval) Source() uint32 { return i.source } |
| 38 |
func (i *Interval) SetSource(src uint32) { i.source = src } |
| 39 |
|
| 40 |
func (i *Interval) String() string { |
| 41 |
return string(bytes.Join(i.Fields, []byte{'\t'})) |
| 42 |
} |
| 43 |
|
| 44 |
func unsafeString(b []byte) string { |
| 45 |
return *(*string)(unsafe.Pointer(&b)) |
| 46 |
} |
| 47 |
|
| 48 |
func IntervalFromBedLine(line []byte) (interfaces.Relatable, error) { |
| 49 |
line = bytes.TrimRight(line, "\r\n") |
| 50 |
fields := bytes.Split([]byte(string(line)), []byte{'\t'}) |
| 51 |
start, err := strconv.ParseUint(unsafeString(fields[1]), 10, 32) |
| 52 |
if err != nil { |
| 53 |
return nil, err |
| 54 |
} |
| 55 |
end, err := strconv.ParseUint(unsafeString(fields[2]), 10, 32) |
| 56 |
if err != nil { |
| 57 |
return nil, err |
| 58 |
} |
| 59 |
i := Interval{chrom: string(fields[0]), start: uint32(start), end: uint32(end), related: nil, Fields: fields} |
| 60 |
return &i, nil |
| 61 |
} |
| 62 |
|
| 63 |
type RefAltInterval struct { |
| 64 |
Interval |
| 65 |
refalt [2]int |
| 66 |
HasEnd bool |
| 67 |
} |
| 68 |
|
| 69 |
func (i *RefAltInterval) SetRefAlt(ra []int) { |
| 70 |
i.refalt[0] = ra[0] |
| 71 |
i.refalt[1] = ra[1] |
| 72 |
} |
| 73 |
|
| 74 |
func (i *RefAltInterval) Ref() string { |
| 75 |
return string(i.Fields[i.refalt[0]]) |
| 76 |
} |
| 77 |
|
| 78 |
func (i *RefAltInterval) Alt() []string { |
| 79 |
f := string(i.Fields[i.refalt[1]]) |
| 80 |
return strings.Split(f, ",") |
| 81 |
} |
| 82 |
|
| 83 |
func (i *RefAltInterval) End() uint32 { |
| 84 |
if i.HasEnd { |
| 85 |
return i.Interval.End() |
| 86 |
} |
| 87 |
return i.Start() + uint32(len(i.Ref())) |
| 88 |
} |
| 89 |
|
| 90 |
var _ interfaces.IRefAlt = (*RefAltInterval)(nil) |