-
Notifications
You must be signed in to change notification settings - Fork 2
/
fastq_reader.cpp
159 lines (135 loc) · 3.84 KB
/
fastq_reader.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
#include <cstdlib>
#include <vector>
#include <string>
#include <iostream>
#include <cstring>
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
#include "fastq_reader.h"
#include "kmer.h"
using namespace std;
namespace fs = boost::filesystem;
FastQReader::FastQReader(vector<string>& fnames, k_t k)
: k(k), paths(), curr_path(), curr_file(), so_far(), max_byte(~1), read_col(0)
{
for (vector<string>::iterator it = fnames.begin(); it < fnames.end(); it++) {
paths.push_back(fs::path(*it));
}
}
uintmax_t FastQReader::total_bytes(void)
{
uintmax_t total = 0;
for (vector<fs::path>::iterator it = paths.begin(); it < paths.end(); it++) {
total += fs::file_size(*it);
}
return total;
}
void FastQReader::set_max_byte(uintmax_t max_byte)
{
this->max_byte = max_byte;
}
void FastQReader::seek(uintmax_t offset)
{
for (curr_path = paths.begin(); curr_path < paths.end(); curr_path++) {
if ((so_far + fs::file_size(*curr_path)) >= offset) {
curr_file.close();
curr_file.open(*curr_path);
break;
}
so_far += fs::file_size(*curr_path);
}
/**
* FIXME - We assume '@' will only appear at the beginning of a read. Check
* to see that quality scores don't clash.
*/
curr_file.seekg(offset - so_far, ios::beg);
seek_to_next_read();
}
void FastQReader::seek_to_next_read()
{
curr_file.ignore(MAX_LINE_LEN, '@');
if (curr_file.eof()) {
next_file();
} else {
curr_file.putback('@');
}
}
uintmax_t FastQReader::tell(void)
{
return so_far + curr_file.tellg();
}
bool FastQReader::next_file()
{
curr_file.seekg(0, ios::end);
so_far += curr_file.tellg();
curr_path++;
curr_file.close();
if (curr_path == paths.end())
{
return false;
}
curr_file.open(*curr_path);
return true;
}
bool FastQReader::read_next(qekmer_t* qekmer)
{
while (true) {
bool skip_kmer = false;
if (curr_path == paths.end()) {
return false;
} else if (!read_col && tell() >= max_byte) {
return false;
}
if (read_col == 0) {
//char read_buf[MAX_LINE_LEN];
char quals_buf[MAX_LINE_LEN];
/* Ignore the header line. */
curr_file.ignore(MAX_LINE_LEN, '\n');
/* Get the actual read. */
curr_file.getline(read, MAX_LINE_LEN);
/* Ignore +. */
curr_file.ignore(MAX_LINE_LEN, '\n');
/* Getthe quality scores. */
curr_file.getline(quals_buf, MAX_LINE_LEN);
read_len = strlen(read);
for (size_t i = 0; i < read_len; i++) {
quals[i] = quals_buf[i] - ILLUMINA_QUAL_OFFSET;
}
}
for (size_t i = 0; i < k; i++) {
char char_base = read[read_col + i];
if ( char_base == 'N') {
read_col = read_col + i + 1;
if (read_col > read_len - k) {
seek_to_next_read();
read_col = 0;
}
skip_kmer = true;
break;
}
base b = char2base(char_base);
set_base(qekmer->kmer, i, b);
}
if (skip_kmer) {
continue;
}
if (read_col == 0) {
qekmer->lqual = 0;
} else {
qekmer->exts.left = char2base(read[read_col - 1]);
qekmer->lqual = quals[read_col - 1];
}
if (read_col + k == read_len) {
qekmer->rqual = 0;
} else {
qekmer->exts.right = char2base(read[read_col + k]);
qekmer->rqual = quals[read_col + k];
}
read_col++;
if (read_col > read_len - k) {
seek_to_next_read();
read_col = 0;
}
return true;
}
}