| 1 | // Implements Relatable for Bam |
| 2 | |
| 3 | package parsers |
| 4 | |
| 5 | import ( |
| 6 | "fmt" |
| 7 | "io" |
| 8 | "log" |
| 9 | "os" |
| 10 | "strings" |
| 11 | |
| 12 | "github.com/biogo/hts/bam" |
| 13 | "github.com/biogo/hts/bgzf/index" |
| 14 | "github.com/biogo/hts/sam" |
| 15 | "github.com/brentp/irelate/interfaces" |
| 16 | ) |
| 17 | |
| 18 | type Bam struct { |
| 19 | *sam.Record |
| 20 | source uint32 |
| 21 | related []interfaces.Relatable |
| 22 | Chromosome string |
| 23 | _end uint32 |
| 24 | } |
| 25 | |
| 26 | func (a *Bam) Chrom() string { |
| 27 | return a.Chromosome |
| 28 | } |
| 29 | |
| 30 | // cast to 32 bits. |
| 31 | func (a *Bam) Start() uint32 { |
| 32 | return uint32(a.Record.Start()) |
| 33 | } |
| 34 | |
| 35 | func (a *Bam) End() uint32 { |
| 36 | if a._end != 0 { |
| 37 | return a._end |
| 38 | } |
| 39 | a._end = uint32(a.Record.End()) |
| 40 | return a._end |
| 41 | } |
| 42 | |
| 43 | func (a *Bam) Source() uint32 { |
| 44 | return a.source |
| 45 | } |
| 46 | |
| 47 | func (a *Bam) SetSource(src uint32) { |
| 48 | a.source = src |
| 49 | } |
| 50 | |
| 51 | func (a *Bam) AddRelated(b interfaces.Relatable) { |
| 52 | if a.related == nil { |
| 53 | a.related = make([]interfaces.Relatable, 1, 2) |
| 54 | a.related[0] = b |
| 55 | } else { |
| 56 | a.related = append(a.related, b) |
| 57 | } |
| 58 | } |
| 59 | func (a *Bam) Related() []interfaces.Relatable { |
| 60 | return a.related |
| 61 | } |
| 62 | |
| 63 | func (a *Bam) MapQ() int { |
| 64 | return int(a.Record.MapQ) |
| 65 | } |
| 66 | |
| 67 | func check(err error) { |
| 68 | if err != nil { |
| 69 | panic(err) |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | func BamToRelatable(f io.Reader) (interfaces.RelatableChannel, error) { |
| 74 | |
| 75 | ch := make(chan interfaces.Relatable, 64) |
| 76 | b, err := bam.NewReader(f, 0) |
| 77 | if err != nil { |
| 78 | return nil, err |
| 79 | } |
| 80 | |
| 81 | go func() { |
| 82 | for { |
| 83 | rec, err := b.Read() |
| 84 | if err != nil { |
| 85 | if err == io.EOF { |
| 86 | break |
| 87 | } else { |
| 88 | log.Println(err) |
| 89 | break |
| 90 | } |
| 91 | } |
| 92 | if rec.RefID() == -1 { // unmapped |
| 93 | continue |
| 94 | } |
| 95 | // TODO: see if keeping the list of chrom names and using a ref is better. |
| 96 | bam := Bam{Record: rec, Chromosome: rec.Ref.Name(), related: nil} |
| 97 | ch <- &bam |
| 98 | } |
| 99 | close(ch) |
| 100 | b.Close() |
| 101 | f.(io.ReadCloser).Close() |
| 102 | }() |
| 103 | return ch, nil |
| 104 | } |
| 105 | |
| 106 | type BamQueryable struct { |
| 107 | idx *bam.Index |
| 108 | path string |
| 109 | file io.Reader |
| 110 | refs map[string]*sam.Reference |
| 111 | } |
| 112 | |
| 113 | func NewBamQueryable(path string, workers ...int) (*BamQueryable, error) { |
| 114 | f, err := os.Open(path + ".bai") |
| 115 | if err != nil { |
| 116 | return nil, err |
| 117 | } |
| 118 | defer f.Close() |
| 119 | idx, err := bam.ReadIndex(f) |
| 120 | if err != nil { |
| 121 | return nil, err |
| 122 | } |
| 123 | |
| 124 | b, err := os.Open(path) |
| 125 | if err != nil { |
| 126 | return nil, err |
| 127 | } |
| 128 | n := 1 |
| 129 | if len(workers) > 0 { |
| 130 | n = workers[0] |
| 131 | } |
| 132 | |
| 133 | br, err := bam.NewReader(b, n) |
| 134 | if err != nil { |
| 135 | return nil, err |
| 136 | } |
| 137 | hdr := br.Header() |
| 138 | refs := make(map[string]*sam.Reference, 40) |
| 139 | for _, r := range hdr.Refs() { |
| 140 | refs[r.Name()] = r |
| 141 | } |
| 142 | br.Close() |
| 143 | |
| 144 | return &BamQueryable{idx: idx, path: path, file: b, refs: refs}, nil |
| 145 | |
| 146 | } |
| 147 | |
| 148 | // make a copy so we can mess with the file pointers. |
| 149 | func newShort(old *BamQueryable) (*BamQueryable, error) { |
| 150 | b := &BamQueryable{ |
| 151 | idx: old.idx, |
| 152 | path: old.path, |
| 153 | refs: old.refs, |
| 154 | } |
| 155 | var err error |
| 156 | b.file, err = os.Open(b.path) |
| 157 | if err != nil { |
| 158 | return nil, err |
| 159 | } |
| 160 | if err != nil { |
| 161 | return nil, err |
| 162 | } |
| 163 | return b, nil |
| 164 | } |
| 165 | |
| 166 | func (b *BamQueryable) Query(region interfaces.IPosition) (interfaces.RelatableIterator, error) { |
| 167 | bn, err := newShort(b) // make a copy since we're messing with the file-pointer |
| 168 | if err != nil { |
| 169 | return nil, err |
| 170 | } |
| 171 | |
| 172 | ref, ok := b.refs[region.Chrom()] |
| 173 | if !ok { |
| 174 | if !strings.HasPrefix(region.Chrom(), "chr") { |
| 175 | ref, ok = b.refs["chr"+region.Chrom()] |
| 176 | } else if strings.HasPrefix(region.Chrom(), "chr") { |
| 177 | ref, ok = b.refs[region.Chrom()[3:]] |
| 178 | } |
| 179 | } |
| 180 | if !ok { |
| 181 | return nil, fmt.Errorf("%s not found in %s", region.Chrom(), bn.path) |
| 182 | } |
| 183 | |
| 184 | ch := make(chan interfaces.Relatable, 20) |
| 185 | go func() { |
| 186 | brdr, err := bam.NewReader(bn.file, 1) |
| 187 | if err != nil { |
| 188 | close(ch) |
| 189 | if err != io.EOF { |
| 190 | log.Println(err) |
| 191 | } |
| 192 | return |
| 193 | } |
| 194 | defer brdr.Close() |
| 195 | chrom := ref.Name() |
| 196 | chunks, err := bn.idx.Chunks(ref, int(region.Start()), int(region.End())) |
| 197 | if err != nil { |
| 198 | if err != io.EOF && err != index.ErrInvalid { |
| 199 | log.Println(err) |
| 200 | } |
| 201 | close(ch) |
| 202 | return |
| 203 | } |
| 204 | it, err := bam.NewIterator(brdr, chunks) |
| 205 | if err != nil { |
| 206 | if err != io.EOF { |
| 207 | log.Println(err) |
| 208 | } |
| 209 | it.Close() |
| 210 | close(ch) |
| 211 | return |
| 212 | } |
| 213 | for it.Next() { |
| 214 | rec := it.Record() |
| 215 | b := &Bam{Record: rec, Chromosome: chrom, related: nil} |
| 216 | if rec.Start() >= int(region.End()) { |
| 217 | break |
| 218 | } |
| 219 | if b.End() > region.Start() { |
| 220 | ch <- b |
| 221 | //log.Printf("%s: %s:%d-%d is in %+v", b.Name, b.Chrom(), b.Start(), b.End(), region) |
| 222 | //log.Println(len(ch)) |
| 223 | } |
| 224 | } |
| 225 | close(ch) |
| 226 | it.Close() |
| 227 | }() |
| 228 | return &BamIterator{ch, bn}, nil |
| 229 | } |
| 230 | |
| 231 | func (b *BamQueryable) Close() error { |
| 232 | if cr, ok := b.file.(io.ReadCloser); ok { |
| 233 | cr.Close() |
| 234 | } |
| 235 | return nil |
| 236 | } |
| 237 | |
| 238 | type BamIterator struct { |
| 239 | ch interfaces.RelatableChannel |
| 240 | b *BamQueryable |
| 241 | } |
| 242 | |
| 243 | func NewBamIterator(f string) (*BamIterator, error) { |
| 244 | fh, err := os.Open(f) |
| 245 | if err != nil { |
| 246 | return nil, err |
| 247 | } |
| 248 | ch, err := BamToRelatable(fh) |
| 249 | |
| 250 | b := &BamIterator{ch: ch} |
| 251 | |
| 252 | return b, err |
| 253 | } |
| 254 | |
| 255 | func (b *BamIterator) Close() error { |
| 256 | if b.b != nil { |
| 257 | return b.b.Close() |
| 258 | } |
| 259 | return nil |
| 260 | } |
| 261 | |
| 262 | func (b *BamIterator) Next() (interfaces.Relatable, error) { |
| 263 | rec, ok := <-b.ch |
| 264 | if !ok { |
| 265 | return nil, io.EOF |
| 266 | } |
| 267 | return rec, nil |
| 268 | } |