-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
773 lines (526 loc) · 20.5 KB
/
main.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
#define NO_ENSEMBL
/*#include <QtCore/QCoreApplication>*/
#include "trees/paralogycorrector.h"
#include "trees/newicklex.h"
#include "div/util.h"
#include <string>
#include <vector>
/*#include <QDebug>
#include <QProcess>*/
#include <iostream>
#include <fstream>
using namespace std;
/**
What's going on here :
Every tree we want to correct is in a .parc file (here, the hard-coded test.parc),
whcih is a pseudo-xml file.
This file includes, for every tree, the list of gene names,
their corresponding species, the gene tree, the species tree,
and the orthology constraints. Most of the main() is dedicated to parsing this file,
and the magic happens on these two lines :
ParalogyCorrector pc;
ctree = pc.CorrectGeneTree(genetree, speciestree, geneSpeciesMapping, orthologs);
The .parc file is not necessary, but here it is used to fill up
the genetree, speciestree, geneSpeciesMapping, orthologs parameters.
*/
int main(int argc, char *argv[])
{
//leave this set empty to parse all trees
set<string> restrictToTrees;
map<string, string> args;
string format = "new";
string prevArg = "";
for (int i = 0; i < argc; i++)
{
if (prevArg != "" && prevArg[0] == '-')
{
args[Util::ReplaceAll(prevArg, "-", "")] = string(argv[i]);
}
prevArg = string(argv[i]);
}
if (args.find("i") == args.end())
{
cout<<"Error : please specify an input .parc file with -i"<<endl;
return -1;
}
string infile = args["i"];
string outfile = "";
if (args.find("o") != args.end())
{
outfile = args["o"];
}
if (args.find("f") != args.end())
{
format = args["f"];
}
if (!Util::FileExists(infile))
{
cout<<"Error : specified infile "<<infile<<" does not exist"<<endl;
return -1;
}
std::ifstream infilestream(infile);
//Parse the parc file, extract everythng
//allModeStrings is the list of instances to correct (demarcated by <INSTANCE>...</INSTANCE>)
//The map<string, string> for a given instance in allModeStrings,
//contains specific values of the instance, the key being the xml tag and the value, well, the value
//eg. string newickk = my_map["<GENETREE>"]
vector<map<string, string> > allModeStrings;
map<string, string> curModeStrings;
bool inInstance = false;
std::string line;
string mode = "";
string modestr = "";
while (std::getline(infilestream, line))
{
if (mode == "")
{
if (line == "<INSTANCE>")
inInstance = true;
else if (line == "</INSTANCE>" && inInstance)
{
if (restrictToTrees.size() == 0 || restrictToTrees.find(curModeStrings["<TREEID>"]) != restrictToTrees.end())
{
allModeStrings.push_back(curModeStrings);
}
curModeStrings = map<string, string>();
mode = "";
modestr = "";
inInstance = false;
}
else if (line == "<CONSTRAINTS>" || line == "<GENETREE>" || line == "<SPECIESTREE>" || line == "<GENESPECIESMAPPING>" || line == "<TREEID>")
mode = line;
}
else
{
string ender = Util::ReplaceAll(mode, "<", "</");
if (line == ender)
{
curModeStrings[mode] = modestr;
mode = "";
modestr = "";
}
else
{
if (line != "")
{
if (modestr != "")
modestr += "\n";
modestr += line;
}
}
}
}
infilestream.close();
string strout = "";
//For each <INSTANCE> in the .parc file, build up the parameters and correct the tree
for (int i = 0; i < allModeStrings.size(); i++)
{
map<string, string> modeStrings = allModeStrings[i];
string treeID = modeStrings["<TREEID>"];
Node* genetree = NewickLex::ParseNewickString(modeStrings["<GENETREE>"], false);
Node* speciestree = NewickLex::ParseNewickString(modeStrings["<SPECIESTREE>"], true);
Node* ctree = NULL;
//Here we parse the <GENESPECIESMAPPING> to build, you guessed it, geneSpeciesMapping
map<Node*, Node*> geneSpeciesMapping;
vector<string> geneSpeciesLines = Util::Split(modeStrings["<GENESPECIESMAPPING>"], "\n");
//TODO : this lazy loop really sucks - we re-search the tree everytime we need a specific node
// Consider using treeInfo, which keeps a map of label -> node
for (int j = 0; j < geneSpeciesLines.size(); j++)
{
vector<string> gs = Util::Split(geneSpeciesLines[j], ":");
Node* g = genetree->GetNodeWithLabel(gs[0], true);
Node* s = speciestree->GetNodeWithLabel(gs[1], true);
geneSpeciesMapping[g] = s;
}
//And here we parse the "<CONSTRAINTS>", the pairs of gene names required to be orthologs
vector<pair<string, string> > orthologs;
vector<string> orthoLines = Util::Split(modeStrings["<CONSTRAINTS>"], "\n");
for (int j = 0; j < orthoLines.size(); j++)
{
vector<string> xz = Util::Split(orthoLines[j], ":");
pair<string, string> p = make_pair(xz[0], xz[1]);
orthologs.push_back(p);
}
ParalogyCorrector pc;
ctree = pc.CorrectGeneTree(genetree, speciestree, geneSpeciesMapping, orthologs);
//ctree might be NULL if no correction was applied
if (ctree)
{
if (format == "old")
{
strout += "TREEID : " + treeID + "\n";
strout += "BEFORE : " + NewickLex::ToNewickString(genetree) + "\n";
strout += "AFTER : " + NewickLex::ToNewickString(ctree) + "\n";
}
else
{
strout += "<INSTANCE>\n<TREEID>\n" + treeID + "\n</TREEID>\n<BEFORE>\n" + NewickLex::ToNewickString(genetree) +
"\n</BEFORE>\n<AFTER>\n" + NewickLex::ToNewickString(ctree) + "\n</AFTER>\n</INSTANCE>\n";
}
delete ctree;
}
else
{
if (format == "old")
{
strout += "TREEID : " + treeID + " WAS NOT CORRECTED";
}
else
{
strout += "<INSTANCE>\n<TREEID>\n" + treeID + "\n</TREEID><BEFORE>\n" + NewickLex::ToNewickString(genetree) +
"\n</BEFORE>\n<AFTER>\n" + NewickLex::ToNewickString(genetree) + "\n</AFTER>\n<ERROR>\nWas not corrected\n</ERROR>\n</INSTANCE>\n";
}
}
geneSpeciesMapping.clear();
geneSpeciesLines.clear();
orthologs.clear();
orthoLines.clear();
if (outfile != "")
{
ofstream outfilestream;
outfilestream.open (outfile);
outfilestream << strout;
outfilestream.close();
}
else
{
cout<<strout;
}
delete genetree;
delete speciestree;
}
return 0;
}
//What follows is just the implementation of the pipeline used to correct a bunch of trees and evaluate them
//BEWARE THE HARD-CODED PATHS
/*string GetFastaFetcherCommand(Node* tree, string &outfile)
{
//WE ASSUME GENE LABEL FORMAT IS species_geneid
//the getfasta command invokes a perl script that fetches all nucleotide sequences of the tree genes
//this command will be dropped if user chose to use peptide sequences instead
string commandFasta = "getfasta_b.perl -s \"";
TreeIterator* it = tree->GetPostOrderIterator();
while (Node* n = it->next())
{
if (n->IsLeaf())
{
vector<string> sg = Util::Split(n->GetLabel(), "_");
commandFasta += sg[0] + ":" + sg[1] + ";";
//commandFasta += EnsemblUtil::Instance()->GetSpeciesEnsemblName(info->taxa, GLOB_COMPARA_DBNAME) + ":" + info->geneStableID + ";";
}
}
tree->CloseIterator(it);
commandFasta += "\"";
if (outfile != "")
commandFasta += " > " + outfile;
commandFasta+= "\n";
//qDebug()<<QString::fromStdString(commandFasta);
return commandFasta;
}
void FetchFasta(Node* genetree, string &outfile, string &treeID)
{
//fasta file might exist
ifstream ifile(outfile);
if (ifile) {
qDebug()<<QString::fromStdString(outfile)<<" exists";
ifile.close();
return;
}
//fasta file might exist from another project
string prevFasta = "/u/lafonman/tmp/temp" + treeID + ".fasta";
ifstream tfile(prevFasta, ios::binary);
if (tfile) {
qDebug()<<QString::fromStdString(prevFasta)<<" exists (will copy)";
std::ofstream ofs(outfile, std::ios::binary);
ofs << tfile.rdbuf();
ofs.close();
tfile.close();
return;
}
string tstr = "";
string command = GetFastaFetcherCommand(genetree, tstr);
QProcess *fastaProcess = new QProcess();
fastaProcess->setStandardOutputFile(QString::fromStdString(outfile));
qDebug()<<"FASTA-ING" <<QString::fromStdString(treeID + " -- " + command);
fastaProcess->start(QString::fromStdString(command));
fastaProcess->waitForFinished(-1);
qDebug()<<"DONE FASTA-ING WITH CODE "<<fastaProcess->error();
delete fastaProcess;
}
void AlignFile(string &fastaFile, string &outfile, string &repairedOutfile)
{
QProcess *clustalProcess = new QProcess();
string command = "clustalw2 -INFILE=" + fastaFile + " -OUTFILE=" + outfile + " -OUTPUT=NEXUS";
qDebug()<<"ALIGNING "; //<<QString::fromStdString(command);
clustalProcess->start(QString::fromStdString(command));
clustalProcess->waitForFinished(-1);
qDebug()<<"DONE ALIGNING WITH CODE "<<clustalProcess->error();
delete clustalProcess;
QProcess *repairProcess = new QProcess();
string in = outfile;
command = "nexrepair.py -i \"" + in + "\" -o \"" + repairedOutfile + "\"";
qDebug()<<"REPAIRING";
repairProcess->start(QString::fromStdString(command));
repairProcess->waitForFinished(-1);
qDebug()<<"DONE REPAIRING WITH CODE "<<repairProcess->error();
delete repairProcess;
}
void ExecutePhyML(string alignedFile, string treesFile)
{
ifstream alfile(alignedFile);
if (!alfile) {
qDebug()<<QString::fromStdString(alignedFile)<<" DOESN'T exists FOR phyml !";
return;
}
alfile.close();
string command = "phyml -i " + alignedFile +
" -u " + treesFile + " -n 1 -o l --print_site_lnl --no_memory_check";
QProcess *phymlProcess = new QProcess();
qDebug()<<"PHYML-ing "<<QString::fromStdString(command);
phymlProcess->setProcessChannelMode(QProcess::MergedChannels);
phymlProcess->start(QString::fromStdString(command));
phymlProcess->waitForReadyRead();
while (phymlProcess->canReadLine())
{
QByteArray bline = phymlProcess->readLine();
QString line(bline);
if (line.toUpper().contains("TYPE ANY KEY"))
{
phymlProcess->putChar('13');
}
}
phymlProcess->waitForFinished(-1);
qDebug()<<"DONE PHYML-ing WITH CODE "<<phymlProcess->error();
delete phymlProcess;
}
void ExecuteConsel(string &phyMLFile, string &resultsFile)
{
string command = "/u/lafonman/consel/consel/bin/makermt --phyml " + phyMLFile;
QProcess *mkmtProcess = new QProcess();
qDebug()<<"MAKERMT";
mkmtProcess->start(QString::fromStdString(command));
mkmtProcess->waitForFinished(-1);
qDebug()<<"DONE MAKERMT WITH CODE "<<mkmtProcess->error();
delete mkmtProcess;
string noext = Util::ReplaceAll(phyMLFile, ".txt", "");
command = "/u/lafonman/consel/consel/bin/consel " + noext;
QProcess *conselProcess = new QProcess();
qDebug()<<"CONSEL";
conselProcess->start(QString::fromStdString(command));
conselProcess->waitForFinished(-1);
qDebug()<<"DONE MAKERMT WITH CODE "<<conselProcess->error();
delete conselProcess;
command = "/u/lafonman/consel/consel/bin/catpv " + noext + ".pv -s 1";
QProcess *catpvProcess = new QProcess();
qDebug()<<"CATPV";
catpvProcess->setStandardOutputFile(QString::fromStdString(resultsFile));
catpvProcess->start(QString::fromStdString(command));
catpvProcess->waitForFinished(-1);
qDebug()<<"DONE CATPV WITH CODE "<<catpvProcess->error();
delete catpvProcess;
}
void OutputTrees(Node* originalTree, Node* correctedTree, string &outfile, string &treeID)
{
//WE ASSUME GENE LABEL FORMAT IS species_geneid
//make a copy since we change labels
Node* treeBefore = new Node(false);
treeBefore->CopyFrom(originalTree);
Node* treeAfter = new Node(false);
treeAfter->CopyFrom(correctedTree);
TreeIterator* it = treeBefore->GetPostOrderIterator();
while (Node* n = it->next())
{
if (n->IsLeaf())
{
vector<string> sg = Util::Split(n->GetLabel(), "_");
n->SetLabel(sg[1]);
}
else
{
n->SetLabel("");
}
}
treeBefore->CloseIterator(it);
it = treeAfter->GetPostOrderIterator();
while (Node* n = it->next())
{
if (n->IsLeaf())
{
vector<string> sg = Util::Split(n->GetLabel(), "_");
n->SetLabel(sg[1]);
}
else
{
n->SetLabel("");
}
}
treeBefore->CloseIterator(it);
ofstream myfile;
myfile.open (outfile);
myfile << NewickLex::ToNewickString(treeBefore) << "\n" << NewickLex::ToNewickString(treeAfter);
myfile.close();
delete treeBefore;
delete treeAfter;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
//leave this set empty to parse all trees
set<string> restrictToTrees;
//when outfasta != "", it just outputs the fasta fetch command for the given tree only, and does nothing else
string outfasta = ""; //ENSGT00390000008140";
string outfastaFile = "/u/lafonman/Projects/ParalogyCorrector/work/sequences/" + outfasta + ".fasta";
string treesDir = "/u/lafonman/Projects/ParalogyCorrector/work/trees/";
string fastaDir = "/u/lafonman/Projects/ParalogyCorrector/work/unaligned_seqs/";
string alignedDir = "/u/lafonman/Projects/ParalogyCorrector/work/aligned_seqs/";
string conselOutDir = "/u/lafonman/Projects/ParalogyCorrector/work/results/";
std::ifstream infile("/u/lafonman/Projects/ParalogyCorrector/work/v2-3.parc");
//Parse the parc file, extract everythng
vector<map<string, string> > allModeStrings;
map<string, string> curModeStrings;
bool inInstance = false;
std::string line;
string mode = "";
string modestr = "";
while (std::getline(infile, line))
{
if (mode == "")
{
if (line == "<INSTANCE>")
inInstance = true;
else if (line == "</INSTANCE>" && inInstance)
{
if (outfasta == "" || outfasta == curModeStrings["<TREEID>"])
{
if (restrictToTrees.size() == 0 || restrictToTrees.find(curModeStrings["<TREEID>"]) != restrictToTrees.end())
{
allModeStrings.push_back(curModeStrings);
}
}
curModeStrings = map<string, string>();
mode = "";
modestr = "";
inInstance = false;
}
else if (line == "<CONSTRAINTS>" || line == "<GENETREE>" || line == "<SPECIESTREE>" || line == "<GENESPECIESMAPPING>" || line == "<TREEID>")
mode = line;
}
else
{
string ender = Util::ReplaceAll(mode, "<", "</");
if (line == ender)
{
curModeStrings[mode] = modestr;
mode = "";
modestr = "";
}
else
{
if (line != "")
{
if (modestr != "")
modestr += "\n";
modestr += line;
}
}
}
}
infile.close();
string outstr;
QString qoutstr;
int nbNull = 0;
for (int i = 0; i < allModeStrings.size(); i++)
{
map<string, string> modeStrings = allModeStrings[i];
string treeID = modeStrings["<TREEID>"];
Node* genetree = NewickLex::ParseNewickString(modeStrings["<GENETREE>"], false);
Node* speciestree = NewickLex::ParseNewickString(modeStrings["<SPECIESTREE>"], true);
Node* ctree = NULL;
//special mode - just get fasta sequence
if (outfasta != "")
{
GetFastaFetcherCommand(genetree, outfastaFile);
}
else
{
map<Node*, Node*> geneSpeciesMapping;
vector<string> geneSpeciesLines = Util::Split(modeStrings["<GENESPECIESMAPPING>"], "\n");
//TODO : this lazy loop really sucks - we re-search the tree everytime we need a specific node
// Consider using treeInfo, which keeps a map of label -> node
for (int j = 0; j < geneSpeciesLines.size(); j++)
{
vector<string> gs = Util::Split(geneSpeciesLines[j], ":");
Node* g = genetree->GetNodeWithLabel(gs[0], true);
Node* s = speciestree->GetNodeWithLabel(gs[1], true);
geneSpeciesMapping[g] = s;
}
vector<pair<string, string> > orthologs;
vector<string> orthoLines = Util::Split(modeStrings["<CONSTRAINTS>"], "\n");
for (int j = 0; j < orthoLines.size(); j++)
{
vector<string> xz = Util::Split(orthoLines[j], ":");
pair<string, string> p = make_pair(xz[0], xz[1]);
orthologs.push_back(p);
}
string conselOut = conselOutDir + treeID + ".consel";
ifstream resultsfile(conselOut);
if (resultsfile) {
qDebug()<<"RESULTS " + QString::fromStdString(conselOut) + " exists";
resultsfile.close();
}
else
{
ParalogyCorrector pc;
ctree = pc.CorrectGeneTree(genetree, speciestree, geneSpeciesMapping, orthologs);
if (ctree)
{
//qDebug()<<QString::number(i) + "/" + QString::number(allModeStrings.size());
//string newick = NewickLex::ToNewickString(ctree);
//for some extremely weird reasons, that line below makes the program enter an infinite loop
//outstr += treeID + "\n" + newick + "\n\n";
//qoutstr += QString::fromStdString(treeID + "\n" + newick + "\n\n");
if (treesDir != "")
{
qDebug()<<"Outputting "<<QString::fromStdString(treesDir + treeID)<<".tree";
string treesFile = treesDir + treeID + ".trees";
OutputTrees(genetree, ctree, treesFile, treeID);
if (fastaDir != "")
{
string fastaFile = fastaDir + treeID + ".fasta";
FetchFasta(genetree, fastaFile, treeID);
if (alignedDir != "")
{
string aligned = alignedDir + treeID + ".nxs";
string repaired = alignedDir + treeID + "-rep.nxs";
AlignFile(fastaFile, aligned, repaired);
ExecutePhyML(repaired, treesFile);
if (conselOutDir != "")
{
string conselIn = repaired + "_phyml_lk.txt";
ExecuteConsel(conselIn, conselOut);
}
}
}
}
delete ctree;
}
else
{
nbNull++;
}
}
geneSpeciesMapping.clear();
geneSpeciesLines.clear();
orthologs.clear();
orthoLines.clear();
}
delete genetree;
delete speciestree;
//if (ctree)
// delete ctree;
}
//qDebug()<<QString::fromStdString(outstr);
qDebug()<<qoutstr;
qDebug()<<nbNull<<" null trees";
//return a.exec();
}*/