forked from aebruno/twobit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
twobit.go
697 lines (581 loc) · 13.9 KB
/
twobit.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
// Copyright 2015 Andrew E. Bruno. All rights reserved.
// Use of this source code is governed by a BSD style
// license that can be found in the LICENSE file.
// Package twobit implements the 2bit compact randomly-accessible file format
// for storing DNA sequence data.
package twobit
import (
"bufio"
"bytes"
"encoding/binary"
"fmt"
"io"
)
// 2bit header
type header struct {
sig uint32
version uint32
count uint32
reserved uint32
byteOrder binary.ByteOrder
}
// Block represents either blocks of Ns or masked (lower-case) blocks
type Block struct {
start int
count int
}
// seqRecord stores sequence record from the file index
type seqRecord struct {
dnaSize uint32
nBlocks []*Block
mBlocks []*Block
reserved uint32
sequence []byte
}
// TwoBit stores the file index and header information of the 2bit file
type twoBit struct {
reader io.ReadSeeker
hdr header
index map[string]int
records map[string]*seqRecord
}
type Reader twoBit
type Writer twoBit
func init() {
NT2BYTES = make([]byte, 256)
NT2BYTES[BASE_N] = uint8(0)
NT2BYTES[BASE_T] = uint8(0)
NT2BYTES[BASE_C] = uint8(1)
NT2BYTES[BASE_A] = uint8(2)
NT2BYTES[BASE_G] = uint8(3)
NT2BYTES[BASE_N+32] = uint8(0)
NT2BYTES[BASE_T+32] = uint8(0)
NT2BYTES[BASE_C+32] = uint8(1)
NT2BYTES[BASE_A+32] = uint8(2)
NT2BYTES[BASE_G+32] = uint8(3)
BYTE2NT = initByte2Nt()
}
// Return the size in packed bytes of a dna sequence. 4 bases per byte
func packedSize(dnaSize int) int {
return (dnaSize + 3) >> 2
}
// Return length of block
func (b *Block) Length() int {
return b.start + b.count
}
// Return start of block
func (b *Block) Start() int {
return b.start
}
// Return count of block
func (b *Block) Count() int {
return b.count
}
// Return the size in bytes the seqRecord rec will take up in the twobit file
func (rec *seqRecord) size() int {
size := 16 // dnaSize (4), nBlockCount (4), mBlockCount (4), reserved (4)
size += 2 * 4 * len(rec.nBlocks) // nBlockStarts, nBlockSizes
size += 2 * 4 * len(rec.mBlocks) // mBlockStarts, mBlockSizes
size += len(rec.sequence) // packedDNA
return size
}
// Parse the file index of a 2bit file
func (r *Reader) parseIndex() error {
r.index = make(map[string]int)
for i := 0; i < r.Count(); i++ {
size := make([]byte, 1)
_, err := r.reader.Read(size)
if err != nil {
return fmt.Errorf("Failed to read file index: %s", err)
}
name := make([]byte, size[0])
_, err = r.reader.Read(name)
if err != nil {
return fmt.Errorf("Failed to read file index: %s", err)
}
offset := make([]byte, 4)
_, err = r.reader.Read(offset)
if err != nil {
return fmt.Errorf("Failed to read file index: %s", err)
}
r.index[string(name)] = int(r.hdr.byteOrder.Uint32(offset))
}
return nil
}
/*
// Seek to given offset in file
func (r *Reader) Seek(offset int64) (int64, error) {
n, err := r.readseeker.Seek(offset, 0)
r.reader.Reset(r.readseeker)
return n, err
}
*/
// Parse the header of a 2bit file
func (r *Reader) parseHeader() error {
b := make([]byte, 16)
_, err := r.reader.Read(b)
if err != nil {
return err
}
r.hdr.sig = binary.BigEndian.Uint32(b[0:4])
r.hdr.byteOrder = binary.BigEndian
if r.hdr.sig != SIG {
r.hdr.sig = binary.LittleEndian.Uint32(b[0:4])
r.hdr.byteOrder = binary.LittleEndian
if r.hdr.sig != SIG {
return fmt.Errorf("Invalid sig. Not a 2bit file?")
}
}
r.hdr.version = r.hdr.byteOrder.Uint32(b[4:8])
if r.hdr.version != uint32(0) {
return fmt.Errorf("Unsupported version %d", r.hdr.version)
}
r.hdr.count = r.hdr.byteOrder.Uint32(b[8:12])
r.hdr.reserved = r.hdr.byteOrder.Uint32(b[12:16])
if r.hdr.reserved != uint32(0) {
return fmt.Errorf("Reserved != 0. got %d", r.hdr.reserved)
}
return nil
}
// Parse the nBlock and mBlock coordinates
func (r *Reader) parseBlockCoords() ([]*Block, error) {
buf := make([]byte, 4)
_, err := r.reader.Read(buf)
if err != nil {
return nil, fmt.Errorf("Failed to read blockCount: %s", err)
}
count := r.hdr.byteOrder.Uint32(buf)
starts := make([]uint32, count)
for i := range starts {
_, err := r.reader.Read(buf)
if err != nil {
return nil, fmt.Errorf("Failed to block start: %s", err)
}
starts[i] = r.hdr.byteOrder.Uint32(buf)
}
sizes := make([]uint32, count)
for i := range sizes {
_, err := r.reader.Read(buf)
if err != nil {
return nil, fmt.Errorf("Failed to block size: %s", err)
}
sizes[i] = r.hdr.byteOrder.Uint32(buf)
}
blocks := make([]*Block, len(starts))
for i := range starts {
blocks[i] = &Block{start: int(starts[i]), count: int(sizes[i])}
}
return blocks, nil
}
// Parse the sequence record information
func (r *Reader) parseRecord(name string, coords bool) (*seqRecord, error) {
rec := new(seqRecord)
offset, ok := r.index[name]
if !ok {
return nil, fmt.Errorf("Invalid sequence name: %s", name)
}
r.reader.Seek(int64(offset), 0)
buf := make([]byte, 4)
_, err := r.reader.Read(buf)
if err != nil {
return nil, fmt.Errorf("Failed to read dnaSize: %s", err)
}
rec.dnaSize = r.hdr.byteOrder.Uint32(buf)
if coords {
rec.nBlocks, err = r.parseBlockCoords()
if err != nil {
return nil, fmt.Errorf("Failed to read nBlocks: %s", err)
}
rec.mBlocks, err = r.parseBlockCoords()
if err != nil {
return nil, fmt.Errorf("Failed to read mBlocks: %s", err)
}
_, err = r.reader.Read(buf)
if err != nil {
return nil, fmt.Errorf("Failed to read reserved: %s", err)
}
rec.reserved = r.hdr.byteOrder.Uint32(buf)
if rec.reserved != uint32(0) {
return nil, fmt.Errorf("Invalid reserved")
}
}
return rec, nil
}
// Return blocks of Ns in sequence with name
func (r *Reader) NBlocks(name string) ([]*Block, error) {
rec, err := r.parseRecord(name, true)
if err != nil {
return nil, err
}
return rec.nBlocks, nil
}
// Read entire sequence.
func (r *Reader) Read(name string) ([]byte, error) {
return r.ReadRange(name, 0, 0)
}
// Read sequence from start to end.
func (r *Reader) ReadRange(name string, start, end int) ([]byte, error) {
rec, err := r.parseRecord(name, true)
if err != nil {
return nil, err
}
bases := int(rec.dnaSize)
// TODO: handle -1 ?
if start < 0 {
start = 0
}
//TODO: should we error out here?
if end > bases {
end = bases
}
// TODO: handle -1 ?
if end == 0 || end < 0 {
end = bases
}
if end <= start {
return nil, fmt.Errorf("Invalid range: %d-%d", start, end)
}
bases = end - start
size := packedSize(bases)
if start > 0 {
shift := packedSize(start)
if start%4 != 0 {
shift--
size++
}
r.reader.Seek(int64(shift), 1)
}
dna := make([]byte, size*4)
chunks := size / defaultBufSize
if size%defaultBufSize > 0 {
chunks++
}
buf := make([]byte, defaultBufSize)
i := 0
for c := 0; c < chunks; c++ {
sz := defaultBufSize
if i+defaultBufSize > size {
sz = size % defaultBufSize
}
n, err := r.reader.Read(buf[0:sz])
if n != sz {
return nil, fmt.Errorf("Failed to read %d dna bytes: %s", sz, err)
} else if err != nil && err != io.EOF {
return nil, fmt.Errorf("Failed to read dna bytes: %s", err)
}
for k := 0; k < n; k++ {
// TODO: test whether precomputing byte to nt improves speed
// practically, this is certainly disc-bound
packedByte := buf[k]
bases := BYTE2NT[int(packedByte)]
for j := 0; j < 4; j++ {
dna[(i*4)+j] = bases[j]
}
i++
/*
for j := 3; j >= 0; j-- {
dna[(i*4)+j] = BIT2NT[int(base&0x3)]
base >>= 2
}
*/
}
}
seq := dna[(start % 4) : (start%4)+bases]
for _, b := range rec.nBlocks {
if b.Length() < start || b.start > end {
continue
}
idx := b.start - start
cnt := b.count
if idx < 0 {
cnt += idx
idx = 0
}
for i := 0; i < cnt; i++ {
seq[idx] = BASE_N
idx++
if idx >= len(seq) {
break
}
}
}
for _, b := range rec.mBlocks {
if b.Length() < start || b.start > end {
continue
}
idx := b.start - start
cnt := b.count
if idx < 0 {
cnt += idx
idx = 0
}
for i := 0; i < cnt; i++ {
// Faster lower case.. see: https://groups.google.com/forum/#!topic/golang-nuts/Il2DX4xpW3w
seq[idx] = seq[idx] + 32 // ('a' - 'A')
idx++
if idx >= len(seq) {
break
}
}
}
return seq, nil
}
// NewReader returns a new TwoBit file reader which reads from r
func NewReader(r io.ReadSeeker) (*Reader, error) {
tb := new(Reader)
tb.reader = r
err := tb.parseHeader()
if err != nil {
return nil, err
}
err = tb.parseIndex()
if err != nil {
return nil, err
}
return tb, nil
}
// Returns the length for sequence with name
func (r *Reader) Length(name string) (int, error) {
rec, err := r.parseRecord(name, false)
if err != nil {
return -1, err
}
return int(rec.dnaSize), nil
}
// Returns the length for sequence with name but does not count Ns
func (r *Reader) LengthNoN(name string) (int, error) {
rec, err := r.parseRecord(name, true)
if err != nil {
return -1, err
}
n := 0
for _, b := range rec.nBlocks {
n += b.count
}
return int(rec.dnaSize) - n, nil
}
// Returns the names of sequences in the 2bit file
func (r *Reader) Names() []string {
names := make([]string, len(r.index))
i := 0
for n := range r.index {
names[i] = n
i++
}
return names
}
// Returns the count of sequences in the 2bit file
func (r *Reader) Count() int {
return int(r.hdr.count)
}
// Returns the version of the 2bit file
func (r *Reader) Version() int {
return int(r.hdr.version)
}
// Unpack array of bytes to DNA string of length sz using array lookup
func UnpackWithArray(raw []byte, sz int) string {
var dna bytes.Buffer
for _, base := range raw {
buf := BYTE2NT[int(base)]
dna.Write(buf)
}
return string(dna.Bytes()[0:sz])
}
// Unpack array of bytes to DNA string of length sz
func Unpack(raw []byte, sz int) string {
var dna bytes.Buffer
for _, base := range raw {
buf := make([]byte, 4)
for j := 3; j >= 0; j-- {
buf[j] = BIT2NT[int(base&0x3)]
base >>= 2
}
dna.Write(buf)
}
return string(dna.Bytes()[0:sz])
}
func initByte2Nt() [][]byte {
BYTE2NT := make([][]byte, 256)
for i := 0; i < 256; i++ {
Byte := byte(i)
seq := make([]byte, 4)
for j := 3; j >= 0; j-- {
seq[j] = BIT2NT[int(Byte&0x03)]
Byte >>= 2
}
BYTE2NT[i] = seq
}
return BYTE2NT
}
// Packs DNA sequence string into an array of bytes. 4 bases per byte.
func Pack(s string) ([]byte, error) {
sz := len(s)
out := make([]byte, packedSize(sz))
idx := 0
for i := range out {
var b uint8
for j := 0; j < 4; j++ {
val := NT2BYTES['T']
if idx < sz {
// TODO: add check for unsupported base?
val = NT2BYTES[s[idx]]
}
b <<= 2
b += val
idx++
}
out[i] = b
}
return out, nil
}
// New Writer
func NewWriter() *Writer {
tb := new(Writer)
tb.records = make(map[string]*seqRecord)
return tb
}
func mapMBlocks(seq string) []*Block {
blocks := make([]*Block, 0)
n := len(seq)
start := 0
match := false
isLast := false
for i := 0; i < n; i++ {
match = seq[i] >= 'a' && seq[i] <= 'z'
if match {
if !isLast {
start = i
}
} else {
if isLast {
blocks = append(blocks, &Block{start: start, count: i - start})
}
}
isLast = match
}
if isLast {
blocks = append(blocks, &Block{start: start, count: n - start})
}
return blocks
}
func mapNBlocks(seq string) []*Block {
blocks := make([]*Block, 0)
n := len(seq)
start := 0
match := false
isLast := false
for i := 0; i < n; i++ {
match = seq[i] == 'N' || seq[i] == 'n'
if match {
if !isLast {
start = i
}
} else {
if isLast {
blocks = append(blocks, &Block{start: start, count: i - start})
}
}
isLast = match
}
if isLast {
blocks = append(blocks, &Block{start: start, count: n - start})
}
return blocks
}
// Add sequence
func (w *Writer) Add(name, seq string) error {
if len(name) > 255 {
return fmt.Errorf("Name string cannot be longer than 255 characters")
}
rec := new(seqRecord)
rec.dnaSize = uint32(len(seq))
rec.nBlocks = mapNBlocks(seq)
rec.mBlocks = mapMBlocks(seq)
pack, err := Pack(seq)
if err != nil {
return err
}
rec.sequence = pack
w.records[name] = rec
return nil
}
// Write sequences in 2bit format to out
func (w *Writer) WriteTo(out io.Writer) error {
outbuf := bufio.NewWriter(out)
buf := make([]byte, 16)
binary.LittleEndian.PutUint32(buf[0:4], SIG)
binary.LittleEndian.PutUint32(buf[4:8], uint32(0))
binary.LittleEndian.PutUint32(buf[8:12], uint32(len(w.records)))
binary.LittleEndian.PutUint32(buf[12:16], uint32(0))
_, err := outbuf.Write(buf)
if err != nil {
return err
}
idxSize := 0
recSize := 0
var names []string
for name, rec := range w.records {
names = append(names, name)
idxSize += 5 + len(name)
recSize += rec.size()
}
buf = make([]byte, idxSize)
offset := 16 + idxSize
idx := 0
// Write out index
for _, name := range names {
buf[idx] = uint8(len(name))
idx++
for j := 0; j < len(name); j++ {
buf[idx] = name[j]
idx++
}
binary.LittleEndian.PutUint32(buf[idx:idx+4], uint32(offset))
offset += w.records[name].size()
idx += 4
}
_, err = outbuf.Write(buf)
if err != nil {
return err
}
// Write out records
for _, name := range names {
rec := w.records[name]
sz := rec.size()
buf = make([]byte, sz)
binary.LittleEndian.PutUint32(buf[0:4], rec.dnaSize)
binary.LittleEndian.PutUint32(buf[4:8], uint32(len(rec.nBlocks)))
idx := 8
for _, b := range rec.nBlocks {
binary.LittleEndian.PutUint32(buf[idx:idx+4], uint32(b.start))
idx += 4
}
for _, b := range rec.nBlocks {
binary.LittleEndian.PutUint32(buf[idx:idx+4], uint32(b.count))
idx += 4
}
binary.LittleEndian.PutUint32(buf[idx:idx+4], uint32(len(rec.mBlocks)))
idx += 4
for _, b := range rec.mBlocks {
binary.LittleEndian.PutUint32(buf[idx:idx+4], uint32(b.start))
idx += 4
}
for _, b := range rec.mBlocks {
binary.LittleEndian.PutUint32(buf[idx:idx+4], uint32(b.count))
idx += 4
}
// reserved
binary.LittleEndian.PutUint32(buf[idx:idx+4], uint32(0))
idx += 4
copy(buf[idx:sz], rec.sequence[:])
_, err := outbuf.Write(buf)
if err != nil {
return err
}
}
err = outbuf.Flush()
if err != nil {
return err
}
return nil
}