5 * Created by Pat Schloss on 6/17/09.
6 * Copyright 2009 Patrick D. Schloss. All rights reserved.
10 #include "phylotree.h"
12 /**************************************************************************************************/
14 PhyloTree::PhyloTree(){
16 m = MothurOut::getInstance();
19 tree.push_back(TaxNode("Root"));
20 tree[0].heirarchyID = "0";
25 m->errorOut(e, "PhyloTree", "PhyloTree");
29 /**************************************************************************************************/
31 PhyloTree::PhyloTree(ifstream& in, string filename){
33 m = MothurOut::getInstance();
43 char inFileName[1024];
44 strcpy(inFileName, filename.c_str());
46 MPI_File_open(MPI_COMM_WORLD, inFileName, MPI_MODE_RDONLY, MPI_INFO_NULL, &inMPI);
47 MPI_File_get_size(inMPI, &size);
49 char* buffer = new char[size];
50 MPI_File_read(inMPI, buffer, size, MPI_CHAR, &status);
52 string tempBuf = buffer;
53 if (tempBuf.length() > size) { tempBuf = tempBuf.substr(0, size); }
54 istringstream iss (tempBuf,istringstream::in);
57 iss >> numNodes; gobble(iss);
59 tree.resize(numNodes);
61 for (int i = 0; i < tree.size(); i++) {
62 iss >> tree[i].name >> tree[i].level >> tree[i].parent; gobble(iss);
67 iss >> numGenus; gobble(iss);
71 for (int i = 0; i < numGenus; i++) {
72 iss >> gnode >> gsize; gobble(iss);
74 uniqueTaxonomies[gnode] = gnode;
75 totals.push_back(gsize);
78 MPI_File_close(&inMPI);
81 in >> numNodes; gobble(in);
83 tree.resize(numNodes);
85 for (int i = 0; i < tree.size(); i++) {
86 in >> tree[i].name >> tree[i].level >> tree[i].parent; gobble(in);
91 in >> numGenus; gobble(in);
95 for (int i = 0; i < numGenus; i++) {
96 in >> gnode >> gsize; gobble(in);
98 uniqueTaxonomies[gnode] = gnode;
99 totals.push_back(gsize);
107 catch(exception& e) {
108 m->errorOut(e, "PhyloTree", "PhyloTree");
112 /**************************************************************************************************/
114 PhyloTree::PhyloTree(string tfile){
116 m = MothurOut::getInstance();
119 tree.push_back(TaxNode("Root"));
120 tree[0].heirarchyID = "0";
127 int pid, num, processors;
128 vector<unsigned long int> positions;
132 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
133 MPI_Comm_size(MPI_COMM_WORLD, &processors);
135 char inFileName[1024];
136 strcpy(inFileName, tfile.c_str());
138 MPI_File_open(MPI_COMM_WORLD, inFileName, MPI_MODE_RDONLY, MPI_INFO_NULL, &inMPI); //comm, filename, mode, info, filepointer
141 positions = setFilePosEachLine(tfile, num);
143 //send file positions to all processes
144 for(int i = 1; i < processors; i++) {
145 MPI_Send(&num, 1, MPI_INT, i, 2001, MPI_COMM_WORLD);
146 MPI_Send(&positions[0], (num+1), MPI_LONG, i, 2001, MPI_COMM_WORLD);
149 MPI_Recv(&num, 1, MPI_INT, 0, 2001, MPI_COMM_WORLD, &status);
150 positions.resize(num+1);
151 MPI_Recv(&positions[0], (num+1), MPI_LONG, 0, 2001, MPI_COMM_WORLD, &status);
155 for(int i=0;i<num;i++){
157 int length = positions[i+1] - positions[i];
158 char* buf4 = new char[length];
160 MPI_File_read_at(inMPI, positions[i], buf4, length, MPI_CHAR, &status);
162 string tempBuf = buf4;
163 if (tempBuf.length() > length) { tempBuf = tempBuf.substr(0, length); }
166 istringstream iss (tempBuf,istringstream::in);
168 addSeqToTree(name, tax);
171 MPI_File_close(&inMPI);
172 MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
176 openInputFile(tfile, in);
178 //read in users taxonomy file and add sequences to tree
180 in >> name >> tax; gobble(in);
182 addSeqToTree(name, tax);
187 assignHeirarchyIDs(0);
189 //create file for summary if needed
192 catch(exception& e) {
193 m->errorOut(e, "PhyloTree", "PhyloTree");
198 /**************************************************************************************************/
200 string PhyloTree::getNextTaxon(string& heirarchy, string seqname){
202 string currentLevel = "";
204 int pos = heirarchy.find_first_of(';');
206 if (pos == -1) { //you can't find another ;
207 currentLevel = heirarchy;
209 m->mothurOut(seqname + " is missing a ;, please check for other errors."); m->mothurOutEndLine();
211 currentLevel=heirarchy.substr(0,pos);
212 if (pos != (heirarchy.length()-1)) { heirarchy=heirarchy.substr(pos+1); }
213 else { heirarchy = ""; }
219 catch(exception& e) {
220 m->errorOut(e, "PhyloTree", "getNextTaxon");
225 /**************************************************************************************************/
227 int PhyloTree::addSeqToTree(string seqName, string seqTaxonomy){
232 map<string, int>::iterator childPointer;
237 tree[0].accessions.push_back(seqName);
238 string taxon;// = getNextTaxon(seqTaxonomy);
240 while(seqTaxonomy != ""){
244 if (m->control_pressed) { return 0; }
246 //somehow the parent is getting one too many accnos
247 //use print to reassign the taxa id
248 taxon = getNextTaxon(seqTaxonomy, seqName);
250 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; }
252 childPointer = tree[currentNode].children.find(taxon);
254 if(childPointer != tree[currentNode].children.end()){ //if the node already exists, move on
255 currentNode = childPointer->second;
256 tree[currentNode].accessions.push_back(seqName);
257 name2Taxonomy[seqName] = currentNode;
259 else{ //otherwise, create it
260 tree.push_back(TaxNode(taxon));
262 tree[currentNode].children[taxon] = numNodes-1;
263 tree[numNodes-1].parent = currentNode;
265 currentNode = tree[currentNode].children[taxon];
266 tree[currentNode].accessions.push_back(seqName);
267 name2Taxonomy[seqName] = currentNode;
270 if (seqTaxonomy == "") { uniqueTaxonomies[currentNode] = currentNode; }
273 catch(exception& e) {
274 m->errorOut(e, "PhyloTree", "addSeqToTree");
278 /**************************************************************************************************/
279 vector<int> PhyloTree::getGenusNodes() {
282 //generate genusIndexes
283 map<int, int>::iterator it2;
284 for (it2=uniqueTaxonomies.begin(); it2!=uniqueTaxonomies.end(); it2++) { genusIndex.push_back(it2->first); }
288 catch(exception& e) {
289 m->errorOut(e, "PhyloTree", "getGenusNodes");
293 /**************************************************************************************************/
294 vector<int> PhyloTree::getGenusTotals() {
299 //reset counts because we are on a new word
300 for (int j = 0; j < genusIndex.size(); j++) {
301 totals.push_back(tree[genusIndex[j]].accessions.size());
309 catch(exception& e) {
310 m->errorOut(e, "PhyloTree", "getGenusNodes");
314 /**************************************************************************************************/
316 void PhyloTree::assignHeirarchyIDs(int index){
318 map<string,int>::iterator it;
321 for(it=tree[index].children.begin();it!=tree[index].children.end();it++){
322 tree[it->second].heirarchyID = tree[index].heirarchyID + '.' + toString(counter);
324 tree[it->second].level = tree[index].level + 1;
326 //save maxLevel for binning the unclassified seqs
327 if (tree[it->second].level > maxLevel) { maxLevel = tree[it->second].level; }
329 assignHeirarchyIDs(it->second);
332 catch(exception& e) {
333 m->errorOut(e, "PhyloTree", "assignHeirarchyIDs");
337 /**************************************************************************************************/
338 void PhyloTree::setUp(string tfile){
340 string taxFileNameTest = tfile.substr(0,tfile.find_last_of(".")+1) + "tree.sum";
344 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
346 if (pid == 0) { binUnclassified(taxFileNameTest); }
349 binUnclassified(taxFileNameTest);
352 catch(exception& e) {
353 m->errorOut(e, "PhyloTree", "setUp");
357 /**************************************************************************************************/
358 void PhyloTree::binUnclassified(string file){
362 openOutputFile(file, out);
364 map<string, int>::iterator itBin;
365 map<string, int>::iterator childPointer;
367 vector<TaxNode> copy = tree;
370 fillOutTree(0, copy);
372 //get leaf nodes that may need extension
373 for (int i = 0; i < copy.size(); i++) {
375 if (copy[i].children.size() == 0) {
380 int copyNodes = copy.size();
382 //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
383 map<int, int>::iterator itLeaf;
384 for (itLeaf = leafNodes.begin(); itLeaf != leafNodes.end(); itLeaf++) {
386 if (m->control_pressed) { out.close(); break; }
388 int level = copy[itLeaf->second].level;
389 int currentNode = itLeaf->second;
391 //this sequence is unclassified at some levels
392 while(level < maxLevel){
396 string taxon = "unclassified";
398 //does the parent have a child names 'unclassified'?
399 childPointer = copy[currentNode].children.find(taxon);
401 if(childPointer != copy[currentNode].children.end()){ //if the node already exists, move on
402 currentNode = childPointer->second; //currentNode becomes 'unclassified'
404 else{ //otherwise, create it
405 copy.push_back(TaxNode(taxon));
407 copy[currentNode].children[taxon] = copyNodes-1;
408 copy[copyNodes-1].parent = currentNode;
409 copy[copyNodes-1].level = copy[currentNode].level + 1;
411 currentNode = copy[currentNode].children[taxon];
416 if (!m->control_pressed) {
422 catch(exception& e) {
423 m->errorOut(e, "PhyloTree", "binUnclassified");
427 /**************************************************************************************************/
428 void PhyloTree::fillOutTree(int index, vector<TaxNode>& copy) {
431 map<string,int>::iterator it;
433 it = copy[index].children.find("unclassified");
434 if (it == copy[index].children.end()) { //no unclassified at this level
435 string taxon = "unclassified";
436 copy.push_back(TaxNode(taxon));
437 copy[index].children[taxon] = copy.size()-1;
438 copy[copy.size()-1].parent = index;
439 copy[copy.size()-1].level = copy[index].level + 1;
442 if (tree[index].level < maxLevel) {
443 for(it=tree[index].children.begin();it!=tree[index].children.end();it++){ //check your children
444 fillOutTree(it->second, copy);
449 catch(exception& e) {
450 m->errorOut(e, "PhyloTree", "fillOutTree");
454 /**************************************************************************************************/
455 string PhyloTree::getFullTaxonomy(string seqName) {
459 int currentNode = name2Taxonomy[seqName];
461 while (tree[currentNode].parent != -1) {
462 tax = tree[currentNode].name + ";" + tax;
463 currentNode = tree[currentNode].parent;
468 catch(exception& e) {
469 m->errorOut(e, "PhyloTree", "getFullTaxonomy");
473 /**************************************************************************************************/
475 void PhyloTree::print(ofstream& out, vector<TaxNode>& copy){
477 out << copy.size() << endl;
479 out << maxLevel << endl;
481 for (int i = 0; i < copy.size(); i++) {
483 out << copy[i].level << '\t'<< copy[i].name << '\t' << copy[i].children.size() << '\t';
485 map<string,int>::iterator it;
486 for(it=copy[i].children.begin();it!=copy[i].children.end();it++){
487 out << it->first << '\t' << it->second << '\t';
494 catch(exception& e) {
495 m->errorOut(e, "PhyloTree", "print");
499 /**************************************************************************************************/
500 void PhyloTree::printTreeNodes(string treefilename) {
505 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
512 openOutputFile(treefilename, outTree);
515 outTree << tree.size() << endl;
516 for (int i = 0; i < tree.size(); i++) {
517 outTree << tree[i].name << '\t' << tree[i].level << '\t' << tree[i].parent << endl;
521 outTree << endl << uniqueTaxonomies.size() << endl;
522 map<int, int>::iterator it2;
523 for (it2=uniqueTaxonomies.begin(); it2!=uniqueTaxonomies.end(); it2++) { outTree << it2->first << '\t' << tree[it2->first].accessions.size() << endl; }
534 catch(exception& e) {
535 m->errorOut(e, "PhyloTree", "printTreeNodes");
539 /**************************************************************************************************/
540 TaxNode PhyloTree::get(int i ){
542 if (i < tree.size()) { return tree[i]; }
543 else { cout << i << '\t' << tree.size() << endl ; m->mothurOut("Mismatch with taxonomy and template files. Cannot continue."); m->mothurOutEndLine(); exit(1); }
545 catch(exception& e) {
546 m->errorOut(e, "PhyloTree", "get");
550 /**************************************************************************************************/
551 TaxNode PhyloTree::get(string seqName){
553 map<string, int>::iterator itFind = name2Taxonomy.find(seqName);
555 if (itFind != name2Taxonomy.end()) { return tree[name2Taxonomy[seqName]]; }
556 else { m->mothurOut("Cannot find " + seqName + ". Mismatch with taxonomy and template files. Cannot continue."); m->mothurOutEndLine(); exit(1);}
558 catch(exception& e) {
559 m->errorOut(e, "PhyloTree", "get");
563 /**************************************************************************************************/
564 string PhyloTree::getName(int i ){
566 if (i < tree.size()) { return tree[i].name; }
567 else { m->mothurOut("Mismatch with taxonomy and template files. Cannot continue."); m->mothurOutEndLine(); exit(1); }
569 catch(exception& e) {
570 m->errorOut(e, "PhyloTree", "get");
574 /**************************************************************************************************/
575 int PhyloTree::getIndex(string seqName){
577 map<string, int>::iterator itFind = name2Taxonomy.find(seqName);
579 if (itFind != name2Taxonomy.end()) { return name2Taxonomy[seqName]; }
580 else { m->mothurOut("Cannot find " + seqName + ". Mismatch with taxonomy and template files. Cannot continue."); m->mothurOutEndLine(); exit(1);}
582 catch(exception& e) {
583 m->errorOut(e, "PhyloTree", "get");
587 /**************************************************************************************************/
588 bool PhyloTree::ErrorCheck(vector<string> templateFileNames){
593 map<string, int>::iterator itFind;
594 map<string, int> taxonomyFileNames = name2Taxonomy;
596 for (int i = 0; i < templateFileNames.size(); i++) {
597 itFind = taxonomyFileNames.find(templateFileNames[i]);
599 if (itFind != taxonomyFileNames.end()) { //found it so erase it
600 taxonomyFileNames.erase(itFind);
602 m->mothurOut(templateFileNames[i] + " is in your template file and is not in your taxonomy file. Please correct."); m->mothurOutEndLine();
606 templateFileNames.erase(templateFileNames.begin()+i);
610 if (taxonomyFileNames.size() > 0) { //there are names in tax file that are not in template
613 for (itFind = taxonomyFileNames.begin(); itFind != taxonomyFileNames.end(); itFind++) {
614 m->mothurOut(itFind->first + " is in your taxonomy file and is not in your template file. Please correct."); m->mothurOutEndLine();
620 catch(exception& e) {
621 m->errorOut(e, "PhyloTree", "ErrorCheck");
625 /**************************************************************************************************/