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 long> 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 m->removeConfidences(seqTaxonomy);
246 string taxon;// = getNextTaxon(seqTaxonomy);
248 while(seqTaxonomy != ""){
252 if (m->control_pressed) { return 0; }
254 //somehow the parent is getting one too many accnos
255 //use print to reassign the taxa id
256 taxon = getNextTaxon(seqTaxonomy, seqName);
258 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; }
260 childPointer = tree[currentNode].children.find(taxon);
262 if(childPointer != tree[currentNode].children.end()){ //if the node already exists, move on
263 currentNode = childPointer->second;
264 tree[currentNode].accessions.push_back(seqName);
265 name2Taxonomy[seqName] = currentNode;
267 else{ //otherwise, create it
268 tree.push_back(TaxNode(taxon));
270 tree[currentNode].children[taxon] = numNodes-1;
271 tree[numNodes-1].parent = currentNode;
273 currentNode = tree[currentNode].children[taxon];
274 tree[currentNode].accessions.push_back(seqName);
275 name2Taxonomy[seqName] = currentNode;
278 if (seqTaxonomy == "") { uniqueTaxonomies[currentNode] = currentNode; }
283 catch(exception& e) {
284 m->errorOut(e, "PhyloTree", "addSeqToTree");
288 /**************************************************************************************************/
289 vector<int> PhyloTree::getGenusNodes() {
292 //generate genusIndexes
293 map<int, int>::iterator it2;
294 for (it2=uniqueTaxonomies.begin(); it2!=uniqueTaxonomies.end(); it2++) { genusIndex.push_back(it2->first); }
298 catch(exception& e) {
299 m->errorOut(e, "PhyloTree", "getGenusNodes");
303 /**************************************************************************************************/
304 vector<int> PhyloTree::getGenusTotals() {
309 //reset counts because we are on a new word
310 for (int j = 0; j < genusIndex.size(); j++) {
311 totals.push_back(tree[genusIndex[j]].accessions.size());
319 catch(exception& e) {
320 m->errorOut(e, "PhyloTree", "getGenusNodes");
324 /**************************************************************************************************/
326 void PhyloTree::assignHeirarchyIDs(int index){
328 map<string,int>::iterator it;
331 for(it=tree[index].children.begin();it!=tree[index].children.end();it++){
332 tree[it->second].heirarchyID = tree[index].heirarchyID + '.' + toString(counter);
334 tree[it->second].level = tree[index].level + 1;
336 //save maxLevel for binning the unclassified seqs
337 if (tree[it->second].level > maxLevel) { maxLevel = tree[it->second].level; }
339 assignHeirarchyIDs(it->second);
342 catch(exception& e) {
343 m->errorOut(e, "PhyloTree", "assignHeirarchyIDs");
347 /**************************************************************************************************/
348 void PhyloTree::setUp(string tfile){
350 string taxFileNameTest = tfile.substr(0,tfile.find_last_of(".")+1) + "tree.sum";
354 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
356 if (pid == 0) { binUnclassified(taxFileNameTest); }
359 binUnclassified(taxFileNameTest);
362 catch(exception& e) {
363 m->errorOut(e, "PhyloTree", "setUp");
367 /**************************************************************************************************/
368 void PhyloTree::binUnclassified(string file){
372 m->openOutputFile(file, out);
374 map<string, int>::iterator itBin;
375 map<string, int>::iterator childPointer;
377 vector<TaxNode> copy = tree;
380 fillOutTree(0, copy);
382 //get leaf nodes that may need extension
383 for (int i = 0; i < copy.size(); i++) {
385 if (copy[i].children.size() == 0) {
390 int copyNodes = copy.size();
392 //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
393 map<int, int>::iterator itLeaf;
394 for (itLeaf = leafNodes.begin(); itLeaf != leafNodes.end(); itLeaf++) {
396 if (m->control_pressed) { out.close(); break; }
398 int level = copy[itLeaf->second].level;
399 int currentNode = itLeaf->second;
401 //this sequence is unclassified at some levels
402 while(level < maxLevel){
406 string taxon = "unclassified";
408 //does the parent have a child names 'unclassified'?
409 childPointer = copy[currentNode].children.find(taxon);
411 if(childPointer != copy[currentNode].children.end()){ //if the node already exists, move on
412 currentNode = childPointer->second; //currentNode becomes 'unclassified'
414 else{ //otherwise, create it
415 copy.push_back(TaxNode(taxon));
417 copy[currentNode].children[taxon] = copyNodes-1;
418 copy[copyNodes-1].parent = currentNode;
419 copy[copyNodes-1].level = copy[currentNode].level + 1;
421 currentNode = copy[currentNode].children[taxon];
426 if (!m->control_pressed) {
432 catch(exception& e) {
433 m->errorOut(e, "PhyloTree", "binUnclassified");
437 /**************************************************************************************************/
438 void PhyloTree::fillOutTree(int index, vector<TaxNode>& copy) {
441 map<string,int>::iterator it;
443 it = copy[index].children.find("unclassified");
444 if (it == copy[index].children.end()) { //no unclassified at this level
445 string taxon = "unclassified";
446 copy.push_back(TaxNode(taxon));
447 copy[index].children[taxon] = copy.size()-1;
448 copy[copy.size()-1].parent = index;
449 copy[copy.size()-1].level = copy[index].level + 1;
452 if (tree[index].level < maxLevel) {
453 for(it=tree[index].children.begin();it!=tree[index].children.end();it++){ //check your children
454 fillOutTree(it->second, copy);
459 catch(exception& e) {
460 m->errorOut(e, "PhyloTree", "fillOutTree");
464 /**************************************************************************************************/
465 string PhyloTree::getFullTaxonomy(string seqName) {
469 int currentNode = name2Taxonomy[seqName];
471 while (tree[currentNode].parent != -1) {
472 tax = tree[currentNode].name + ";" + tax;
473 currentNode = tree[currentNode].parent;
478 catch(exception& e) {
479 m->errorOut(e, "PhyloTree", "getFullTaxonomy");
483 /**************************************************************************************************/
485 void PhyloTree::print(ofstream& out, vector<TaxNode>& copy){
488 //output mothur version
489 out << "#" << m->getVersion() << endl;
491 out << copy.size() << endl;
493 out << maxLevel << endl;
495 for (int i = 0; i < copy.size(); i++) {
497 out << copy[i].level << '\t'<< copy[i].name << '\t' << copy[i].children.size() << '\t';
499 map<string,int>::iterator it;
500 for(it=copy[i].children.begin();it!=copy[i].children.end();it++){
501 out << it->first << '\t' << it->second << '\t';
508 catch(exception& e) {
509 m->errorOut(e, "PhyloTree", "print");
513 /**************************************************************************************************/
514 void PhyloTree::printTreeNodes(string treefilename) {
519 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
526 m->openOutputFile(treefilename, outTree);
528 //output mothur version
529 outTree << "#" << m->getVersion() << endl;
532 outTree << tree.size() << endl;
533 for (int i = 0; i < tree.size(); i++) {
534 outTree << tree[i].name << '\t' << tree[i].level << '\t' << tree[i].parent << endl;
538 outTree << endl << uniqueTaxonomies.size() << endl;
539 map<int, int>::iterator it2;
540 for (it2=uniqueTaxonomies.begin(); it2!=uniqueTaxonomies.end(); it2++) { outTree << it2->first << '\t' << tree[it2->first].accessions.size() << endl; }
551 catch(exception& e) {
552 m->errorOut(e, "PhyloTree", "printTreeNodes");
556 /**************************************************************************************************/
557 TaxNode PhyloTree::get(int i ){
559 if (i < tree.size()) { return tree[i]; }
560 else { cout << i << '\t' << tree.size() << endl ; m->mothurOut("Mismatch with taxonomy and template files. Cannot continue."); m->mothurOutEndLine(); exit(1); }
562 catch(exception& e) {
563 m->errorOut(e, "PhyloTree", "get");
567 /**************************************************************************************************/
568 TaxNode PhyloTree::get(string seqName){
570 map<string, int>::iterator itFind = name2Taxonomy.find(seqName);
572 if (itFind != name2Taxonomy.end()) { return tree[name2Taxonomy[seqName]]; }
573 else { m->mothurOut("Cannot find " + seqName + ". Mismatch with taxonomy and template files. Cannot continue."); m->mothurOutEndLine(); exit(1);}
575 catch(exception& e) {
576 m->errorOut(e, "PhyloTree", "get");
580 /**************************************************************************************************/
581 string PhyloTree::getName(int i ){
583 if (i < tree.size()) { return tree[i].name; }
584 else { m->mothurOut("Mismatch with taxonomy and template files. Cannot continue."); m->mothurOutEndLine(); exit(1); }
586 catch(exception& e) {
587 m->errorOut(e, "PhyloTree", "get");
591 /**************************************************************************************************/
592 int PhyloTree::getIndex(string seqName){
594 map<string, int>::iterator itFind = name2Taxonomy.find(seqName);
596 if (itFind != name2Taxonomy.end()) { return name2Taxonomy[seqName]; }
597 else { m->mothurOut("Cannot find " + seqName + ". Mismatch with taxonomy and template files. Cannot continue."); m->mothurOutEndLine(); exit(1);}
599 catch(exception& e) {
600 m->errorOut(e, "PhyloTree", "get");
604 /**************************************************************************************************/
605 bool PhyloTree::ErrorCheck(vector<string> templateFileNames){
610 map<string, int>::iterator itFind;
611 map<string, int> taxonomyFileNames = name2Taxonomy;
613 for (int i = 0; i < templateFileNames.size(); i++) {
614 itFind = taxonomyFileNames.find(templateFileNames[i]);
616 if (itFind != taxonomyFileNames.end()) { //found it so erase it
617 taxonomyFileNames.erase(itFind);
619 m->mothurOut(templateFileNames[i] + " is in your template file and is not in your taxonomy file. Please correct."); m->mothurOutEndLine();
623 //templateFileNames.erase(templateFileNames.begin()+i);
626 templateFileNames.clear();
628 if (taxonomyFileNames.size() > 0) { //there are names in tax file that are not in template
631 for (itFind = taxonomyFileNames.begin(); itFind != taxonomyFileNames.end(); itFind++) {
632 m->mothurOut(itFind->first + " is in your taxonomy file and is not in your template file. Please correct."); m->mothurOutEndLine();
638 catch(exception& e) {
639 m->errorOut(e, "PhyloTree", "ErrorCheck");
643 /**************************************************************************************************/