-
Notifications
You must be signed in to change notification settings - Fork 0
/
AggregateRanger_recipient.cpp
617 lines (447 loc) · 12.7 KB
/
AggregateRanger_recipient.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
/*
* * Copyright (C) 2017 Mukul S. Bansal ([email protected]).
* *
* * This program is free software: you can redistribute it and/or modify
* * it under the terms of the GNU General Public License as published by
* * the Free Software Foundation, either version 3 of the License, or
* * (at your option) any later version.
* *
* * This program is distributed in the hope that it will be useful,
* * but WITHOUT ANY WARRANTY; without even the implied warranty of
* * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* * GNU General Public License for more details.
* *
* * You should have received a copy of the GNU General Public License
* * along with this program. If not, see <http://www.gnu.org/licenses/>.
* */
#define THRESHOLD 100
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <set>
#include <ctype.h>
#include <string.h>
//#include <stdlib>
//#include <boost/lexical_cast.hpp>
using namespace std;
struct data // for parsing data from files into array
{
string genenode;
bool isLeaf;
int event; // 0 = speciation, 1 = duplication, 2 = transfer
string mapping;
string recipient;
};
struct data2 // for storing event counts
{
int spec;
int dup;
int tran;
bool isLeaf;
};
struct data3 // for storing all the mappings from a gene node
{
string node;
int count;
string recnode; // and recipients
int reccount;
};
int main (int argc, char* argv[])
{
char filename[100];
char s[100];
string line, part1, part2, part3;
string numsols; // to store the line containing the total number of optimal solutions
// stringstream out;
// string s;
// int numfiles = boost::lexical_cast<int>(argv[2] );
int numfiles = 1;
int i;
int FilesFullCount = 0; // to check is all the input reconciliation files have run to completion (i.e., they didn't crash)
bool FilesFullFlag = false;
vector< vector<data> > array;
vector<data> arrayline;
vector<data2> counts;
data2 tempcounts;
vector< vector<data3> > mappings;
vector<data3> tempmappings;
data3 tempmap;
vector<data3> frequentmapping;
// Added vectors to mirror the functionality of mapping vectors
vector< vector<data3> > recipients;
vector<data3> temprecipients;
data3 temprec;
vector<data3> frequentrecipient;
size_t found;
size_t found2;
size_t found3;
int start = 0;
// outer loop to iterate over the different input files
for(i = 1; ; i++)
{
// filename.clear();
// out << i;
// s = out.str();
arrayline.clear();
sprintf(s,"%d",i);
// itoa(i, s, 10);
strcpy (filename,argv[1]);
strcat(filename,s);
//filename = argv[1] + s; // prepare the file name
start = 0; // to keep track of when we are in the reconcliation block
ifstream myfile;
myfile.open(filename);
if (myfile.is_open ())
{
while (myfile.good ()) // inner lop to iterate over all lines in the file
{
data temp;
getline (myfile, line);
found3 = line.find("optimal"); // to copy the number of optimal solutions
if (found3!=string::npos)
{
FilesFullCount++;
numsols = line;
// cout << numsols << endl;
}
found=line.find("Reconciliation:");
if (found!=string::npos)
{
start = 1;
continue;
}
if (start == 1)
{
found=line.find(":");
if(found == string::npos)
start = 0;
}
if (start == 1)
{
//cout << line << endl;
found=line.find(":");
if(found ==string::npos)
cout << "ERROR!!" << endl;
part1 = line.substr (0, found);
temp.genenode = part1;
part2 = line.substr(found+1);
found=part2.find("Leaf");
if(found !=string::npos)
temp.isLeaf = true;
else temp.isLeaf = false;
found=part2.find("Speciation");
if(found !=string::npos)
{
temp.event = 0;
found = part2.find("-->");
part3 = part2.substr(found+4);
temp.mapping = part3;
}
found=part2.find("Duplication");
if(found !=string::npos)
{
temp.event = 1;
found = part2.find("-->");
part3 = part2.substr(found+4);
temp.mapping = part3;
}
found=part2.find("Transfer");
if(found !=string::npos)
{
temp.event = 2;
found = part2.find("-->");
found2 = part2.find(",", found+4);
part3 = part2.substr(found+4, (found2 - (found + 4)));
temp.mapping = part3;
temp.recipient = part2.substr(found2 + 16);
}
arrayline.push_back(temp);
// cout << temp.isLeaf << " " << temp.genenode << " " << temp.event << " " << temp.mapping << " " << temp.recepient << endl;
}
}
myfile.close ();
// fill the main array with the parsed data from all files
array.push_back(arrayline);
}
else
{
if (i ==1)
{
cout << "ERROR: Unable to open reconciliation files. Please ensure the files' filename-prefix and path are correct." << endl;
return 1;
}
else
cout << "Processed " << i-1 << " files" << endl;
if (FilesFullCount == (i-1))
{
FilesFullFlag = true;
}
break;
}
}
int x = array[0].size();
int y = array.size();
// cout << y << endl;
// process to consolidate events
for (int i = 0; i < x; i++)
{
tempcounts.spec = 0;
tempcounts.tran = 0;
tempcounts.dup = 0;
tempcounts.isLeaf = false;
for(int j = 0; j < y ; j++)
{
if (array[j][i].isLeaf == true)
{
tempcounts.isLeaf = true;
break;
}
if (array[j][i].event == 0)
{
tempcounts.spec = tempcounts.spec + 1;
}
else if (array[j][i].event == 1)
{
tempcounts.dup = tempcounts.dup + 1;
}
else if (array[j][i].event == 2)
{
tempcounts.tran = tempcounts.tran + 1;
}
}
counts.push_back(tempcounts);
}
// process to consolidate the mappings for any given gene node.
multiset<string> myset;
multiset<string>::iterator it;
for (int i = 0; i < x; i++)
{
tempmappings.clear();
myset.clear();
for(int j = 0; j < y ; j++)
{
if (array[j][i].isLeaf == true)
{
break;
}
myset.insert(array[j][i].mapping);
}
if (array[0][i].isLeaf == true)
{
tempmap.node = " ";
tempmap.count = -1;
tempmappings.push_back(tempmap);
}
else
{
for (it=myset.begin(); it!=myset.end(); it++)
{
tempmap.node.assign(*it);
tempmap.count = 1;
// remove the extra wierd characters from the end of *it char array.
char c;
for (int k = 0; k < tempmap.node.size(); k++)
{
char c = tempmap.node[k];
if (iscntrl(c))
tempmap.node.erase(k,1);
}
// cout << tempmap.node.size() << " " << tempmap.node << "XYZ" << tempmap.node.size() <<endl;
if (tempmappings.size() > 0)
{
if (tempmappings[tempmappings.size()-1].node == tempmap.node)
{
tempmappings[tempmappings.size()-1].count = tempmappings[tempmappings.size()-1].count + 1;
}
else
tempmappings.push_back(tempmap);
}
else
tempmappings.push_back(tempmap);
}
}
mappings.push_back(tempmappings);
}
// process to consolidate the recipient mappings for any given gene node.
for (int i = 0; i < x; i++)
{
temprecipients.clear();
myset.clear();
for(int j = 0; j < y ; j++)
{
if (array[j][i].event == 2)
{
myset.insert(array[j][i].recipient);
}
}
// if (array[0][i].isLeaf == true)
// {
// temprec.recnode = " ";
// temprec.reccount = -1;
//
// temprecipients.push_back(tempmrec);
// }
// else
// {
for (it=myset.begin(); it!=myset.end(); it++)
{
temprec.recnode.assign(*it);
temprec.reccount = 1;
// remove the extra wierd characters from the end of *it char array.
char c;
for (int k = 0; k < temprec.recnode.size(); k++)
{
char c = temprec.recnode[k];
if (iscntrl(c))
temprec.recnode.erase(k,1);
}
// cout << tempmap.node.size() << " " << tempmap.node << "XYZ" << tempmap.node.size() <<endl;
if (temprecipients.size() > 0)
{
if (temprecipients[temprecipients.size()-1].recnode == temprec.recnode)
{
temprecipients[temprecipients.size()-1].reccount += 1;
}
else
temprecipients.push_back(temprec);
}
else
temprecipients.push_back(temprec);
}
// }
recipients.push_back(temprecipients);
}
// process to choose the most frequent mapping for any given gene node
for (int i = 0; i < x; i++)
{
tempmappings.clear();
int best = 0;
int mapsize = mappings[i].size();
if (array[0][i].isLeaf == true)
{
tempmap.node = " ";
tempmap.count = -1;
frequentmapping.push_back(tempmap);
}
else
{
for(int j = 0; j< mapsize; j++)
{
if(mappings[i][j].count > best)
{
best = mappings[i][j].count;
tempmap.node = mappings[i][j].node;
tempmap.count = mappings[i][j].count;
}
}
frequentmapping.push_back(tempmap);
}
}
// process to choose the most frequent recipient for any given gene transfer
for (int i = 0; i < x; i++)
{
temprecipients.clear();
int best = 0;
int recsize = recipients[i].size();
// if (array[0][i].isLeaf == true)
// {
// temprec.recnode = " ";
// temprec.reccount = -1;
// frequentrecipient.push_back(temprec);
// }
// else
// {
for(int j = 0; j< recsize; j++)
{
if(recipients[i][j].reccount > best)
{
best = recipients[i][j].reccount;
temprec.recnode = recipients[i][j].recnode;
temprec.reccount = recipients[i][j].reccount;
}
}
frequentrecipient.push_back(temprec);
// }
}
// calculate fraction of nodes with reliable events and mappings
int totalnonleaf = 0;
int goodevents = 0;
int goodmappings = 0;
int goodrecipients = 0;
for (int i = 0; i < x; i++)
{
if (array[0][i].isLeaf == true)
{
}
else
{
totalnonleaf++;
if (((counts[i].spec * 100)/y) >= THRESHOLD)
{
goodevents++;
}
else if (((counts[i].dup * 100)/y) >= THRESHOLD)
{
goodevents++;
}
else if (((counts[i].tran * 100)/y) >= THRESHOLD)
{
goodevents++;
}
if (((frequentmapping[i].count * 100)/y) >= THRESHOLD)
{
goodmappings++;
}
if (((frequentrecipient[i].reccount * 100)/y) >= THRESHOLD)
{
goodrecipients++;
}
}
}
// check if all files were full
if (FilesFullFlag == false)
cout << "ERROR: Some reconciliation runs did not terminate correctly (" << FilesFullCount << " terminated correctly)" << endl;
// output the aggregate information
cout << endl << endl << "Aggregate reconciliation:" << endl;
// cout << x << " " << y << endl;
for (int i = 0; i < x; i++)
{
cout << array[0][i].genenode << ": ";
if (array[0][i].isLeaf == true)
{
cout << "Leaf Node" << endl;
}
else
{
cout <<"[Speciations = " << counts[i].spec << ", Duplications = " << counts[i].dup << ", Transfers = " << counts[i].tran << "]";
if (counts[i].tran > 0)
{
cout << ", [Most Frequent mapping --> " << frequentmapping[i].node << ", " << frequentmapping[i].count << " times]";
cout << ", [Most Frequent recipient --> " << frequentrecipient[i].recnode << ", " << frequentrecipient[i].reccount << " times]." << endl;
}
else
{
cout << ", [Most Frequent mapping --> " << frequentmapping[i].node << ", " << frequentmapping[i].count << " times]." << endl;
}
}
}
if (FilesFullFlag == true)
{
cout << endl << endl << "Percentage of events with " << THRESHOLD << "% consistency = " << (((float)goodevents * 100)/(float)totalnonleaf) << endl;
cout << "Percentage of mappings with " << THRESHOLD << "% consistency = " << (((float)goodmappings * 100)/(float)totalnonleaf) << endl;
// cout << "Percentage of recipients with " << THRESHOLD << "% consistency = " << (((float)goodrecipients * 100)/(float)counts.tran) << endl;
cout << "Total number of non-leaf nodes in gene tree: " << totalnonleaf << endl;
cout << numsols << endl;
}
else
{
cout << endl << endl << "Percentage of events with " << THRESHOLD << "% consistency = ERROR" << endl;
cout << "Percentage of mappings with " << THRESHOLD << "% consistency = ERROR" << endl;
// cout << "Percentage of recipients with " << THRESHOLD << "% consistency = ERROR" << endl;
cout << "Total number of non-leaf nodes in gene tree: ERROR" << endl;
cout << "Total number of optimal solutions: ERROR" << endl;
}
return 0;
}