-
Notifications
You must be signed in to change notification settings - Fork 2
/
parse_sequence.cpp
262 lines (197 loc) · 6.02 KB
/
parse_sequence.cpp
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
#include <fstream>
#include <iostream>
#include <deque>
#include <string.h>
#include "parse_sequence.h"
#include "file_util.h"
using namespace std;
FileType get_file_type(const string &m_filename);
FileType get_file_type(const string &m_filename)
{
if( find_file_extension(m_filename, ".fna") || find_file_extension(m_filename, ".fna.gz") ||
find_file_extension(m_filename, ".fa") || find_file_extension(m_filename, ".fa.gz") ||
find_file_extension(m_filename, ".fasta") || find_file_extension(m_filename, ".fasta.gz")){
return FASTA;
}
if( find_file_extension(m_filename, ".fastq") || find_file_extension(m_filename, ".fastq.gz") ){
return FASTQ;
}
return UNKNOWN_SEQUENCE;
}
void SequenceIterator::load(const string &m_filename)
{
// Clean up any open file pointers or memory buffers
clear();
file_type = get_file_type(m_filename);
switch(file_type){
case FASTA:
case FASTQ:
// Use zlib to read both compressed and uncompressed fasta files.
fin = gzopen(m_filename.c_str(), "r");
if(fin == NULL){
cerr << "Error opening: " << m_filename << endl;
throw __FILE__ ":SequenceIterator::SequenceIterator: Unable to open sequence file";
}
break;
default:
throw __FILE__ ":SequenceIterator::SequenceIterator: Unknown file type";
}
next();
}
void SequenceIterator::next()
{
switch(file_type){
case FASTA:
next_fasta();
break;
case FASTQ:
next_fastq();
break;
default:
throw __FILE__ ":SequenceIterator::next: Unknown sequence file type";
break;
};
}
void SequenceIterator::next_fasta()
{
if(fin == NULL){
return;
}
const int buffer_len = 2048;
char buffer[buffer_len];
deque<char> seq_buffer;
deque<char> info_buffer;
while( gzgets(fin, buffer, buffer_len) ){
if(strchr(buffer, '>') != NULL){
info_buffer.clear();
for(char* p = buffer;*p != '\0';++p){
if( (*p != '\n') && (*p != '\r') ){
info_buffer.push_back(*p);
}
}
// Is this defline longer than the buffer?
if(strpbrk(buffer, "\n\r") == NULL ){
// Keep reading until we find an end of line symbol
while( gzgets(fin, buffer, buffer_len) && !strpbrk(buffer, "\n\r") ){
for(char* p = buffer;*p != '\0';++p){
if( (*p != '\n') && (*p != '\r') ){
info_buffer.push_back(*p);
}
}
}
}
// Remove the fasta defline delimeter
while(!info_buffer.empty() && ( isspace(info_buffer[0]) || (info_buffer[0] == '>') ) ){
info_buffer.pop_front();
}
if( !seq_buffer.empty() ){
swap(curr_defline, next_defline);
next_defline.assign( info_buffer.begin(), info_buffer.end() );
seq.assign( seq_buffer.begin(), seq_buffer.end() );
return;
}
else{
next_defline.assign( info_buffer.begin(), info_buffer.end() );
}
}
else{
for(char* p = buffer;*p != '\0';++p){
if( !isspace(*p) ){
// Make sure all bases are upper-case
seq_buffer.push_back( toupper(*p) );
}
}
}
}
if( !seq_buffer.empty() ){
swap(curr_defline, next_defline);
seq.assign( seq_buffer.begin(), seq_buffer.end() );
return;
}
gzclose(fin);
fin = NULL;
}
void SequenceIterator::next_fastq()
{
if(fin == NULL){
return;
}
const int buffer_len = 2048;
char buffer[buffer_len];
deque<char> seq_buffer;
deque<char> info_buffer;
// The FASTQ file format is as follows for each record
// (from the Wikipedia entry):
// Line 1 begins with a '@' character and is followed by a sequence identifier and an optional description (like a FASTA title line).
// Line 2 is the raw sequence letters.
// Line 3 begins with a '+' character and is optionally followed by the same sequence identifier (and any description) again.
// Line 4 encodes the quality values for the sequence in Line 2, and must contain the same number of symbols as letters in the sequence.
/////////////////////////////////////////////////////////////////////////////////
// Read the header
/////////////////////////////////////////////////////////////////////////////////
while(true){
if(gzgets(fin, buffer, buffer_len) == NULL){
gzclose(fin);
fin = NULL;
return;
}
for(char* p = buffer;*p != '\0';++p){
if( (*p != '\n') && (*p != '\r') ){
info_buffer.push_back(*p);
}
}
// If we found an end of line symbol, we can stop reading
if(strpbrk(buffer, "\n\r") != NULL){
break;
}
}
// Remove the fastq defline delimeter
while(!info_buffer.empty() && ( isspace(info_buffer[0]) || (info_buffer[0] == '@') ) ){
info_buffer.pop_front();
}
curr_defline.assign( info_buffer.begin(), info_buffer.end() );
/////////////////////////////////////////////////////////////////////////////////
// Read the sequence
/////////////////////////////////////////////////////////////////////////////////
while(true){
if(gzgets(fin, buffer, buffer_len) == NULL){
throw __FILE__ ":next_fastq: Unable to read sequence";
}
for(char* p = buffer;*p != '\0';++p){
if( !isspace(*p) ){
// Make sure all bases are upper-case
seq_buffer.push_back( toupper(*p) );
}
}
// If we found an end of line symbol, we can stop reading
if(strpbrk(buffer, "\n\r") != NULL){
break;
}
}
// Skip the '+'. Note that we *don't* actually test to make sure we read a single '+' on a line
// by itself.
if(gzgets(fin, buffer, buffer_len) == NULL){
throw __FILE__ ":next_fastq: Unable to read '+'";
}
if( strpbrk(buffer, "\n\r") == NULL ){
throw __FILE__ ":next_fastq: Error reading '+' delimiter";
}
/////////////////////////////////////////////////////////////////////////////////
// Read and discard the quality
/////////////////////////////////////////////////////////////////////////////////
while(true){
if(gzgets(fin, buffer, buffer_len) == NULL){
throw __FILE__ ":next_fastq: Unable to read quality";
}
// If we found an end of line symbol, we can stop reading
if(strpbrk(buffer, "\n\r") != NULL){
break;
}
}
if( !seq_buffer.empty() ){
seq.assign( seq_buffer.begin(), seq_buffer.end() );
return;
}
gzclose(fin);
fin = NULL;
}