-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReadMapping.cpp
330 lines (244 loc) · 6.75 KB
/
ReadMapping.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#include "Tree.h"
#include "CodonStateSpace.h"
#include "MappingParser.h"
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class BranchStat {
CodonStateSpace* cstatespace;
int samplesize;
double meansub;
double meansyn;
double meannonsyn;
double sub1;
double sub2;
double sub3;
double branchlength;
public:
BranchStat(CodonStateSpace* instatespace) {
cstatespace = instatespace;
samplesize = 0;
meansub = 0;
meansyn = 0;
meannonsyn = 0;
sub1 = 0;
sub2 = 0;
sub3 = 0;
branchlength = 0;
};
void addnewstat(string input) {
samplesize++;
CodonMappingParser* m = new CodonMappingParser(input, cstatespace);
meannonsyn += m->GetNbNonSynSub();
meansub += m->GetNbSub();
meansyn += m->GetNbSynSub();
sub1 += m->GetNbSingleSub();
sub2 += m->GetNbDoubleSub();
sub3 += m->GetNbTrippleSub();
branchlength += m->GetTotalLength();
delete m;
}
void Normalize() {
meansub /= samplesize;
meannonsyn /= samplesize;
meansyn /= samplesize;
sub1 /= samplesize;
sub2 /= samplesize;
sub3 /= samplesize;
branchlength /= samplesize;
}
double GetMeanNonSyn() {
return meannonsyn;
}
double GetMeanSyn() {
return meansyn;
}
double GetMeanSub() {
return meansub;
}
double GetMeanSingleSub() {
return sub1;
}
double GetMeanDoubleSub() {
return sub2;
}
double GetMeanTrippleSub() {
return sub3;
}
double GetBranchLength(){
return branchlength;
}
};
class ReadMapTree : public Tree {
CodonStateSpace* statespace;
BranchStat** branchstats;
public:
ReadMapTree(string filename) : Tree(filename) {
statespace = new CodonStateSpace(Universal);
SetIndices();
branchstats = new BranchStat*[GetNbranch()];
for (int j = 1; j < GetNbranch(); j++) {
branchstats[j] = new BranchStat(statespace);
}
};
~ReadMapTree() {
for (int j = 1; j < GetNbranch(); j++) {
delete branchstats[j];
}
delete[] branchstats;
};
void JsonToStream(ostream& os) {
os << "{\n";
JsonToStream(GetRoot(), "", os);
os << "}\n";
}
string Rereadtree(Link * from, string input) {
// Position of the last character of the mapping
int end = 0;
if (from->isLeaf()) {
int last = 0;
while ((input[end] != ',') and (input[end] != ')')) {
if (input[end] == '_') {last = end; }
end++;
}
// to remove the name of the leaf
input = input.substr(last, -1);
end -= last;
} else {
// to remove an open bracket
input = input.substr(1, -1);
//recursion
for (Link* link = from->Next(); link != from; link = link->Next()) {
input = Rereadtree(link->Out(), input);
}
// to find the last character of the mapping
while ((input[end] != ',') and (input[end] != ')') and (input[end] != ';')) {
end++;
}
}
// send the maping
if (!from->isRoot()) {
GetBranchStat(from)->addnewstat(input.substr(1, end - 1));
}
// to remove the maping, and a closing bracket or a coma
input = input.substr(end + 1, -1);
return input;
}
void RecursiveNormalise(Link* from) {
for (Link* link = from->Next(); link != from; link = link->Next()) {
RecursiveNormalise(link->Out());
}
if (!from->isRoot()) {
GetBranchStat(from)->Normalize();
}
}
double GetMeanNonSyn(){
return RecursiveAddValue(GetRoot(), &BranchStat::GetMeanNonSyn);
}
double GetMeanSyn(){
return RecursiveAddValue(GetRoot(), &BranchStat::GetMeanSyn);
}
double GetMeanSub(){
return RecursiveAddValue(GetRoot(), &BranchStat::GetMeanSub);
}
double GetMeanSingleSub(){
return RecursiveAddValue(GetRoot(), &BranchStat::GetMeanSingleSub);
}
double GetMeanDoubleSub(){
return RecursiveAddValue(GetRoot(), &BranchStat::GetMeanDoubleSub);
}
double GetMeanTrippleSub(){
return RecursiveAddValue(GetRoot(), &BranchStat::GetMeanTrippleSub);
}
private:
BranchStat* GetBranchStat(Link* from) {
if (from->isRoot()) {
cerr << "The root should not have been call";
}
return branchstats[from->GetBranch()->GetIndex()];
}
double RecursiveAddValue(Link* from, double(BranchStat::*ptr)() ){
double value = 0;
for (Link* link = from->Next(); link != from; link = link->Next()) {
value += RecursiveAddValue(link->Out(), ptr);
}
if (!from->isRoot()) {
value += (GetBranchStat(from)->*ptr)();
}
return value;
}
void JsonToStream(Link* from, string tab, ostream& os) {
tab += ' ';
tab += ' ';
if (!from->isRoot()) {
os << tab << "\"length\": \"" << GetBranchStat(from)->GetBranchLength() << "\",\n";
os << tab << "\"Sub\": \"" << GetBranchStat(from)->GetMeanSub() << "\",\n";
os << tab << "\"SingleSub\": \"" << GetBranchStat(from)->GetMeanSingleSub() << "\",\n";
os << tab << "\"DoubleSub\": \"" << GetBranchStat(from)->GetMeanDoubleSub() << "\",\n";
os << tab << "\"TrippleSub\": \"" << GetBranchStat(from)->GetMeanTrippleSub() << "\",\n";
os << tab << "\"dN\": \"" << GetBranchStat(from)->GetMeanNonSyn() << "\",\n";
os << tab << "\"dS\": \"" << GetBranchStat(from)->GetMeanSyn() << "\",\n";
os << tab << "\"dNdS\": \"" << GetBranchStat(from)->GetMeanNonSyn() / GetBranchStat(from)->GetMeanSyn() << "\",\n";
os << tab << "\"mapping\": \"" << GetBranchName(from).c_str() << "\",\n";
os << tab << "\"name\": \"" << GetBranchName(from).substr(0, 3);
if (from->isLeaf()) {
os << '_' << from->GetNode()->GetName();
}
os << '\"';
} else {
os << tab << "\"length\": \"0\",\n";
os << tab << "\"nbSub\": \"na\",\n";
os << tab << "\"name\": \"" << from->GetNode()->GetName() << '\"';
}
if (from->isLeaf()) {
os << '\n';
} else {
os << ",\n";
os << tab << "\"children\":\n";
os << tab << '[';
for (const Link* link = from->Next(); link != from; link = link->Next()) {
os << "{\n";
JsonToStream(link->Out(), tab, os);
if (link->Next() != from) {
os << tab << "},\n" << tab;
} else {
os << tab << "}]\n";
}
}
}
}
};
int main(int argc, char* argv[]) {
string mapname = argv[1];
ReadMapTree* tree = new ReadMapTree(mapname);
ifstream is(mapname.c_str());
if (!is) {
cout << "cannot find file : " << mapname << '\n';
}
string line;
while (!is.eof()) {
is >> line;
tree->Rereadtree(tree->GetRoot(), line);
is >> line;
is >> line;
}
tree->RecursiveNormalise(tree->GetRoot());
ofstream os((mapname+".json").c_str());
tree->JsonToStream(os);
os.close();
double dN = tree->GetMeanNonSyn();
double dS = tree->GetMeanSyn();
double omega = -1;
if (dS != 0) {
omega = dN / dS;
}
cout << "sub:\t" << tree->GetMeanSub() << '\n';
cout << "dS:\t" << dS << '\n';
cout << "dN:\t" << dN << '\n';
cout << "omega:\t" << omega << '\n';
cout << "Single:\t" << tree->GetMeanSingleSub() << '\n';
cout << "Double:\t" << tree->GetMeanDoubleSub() << '\n';
cout << "Trippl:\t" << tree->GetMeanTrippleSub() << '\n';
}