-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_tests.cpp
1927 lines (1306 loc) · 88.6 KB
/
main_tests.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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifdef USE_MAIN_TEST
#include "trees/node.h"
#include "trees/polysolver.h"
#include "trees/newicklex.h"
#include "trees/genespeciestreeutil.h"
#include "trees/polysolver_nad.h"
#include "trees/polysolver_distance.h"
#include "div/tinydir.h"
#include "trees/autester.h"
#include <fstream>
#include <string>
#include <unordered_map>
#include <iostream>
using namespace std;
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
// FOR NAD CORRECTION
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
//IN MAIN :
//-gl 598 -s "C:\Users\Manuel\Desktop\School\PolytomySolver_home\data_eric\speciestree.newick" -g "C:\Users\Manuel\Desktop\School\PolytomySolver_home\data_eric\star_trees.trees" -d "C:\Users\Manuel\Desktop\School\PolytomySolver_home\data_eric\famille_598.dist" -v2
//-gl 1 -s "C:\Users\Manuel\Desktop\PolytomySolverDistance_1.2\example_files\speciestree_test.newick" -g "C:\Users\Manuel\Desktop\PolytomySolverDistance_1.2\example_files\test_tree.trees" -d "C:\Users\Manuel\Desktop\PolytomySolverDistance_1.2\example_files\famille_test.dist" -v -r outputallroots -o C:\Users\Manuel\Desktop\PolytomySolverDistance_1.2\example_files\test_out.txt
//-gl 1 -s "C:\Users\Manuel\Desktop\PolytomySolverDistance_1.2\example_files\speciestree.newick" -g "C:\Users\Manuel\Desktop\PolytomySolverDistance_1.2\example_files\star_trees.trees" -d "C:\Users\Manuel\Desktop\PolytomySolverDistance_1.2\example_files\famille_1.dist" -v -r outputallroots -o C:\Users\Manuel\Desktop\PolytomySolverDistance_1.2\example_files\test_out.txt
//HERE I SHAMELESSLY LEAVE MY JUNK CODE !
//EvaluateRFDistFile();
// CorrectFolderNADs("/u/lafonman/Projects/all_protein_trees_70/testtrees/", "/u/lafonman/Projects/all_protein_trees_70/testtrees_corrected/");
// FindTreesInWhichEnsemblCorrectedNADs();
//CorrectFolderNADs("C:/Users/Manuel/Desktop/School/PolytomySolver_home/data/fishtrees/", "C:/Users/Manuel/Desktop/School/PolytomySolver_home/data/fishtrees_corrected_clever/");
//CompareNADCounts("C:/Users/Manuel/Desktop/School/PolytomySolver_home/data/fishtrees_corrected_clever/", "C:/Users/Manuel/Desktop/School/PolytomySolver_home/data/fishtrees_corrected_v2/");
// PolySolverNAD solver;
// solver.PerformRandomTest(4);
//DoParalogyCorrection();
//return 0;
/*for (int k = 13; k <= 15; k++)
{
for (int j = 0; j < 1000; j++)
{
PolySolverNAD* solver = new PolySolverNAD();
cout<<"Test k="<<k<<", iter="<<j<<" : ";
solver->PerformRandomTest(k); //, (k == 7) ? 1 : 0);
delete solver;
}
}
return 0;
//string sstr = "(((XL0, (XL3, XL1))S11, (XR0, XR1)S6)_0_1, (((XL4, ((AD_G4_G8, (AD_G3_G8, XL8)), XR3))S15, ((XL2, ((AD_G2_G4, (XL7, XL5)), (XR4, AD_G2_G3)))S4, (XR5, (XR7, XR2))S13)_2_5_7)_4, (((((XL6, XR8)S10, ((S7, S9), (S0, S16))), (S3, XR6))_6, ((S2, (S8, S5)), S1)), (S14, S17)))_8)_3;";
//string gstr = "((XL0, XR0)G0, (XL1, XR1)G1, (AD_G2_G4, (AD_G2_G3, (XL2, XR2)))G2, (AD_G2_G3, (AD_G3_G8, (XL3, XR3)))G3, (XR4, ((XL4, AD_G2_G4), AD_G4_G8))G4, (XL5, XR5)G5, (XL6, XR6)G6, (XL7, XR7)G7, (XL8, ((XR8, AD_G3_G8), AD_G4_G8))G8);";
string sstr = "(((XL2, (XL5, (XL0, AD_G2_G5)))S2, ((XL6, (XR2, XR0))S4, XR6)_6)_0_2, ((((AD_G1_G7, XL4), ((XL7, AD_G4_G5), (XL1, XR5)))S1, (XR7, (XR4, XR1))S5)_1_4_7, (XL3, XR3)_3))_5;";
string gstr = "((XL0, XR0)G0, (XR1, (XL1, AD_G1_G7))G1, (XR2, (XL2, AD_G2_G5))G2, (XL3, XR3)G3, (XL4, (AD_G4_G5, XR4))G4, (AD_G4_G5, (XL5, (AD_G2_G5, XR5)))G5, (XL6, XR6)G6, (AD_G1_G7, (XL7, XR7))G7);";
Node* s = NewickLex::ParseNewickString(sstr, true);
Node* g = NewickLex::ParseNewickString(gstr);
unordered_map<Node*,Node*> lcaMap = GeneSpeciesTreeUtil::Instance()->GetLCAMapping(g, s, "*", 0);
cout<<"NADS B4="<<GeneSpeciesTreeUtil::Instance()->GetNADNodes(g,s,lcaMap).size()<<endl;
PolySolverNAD solverX;
Node* solved = solverX.SolvePolytomy(g, s, lcaMap);
cout<<"SOLVED="<<endl<<NewickLex::ToNewickString(solved)<<endl;
unordered_map<Node*,Node*> lcaMapSolved = GeneSpeciesTreeUtil::Instance()->GetLCAMapping(solved, s, "*", 0);
vector<Node*> nads = GeneSpeciesTreeUtil::Instance()->GetNADNodes(solved,s,lcaMapSolved);
cout<<"NADS="<<nads.size()<<endl;
cout<<"COMPS="<<solverX.last_nb_ad_components;
return 0;
vector<Node*> forest;
for (int i = 0; i < g->GetNbChildren(); i++)
{
forest.push_back(g->GetChild(i));
}
SADNADGraph graph;
graph.BuildGraph(forest, s, lcaMap);
vector<pair<Node*, Node*> > useful = graph.GetUsefulSpeciationEdges();
/*useful.clear();
useful.push_back( make_pair(g->GetNodeWithLabel("G7"), g->GetNodeWithLabel("G0")));
useful.push_back( make_pair(g->GetNodeWithLabel("G6"), g->GetNodeWithLabel("G2")));
useful.push_back( make_pair(g->GetNodeWithLabel("G6"), g->GetNodeWithLabel("G0")));
useful.push_back( make_pair(g->GetNodeWithLabel("G4"), g->GetNodeWithLabel("G1")));
//
cout<<"ALL USEFUL EDGES"<<endl;
for (vector<pair<Node*, Node*> >::iterator it = useful.begin(); it != useful.end(); it++)
{
cout<<(*it).first->GetLabel()<<" "<<(*it).second->GetLabel()<<endl;
}
cout<<endl<<endl;
set<pair<Node*, Node*> > ch;
PolySolverNAD solver;
solver.bestSolSoFar = 9999;
solver.bestChoiceSoFar.clear();
solver.FindBestSolution(graph, useful, ch, 0);
cout<<"BEST SOL="<<solver.bestSolSoFar<<" NADS"<<endl;
for (set<pair<Node*, Node*> >::iterator it = solver.bestChoiceSoFar.begin(); it != solver.bestChoiceSoFar.end(); it++)
{
cout<<(*it).first->GetLabel()<<" "<<(*it).second->GetLabel()<<endl;
}
cout<<"------------------------"<<endl;
return -1;
*/
map<string, string> FindTreesInWhichEnsemblCorrectedNADs()
{
//CORRESPONDANCE FORMAT : V74FILE:V70FILE
map<string, string> treePairsOfInterest;
string speciesNewick = "((((((Xiphophorus maculatus, Oryzias latipes), Gasterosteus aculeatus),Oreochromis niloticus), (Takifugu rubripes,Tetraodon nigroviridis)), Gadus morhua), Danio rerio)";
Node* speciesTree = NewickLex::ParseNewickString(speciesNewick, true);
string corr = Util::GetFileContent("/u/lafonman/Projects/protein_trees_correspondance.txt");
vector<string> lines = Util::Split(corr, "\n", false);
int cptMoreNadsIn74 = 0;
int cptLessNadsIn74 = 0;
int cptSameNads = 0;
for (int i = 0; i < lines.size(); i++)
{
vector<string> parts = Util::Split(lines[i],":", false);
if (parts.size() == 2 && parts[1] != "NONE")
{
string t1str = Util::GetFileContent(parts[0]);
Node* pre_tree1 = NewickLex::ParseNewickString(t1str);
Node* tree1 = pre_tree1;
//HACK ALERT ! The following is due to a bug in which the tree might have a single child root.
if (pre_tree1->GetNbChildren() == 1)
{
tree1 = pre_tree1->GetChild(0);
}
unordered_map<Node*, Node*> geneSpeciesMapping1 = GeneSpeciesTreeUtil::Instance()->GetGeneSpeciesMappingByLabel(tree1, speciesTree, "___", 1);
unordered_map<Node*, Node*> lcaMapping1 = GeneSpeciesTreeUtil::Instance()->GetLCAMapping(tree1, speciesTree, geneSpeciesMapping1);
string t2str = Util::GetFileContent(parts[1]);
Node* pre_tree2 = NewickLex::ParseNewickString(t2str);
Node* tree2 = pre_tree2;
if (pre_tree2->GetNbChildren() == 1)
{
tree2 = pre_tree2->GetChild(0);
}
unordered_map<Node*, Node*> geneSpeciesMapping2 = GeneSpeciesTreeUtil::Instance()->GetGeneSpeciesMappingByLabel(tree2, speciesTree, "___", 1);
unordered_map<Node*, Node*> lcaMapping2 = GeneSpeciesTreeUtil::Instance()->GetLCAMapping(tree2, speciesTree, geneSpeciesMapping2);
vector<Node*> nads1 = GeneSpeciesTreeUtil::Instance()->GetNADNodes(tree1, speciesTree, lcaMapping1);
vector<Node*> nads2 = GeneSpeciesTreeUtil::Instance()->GetNADNodes(tree2, speciesTree, lcaMapping2);
//same # of leaves, but less nads = interesting
if (geneSpeciesMapping1.size() == geneSpeciesMapping2.size())
{
if (nads1.size() < nads2.size())
cptLessNadsIn74++;
else if (nads1.size() > nads2.size())
cptMoreNadsIn74++;
else
cptSameNads++;
//cout<<nads1.size()<<" "<<nads2.size()<<endl;
if (false && nads1.size() < nads2.size())
{
cout<<parts[0]<<":"<<parts[1]<<" --> "<<nads2.size()<<" - "<<nads1.size()<<" = "<<(nads2.size() - nads1.size())<<endl;
cout<<geneSpeciesMapping1.size()<<" "<<geneSpeciesMapping2.size()<<endl;
treePairsOfInterest[parts[0]] = parts[1];
string v70corrected_file = Util::ReplaceAll(parts[1], "fishtrees", "fishtrees_corrected_v2");
/*string v70corrected_newick = Util::GetFileContent(v70corrected_file);
Node* pre_v70corrected = NewickLex::ParseNewickString(v70corrected_newick);
Node* v70_corrected = pre_v70corrected;
if (v70_corrected->GetNbChildren() == 1)
v70_corrected = v70_corrected->GetChild(0);
unordered_map<Node*, Node*> geneSpeciesMapping70_corrected = GeneSpeciesTreeUtil::Instance()->GetGeneSpeciesMappingByLabel(v70_corrected, speciesTree, "___", 1);
unordered_map<Node*, Node*> lcaMapping70_corrected = GeneSpeciesTreeUtil::Instance()->GetLCAMapping(v70_corrected, speciesTree, geneSpeciesMapping70_corrected);
vector<Node*> nads70Corrected = GeneSpeciesTreeUtil::Instance()->GetNADNodes(v70_corrected, speciesTree, lcaMapping70_corrected);
v70corrected_newick = NewickLex::ToNewickString(v70_corrected);
delete pre_v70corrected;*/
string tree1newick = NewickLex::ToNewickString(tree1);
string tree2newick = NewickLex::ToNewickString(tree2);
string f74 = parts[0];
string f70 = parts[1];
string pairFile = "/u/lafonman/Projects/nad_data/v70_vs_v74/" + Util::GetPathFilename(f70) + "__vs__" + Util::GetPathFilename(f74);
string pairContent = "V70FILE=" + f70 + "\n" + "V70NADS=" + Util::ToString((int)nads2.size()) + "\n" + "V70TREE=" + tree2newick + "\n" +
"V70CORRFILE=" + v70corrected_file + "\n" + //"V70CORRNADS=" + Util::ToString((int)nads70Corrected.size()) + "\n" + "V70CORRTREE=" + v70corrected_newick + "\n" +
"V74FILE=" + f74 + "\n" + "V74NADS=" + Util::ToString((int)nads1.size()) + "\n" + "V74TREE=" + tree1newick + "\n";
cout<<"WRITING "<<pairFile<<endl;
Util::WriteFileContent(pairFile, pairContent);
}
}
delete pre_tree1;
delete pre_tree2;
}
cout<<"MORE-NADS-IN-74="<<cptMoreNadsIn74<<endl<<"LESS-NADS-IN-74="<<cptLessNadsIn74<<endl<<"SAME-NADS="<<cptSameNads<<endl<<"TOTAL="<<(cptMoreNadsIn74 + cptLessNadsIn74 + cptSameNads)<<endl;
}
delete speciesTree;
return treePairsOfInterest;
}
void EvaluateRFDistFile()
{
/*tinydir_dir dir;
tinydir_open(&dir, "/u/lafonman/Projects/all_protein_trees_70/fishtrees_corrected_v2/");
int cnt = 0;
while (dir.has_next)
{
tinydir_file file;
tinydir_readfile(&dir, &file);
if (!file.is_dir)
{
string fullname = "/u/lafonman/Projects/all_protein_trees_70/fishtrees_corrected_v2/" + string(file.name);
string poly1 = "";
string poly2 = "";
string corrclade = "";
string nadsBefore = "";
string nadsAfter = "";
string v70corrcontent = Util::GetFileContent(fullname);
vector<string> v70lines = Util::Split(v70corrcontent, "\n", false);
for (int v70l = 0; v70l < v70lines.size(); v70l++)
{
vector<string> kv = Util::Split(v70lines[v70l], "=", false);
string key = kv[0];
string val = kv[1];
if (key == "POLYSIZE1")
poly1 = val;
if (key == "POLYSIZE2")
poly2 = val;
if (key == "CORRECTEDCLADE")
corrclade = val;
if (key == "NADSBEFORE")
nadsBefore = val;
if (key == "NADSAFTER")
nadsAfter = val;
}
if (Util::ToInt(poly1) > 3 || Util::ToInt(poly2) > 2)
{
cout<<"P1="<<poly1<<" P2="<<poly2<<" NB="<<nadsBefore<<" NA="<<nadsAfter<<" F="<<fullname<<endl;
cnt++;
}
}
tinydir_next(&dir);
}
tinydir_close(&dir);
cout<<cnt<<endl;*/
vector<string> fieldsToOutput;
fieldsToOutput.push_back("V70C_POLYSIZE1");
fieldsToOutput.push_back("V70C_POLYSIZE2");
fieldsToOutput.push_back("V70C_NADSBEFORE");
fieldsToOutput.push_back("V70C_NADSAFTER");
fieldsToOutput.push_back("V74_NADS");
fieldsToOutput.push_back("V70C_DLSCORE");
fieldsToOutput.push_back("V74_DLSCORE");
fieldsToOutput.push_back("V74_NADS");
fieldsToOutput.push_back("V74_CLADETYPE");
fieldsToOutput.push_back("V74_ISGONE");
fieldsToOutput.push_back("NBLEAVES");
fieldsToOutput.push_back("CORRECTED_CLADE_SIZE");
fieldsToOutput.push_back("RF70_70CORR_V");
fieldsToOutput.push_back("RF70_74_V");
fieldsToOutput.push_back("RF70CORR_74_V");
fieldsToOutput.push_back("RF70_70CORR");
fieldsToOutput.push_back("RF70_74");
fieldsToOutput.push_back("RF70CORR_74");
fieldsToOutput.push_back("V70CORRFILE");
fieldsToOutput.push_back("V74FILE");
for (int k = 0; k < fieldsToOutput.size(); k++)
{
if (k != 0)
cout<<" ";
cout<<fieldsToOutput[k];
}
cout<<endl;
string speciesNewick = "((((((Xiphophorus maculatus, Oryzias latipes), Gasterosteus aculeatus),Oreochromis niloticus), (Takifugu rubripes,Tetraodon nigroviridis)), Gadus morhua), (Astyanax mexicanus,Danio rerio))";
Node* speciesTree = NewickLex::ParseNewickString(speciesNewick, true);
int cptSpec = 0, cptNAD = 0, cptAD = 0, cptGone = 0;
string content = Util::GetFileContent("/u/lafonman/Projects/nad_data/rfdists_v5.txt");
vector<string> lines = Util::Split(content, "\n", false);
for (int i = 0; i < lines.size(); i++)
{
map<string, string> theLineData;
string rfdist = "";
string v70corrfile = "";
string nadsBefore = "";
string nadsAfter = "";
string v74nads = "";
Node* v70Tree = NULL;
Node* v74Tree = NULL;
Node* v70correctedTree = NULL;
vector<string> correctedClade;
vector<string> params = Util::Split(lines[i], ";;", false);
for (int p = 0; p < params.size(); p++)
{
vector<string> kv = Util::Split(params[p], "=");
string key = kv[0];
string val = kv[1];
if (key == "RF")
rfdist = val;
if (key == "V70CORRFILE")
v70corrfile = val;
if (key == "V74NADS")
v74nads = val;
if (key == "V74FILE")
{
string ttttt = Util::GetFileContent(val);
v74Tree = NewickLex::ParseNewickString(ttttt);
//TODO : MEMORY LEAK HERE
if (v74Tree->GetNbChildren() == 1)
v74Tree = v74Tree->GetChild(0);
}
if (key == "V70FILE")
{
string uuuuu = Util::GetFileContent(val);
v70Tree = NewickLex::ParseNewickString(uuuuu);
//TODO : MEMORY LEAK HERE
if (v70Tree->GetNbChildren() == 1)
v70Tree = v70Tree->GetChild(0);
}
theLineData[key] = val;
}
string poly1 = "";
string poly2 = "";
if (v70corrfile != "")
{
string v70corrcontent = Util::GetFileContent(v70corrfile);
vector<string> v70lines = Util::Split(v70corrcontent, "\n", false);
for (int v70l = 0; v70l < v70lines.size(); v70l++)
{
vector<string> kv = Util::Split(v70lines[v70l], "=", false);
string key = kv[0];
string val = kv[1];
if (key == "POLYSIZE1")
poly1 = val;
if (key == "POLYSIZE2")
poly2 = val;
if (key == "NADSBEFORE")
nadsBefore = val;
if (key == "NADSAFTER")
nadsAfter = val;
if (key == "CORRECTEDTREE")
{
v70correctedTree = NewickLex::ParseNewickString(val);
//TODO : MEMORY LEAK HERE
if (v70correctedTree->GetNbChildren() == 1)
v70correctedTree = v70correctedTree->GetChild(0);
}
if (key == "CORRECTEDCLADE")
{
correctedClade = Util::Split(val, ";");
}
}
}
theLineData["V70C_POLYSIZE1"] = poly1;
theLineData["V70C_POLYSIZE2"] = poly2;
theLineData["V70C_NADSBEFORE"] = nadsBefore;
theLineData["V70C_NADSAFTER"] = nadsAfter;
theLineData["RF70_70CORR_V"] = Util::GetSubstringBefore(theLineData["RF70_70CORR"], "/");
theLineData["RF70_74_V"] = Util::GetSubstringBefore(theLineData["RF70_74"], "/");
theLineData["RF70CORR_74_V"] = Util::GetSubstringBefore(theLineData["RF70CORR_74"], "/");
theLineData["V74_NADS"] = v74nads;
//theLineData["RFDIST"] = rfdist;
theLineData["V70CORRFILE"] = v70corrfile;
if (v70Tree && v74Tree && v70correctedTree)
{
set<Node*> v70leaves = v70correctedTree->GetLeafSet();
theLineData["NBLEAVES"] = Util::ToString((int)v70leaves.size());
unordered_map<Node*, Node*> v70correctedMapping = GeneSpeciesTreeUtil::Instance()->GetLCAMapping(v70correctedTree, speciesTree, "___", 1);
int v70corrected_DL = GeneSpeciesTreeUtil::Instance()->GetDLScore(v70correctedTree, speciesTree, v70correctedMapping);
theLineData["V70C_DLSCORE"] = Util::ToString(v70corrected_DL);
unordered_map<Node*, Node*> v74mapping = GeneSpeciesTreeUtil::Instance()->GetLCAMapping(v74Tree, speciesTree, "___", 1);
int v74_DL = GeneSpeciesTreeUtil::Instance()->GetDLScore(v74Tree, speciesTree, v74mapping);
theLineData["V74_DLSCORE"] = Util::ToString(v74_DL);
vector<Node*> v74CladeLeaves;
for (int k = 0; k < correctedClade.size(); k++)
{
Node* g = v74Tree->GetNodeWithLabel(correctedClade[k]);
v74CladeLeaves.push_back(g);
}
theLineData["CORRECTED_CLADE_SIZE"] = Util::ToString((int)correctedClade.size());
Node* lca = v74Tree->FindLCA(v74CladeLeaves);
set<Node*> lcaLeaves = lca->GetLeafSet();
string lcaType = "";
if (v74mapping[lca->GetChild(0)] != v74mapping[lca] && v74mapping[lca->GetChild(1)] != v74mapping[lca])
{
lcaType = "SPEC";
//cout<<"CLADE IS A SPEC"<<endl;
theLineData["V74_CLADETYPE"] = "S";
//-------------------------------------------------------------
//SPECIAL CODE HERE : run au test on ensembl made 'S' and polysize is interesting
//if (Util::ToInt(poly1) > 2 || Util::ToInt(poly2) > 2)
//{
// RunAUTestOnTrees(Util::GetSubstringAfter(theLineData["V74FILE"], "/"), v74Tree, v70correctedTree);
//}
//-------------------------------------------------------------
cptSpec++;
}
else
{
if (!GeneSpeciesTreeUtil::Instance()->HaveCommonSpecies(lca->GetChild(0), lca->GetChild(1), v74mapping))
{
lcaType = "NAD";
theLineData["V74_CLADETYPE"] = "NAD";
//cout<<"CLADE IS A NAD"<<endl;
cptNAD++;
}
else
{
lcaType = "AD";
theLineData["V74_CLADETYPE"] = "AD";
//cout<<"CLADE IS AN AD"<<endl;
cptAD++;
}
}
if (lcaLeaves.size() != correctedClade.size())
{
//cout<<"CLADE IS GONE (type="<<lcaType<<")"<<endl;
theLineData["V74_ISGONE"] = "1";
cptGone++;
}
else
{
theLineData["V74_ISGONE"] = "0";
}
//cout<<"V70C_DL="<<v70corrected_DL<<" V74DL="<<v74_DL<<endl;
for (int k = 0; k < fieldsToOutput.size(); k++)
{
string fval = "-";
if (theLineData.find(fieldsToOutput[k]) != theLineData.end())
fval = Util::ReplaceAll(theLineData[fieldsToOutput[k]], " ", "_");
if (k != 0)
cout<<" ";
cout<<fval;
}
cout<<endl;
}
// if (Util::ToInt(poly1) > 2 || Util::ToInt(poly2) > 2)
{
/*if (Util::ToInt(nadsBefore) == 1)
{
//cout<<i<<" : RF="<<rfdist<<" P1="<<poly1<<" P2="<<poly2<<" NB="<<nadsBefore<<" NA="<<nadsAfter<<" NA(Ensembl)="<<v74nads<<" V70="<<v70corrfile<<endl;
cpt++;
if (rfdist.find("0/") != string::npos)
cptrf0++;
}*/
}
correctedClade.clear();
delete v70Tree;
delete v74Tree;
delete v70correctedTree;
}
//cout<<"GONE="<<cptGone<<" SPEC="<<cptSpec<<" NAD="<<cptNAD<<" AD="<<cptAD<<endl;
//cout<<cpt<<endl;
//cout<<cptrf0<<endl;
}
void CorrectFolderNADs(string folderIn, string folderOut)
{
string speciesNewick = "((((((Xiphophorus maculatus, Oryzias latipes), Gasterosteus aculeatus),Oreochromis niloticus), (Takifugu rubripes,Tetraodon nigroviridis)), Gadus morhua), (Astyanax mexicanus,Danio rerio))";
Node* speciesTree = NewickLex::ParseNewickString(speciesNewick, true);
tinydir_dir dir;
tinydir_open(&dir, folderIn.c_str());
int cpt = 0, cptCorrected = 0;
int cptDidNothing = 0, cptKilledOne = 0, cptKilledMore = 0, cptCreatedMore = 0;
while (dir.has_next)
{
tinydir_file file;
tinydir_readfile(&dir, &file);
if (file.is_dir)
{
;
}
else
{
string filename = string(file.name); //for some reason, calling file.name later causes crashes
string fullname = folderIn + filename;
if (fullname.find(".newick") != string::npos)
{
std::ifstream ifs( fullname );
std::string content( (std::istreambuf_iterator<char>(ifs) ),
(std::istreambuf_iterator<char>() ));
//cout<<fullname<<endl<<content<<endl;
Node* geneTree = NewickLex::ParseNewickString(content);
Node* realGeneTree = geneTree;
//HACK ALERT ! The following is due to a bug in which the tree might have a single child root.
//PolySolver expects its input gene trees to be binary
if (realGeneTree->GetNbChildren() == 1)
{
realGeneTree = realGeneTree->GetChild(0);
}
unordered_map<Node*, Node*> geneSpeciesMapping = GeneSpeciesTreeUtil::Instance()->GetGeneSpeciesMappingByLabel(realGeneTree, speciesTree, "___", 1);
PolySolverNAD solver;
PolySolverCorrectionInfo info = solver.CorrectHighestNAD(realGeneTree, speciesTree, geneSpeciesMapping);
Node* solved = info.correction;
if (solved)
{
Node* realSolved = solved;
if (solved->GetNbChildren() == 1)
realSolved = solved->GetChild(0);
//TODO : lca mappings were already computed by solver
//cout<<NewickLex::ToNewickString(realSolved);
//return;
unordered_map<Node*, Node*> beforeLCAMapping = GeneSpeciesTreeUtil::Instance()->GetLCAMapping(realGeneTree, speciesTree, geneSpeciesMapping);
vector<Node*> nadsBefore = GeneSpeciesTreeUtil::Instance()->GetNADNodes(realGeneTree, speciesTree, beforeLCAMapping);
unordered_map<Node*, Node*> afterGeneSpeciesMapping = GeneSpeciesTreeUtil::Instance()->GetGeneSpeciesMappingByLabel(realSolved, speciesTree, "___", 1);
unordered_map<Node*, Node*> afterLCAMapping = GeneSpeciesTreeUtil::Instance()->GetLCAMapping(realSolved, speciesTree, afterGeneSpeciesMapping);
vector<Node*> nadsAfter = GeneSpeciesTreeUtil::Instance()->GetNADNodes(realSolved, speciesTree, afterLCAMapping);
cout<<"BEFORE : "<<nadsBefore.size()<<", AFTER : "<<nadsAfter.size()<<endl;
if (nadsBefore.size() == nadsAfter.size())
cptDidNothing++;
if (nadsBefore.size() == nadsAfter.size() + 1)
cptKilledOne++;
if (nadsBefore.size() > nadsAfter.size() + 1)
cptKilledMore++;
if (nadsBefore.size() < nadsAfter.size())
cptCreatedMore++;
cptCorrected++;
string baseDir = "/u/lafonman/Projects/PolytomySolver/data/";
string treePairsDir = baseDir + "treepairs/";
string fastaDir = baseDir + "fasta/";
string alignementsDir = baseDir + "alignments/";
string conseloutDir = baseDir + "conselout/";
string treeID = Util::GetSubstringAfter(Util::ReplaceAll(fullname, ".newick", ""), "/");
//AUTester tester;
//tester.RunAUTest(realGeneTree, solved, treeID, "___", 0, 1, treePairsDir, fastaDir, alignementsDir, conseloutDir);
cout<<"WRITING "<<folderOut<<filename<<endl;
string outtreefile = folderOut + filename;
string newick = NewickLex::ToNewickString(solved);
string beforeNewick = NewickLex::ToNewickString(realGeneTree);
string txt = "ORIGINALFILE=" + fullname + "\n" +
"ORIGINALTREE=" + beforeNewick + "\n" +
"NADSBEFORE=" + Util::ToString((int)nadsBefore.size()) + "\n" +
"NADSAFTER=" + Util::ToString((int)nadsAfter.size()) + "\n" +
"POLYSIZE1=" + Util::ToString(info.firstPolySize) + "\n" +
"POLYSIZE2=" + Util::ToString(info.secondPolySize) + "\n" +
"CORRECTEDCLADE=";
for (int i = 0; i < info.nadCladeGenes.size();i++)
{
if (i != 0)
txt += ";";
txt += info.nadCladeGenes[i];
}
txt = txt + "\n" + "CORRECTEDTREE=" + newick;
Util::WriteFileContent(outtreefile, txt);
delete solved;
}
delete geneTree;
cpt++;
}
}
tinydir_next(&dir);
}
tinydir_close(&dir);
cout<<"FILES="<<cpt<<endl<<"CORRECTED="<<cptCorrected<<endl<<
"KILLED ONE="<<cptKilledOne<<endl<<"KILLED MORE="<<cptKilledMore<<endl<<
"DID NOTHING="<<cptDidNothing<<endl<<"HARMFUL="<<cptCreatedMore<<endl;
delete speciesTree;
}
void CompareNADCounts(string folder1, string folder2)
{
string speciesNewick = "((((((Xiphophorus maculatus, Oryzias latipes), Gasterosteus aculeatus),Oreochromis niloticus), (Takifugu rubripes,Tetraodon nigroviridis)), Gadus morhua), (Astyanax mexicanus,Danio rerio))";
Node* speciesTree = NewickLex::ParseNewickString(speciesNewick, true);
tinydir_dir dir;
tinydir_open(&dir, folder1.c_str());
int cntDiff = 0;
while (dir.has_next)
{
tinydir_file file;
tinydir_readfile(&dir, &file);
if (!file.is_dir)
{
string fullname1 = folder1 + string(file.name);
string fullname2 = folder2 + string(file.name);
if (!Util::FileExists(fullname1) || !Util::FileExists(fullname2))
continue;
string c1 = Util::GetFileContent(fullname1);
Node* t1 = NewickLex::ParseNewickString(c1);
//TODO : leak here, root not deleted
if (t1->GetNbChildren() == 1)
t1 = t1->GetChild(0);
string c2 = Util::GetFileContent(fullname2);
Node* t2 = NewickLex::ParseNewickString(c2);
if (t2->GetNbChildren() == 1)
t2 = t2->GetChild(0);
unordered_map<Node*, Node*> geneSpeciesMapping1 = GeneSpeciesTreeUtil::Instance()->GetGeneSpeciesMappingByLabel(t1, speciesTree, "___", 1);
unordered_map<Node*, Node*> lcaMapping1 = GeneSpeciesTreeUtil::Instance()->GetLCAMapping(t1, speciesTree, geneSpeciesMapping1);
vector<Node*> nads1 = GeneSpeciesTreeUtil::Instance()->GetNADNodes(t1, speciesTree, lcaMapping1);
unordered_map<Node*, Node*> geneSpeciesMapping2 = GeneSpeciesTreeUtil::Instance()->GetGeneSpeciesMappingByLabel(t2, speciesTree, "___", 1);
unordered_map<Node*, Node*> lcaMapping2 = GeneSpeciesTreeUtil::Instance()->GetLCAMapping(t2, speciesTree, geneSpeciesMapping2);
vector<Node*> nads2 = GeneSpeciesTreeUtil::Instance()->GetNADNodes(t2, speciesTree, lcaMapping2);
cout<<nads1.size()<<" VS "<<nads2.size()<<endl;
cntDiff += (int)(nads1.size() - nads2.size());
delete t1;
delete t2;
}
tinydir_next(&dir);
}
tinydir_close(&dir);
cout<<"CNTDIFF="<<cntDiff<<endl;
delete speciesTree;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
// END NAD CORRECTION
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
map<string, string> FindCorrespondingTrees(string folder1, string folder2)
{
map<string,string> correspondance;
map<Node*, string> treesToFile1;
map<Node*, string> treesToFile2;
tinydir_dir dir;
tinydir_open(&dir, folder1.c_str());
while (dir.has_next)
{
tinydir_file file;
tinydir_readfile(&dir, &file);
if (!file.is_dir)
{
string fullname = folder1 + string(file.name);
string content1 = Util::GetFileContent(fullname);
Node* tree1 = NewickLex::ParseNewickString(content1);
if (tree1->GetLeafVector().size() > 3)
{
treesToFile1[tree1] = fullname;
}
}
tinydir_next(&dir);
}
tinydir_close(&dir);
cout<<"LOADED DIR1 WITH "<<treesToFile1.size()<<" CONSIDERED"<<endl;
tinydir_open(&dir, folder2.c_str());
while (dir.has_next)
{
tinydir_file file2;
tinydir_readfile(&dir, &file2);
if (!file2.is_dir)
{
string fullname2 = folder2 + string(file2.name);
string content2 = Util::GetFileContent(fullname2);
Node* tree2 = NewickLex::ParseNewickString(content2);
if (tree2->GetLeafVector().size() > 3)
{
treesToFile2[tree2] = fullname2;
}
}
tinydir_next(&dir);
}
tinydir_close(&dir);
cout<<"LOADED DIR2 WITH "<<treesToFile2.size()<<" CONSIDERED"<<endl;
for (map<Node*, string>::iterator t1it = treesToFile1.begin(); t1it != treesToFile1.end(); t1it++)
{
Node* tree1 = (*t1it).first;
string file1 = (*t1it).second;
set<Node*> leaves1 = tree1->GetLeafSet();
for (map<Node*, string>::iterator t2it = treesToFile2.begin(); t2it != treesToFile2.end(); t2it++)
{
Node* tree2 = (*t2it).first;
string file2 = (*t2it).second;
set<Node*> leaves2 = tree2->GetLeafSet();
bool foundEverything = true; //true until proven otherwise
for (set<Node*>::iterator it1 = leaves1.begin(); it1 != leaves1.end() && foundEverything; it1++)
{
bool foundN1 = false;
Node* n1 = (*it1);
for (set<Node*>::iterator it2 = leaves2.begin(); it2 != leaves2.end() && !foundN1; it2++)
{
Node* n2 = (*it2);
if (n1->GetLabel() == n2->GetLabel())
{
foundN1 = true;
}
}
if (!foundN1)
foundEverything = false;
}
if (foundEverything)
{
correspondance[file1] = file2;
cout<<file1<<":"<<file2<<endl;
}
}
if (correspondance.find(file1) == correspondance.end())
{
cout<<file1<<":NONE"<<endl;
}
}
//TODO : delete trees
return correspondance;
}
map<string, string> FindTreesInWhichEnsemblCorrectedNADs()
{
//CORRESPONDANCE FORMAT : V74FILE:V70FILE
map<string, string> treePairsOfInterest;
string speciesNewick = "((((((Xiphophorus maculatus, Oryzias latipes), Gasterosteus aculeatus),Oreochromis niloticus), (Takifugu rubripes,Tetraodon nigroviridis)), Gadus morhua), Danio rerio)";
Node* speciesTree = NewickLex::ParseNewickString(speciesNewick, true);
string corr = Util::GetFileContent("/u/lafonman/Projects/protein_trees_correspondance.txt");
vector<string> lines = Util::Split(corr, "\n", false);