-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathboom.go
756 lines (670 loc) · 19.2 KB
/
boom.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
// Copyright ©2012 The bíogo Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package boom is a wrapper for the samtools bam library.
package boom
// http://samtools.sourceforge.net/samtools/sam/index.html
/*
#cgo CFLAGS: -g -O2 -fPIC -m64 -pthread
#cgo LDFLAGS: -lz
#include "sam.h"
#include "bam_endian.h"
void bam_init_header_hash(bam_header_t *header);
void bam_destroy_header_hash(bam_header_t *header);
void setBin(bam1_t *b, uint16_t bin) { b->core.bin = bin; }
void setQual(bam1_t *b, uint8_t flag) { b->core.flag = flag; }
void setLQname(bam1_t *b, uint8_t l_qname) { b->core.l_qname = l_qname; }
void setFlag(bam1_t *b, uint16_t flag) { b->core.flag = flag; }
void setNCigar(bam1_t *b, uint16_t n_cigar) { b->core.n_cigar = n_cigar; }
*/
import "C"
import (
"encoding/binary"
"errors"
"fmt"
"io"
"reflect"
"runtime"
"unsafe"
)
var (
valueIsNil = fmt.Errorf("boom: value is nil")
notBamFile = fmt.Errorf("boom: not bam file")
couldNotAllocate = fmt.Errorf("boom: could not allocate")
cannotAddr = fmt.Errorf("boom: cannot address value")
bamIsBigEndian = C.bam_is_big_endian() == 1
endian = [2]binary.ByteOrder{
binary.LittleEndian,
binary.BigEndian,
}[C.bam_is_big_endian()]
)
var (
noHeader = errors.New("boom: no header")
)
// Verbosity sets and returns the level of debugging information emitted on stderr by libbam.
// The level of verbosity intrepreted by libbam ranges from 0 to 3 inclusive, with lower values
// being less verbose. Passing values of v outside this range do not alter verbosity.
func Verbosity(v int) int {
if 0 <= v && v <= 3 {
C.bam_verbose = C.int(v)
}
return int(C.bam_verbose)
}
// A bamRecord wraps the bam1_t BAM record.
type bamRecord struct {
b *C.bam1_t
}
// newBamRecord creates a new bamRecord wrapping b or a newly malloc'd bam1_t if b is nil,
// and setting a finaliser that C.free()s the contained bam1_t.
// newBamRecord should always be used unless the bamRecord will be explicitly memory managed, or
// wraps a bam1_t that will be memory managed elsewhere.
func newBamRecord(b *C.bam1_t) (br *bamRecord, err error) {
if b == nil {
b = (*C.bam1_t)(unsafe.Pointer(C.malloc((C.size_t)(unsafe.Sizeof(C.bam1_t{})))))
if b == nil {
return nil, couldNotAllocate
}
*b = C.bam1_t{}
}
br = &bamRecord{b}
runtime.SetFinalizer(br, (*bamRecord).bamRecordFree)
return
}
// The following methods are helpers to safely return bam1_t field values.
// All first check that the pointer to the bam1_t is not nil and convert to the appropriate
// Go type.
func (br *bamRecord) tid() int32 {
if br.b == nil {
panic(valueIsNil)
}
return int32(br.b.core.tid)
}
func (br *bamRecord) setTid(tid int32) {
if br.b == nil {
panic(valueIsNil)
}
br.b.core.tid = C.int32_t(tid)
}
func (br *bamRecord) pos() int32 {
if br.b == nil {
panic(valueIsNil)
}
return int32(br.b.core.pos)
}
func (br *bamRecord) setPos(pos int32) {
if br.b == nil {
panic(valueIsNil)
}
br.b.core.pos = C.int32_t(pos)
}
func (br *bamRecord) bin() uint16 {
if br.b == nil {
panic(valueIsNil)
}
return uint16(br.b.core.bin)
}
func (br *bamRecord) setBin(bin uint16) {
if br.b == nil {
panic(valueIsNil)
}
C.setBin(br.b, C.uint16_t(bin))
}
func (br *bamRecord) qual() byte {
if br.b == nil {
panic(valueIsNil)
}
return byte(br.b.core.qual)
}
func (br *bamRecord) setQual(qual byte) {
if br.b == nil {
panic(valueIsNil)
}
C.setQual(br.b, C.uint8_t(qual))
}
func (br *bamRecord) lQname() byte {
if br.b == nil {
panic(valueIsNil)
}
return byte(br.b.core.l_qname)
}
func (br *bamRecord) setLQname(lQname byte) {
if br.b == nil {
panic(valueIsNil)
}
C.setLQname(br.b, C.uint8_t(lQname))
}
func (br *bamRecord) flag() Flags {
if br.b == nil {
panic(valueIsNil)
}
return Flags(br.b.core.flag)
}
func (br *bamRecord) setFlag(flags Flags) {
if br.b == nil {
panic(valueIsNil)
}
C.setFlag(br.b, C.uint16_t(flags))
}
func (br *bamRecord) nCigar() uint16 {
if br.b == nil {
panic(valueIsNil)
}
return uint16(br.b.core.n_cigar)
}
func (br *bamRecord) setNCigar(nCigar uint16) {
if br.b == nil {
panic(valueIsNil)
}
C.setNCigar(br.b, C.uint16_t(nCigar))
}
func (br *bamRecord) lQseq() int32 {
if br.b == nil {
panic(valueIsNil)
}
return int32(br.b.core.l_qseq)
}
func (br *bamRecord) setLQseq(lQseq int32) {
if br.b == nil {
panic(valueIsNil)
}
br.b.core.l_qseq = C.int32_t(lQseq)
}
func (br *bamRecord) mtid() int32 {
if br.b == nil {
panic(valueIsNil)
}
return int32(br.b.core.mtid)
}
func (br *bamRecord) setMtid() int32 {
if br.b == nil {
panic(valueIsNil)
}
return int32(br.b.core.mtid)
}
func (br *bamRecord) mpos() int32 {
if br.b == nil {
panic(valueIsNil)
}
return int32(br.b.core.mpos)
}
func (br *bamRecord) setMpos(mpos int32) {
if br.b == nil {
panic(valueIsNil)
}
br.b.core.mpos = C.int32_t(mpos)
}
func (br *bamRecord) isize() int32 {
if br.b == nil {
panic(valueIsNil)
}
return int32(br.b.core.isize)
}
func (br *bamRecord) setIsize(isize int32) {
if br.b == nil {
panic(valueIsNil)
}
br.b.core.isize = C.int32_t(isize)
}
func (br *bamRecord) lAux() int32 {
if br.b == nil {
panic(valueIsNil)
}
return int32(br.b.l_aux)
}
func (br *bamRecord) setLAux(lAux int32) {
if br.b == nil {
panic(valueIsNil)
}
br.b.l_aux = C.int(lAux)
}
func (br *bamRecord) dataLen() int {
if br.b == nil {
panic(valueIsNil)
}
return int(br.b.data_len)
}
func (br *bamRecord) dataCap() int {
if br.b == nil {
panic(valueIsNil)
}
return int(br.b.m_data)
}
func (br *bamRecord) dataPtr() uintptr {
if br.b == nil {
panic(valueIsNil)
}
return uintptr(unsafe.Pointer(br.b.data))
}
func (br *bamRecord) dataUnsafe() []byte {
if br.b == nil {
panic(valueIsNil)
}
l := int(br.b.data_len)
var data []byte
sliceHeader := (*reflect.SliceHeader)(unsafe.Pointer(&data))
sliceHeader.Cap = l
sliceHeader.Len = l
sliceHeader.Data = uintptr(unsafe.Pointer(br.b.data))
return data
}
func (br *bamRecord) setDataUnsafe(data []byte) {
if br.b == nil {
panic(valueIsNil)
}
l := len(data)
if br.dataCap() < len(data) {
if br.b.data == nil {
br.b.data = (*C.uint8_t)(unsafe.Pointer(C.malloc((C.size_t)(l))))
} else {
br.b.data = (*C.uint8_t)(unsafe.Pointer(C.realloc(unsafe.Pointer(br.b.data), (C.size_t)(l))))
}
if br.b.data == nil {
panic(couldNotAllocate)
}
}
var newData []byte
sliceHeader := (*reflect.SliceHeader)(unsafe.Pointer(&newData))
sliceHeader.Cap = l
sliceHeader.Len = l
sliceHeader.Data = uintptr(unsafe.Pointer(br.b.data))
copy(newData, data)
}
// bamRecordFree C.free()s the contained bam1_t and its data, first checking for nil pointers.
func (br *bamRecord) bamRecordFree() {
if br.b != nil {
if br.b.data != nil {
C.free(unsafe.Pointer(br.b.data))
}
C.free(unsafe.Pointer(br.b))
br.b = nil
}
}
// A samFile wraps a samfile_t.
type samFile struct {
fp *C.samfile_t
}
// samOpen/samFdOpen open a SAM or BAM file with the given filename/fd, mode and optional auxilliary header.
// According to sam.h:
//
// mode matches /[rw](b?)(u?)(h?)([xX]?)/
//
// 'r' for reading,
// 'w' for writing,
// 'b' for BAM I/O,
// 'u' for uncompressed BAM output,
// 'h' for outputing header in SAM,
// 'x' for HEX flag and
// 'X' for string flag.
//
// If 'b' present, it must immediately follow 'r' or 'w'.
// Valid modes are "r", "w", "wh", "wx", "whx", "wX", "whX", "rb", "wb" and "wbu" exclusively.
//
// If mode[0] == 'w', aux must be a bamHeader.
// If mode[0] == 'r' && mode != "rb" and @SQ header lines in SAM are absent,
// aux must contain the name of a file listing of the reference sequences in SAM format.
// If neither of these conditions is met aux is not used.
func samOpen(filename, mode string, aux header) (sf *samFile, err error) {
fn, m := C.CString(filename), C.CString(mode)
defer C.free(unsafe.Pointer(fn))
defer C.free(unsafe.Pointer(m))
var auxAddr unsafe.Pointer
switch a := aux.(type) {
case textHeader:
if len(a) > 0 {
auxAddr = unsafe.Pointer(&a[0])
} else {
auxAddr = nil
}
case stringHeader:
auxAddr = unsafe.Pointer(C.CString(string(a)))
defer C.free(unsafe.Pointer(auxAddr))
case *bamHeader:
auxAddr = unsafe.Pointer(a.bh)
default:
if aux == nil {
break
}
panic(fmt.Sprintf("boom: wrong type %T", aux))
}
fp, err := C.samopen(
(*C.char)(unsafe.Pointer(fn)),
(*C.char)(unsafe.Pointer(m)),
unsafe.Pointer(auxAddr),
)
sf = &samFile{fp: (*C.samfile_t)(unsafe.Pointer(fp))}
runtime.SetFinalizer(sf, (*samFile).samClose)
return
}
func samFdOpen(fd uintptr, mode string, aux header) (sf *samFile, err error) {
m := C.CString(mode)
defer C.free(unsafe.Pointer(m))
var auxAddr unsafe.Pointer
switch a := aux.(type) {
case textHeader:
if len(a) > 0 {
auxAddr = unsafe.Pointer(&a[0])
} else {
auxAddr = nil
}
case stringHeader:
auxAddr = unsafe.Pointer(C.CString(string(a)))
defer C.free(unsafe.Pointer(auxAddr))
case *bamHeader:
auxAddr = unsafe.Pointer(a.bh)
default:
if aux == nil {
break
}
panic(fmt.Sprintf("boom: wrong type %T", aux))
}
fp, err := C.samdopen(
C.int(fd),
(*C.char)(unsafe.Pointer(m)),
auxAddr,
)
sf = &samFile{fp: (*C.samfile_t)(unsafe.Pointer(fp))}
runtime.SetFinalizer(sf, (*samFile).samClose)
return
}
type bamTypeFlags int
const (
// TODO: Curent definitions are a bit haphazard due to the underlying libbam defs. When tests exist, use 1<<iota.
bamFile bamTypeFlags = iota + 1 // File is a BAM file. Defined in sam.c TYPE_BAM
readFile // File is opened for reading. Defined in sam.c TYPE_READ
hexFlags bamTypeFlags = C.BAM_OFHEX << 2 // Flags are in string format.
strFlags bamTypeFlags = C.BAM_OFSTR << 2 // Flags are in hex format.
)
// fileType returns the type of file wrapped by the samFile struct.
func (sf *samFile) fileType() bamTypeFlags {
if sf.fp != nil {
return bamTypeFlags(sf.fp._type)
}
panic(valueIsNil)
}
// header returns the bamHeader wrapping the bam_header_t associated with sf.fp
func (sf *samFile) header() *bamHeader {
if sf.fp == nil {
return nil
}
return &bamHeader{bh: sf.fp.header}
}
// samClose closes the samFile, freeing the C data allocations as part of C.samclose.
func (sf *samFile) samClose() error {
if sf.fp == nil {
return valueIsNil
}
runtime.SetFinalizer(sf, nil)
if h := sf.header(); h != nil && h.bh != nil && h.bh.hash != nil {
C.bam_destroy_header_hash(
(*C.bam_header_t)(unsafe.Pointer(h.bh)),
)
}
C.samclose((*C.samfile_t)(unsafe.Pointer(sf.fp)))
sf.fp = nil
return nil
}
// samRead reads and returns the next BAM record returning the number of bytes read,
// a *bamRecord containing the record data and any error that occurred.
func (sf *samFile) samRead() (n int, br *bamRecord, err error) {
if sf.fp == nil {
return 0, nil, valueIsNil
}
br, err = newBamRecord(nil)
if err != nil {
return
}
cn, err := C.samread(
(*C.samfile_t)(unsafe.Pointer(sf.fp)),
(*C.bam1_t)(unsafe.Pointer(br.b)),
)
n = int(cn)
if n < 0 {
err = io.EOF
}
return
}
// samWrite writes a BAM record represented by br, returning the number of bytes written
// and any error that occurred.
func (sf *samFile) samWrite(br *bamRecord) (n int, err error) {
if sf.fp == nil || br.b == nil {
return 0, valueIsNil
}
return int(C.samwrite(
(*C.samfile_t)(unsafe.Pointer(sf.fp)),
(*C.bam1_t)(unsafe.Pointer(br.b)),
)), nil
}
// A bamIndex wraps a bam_index_t.
type bamIndex struct {
idx *C.bam_index_t
}
// bamIndexBuild builds a BAM index file, filename.bai, from a bam file, filename. It returns an
// integer value (currently defined as always 0) and any error that occured.
func bamIndexBuild(filename string) (ret int, err error) {
fn := C.CString(filename)
defer C.free(unsafe.Pointer(fn))
r, err := C.bam_index_build(
(*C.char)(unsafe.Pointer(fn)),
)
return int(r), err
}
// bamIndexLoad loads a BAM index, returning a *bamIndex and any error that occurred.
// The error should be checked as a non-nil bamIndex is returned independent of error conditions.
// The bamIndex is created setting a finaliser that C.free()s the contained bam_index_t.
func bamIndexLoad(filename string) (bi *bamIndex, err error) {
fn := C.CString(filename)
defer C.free(unsafe.Pointer(fn))
ip, err := C.bam_index_load(
(*C.char)(unsafe.Pointer(fn)),
)
bi = &bamIndex{idx: (*C.bam_index_t)(unsafe.Pointer(ip))}
runtime.SetFinalizer(bi, (*bamIndex).bamIndexDestroy)
return
}
// bamIndexDestroy C.free()s the contained bam_index_t and its data, first checking for nil pointers.
func (bi *bamIndex) bamIndexDestroy() (err error) {
if bi.idx == nil {
return valueIsNil
}
C.bam_index_destroy(
(*C.bam_index_t)(unsafe.Pointer(bi.idx)),
)
return
}
// A bamFetchFn is called on each bamRecord found by bamFetch. The return value is used to indicate
// the iteration is complete.
type bamFetchFn func(*bamRecord) bool
// bamFetch calls fn on all BAM records within the interval [beg, end) of the reference sequence
// identified by tid. Note that beg >= 0 || beg = 0.
func (sf *samFile) bamFetch(bi *bamIndex, tid, beg, end int, fn bamFetchFn) (ret int, err error) {
if sf.fp == nil || bi.idx == nil {
return 0, valueIsNil
}
if sf.fileType()&bamFile == 0 {
return 0, notBamFile
}
fp := *(*C.bamFile)(unsafe.Pointer(&sf.fp.x))
iter := C.bam_iter_query(bi.idx, C.int(tid), C.int(beg), C.int(end))
var br *bamRecord
for {
br, err = newBamRecord(nil)
if err != nil {
return
}
ret = int(C.bam_iter_read(fp, iter, br.b))
if ret < 0 {
break
}
if fn(br) {
break
}
}
C.bam_iter_destroy(iter)
return
}
// A bamFetchCFn is called on each bam1_t found by bamFetchC and the unsafe.Pointer is passed as a
// pointer to a store of user data. The integer return value is ignored internally by bam_fetch,
// but is specified in the libbam headers.
type bamFetchCFn func(*C.bam1_t, unsafe.Pointer) C.int
// bamFetchC calls fn on all BAM records within the interval [beg, end) of the reference sequence
// identified by tid. Note that beg >= 0 || beg = 0. data is passed to fn.
func (sf *samFile) bamFetchC(bi *bamIndex, tid, beg, end int, data unsafe.Pointer, fn bamFetchCFn) (ret int, err error) {
if sf.fp == nil || bi.idx == nil {
return 0, valueIsNil
}
if sf.fileType()&bamFile == 0 {
return 0, notBamFile
}
r := C.bam_fetch(
*(*C.bamFile)(unsafe.Pointer(&sf.fp.x)),
bi.idx,
C.int(tid),
C.int(beg),
C.int(end),
data,
(*[0]byte)(unsafe.Pointer(&fn)),
)
return int(r), nil
}
// Type header defines types that can be passed to samOpen as a SAM header or header filename.
type header interface {
header() // No-op for interface definition.
}
// A bamHeader wraps a bam_header_t.
type bamHeader struct {
bh *C.bam_header_t
}
// bamGetTid return the target id for for a reference sequence target matching the string, name.
func (bh *bamHeader) bamGetTid(name string) int {
if bh.bh == nil {
panic(valueIsNil)
}
sn := C.CString(name)
defer C.free(unsafe.Pointer(sn))
C.bam_init_header_hash( // This is idempotent - checks against NULL in bam_aux.c
(*C.bam_header_t)(unsafe.Pointer(bh.bh)),
)
tid := C.bam_get_tid(
(*C.bam_header_t)(unsafe.Pointer(bh.bh)),
(*C.char)(unsafe.Pointer(sn)),
)
return int(tid)
}
// nTargets returns the number of reference sequence targets described in the BAM header.
func (bh *bamHeader) nTargets() int32 {
if bh.bh != nil {
return int32(bh.bh.n_targets)
}
panic(valueIsNil)
}
// targetNames returns a slice of strings containing the names of the reference sequence
// targets described in the BAM header.
func (bh *bamHeader) targetNames() (n []string) {
if bh.bh != nil {
n = make([]string, bh.bh.n_targets)
l := int(bh.bh.n_targets)
var nPtrs []*C.char
sh := (*reflect.SliceHeader)(unsafe.Pointer(&nPtrs))
sh.Cap = l
sh.Len = l
sh.Data = uintptr(unsafe.Pointer(bh.bh.target_name))
for i, p := range nPtrs {
n[i] = C.GoString(p)
}
return
}
panic(valueIsNil)
}
// targetLengths returns a slice of uint32 containing the lengths of the reference sequence
// targets described in the BAM header.
func (bh *bamHeader) targetLengths() []uint32 {
if bh.bh != nil {
l := int(bh.bh.n_targets)
var unsafeLengths []uint32
sh := (*reflect.SliceHeader)(unsafe.Pointer(&unsafeLengths))
sh.Cap = l
sh.Len = l
sh.Data = uintptr(unsafe.Pointer(bh.bh.target_len))
return append([]uint32(nil), unsafeLengths...)
}
panic(valueIsNil)
}
// text returns a string containing the full unparsed BAM header.
func (bh *bamHeader) text() (t string) {
if bh.bh != nil {
return C.GoStringN(bh.bh.text, C.int(bh.bh.l_text))
}
panic(valueIsNil)
}
// header is a no-op function required to allow *bamHeader to satisfy the header interface.
func (bh *bamHeader) header() {}
// stringHeader is a string representation of a filename of a SAM header file.
type stringHeader string
// header is a no-op function required to allow stringHeader to satisfy the header interface.
func (sh stringHeader) header() {}
// textHeader is a []byte representation of a filename of a SAM header file.
type textHeader []byte
// header is a no-op function required to allow textHeader to satisfy the header interface.
func (th textHeader) header() {}
const (
Paired Flags = paired // The read is paired in sequencing, no matter whether it is mapped in a pair.
ProperPair Flags = properPair // The read is mapped in a proper pair.
Unmapped Flags = unmapped // The read itself is unmapped; conflictive with BAM_FPROPER_PAIR.
MateUnmapped Flags = mateUnmapped // The mate is unmapped.
Reverse Flags = reverse // The read is mapped to the reverse strand.
MateReverse Flags = mateReverse // The mate is mapped to the reverse strand.
Read1 Flags = read1 // This is read1.
Read2 Flags = read2 // This is read2.
Secondary Flags = secondary // Not primary alignment.
QCFail Flags = qCFail // QC failure.
Duplicate Flags = duplicate // Optical or PCR duplicate.
Supplementary Flags = supplementary // Supplementary alignment, indicates alignment is part of a chimeric alignment.
)
const (
paired = C.BAM_FPAIRED
properPair = C.BAM_FPROPER_PAIR
unmapped = C.BAM_FUNMAP
mateUnmapped = C.BAM_FMUNMAP
reverse = C.BAM_FREVERSE
mateReverse = C.BAM_FMREVERSE
read1 = C.BAM_FREAD1
read2 = C.BAM_FREAD2
secondary = C.BAM_FSECONDARY
qCFail = C.BAM_FQCFAIL
duplicate = C.BAM_FDUP
supplementary = C.BAM_FSUPP
)
// A Flags represents a BAM record's alignment FLAG field.
type Flags uint32
// String representation of BAM alignment flags:
// 0x001 - p - Paired
// 0x002 - P - ProperPair
// 0x004 - u - Unmapped
// 0x008 - U - MateUnmapped
// 0x010 - r - Reverse
// 0x020 - R - MateReverse
// 0x040 - 1 - Read1
// 0x080 - 2 - Read2
// 0x100 - s - Secondary
// 0x200 - f - QCFail
// 0x400 - d - Duplicate
// 0x800 - S - Supplementary
//
// Note that flag bits are represented high order to the right.
func (f Flags) String() string {
// If 0x01 is unset, no assumptions can be made about 0x02, 0x08, 0x20, 0x40 and 0x80
const pairedMask = ProperPair | MateUnmapped | MateReverse | MateReverse | Read1 | Read2
if f&1 == 0 {
f &^= pairedMask
}
const flags = "pPuUrR12sfdS"
b := make([]byte, len(flags))
for i, c := range flags {
if f&(1<<uint(i)) != 0 {
b[i] = byte(c)
} else {
b[i] = '-'
}
}
return string(b)
}