This repository has been archived by the owner on Mar 18, 2023. It is now read-only.
forked from aebruno/twobit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
twobit.go
461 lines (380 loc) · 9.52 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
// 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 (
"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
}
// Reader reads twobits
type Reader twoBit
// Writer writes twobits
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)
}
// Return the size in packed bytes of a dna sequence. 4 bases per byte
func packedSize(dnaSize int) int {
return (dnaSize + 3) >> 2
}
// Length - Return length of block
func (b *Block) Length() int {
return b.start + b.count
}
// Start - Return start of block
func (b *Block) Start() int {
return b.start
}
// Count - 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
}
// NBlocks - 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)
}
// ReadRange - 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++ {
base := buf[k]
for j := 3; j >= 0; j-- {
dna[(i*4)+j] = BYTES2NT[int(base&0x3)]
base >>= 2
}
i++
}
}
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++ {
// moved this up because a few situations caused a panic due to index out of range from get-go.
if idx >= len(seq) {
break
}
seq[idx] = BASE_N
idx++
}
}
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
}
// Length - 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
}
// LengthNoN - 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
}
// Names - 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
}
// Count - Returns the count of sequences in the 2bit file
func (r *Reader) Count() int {
return int(r.hdr.count)
}
// Version - 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
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] = BYTES2NT[int(base&0x3)]
base >>= 2
}
dna.Write(buf)
}
return string(dna.Bytes()[0:sz])
}