- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMatrix.cpp
More file actions
Latest commit
57 lines (54 loc) · 1.55 KB
/
Copy pathMatrix.cpp
File metadata and controls
57 lines (54 loc) · 1.55 KB
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
#include"Matrix.h"
Matrix::Matrix(int depth, vector<string> taxs, bool reportRead):mat(depth),
colIDs(taxs), rowIDs(depth), readReport(reportRead)
{
if (readReport){//means that not only species level is reported, but also singular hit (shini table)
mat.resize(depth + 1);
rowIDs.resize(depth + 1); colIDs.resize(depth + 1, "Hit2DB");
}
}
Matrix::~Matrix()
{
}
voidMatrix::add(TaxObj* t) {
string addStr("");// t->get(0));
for (size_tDL = 0; DL < rowIDs.size(); DL++) {
string curT = readReport && DL + 1 == rowIDs.size()
? t->getHitDB() : t->get(static_cast<int>(DL));
if (curT == __unkwnTax) {curT = __unkwnTaxWR;}
if (DL == 0) {
addStr += curT;
} else {
addStr += __taxSepMat + curT;
}
auto fnd = rowIDs[DL].find(addStr);
if (fnd == rowIDs[DL].end()) {
rowIDs[DL][addStr] = 1;
} else {
rowIDs[DL][addStr]++;
}
}
}
boolMatrix::writeAllLevels(const string& outF) {
for (size_tDL = 0; DL < rowIDs.size(); DL++) {
string outF1 = outF + "_" + colIDs[DL];
ofstream of(outF1.c_str());
if (!of) {
cerr << "Could not create matrix output file " << outF1 << endl;
returnfalse;
}
vector<pair<string, size_t> > rows(rowIDs[DL].begin(), rowIDs[DL].end());
std::sort(rows.begin(), rows.end(), [](const pair<string, size_t>& a, const pair<string, size_t>& b) {
return a.first < b.first;
});
for (constauto& row : rows) {
of << row.first << __MatSep << row.second << '\n';
}
of.flush();
if (!of) {
cerr << "Failed while writing matrix output file " << outF1 << endl;
returnfalse;
}
}
returntrue;
}