-
Notifications
You must be signed in to change notification settings - Fork 23
/
fasta.cpp
132 lines (101 loc) · 2.24 KB
/
fasta.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
#include "fasta.h"
Fasta::Fasta() : id_(""), seq_("") {}
Fasta::Fasta(string id, string seq) : id_(id), seq_(seq) {}
void Fasta::toString(ostream& outStream, unsigned int lineWidth) const
{
outStream << ">" << id_ << '\n';
if (lineWidth)
{
for (unsigned long i = 0; i < length(); i += lineWidth)
{
outStream << seq_.substr(i, lineWidth) << '\n';
}
}
else
{
outStream << seq_ << '\n';
}
}
string Fasta::name() const
{
return id_;
}
string Fasta::qual() const
{
return "";
}
void Fasta::seq(string s)
{
seq_ = s;
}
void Fasta::setQual(string s)
{
}
string Fasta::seq() const
{
return seq_;
}
void Fasta::name(string s)
{
id_ = s;
}
unsigned long Fasta::length() const
{
return seq_.length();
}
unsigned long Fasta::nCount() const
{
return count(seq_.begin(), seq_.end(), 'n') + count(seq_.begin(), seq_.end(), 'N');
}
vector< pair<unsigned long, unsigned long> > Fasta::gaps() const
{
vector< pair<unsigned long, unsigned long> > gaps;
string seq = seq_;
transform(seq.begin(), seq.end(), seq.begin(), ::toupper);
unsigned long pos = seq.find('N');
while (pos != string::npos)
{
unsigned long start = pos;
pos = seq.find_first_not_of('N', pos);
if (pos == string::npos)
{
gaps.push_back(make_pair(start, seq_.length() - 1));
}
else
{
gaps.push_back(make_pair(start, pos - 1));
pos = seq.find('N', pos);
}
}
return gaps;
}
bool Fasta::fillFromFile(bxz::ifstream& inStream)
{
string line;
seq_ = "";
id_ = "";
getline(inStream, line);
if (inStream.eof())
{
return false;
}
// Expecting a header line. If not, abort
else if (line[0] == '>')
{
id_ = line.substr(1);
}
else
{
cerr << "Error reading fasta file!" << endl
<< "Expected line starting with '>', but got this:" << endl
<< line << endl;
exit(1);
}
// Next lines should be sequence, up to next header, or end of file
while ((inStream.peek() != '>') && (!inStream.eof()))
{
getline(inStream, line);
seq_ += line;
}
return true;
}