]> git.donarmstrong.com Git - mothur.git/blob - phylotree.cpp
paralellized chimera.uchime for windows for both by group and with a template.
[mothur.git] / phylotree.cpp
1 /*
2  *  doTaxonomy.cpp
3  *  
4  *
5  *  Created by Pat Schloss on 6/17/09.
6  *  Copyright 2009 Patrick D. Schloss. All rights reserved.
7  *
8  */
9
10 #include "phylotree.h"
11
12 /**************************************************************************************************/
13
14 PhyloTree::PhyloTree(){
15         try {
16                 m = MothurOut::getInstance();
17                 numNodes = 1;
18                 numSeqs = 0;
19                 tree.push_back(TaxNode("Root"));
20                 tree[0].heirarchyID = "0";
21                 maxLevel = 0;
22                 calcTotals = true;
23         }
24         catch(exception& e) {
25                 m->errorOut(e, "PhyloTree", "PhyloTree");
26                 exit(1);
27         }
28 }
29 /**************************************************************************************************/
30
31 PhyloTree::PhyloTree(ifstream& in, string filename){
32         try {
33                 m = MothurOut::getInstance();
34                 calcTotals = false;
35                 numNodes = 0;
36                 numSeqs = 0;
37                 
38                 #ifdef USE_MPI
39                         MPI_File inMPI;
40                         MPI_Offset size;
41                         MPI_Status status;
42
43                         char inFileName[1024];
44                         strcpy(inFileName, filename.c_str());
45
46                         MPI_File_open(MPI_COMM_WORLD, inFileName, MPI_MODE_RDONLY, MPI_INFO_NULL, &inMPI);  
47                         MPI_File_get_size(inMPI, &size);
48                         
49                         char* buffer = new char[size];
50                         MPI_File_read(inMPI, buffer, size, MPI_CHAR, &status);
51
52                         string tempBuf = buffer;
53                         if (tempBuf.length() > size) { tempBuf = tempBuf.substr(0, size);  }
54                         istringstream iss (tempBuf,istringstream::in);
55                         delete buffer;
56                         
57                         //read version
58                         m->getline(iss); m->gobble(iss);
59                         
60                         iss >> numNodes; m->gobble(iss);
61                         
62                         tree.resize(numNodes);
63                         
64                         for (int i = 0; i < tree.size(); i++) {
65                                 iss >> tree[i].name >> tree[i].level >> tree[i].parent; m->gobble(iss);
66                         }
67                         
68                         //read genus nodes
69                         int numGenus = 0;
70                         iss >> numGenus; m->gobble(iss);
71                         
72                         int gnode, gsize;
73                         totals.clear();
74                         for (int i = 0; i < numGenus; i++) {
75                                 iss >> gnode >> gsize; m->gobble(iss);
76                                 
77                                 uniqueTaxonomies[gnode] = gnode;
78                                 totals.push_back(gsize);
79                         }
80                         
81                         MPI_File_close(&inMPI);
82                         
83                 #else
84                         //read version
85                         string line = m->getline(in); m->gobble(in);
86                         
87                         in >> numNodes; m->gobble(in);
88                         
89                         tree.resize(numNodes);
90                         
91                         for (int i = 0; i < tree.size(); i++) {
92                                 in >> tree[i].name >> tree[i].level >> tree[i].parent; m->gobble(in);
93                         }
94                         
95                         //read genus nodes
96                         int numGenus = 0;
97                         in >> numGenus; m->gobble(in);
98                         
99                         int gnode, gsize;
100                         totals.clear();
101                         for (int i = 0; i < numGenus; i++) {
102                                 in >> gnode >> gsize; m->gobble(in);
103                                 
104                                 uniqueTaxonomies[gnode] = gnode;
105                                 totals.push_back(gsize);
106                         }
107                         
108                         in.close();
109                         
110                 #endif
111                 
112         }
113         catch(exception& e) {
114                 m->errorOut(e, "PhyloTree", "PhyloTree");
115                 exit(1);
116         }
117 }
118 /**************************************************************************************************/
119
120 PhyloTree::PhyloTree(string tfile){
121         try {
122                 m = MothurOut::getInstance();
123                 numNodes = 1;
124                 numSeqs = 0;
125                 tree.push_back(TaxNode("Root"));
126                 tree[0].heirarchyID = "0";
127                 maxLevel = 0;
128                 calcTotals = true;
129                 string name, tax;
130
131                 
132                 #ifdef USE_MPI
133                         int pid, num, processors;
134                         vector<unsigned long long> positions;
135                         
136                         MPI_Status status; 
137                         MPI_File inMPI;
138                         MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
139                         MPI_Comm_size(MPI_COMM_WORLD, &processors);
140
141                         char inFileName[1024];
142                         strcpy(inFileName, tfile.c_str());
143
144                         MPI_File_open(MPI_COMM_WORLD, inFileName, MPI_MODE_RDONLY, MPI_INFO_NULL, &inMPI);  //comm, filename, mode, info, filepointer
145
146                         if (pid == 0) {
147                                 positions = m->setFilePosEachLine(tfile, num);
148                                 
149                                 //send file positions to all processes
150                                 for(int i = 1; i < processors; i++) { 
151                                         MPI_Send(&num, 1, MPI_INT, i, 2001, MPI_COMM_WORLD);
152                                         MPI_Send(&positions[0], (num+1), MPI_LONG, i, 2001, MPI_COMM_WORLD);
153                                 }
154                         }else{
155                                 MPI_Recv(&num, 1, MPI_INT, 0, 2001, MPI_COMM_WORLD, &status);
156                                 positions.resize(num+1);
157                                 MPI_Recv(&positions[0], (num+1), MPI_LONG, 0, 2001, MPI_COMM_WORLD, &status);
158                         }
159                 
160                         //read file 
161                         for(int i=0;i<num;i++){
162                                 //read next sequence
163                                 int length = positions[i+1] - positions[i];
164                                 char* buf4 = new char[length];
165
166                                 MPI_File_read_at(inMPI, positions[i], buf4, length, MPI_CHAR, &status);
167
168                                 string tempBuf = buf4;
169                                 if (tempBuf.length() > length) { tempBuf = tempBuf.substr(0, length); }
170                                 delete buf4;
171
172                                 istringstream iss (tempBuf,istringstream::in);
173                                 iss >> name >> tax;
174                                 addSeqToTree(name, tax);
175                         }
176                         
177                         MPI_File_close(&inMPI);
178                         MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
179                 
180                 #else
181                         ifstream in;
182                         m->openInputFile(tfile, in);
183                         
184                         //read in users taxonomy file and add sequences to tree
185                         while(!in.eof()){
186                                 in >> name >> tax; m->gobble(in);
187                         
188                                 addSeqToTree(name, tax);
189                         }
190                         in.close();
191                 #endif
192         
193                 assignHeirarchyIDs(0);
194         
195                 //create file for summary if needed
196                 setUp(tfile);
197         }
198         catch(exception& e) {
199                 m->errorOut(e, "PhyloTree", "PhyloTree");
200                 exit(1);
201         }
202 }
203
204 /**************************************************************************************************/
205
206 string PhyloTree::getNextTaxon(string& heirarchy, string seqname){
207         try {
208                 string currentLevel = "";
209                 if(heirarchy != ""){
210                         int pos = heirarchy.find_first_of(';');
211                         
212                         if (pos == -1) { //you can't find another ;
213                                 currentLevel = heirarchy;
214                                 heirarchy = "";
215                                 m->mothurOut(seqname + " is missing a ;, please check for other errors."); m->mothurOutEndLine();
216                         }else{
217                                 currentLevel=heirarchy.substr(0,pos);
218                                 if (pos != (heirarchy.length()-1)) {  heirarchy=heirarchy.substr(pos+1);  }
219                                 else { heirarchy = ""; }
220                         }
221                         
222                 }
223                 return currentLevel;
224         }
225         catch(exception& e) {
226                 m->errorOut(e, "PhyloTree", "getNextTaxon");
227                 exit(1);
228         }
229 }
230
231 /**************************************************************************************************/
232
233 int PhyloTree::addSeqToTree(string seqName, string seqTaxonomy){
234         try {
235                         
236                 numSeqs++;
237                 
238                 map<string, int>::iterator childPointer;
239                 
240                 int currentNode = 0;
241                 int level = 1;
242                 
243                 tree[0].accessions.push_back(seqName);
244                 string taxon;// = getNextTaxon(seqTaxonomy);
245         
246                 while(seqTaxonomy != ""){
247                         
248                         level++;
249                 
250                         if (m->control_pressed) { return 0; }
251                         
252                         //somehow the parent is getting one too many accnos
253                         //use print to reassign the taxa id
254                         taxon = getNextTaxon(seqTaxonomy, seqName);
255                         
256                         if (taxon == "") {  m->mothurOut(seqName + " has an error in the taxonomy.  This may be due to a ;;"); m->mothurOutEndLine(); if (currentNode != 0) {  uniqueTaxonomies[currentNode] = currentNode; } break;  }
257                         
258                         childPointer = tree[currentNode].children.find(taxon);
259                         
260                         if(childPointer != tree[currentNode].children.end()){   //if the node already exists, move on
261                                 currentNode = childPointer->second;
262                                 tree[currentNode].accessions.push_back(seqName);
263                                 name2Taxonomy[seqName] = currentNode;
264                         }
265                         else{                                                                                   //otherwise, create it
266                                 tree.push_back(TaxNode(taxon));
267                                 numNodes++;
268                                 tree[currentNode].children[taxon] = numNodes-1;
269                                 tree[numNodes-1].parent = currentNode;
270                                 
271                                 currentNode = tree[currentNode].children[taxon];
272                                 tree[currentNode].accessions.push_back(seqName);
273                                 name2Taxonomy[seqName] = currentNode;
274                         }
275         
276                         if (seqTaxonomy == "") {   uniqueTaxonomies[currentNode] = currentNode; }
277                 }
278                 
279                 return 0;
280         }
281         catch(exception& e) {
282                 m->errorOut(e, "PhyloTree", "addSeqToTree");
283                 exit(1);
284         }
285 }
286 /**************************************************************************************************/
287 vector<int> PhyloTree::getGenusNodes()  {
288         try {
289                 genusIndex.clear();
290                 //generate genusIndexes
291                 map<int, int>::iterator it2;
292                 for (it2=uniqueTaxonomies.begin(); it2!=uniqueTaxonomies.end(); it2++) {  genusIndex.push_back(it2->first);     }
293                 
294                 return genusIndex;
295         }
296         catch(exception& e) {
297                 m->errorOut(e, "PhyloTree", "getGenusNodes");
298                 exit(1);
299         }
300 }
301 /**************************************************************************************************/
302 vector<int> PhyloTree::getGenusTotals() {
303         try {
304         
305                 if (calcTotals) {
306                         totals.clear();
307                         //reset counts because we are on a new word
308                         for (int j = 0; j < genusIndex.size(); j++) {
309                                 totals.push_back(tree[genusIndex[j]].accessions.size());
310                         }
311                         return totals;
312                 }else{
313                         return totals;
314                 }
315                 
316         }
317         catch(exception& e) {
318                 m->errorOut(e, "PhyloTree", "getGenusNodes");
319                 exit(1);
320         }
321 }
322 /**************************************************************************************************/
323
324 void PhyloTree::assignHeirarchyIDs(int index){
325         try {
326                 map<string,int>::iterator it;
327                 int counter = 1;
328                 
329                 for(it=tree[index].children.begin();it!=tree[index].children.end();it++){
330                         tree[it->second].heirarchyID = tree[index].heirarchyID + '.' + toString(counter);
331                         counter++;
332                         tree[it->second].level = tree[index].level + 1;
333                                                 
334                         //save maxLevel for binning the unclassified seqs
335                         if (tree[it->second].level > maxLevel) { maxLevel = tree[it->second].level; } 
336                         
337                         assignHeirarchyIDs(it->second);
338                 }
339         }
340         catch(exception& e) {
341                 m->errorOut(e, "PhyloTree", "assignHeirarchyIDs");
342                 exit(1);
343         }
344 }
345 /**************************************************************************************************/
346 void PhyloTree::setUp(string tfile){
347         try{
348                 string taxFileNameTest = tfile.substr(0,tfile.find_last_of(".")+1) + "tree.sum";
349                 
350                 #ifdef USE_MPI
351                         int pid;
352                         MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
353
354                         if (pid == 0) {  binUnclassified(taxFileNameTest);  }
355                 
356                 #else
357                         binUnclassified(taxFileNameTest); 
358                 #endif
359         }
360         catch(exception& e) {
361                 m->errorOut(e, "PhyloTree", "setUp");
362                 exit(1);
363         }
364 }
365 /**************************************************************************************************/
366 void PhyloTree::binUnclassified(string file){
367         try {
368         
369                 ofstream out;
370                 m->openOutputFile(file, out);
371                 
372                 map<string, int>::iterator itBin;
373                 map<string, int>::iterator childPointer;
374                 
375                 vector<TaxNode> copy = tree;
376                         
377                 //fill out tree
378                 fillOutTree(0, copy);
379         
380                 //get leaf nodes that may need extension
381                 for (int i = 0; i < copy.size(); i++) {  
382
383                         if (copy[i].children.size() == 0) {
384                                 leafNodes[i] = i;
385                         }
386                 }
387                 
388                 int copyNodes = copy.size();
389         
390                 //go through the seqs and if a sequence finest taxon is not the same level as the most finely defined taxon then classify it as unclassified where necessary
391                 map<int, int>::iterator itLeaf;
392                 for (itLeaf = leafNodes.begin(); itLeaf != leafNodes.end(); itLeaf++) {
393                         
394                         if (m->control_pressed) {  out.close(); break;  }
395                         
396                         int level = copy[itLeaf->second].level;
397                         int currentNode = itLeaf->second;
398                         
399                         //this sequence is unclassified at some levels
400                         while(level < maxLevel){
401                 
402                                 level++;
403                         
404                                 string taxon = "unclassified";  
405                                 
406                                 //does the parent have a child names 'unclassified'?
407                                 childPointer = copy[currentNode].children.find(taxon);
408                                 
409                                 if(childPointer != copy[currentNode].children.end()){   //if the node already exists, move on
410                                         currentNode = childPointer->second; //currentNode becomes 'unclassified'
411                                 }
412                                 else{                                                                                   //otherwise, create it
413                                         copy.push_back(TaxNode(taxon));
414                                         copyNodes++;
415                                         copy[currentNode].children[taxon] = copyNodes-1;
416                                         copy[copyNodes-1].parent = currentNode;
417                                         copy[copyNodes-1].level = copy[currentNode].level + 1;
418                                                                         
419                                         currentNode = copy[currentNode].children[taxon];
420                                 }
421                         }
422                 }
423                 
424                 if (!m->control_pressed) {
425                         //print copy tree
426                         print(out, copy);
427                 }
428                                 
429         }
430         catch(exception& e) {
431                 m->errorOut(e, "PhyloTree", "binUnclassified");
432                 exit(1);
433         }
434 }
435 /**************************************************************************************************/
436 void PhyloTree::fillOutTree(int index, vector<TaxNode>& copy) {
437         try {
438         
439                 map<string,int>::iterator it;
440                 
441                 it = copy[index].children.find("unclassified");
442                 if (it == copy[index].children.end()) { //no unclassified at this level
443                         string taxon = "unclassified";
444                         copy.push_back(TaxNode(taxon));
445                         copy[index].children[taxon] = copy.size()-1;
446                         copy[copy.size()-1].parent = index;
447                         copy[copy.size()-1].level = copy[index].level + 1;
448                 }
449                 
450                 if (tree[index].level < maxLevel) {
451                         for(it=tree[index].children.begin();it!=tree[index].children.end();it++){ //check your children
452                                 fillOutTree(it->second, copy);
453                         }
454                 }
455
456         }
457         catch(exception& e) {
458                 m->errorOut(e, "PhyloTree", "fillOutTree");
459                 exit(1);
460         }
461 }
462 /**************************************************************************************************/
463 string PhyloTree::getFullTaxonomy(string seqName) {
464         try {
465                 string tax = "";
466                 
467                 int currentNode = name2Taxonomy[seqName];
468                 
469                 while (tree[currentNode].parent != -1) {
470                         tax = tree[currentNode].name + ";" + tax;
471                         currentNode = tree[currentNode].parent;
472                 }
473                 
474                 return tax;
475         }
476         catch(exception& e) {
477                 m->errorOut(e, "PhyloTree", "getFullTaxonomy");
478                 exit(1);
479         }
480 }
481 /**************************************************************************************************/
482
483 void PhyloTree::print(ofstream& out, vector<TaxNode>& copy){
484         try {
485         
486                 //output mothur version
487                 out << "#" << m->getVersion() << endl;
488                 
489                 out << copy.size() << endl;
490                 
491                 out << maxLevel << endl;
492                 
493                 for (int i = 0; i < copy.size(); i++) {
494         
495                         out << copy[i].level << '\t'<< copy[i].name << '\t' << copy[i].children.size() << '\t';
496                         
497                         map<string,int>::iterator it;
498                         for(it=copy[i].children.begin();it!=copy[i].children.end();it++){
499                                 out << it->first << '\t' << it->second << '\t';
500                         }
501                         out << endl;
502                 }
503                 
504                 out.close();
505         }
506         catch(exception& e) {
507                 m->errorOut(e, "PhyloTree", "print");
508                 exit(1);
509         }
510 }
511 /**************************************************************************************************/
512 void PhyloTree::printTreeNodes(string treefilename) {
513         try {
514         
515                 #ifdef USE_MPI
516                         int pid;
517                         MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
518
519                         if (pid == 0) {  
520                 
521                 #endif
522
523                         ofstream outTree;
524                         m->openOutputFile(treefilename, outTree);
525                         
526                         //output mothur version
527                         outTree << "#" << m->getVersion() << endl;
528                         
529                         //print treenodes
530                         outTree << tree.size() << endl;
531                         for (int i = 0; i < tree.size(); i++) {
532                                 outTree << tree[i].name << '\t' << tree[i].level << '\t' << tree[i].parent << endl;
533                         }
534                         
535                         //print genus nodes
536                         outTree << endl << uniqueTaxonomies.size() << endl;
537                         map<int, int>::iterator it2;
538                         for (it2=uniqueTaxonomies.begin(); it2!=uniqueTaxonomies.end(); it2++) {  outTree << it2->first << '\t' << tree[it2->first].accessions.size() << endl;  }
539                         outTree << endl;
540                         
541                         outTree.close();
542                 
543                 #ifdef USE_MPI
544                         }
545                 #endif
546
547                 
548         }
549         catch(exception& e) {
550                 m->errorOut(e, "PhyloTree", "printTreeNodes");
551                 exit(1);
552         }
553 }
554 /**************************************************************************************************/
555 TaxNode PhyloTree::get(int i ){
556         try {
557                 if (i < tree.size()) {  return tree[i];  }
558                 else {  cout << i << '\t' << tree.size() << endl ; m->mothurOut("Mismatch with taxonomy and template files. Cannot continue."); m->mothurOutEndLine(); exit(1); }
559         }
560         catch(exception& e) {
561                 m->errorOut(e, "PhyloTree", "get");
562                 exit(1);
563         }
564 }
565 /**************************************************************************************************/
566 TaxNode PhyloTree::get(string seqName){
567         try {
568                 map<string, int>::iterator itFind = name2Taxonomy.find(seqName);
569         
570                 if (itFind != name2Taxonomy.end()) {  return tree[name2Taxonomy[seqName]];  }
571                 else { m->mothurOut("Cannot find " + seqName + ". Mismatch with taxonomy and template files. Cannot continue."); m->mothurOutEndLine(); exit(1);}
572         }
573         catch(exception& e) {
574                 m->errorOut(e, "PhyloTree", "get");
575                 exit(1);
576         }
577 }
578 /**************************************************************************************************/
579 string PhyloTree::getName(int i ){
580         try {
581                 if (i < tree.size()) {  return tree[i].name;     }
582                 else { m->mothurOut("Mismatch with taxonomy and template files. Cannot continue."); m->mothurOutEndLine(); exit(1); }
583         }
584         catch(exception& e) {
585                 m->errorOut(e, "PhyloTree", "get");
586                 exit(1);
587         }
588 }
589 /**************************************************************************************************/
590 int PhyloTree::getIndex(string seqName){
591         try {
592                 map<string, int>::iterator itFind = name2Taxonomy.find(seqName);
593         
594                 if (itFind != name2Taxonomy.end()) {  return name2Taxonomy[seqName];  }
595                 else { m->mothurOut("Cannot find " + seqName + ". Mismatch with taxonomy and template files. Cannot continue."); m->mothurOutEndLine(); exit(1);}
596         }
597         catch(exception& e) {
598                 m->errorOut(e, "PhyloTree", "get");
599                 exit(1);
600         }
601 }
602 /**************************************************************************************************/
603 bool PhyloTree::ErrorCheck(vector<string> templateFileNames){
604         try {
605         
606                 bool okay = true;
607                 
608                 map<string, int>::iterator itFind;
609                 map<string, int> taxonomyFileNames = name2Taxonomy;
610                 
611                 for (int i = 0; i < templateFileNames.size(); i++) {
612                         itFind = taxonomyFileNames.find(templateFileNames[i]);
613                         
614                         if (itFind != taxonomyFileNames.end()) { //found it so erase it
615                                 taxonomyFileNames.erase(itFind);
616                         }else {
617                                 m->mothurOut(templateFileNames[i] + " is in your template file and is not in your taxonomy file. Please correct."); m->mothurOutEndLine();
618                                 okay = false;
619                         }
620                         
621                         //templateFileNames.erase(templateFileNames.begin()+i);
622                         //i--;
623                 }
624                 templateFileNames.clear();
625                 
626                 if (taxonomyFileNames.size() > 0) { //there are names in tax file that are not in template
627                         okay = false;
628                         
629                         for (itFind = taxonomyFileNames.begin(); itFind != taxonomyFileNames.end(); itFind++) {
630                                 m->mothurOut(itFind->first + " is in your taxonomy file and is not in your template file. Please correct."); m->mothurOutEndLine();
631                         }
632                 }
633                 
634                 return okay;
635         }
636         catch(exception& e) {
637                 m->errorOut(e, "PhyloTree", "ErrorCheck");
638                 exit(1);
639         }
640 }
641 /**************************************************************************************************/
642         
643
644
645