-
Notifications
You must be signed in to change notification settings - Fork 23
/
stats.cpp
221 lines (184 loc) · 5.2 KB
/
stats.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
#include "stats.h"
#include "bxzstr.hpp"
void Stats::init()
{
mean_ = 0;
for (int i = 0; i < 9; i++)
{
n50_[i] = 0;
n50n_[i] = 0;
}
longest_ = 0;
shortest_ = 0;
number_ = 0;
totalLength_ = 0;
nCount_ = 0;
gapCount_ = 0;
filename_ = "";
}
Stats::Stats()
{
init();
}
Stats::Stats(string filename, unsigned long minimumLength)
{
init();
vector<int> seqLengths;
bxz::ifstream inStream;
unsigned long cumulativeLength = 0;
inStream.open(filename.c_str());
if (! inStream.is_open())
{
cerr << "Error opening file '" << filename << "'" << endl;
exit(1);
}
filename_ = filename;
short filetype = fastaOrFastq(inStream);
while(inStream.good())
{
Fasta* seq;
if (filetype == FASTQ_FILE)
{
Fastq* p;
p = new Fastq;
seq = (Fastq*) p;
}
else if (filetype == FASTA_FILE)
{
seq = new Fasta();
}
else
{
cerr << "Input file type not recognised as fasta or fastq. Aborting" << endl;
exit(1);
}
if ( !(seq->fillFromFile(inStream)) )
{
break;
}
else if (seq->length() >= minimumLength)
{
int l = seq->length();
seqLengths.push_back(l);
totalLength_ += l;
vector< pair<unsigned long, unsigned long> > gaps = seq->gaps();
gapCount_ += gaps.size();
for (vector< pair<unsigned long, unsigned long> >::iterator p = gaps.begin(); p != gaps.end(); ++p)
{
nCount_ += p->second - p->first + 1;
}
}
delete seq;
}
inStream.close();
if (seqLengths.size() == 0)
{
return;
}
sort(seqLengths.begin(), seqLengths.end());
longest_ = seqLengths.back();
shortest_ = seqLengths.front();
number_ = seqLengths.size();
mean_ = 1.0 * totalLength_ / number_;
int k = 0;
for (int i = seqLengths.size() - 1; 0 <= i; i--)
{
cumulativeLength += seqLengths[i];
while (k < 9 && cumulativeLength >= totalLength_ * (k + 1) / 10.0)
{
n50_[k] = seqLengths[i];
n50n_[k] = seqLengths.size() - i;
k++;
}
}
}
string Stats::toString(short format) const
{
switch (format)
{
case FORMAT_HUMAN:
return toString_human_readable();
case FORMAT_TAB:
return toString_tab_delimited(true);
case FORMAT_TAB_NO_HEAD:
return toString_tab_delimited(false);
case FORMAT_GREPPY:
return toString_greppy();
default:
cerr << "Wrong format supplied to Stats::toString(). Cannot continue" << endl;
exit(1);
}
}
string Stats::toString_greppy() const
{
stringstream ss;
ss.precision(2);
ss << filename_ << "\t" << "total_length\t" << totalLength_ << endl
<< filename_ << "\t" << "number\t" << number_ << endl
<< filename_ << "\t" << "mean_length\t" << fixed << mean_ << endl
<< filename_ << "\t" << "longest\t" << longest_ << endl
<< filename_ << "\t" << "shortest\t" << shortest_ << endl
<< filename_ << "\t" << "N_count\t" << nCount_ << endl
<< filename_ << "\t" << "Gaps\t" << gapCount_ << endl;
for (int j = 0; j < 9; j++)
{
ss << filename_ << "\t" << "n" << j + 1 << "0\t" << n50_[j] << endl
<< filename_ << "\t" << "n" << j + 1 << "0n\t" << n50n_[j] << endl;
}
return ss.str();
}
string Stats::toString_tab_delimited(bool header) const
{
stringstream ss;
ss.precision(2);
if (header)
{
ss << "filename"
<< '\t' << "total_length"
<< '\t' << "number"
<< '\t' << "mean_length"
<< '\t' << "longest"
<< '\t' << "shortest"
<< '\t' << "N_count"
<< '\t' << "Gaps"
<< '\t' << "N50"
<< '\t' << "N50n"
<< '\t' << "N70"
<< '\t' << "N70n"
<< '\t' << "N90"
<< '\t' << "N90n" << endl;
}
ss << filename_ << '\t'
<< totalLength_ << '\t'
<< number_ << '\t'
<< fixed << mean_ << '\t'
<< longest_ << '\t'
<< shortest_ << '\t'
<< nCount_ << '\t'
<< gapCount_;
for (int j = 4; j < 9; j+=2)
{
ss << '\t' << n50_[j] << '\t' << n50n_[j];
}
ss << endl;
return ss.str();
}
string Stats::toString_human_readable() const
{
stringstream ss;
ss.precision(2);
ss << "stats for " << filename_ << endl
<< "sum = " << totalLength_
<< ", n = " << number_
<< ", ave = " << fixed << mean_
<< ", largest = " << longest_ << endl
<< "N50 = " << n50_[4] << ", n = " << n50n_[4] << endl
<< "N60 = " << n50_[5] << ", n = " << n50n_[5] << endl
<< "N70 = " << n50_[6] << ", n = " << n50n_[6] << endl
<< "N80 = " << n50_[7] << ", n = " << n50n_[7] << endl
<< "N90 = " << n50_[8] << ", n = " << n50n_[8] << endl
<< "N100 = " << shortest_ << ", n = " << number_ << endl
<< "N_count = " << nCount_ << endl
<< "Gaps = " << gapCount_ << endl;
return ss.str();
}