-
Notifications
You must be signed in to change notification settings - Fork 23
/
fastq.cpp
100 lines (80 loc) · 2 KB
/
fastq.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
#include "fastq.h"
Fastq::Fastq() : Fasta(), qual_("") {}
Fastq::Fastq(string id, string seq, string qual)
{
if (seq.length() != qual.length())
{
cerr << "Error constructing Fastq object: mismatch in sequence and qual length." << endl
<< "id: " << id << endl
<< "Seq: " << seq << endl
<< "Qual: " << qual << endl
<< "Aborting" << endl;
exit(1);
}
id_ = id;
seq_ = seq;
qual_ = qual;
}
string Fastq::qual() const
{
return qual_;
}
void Fastq::qual(string s)
{
qual_ = s;
}
bool Fastq::fillFromFile(bxz::ifstream& inStream)
{
string line;
getline(inStream, line);
// check if we're at the end of the file
if (inStream.eof())
{
id_ = "";
seq_ = "";
qual_ = "";
return false;
}
// Expecting a header line. If not, abort
else if (line[0] == '@')
{
id_ = line.substr(1);
}
else
{
cerr << "Error reading fastq file!" << endl
<< "Expected line starting with '@', but got this:" << endl
<< line << endl;
exit(1);
}
// Next line is sequence string
getline(inStream, line);
seq_ = line;
// Next line is +... line
getline(inStream, line);
if (line[0] != '+')
{
cerr << "Error reading fastq file!" << endl
<< "Expected line starting with '+', but got this:" << endl
<< line << endl;
exit(1);
}
// Next line is quality string
getline(inStream, line);
if (line.length() != seq_.length())
{
cerr << "Mismatch in sequence and quality length." << endl
<< "id: " << id_ << endl
<< "seq: " << seq_ << endl
<< "qual: " << line << endl
<< "Aborting" << endl;
exit(1);
}
qual_ = line;
return true;
}
void Fastq::toString(ostream& outStream) const
{
outStream << "@" << id_ << endl << seq_ << '\n'
<< "+" << endl << qual_ << '\n';
}