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);
58 m->getline(iss); m->gobble(iss);
60 iss >> numNodes; m->gobble(iss);
62 tree.resize(numNodes);
64 for (int i = 0; i < tree.size(); i++) {
65 iss >> tree[i].name >> tree[i].level >> tree[i].parent; m->gobble(iss);
70 iss >> numGenus; m->gobble(iss);
74 for (int i = 0; i < numGenus; i++) {
75 iss >> gnode >> gsize; m->gobble(iss);
77 uniqueTaxonomies[gnode] = gnode;
78 totals.push_back(gsize);
81 MPI_File_close(&inMPI);
85 string line = m->getline(in); m->gobble(in);
87 in >> numNodes; m->gobble(in);
89 tree.resize(numNodes);
91 for (int i = 0; i < tree.size(); i++) {
92 in >> tree[i].name >> tree[i].level >> tree[i].parent; m->gobble(in);
97 in >> numGenus; m->gobble(in);
101 for (int i = 0; i < numGenus; i++) {
102 in >> gnode >> gsize; m->gobble(in);
104 uniqueTaxonomies[gnode] = gnode;
105 totals.push_back(gsize);
113 catch(exception& e) {
114 m->errorOut(e, "PhyloTree", "PhyloTree");
118 /**************************************************************************************************/
120 PhyloTree::PhyloTree(string tfile){
122 m = MothurOut::getInstance();
125 tree.push_back(TaxNode("Root"));
126 tree[0].heirarchyID = "0";
133 int pid, num, processors;
134 vector<unsigned long int> positions;
138 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
139 MPI_Comm_size(MPI_COMM_WORLD, &processors);
141 char inFileName[1024];
142 strcpy(inFileName, tfile.c_str());
144 MPI_File_open(MPI_COMM_WORLD, inFileName, MPI_MODE_RDONLY, MPI_INFO_NULL, &inMPI); //comm, filename, mode, info, filepointer
147 positions = m->setFilePosEachLine(tfile, num);
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);
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);
161 for(int i=0;i<num;i++){
163 int length = positions[i+1] - positions[i];
164 char* buf4 = new char[length];
166 MPI_File_read_at(inMPI, positions[i], buf4, length, MPI_CHAR, &status);
168 string tempBuf = buf4;
169 if (tempBuf.length() > length) { tempBuf = tempBuf.substr(0, length); }
172 istringstream iss (tempBuf,istringstream::in);
174 addSeqToTree(name, tax);
177 MPI_File_close(&inMPI);
178 MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
182 m->openInputFile(tfile, in);
184 //read in users taxonomy file and add sequences to tree
186 in >> name >> tax; m->gobble(in);
188 addSeqToTree(name, tax);
193 assignHeirarchyIDs(0);
195 //create file for summary if needed
198 catch(exception& e) {
199 m->errorOut(e, "PhyloTree", "PhyloTree");
204 /**************************************************************************************************/
206 string PhyloTree::getNextTaxon(string& heirarchy, string seqname){
208 string currentLevel = "";
210 int pos = heirarchy.find_first_of(';');
212 if (pos == -1) { //you can't find another ;
213 currentLevel = heirarchy;
215 m->mothurOut(seqname + " is missing a ;, please check for other errors."); m->mothurOutEndLine();
217 currentLevel=heirarchy.substr(0,pos);
218 if (pos != (heirarchy.length()-1)) { heirarchy=heirarchy.substr(pos+1); }
219 else { heirarchy = ""; }
225 catch(exception& e) {
226 m->errorOut(e, "PhyloTree", "getNextTaxon");
231 /**************************************************************************************************/
233 int PhyloTree::addSeqToTree(string seqName, string seqTaxonomy){
238 map<string, int>::iterator childPointer;
243 tree[0].accessions.push_back(seqName);
244 string taxon;// = getNextTaxon(seqTaxonomy);
246 while(seqTaxonomy != ""){
250 if (m->control_pressed) { return 0; }
252 //somehow the parent is getting one too many accnos
253 //use print to reassign the taxa id
254 taxon = getNextTaxon(seqTaxonomy, seqName);
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; }
258 childPointer = tree[currentNode].children.find(taxon);
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;
265 else{ //otherwise, create it
266 tree.push_back(TaxNode(taxon));
268 tree[currentNode].children[taxon] = numNodes-1;
269 tree[numNodes-1].parent = currentNode;
271 currentNode = tree[currentNode].children[taxon];
272 tree[currentNode].accessions.push_back(seqName);
273 name2Taxonomy[seqName] = currentNode;
276 if (seqTaxonomy == "") { uniqueTaxonomies[currentNode] = currentNode; }
279 catch(exception& e) {
280 m->errorOut(e, "PhyloTree", "addSeqToTree");
284 /**************************************************************************************************/
285 vector<int> PhyloTree::getGenusNodes() {
288 //generate genusIndexes
289 map<int, int>::iterator it2;
290 for (it2=uniqueTaxonomies.begin(); it2!=uniqueTaxonomies.end(); it2++) { genusIndex.push_back(it2->first); }
294 catch(exception& e) {
295 m->errorOut(e, "PhyloTree", "getGenusNodes");
299 /**************************************************************************************************/
300 vector<int> PhyloTree::getGenusTotals() {
305 //reset counts because we are on a new word
306 for (int j = 0; j < genusIndex.size(); j++) {
307 totals.push_back(tree[genusIndex[j]].accessions.size());
315 catch(exception& e) {
316 m->errorOut(e, "PhyloTree", "getGenusNodes");
320 /**************************************************************************************************/
322 void PhyloTree::assignHeirarchyIDs(int index){
324 map<string,int>::iterator it;
327 for(it=tree[index].children.begin();it!=tree[index].children.end();it++){
328 tree[it->second].heirarchyID = tree[index].heirarchyID + '.' + toString(counter);
330 tree[it->second].level = tree[index].level + 1;
332 //save maxLevel for binning the unclassified seqs
333 if (tree[it->second].level > maxLevel) { maxLevel = tree[it->second].level; }
335 assignHeirarchyIDs(it->second);
338 catch(exception& e) {
339 m->errorOut(e, "PhyloTree", "assignHeirarchyIDs");
343 /**************************************************************************************************/
344 void PhyloTree::setUp(string tfile){
346 string taxFileNameTest = tfile.substr(0,tfile.find_last_of(".")+1) + "tree.sum";
350 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
352 if (pid == 0) { binUnclassified(taxFileNameTest); }
355 binUnclassified(taxFileNameTest);
358 catch(exception& e) {
359 m->errorOut(e, "PhyloTree", "setUp");
363 /**************************************************************************************************/
364 void PhyloTree::binUnclassified(string file){
368 m->openOutputFile(file, out);
370 map<string, int>::iterator itBin;
371 map<string, int>::iterator childPointer;
373 vector<TaxNode> copy = tree;
376 fillOutTree(0, copy);
378 //get leaf nodes that may need extension
379 for (int i = 0; i < copy.size(); i++) {
381 if (copy[i].children.size() == 0) {
386 int copyNodes = copy.size();
388 //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
389 map<int, int>::iterator itLeaf;
390 for (itLeaf = leafNodes.begin(); itLeaf != leafNodes.end(); itLeaf++) {
392 if (m->control_pressed) { out.close(); break; }
394 int level = copy[itLeaf->second].level;
395 int currentNode = itLeaf->second;
397 //this sequence is unclassified at some levels
398 while(level < maxLevel){
402 string taxon = "unclassified";
404 //does the parent have a child names 'unclassified'?
405 childPointer = copy[currentNode].children.find(taxon);
407 if(childPointer != copy[currentNode].children.end()){ //if the node already exists, move on
408 currentNode = childPointer->second; //currentNode becomes 'unclassified'
410 else{ //otherwise, create it
411 copy.push_back(TaxNode(taxon));
413 copy[currentNode].children[taxon] = copyNodes-1;
414 copy[copyNodes-1].parent = currentNode;
415 copy[copyNodes-1].level = copy[currentNode].level + 1;
417 currentNode = copy[currentNode].children[taxon];
422 if (!m->control_pressed) {
428 catch(exception& e) {
429 m->errorOut(e, "PhyloTree", "binUnclassified");
433 /**************************************************************************************************/
434 void PhyloTree::fillOutTree(int index, vector<TaxNode>& copy) {
437 map<string,int>::iterator it;
439 it = copy[index].children.find("unclassified");
440 if (it == copy[index].children.end()) { //no unclassified at this level
441 string taxon = "unclassified";
442 copy.push_back(TaxNode(taxon));
443 copy[index].children[taxon] = copy.size()-1;
444 copy[copy.size()-1].parent = index;
445 copy[copy.size()-1].level = copy[index].level + 1;
448 if (tree[index].level < maxLevel) {
449 for(it=tree[index].children.begin();it!=tree[index].children.end();it++){ //check your children
450 fillOutTree(it->second, copy);
455 catch(exception& e) {
456 m->errorOut(e, "PhyloTree", "fillOutTree");
460 /**************************************************************************************************/
461 string PhyloTree::getFullTaxonomy(string seqName) {
465 int currentNode = name2Taxonomy[seqName];
467 while (tree[currentNode].parent != -1) {
468 tax = tree[currentNode].name + ";" + tax;
469 currentNode = tree[currentNode].parent;
474 catch(exception& e) {
475 m->errorOut(e, "PhyloTree", "getFullTaxonomy");
479 /**************************************************************************************************/
481 void PhyloTree::print(ofstream& out, vector<TaxNode>& copy){
484 //output mothur version
485 out << "#" << m->getVersion() << endl;
487 out << copy.size() << endl;
489 out << maxLevel << endl;
491 for (int i = 0; i < copy.size(); i++) {
493 out << copy[i].level << '\t'<< copy[i].name << '\t' << copy[i].children.size() << '\t';
495 map<string,int>::iterator it;
496 for(it=copy[i].children.begin();it!=copy[i].children.end();it++){
497 out << it->first << '\t' << it->second << '\t';
504 catch(exception& e) {
505 m->errorOut(e, "PhyloTree", "print");
509 /**************************************************************************************************/
510 void PhyloTree::printTreeNodes(string treefilename) {
515 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
522 m->openOutputFile(treefilename, outTree);
524 //output mothur version
525 outTree << "#" << m->getVersion() << endl;
528 outTree << tree.size() << endl;
529 for (int i = 0; i < tree.size(); i++) {
530 outTree << tree[i].name << '\t' << tree[i].level << '\t' << tree[i].parent << endl;
534 outTree << endl << uniqueTaxonomies.size() << endl;
535 map<int, int>::iterator it2;
536 for (it2=uniqueTaxonomies.begin(); it2!=uniqueTaxonomies.end(); it2++) { outTree << it2->first << '\t' << tree[it2->first].accessions.size() << endl; }
547 catch(exception& e) {
548 m->errorOut(e, "PhyloTree", "printTreeNodes");
552 /**************************************************************************************************/
553 TaxNode PhyloTree::get(int i ){
555 if (i < tree.size()) { return tree[i]; }
556 else { cout << i << '\t' << tree.size() << endl ; m->mothurOut("Mismatch with taxonomy and template files. Cannot continue."); m->mothurOutEndLine(); exit(1); }
558 catch(exception& e) {
559 m->errorOut(e, "PhyloTree", "get");
563 /**************************************************************************************************/
564 TaxNode PhyloTree::get(string seqName){
566 map<string, int>::iterator itFind = name2Taxonomy.find(seqName);
568 if (itFind != name2Taxonomy.end()) { return tree[name2Taxonomy[seqName]]; }
569 else { m->mothurOut("Cannot find " + seqName + ". Mismatch with taxonomy and template files. Cannot continue."); m->mothurOutEndLine(); exit(1);}
571 catch(exception& e) {
572 m->errorOut(e, "PhyloTree", "get");
576 /**************************************************************************************************/
577 string PhyloTree::getName(int i ){
579 if (i < tree.size()) { return tree[i].name; }
580 else { m->mothurOut("Mismatch with taxonomy and template files. Cannot continue."); m->mothurOutEndLine(); exit(1); }
582 catch(exception& e) {
583 m->errorOut(e, "PhyloTree", "get");
587 /**************************************************************************************************/
588 int PhyloTree::getIndex(string seqName){
590 map<string, int>::iterator itFind = name2Taxonomy.find(seqName);
592 if (itFind != name2Taxonomy.end()) { return name2Taxonomy[seqName]; }
593 else { m->mothurOut("Cannot find " + seqName + ". Mismatch with taxonomy and template files. Cannot continue."); m->mothurOutEndLine(); exit(1);}
595 catch(exception& e) {
596 m->errorOut(e, "PhyloTree", "get");
600 /**************************************************************************************************/
601 bool PhyloTree::ErrorCheck(vector<string> templateFileNames){
606 map<string, int>::iterator itFind;
607 map<string, int> taxonomyFileNames = name2Taxonomy;
609 for (int i = 0; i < templateFileNames.size(); i++) {
610 itFind = taxonomyFileNames.find(templateFileNames[i]);
612 if (itFind != taxonomyFileNames.end()) { //found it so erase it
613 taxonomyFileNames.erase(itFind);
615 m->mothurOut(templateFileNames[i] + " is in your template file and is not in your taxonomy file. Please correct."); m->mothurOutEndLine();
619 templateFileNames.erase(templateFileNames.begin()+i);
623 if (taxonomyFileNames.size() > 0) { //there are names in tax file that are not in template
626 for (itFind = taxonomyFileNames.begin(); itFind != taxonomyFileNames.end(); itFind++) {
627 m->mothurOut(itFind->first + " is in your taxonomy file and is not in your template file. Please correct."); m->mothurOutEndLine();
633 catch(exception& e) {
634 m->errorOut(e, "PhyloTree", "ErrorCheck");
638 /**************************************************************************************************/