Debian Codegraph @

bam/bam_test.go

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 // Copyright ©2013 The bíogo Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package bam
6
7 import (
8 "bytes"
9 "compress/gzip"
10 "flag"
11 "fmt"
12 "io"
13 "os"
14 "reflect"
•15 "runtime"
16 "testing"
17 "time"
18
19 "github.com/biogo/hts/bgzf"
20 "github.com/biogo/hts/bgzf/index"
21 "github.com/biogo/hts/internal"
22 "github.com/biogo/hts/sam"
23
24 "gopkg.in/check.v1"
25 )
26
27 var (
28 bam = flag.Bool("bam", false, "output failing bam data for inspection")
29 allbam = flag.Bool("allbam", false, "output all bam data for inspection")
30 )
31
32 type failure bool
33
34 func (f failure) String() string {
35 if f {
36 return "fail"
37 }
38 return "ok"
39 }
40
41 func clonedRefs(refs []*sam.Reference) []*sam.Reference {
42 c := make([]*sam.Reference, len(refs))
43 for i, r := range refs {
44 c[i] = r.Clone()
45 }
46 return c
47 }
48
49 func clonedRGs(rgs []*sam.ReadGroup) []*sam.ReadGroup {
50 c := make([]*sam.ReadGroup, len(rgs))
51 for i, r := range rgs {
52 c[i] = r.Clone()
53 }
54 return c
55 }
56
57 func clonedProgs(progs []*sam.Program) []*sam.Program {
58 c := make([]*sam.Program, len(progs))
59 for i, p := range progs {
60 c[i] = p.Clone()
61 }
62 return c
63 }
64
65 func Test(t *testing.T) { check.TestingT(t) }
66
67 type S struct{}
68
69 var _ = check.Suite(&S{})
70
71 const maxInt = int(^uint(0) >> 1)
72
73 var (
74 file = flag.String("bench.file", "", "file to read for benchmarking")
75 findex = flag.String("bench.index", "", "index file to read for benchmarking")
76 conc = flag.Int("conc", 1, "sets the level of concurrency for compression")
77 )
78
79 func (s *S) TestRead(c *check.C) {
80 for i, t := range []struct {
81 in []byte
82 header *sam.Header
83 lines int
84 }{
85 {
86 in: bamHG00096_1000,
87 header: headerHG00096_1000,
88 lines: 1000,
89 },
90 } {
91 for omit := None; omit <= AllVariableLengthData; omit++ {
92 br, err := NewReader(bytes.NewBuffer(t.in), *conc)
93 c.Assert(err, check.Equals, nil)
94
95 c.Check(br.Header().Version, check.Equals, t.header.Version)
96 c.Check(br.Header().SortOrder, check.Equals, t.header.SortOrder)
97 c.Check(br.Header().GroupOrder, check.Equals, t.header.GroupOrder)
98 c.Check(br.Header().Comments, check.DeepEquals, t.header.Comments)
99 c.Check(clonedRefs(br.Header().Refs()), check.DeepEquals, clonedRefs(t.header.Refs()))
100 c.Check(clonedRGs(br.Header().RGs()), check.DeepEquals, clonedRGs(t.header.RGs()))
101 c.Check(clonedProgs(br.Header().Progs()), check.DeepEquals, clonedProgs(t.header.Progs()))
102
103 var lines int
104 for {
105 _, err := br.Read()
106 if err != nil {
107 c.Assert(err, check.Equals, io.EOF)
108 break
109 }
110 lines++
111 }
112 c.Check(lines, check.Equals, t.lines,
113 check.Commentf("got:%d expected:%d when omit=%d", lines, t.lines, omit))
114 if ok := reflect.DeepEqual(br.Header(), t.header) && lines == t.lines; *bam && !ok || *allbam {
115 bf, err := os.Create(fmt.Sprintf("read-%d-%s.bam", i, failure(!ok)))
116 c.Assert(err, check.Equals, nil)
117 bf.Write(t.in)
118 bf.Close()
119 }
120 }
121 }
122 }
123
124 func headerText(h *sam.Header) []byte {
125 b, _ := h.MarshalText()
126 return b
127 }
128
129 func (s *S) TestRoundTrip(c *check.C) {
130 for i, t := range []struct {
131 in []byte
132 header *sam.Header
133 conc int
134 lines int
135 }{
136 {
137 in: bamHG00096_1000,
138 header: headerHG00096_1000,
139 conc: 2,
140 lines: 1000,
141 },
142 } {
143 br, err := NewReader(bytes.NewBuffer(t.in), *conc)
144 c.Assert(err, check.Equals, nil)
145
146 var buf bytes.Buffer
147 bw, err := NewWriter(&buf, br.Header().Clone(), t.conc)
148 for {
149 r, err := br.Read()
150 if err != nil {
151 c.Assert(err, check.Equals, io.EOF)
152 break
153 }
154 bw.Write(r)
155 }
156 c.Assert(bw.Close(), check.Equals, nil)
157
158 br, err = NewReader(bytes.NewBuffer(t.in), *conc)
159 c.Assert(err, check.Equals, nil)
160 brr, err := NewReader(&buf, *conc)
161 c.Assert(err, check.Equals, nil)
162 c.Check(headerText(brr.Header()), check.DeepEquals, headerText(br.Header()))
163 c.Check(brr.Header(), check.DeepEquals, br.Header())
164 if !reflect.DeepEqual(brr.Header(), br.Header()) {
165 c.Check(brr.Header().Refs(), check.DeepEquals, br.Header().Refs())
166 c.Check(brr.Header().RGs(), check.DeepEquals, br.Header().RGs())
167 c.Check(brr.Header().Progs(), check.DeepEquals, br.Header().Progs())
168 c.Check(brr.Header().Comments, check.DeepEquals, br.Header().Comments)
169 }
170 allOK := true
171 for {
172 r, err := br.Read()
173 if err != nil {
174 c.Assert(err, check.Equals, io.EOF)
175 }
176 rr, err := brr.Read()
177 if err != nil {
178 c.Assert(err, check.Equals, io.EOF)
179 break
180 }
181 c.Check(rr, check.DeepEquals, r)
182 if !reflect.DeepEqual(rr, r) {
183 allOK = false
184 }
185 }
186 if ok := reflect.DeepEqual(br.Header(), brr.Header()) && allOK; *bam && !ok || *allbam {
187 bf, err := os.Create(fmt.Sprintf("roundtrip-%d-%s.bam", i, failure(!ok)))
188 c.Assert(err, check.Equals, nil)
189 bf.Write(t.in)
190 bf.Close()
191 }
192 }
193 }
194
195 func BenchmarkRead(b *testing.B) {
196 if *file == "" {
197 b.Skip("no file specified")
198 }
199 b.StopTimer()
200 f, err := os.Open(*file)
201 if err != nil {
202 b.Fatalf("Open failed: %v", err)
203 }
204 br, err := NewReader(f, *conc)
205 if err != nil {
206 b.Fatalf("NewReader failed: %v", err)
207 }
208 b.StartTimer()
209
210 for i := 0; i < b.N; i++ {
211 for {
212 _, err := br.Read()
213 if err != nil {
214 break
215 }
216 }
217 }
218 f.Close()
219 }
220
221 func BenchmarkReadIndex(b *testing.B) {
222 if *findex == "" {
223 b.Skip("no index file specified")
224 }
225 buf, err := os.ReadFile(*findex)
226 if err != nil {
227 b.Fatalf("Index read failed: %v", err)
228 }
229 r := bytes.NewReader(buf)
230
231 b.ResetTimer()
232 for i := 0; i < b.N; i++ {
233 r.Seek(0, io.SeekStart)
234 _, err = ReadIndex(r)
235 if err != nil {
236 b.Fatal(err)
237 }
238 }
239 }
240
241 func BenchmarkReadCoreAndSeq(b *testing.B) {
242 if *file == "" {
243 b.Skip("no file specified")
244 }
245 b.StopTimer()
246 f, err := os.Open(*file)
247 if err != nil {
248 b.Fatalf("Open failed: %v", err)
249 }
250 br, err := NewReader(f, *conc)
251 if err != nil {
252 b.Fatalf("NewReader failed: %v", err)
253 }
254 b.StartTimer()
255
256 br.Omit(AuxTags)
257 for i := 0; i < b.N; i++ {
258 for {
259 _, err := br.Read()
260 if err != nil {
261 break
262 }
263 }
264 }
265 f.Close()
266 }
267
268 func BenchmarkReadCoreOnly(b *testing.B) {
269 if *file == "" {
270 b.Skip("no file specified")
271 }
272 b.StopTimer()
273 f, err := os.Open(*file)
274 if err != nil {
275 b.Fatalf("Open failed: %v", err)
276 }
277 br, err := NewReader(f, *conc)
278 if err != nil {
279 b.Fatalf("NewReader failed: %v", err)
280 }
281 b.StartTimer()
282
283 br.Omit(AllVariableLengthData)
284 for i := 0; i < b.N; i++ {
285 for {
286 _, err := br.Read()
287 if err != nil {
288 break
289 }
290 }
291 }
292 f.Close()
293 }
294
295 func BenchmarkWrite(b *testing.B) {
296 b.StopTimer()
297 br, err := NewReader(bytes.NewReader(bamHG00096_1000), *conc)
298 if err != nil {
299 b.Fatalf("NewReader failed: %v", err)
300 }
301 r, err := br.Read()
302 if err != nil {
303 b.Fatalf("Read failed: %v", err)
304 }
305 bw, err := NewWriter(io.Discard, br.Header().Clone(), *conc)
306 if err != nil {
307 b.Fatalf("NewWriter failed: %v", err)
308 }
309 b.StartTimer()
310
311 for i := 0; i < b.N; i++ {
312 err = bw.Write(r)
313 if err != nil {
314 b.Fatalf("Write failed: %v", err)
315 }
316 }
317 }
318
319 func BenchmarkReadFile(b *testing.B) {
320 if *file == "" {
321 b.Skip("no file specified")
322 }
323
324 for i := 0; i < b.N; i++ {
325 f, err := os.Open(*file)
326 if err != nil {
327 b.Fatalf("Open failed: %v", err)
328 }
329 br, err := NewReader(f, *conc)
330 if err != nil {
331 b.Fatalf("NewReader failed: %v", err)
332 }
333 for {
334 _, err := br.Read()
335 if err != nil {
336 break
337 }
338 }
339 f.Close()
340 }
341 }
342
343 // The is to comparable to `time samtools view -b $file > /dev/null'.
344 func BenchmarkRoundtripFile(b *testing.B) {
345 if *file == "" {
346 b.Skip("no file specified")
347 }
348
349 for i := 0; i < b.N; i++ {
350 f, err := os.Open(*file)
351 if err != nil {
352 b.Fatalf("Open failed: %v", err)
353 }
354 br, err := NewReader(f, *conc)
355 if err != nil {
356 b.Fatalf("NewReader failed: %v", err)
357 }
358 bw, err := NewWriter(io.Discard, br.Header().Clone(), *conc)
359 if err != nil {
360 b.Fatalf("NewWriter failed: %v", err)
361 }
362 for {
363 r, err := br.Read()
364 if err != nil {
365 break
366 }
367 err = bw.Write(r)
368 if err != nil {
369 b.Fatalf("Write failed: %v", err)
370 }
371 }
372 f.Close()
373 }
374 }
375
376 func (s *S) TestSpecExamples(c *check.C) {
377 br, err := NewReader(bytes.NewReader(specExamples.data), *conc)
378 c.Assert(err, check.Equals, nil)
379 bh := br.Header()
380 c.Check(bh.Version, check.Equals, specExamples.header.Version)
381 c.Check(bh.SortOrder, check.Equals, specExamples.header.SortOrder)
382 c.Check(bh.GroupOrder, check.Equals, specExamples.header.GroupOrder)
383 c.Check(bh.Comments, check.DeepEquals, specExamples.header.Comments)
384 for i, expect := range specExamples.records {
385 r, err := br.Read()
386 if err != nil {
387 c.Errorf("Unexpected early error: %v", err)
388 }
389 c.Check(r.Name, check.Equals, expect.Name)
390 c.Check(r.Pos, check.Equals, expect.Pos) // Zero-based here.
391 c.Check(r.Flags, check.Equals, expect.Flags)
392 if r.Flags&sam.Unmapped == 0 {
393 c.Check(r.Ref, check.Not(check.Equals), nil)
394 if r.Ref != nil {
395 c.Check(r.Ref.Name(), check.Equals, bh.Refs()[0].Name())
396 }
397 } else {
398 c.Check(r.Ref, check.Equals, nil)
399 }
400 c.Check(r.MatePos, check.Equals, expect.MatePos) // Zero-based here.
401 c.Check(r.Cigar, check.DeepEquals, expect.Cigar)
402 c.Check(r.Cigar.IsValid(r.Seq.Length), check.Equals, true)
403 c.Check(r.TempLen, check.Equals, expect.TempLen)
404 c.Check(r.Seq, check.DeepEquals, expect.Seq, check.Commentf("got:%q expected:%q", r.Seq.Expand(), expect.Seq.Expand()))
405 c.Check(r.Qual, check.DeepEquals, expect.Qual) // No valid qualities here.
406 c.Check(r.End(), check.Equals, specExamples.readEnds[i], check.Commentf("unexpected end position for %q at %v, got:%d expected:%d", r.Name, r.Pos, r.End(), specExamples.readEnds[i]))
407 c.Check(r.AuxFields, check.DeepEquals, expect.AuxFields)
408
409 parsedCigar, err := sam.ParseCigar([]byte(specExamples.cigars[i]))
410 c.Check(err, check.Equals, nil)
411 c.Check(parsedCigar, check.DeepEquals, expect.Cigar)
412
413 // In all the examples the last base of the read and the last
414 // base of the ref are valid, so we can check this.
415 expSeq := r.Seq.Expand()
416 c.Check(specExamples.ref[r.End()-1], check.Equals, expSeq[len(expSeq)-1])
417 }
418 }
419
420 func (s *S) TestSpecExamplesIterator(c *check.C) {
421 br, err := NewReader(bytes.NewReader(specExamples.data), *conc)
422 c.Assert(err, check.Equals, nil)
423 it, err := NewIterator(br, nil)
424 c.Assert(err, check.Equals, nil)
425 i := 0
426 for ; it.Next(); i++ {
427 expect := specExamples.records[i]
428 r := it.Record()
429 c.Check(r.Name, check.Equals, expect.Name)
430 c.Check(r.Pos, check.Equals, expect.Pos)
431 }
432 c.Assert(i, check.Equals, len(specExamples.records))
433 c.Assert(it.Error(), check.Equals, nil)
434 }
435
436 func mustAux(a sam.Aux, err error) sam.Aux {
437 if err != nil {
438 panic(err)
439 }
440 return a
441 }
442
443 // The following data is the BAM file created from the following SAM using:
444 //
445 // samtools view -Sb spec-example.sam > spec-example.bam
446 //
447 // @HD VN:1.5 SO:coordinate
448 // @SQ SN:ref LN:45
449 // @CO --------------------------------------------------------
450 // @CO Coor 12345678901234 5678901234567890123456789012345
451 // @CO ref AGCATGTTAGATAA**GATAGCTGTGCTAGTAGGCAGTCAGCGCCAT
452 // @CO --------------------------------------------------------
453 // @CO +r001/1 TTAGATAAAGGATA*CTG
454 // @CO +r002 aaaAGATAA*GGATA
455 // @CO +r003 gcctaAGCTAA
456 // @CO +r004 ATAGCT..............TCAGC
457 // @CO -r003 ttagctTAGGC
458 // @CO -r001/2 CAGCGGCAT
459 // @CO --------------------------------------------------------
460 // r001 99 ref 7 30 8M2I4M1D3M = 37 39 TTAGATAAAGGATACTG *
461 // r002 0 ref 9 30 3S6M1P1I4M * 0 0 AAAAGATAAGGATA *
462 // r003 0 ref 9 30 5S6M * 0 0 GCCTAAGCTAA * SA:Z:ref,29,-,6H5M,17,0;
463 // r004 0 ref 16 30 6M14N5M * 0 0 ATAGCTTCAGC *
464 // r003 2064 ref 29 17 6H5M * 0 0 TAGGC * SA:Z:ref,9,+,5S6M,30,1;
465 // r001 147 ref 37 30 9M = 7 -39 CAGCGGCAT * NM:i:1
466 var specExamples = struct {
467 ref string
468 data []byte
469 header sam.Header
470 records []*sam.Record
471 cigars []string
472 readEnds []int
473 }{
474 ref: "AGCATGTTAGATAAGATAGCTGTGCTAGTAGGCAGTCAGCGCCAT",
475 data: []byte{
476 0x1f, 0x8b, 0x08, 0x04, 0x00, 0x00, 0x00, 0x00,
477 0x00, 0xff, 0x06, 0x00, 0x42, 0x43, 0x02, 0x00,
478 0xf7, 0x00, 0xa5, 0x51, 0xcb, 0x0a, 0xc2, 0x30,
479 0x10, 0x8c, 0xe2, 0x25, 0xfd, 0x8a, 0x9c, 0x2b,
480 0x6a, 0x53, 0x5b, 0x1f, 0x3d, 0x75, 0x8d, 0x10,
481 0x0f, 0x5a, 0x91, 0x06, 0xef, 0xa1, 0x56, 0xf1,
482 0xa2, 0x50, 0xf2, 0xff, 0xb8, 0x4d, 0x5f, 0x0a,
483 0xc5, 0x83, 0x0e, 0x49, 0x76, 0xc9, 0xce, 0x64,
484 0x1f, 0xd9, 0xc0, 0x61, 0x90, 0x0f, 0x09, 0x89,
485 0x77, 0x5b, 0x7a, 0x4e, 0x22, 0x3e, 0x0d, 0x69,
486 0x7a, 0x8c, 0xb2, 0xe7, 0xb3, 0xb8, 0xdc, 0x1f,
487 0xda, 0xe4, 0x4e, 0x9c, 0x9e, 0x68, 0x9a, 0x44,
488 0x45, 0x7e, 0xa5, 0xfb, 0x24, 0x0a, 0x42, 0x27,
489 0x16, 0x47, 0x3a, 0xf9, 0x11, 0x56, 0x2c, 0xf0,
490 0x71, 0x56, 0x82, 0xfb, 0xf3, 0x20, 0x5c, 0x2c,
491 0x57, 0x6b, 0xaf, 0xf4, 0x18, 0xeb, 0xfc, 0x5e,
492 0xcf, 0x8a, 0xb1, 0x0e, 0xab, 0x65, 0x20, 0x05,
493 0x28, 0xa9, 0x14, 0x48, 0x50, 0x00, 0xae, 0x5b,
494 0x1a, 0x29, 0xf0, 0x06, 0x0f, 0x90, 0xb8, 0x30,
495 0x2e, 0x15, 0x6e, 0x21, 0x05, 0x32, 0xff, 0x2f,
496 0x7b, 0x5c, 0x78, 0x1e, 0x9f, 0x71, 0x56, 0xa3,
497 0xc9, 0x8c, 0x89, 0xd0, 0xb8, 0x98, 0xb9, 0x65,
498 0xf9, 0x0d, 0x87, 0x69, 0xad, 0xeb, 0xfa, 0x2c,
499 0xab, 0x65, 0xcc, 0xeb, 0xf8, 0x2d, 0xcb, 0x8c,
500 0x2e, 0xcb, 0x86, 0x2e, 0x16, 0xb0, 0x3e, 0x54,
501 0xdd, 0x4d, 0x3f, 0x60, 0xbb, 0xab, 0x1a, 0x7b,
502 0x7b, 0xb3, 0x17, 0xc6, 0xe8, 0x5b, 0x66, 0xec,
503 0x54, 0x5a, 0x01, 0x9f, 0xf9, 0xdf, 0x24, 0xef,
504 0xb0, 0x63, 0x94, 0x7f, 0x8f, 0x71, 0x40, 0x08,
505 0x19, 0xe1, 0xc6, 0x4f, 0x24, 0x13, 0xb4, 0x2f,
506 0x02, 0xfe, 0x37, 0xdf, 0x7d, 0x02, 0x00, 0x00,
507 0x1f, 0x8b, 0x08, 0x04, 0x00, 0x00, 0x00, 0x00,
508 0x00, 0xff, 0x06, 0x00, 0x42, 0x43, 0x02, 0x00,
509 0x01, 0x01, 0x0b, 0x66, 0x80, 0x00, 0x36, 0x20,
510 0x66, 0x95, 0xf3, 0x14, 0x62, 0x65, 0x48, 0x66,
511 0x10, 0x84, 0x8a, 0xa9, 0x00, 0xb1, 0x3a, 0x10,
512 0x17, 0x19, 0x18, 0x18, 0x32, 0x34, 0x00, 0x19,
513 0x8a, 0x40, 0xec, 0x00, 0xc4, 0x42, 0x40, 0x6c,
514 0x00, 0xc4, 0x1d, 0x22, 0x12, 0x82, 0x22, 0x8e,
515 0x8d, 0x1a, 0x0e, 0xff, 0xd1, 0x81, 0x1f, 0xd4,
516 0x08, 0x0e, 0xb8, 0xb1, 0x0c, 0x0c, 0x7c, 0x40,
517 0x0c, 0x93, 0x67, 0x80, 0x18, 0x6b, 0xc4, 0x60,
518 0x02, 0x64, 0x24, 0x00, 0xb1, 0x18, 0x10, 0x0b,
519 0x42, 0x8d, 0x17, 0x14, 0x74, 0x6c, 0x04, 0x1a,
520 0x8b, 0x6a, 0x62, 0x28, 0x9a, 0x89, 0x4c, 0x40,
521 0x9a, 0x1b, 0xd3, 0x44, 0x63, 0x86, 0x10, 0xa8,
522 0x89, 0x4e, 0x1a, 0x82, 0x4e, 0x8d, 0x02, 0x48,
523 0x26, 0x04, 0x3b, 0x46, 0x15, 0xa5, 0xa6, 0xe9,
524 0x18, 0x59, 0xea, 0xe8, 0xea, 0x98, 0x79, 0x98,
525 0xfa, 0xea, 0x18, 0x9a, 0xeb, 0x18, 0x58, 0x33,
526 0x38, 0x41, 0x0d, 0xe6, 0x87, 0x1a, 0xcc, 0x8c,
527 0xdd, 0x60, 0x13, 0xb0, 0xa1, 0x8f, 0x81, 0x38,
528 0x00, 0x88, 0x25, 0x44, 0x34, 0x9a, 0x44, 0x14,
529 0x90, 0x0c, 0xf7, 0x86, 0x9a, 0x22, 0x03, 0x32,
530 0x45, 0x10, 0xe4, 0x3c, 0x01, 0x0e, 0x56, 0x6c,
531 0xce, 0x4b, 0x85, 0x9a, 0xd0, 0xe8, 0xa2, 0x80,
532 0xec, 0x2a, 0x4b, 0x1d, 0x6d, 0x1d, 0xd3, 0x60,
533 0x33, 0x5f, 0x1d, 0x63, 0x03, 0x1d, 0x43, 0x6b,
534 0x06, 0x6b, 0xa4, 0x28, 0x00, 0x39, 0x8a, 0x91,
535 0x61, 0x32, 0x03, 0x27, 0x52, 0x54, 0xdd, 0x04,
536 0xea, 0x04, 0x47, 0xcb, 0x04, 0x50, 0xb4, 0x38,
537 0xb9, 0x28, 0x36, 0x20, 0x82, 0xde, 0xd7, 0x99,
538 0x11, 0x00, 0x9e, 0x55, 0xee, 0x4f, 0xd6, 0x01,
539 0x00, 0x00, 0x1f, 0x8b, 0x08, 0x04, 0x00, 0x00,
540 0x00, 0x00, 0x00, 0xff, 0x06, 0x00, 0x42, 0x43,
541 0x02, 0x00, 0x1b, 0x00, 0x03, 0x00, 0x00, 0x00,
542 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
543 },
544 header: sam.Header{
545 Version: "1.5",
546 SortOrder: sam.Coordinate,
547 GroupOrder: sam.GroupUnspecified,
548 Comments: []string{
549 "--------------------------------------------------------",
550 "Coor 12345678901234 5678901234567890123456789012345",
551 "ref AGCATGTTAGATAA**GATAGCTGTGCTAGTAGGCAGTCAGCGCCAT",
552 "--------------------------------------------------------",
553 "+r001/1 TTAGATAAAGGATA*CTG",
554 "+r002 aaaAGATAA*GGATA",
555 "+r003 gcctaAGCTAA",
556 "+r004 ATAGCT..............TCAGC",
557 "-r003 ttagctTAGGC",
558 "-r001/2 CAGCGGCAT",
559 "--------------------------------------------------------",
560 },
561 },
562 records: []*sam.Record{
563 {
564 Name: "r001",
565 Pos: 6,
566 MapQ: 30,
567 Cigar: sam.Cigar{
568 sam.NewCigarOp(sam.CigarMatch, 8),
569 sam.NewCigarOp(sam.CigarInsertion, 2),
570 sam.NewCigarOp(sam.CigarMatch, 4),
571 sam.NewCigarOp(sam.CigarDeletion, 1),
572 sam.NewCigarOp(sam.CigarMatch, 3),
573 },
574 Flags: sam.Paired | sam.ProperPair | sam.MateReverse | sam.Read1,
575 MatePos: 36,
576 TempLen: 39,
577 Seq: sam.NewSeq([]byte("TTAGATAAAGGATACTG")),
578 Qual: []uint8{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
579 },
580 {
581 Name: "r002",
582 Pos: 8,
583 MapQ: 30,
584 Cigar: sam.Cigar{
585 sam.NewCigarOp(sam.CigarSoftClipped, 3),
586 sam.NewCigarOp(sam.CigarMatch, 6),
587 sam.NewCigarOp(sam.CigarPadded, 1),
588 sam.NewCigarOp(sam.CigarInsertion, 1),
589 sam.NewCigarOp(sam.CigarMatch, 4),
590 },
591 MatePos: -1,
592 TempLen: 0,
593 Seq: sam.NewSeq([]byte("AAAAGATAAGGATA")),
594 Qual: []uint8{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
595 },
596 {
597 Name: "r003",
598 Pos: 8,
599 MapQ: 30,
600 Cigar: sam.Cigar{
601 sam.NewCigarOp(sam.CigarSoftClipped, 5),
602 sam.NewCigarOp(sam.CigarMatch, 6),
603 },
604 MatePos: -1,
605 TempLen: 0,
606 Seq: sam.NewSeq([]byte("GCCTAAGCTAA")),
607 Qual: []uint8{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
608 AuxFields: []sam.Aux{
609 mustAux(sam.NewAux(sam.NewTag("SA"), "ref,29,-,6H5M,17,0;")),
610 },
611 },
612 {
613 Name: "r004",
614 Pos: 15,
615 MapQ: 30,
616 Cigar: sam.Cigar{
617 sam.NewCigarOp(sam.CigarMatch, 6),
618 sam.NewCigarOp(sam.CigarSkipped, 14),
619 sam.NewCigarOp(sam.CigarMatch, 5),
620 },
621 MatePos: -1,
622 TempLen: 0,
623 Seq: sam.NewSeq([]byte("ATAGCTTCAGC")),
624 Qual: []uint8{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
625 },
626 {
627 Name: "r003",
628 Pos: 28,
629 MapQ: 17,
630 Cigar: sam.Cigar{
631 sam.NewCigarOp(sam.CigarHardClipped, 6),
632 sam.NewCigarOp(sam.CigarMatch, 5),
633 },
634 Flags: sam.Reverse | sam.Supplementary,
635 MatePos: -1,
636 TempLen: 0,
637 Seq: sam.NewSeq([]byte("TAGGC")),
638 Qual: []uint8{0xff, 0xff, 0xff, 0xff, 0xff},
639 AuxFields: []sam.Aux{
640 mustAux(sam.NewAux(sam.NewTag("SA"), "ref,9,+,5S6M,30,1;")),
641 },
642 },
643 {
644 Name: "r001",
645 Pos: 36,
646 MapQ: 30,
647 Cigar: sam.Cigar{
648 sam.NewCigarOp(sam.CigarMatch, 9),
649 },
650 Flags: sam.Paired | sam.ProperPair | sam.Reverse | sam.Read2,
651 MatePos: 6,
652 TempLen: -39,
653 Seq: sam.NewSeq([]byte("CAGCGGCAT")),
654 Qual: []uint8{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
655 AuxFields: []sam.Aux{
656 mustAux(sam.NewAux(sam.NewTag("NM"), uint(1))),
657 },
658 },
659 },
660 cigars: []string{
661 "8M2I4M1D3M",
662 "3S6M1P1I4M",
663 "5S6M",
664 "6M14N5M",
665 "6H5M",
666 "9M",
667 },
668 // These coordinates are all open (and zero-based) so that
669 // a slice of the reference doesn't need any alteration.
670 readEnds: []int{
671 22,
672 18,
673 14,
674 40,
675 33,
676 45,
677 },
678 }
679
680 func (s *S) TestIssue3(c *check.C) {
681 for _, test := range issue3 {
682 br, err := NewReader(bytes.NewReader(test), *conc)
683 c.Assert(err, check.Equals, nil, check.Commentf("Failed to open BAM"))
684
685 for {
686 _, err := br.Read()
687 if err != nil {
688 c.Check(err, check.Equals, io.EOF)
689 break
690 }
691 }
692 }
693 }
694
695 var issue3 = [][]byte{
696 // Specific case posted at https://code.google.com/p/biogo/issues/detail?id=3#c6
697 // edited to a minimal reproducer.
698 {
699 0x1f, 0x8b, 0x08, 0x04, 0x00, 0x00, 0x00, 0x00,
700 0x00, 0xff, 0x06, 0x00, 0x42, 0x43, 0x02, 0x00,
701 0x61, 0x00, 0x73, 0x72, 0xf4, 0x65, 0x34, 0x62,
702 0x60, 0x60, 0x70, 0xf0, 0x70, 0xe1, 0x0c, 0xf3,
703 0xb3, 0x32, 0xd4, 0x33, 0xe0, 0x0c, 0xf6, 0xb7,
704 0x4a, 0xce, 0xcf, 0x2f, 0x4a, 0xc9, 0xcc, 0x4b,
705 0x2c, 0x49, 0xe5, 0x72, 0x08, 0x0e, 0xe4, 0x0c,
706 0xf6, 0xb3, 0x8a, 0x2a, 0xb3, 0x8c, 0xf7, 0x73,
707 0x34, 0x34, 0xe0, 0xf4, 0xf1, 0xb3, 0x32, 0xb1,
708 0x30, 0x30, 0x33, 0xe2, 0x62, 0x04, 0xea, 0xe2,
709 0x04, 0x62, 0x98, 0x0c, 0xc3, 0xbe, 0xdd, 0x0c,
710 0x0c, 0x00, 0xaa, 0x89, 0xab, 0xc2, 0x4f, 0x00,
711 0x00, 0x00, 0x1f, 0x8b, 0x08, 0x04, 0x00, 0x00,
712 0x00, 0x00, 0x00, 0xff, 0x06, 0x00, 0x42, 0x43,
713 0x02, 0x00, 0xa2, 0x00, 0x9b, 0xc5, 0x00, 0x01,
714 0xb3, 0xa5, 0x19, 0x18, 0xf8, 0x19, 0x3c, 0x85,
715 0x18, 0x19, 0x18, 0x18, 0x25, 0x81, 0xfc, 0xff,
716 0x50, 0x00, 0x92, 0x33, 0x34, 0x28, 0x30, 0x30,
717 0xcc, 0x4a, 0x4b, 0xce, 0x53, 0x76, 0x76, 0x76,
718 0x0f, 0x61, 0x98, 0x00, 0x54, 0x23, 0xd4, 0x01,
719 0x05, 0x4a, 0x1d, 0x1d, 0x0d, 0xaa, 0xca, 0xca,
720 0xca, 0x8a, 0xaa, 0xea, 0xea, 0xea, 0x1a, 0x9a,
721 0x9a, 0x9a, 0x1a, 0x40, 0x52, 0x55, 0x59, 0x51,
722 0x45, 0x55, 0x55, 0x45, 0xc9, 0x31, 0x38, 0xf9,
723 0x77, 0x84, 0x9f, 0x33, 0x43, 0x84, 0xaf, 0x33,
724 0x63, 0x84, 0x3f, 0x90, 0x76, 0x77, 0x66, 0xf0,
725 0x03, 0xb2, 0x7d, 0x5d, 0xa2, 0x8c, 0x8c, 0x1d,
726 0x0d, 0x19, 0x22, 0x43, 0xa2, 0x42, 0x43, 0x19,
727 0xfc, 0x3c, 0x9c, 0x45, 0x9c, 0x9d, 0xa3, 0x92,
728 0x33, 0x8a, 0x0c, 0x19, 0x9c, 0x03, 0x3c, 0xf7,
729 0x9a, 0x3f, 0x67, 0x8a, 0x08, 0x76, 0xd4, 0xf6,
730 0xf0, 0x74, 0x66, 0x00, 0x00, 0xa6, 0xdc, 0x77,
731 0x77, 0x9e, 0x00, 0x00, 0x00, 0x1f, 0x8b, 0x08,
732 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x06,
733 0x00, 0x42, 0x43, 0x02, 0x00, 0x1b, 0x00, 0x03,
734 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
735 0x00,
736 },
737
738 // More general testing: sequence lengths range from 19 to 26 inclusive.
739 {
740 0x1f, 0x8b, 0x08, 0x04, 0x00, 0x00, 0x00, 0x00,
741 0x00, 0xff, 0x06, 0x00, 0x42, 0x43, 0x02, 0x00,
742 0x61, 0x00, 0x73, 0x72, 0xf4, 0x65, 0x34, 0x62,
743 0x60, 0x60, 0x70, 0xf0, 0x70, 0xe1, 0x0c, 0xf3,
744 0xb3, 0x32, 0xd4, 0x33, 0xe0, 0x0c, 0xf6, 0xb7,
745 0x4a, 0xce, 0xcf, 0x2f, 0x4a, 0xc9, 0xcc, 0x4b,
746 0x2c, 0x49, 0xe5, 0x72, 0x08, 0x0e, 0xe4, 0x0c,
747 0xf6, 0xb3, 0x8a, 0x2a, 0xb3, 0x8c, 0xf7, 0x73,
748 0x34, 0x34, 0xe0, 0xf4, 0xf1, 0xb3, 0x32, 0xb1,
749 0x30, 0x30, 0x33, 0xe2, 0x62, 0x04, 0xea, 0xe2,
750 0x04, 0x62, 0x98, 0x0c, 0xc3, 0xbe, 0xdd, 0x0c,
751 0x0c, 0x00, 0xaa, 0x89, 0xab, 0xc2, 0x4f, 0x00,
752 0x00, 0x00, 0x1f, 0x8b, 0x08, 0x04, 0x00, 0x00,
753 0x00, 0x00, 0x00, 0xff, 0x06, 0x00, 0x42, 0x43,
754 0x02, 0x00, 0x12, 0x01, 0x9b, 0xc8, 0x00, 0x01,
755 0xb3, 0xa5, 0x19, 0x18, 0xf8, 0x19, 0x3c, 0x85,
756 0x18, 0x19, 0x18, 0x18, 0x85, 0x81, 0xfc, 0xff,
757 0x50, 0x00, 0x92, 0x33, 0x34, 0x28, 0x30, 0x30,
758 0xcc, 0x4a, 0x4b, 0xce, 0x53, 0x76, 0x76, 0x76,
759 0x0f, 0x61, 0x30, 0x00, 0xaa, 0x11, 0xea, 0x80,
760 0x02, 0x05, 0x55, 0x65, 0x65, 0x65, 0x45, 0x55,
761 0x75, 0x75, 0x75, 0x0d, 0x4d, 0x4d, 0x4d, 0x0d,
762 0x20, 0xa9, 0xaa, 0xec, 0x18, 0x9c, 0xfc, 0x3b,
763 0xc2, 0xcf, 0x99, 0x21, 0xc2, 0xd7, 0x99, 0x31,
764 0xc2, 0x1f, 0x48, 0xbb, 0x3b, 0x33, 0xf8, 0x01,
765 0xd9, 0xbe, 0x2e, 0x51, 0x46, 0xc6, 0x8e, 0x86,
766 0x0c, 0x91, 0x21, 0x51, 0xa1, 0xa1, 0x0c, 0x7e,
767 0x1e, 0xce, 0x22, 0xce, 0xce, 0x51, 0xc9, 0x19,
768 0x45, 0x86, 0x0c, 0xce, 0x01, 0x9e, 0x7b, 0xcd,
769 0x9f, 0x33, 0x45, 0x04, 0x3b, 0x6a, 0x7b, 0x78,
770 0x3a, 0x33, 0x4c, 0xc2, 0xe2, 0x28, 0x11, 0xfc,
771 0x8e, 0x72, 0x40, 0x76, 0x94, 0x12, 0x16, 0x47,
772 0x29, 0x52, 0xec, 0xaa, 0x29, 0x58, 0x5c, 0x25,
773 0x8a, 0xdf, 0x55, 0x01, 0x28, 0xae, 0x6a, 0xc0,
774 0xe6, 0x2c, 0x15, 0x8a, 0xdd, 0x35, 0x15, 0x8b,
775 0xbb, 0xc4, 0xf0, 0xbb, 0x2b, 0x01, 0xc5, 0x5d,
776 0x1d, 0x58, 0xdd, 0xa5, 0x4a, 0xb1, 0xc3, 0xa6,
777 0x63, 0x71, 0x98, 0x38, 0x7e, 0x87, 0x15, 0xa0,
778 0x3a, 0x0c, 0x7b, 0x88, 0xa9, 0x52, 0xee, 0xb4,
779 0x19, 0x58, 0x9c, 0x26, 0x81, 0xdf, 0x69, 0x0d,
780 0xa8, 0x4e, 0xc3, 0x11, 0x68, 0xaa, 0x94, 0xc7,
781 0xe7, 0x2c, 0x2c, 0x6e, 0x93, 0xc4, 0xef, 0xb6,
782 0x09, 0x68, 0x6e, 0xc3, 0x15, 0x6e, 0x2a, 0x4a,
783 0x14, 0xbb, 0x6e, 0x36, 0x16, 0xd7, 0x49, 0xe1,
784 0x77, 0xdd, 0x02, 0x34, 0xd7, 0xe1, 0x0c, 0x3a,
785 0x25, 0xca, 0x9d, 0x07, 0x00, 0x31, 0x91, 0x24,
786 0x5a, 0xd0, 0x04, 0x00, 0x00, 0x1f, 0x8b, 0x08,
787 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x06,
788 0x00, 0x42, 0x43, 0x02, 0x00, 0x1b, 0x00, 0x03,
789 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
790 0x00,
791 },
792 }
793
794 func (s *S) TestIssue11(c *check.C) {
795 for _, test := range issue11 {
796 br, err := NewReader(bytes.NewReader(test.input), *conc)
797 c.Check(err, check.Equals, nil)
798 c.Check(br.Header().GroupOrder, check.Equals, test.expect)
799 }
800 }
801
802 var issue11 = []struct {
803 input []byte
804 expect sam.GroupOrder
805 }{
806 {
807 // 1000 genomes file hand edited to reduce number of Refs and reads;
808 // no specified group ordering.
809 input: []byte{
810 0x1f, 0x8b, 0x08, 0x04, 0x00, 0x00, 0x00, 0x00,
811 0x00, 0xff, 0x06, 0x00, 0x42, 0x43, 0x02, 0x00,
812 0xec, 0x01, 0xad, 0x92, 0xcd, 0x8e, 0xda, 0x30,
813 0x10, 0xc7, 0xb3, 0xdd, 0x93, 0x79, 0x8a, 0x1c,
814 0x38, 0xb4, 0x07, 0x1c, 0xe7, 0xc3, 0x81, 0x58,
815 0xaa, 0xc4, 0x97, 0x04, 0x48, 0x21, 0x9b, 0x12,
816 0xd1, 0x6a, 0x4f, 0x91, 0x93, 0xd8, 0xc1, 0x5a,
817 0xe3, 0x00, 0x0e, 0xd0, 0xf6, 0x0d, 0xfa, 0xc4,
818 0xbd, 0x55, 0x35, 0xd1, 0xb6, 0x5d, 0xb5, 0x3d,
819 0xee, 0xc1, 0x23, 0xfb, 0x3f, 0x33, 0xbf, 0x19,
820 0xdb, 0x33, 0x9d, 0xac, 0xef, 0xb2, 0x7b, 0xcb,
821 0x1a, 0x2f, 0xe7, 0xe0, 0x63, 0x42, 0x5c, 0x88,
822 0x40, 0xf6, 0x40, 0xca, 0xa6, 0x39, 0x55, 0x42,
823 0xd1, 0x96, 0xf5, 0xc6, 0xd9, 0x07, 0x90, 0x25,
824 0xe4, 0x11, 0xc4, 0x09, 0xc1, 0x91, 0x3f, 0xf4,
825 0x71, 0x18, 0x82, 0x35, 0x26, 0x2e, 0xa7, 0x7e,
826 0x30, 0x0c, 0x86, 0x18, 0x51, 0x8e, 0xa2, 0x60,
827 0x54, 0x54, 0x3c, 0x1a, 0x56, 0x98, 0x22, 0xc6,
828 0xb0, 0xc7, 0xb0, 0x0b, 0xb6, 0x1b, 0xc2, 0xdb,
829 0x03, 0x71, 0x1c, 0x63, 0xa1, 0x8b, 0x10, 0xaa,
830 0x99, 0x6a, 0xf6, 0x4c, 0x43, 0x56, 0x08, 0x48,
831 0x4b, 0x78, 0x7e, 0x72, 0x2e, 0x8d, 0x74, 0x6f,
832 0x6e, 0xa7, 0x65, 0xe5, 0x4e, 0x89, 0x92, 0x4a,
833 0xe7, 0xc4, 0x38, 0x3b, 0x31, 0x55, 0x32, 0xe7,
834 0xb0, 0xa3, 0x9a, 0x79, 0xf9, 0x6f, 0x21, 0xa7,
835 0x5a, 0xb3, 0x7d, 0x21, 0xbf, 0xe4, 0x9a, 0x1d,
836 0xcf, 0x5d, 0xc8, 0x4e, 0xfb, 0xa6, 0x24, 0xe4,
837 0x14, 0xd6, 0x5f, 0xc1, 0x24, 0x23, 0xc9, 0x6c,
838 0xba, 0xf2, 0x87, 0x20, 0x4b, 0xc9, 0xf2, 0xbc,
839 0xa7, 0xaa, 0x37, 0xde, 0x2c, 0xc0, 0x6a, 0x4e,
840 0xb2, 0xcd, 0x06, 0x85, 0x5e, 0xe8, 0x07, 0x20,
841 0x9e, 0x12, 0x6f, 0x14, 0xe0, 0x11, 0x0e, 0x47,
842 0xd8, 0x5c, 0x74, 0x4d, 0x96, 0x0b, 0xd3, 0x58,
843 0x14, 0x82, 0x74, 0x45, 0x3c, 0x14, 0x82, 0x59,
844 0x42, 0x3e, 0x6d, 0x17, 0xd9, 0x0c, 0xa4, 0x31,
845 0x59, 0xc5, 0xf1, 0x76, 0xbd, 0x4a, 0x26, 0x60,
846 0x9e, 0x19, 0x42, 0x8a, 0x90, 0xeb, 0x45, 0xc1,
847 0xdf, 0x48, 0xfc, 0xea, 0xc8, 0xc0, 0x7d, 0x0d,
848 0x64, 0xda, 0x21, 0x8b, 0x2b, 0xcd, 0x85, 0xaa,
849 0xd8, 0x67, 0x90, 0x26, 0xb7, 0xc3, 0xed, 0x87,
850 0x11, 0xc4, 0x30, 0x1a, 0x9c, 0x5c, 0x83, 0x89,
851 0x6f, 0x9a, 0xdd, 0x05, 0xd8, 0x03, 0x6a, 0x17,
852 0xd7, 0x56, 0x5f, 0xed, 0xfe, 0x9f, 0x07, 0xe7,
853 0x54, 0xb7, 0xf4, 0x25, 0x8b, 0x4a, 0xd5, 0x89,
854 0xc7, 0x5f, 0xbc, 0x34, 0x7d, 0x51, 0xe3, 0x7f,
855 0x70, 0x93, 0x61, 0x0f, 0x8e, 0xb6, 0x8b, 0xed,
856 0x01, 0xb7, 0xfb, 0x9a, 0x8a, 0x9c, 0x0b, 0xc9,
857 0xfe, 0x29, 0x62, 0xf7, 0x3b, 0x6c, 0xe7, 0xec,
858 0x8d, 0x67, 0x0f, 0xa0, 0xff, 0xa4, 0x9a, 0xab,
859 0xea, 0xc0, 0x52, 0x77, 0xf2, 0x5b, 0xfd, 0xce,
860 0x7e, 0x6f, 0xbf, 0xc6, 0x44, 0xed, 0xe9, 0xe1,
861 0x20, 0x54, 0x6d, 0x26, 0x4b, 0x37, 0xe7, 0x53,
862 0xc9, 0xb4, 0x33, 0x89, 0x63, 0x78, 0xad, 0x35,
863 0x7c, 0x2e, 0xb7, 0x17, 0xd2, 0xd8, 0x8a, 0x5d,
864 0x84, 0x62, 0xf9, 0xae, 0x76, 0xa3, 0x5c, 0x32,
865 0xde, 0x4e, 0xa4, 0xa8, 0x15, 0xab, 0xf2, 0xb2,
866 0x91, 0x92, 0x1e, 0xb4, 0xd9, 0x55, 0xcd, 0xb9,
867 0x90, 0x26, 0x42, 0xb4, 0xcf, 0x99, 0x50, 0x8b,
868 0xd6, 0xb4, 0x73, 0x29, 0xb9, 0x19, 0xc8, 0xde,
869 0x9d, 0x65, 0x59, 0x6f, 0xcc, 0x7a, 0xb4, 0x7e,
870 0x7c, 0xff, 0x76, 0xff, 0x13, 0x01, 0xfd, 0x62,
871 0x77, 0x69, 0x03, 0x00, 0x00, 0x1f, 0x8b, 0x08,
872 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x06,
873 0x00, 0x42, 0x43, 0x02, 0x00, 0xcf, 0x00, 0x4d,
874 0xce, 0x31, 0x6a, 0xc3, 0x30, 0x18, 0x05, 0x60,
875 0x41, 0x0f, 0x50, 0xd2, 0x66, 0x30, 0x74, 0x88,
876 0xab, 0x18, 0xeb, 0x59, 0xb2, 0xa5, 0xff, 0xb7,
877 0xa9, 0x93, 0x8c, 0x32, 0x82, 0x6a, 0x76, 0xb6,
878 0xce, 0x3d, 0x40, 0x2e, 0x90, 0x41, 0xc1, 0x83,
879 0x73, 0xc1, 0xdc, 0xa4, 0x74, 0xad, 0x4b, 0xa0,
880 0xf4, 0x5b, 0xde, 0xe3, 0x4d, 0xef, 0x26, 0xee,
881 0xbe, 0xbe, 0xaf, 0x0f, 0xcf, 0xe2, 0x94, 0x0b,
882 0x71, 0x16, 0x9f, 0xff, 0xb6, 0xdf, 0x3c, 0x8e,
883 0x23, 0xf5, 0x6d, 0xdf, 0xbd, 0x59, 0x3e, 0xf4,
884 0xdd, 0x6e, 0xdf, 0xb6, 0x62, 0xb8, 0xc8, 0xe8,
885 0x63, 0x98, 0x57, 0x29, 0x44, 0x9f, 0xbc, 0x9c,
886 0xa6, 0x4c, 0x66, 0xc5, 0x94, 0x45, 0xa4, 0x39,
887 0xad, 0x83, 0x7f, 0x8a, 0x31, 0x0c, 0x4b, 0x0d,
888 0x98, 0xb3, 0x20, 0xe1, 0xd7, 0xe9, 0x55, 0xe6,
889 0x79, 0xfe, 0x52, 0xa0, 0xd2, 0xa5, 0xde, 0x42,
890 0xab, 0x2d, 0x60, 0xea, 0x1a, 0xc6, 0x28, 0x65,
891 0xa1, 0x01, 0xab, 0x6c, 0xa9, 0x50, 0x35, 0x0a,
892 0x1b, 0xc7, 0xda, 0xb0, 0x61, 0xaa, 0x1d, 0xa1,
893 0xd4, 0xa6, 0x65, 0x36, 0x64, 0xa0, 0x1c, 0x3b,
894 0x66, 0x22, 0x76, 0x8e, 0xc8, 0x71, 0x45, 0x0b,
895 0x0b, 0xba, 0xb3, 0x4d, 0xa3, 0xf1, 0x38, 0xbe,
896 0x7f, 0xfc, 0x7d, 0x15, 0x3f, 0xf2, 0x15, 0x80,
897 0x8f, 0xda, 0x00, 0x00, 0x00, 0x1f, 0x8b, 0x08,
898 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x06,
899 0x00, 0x42, 0x43, 0x02, 0x00, 0x1b, 0x00, 0x03,
900 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
901 0x00,
902 },
903 expect: sam.GroupUnspecified,
904 },
905 {
906 input: []byte{
907 0x1f, 0x8b, 0x08, 0x04, 0x00, 0x00, 0x00, 0x00,
908 0x00, 0xff, 0x06, 0x00, 0x42, 0x43, 0x02, 0x00,
909 0xf3, 0x01, 0xad, 0x92, 0xcd, 0x8e, 0xda, 0x30,
910 0x10, 0xc7, 0xb3, 0xdd, 0x93, 0x79, 0x8a, 0x1c,
911 0x38, 0xb4, 0x07, 0x1c, 0xe7, 0xc3, 0x81, 0x58,
912 0xaa, 0xc4, 0x97, 0x04, 0x48, 0x21, 0xa4, 0x44,
913 0xb4, 0x5a, 0xf5, 0x10, 0x39, 0x89, 0x13, 0xac,
914 0x35, 0x0e, 0xe0, 0x00, 0xdb, 0xbe, 0xc1, 0x3e,
915 0xf1, 0xde, 0xaa, 0x9a, 0x68, 0xdb, 0xae, 0xda,
916 0x1e, 0x39, 0x78, 0x64, 0xff, 0x67, 0xe6, 0x37,
917 0x63, 0x7b, 0xc6, 0xa3, 0xe5, 0xdd, 0xd7, 0x7b,
918 0xc3, 0x18, 0xce, 0xa7, 0xe0, 0x73, 0x44, 0x6c,
919 0x88, 0x40, 0xb2, 0x22, 0x79, 0x5d, 0x1f, 0x0b,
920 0x2e, 0x69, 0xc3, 0xc0, 0x6c, 0x45, 0x64, 0x2d,
921 0x59, 0x67, 0x98, 0x7c, 0x02, 0x49, 0x44, 0x1e,
922 0x40, 0x18, 0x11, 0x1c, 0xb8, 0x7d, 0x17, 0xfb,
923 0x3e, 0x58, 0x62, 0x62, 0x97, 0xd4, 0xf5, 0xfa,
924 0x5e, 0x1f, 0x23, 0x5a, 0xa2, 0xc0, 0x1b, 0x64,
925 0x45, 0x19, 0xf4, 0x0b, 0x4c, 0x11, 0x63, 0xd8,
926 0x61, 0xd8, 0x06, 0x9b, 0x35, 0x29, 0x9b, 0x3d,
927 0xb1, 0x2c, 0x6d, 0xa1, 0x8d, 0x10, 0xaa, 0x98,
928 0xac, 0x77, 0x4c, 0x41, 0x96, 0x71, 0x48, 0x73,
929 0x78, 0x7a, 0xb4, 0xce, 0xb5, 0xb0, 0xaf, 0x6e,
930 0xab, 0x61, 0xf9, 0x56, 0xf2, 0x9c, 0x0a, 0xeb,
931 0xc8, 0x4a, 0x76, 0x64, 0x32, 0x67, 0xd6, 0x7e,
932 0x4b, 0x15, 0x73, 0xd2, 0xdf, 0x42, 0x4a, 0x95,
933 0x62, 0xbb, 0x4c, 0x7c, 0x4b, 0x15, 0x3b, 0x9c,
934 0xda, 0x90, 0xad, 0x72, 0x75, 0x49, 0x58, 0x52,
935 0x58, 0x7d, 0x07, 0xa3, 0x84, 0x44, 0x93, 0xf1,
936 0xc2, 0xed, 0x83, 0x24, 0x26, 0xf3, 0xd3, 0x8e,
937 0xca, 0xce, 0x70, 0x3d, 0x03, 0x8b, 0x29, 0x49,
938 0xd6, 0x6b, 0xe4, 0x3b, 0xbe, 0xeb, 0x81, 0x70,
939 0x4c, 0x9c, 0x81, 0x87, 0x07, 0xd8, 0x1f, 0x60,
940 0x7d, 0xe1, 0x25, 0x99, 0xcf, 0x74, 0x63, 0x81,
941 0x0f, 0xe2, 0x05, 0x71, 0x90, 0x0f, 0x26, 0x11,
942 0xf9, 0xb2, 0x99, 0x25, 0x13, 0x10, 0x87, 0x64,
943 0x11, 0x86, 0x9b, 0xe5, 0x22, 0x1a, 0x81, 0x69,
944 0xa2, 0x09, 0x31, 0x42, 0xb6, 0x13, 0x78, 0x7f,
945 0x23, 0xf1, 0xcd, 0x91, 0x9e, 0x7d, 0x0b, 0x64,
946 0xdc, 0x22, 0xb3, 0x0b, 0x4d, 0xb9, 0x2c, 0xd8,
947 0x13, 0x88, 0xa3, 0xeb, 0xe1, 0xfa, 0xd3, 0x08,
948 0x62, 0x18, 0xf4, 0x8e, 0xb6, 0xc6, 0x84, 0x57,
949 0xcd, 0x6c, 0x03, 0xcc, 0x1e, 0x35, 0xb3, 0x4b,
950 0xa3, 0x2e, 0x66, 0xf7, 0xcf, 0x83, 0x97, 0x54,
951 0x35, 0xf4, 0x2d, 0x8b, 0x0a, 0xd9, 0x8a, 0x87,
952 0x5f, 0xbc, 0x38, 0x7e, 0x53, 0xe3, 0x7f, 0x70,
953 0x9d, 0x61, 0xf6, 0x0e, 0xa6, 0x8d, 0xcd, 0x5e,
954 0x69, 0x76, 0x15, 0xe5, 0x69, 0xc9, 0x05, 0xfb,
955 0xa7, 0x88, 0xd9, 0x6d, 0xb1, 0xad, 0xb3, 0x33,
956 0x9c, 0xac, 0x40, 0xf7, 0x51, 0xd6, 0x17, 0xd9,
957 0x82, 0x85, 0x6a, 0xe5, 0xf7, 0xea, 0x83, 0xf9,
958 0xd1, 0xbc, 0xc5, 0x44, 0xed, 0xe8, 0x7e, 0xcf,
959 0x65, 0xa5, 0x27, 0x4b, 0xd5, 0xa7, 0x63, 0xce,
960 0x94, 0x35, 0x0a, 0x43, 0x78, 0xa9, 0x14, 0x7c,
961 0x2d, 0xb7, 0xe3, 0x42, 0xdb, 0x82, 0x9d, 0xb9,
962 0x64, 0xe9, 0xb6, 0xb2, 0x83, 0x54, 0xb0, 0xb2,
963 0x19, 0x09, 0x5e, 0x49, 0x56, 0xa4, 0x79, 0x2d,
964 0x04, 0xdd, 0x2b, 0xbd, 0x2b, 0xea, 0x53, 0x26,
965 0x74, 0x04, 0x6f, 0x5e, 0x33, 0xa1, 0xe2, 0x8d,
966 0x6e, 0xe7, 0x9c, 0x97, 0x7a, 0x20, 0x3b, 0x77,
967 0x86, 0x61, 0xbc, 0xd3, 0xeb, 0xc1, 0xf8, 0xf1,
968 0xf2, 0x7c, 0xff, 0x13, 0x5a, 0x38, 0x56, 0x0a,
969 0x71, 0x03, 0x00, 0x00, 0x1f, 0x8b, 0x08, 0x04,
970 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x06, 0x00,
971 0x42, 0x43, 0x02, 0x00, 0xcf, 0x00, 0x4d, 0xce,
972 0x31, 0x6a, 0xc3, 0x30, 0x18, 0x05, 0x60, 0x41,
973 0x0f, 0x50, 0xd2, 0x66, 0x30, 0x74, 0x88, 0xab,
974 0x18, 0xeb, 0x59, 0xb2, 0xa5, 0xff, 0xb7, 0xa9,
975 0x93, 0x8c, 0x32, 0x82, 0x6a, 0x76, 0xb6, 0xce,
976 0x3d, 0x40, 0x2e, 0x90, 0x41, 0xc1, 0x83, 0x73,
977 0xc1, 0xdc, 0xa4, 0x74, 0xad, 0x4b, 0xa0, 0xf4,
978 0x5b, 0xde, 0xe3, 0x4d, 0xef, 0x26, 0xee, 0xbe,
979 0xbe, 0xaf, 0x0f, 0xcf, 0xe2, 0x94, 0x0b, 0x71,
980 0x16, 0x9f, 0xff, 0xb6, 0xdf, 0x3c, 0x8e, 0x23,
981 0xf5, 0x6d, 0xdf, 0xbd, 0x59, 0x3e, 0xf4, 0xdd,
982 0x6e, 0xdf, 0xb6, 0x62, 0xb8, 0xc8, 0xe8, 0x63,
983 0x98, 0x57, 0x29, 0x44, 0x9f, 0xbc, 0x9c, 0xa6,
984 0x4c, 0x66, 0xc5, 0x94, 0x45, 0xa4, 0x39, 0xad,
985 0x83, 0x7f, 0x8a, 0x31, 0x0c, 0x4b, 0x0d, 0x98,
986 0xb3, 0x20, 0xe1, 0xd7, 0xe9, 0x55, 0xe6, 0x79,
987 0xfe, 0x52, 0xa0, 0xd2, 0xa5, 0xde, 0x42, 0xab,
988 0x2d, 0x60, 0xea, 0x1a, 0xc6, 0x28, 0x65, 0xa1,
989 0x01, 0xab, 0x6c, 0xa9, 0x50, 0x35, 0x0a, 0x1b,
990 0xc7, 0xda, 0xb0, 0x61, 0xaa, 0x1d, 0xa1, 0xd4,
991 0xa6, 0x65, 0x36, 0x64, 0xa0, 0x1c, 0x3b, 0x66,
992 0x22, 0x76, 0x8e, 0xc8, 0x71, 0x45, 0x0b, 0x0b,
993 0xba, 0xb3, 0x4d, 0xa3, 0xf1, 0x38, 0xbe, 0x7f,
994 0xfc, 0x7d, 0x15, 0x3f, 0xf2, 0x15, 0x80, 0x8f,
995 0xda, 0x00, 0x00, 0x00, 0x1f, 0x8b, 0x08, 0x04,
996 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x06, 0x00,
997 0x42, 0x43, 0x02, 0x00, 0x1b, 0x00, 0x03, 0x00,
998 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
999 },
1000 expect: sam.GroupNone,
1001 },
1002 {
1003 input: []byte{
1004 0x1f, 0x8b, 0x08, 0x04, 0x00, 0x00, 0x00, 0x00,
1005 0x00, 0xff, 0x06, 0x00, 0x42, 0x43, 0x02, 0x00,
1006 0xf5, 0x01, 0xad, 0x92, 0xcb, 0x8e, 0xda, 0x30,
1007 0x14, 0x86, 0x33, 0x9d, 0x95, 0x79, 0x8a, 0x2c,
1008 0x58, 0xb4, 0x0b, 0x1c, 0xe7, 0xe2, 0x40, 0x2c,
1009 0x55, 0xe2, 0x26, 0x01, 0x52, 0x08, 0x29, 0x11,
1010 0xad, 0x46, 0xaa, 0x14, 0x39, 0x89, 0x13, 0xac,
1011 0x31, 0x0e, 0x13, 0x07, 0xe8, 0xf4, 0x0d, 0xfa,
1012 0xc4, 0xdd, 0x55, 0x35, 0xd1, 0xb4, 0x1d, 0xb5,
1013 0x5d, 0xb2, 0xf0, 0x91, 0x7d, 0x2e, 0xdf, 0x7f,
1014 0x6c, 0x9f, 0xe9, 0x64, 0x7d, 0xf7, 0xf9, 0xde,
1015 0x30, 0xc6, 0xcb, 0x39, 0xf8, 0x18, 0x11, 0x1b,
1016 0x22, 0x90, 0x6c, 0x48, 0x5e, 0xd7, 0x4d, 0xc1,
1017 0x25, 0x6d, 0x19, 0x58, 0x6c, 0xc8, 0xd3, 0x89,
1018 0x35, 0xcf, 0xbd, 0x71, 0xf2, 0x01, 0x24, 0x11,
1019 0x79, 0x00, 0x61, 0x44, 0x70, 0xe0, 0x0e, 0x5d,
1020 0xec, 0xfb, 0x60, 0x8d, 0x89, 0x5d, 0x52, 0xd7,
1021 0x1b, 0x7a, 0x43, 0x8c, 0x68, 0x89, 0x02, 0x6f,
1022 0x94, 0x15, 0x65, 0x30, 0x2c, 0x30, 0x45, 0x8c,
1023 0x61, 0x87, 0x61, 0x1b, 0xec, 0xb6, 0xa4, 0x6c,
1024 0x8f, 0xc4, 0xb2, 0xb4, 0x85, 0x36, 0x42, 0xa8,
1025 0x62, 0xb2, 0x3e, 0x30, 0x05, 0x59, 0xc6, 0x21,
1026 0xcd, 0xe1, 0xe9, 0xd1, 0x3a, 0xd7, 0xc2, 0xbe,
1027 0x86, 0xad, 0x96, 0xe5, 0x7b, 0xc9, 0x73, 0x2a,
1028 0xac, 0x86, 0x95, 0xac, 0x61, 0x32, 0x67, 0xd6,
1029 0x71, 0x4f, 0x15, 0x73, 0xd2, 0xdf, 0x8e, 0x94,
1030 0x2a, 0xc5, 0x0e, 0x99, 0x78, 0x4e, 0x15, 0xd3,
1031 0xad, 0x5d, 0x53, 0xf6, 0xca, 0xd5, 0x92, 0xb0,
1032 0xa4, 0xb0, 0xfa, 0x0a, 0x26, 0x09, 0x89, 0x66,
1033 0xd3, 0x95, 0x3b, 0x04, 0x49, 0x4c, 0x96, 0xa7,
1034 0x03, 0x95, 0xbd, 0xf1, 0x76, 0x01, 0x56, 0x73,
1035 0x92, 0x6c, 0xb7, 0xc8, 0x77, 0x7c, 0xd7, 0x03,
1036 0xe1, 0x94, 0x38, 0x23, 0x0f, 0x8f, 0xb0, 0x3f,
1037 0xc2, 0xfa, 0xc6, 0x6b, 0xb2, 0x5c, 0xe8, 0xc6,
1038 0x02, 0x1f, 0xc4, 0x2b, 0xe2, 0x20, 0x1f, 0xcc,
1039 0x22, 0xf2, 0x69, 0xb7, 0x48, 0x66, 0x20, 0x0e,
1040 0xc9, 0x2a, 0x0c, 0x77, 0xeb, 0x55, 0x34, 0x01,
1041 0xf3, 0x44, 0x13, 0x62, 0x84, 0x6c, 0x27, 0xf0,
1042 0xfe, 0x46, 0xe2, 0x9b, 0x23, 0x3d, 0xfb, 0x16,
1043 0xc8, 0xb8, 0x43, 0x66, 0x17, 0x9a, 0x72, 0x59,
1044 0xb0, 0x2f, 0x20, 0x8e, 0xae, 0x87, 0xeb, 0x57,
1045 0x23, 0x88, 0x61, 0x30, 0x68, 0x6c, 0x8d, 0x09,
1046 0xaf, 0x3e, 0xb3, 0x4b, 0x30, 0x07, 0xd4, 0xcc,
1047 0x2e, 0xad, 0xba, 0x98, 0xfd, 0x3f, 0x0f, 0x5e,
1048 0x52, 0xd5, 0xd2, 0xd7, 0x2c, 0x2a, 0x64, 0xe7,
1049 0x7c, 0xfa, 0xc5, 0x8b, 0xe3, 0x57, 0x1a, 0xff,
1050 0x83, 0xeb, 0x0a, 0x73, 0xf0, 0x64, 0xda, 0xd8,
1051 0x1c, 0x94, 0x66, 0x5f, 0x51, 0x9e, 0x96, 0x5c,
1052 0xb0, 0x7f, 0x44, 0xcc, 0x7e, 0x87, 0xed, 0x82,
1053 0xbd, 0xf1, 0x6c, 0x03, 0xfa, 0x8f, 0xb2, 0xbe,
1054 0xc8, 0x0e, 0x2c, 0x54, 0xe7, 0x7e, 0xab, 0xde,
1055 0x99, 0xef, 0xcd, 0x5b, 0x4c, 0xd4, 0x81, 0x1e,
1056 0x8f, 0x5c, 0x56, 0x7a, 0xb2, 0x54, 0x7d, 0x6a,
1057 0x72, 0xa6, 0xac, 0x49, 0x18, 0xc2, 0x4b, 0xa5,
1058 0xe0, 0x8b, 0xdc, 0x81, 0x0b, 0x6d, 0x0b, 0x76,
1059 0xe6, 0x92, 0xa5, 0xfb, 0xca, 0x0e, 0x52, 0xc1,
1060 0xca, 0x76, 0x22, 0x78, 0x25, 0x59, 0x91, 0xe6,
1061 0xb5, 0x10, 0xf4, 0xa8, 0xf4, 0xae, 0xa8, 0x4f,
1062 0x99, 0xd0, 0x19, 0xbc, 0x7d, 0xa9, 0x84, 0x8a,
1063 0xb7, 0xba, 0x9d, 0x73, 0x5e, 0xea, 0x81, 0xec,
1064 0xdd, 0x19, 0x86, 0xf1, 0x46, 0xaf, 0x07, 0xe3,
1065 0xc7, 0xf7, 0x6f, 0xf7, 0x3f, 0x01, 0x63, 0x09,
1066 0x85, 0xc2, 0x72, 0x03, 0x00, 0x00, 0x1f, 0x8b,
1067 0x08, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff,
1068 0x06, 0x00, 0x42, 0x43, 0x02, 0x00, 0xcf, 0x00,
1069 0x4d, 0xce, 0x31, 0x6a, 0xc3, 0x30, 0x18, 0x05,
1070 0x60, 0x41, 0x0f, 0x50, 0xd2, 0x66, 0x30, 0x74,
1071 0x88, 0xab, 0x18, 0xeb, 0x59, 0xb2, 0xa5, 0xff,
1072 0xb7, 0xa9, 0x93, 0x8c, 0x32, 0x82, 0x6a, 0x76,
1073 0xb6, 0xce, 0x3d, 0x40, 0x2e, 0x90, 0x41, 0xc1,
1074 0x83, 0x73, 0xc1, 0xdc, 0xa4, 0x74, 0xad, 0x4b,
1075 0xa0, 0xf4, 0x5b, 0xde, 0xe3, 0x4d, 0xef, 0x26,
1076 0xee, 0xbe, 0xbe, 0xaf, 0x0f, 0xcf, 0xe2, 0x94,
1077 0x0b, 0x71, 0x16, 0x9f, 0xff, 0xb6, 0xdf, 0x3c,
1078 0x8e, 0x23, 0xf5, 0x6d, 0xdf, 0xbd, 0x59, 0x3e,
1079 0xf4, 0xdd, 0x6e, 0xdf, 0xb6, 0x62, 0xb8, 0xc8,
1080 0xe8, 0x63, 0x98, 0x57, 0x29, 0x44, 0x9f, 0xbc,
1081 0x9c, 0xa6, 0x4c, 0x66, 0xc5, 0x94, 0x45, 0xa4,
1082 0x39, 0xad, 0x83, 0x7f, 0x8a, 0x31, 0x0c, 0x4b,
1083 0x0d, 0x98, 0xb3, 0x20, 0xe1, 0xd7, 0xe9, 0x55,
1084 0xe6, 0x79, 0xfe, 0x52, 0xa0, 0xd2, 0xa5, 0xde,
1085 0x42, 0xab, 0x2d, 0x60, 0xea, 0x1a, 0xc6, 0x28,
1086 0x65, 0xa1, 0x01, 0xab, 0x6c, 0xa9, 0x50, 0x35,
1087 0x0a, 0x1b, 0xc7, 0xda, 0xb0, 0x61, 0xaa, 0x1d,
1088 0xa1, 0xd4, 0xa6, 0x65, 0x36, 0x64, 0xa0, 0x1c,
1089 0x3b, 0x66, 0x22, 0x76, 0x8e, 0xc8, 0x71, 0x45,
1090 0x0b, 0x0b, 0xba, 0xb3, 0x4d, 0xa3, 0xf1, 0x38,
1091 0xbe, 0x7f, 0xfc, 0x7d, 0x15, 0x3f, 0xf2, 0x15,
1092 0x80, 0x8f, 0xda, 0x00, 0x00, 0x00, 0x1f, 0x8b,
1093 0x08, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff,
1094 0x06, 0x00, 0x42, 0x43, 0x02, 0x00, 0x1b, 0x00,
1095 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1096 0x00, 0x00,
1097 },
1098 expect: sam.GroupQuery,
1099 },
1100 {
1101 input: []byte{
1102 0x1f, 0x8b, 0x08, 0x04, 0x00, 0x00, 0x00, 0x00,
1103 0x00, 0xff, 0x06, 0x00, 0x42, 0x43, 0x02, 0x00,
1104 0xf1, 0x01, 0xad, 0x92, 0xcb, 0x8e, 0xda, 0x30,
1105 0x14, 0x86, 0x33, 0x9d, 0x95, 0x79, 0x8a, 0x2c,
1106 0x58, 0xb4, 0x0b, 0x1c, 0xe7, 0xe2, 0x40, 0x2c,
1107 0x55, 0xe2, 0x26, 0x01, 0x52, 0x08, 0x29, 0x11,
1108 0xad, 0x66, 0x95, 0x3a, 0x89, 0x13, 0xac, 0x31,
1109 0x0e, 0xe0, 0x00, 0x6d, 0xdf, 0xa0, 0x4f, 0x3c,
1110 0xbb, 0x51, 0x4d, 0x34, 0x9d, 0x8e, 0xda, 0x2e,
1111 0x59, 0xf8, 0xc8, 0x3e, 0x97, 0xef, 0x3f, 0xb6,
1112 0xcf, 0x78, 0xb4, 0xbc, 0xfb, 0x7a, 0x6f, 0x18,
1113 0xc3, 0xf9, 0x14, 0x7c, 0x8e, 0x88, 0x0d, 0x11,
1114 0x48, 0x56, 0x24, 0xaf, 0xeb, 0x63, 0xc1, 0x25,
1115 0x6d, 0x18, 0x98, 0xad, 0xc8, 0x91, 0x95, 0xec,
1116 0xc8, 0x64, 0xce, 0x3a, 0xc3, 0xe4, 0x13, 0x48,
1117 0x22, 0xf2, 0x00, 0xc2, 0x88, 0xe0, 0xc0, 0xed,
1118 0xbb, 0xd8, 0xf7, 0xc1, 0x12, 0x13, 0xbb, 0xa4,
1119 0xae, 0xd7, 0xf7, 0xfa, 0x18, 0xd1, 0x12, 0x05,
1120 0xde, 0x20, 0x2b, 0xca, 0xa0, 0x5f, 0x60, 0x8a,
1121 0x18, 0xc3, 0x0e, 0xc3, 0x36, 0xd8, 0xac, 0x49,
1122 0xd9, 0xec, 0x89, 0x65, 0x69, 0x0b, 0x6d, 0x84,
1123 0x50, 0xc5, 0x64, 0xbd, 0x63, 0x0a, 0xb2, 0x8c,
1124 0x43, 0x9a, 0xc3, 0xd3, 0xa3, 0x75, 0xae, 0x85,
1125 0x7d, 0x0d, 0x5b, 0x0d, 0xcb, 0xb7, 0x92, 0xe7,
1126 0x54, 0x58, 0xaf, 0xc2, 0xd6, 0x7e, 0x4b, 0x15,
1127 0x73, 0xd2, 0x57, 0x47, 0x4a, 0x95, 0x62, 0xbb,
1128 0x4c, 0x7c, 0x4f, 0x15, 0x3b, 0x9c, 0xda, 0x94,
1129 0xad, 0x72, 0xb5, 0x24, 0x2c, 0x29, 0xac, 0x7e,
1130 0x80, 0x51, 0x42, 0xa2, 0xc9, 0x78, 0xe1, 0xf6,
1131 0x41, 0x12, 0x93, 0xf9, 0x69, 0x47, 0x65, 0x67,
1132 0xb8, 0x9e, 0x81, 0xc5, 0x94, 0x24, 0xeb, 0x35,
1133 0xf2, 0x1d, 0xdf, 0xf5, 0x40, 0x38, 0x26, 0xce,
1134 0xc0, 0xc3, 0x03, 0xec, 0x0f, 0xb0, 0xbe, 0xf5,
1135 0x92, 0xcc, 0x67, 0xba, 0xb1, 0xc0, 0x07, 0xf1,
1136 0x82, 0x38, 0xc8, 0x07, 0x93, 0x88, 0x7c, 0xd9,
1137 0xcc, 0x92, 0x09, 0x88, 0x43, 0xb2, 0x08, 0xc3,
1138 0xcd, 0x72, 0x11, 0x8d, 0xc0, 0x34, 0xd1, 0x84,
1139 0x18, 0x21, 0xdb, 0x09, 0xbc, 0xbf, 0x91, 0xf8,
1140 0xe6, 0x48, 0xcf, 0xbe, 0x05, 0x32, 0x6e, 0x91,
1141 0xd9, 0x85, 0xa6, 0x5c, 0x16, 0xec, 0x1b, 0x88,
1142 0xa3, 0xeb, 0xe1, 0xfa, 0xdd, 0x08, 0x62, 0x18,
1143 0xf4, 0x8e, 0xb6, 0xc6, 0x84, 0x57, 0x9f, 0xd9,
1144 0x26, 0x98, 0x3d, 0x6a, 0x66, 0x97, 0x46, 0x5d,
1145 0xcc, 0xee, 0x9f, 0x07, 0x2f, 0xa9, 0x6a, 0xe8,
1146 0x5b, 0x16, 0x15, 0xb2, 0x75, 0x1e, 0x7e, 0xf3,
1147 0xe2, 0xf8, 0x8d, 0xc6, 0xff, 0xe0, 0xba, 0xc2,
1148 0xec, 0x1d, 0x4c, 0x1b, 0x9b, 0xbd, 0xd2, 0xec,
1149 0x2a, 0xca, 0xd3, 0x92, 0x0b, 0xf6, 0x8f, 0x88,
1150 0xd9, 0x6d, 0xb1, 0x6d, 0xb0, 0x33, 0x9c, 0xac,
1151 0x40, 0xf7, 0x51, 0xd6, 0x17, 0xd9, 0x82, 0x85,
1152 0x6a, 0xdd, 0xef, 0xd5, 0x07, 0xf3, 0xa3, 0x79,
1153 0x8b, 0x89, 0xda, 0xd1, 0xfd, 0x9e, 0xcb, 0x4a,
1154 0x4f, 0x96, 0xaa, 0x4f, 0xc7, 0x9c, 0x29, 0x6b,
1155 0x14, 0x86, 0xf0, 0x52, 0x29, 0xf8, 0x22, 0xb7,
1156 0xe3, 0x42, 0xdb, 0x82, 0x9d, 0xb9, 0x64, 0xe9,
1157 0xb6, 0xb2, 0x83, 0x54, 0xb0, 0xb2, 0x19, 0x09,
1158 0x5e, 0x49, 0x56, 0xa4, 0x79, 0x2d, 0x04, 0xdd,
1159 0x2b, 0xbd, 0x2b, 0xea, 0x53, 0x26, 0x74, 0x06,
1160 0x6f, 0x5e, 0x2a, 0xa1, 0xe2, 0x8d, 0x6e, 0xe7,
1161 0x9c, 0x97, 0x7a, 0x20, 0x3b, 0x77, 0x86, 0x61,
1162 0xbc, 0xd3, 0xeb, 0xc1, 0x78, 0x7e, 0xfa, 0x79,
1163 0xff, 0x0b, 0xf6, 0x33, 0x1f, 0x79, 0x76, 0x03,
1164 0x00, 0x00, 0x1f, 0x8b, 0x08, 0x04, 0x00, 0x00,
1165 0x00, 0x00, 0x00, 0xff, 0x06, 0x00, 0x42, 0x43,
1166 0x02, 0x00, 0xcf, 0x00, 0x4d, 0xce, 0x31, 0x6a,
1167 0xc3, 0x30, 0x18, 0x05, 0x60, 0x41, 0x0f, 0x50,
1168 0xd2, 0x66, 0x30, 0x74, 0x88, 0xab, 0x18, 0xeb,
1169 0x59, 0xb2, 0xa5, 0xff, 0xb7, 0xa9, 0x93, 0x8c,
1170 0x32, 0x82, 0x6a, 0x76, 0xb6, 0xce, 0x3d, 0x40,
1171 0x2e, 0x90, 0x41, 0xc1, 0x83, 0x73, 0xc1, 0xdc,
1172 0xa4, 0x74, 0xad, 0x4b, 0xa0, 0xf4, 0x5b, 0xde,
1173 0xe3, 0x4d, 0xef, 0x26, 0xee, 0xbe, 0xbe, 0xaf,
1174 0x0f, 0xcf, 0xe2, 0x94, 0x0b, 0x71, 0x16, 0x9f,
1175 0xff, 0xb6, 0xdf, 0x3c, 0x8e, 0x23, 0xf5, 0x6d,
1176 0xdf, 0xbd, 0x59, 0x3e, 0xf4, 0xdd, 0x6e, 0xdf,
1177 0xb6, 0x62, 0xb8, 0xc8, 0xe8, 0x63, 0x98, 0x57,
1178 0x29, 0x44, 0x9f, 0xbc, 0x9c, 0xa6, 0x4c, 0x66,
1179 0xc5, 0x94, 0x45, 0xa4, 0x39, 0xad, 0x83, 0x7f,
1180 0x8a, 0x31, 0x0c, 0x4b, 0x0d, 0x98, 0xb3, 0x20,
1181 0xe1, 0xd7, 0xe9, 0x55, 0xe6, 0x79, 0xfe, 0x52,
1182 0xa0, 0xd2, 0xa5, 0xde, 0x42, 0xab, 0x2d, 0x60,
1183 0xea, 0x1a, 0xc6, 0x28, 0x65, 0xa1, 0x01, 0xab,
1184 0x6c, 0xa9, 0x50, 0x35, 0x0a, 0x1b, 0xc7, 0xda,
1185 0xb0, 0x61, 0xaa, 0x1d, 0xa1, 0xd4, 0xa6, 0x65,
1186 0x36, 0x64, 0xa0, 0x1c, 0x3b, 0x66, 0x22, 0x76,
1187 0x8e, 0xc8, 0x71, 0x45, 0x0b, 0x0b, 0xba, 0xb3,
1188 0x4d, 0xa3, 0xf1, 0x38, 0xbe, 0x7f, 0xfc, 0x7d,
1189 0x15, 0x3f, 0xf2, 0x15, 0x80, 0x8f, 0xda, 0x00,
1190 0x00, 0x00, 0x1f, 0x8b, 0x08, 0x04, 0x00, 0x00,
1191 0x00, 0x00, 0x00, 0xff, 0x06, 0x00, 0x42, 0x43,
1192 0x02, 0x00, 0x1b, 0x00, 0x03, 0x00, 0x00, 0x00,
1193 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1194 },
1195 expect: sam.GroupReference,
1196 },
1197 }
1198
1199 func (s *S) TestIssue13(c *check.C) {
1200 br, err := NewReader(bytes.NewReader(issue13), *conc)
1201 c.Assert(err, check.Equals, nil, check.Commentf("Failed to open BAM"))
1202
1203 r, err := br.Read()
1204 c.Assert(err, check.Equals, nil, check.Commentf("Failed to read BAM record"))
1205
1206 end := r.End()
1207 c.Check(end, check.Equals, 4391)
1208 }
1209
1210 // This is BAM data provided in issue 12 [sic] from SAM file in comment 4.
1211 // Issue 12 as described is working as intended.
1212 //
1213 // @HD VN:1.0 SO:coordinate
1214 // @SQ SN:Zv9_NA129 LN:9079
1215 // 10 0 Zv9_NA129 4272 50 40M75N5M * 0 0 TGACGCAGAGGTGCGCAAGACCTCCAAGAAGCAGCTGAAGGACAC DDDDHGHHHJI<ABGGIHGIGGHIJEEEGGGCBFCEHIIFFHECI AS:i:-3 XN:i:0 XM:i:1 XO:i:0 XG:i:0 NM:i:1 MD:Z:0C44 YT:Z:UU XS:A:+ NH:i:1
1216 var issue13 = []byte{
1217 0x1f, 0x8b, 0x08, 0x04, 0x00, 0x00, 0x00, 0x00,
1218 0x00, 0xff, 0x06, 0x00, 0x42, 0x43, 0x02, 0x00,
1219 0x61, 0x00, 0x73, 0x72, 0xf4, 0x65, 0x34, 0x62,
1220 0x60, 0x60, 0x70, 0xf0, 0x70, 0xe1, 0x0c, 0xf3,
1221 0xb3, 0x32, 0xd4, 0x33, 0xe0, 0x0c, 0xf6, 0xb7,
1222 0x4a, 0xce, 0xcf, 0x2f, 0x4a, 0xc9, 0xcc, 0x4b,
1223 0x2c, 0x49, 0xe5, 0x72, 0x08, 0x0e, 0xe4, 0x0c,
1224 0xf6, 0xb3, 0x8a, 0x2a, 0xb3, 0x8c, 0xf7, 0x73,
1225 0x34, 0x34, 0xb2, 0xe4, 0xf4, 0xf1, 0xb3, 0xb2,
1226 0x34, 0x30, 0xb7, 0xe4, 0x62, 0x04, 0xea, 0xe2,
1227 0x02, 0x62, 0xb8, 0x0c, 0x43, 0xb9, 0x32, 0x03,
1228 0x03, 0x00, 0x03, 0x99, 0x97, 0x23, 0x50, 0x00,
1229 0x00, 0x00, 0x1f, 0x8b, 0x08, 0x04, 0x00, 0x00,
1230 0x00, 0x00, 0x00, 0xff, 0x06, 0x00, 0x42, 0x43,
1231 0x02, 0x00, 0xab, 0x00, 0x2d, 0x89, 0x41, 0x0a,
1232 0xc2, 0x30, 0x10, 0x45, 0xa7, 0xd6, 0x03, 0xd8,
1233 0x92, 0x9d, 0x9b, 0x26, 0xa6, 0x33, 0x53, 0x44,
1234 0xa8, 0xd2, 0x0b, 0x24, 0x11, 0x6c, 0x17, 0xad,
1235 0x42, 0x2d, 0xc4, 0x6e, 0xbd, 0x88, 0x88, 0x17,
1236 0xf1, 0x04, 0x2e, 0x3c, 0x9f, 0x68, 0x44, 0x1f,
1237 0x7c, 0xfe, 0xfb, 0xfc, 0x3b, 0xfc, 0x78, 0xcc,
1238 0x00, 0xe2, 0x4d, 0x93, 0xc6, 0xc1, 0x57, 0x21,
1239 0xef, 0x3f, 0xdf, 0x6f, 0x5d, 0xc2, 0x65, 0x02,
1240 0xf0, 0x9c, 0x02, 0x1c, 0xc2, 0xbc, 0xa5, 0x56,
1241 0x88, 0xda, 0xda, 0xc4, 0xa8, 0xab, 0x14, 0x89,
1242 0x15, 0x6c, 0x84, 0x91, 0xd9, 0x22, 0x40, 0x48,
1243 0x44, 0x05, 0xcf, 0x33, 0x89, 0xc8, 0x84, 0x8c,
1244 0x48, 0x5c, 0x68, 0xad, 0x11, 0x51, 0xc9, 0x5c,
1245 0x69, 0x62, 0xce, 0x73, 0xd2, 0x8a, 0x4d, 0x7f,
1246 0x7e, 0xf9, 0xce, 0x81, 0x6f, 0x5d, 0xe4, 0xf7,
1247 0xa1, 0x77, 0x0e, 0xba, 0xe0, 0xed, 0x76, 0x2c,
1248 0x5d, 0x55, 0xc1, 0xe9, 0x38, 0x0e, 0x03, 0xf8,
1249 0xde, 0x2c, 0xbb, 0xda, 0x45, 0x1f, 0xf4, 0x97,
1250 0xb4, 0x05, 0xa5, 0x00, 0x00, 0x00, 0x1f, 0x8b,
1251 0x08, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff,
1252 0x06, 0x00, 0x42, 0x43, 0x02, 0x00, 0x1b, 0x00,
1253 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1254 0x00, 0x00,
1255 }
1256
1257 func (s *S) TestIssueDate(c *check.C) {
1258 br, err := NewReader(bytes.NewReader(issueDate), *conc)
1259 c.Assert(err, check.Equals, nil, check.Commentf("Failed to open BAM"))
1260
1261 rg := br.Header().RGs()
1262 c.Assert(len(rg), check.Equals, 1)
1263 expect := time.Date(2014, 03, 25, 12, 26, 51, 0, time.FixedZone("", -int(7*time.Hour/time.Second)))
1264 c.Check(rg[0].Time(), check.DeepEquals, expect)
1265 }
1266
1267 // This is BAM data from the following SAM.
1268 //
1269 // @HD VN:1.0
1270 // @SQ SN:ref LN:1
1271 // @RG ID:rg DT:2014-03-25T12:26:51-0700
1272 // r1 125 ref -1 0 * = -1 0 A B RG:Z:rg
1273 var issueDate = []byte{
1274 0x1f, 0x8b, 0x08, 0x04, 0x00, 0x00, 0x00, 0x00,
1275 0x00, 0xff, 0x06, 0x00, 0x42, 0x43, 0x02, 0x00,
1276 0x6d, 0x00, 0x73, 0x72, 0xf4, 0x65, 0x74, 0x64,
1277 0x60, 0x60, 0x70, 0xf0, 0x70, 0xe1, 0x0c, 0xf3,
1278 0xb3, 0x32, 0xd4, 0x33, 0xe0, 0x72, 0x08, 0x0e,
1279 0xe4, 0x0c, 0xf6, 0xb3, 0x2a, 0x4a, 0x4d, 0xe3,
1280 0xf4, 0x01, 0x8a, 0x70, 0x39, 0x04, 0xb9, 0x73,
1281 0x7a, 0xba, 0x58, 0x15, 0xa5, 0x73, 0xba, 0x84,
1282 0x58, 0x19, 0x19, 0x18, 0x9a, 0xe8, 0x1a, 0x18,
1283 0xeb, 0x1a, 0x99, 0x86, 0x18, 0x1a, 0x59, 0x19,
1284 0x99, 0x59, 0x99, 0x1a, 0xea, 0x1a, 0x98, 0x1b,
1285 0x18, 0x70, 0x31, 0x02, 0x0d, 0x61, 0x01, 0x62,
1286 0xa0, 0x36, 0x06, 0x10, 0x1b, 0x00, 0x38, 0x08,
1287 0xd9, 0xec, 0x59, 0x00, 0x00, 0x00, 0x1f, 0x8b,
1288 0x08, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff,
1289 0x06, 0x00, 0x42, 0x43, 0x02, 0x00, 0x38, 0x00,
1290 0xd3, 0x66, 0x80, 0x80, 0xff, 0x40, 0xc0, 0xcc,
1291 0xe0, 0x21, 0xc4, 0xc0, 0x50, 0xcb, 0xc0, 0x88,
1292 0x24, 0x06, 0xa2, 0x8b, 0x0c, 0x19, 0x04, 0x14,
1293 0x83, 0xdc, 0xa3, 0x8a, 0xd2, 0x19, 0x00, 0xe6,
1294 0x18, 0xef, 0x3c, 0x2f, 0x00, 0x00, 0x00, 0x1f,
1295 0x8b, 0x08, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00,
1296 0xff, 0x06, 0x00, 0x42, 0x43, 0x02, 0x00, 0x1b,
1297 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1298 0x00, 0x00, 0x00,
1299 }
1300
1301 var baiTestData = []struct {
1302 data []byte
1303 expect *Index
1304 err error
1305 }{
1306 {
1307 /*
1308 0x42, 0x41, 0x49, 0x01, // BAI\1
1309 0x01, 0x00, 0x00, 0x00, // n_ref
1310
1311 0x02, 0x00, 0x00, 0x00, // n_bin
1312
1313 0x49, 0x12, 0x00, 0x00, // bin
1314 0x01, 0x00, 0x00, 0x00, // n_chunk
1315
1316 0x00, 0x00, 0x62, 0x00, 0x00, 0x00, 0x00, 0x00, // chunk_beg
1317 0x00, 0x00, 0x91, 0x01, 0x00, 0x00, 0x00, 0x00, // chunk_end
1318
1319 0x4a, 0x92, 0x00, 0x00, // bin - always 0x924a
1320 0x02, 0x00, 0x00, 0x00, // n_chunk - always 2
1321
1322 0x00, 0x00, 0x62, 0x00, 0x00, 0x00, 0x00, 0x00, // unmapped_beg
1323 0x00, 0x00, 0x91, 0x01, 0x00, 0x00, 0x00, 0x00, // unmapped_end
1324
1325 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mapped_count
1326 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // unmapped_count
1327
1328 0x01, 0x00, 0x00, 0x00, // n_intv
1329
1330 0x00, 0x00, 0x62, 0x00, 0x00, 0x00, 0x00, 0x00, // ioffset
1331
1332 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // n_no_coor
1333 */
1334 data: []byte{
1335 0x42, 0x41, 0x49, 0x01, 0x01, 0x00, 0x00, 0x00,
1336 0x02, 0x00, 0x00, 0x00, 0x49, 0x12, 0x00, 0x00,
1337 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x62, 0x00,
1338 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x91, 0x01,
1339 0x00, 0x00, 0x00, 0x00, 0x4a, 0x92, 0x00, 0x00,
1340 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x62, 0x00,
1341 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x91, 0x01,
1342 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
1343 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
1344 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
1345 0x00, 0x00, 0x62, 0x00, 0x00, 0x00, 0x00, 0x00,
1346 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1347 },
1348 expect: &Index{idx: internal.Index{
1349 Refs: []internal.RefIndex{
1350 {
1351 Bins: []internal.Bin{
1352 {
1353 Bin: 4681, Chunks: []bgzf.Chunk{
1354 {Begin: bgzf.Offset{File: 98, Block: 0}, End: bgzf.Offset{File: 401, Block: 0}},
1355 },
1356 },
1357 },
1358 Stats: &internal.ReferenceStats{
1359 Chunk: bgzf.Chunk{Begin: bgzf.Offset{File: 98, Block: 0}, End: bgzf.Offset{File: 401, Block: 0}},
1360 Mapped: 8,
1361 Unmapped: 1,
1362 },
1363 Intervals: []bgzf.Offset{{File: 98, Block: 0}},
1364 },
1365 },
1366 Unmapped: uint64ptr(1),
1367 IsSorted: true,
1368 LastRecord: maxInt,
1369 }},
1370 err: nil,
1371 },
1372 {
1373 /*
1374 0x42, 0x41, 0x49, 0x01, // BAI\1
1375 0x01, 0x00, 0x00, 0x00, // n_ref
1376
1377 0x02, 0x00, 0x00, 0x00, // n_bin
1378
1379 0x49, 0x12, 0x00, 0x00, // bin
1380 0x01, 0x00, 0x00, 0x00, // n_chunk
1381
1382 0x00, 0x00, 0x62, 0x00, 0x00, 0x00, 0x00, 0x00, // chunk_beg
1383 0x00, 0x00, 0x91, 0x01, 0x00, 0x00, 0x00, 0x00, // chunk_end
1384
1385 0x4a, 0x92, 0x00, 0x00, // bin - always 0x924a
1386 0x02, 0x00, 0x00, 0x00, // n_chunk - always 2
1387
1388 0x00, 0x00, 0x62, 0x00, 0x00, 0x00, 0x00, 0x00, // unmapped_beg
1389 0x00, 0x00, 0x91, 0x01, 0x00, 0x00, 0x00, 0x00, // unmapped_end
1390
1391 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mapped_count
1392 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // unmapped_count
1393
1394 0x01, 0x00, 0x00, 0x00, // n_intv
1395
1396 0x00, 0x00, 0x62, 0x00, 0x00, 0x00, 0x00, 0x00, // ioffset
1397 */
1398 data: []byte{
1399 0x42, 0x41, 0x49, 0x01, 0x01, 0x00, 0x00, 0x00,
1400 0x02, 0x00, 0x00, 0x00, 0x49, 0x12, 0x00, 0x00,
1401 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x62, 0x00,
1402 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x91, 0x01,
1403 0x00, 0x00, 0x00, 0x00, 0x4a, 0x92, 0x00, 0x00,
1404 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x62, 0x00,
1405 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x91, 0x01,
1406 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
1407 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
1408 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
1409 0x00, 0x00, 0x62, 0x00, 0x00, 0x00, 0x00, 0x00,
1410 },
1411 expect: &Index{idx: internal.Index{
1412 Refs: []internal.RefIndex{
1413 {
1414 Bins: []internal.Bin{
1415 {
1416 Bin: 4681, Chunks: []bgzf.Chunk{
1417 {Begin: bgzf.Offset{File: 98, Block: 0}, End: bgzf.Offset{File: 401, Block: 0}},
1418 },
1419 },
1420 },
1421 Stats: &internal.ReferenceStats{
1422 Chunk: bgzf.Chunk{Begin: bgzf.Offset{File: 98, Block: 0}, End: bgzf.Offset{File: 401, Block: 0}},
1423 Mapped: 8,
1424 Unmapped: 1,
1425 },
1426 Intervals: []bgzf.Offset{{File: 98, Block: 0}},
1427 },
1428 },
1429 Unmapped: nil,
1430 IsSorted: true,
1431 LastRecord: maxInt,
1432 }},
1433 err: nil,
1434 },
1435 {
1436 /*
1437 0x42, 0x41, 0x49, 0x01, // BAI\1
1438 0x01, 0x00, 0x00, 0x00, // n_ref
1439
1440 0x01, 0x00, 0x00, 0x00, // n_bin
1441
1442 0x49, 0x12, 0x00, 0x00, // bin
1443 0x01, 0x00, 0x00, 0x00, // n_chunk
1444
1445 0x00, 0x00, 0x62, 0x00, 0x00, 0x00, 0x00, 0x00, // chunk_beg
1446 0x00, 0x00, 0x91, 0x01, 0x00, 0x00, 0x00, 0x00, // chunk_end
1447
1448 0x01, 0x00, 0x00, 0x00, // n_intv
1449
1450 0x00, 0x00, 0x62, 0x00, 0x00, 0x00, 0x00, 0x00, // ioffset
1451
1452 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // n_no_coor
1453 */
1454 data: []byte{
1455 0x42, 0x41, 0x49, 0x01, 0x01, 0x00, 0x00, 0x00,
1456 0x01, 0x00, 0x00, 0x00, 0x49, 0x12, 0x00, 0x00,
1457 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x62, 0x00,
1458 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x91, 0x01,
1459 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
1460 0x00, 0x00, 0x62, 0x00, 0x00, 0x00, 0x00, 0x00,
1461 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1462 },
1463 expect: &Index{idx: internal.Index{
1464 Refs: []internal.RefIndex{
1465 {
1466 Bins: []internal.Bin{
1467 {
1468 Bin: 4681, Chunks: []bgzf.Chunk{
1469 {Begin: bgzf.Offset{File: 98, Block: 0}, End: bgzf.Offset{File: 401, Block: 0}},
1470 },
1471 },
1472 },
1473 Stats: nil,
1474 Intervals: []bgzf.Offset{{File: 98, Block: 0}},
1475 },
1476 },
1477 Unmapped: uint64ptr(1),
1478 IsSorted: true,
1479 LastRecord: maxInt,
1480 }},
1481 err: nil,
1482 },
1483 {
1484 /*
1485 0x42, 0x41, 0x49, 0x01, // BAI\1
1486 0x01, 0x00, 0x00, 0x00, // n_ref
1487
1488 0x01, 0x00, 0x00, 0x00, // n_bin
1489
1490 0x49, 0x12, 0x00, 0x00, // bin
1491 0x01, 0x00, 0x00, 0x00, // n_chunk
1492
1493 0x00, 0x00, 0x62, 0x00, 0x00, 0x00, 0x00, 0x00, // chunk_beg
1494 0x00, 0x00, 0x91, 0x01, 0x00, 0x00, 0x00, 0x00, // chunk_end
1495
1496 0x01, 0x00, 0x00, 0x00, // n_intv
1497
1498 0x00, 0x00, 0x62, 0x00, 0x00, 0x00, 0x00, 0x00, // ioffset
1499 */
1500 data: []byte{
1501 0x42, 0x41, 0x49, 0x01, 0x01, 0x00, 0x00, 0x00,
1502 0x01, 0x00, 0x00, 0x00, 0x49, 0x12, 0x00, 0x00,
1503 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x62, 0x00,
1504 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x91, 0x01,
1505 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
1506 0x00, 0x00, 0x62, 0x00, 0x00, 0x00, 0x00, 0x00,
1507 },
1508 expect: &Index{idx: internal.Index{
1509 Refs: []internal.RefIndex{
1510 {
1511 Bins: []internal.Bin{
1512 {
1513 Bin: 4681, Chunks: []bgzf.Chunk{
1514 {Begin: bgzf.Offset{File: 98, Block: 0}, End: bgzf.Offset{File: 401, Block: 0}},
1515 },
1516 },
1517 },
1518 Stats: nil,
1519 Intervals: []bgzf.Offset{{File: 98, Block: 0}},
1520 },
1521 },
1522 Unmapped: nil,
1523 IsSorted: true,
1524 LastRecord: maxInt,
1525 }},
1526 err: nil,
1527 },
1528 {
1529 /*
1530 0x42, 0x41, 0x49, 0x01, // BAI\1
1531 0x56, 0x00, 0x00, 0x00, // n_ref
1532
1533 0x00, 0x00, 0x00, 0x00, // empty bins
1534 [ lines removed ]
1535 0x00, 0x00, 0x00, 0x00, // empty intervals
1536
1537 0x02, 0x00, 0x00, 0x00, // n_bins - ref 23
1538 0x4a, 0x92, 0x00, 0x00, // bin - always 0x924a
1539 0x02, 0x00, 0x00, 0x00, // n_chunk - always 2
1540 0x00, 0x00, 0x46, 0x12, 0x00, 0x00, 0x00, 0x00, // unmapped_beg
1541 0xf9, 0x1c, 0x46, 0x12, 0x00, 0x00, 0x00, 0x00, // unmapped_end
1542 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mapped_count
1543 0x1d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // unmapped_count
1544
1545 0x70, 0x20, 0x00, 0x00, // bin
1546 0x01, 0x00, 0x00, 0x00, // n_chunk
1547 0x00, 0x00, 0x46, 0x12, 0x00, 0x00, 0x00, 0x00, // chunk_beg
1548 0xf9, 0x1c, 0x46, 0x12, 0x00, 0x00, 0x00, 0x00, // chunk_end
1549
1550 0x00, 0x00, 0x00, 0x00, // n_intv - ref 23
1551
1552 0x02, 0x00, 0x00, 0x00, // n_bins - ref 24
1553 0x4a, 0x92, 0x00, 0x00, // bin - always 0x924a
1554 0x02, 0x00, 0x00, 0x00, // n_chunk - always 2
1555 0xf9, 0x1c, 0x46, 0x12, 0x00, 0x00, 0x00, 0x00, // unmapped_beg
1556 0x1d, 0x40, 0x46, 0x12, 0x00, 0x00, 0x00, 0x00, // unmapped_end
1557 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mapped_count
1558 0x25, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // unmapped_count
1559
1560 0x4a, 0x12, 0x00, 0x00, // bin
1561 0x01, 0x00, 0x00, 0x00, // n_chunk
1562 0xf9, 0x1c, 0x46, 0x12, 0x00, 0x00, 0x00, 0x00, // chunk_beg
1563 0x1d, 0x40, 0x46, 0x12, 0x00, 0x00, 0x00, 0x00, // chunk_end
1564
1565 0x00, 0x00, 0x00, 0x00, // n_intv - ref 24
1566
1567 0x00, 0x00, 0x00, 0x00, // empty bins
1568 [ lines removed ]
1569 0x00, 0x00, 0x00, 0x00, // empty intervals
1570
1571 0x02, 0x00, 0x00, 0x00, // n_bins - ref 72
1572 0x53, 0x12, 0x00, 0x00, // bin
1573 0x01, 0x00, 0x00, 0x00, // n_chunk
1574 0x1d, 0x40, 0x46, 0x12, 0x00, 0x00, 0x00, 0x00, // chunk_beg
1575 0xf5, 0x41, 0x46, 0x12, 0x00, 0x00, 0x00, 0x00, // chunk_end
1576
1577 0x4a, 0x92, 0x00, 0x00, // bin - always 0x924a
1578 0x02, 0x00, 0x00, 0x00, // n_chunk - always 2
1579 0x1d, 0x40, 0x46, 0x12, 0x00, 0x00, 0x00, 0x00, // unmapped_beg
1580 0xf5, 0x41, 0x46, 0x12, 0x00, 0x00, 0x00, 0x00, // unmapped_end
1581 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mapped_count
1582 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // unmapped_count
1583
1584 0x00, 0x00, 0x00, 0x00, // n_intv - ref 72
1585
1586 0x00, 0x00, 0x00, 0x00, // empty bins
1587 [ lines removed ]
1588 0x00, 0x00, 0x00, 0x00, // empty intervals
1589
1590 0xa4, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // n_no_coor
1591 */
1592 data: []byte{
1593 0x42, 0x41, 0x49, 0x01, 0x56, 0x00, 0x00, 0x00,
1594 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1595 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1596 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1597 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1598 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1599 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1600 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1601 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1602 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1603 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1604 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1605 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1606 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1607 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1608 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1609 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1610 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1611 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1612 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1613 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1614 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1615 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1616 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1617 0x02, 0x00, 0x00, 0x00, 0x4a, 0x92, 0x00, 0x00,
1618 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46, 0x12,
1619 0x00, 0x00, 0x00, 0x00, 0xf9, 0x1c, 0x46, 0x12,
1620 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1621 0x00, 0x00, 0x00, 0x00, 0x1d, 0x00, 0x00, 0x00,
1622 0x00, 0x00, 0x00, 0x00, 0x70, 0x20, 0x00, 0x00,
1623 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46, 0x12,
1624 0x00, 0x00, 0x00, 0x00, 0xf9, 0x1c, 0x46, 0x12,
1625 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1626 0x02, 0x00, 0x00, 0x00, 0x4a, 0x92, 0x00, 0x00,
1627 0x02, 0x00, 0x00, 0x00, 0xf9, 0x1c, 0x46, 0x12,
1628 0x00, 0x00, 0x00, 0x00, 0x1d, 0x40, 0x46, 0x12,
1629 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1630 0x00, 0x00, 0x00, 0x00, 0x25, 0x00, 0x00, 0x00,
1631 0x00, 0x00, 0x00, 0x00, 0x4a, 0x12, 0x00, 0x00,
1632 0x01, 0x00, 0x00, 0x00, 0xf9, 0x1c, 0x46, 0x12,
1633 0x00, 0x00, 0x00, 0x00, 0x1d, 0x40, 0x46, 0x12,
1634 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1635 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1636 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1637 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1638 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1639 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1640 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1641 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1642 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1643 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1644 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1645 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1646 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1647 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1648 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1649 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1650 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1651 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1652 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1653 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1654 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1655 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1656 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1657 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1658 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1659 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1660 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1661 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1662 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1663 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1664 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1665 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1666 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1667 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1668 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1669 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1670 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1671 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1672 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1673 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1674 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1675 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1676 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1677 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1678 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1679 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1680 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1681 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1682 0x02, 0x00, 0x00, 0x00, 0x53, 0x12, 0x00, 0x00,
1683 0x01, 0x00, 0x00, 0x00, 0x1d, 0x40, 0x46, 0x12,
1684 0x00, 0x00, 0x00, 0x00, 0xf5, 0x41, 0x46, 0x12,
1685 0x00, 0x00, 0x00, 0x00, 0x4a, 0x92, 0x00, 0x00,
1686 0x02, 0x00, 0x00, 0x00, 0x1d, 0x40, 0x46, 0x12,
1687 0x00, 0x00, 0x00, 0x00, 0xf5, 0x41, 0x46, 0x12,
1688 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1689 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
1690 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1691 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1692 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1693 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1694 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1695 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1696 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1697 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1698 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1699 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1700 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1701 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1702 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1703 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1704 0xa4, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1705 },
1706 expect: &Index{idx: internal.Index{
1707 Refs: func() []internal.RefIndex {
1708 idx := [86]internal.RefIndex{
1709 23: {
1710 Bins: []internal.Bin{
1711 {
1712 Bin: 0x2070,
1713 Chunks: []bgzf.Chunk{
1714 {
1715 Begin: bgzf.Offset{File: 0x1246, Block: 0x0},
1716 End: bgzf.Offset{File: 0x1246, Block: 0x1cf9},
1717 },
1718 },
1719 },
1720 },
1721 Stats: &internal.ReferenceStats{
1722 Chunk: bgzf.Chunk{
1723 Begin: bgzf.Offset{File: 0x1246, Block: 0x0},
1724 End: bgzf.Offset{File: 0x1246, Block: 0x1cf9},
1725 },
1726 Mapped: 0, Unmapped: 0x1d,
1727 },
1728 Intervals: nil,
1729 },
1730 24: {
1731 Bins: []internal.Bin{
1732 {
1733 Bin: 0x124a,
1734 Chunks: []bgzf.Chunk{
1735 {
1736 Begin: bgzf.Offset{File: 0x1246, Block: 0x1cf9},
1737 End: bgzf.Offset{File: 0x1246, Block: 0x401d},
1738 },
1739 },
1740 },
1741 },
1742 Stats: &internal.ReferenceStats{
1743 Chunk: bgzf.Chunk{
1744 Begin: bgzf.Offset{File: 0x1246, Block: 0x1cf9},
1745 End: bgzf.Offset{File: 0x1246, Block: 0x401d},
1746 },
1747 Mapped: 0, Unmapped: 0x25,
1748 },
1749 Intervals: nil,
1750 },
1751 72: {
1752 Bins: []internal.Bin{
1753 {
1754 Bin: 0x1253,
1755 Chunks: []bgzf.Chunk{
1756 {
1757 Begin: bgzf.Offset{File: 0x1246, Block: 0x401d},
1758 End: bgzf.Offset{File: 0x1246, Block: 0x41f5},
1759 },
1760 },
1761 },
1762 },
1763 Stats: &internal.ReferenceStats{
1764 Chunk: bgzf.Chunk{
1765 Begin: bgzf.Offset{File: 0x1246, Block: 0x401d},
1766 End: bgzf.Offset{File: 0x1246, Block: 0x41f5},
1767 },
1768 Mapped: 0, Unmapped: 0x2,
1769 },
1770 Intervals: nil,
1771 },
1772 }
1773 return idx[:]
1774 }(),
1775 Unmapped: uint64ptr(932),
1776 IsSorted: true,
1777 LastRecord: maxInt,
1778 }},
1779 err: nil,
1780 },
1781 }
1782
1783 func uint64ptr(i uint64) *uint64 {
1784 return &i
1785 }
1786
1787 func (s *S) TestReadBAI(c *check.C) {
1788 for _, test := range baiTestData {
1789 bai, err := ReadIndex(bytes.NewReader(test.data))
1790 c.Assert(err, check.Equals, test.err)
1791 c.Check(bai, check.DeepEquals, test.expect)
1792 }
1793 }
1794
1795 // conceptualBAIdata is gzip compressed BAI for the alignments in the BAM
1796 // corresponding to:
1797 //
1798 // @HD VN:1.0 SO:coordinate
1799 // @SQ SN:conceptual LN:134217728
1800 // 60m66m:bin0 0 conceptual 62914561 40 6291456M * 0 0 * *
1801 // 70m76m:bin2 0 conceptual 73400321 40 6291456M * 0 0 * *
1802 // 73m75m:bin18 0 conceptual 76546049 40 2097152M * 0 0 * *
1803 //
1804 // This is a coordinate-translated version of the conceptual example in the
1805 // SAM spec using binning as actually used by BAM rather than as presented.
1806 var conceptualBAIdata = []byte{
1807 0x1f, 0x8b, 0x08, 0x08, 0xe3, 0xf6, 0x2c, 0x54,
1808 0x00, 0x03, 0x63, 0x6f, 0x6e, 0x63, 0x65, 0x70,
1809 0x74, 0x75, 0x61, 0x6c, 0x2e, 0x62, 0x61, 0x6d,
1810 0x2e, 0x62, 0x61, 0x69, 0x00, 0xed, 0xcb, 0x41,
1811 0x0d, 0x85, 0x30, 0x10, 0x40, 0xc1, 0x2d, 0xa0,
1812 0x00, 0x2d, 0x18, 0x80, 0x1b, 0xf8, 0x20, 0x41,
1813 0x0f, 0x8a, 0x10, 0xf3, 0x85, 0xfc, 0xa4, 0xb4,
1814 0x07, 0x08, 0x0a, 0x60, 0xe6, 0xb0, 0x2f, 0x6d,
1815 0xb7, 0xd3, 0x38, 0xa7, 0x14, 0x11, 0x5d, 0x9c,
1816 0x52, 0x9e, 0x6b, 0x9e, 0x43, 0x69, 0x53, 0xee,
1817 0xeb, 0x79, 0x2b, 0x5d, 0xf6, 0xf3, 0xad, 0xee,
1818 0x47, 0xfc, 0xf2, 0x6c, 0xe3, 0xaa, 0x2f, 0xff,
1819 0xb7, 0xdb, 0xde, 0xd1, 0x07, 0x00, 0x00, 0x00,
1820 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1821 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1822 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1823 0x00, 0x00, 0xf0, 0x68, 0x55, 0x55, 0x55, 0x55,
1824 0x55, 0x55, 0x55, 0x55, 0x55, 0xd5, 0x97, 0x76,
1825 0x50, 0x55, 0x55, 0x55, 0xd5, 0x4f, 0xb5, 0xfa,
1826 0x03, 0x6e, 0x81, 0xb2, 0xab, 0x88, 0x96, 0x00,
1827 0x00,
1828 }
1829
1830 var conceptualBAMdata = []byte{
1831 // sam.Header block [{File:0, Block:0}, {File:0, Block:87}).
1832 0x1f, 0x8b, 0x08, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff,
1833 0x06, 0x00, 0x42, 0x43, 0x02, 0x00, 0x64, 0x00, 0x73, 0x72,
1834 0xf4, 0x65, 0xb4, 0x60, 0x60, 0x60, 0x70, 0xf0, 0x70, 0xe1,
1835 0x0c, 0xf3, 0xb3, 0x32, 0xd4, 0x33, 0xe0, 0x0c, 0xf6, 0xb7,
1836 0x4a, 0xce, 0xcf, 0x2f, 0x4a, 0xc9, 0xcc, 0x4b, 0x2c, 0x49,
1837 0xe5, 0x72, 0x08, 0x0e, 0xe4, 0x0c, 0xf6, 0x03, 0x8a, 0xe4,
1838 0x25, 0xa7, 0x16, 0x94, 0x94, 0x26, 0xe6, 0x70, 0xfa, 0x00,
1839 0x95, 0x19, 0x9b, 0x18, 0x19, 0x9a, 0x9b, 0x1b, 0x59, 0x70,
1840 0x31, 0x02, 0xf5, 0x72, 0x03, 0x31, 0x42, 0x1e, 0xc8, 0x61,
1841 0xe0, 0x00, 0x00, 0x42, 0x51, 0xcc, 0xea, 0x57, 0x00, 0x00,
1842 0x00,
1843
1844 // Record block [{File:101, Block:0}, {File:101, Block:157}).
1845 0x1f, 0x8b, 0x08, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff,
1846 0x06, 0x00, 0x42, 0x43, 0x02, 0x00, 0x62, 0x00, 0x33, 0x60,
1847 0x80, 0x81, 0x03, 0xcc, 0x3c, 0x1a, 0x0c, 0x0c, 0x8c, 0x50,
1848 0xde, 0x7f, 0x28, 0x00, 0xb1, 0xcd, 0x0c, 0x72, 0xcd, 0xcc,
1849 0x72, 0xad, 0x92, 0x32, 0xf3, 0x0c, 0x40, 0x5c, 0x36, 0x03,
1850 0xb8, 0x9e, 0x04, 0x16, 0x1e, 0x0d, 0x26, 0xac, 0x7a, 0xcc,
1851 0x0d, 0x72, 0xcd, 0x21, 0x7a, 0x8c, 0xc0, 0x7a, 0x0c, 0xe1,
1852 0x7a, 0x26, 0xb0, 0xf0, 0x6a, 0x08, 0x61, 0xd7, 0x63, 0x9c,
1853 0x6b, 0x6e, 0x0a, 0xd6, 0x63, 0x68, 0x01, 0xe2, 0x33, 0x01,
1854 0x00, 0x5a, 0x80, 0xfe, 0xec, 0x9d, 0x00, 0x00, 0x00,
1855
1856 // Magic block [{File:200, Block:0}, {File:200, Block:0}).
1857 0x1f, 0x8b, 0x08, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff,
1858 0x06, 0x00, 0x42, 0x43, 0x02, 0x00, 0x1b, 0x00, 0x03, 0x00,
1859 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1860
1861 // End {File:228, Block:0}
1862 }
1863
1864 // conceptualChunks differs from the chunks described in chunkTests
1865 // since samtools defines the end offset of the last chunk as the
1866 // end of the file. We define it as the end of the chunk itself.
1867 var conceptualChunks = []bgzf.Chunk{
1868 {Begin: bgzf.Offset{File: 0, Block: 0}, End: bgzf.Offset{File: 0, Block: 87}}, // header
1869 {Begin: bgzf.Offset{File: 101, Block: 0}, End: bgzf.Offset{File: 101, Block: 52}}, // 60m66m:bin0
1870 {Begin: bgzf.Offset{File: 101, Block: 52}, End: bgzf.Offset{File: 101, Block: 104}}, // 70m76m:bin2
1871 {Begin: bgzf.Offset{File: 101, Block: 104}, End: bgzf.Offset{File: 101, Block: 157}}, // 73m75m:bin18
1872 {Begin: bgzf.Offset{File: 101, Block: 153}, End: bgzf.Offset{File: 101, Block: 157}}, // EOF - not checked.
1873 }
1874
1875 func (s *S) TestConceptualBAM(c *check.C) {
1876 br, err := NewReader(bytes.NewReader(conceptualBAMdata), *conc)
1877 c.Assert(err, check.Equals, nil)
1878 c.Check(br.LastChunk(), check.Equals, conceptualChunks[0])
1879 for _, chunk := range conceptualChunks[1:] {
1880 _, err := br.Read()
1881 if err != nil {
1882 break
1883 }
1884 c.Check(br.LastChunk(), check.Equals, chunk)
1885 }
1886 }
1887
1888 var chunkTests = []struct {
1889 beg, end int
1890 expect []bgzf.Chunk
1891 err error
1892 }{
1893 {
1894 beg: 65000, end: 71000, // Not in covered region.
1895 expect: []bgzf.Chunk{
1896 {Begin: bgzf.Offset{File: 101, Block: 0}, End: bgzf.Offset{File: 101, Block: 52}},
1897 },
1898 err: nil,
1899 },
1900 {
1901 beg: 77594624, end: 80740352, // 73m77m:bin2+bin18 - This is the equivalent to the given example.
1902 expect: []bgzf.Chunk{
1903 {Begin: bgzf.Offset{File: 101, Block: 52}, End: bgzf.Offset{File: 228, Block: 0}},
1904 },
1905 err: nil,
1906 },
1907 {
1908 beg: 62914561, end: 68157440, // 60m65m:bin0+bin2
1909 expect: []bgzf.Chunk{
1910 {Begin: bgzf.Offset{File: 101, Block: 0}, End: bgzf.Offset{File: 101, Block: 104}},
1911 },
1912 err: nil,
1913 },
1914 {
1915 beg: 72351744, end: 80740352, // 69m77m:bin0+bin2+18
1916 expect: []bgzf.Chunk{
1917 {Begin: bgzf.Offset{File: 101, Block: 0}, End: bgzf.Offset{File: 228, Block: 0}},
1918 },
1919 err: nil,
1920 },
1921 {
1922 beg: 61865984, end: 80740352, // 59m77m:bin0+bin2+bin18
1923 expect: []bgzf.Chunk{
1924 {Begin: bgzf.Offset{File: 101, Block: 0}, End: bgzf.Offset{File: 228, Block: 0}},
1925 },
1926 err: nil,
1927 },
1928 {
1929 beg: 80740352, end: 81788928, // 77m78m - Not in covered region.
1930 expect: nil,
1931 err: index.ErrInvalid,
1932 },
1933 }
1934
1935 func (s *S) TestConceptualBAI(c *check.C) {
1936 gz, err := gzip.NewReader(bytes.NewReader(conceptualBAIdata))
1937 c.Assert(err, check.Equals, nil)
1938 bai, err := ReadIndex(gz)
1939 c.Assert(err, check.Equals, nil)
1940
1941 var ref sam.Reference
1942 var h sam.Header
1943 h.AddReference(&ref)
1944
1945 for _, test := range chunkTests {
1946 chunks, err := bai.Chunks(&ref, test.beg, test.end)
1947 c.Check(err, check.Equals, test.err,
1948 check.Commentf("Unexpected error for [%d,%d).", test.beg, test.end),
1949 )
1950 c.Check(chunks, check.DeepEquals, test.expect,
1951 check.Commentf("Unexpected result for [%d,%d).", test.beg, test.end),
1952 )
1953 }
1954 }
1955
1956 // @HD VN:1.0 SO:coordinate
1957 // @SQ SN:conceptual LN:134217728
1958 // 60m66m:bin0 0 conceptual 62914561 40 6291456M * 0 0 * *
1959 // 70m76m:bin2 0 conceptual 73400321 40 6291456M * 0 0 * *
1960 // 73m75m:bin18 0 conceptual 76546049 40 2097152M * 0 0 * *
1961 var (
1962 conceptual = func() *sam.Reference {
1963 ref, err := sam.NewReference("conceptual", "0", "unicorns", 134217728, nil, nil)
1964 if err != nil {
1965 panic("Failed to initialise conceptual reference")
1966 }
1967 // Ensure the reference id is valid.
1968 sam.NewHeader(nil, []*sam.Reference{ref})
1969 return ref
1970 }()
1971 bamFile = []struct {
1972 rec *sam.Record
1973 chunk bgzf.Chunk
1974 }{
1975 {
1976 rec: &sam.Record{
1977 Name: "60m66m:bin0", // [62914560,69206016)
1978 Ref: conceptual,
1979 Pos: 62914560,
1980 MapQ: 40,
1981 Cigar: sam.Cigar{
1982 sam.NewCigarOp(sam.CigarMatch, 6291456),
1983 },
1984 },
1985 chunk: bgzf.Chunk{
1986 Begin: bgzf.Offset{File: 101, Block: 0},
1987 End: bgzf.Offset{File: 101, Block: 52},
1988 },
1989 },
1990 {
1991 rec: &sam.Record{
1992 Name: "70m76m:bin2", // [73400320,79691776)
1993 Ref: conceptual,
1994 Pos: 73400320,
1995 MapQ: 40,
1996 Cigar: sam.Cigar{
1997 sam.NewCigarOp(sam.CigarMatch, 6291456),
1998 },
1999 },
2000 chunk: bgzf.Chunk{
2001 Begin: bgzf.Offset{File: 101, Block: 52},
2002 End: bgzf.Offset{File: 101, Block: 104},
2003 },
2004 },
2005 {
2006 rec: &sam.Record{
2007 Name: "73m75m:bin18", // [76546048,78643200)
2008 Ref: conceptual,
2009 Pos: 76546048,
2010 MapQ: 40,
2011 Cigar: sam.Cigar{
2012 sam.NewCigarOp(sam.CigarMatch, 2097152),
2013 },
2014 },
2015 chunk: bgzf.Chunk{
2016 Begin: bgzf.Offset{File: 101, Block: 104},
2017 End: bgzf.Offset{File: 228, Block: 0},
2018 },
2019 },
2020 }
2021 )
2022
2023 func (s *S) TestAdd(c *check.C) {
2024 var bai Index
2025 for _, r := range bamFile {
2026 c.Assert(bai.Add(r.rec, r.chunk), check.Equals, nil)
2027 }
2028 gz, err := gzip.NewReader(bytes.NewReader(conceptualBAIdata))
2029 c.Assert(err, check.Equals, nil)
2030 expect, err := ReadIndex(gz)
2031 c.Check(bai.idx.Refs[0].Bins, check.DeepEquals, expect.idx.Refs[0].Bins)
2032 c.Check(bai.idx.Refs[0].Stats, check.DeepEquals, expect.idx.Refs[0].Stats)
2033 c.Check(bai.idx.Unmapped, check.DeepEquals, expect.idx.Unmapped)
2034
2035 // We check naively for overlap with tiles since we do not merge chunks in Add.
2036 for i := range bai.idx.Refs[0].Intervals {
2037 got := bai.idx.Refs[0].Intervals[i]
2038 tbeg := i * internal.TileWidth
2039 tend := tbeg + internal.TileWidth
2040 hasOverlap := false
2041 for _, r := range bamFile {
2042 if tbeg < r.rec.End() && tend > r.rec.Start() {
2043 c.Check(got, check.Equals, r.chunk.Begin,
2044 check.Commentf("Unexpected tile offset for [%d,%d) got:%+v expect:%+v", tbeg, tend, got, r.chunk.Begin),
2045 )
2046 hasOverlap = true
2047 break
2048 }
2049 }
2050 if !hasOverlap {
2051 c.Check(got, check.Equals, bgzf.Offset{},
2052 check.Commentf("Unexpected non-zero offset for [%d,%d) got:%+v", tbeg, tend, got),
2053 )
2054 }
2055 }
2056 }
2057
2058 var chunkMergeTests = []struct {
2059 index func() *Index
2060
2061 expectAdjacent *Index
2062
2063 expectSquash *Index
2064
2065 compStrat index.MergeStrategy
2066 expectCompress *Index
2067 }{
2068 {
2069 index: func() *Index {
2070 return &Index{idx: internal.Index{
2071 Refs: []internal.RefIndex{
2072 {
2073 Bins: []internal.Bin{
2074 {
2075 Bin: 0,
2076 Chunks: []bgzf.Chunk{
2077 {
2078 Begin: bgzf.Offset{File: 1, Block: 0},
2079 End: bgzf.Offset{File: 2, Block: 0},
2080 },
2081 {
2082 Begin: bgzf.Offset{File: 2, Block: 0},
2083 End: bgzf.Offset{File: 3, Block: 0},
2084 },
2085 },
2086 },
2087 {
2088 Bin: 1,
2089 Chunks: []bgzf.Chunk{
2090 {
2091 Begin: bgzf.Offset{File: 1, Block: 0},
2092 End: bgzf.Offset{File: 2, Block: 0},
2093 },
2094 {
2095 Begin: bgzf.Offset{File: 2, Block: 0},
2096 End: bgzf.Offset{File: 3, Block: 0},
2097 },
2098 },
2099 },
2100 },
2101 },
2102 },
2103 }}
2104 },
2105 expectAdjacent: &Index{idx: internal.Index{
2106 Refs: []internal.RefIndex{
2107 {
2108 Bins: []internal.Bin{
2109 {
2110 Bin: 0,
2111 Chunks: []bgzf.Chunk{
2112 {
2113 Begin: bgzf.Offset{File: 1, Block: 0},
2114 End: bgzf.Offset{File: 3, Block: 0},
2115 },
2116 },
2117 },
2118 {
2119 Bin: 1,
2120 Chunks: []bgzf.Chunk{
2121 {
2122 Begin: bgzf.Offset{File: 1, Block: 0},
2123 End: bgzf.Offset{File: 3, Block: 0},
2124 },
2125 },
2126 },
2127 },
2128 },
2129 },
2130 }},
2131 expectSquash: &Index{idx: internal.Index{
2132 Refs: []internal.RefIndex{
2133 {
2134 Bins: []internal.Bin{
2135 {
2136 Bin: 0,
2137 Chunks: []bgzf.Chunk{
2138 {
2139 Begin: bgzf.Offset{File: 1, Block: 0},
2140 End: bgzf.Offset{File: 3, Block: 0},
2141 },
2142 },
2143 },
2144 {
2145 Bin: 1,
2146 Chunks: []bgzf.Chunk{
2147 {
2148 Begin: bgzf.Offset{File: 1, Block: 0},
2149 End: bgzf.Offset{File: 3, Block: 0},
2150 },
2151 },
2152 },
2153 },
2154 },
2155 },
2156 }},
2157 },
2158 {
2159 index: func() *Index {
2160 return &Index{idx: internal.Index{
2161 Refs: []internal.RefIndex{
2162 {
2163 Bins: []internal.Bin{
2164 {
2165 Bin: 0,
2166 Chunks: []bgzf.Chunk{
2167 {
2168 Begin: bgzf.Offset{File: 1, Block: 0},
2169 End: bgzf.Offset{File: 2, Block: 0},
2170 },
2171 {
2172 Begin: bgzf.Offset{File: 3, Block: 0},
2173 End: bgzf.Offset{File: 4, Block: 0},
2174 },
2175 },
2176 },
2177 {
2178 Bin: 1,
2179 Chunks: []bgzf.Chunk{
2180 {
2181 Begin: bgzf.Offset{File: 1, Block: 0},
2182 End: bgzf.Offset{File: 2, Block: 0},
2183 },
2184 {
2185 Begin: bgzf.Offset{File: 3, Block: 0},
2186 End: bgzf.Offset{File: 4, Block: 0},
2187 },
2188 },
2189 },
2190 },
2191 },
2192 },
2193 }}
2194 },
2195 expectAdjacent: &Index{idx: internal.Index{
2196 Refs: []internal.RefIndex{
2197 {
2198 Bins: []internal.Bin{
2199 {
2200 Bin: 0,
2201 Chunks: []bgzf.Chunk{
2202 {
2203 Begin: bgzf.Offset{File: 1, Block: 0},
2204 End: bgzf.Offset{File: 2, Block: 0},
2205 },
2206 {
2207 Begin: bgzf.Offset{File: 3, Block: 0},
2208 End: bgzf.Offset{File: 4, Block: 0},
2209 },
2210 },
2211 },
2212 {
2213 Bin: 1,
2214 Chunks: []bgzf.Chunk{
2215 {
2216 Begin: bgzf.Offset{File: 1, Block: 0},
2217 End: bgzf.Offset{File: 2, Block: 0},
2218 },
2219 {
2220 Begin: bgzf.Offset{File: 3, Block: 0},
2221 End: bgzf.Offset{File: 4, Block: 0},
2222 },
2223 },
2224 },
2225 },
2226 },
2227 },
2228 }},
2229 expectSquash: &Index{idx: internal.Index{
2230 Refs: []internal.RefIndex{
2231 {
2232 Bins: []internal.Bin{
2233 {
2234 Bin: 0,
2235 Chunks: []bgzf.Chunk{
2236 {
2237 Begin: bgzf.Offset{File: 1, Block: 0},
2238 End: bgzf.Offset{File: 4, Block: 0},
2239 },
2240 },
2241 },
2242 {
2243 Bin: 1,
2244 Chunks: []bgzf.Chunk{
2245 {
2246 Begin: bgzf.Offset{File: 1, Block: 0},
2247 End: bgzf.Offset{File: 4, Block: 0},
2248 },
2249 },
2250 },
2251 },
2252 },
2253 },
2254 }},
2255 },
2256 {
2257 index: func() *Index {
2258 return &Index{idx: internal.Index{
2259 Refs: []internal.RefIndex{
2260 {
2261 Bins: []internal.Bin{
2262 {
2263 Bin: 0,
2264 Chunks: []bgzf.Chunk{
2265 {
2266 Begin: bgzf.Offset{File: 1, Block: 0},
2267 End: bgzf.Offset{File: 2, Block: 0},
2268 },
2269 {
2270 Begin: bgzf.Offset{File: 0xffff, Block: 0},
2271 End: bgzf.Offset{File: 0x10000, Block: 0},
2272 },
2273 },
2274 },
2275 {
2276 Bin: 1,
2277 Chunks: []bgzf.Chunk{
2278 {
2279 Begin: bgzf.Offset{File: 1, Block: 0},
2280 End: bgzf.Offset{File: 2, Block: 0},
2281 },
2282 {
2283 Begin: bgzf.Offset{File: 0x4ffff, Block: 0},
2284 End: bgzf.Offset{File: 0x50000, Block: 0},
2285 },
2286 },
2287 },
2288 },
2289 },
2290 },
2291 }}
2292 },
2293 compStrat: index.CompressorStrategy(0x20000),
2294 expectCompress: &Index{idx: internal.Index{
2295 Refs: []internal.RefIndex{
2296 {
2297 Bins: []internal.Bin{
2298 {
2299 Bin: 0,
2300 Chunks: []bgzf.Chunk{
2301 {
2302 Begin: bgzf.Offset{File: 1, Block: 0},
2303 End: bgzf.Offset{File: 0x10000, Block: 0},
2304 },
2305 },
2306 },
2307 {
2308 Bin: 1,
2309 Chunks: []bgzf.Chunk{
2310 {
2311 Begin: bgzf.Offset{File: 1, Block: 0},
2312 End: bgzf.Offset{File: 2, Block: 0},
2313 },
2314 {
2315 Begin: bgzf.Offset{File: 0x4ffff, Block: 0},
2316 End: bgzf.Offset{File: 0x50000, Block: 0},
2317 },
2318 },
2319 },
2320 },
2321 },
2322 },
2323 }},
2324 },
2325 }
2326
2327 func (s *S) TestMergeChunks(c *check.C) {
2328 var bai *Index
2329 for _, test := range chunkMergeTests {
2330 if test.expectAdjacent != nil {
2331 bai = test.index()
2332 bai.MergeChunks(index.Adjacent)
2333 c.Check(bai, check.DeepEquals, test.expectAdjacent)
2334 }
2335
2336 if test.expectSquash != nil {
2337 bai = test.index()
2338 bai.MergeChunks(index.Squash)
2339 c.Check(bai, check.DeepEquals, test.expectSquash)
2340 }
2341
2342 if test.expectCompress != nil {
2343 bai = test.index()
2344 bai.MergeChunks(test.compStrat)
2345 c.Check(bai, check.DeepEquals, test.expectCompress)
2346 }
2347 }
2348 }
2349
2350 func (s *S) TestIndexRoundtrip(c *check.C) {
2351 for i, test := range baiTestData {
2352 expect := test.expect
2353 var buf bytes.Buffer
2354 c.Assert(WriteIndex(&buf, expect), check.Equals, nil)
2355 got, err := ReadIndex(&buf)
2356 c.Assert(err, check.Equals, nil, check.Commentf("Test %d", i))
2357 c.Check(got, check.DeepEquals, expect, check.Commentf("Test %d", i))
2358 }
2359 }
2360
2361 func (s *S) TestBufferSharing(c *check.C) {
2362 // @HD VN:1.5 GO:none SO:coordinate
2363 // @SQ SN:1 LN:249250621
2364 // A 1187 1 1000025 60 74M = 1000064 113 AGCTGGGGGTCCACTGGGCTCAGGGAAGACCCCCTGCCAGGGAGACCCCAGGCGCCTGAATGGCCACGGGAAGG BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
2365 // A 1107 1 1000064 60 74M = 1000064 -113 AGCTGGGGGTCCACTGGGCTCAGGGAAGACCCCCTGCCAGGGAGACCCCAGGCGCCTGAATGGCCACGGGAAGG DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD
2366 data := []byte{
2367 0x1f, 0x8b, 0x08, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x06, 0x00, 0x42, 0x43, 0x02, 0x00,
2368 0x64, 0x00, 0x73, 0x72, 0xf4, 0x65, 0x34, 0x67, 0x60, 0x60, 0x70, 0xf0, 0x70, 0xe1, 0x0c, 0xf3,
2369 0xb3, 0x32, 0xd4, 0x33, 0xe5, 0x74, 0xf7, 0xb7, 0xca, 0xcb, 0xcf, 0x4b, 0xe5, 0x0c, 0xf6, 0xb7,
2370 0x4a, 0xce, 0xcf, 0x2f, 0x4a, 0xc9, 0xcc, 0x4b, 0x2c, 0x49, 0xe5, 0x72, 0x08, 0x0e, 0xe4, 0x0c,
2371 0x06, 0x2a, 0xe0, 0xf4, 0xf1, 0xb3, 0x32, 0x32, 0xb1, 0x34, 0x32, 0x35, 0x30, 0x33, 0x32, 0xe4,
2372 0x62, 0x04, 0x6a, 0x65, 0x02, 0x62, 0x43, 0x06, 0x5b, 0xe7, 0xdb, 0x7c, 0x00, 0x65, 0x5c, 0xb8,
2373 0xd2, 0x4d, 0x00, 0x00, 0x00, 0x1f, 0x8b, 0x08, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x06,
2374 0x00, 0x42, 0x43, 0x02, 0x00, 0x73, 0x00, 0x9b, 0xca, 0x00, 0x01, 0x11, 0x4e, 0xfc, 0x0c, 0x4c,
2375 0x36, 0x6d, 0x42, 0x8c, 0x0c, 0x8b, 0x59, 0xbc, 0xa0, 0x62, 0xf5, 0x40, 0xb1, 0x42, 0x20, 0xed,
2376 0xc8, 0xb0, 0x80, 0x85, 0x81, 0x41, 0x44, 0xc3, 0xc5, 0xc5, 0x43, 0x49, 0xa8, 0xc5, 0x45, 0x43,
2377 0xd1, 0xc5, 0x51, 0x44, 0x48, 0x49, 0xa9, 0x45, 0x49, 0xc4, 0x05, 0x48, 0x2b, 0xba, 0xa8, 0x28,
2378 0xb5, 0x08, 0xb6, 0x38, 0x29, 0xaa, 0xb8, 0x08, 0xba, 0x28, 0x52, 0x0d, 0x4c, 0x45, 0x72, 0x04,
2379 0xc4, 0x61, 0xc1, 0x28, 0x0e, 0xeb, 0xff, 0xff, 0xff, 0x3f, 0x09, 0x0e, 0x53, 0xa6, 0x1a, 0x00,
2380 0x00, 0x16, 0x09, 0x97, 0x4a, 0x32, 0x01, 0x00, 0x00, 0x1f, 0x8b, 0x08, 0x04, 0x00, 0x00, 0x00,
2381 0x00, 0x00, 0xff, 0x06, 0x00, 0x42, 0x43, 0x02, 0x00, 0x1b, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00,
2382 0x00, 0x00, 0x00, 0x00, 0x00,
2383 }
2384 r, err := NewReader(bytes.NewReader(data), 1)
2385 c.Assert(err, check.Equals, nil)
2386 defer r.Close()
2387 rec, err := r.Read()
2388 c.Assert(err, check.Equals, nil)
2389 recClone := *rec
2390 recClone.Seq.Seq = append(rec.Seq.Seq[:0:0], rec.Seq.Seq...)
2391 recClone.Qual = append(rec.Qual[:0:0], rec.Qual...)
2392 recClone.AuxFields = append(rec.AuxFields[:0:0], rec.AuxFields...)
2393 _, err = r.Read()
2394 c.Assert(err, check.Equals, nil)
2395 if !reflect.DeepEqual(rec, &recClone) {
2396 c.Errorf("unexpected record mutation:\ngot: %v\nwant:%v",
2397 rec, &recClone)
2398 }
2399 }
2400
2401 var fuzzCrashers = []string{
2402 // lText
2403 "BAM\x01000\x86",
2404 "BAM\x01\x00\x00\x00v",
2405 "BAM\x01000\xef",
2406
2407 // nRef
2408 "BAM\x01\x00\x00\x00\x00000\xbd",
2409 "BAM\x01\x00\x00\x00\x000000",
2410 "BAM\x01\x00\x00\x00\x000000\x00\x00\x00\x000",
2411 "BAM\x01\x00\x00\x00\x00000\xbf",
2412
2413 // lName
2414 "BAM\x01\x00\x00\x00\x000000\x00\x00\x00\x000",
2415 "BAM\x01\x00\x00\x00\x00000\xbf",
2416
2417 // reader.go 412
2418 "BAM\x01\xbf\xbdC_t\x0efRRUAe\x80t.V",
2419 "BAM\x01\x00\x00\x00\x00\x00\x00\x00\x00000\xbf" +
2420 "e.Ce&Cell",
2421 "BAM\x01\x00\x00\x00\x00\x00\x00\x00\x00000\xe9",
2422
2423 // Zero size buffer.
2424 "BAM\x01\x00\x00\x00\x00\x00\x9f7R",
2425 // Negative size buffer
2426 "BAM\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
2427
2428 // Short reads.
2429 "BAM\x01\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x000",
2430 "BAM\x01\x00\x00\x00\x00\x00\x00\x00\x00\b\x00\x00\x000000" +
2431 "0000",
2432
2433 // Invalid aux tags.
2434 "BAM\x01\x00\x00\x00\x00\x00\x00\x00\x00\x1c\x00\x00\x000000" +
2435 "000000000000000\xbd0000" +
2436 "0000",
2437 "BAM\x01\x00\x00\x00\x00\x00\x00\x00\x00-\x00\x00\x000000" +
2438 "0000\x04000\x01\x0000\x00\x00\x00\x000000" +
2439 "000000000000000000\x000" +
2440 "0",
2441 "BAM\x01\x00\x00\x00\x00\x00\x00\x00\x00-\x00\x00\x00 \r\n\xbd" +
2442 "\xff\xff\xff\xff\aun\x00\x00\x00\u007f\x00\x00\x00\x00\x00\x00\u007f\x00\x00" +
2443 "\x00 \r\n\xbd\xffxpect\\<invalid" +
2444 " ",
2445 "BAM\x01\x00\x00\x00\x00\x00\x00\x00\x00-\x00\x00\x000000" +
2446 "0000\x01000\x00\x0000\x00\x00\x00\x000000" +
2447 "00000000000000000000" +
2448 "0",
2449 "BAM\x01\x00\x00\x00\x00\x00\x00\x00\x00-\x00\x00\x000000" +
2450 "0000\x06000\x01\x0000\x00\x00\x00\x000000" +
2451 "00000000000000000000" +
2452 "n",
2453 "BAM\x01\x00\x00ewline;�\xbf\xb2\x10e" +
2454 "�\x16\x00\x00\x00\x00\x00\xbf89rf",
2455 "BAM\x01\x00\x00\x00\x00\x00\x00\x00\x00-\x00\x00\x000000" +
2456 "0000\x01000\x00\x0000\x00\x00\x00\x000000" +
2457 "00000000000\\00000000" +
2458 "0",
2459 "BAM\x01\x00\x00\x00\x00\x00\x00\x00\x00-\x00\x00\x000000" +
2460 "0000\a000\x00\x0000\x00\x00\x00\x000000" +
2461 "00000000000000000\b00" +
2462 "0",
2463 "BAM\x01\x00\x00ewline;�\xbf\xb2\x10e" +
2464 "�\x16\x00\x00\x00\x00\x00\xbf89rf",
2465
2466 // lSeq overflow.
2467 "BAM\x01\x00\x00\x00\x00\x00\x00\x00\x000\x00\x00\x000000" +
2468 "000000000000\xff\xff\xff\u007f0000" +
2469 "00000000000000000000" +
2470 "00000000",
2471
2472 // B-type aux data length overflow.
2473 "BAM\x01\x00\x00\x00\x00\x00\x00\x00\x000\x00\x00\x000000" +
2474 "0000\x01000\x01\x0000\x00\x00\x00\x000000" +
2475 "000000000000000BC000" +
2476 "00000000",
2477 "BAM\x01\x00\x00\x00\x00\x00\x00\x00\x000\x00\x00\x000000" +
2478 "0000\x02000\x01\x0000\x00\x00\x00\x000000" +
2479 "0000000000000000BB00" +
2480 "00000000000000000000",
2481 }
2482
2483 func TestFuzzCrashers(t *testing.T) {
•2484 if runtime.GOARCH != "amd64" {
2485 t.Skip("run out of memory")
2486 }
2487 for i, test := range fuzzCrashers {
2488 func() {
2489 i := i
2490 defer func() {
2491 r := recover()
2492 if r != nil {
2493 t.Errorf("unexpected panic for crasher %d: %v", i, r)
2494 }
2495 }()
2496 var buf bytes.Buffer
2497 w := bgzf.NewWriter(&buf, 1)
2498 _, err := w.Write([]byte(test))
2499 if err != nil {
2500 t.Errorf("unexpected error preparing buffer: %v", err)
2501 }
2502 err = w.Close()
2503 if err != nil {
2504 t.Errorf("unexpected error closing buffer: %v", err)
2505 }
2506
2507 r, err := NewReader(&buf, 1)
2508 if err != nil {
2509 return
2510 }
2511 for {
2512 _, err := r.Read()
2513 if err != nil {
2514 break
2515 }
2516 }
2517 }()
2518 }
2519 }