| 1 |
|
| 2 |
package bix |
| 3 |
|
| 4 |
import ( |
| 5 |
"bytes" |
| 6 |
"encoding/binary" |
| 7 |
"io" |
| 8 |
"strings" |
| 9 |
|
| 10 |
"github.com/biogo/hts/bgzf" |
| 11 |
"github.com/biogo/hts/csi" |
| 12 |
"github.com/biogo/hts/tabix" |
| 13 |
) |
| 14 |
|
| 15 |
|
| 16 |
type Index interface { |
| 17 |
Chunks(string, int, int) ([]bgzf.Chunk, error) |
| 18 |
NameColumn() int |
| 19 |
BeginColumn() int |
| 20 |
EndColumn() int |
| 21 |
ZeroBased() bool |
| 22 |
MetaChar() rune |
| 23 |
Skip() int |
| 24 |
} |
| 25 |
|
| 26 |
type tIndex struct{ *tabix.Index } |
| 27 |
|
| 28 |
func (t tIndex) NameColumn() int { |
| 29 |
return int(t.Index.NameColumn) |
| 30 |
} |
| 31 |
func (t tIndex) BeginColumn() int { |
| 32 |
return int(t.Index.BeginColumn) |
| 33 |
} |
| 34 |
|
| 35 |
func (t tIndex) EndColumn() int { |
| 36 |
return int(t.Index.EndColumn) |
| 37 |
} |
| 38 |
|
| 39 |
func (t tIndex) ZeroBased() bool { |
| 40 |
return t.Index.ZeroBased |
| 41 |
} |
| 42 |
|
| 43 |
func (t tIndex) MetaChar() rune { |
| 44 |
return t.Index.MetaChar |
| 45 |
} |
| 46 |
|
| 47 |
func (t tIndex) Skip() int { |
| 48 |
return int(t.Index.Skip) |
| 49 |
} |
| 50 |
|
| 51 |
type cIndex struct { |
| 52 |
*csi.Index |
| 53 |
chroms []string |
| 54 |
nameColumn int |
| 55 |
beginColumn int |
| 56 |
endColumn int |
| 57 |
metaChar rune |
| 58 |
zeroBased bool |
| 59 |
skip int |
| 60 |
} |
| 61 |
|
| 62 |
func (c cIndex) NameColumn() int { |
| 63 |
return c.nameColumn |
| 64 |
} |
| 65 |
func (c cIndex) BeginColumn() int { |
| 66 |
return c.beginColumn |
| 67 |
} |
| 68 |
|
| 69 |
func (c cIndex) EndColumn() int { |
| 70 |
return c.endColumn |
| 71 |
} |
| 72 |
|
| 73 |
func (c cIndex) ZeroBased() bool { |
| 74 |
return c.zeroBased |
| 75 |
} |
| 76 |
|
| 77 |
func (c cIndex) MetaChar() rune { |
| 78 |
return c.metaChar |
| 79 |
} |
| 80 |
|
| 81 |
func (c cIndex) Skip() int { |
| 82 |
return c.skip |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
func stripChr(c string) string { |
| 87 |
if strings.HasPrefix(c, "chr") { |
| 88 |
return c[3:] |
| 89 |
} |
| 90 |
return c |
| 91 |
} |
| 92 |
|
| 93 |
func (c cIndex) Chunks(chrom string, start int, end int) ([]bgzf.Chunk, error) { |
| 94 |
idx := -1 |
| 95 |
chrom = stripChr(chrom) |
| 96 |
for i, ichrom := range c.chroms { |
| 97 |
if stripChr(ichrom) == chrom { |
| 98 |
idx = i |
| 99 |
break |
| 100 |
} |
| 101 |
|
| 102 |
} |
| 103 |
return c.Index.Chunks(idx, start, end), nil |
| 104 |
} |
| 105 |
|
| 106 |
func NewCSI(r io.Reader) (cIndex, error) { |
| 107 |
c, err := csi.ReadFrom(r) |
| 108 |
ci := cIndex{Index: c} |
| 109 |
if err != nil { |
| 110 |
return ci, err |
| 111 |
} |
| 112 |
aux := c.Auxilliary |
| 113 |
|
| 114 |
ci.nameColumn = int(binary.LittleEndian.Uint32(aux[4:8])) |
| 115 |
ci.beginColumn = int(binary.LittleEndian.Uint32(aux[8:12])) |
| 116 |
ci.endColumn = int(binary.LittleEndian.Uint32(aux[12:16])) |
| 117 |
ci.metaChar = rune(binary.LittleEndian.Uint32(aux[16:20])) |
| 118 |
ci.skip = int(binary.LittleEndian.Uint32(aux[20:24])) |
| 119 |
|
| 120 |
L := int(binary.LittleEndian.Uint32(aux[24:28])) |
| 121 |
|
| 122 |
chroms := bytes.Split(aux[28:28+L-1], []byte{0}) |
| 123 |
ci.chroms = make([]string, len(chroms)) |
| 124 |
for i, chrom := range chroms { |
| 125 |
ci.chroms[i] = string(chrom) |
| 126 |
} |
| 127 |
return ci, nil |
| 128 |
} |