-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcigar.go
75 lines (68 loc) · 1.55 KB
/
cigar.go
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
package bigly
import (
"log"
"github.com/biogo/hts/sam"
)
// RefPieces returns an array of start, end pairs of places where the given Cigar covers the reference.
// E.g. 6, 8M2I4M1D3M will return {6, 18, 19, 22} meaning 6-18, 19-22.
func RefPieces(pos int, c sam.Cigar) []int {
if len(c) == 1 && c[0].Type() == sam.CigarMatch {
return []int{pos, pos + c[0].Len()}
}
p := make([]int, 0, 4)
for _, co := range c {
con := co.Type().Consumes()
if con.Reference > 0 {
if con.Query != 0 {
if len(p) == 0 || pos != p[len(p)-1] {
p = append(p, pos)
p = append(p, pos+co.Len())
} else {
p[len(p)-1] = pos + co.Len()
}
}
pos += co.Len()
}
}
return p
}
// ReadPieces gives the offsets into the cigar that consome reference bases.
func ReadPieces(c sam.Cigar) []int {
if len(c) == 1 && c[0].Type() == sam.CigarMatch {
return []int{0, c[0].Len()}
}
p := make([]int, 0, 4)
off := 0
for _, co := range c {
con := co.Type().Consumes()
if con.Query != 0 && con.Reference != 0 {
if len(p) == 0 || off != p[len(p)-1] {
p = append(p, off)
p = append(p, off+co.Len())
} else {
p[len(p)-1] = off + co.Len()
}
}
if con.Query != 0 {
off += co.Len()
}
}
return p
}
// FirstMatch reports the first base in the read that matches the reference.
func FirstMatch(c sam.Cigar) int {
start := 0
if c == nil {
log.Fatal("no cigar to parse")
}
for _, co := range c {
if co.Type() == sam.CigarMatch {
return start
}
con := co.Type().Consumes()
if con.Query > 0 {
start += co.Len()
}
}
return start
}