-
Notifications
You must be signed in to change notification settings - Fork 0
/
fastq_reader.cpp
65 lines (52 loc) · 1.82 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
#include <algorithm>
#include <iostream>
#include "error.h"
#include "fastq_reader.h"
#include "stringops.h"
FASTQReader::FASTQReader(std::string filename, bool paired_end, bool reverse_complement){
this->filename = filename;
this->paired_end = paired_end;
this->rev_complement = reverse_complement;
input.open(filename.c_str());
std::getline(input, next_line);
}
FASTQReader::~FASTQReader(){ close(); }
bool FASTQReader::is_empty(){ return !input; }
ReadInfo FASTQReader::next_read(){
std::string identifier, sequence, quality;
identifier = next_line;
size_t space = identifier.find(" ");
if (space != std::string::npos)
identifier = identifier.substr(0, space);
if (!input)
printErrorAndDie("Attempt to read line in FASTQ_READER when stream is empty");
std::getline(input, sequence);
if (!input)
printErrorAndDie("Attempt to read line in FASTQ_READER when stream is empty");
std::getline(input, quality);
if (!input)
printErrorAndDie("Attempt to read line in FASTQ_READER when stream is empty");
std::getline(input, quality);
if (identifier.at(0) != '@')
printErrorAndDie("Read identifier is FASTQ file must begin with @ character");
if (paired_end){
if (identifier.length() > 2 && identifier[identifier.length()-2] == '/') {
if (identifier.back() == '1' || identifier.back() == '2'){
identifier.pop_back();
identifier.pop_back();
}
else
printErrorAndDie("Read identifiers for paired-end files must end in /1 or /2");
}
}
if (rev_complement){
// Reverse complement the sequence and reverse the quality scores
reverse_complement(sequence);
std::reverse(quality.begin(), quality.end());
}
std::getline(input, next_line);
return ReadInfo(identifier.substr(1), sequence, quality, rev_complement);
}
void FASTQReader::close(){
input.close();
}