Repository navigation
Expand file tree
/
Copy pathfasta.go
More file actions
151 lines (135 loc) · 3.79 KB
/
Copy pathfasta.go
File metadata and controls
151 lines (135 loc) · 3.79 KB
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
package main
import (
"bufio"
"bytes"
"errors"
"io"
"github.com/biogo/hts/bgzf"
)
type Record struct {
ID string
Description string
Len int64
BaseCounts [256]int64
}
var (
errBadlyFormedFasta = errors.New("badly formed fasta")
)
type Reader struct {
*bufio.Reader
header []byte // the header line being read, reused for each record
}
// readerSize is the size of the buffer lines are read from: lines are examined where they sit in it, without
// being copied, so a larger buffer means fewer reads from the file and fewer long lines read in pieces
const readerSize = 1 << 20
func NewReader(f io.Reader) *Reader {
return &Reader{Reader: bufio.NewReaderSize(f, readerSize)}
}
func NewZReader(f io.Reader) *Reader {
rz, _ := bgzf.NewReader(f, 0)
return &Reader{Reader: bufio.NewReaderSize(rz, readerSize)}
}
func (r *Reader) Read() (Record, error) {
return r.read(false)
}
func (r *Reader) ReadCalcBaseCounts() (Record, error) {
return r.read(true)
}
// read reads one fasta record's info from the underlying reader. The final record is returned with error = nil,
// and the next call to read() returns an empty Record struct and error = io.EOF.
// Base content is optionally counted.
//
// Lines are examined where they sit in the bufio.Reader's buffer (ReadSlice), not copied: the sequence is only
// counted, never kept. A line longer than the buffer arrives in pieces (bufio.ErrBufferFull); a line's
// trailing "\n" or "\r\n" is not counted, even if the "\r" ends one piece and the "\n" starts the next.
func (r *Reader) read(calcBaseCounts bool) (Record, error) {
var (
record Record
bases [256]int64
seqLength int64
)
// the header, which must be there and must end in a newline: the file should never end on a header line
header := r.header[:0]
for {
piece, err := r.ReadSlice('\n')
header = append(header, piece...)
if err == bufio.ErrBufferFull {
continue
}
if err != nil {
r.header = header
if err == io.EOF && len(header) == 0 {
return Record{}, io.EOF
}
return Record{}, err
}
break
}
r.header = header
if header[0] != '>' {
return Record{}, errBadlyFormedFasta
}
header = header[:len(header)-1]
if len(header) > 0 && header[len(header)-1] == '\r' {
header = header[:len(header)-1]
}
// split the header on whitespace
fields := bytes.Fields(header[1:])
// fasta ID
record.ID = string(fields[0])
// fasta description
record.Description = string(header[1:])
// the sequence lines, up to the next header or the end of the file
count := func(b []byte) {
if calcBaseCounts {
for _, c := range b {
bases[c]++
}
}
seqLength += int64(len(b))
}
for {
// peek at the next byte to see if we've reached the end of this record (or the file)
peek, err := r.Peek(1)
if err == io.EOF || (err == nil && peek[0] == '>') {
break
} else if err != nil {
return Record{}, err
}
// one line, perhaps in pieces; a "\r" that ends a piece is held back until the next piece shows
// whether it is the line's "\r\n"
heldCR := false
for {
piece, err := r.ReadSlice('\n')
if heldCR {
if len(piece) > 0 && piece[0] == '\n' {
break // the line ended "\r\n" across two pieces
}
count([]byte{'\r'})
heldCR = false
}
if err == bufio.ErrBufferFull {
if piece[len(piece)-1] == '\r' {
piece, heldCR = piece[:len(piece)-1], true
}
count(piece)
continue
}
if err != nil && err != io.EOF {
return Record{}, err
}
// a final line without a newline gives io.EOF, which the next Peek sees
if len(piece) > 0 && piece[len(piece)-1] == '\n' {
piece = piece[:len(piece)-1]
if len(piece) > 0 && piece[len(piece)-1] == '\r' {
piece = piece[:len(piece)-1]
}
}
count(piece)
break
}
}
record.Len = seqLength
record.BaseCounts = bases
return record, nil
}