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();
41 char inFileName[1024];
42 strcpy(inFileName, filename.c_str());
44 MPI_File_open(MPI_COMM_WORLD, inFileName, MPI_MODE_RDONLY, MPI_INFO_NULL, &inMPI);
45 MPI_File_get_size(inMPI, &size);
47 char* buffer = new char[size];
48 MPI_File_read(inMPI, buffer, size, MPI_CHAR, &status);
50 string tempBuf = buffer;
51 if (tempBuf.length() > size) { tempBuf = tempBuf.substr(0, size); }
52 istringstream iss (tempBuf,istringstream::in);
55 iss >> numNodes; gobble(iss);
57 tree.resize(numNodes);
59 for (int i = 0; i < tree.size(); i++) {
60 iss >> tree[i].name >> tree[i].level >> tree[i].parent; gobble(iss);
65 iss >> numGenus; gobble(iss);
69 for (int i = 0; i < numGenus; i++) {
70 iss >> gnode >> gsize; gobble(iss);
72 uniqueTaxonomies[gnode] = gnode;
73 totals.push_back(gsize);
76 MPI_File_close(&inMPI);
79 in >> numNodes; gobble(in);
81 tree.resize(numNodes);
83 for (int i = 0; i < tree.size(); i++) {
84 in >> tree[i].name >> tree[i].level >> tree[i].parent; gobble(in);
89 in >> numGenus; gobble(in);
93 for (int i = 0; i < numGenus; i++) {
94 in >> gnode >> gsize; gobble(in);
96 uniqueTaxonomies[gnode] = gnode;
97 totals.push_back(gsize);
105 catch(exception& e) {
106 m->errorOut(e, "PhyloTree", "PhyloTree");
110 /**************************************************************************************************/
112 PhyloTree::PhyloTree(string tfile){
114 m = MothurOut::getInstance();
117 tree.push_back(TaxNode("Root"));
118 tree[0].heirarchyID = "0";
126 vector<long> positions;
130 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
132 char inFileName[1024];
133 strcpy(inFileName, tfile.c_str());
135 MPI_File_open(MPI_COMM_WORLD, inFileName, MPI_MODE_RDONLY, MPI_INFO_NULL, &inMPI); //comm, filename, mode, info, filepointer
138 positions = setFilePosEachLine(tfile, num);
140 //send file positions to all processes
141 MPI_Bcast(&num, 1, MPI_INT, 0, MPI_COMM_WORLD); //send numSeqs
142 MPI_Bcast(&positions[0], (num+1), MPI_LONG, 0, MPI_COMM_WORLD); //send file pos
144 MPI_Bcast(&num, 1, MPI_INT, 0, MPI_COMM_WORLD); //get numSeqs
145 positions.resize(num);
146 MPI_Bcast(&positions[0], (num+1), MPI_LONG, 0, MPI_COMM_WORLD); //get file positions
150 for(int i=0;i<num;i++){
152 int length = positions[i+1] - positions[i];
153 char* buf4 = new char[length];
155 MPI_File_read_at(inMPI, positions[i], buf4, length, MPI_CHAR, &status);
157 string tempBuf = buf4;
158 if (tempBuf.length() > length) { tempBuf = tempBuf.substr(0, length); }
161 istringstream iss (tempBuf,istringstream::in);
163 addSeqToTree(name, tax);
166 MPI_File_close(&inMPI);
170 openInputFile(tfile, in);
172 //read in users taxonomy file and add sequences to tree
174 in >> name >> tax; gobble(in);
176 addSeqToTree(name, tax);
181 assignHeirarchyIDs(0);
183 //create file for summary if needed
186 catch(exception& e) {
187 m->errorOut(e, "PhyloTree", "PhyloTree");
192 /**************************************************************************************************/
194 string PhyloTree::getNextTaxon(string& heirarchy){
196 string currentLevel = "";
198 int pos = heirarchy.find_first_of(';');
199 currentLevel=heirarchy.substr(0,pos);
200 if (pos != (heirarchy.length()-1)) { heirarchy=heirarchy.substr(pos+1); }
201 else { heirarchy = ""; }
205 catch(exception& e) {
206 m->errorOut(e, "PhyloTree", "getNextTaxon");
211 /**************************************************************************************************/
213 int PhyloTree::addSeqToTree(string seqName, string seqTaxonomy){
217 map<string, int>::iterator childPointer;
222 tree[0].accessions.push_back(seqName);
223 string taxon;// = getNextTaxon(seqTaxonomy);
225 while(seqTaxonomy != ""){
229 if (m->control_pressed) { return 0; }
231 //somehow the parent is getting one too many accnos
232 //use print to reassign the taxa id
233 taxon = getNextTaxon(seqTaxonomy);
235 childPointer = tree[currentNode].children.find(taxon);
237 if(childPointer != tree[currentNode].children.end()){ //if the node already exists, move on
238 currentNode = childPointer->second;
239 tree[currentNode].accessions.push_back(seqName);
240 name2Taxonomy[seqName] = currentNode;
242 else{ //otherwise, create it
243 tree.push_back(TaxNode(taxon));
245 tree[currentNode].children[taxon] = numNodes-1;
246 tree[numNodes-1].parent = currentNode;
248 // int numChildren = tree[currentNode].children.size();
249 // string heirarchyID = tree[currentNode].heirarchyID;
250 // tree[currentNode].accessions.push_back(seqName);
252 currentNode = tree[currentNode].children[taxon];
253 tree[currentNode].accessions.push_back(seqName);
254 name2Taxonomy[seqName] = currentNode;
255 // tree[currentNode].level = level;
256 // tree[currentNode].childNumber = numChildren;
257 // tree[currentNode].heirarchyID = heirarchyID + '.' + toString(tree[currentNode].childNumber);
260 if (seqTaxonomy == "") { uniqueTaxonomies[currentNode] = currentNode; }
264 catch(exception& e) {
265 m->errorOut(e, "PhyloTree", "addSeqToTree");
269 /**************************************************************************************************/
270 vector<int> PhyloTree::getGenusNodes() {
273 //generate genusIndexes
274 map<int, int>::iterator it2;
275 for (it2=uniqueTaxonomies.begin(); it2!=uniqueTaxonomies.end(); it2++) { genusIndex.push_back(it2->first); }
279 catch(exception& e) {
280 m->errorOut(e, "PhyloTree", "getGenusNodes");
284 /**************************************************************************************************/
285 vector<int> PhyloTree::getGenusTotals() {
290 //reset counts because we are on a new word
291 for (int j = 0; j < genusIndex.size(); j++) {
292 totals.push_back(tree[genusIndex[j]].accessions.size());
300 catch(exception& e) {
301 m->errorOut(e, "PhyloTree", "getGenusNodes");
305 /**************************************************************************************************/
307 void PhyloTree::assignHeirarchyIDs(int index){
309 map<string,int>::iterator it;
312 for(it=tree[index].children.begin();it!=tree[index].children.end();it++){
313 tree[it->second].heirarchyID = tree[index].heirarchyID + '.' + toString(counter);
315 tree[it->second].level = tree[index].level + 1;
317 //save maxLevel for binning the unclassified seqs
318 if (tree[it->second].level > maxLevel) { maxLevel = tree[it->second].level; }
320 assignHeirarchyIDs(it->second);
323 catch(exception& e) {
324 m->errorOut(e, "PhyloTree", "assignHeirarchyIDs");
328 /**************************************************************************************************/
329 void PhyloTree::setUp(string tfile){
331 string taxFileNameTest = tfile.substr(0,tfile.find_last_of(".")+1) + "tree.sum";
335 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
337 if (pid == 0) { binUnclassified(taxFileNameTest); }
340 //create file needed for summary if it doesn't exist
341 ifstream FileTest(taxFileNameTest.c_str());
344 binUnclassified(taxFileNameTest);
348 catch(exception& e) {
349 m->errorOut(e, "PhyloTree", "setUp");
353 /**************************************************************************************************/
354 void PhyloTree::binUnclassified(string file){
358 openOutputFile(file, out);
360 map<string, int>::iterator itBin;
361 map<string, int>::iterator childPointer;
363 vector<TaxNode> copy = tree;
366 fillOutTree(0, copy);
368 //get leaf nodes that may need externsion
369 for (int i = 0; i < copy.size(); i++) {
371 if (copy[i].children.size() == 0) {
376 int copyNodes = copy.size();
378 //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
379 map<int, int>::iterator itLeaf;
380 for (itLeaf = leafNodes.begin(); itLeaf != leafNodes.end(); itLeaf++) {
382 if (m->control_pressed) { out.close(); break; }
384 int level = copy[itLeaf->second].level;
385 int currentNode = itLeaf->second;
387 //this sequence is unclassified at some levels
388 while(level <= maxLevel){
392 string taxon = "unclassified";
394 //does the parent have a child names 'unclassified'?
395 childPointer = copy[currentNode].children.find(taxon);
397 if(childPointer != copy[currentNode].children.end()){ //if the node already exists, move on
398 currentNode = childPointer->second; //currentNode becomes 'unclassified'
400 else{ //otherwise, create it
401 copy.push_back(TaxNode(taxon));
403 copy[currentNode].children[taxon] = copyNodes-1;
404 copy[copyNodes-1].parent = currentNode;
405 copy[copyNodes-1].level = copy[currentNode].level + 1;
407 currentNode = copy[currentNode].children[taxon];
412 if (!m->control_pressed) {
418 catch(exception& e) {
419 m->errorOut(e, "PhyloTree", "binUnclassified");
423 /**************************************************************************************************/
424 void PhyloTree::fillOutTree(int index, vector<TaxNode>& copy) {
426 map<string,int>::iterator it;
428 it = copy[index].children.find("unclassified");
429 if (it == copy[index].children.end()) { //no unclassified at this level
430 string taxon = "unclassified";
431 copy.push_back(TaxNode(taxon));
432 copy[index].children[taxon] = copy.size()-1;
433 copy[copy.size()-1].parent = index;
434 copy[copy.size()-1].level = copy[index].level + 1;
437 if (tree[index].level <= maxLevel) {
438 for(it=tree[index].children.begin();it!=tree[index].children.end();it++){ //check your children
439 fillOutTree(it->second, copy);
444 catch(exception& e) {
445 m->errorOut(e, "PhyloTree", "fillOutTree");
449 /**************************************************************************************************/
450 string PhyloTree::getFullTaxonomy(string seqName) {
454 int currentNode = name2Taxonomy[seqName];
456 while (tree[currentNode].parent != -1) {
457 tax = tree[currentNode].name + ";" + tax;
458 currentNode = tree[currentNode].parent;
463 catch(exception& e) {
464 m->errorOut(e, "PhyloTree", "getFullTaxonomy");
468 /**************************************************************************************************/
470 void PhyloTree::print(ofstream& out, vector<TaxNode>& copy){
472 out << copy.size() << endl;
474 for (int i = 0; i < copy.size(); i++) {
476 out << copy[i].level << '\t'<< copy[i].name << '\t' << copy[i].children.size() << '\t';
478 map<string,int>::iterator it;
479 for(it=copy[i].children.begin();it!=copy[i].children.end();it++){
480 out << it->first << '\t' << it->second << '\t';
487 catch(exception& e) {
488 m->errorOut(e, "PhyloTree", "print");
492 /**************************************************************************************************/
493 void PhyloTree::printTreeNodes(string treefilename) {
498 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
505 openOutputFile(treefilename, outTree);
508 outTree << tree.size() << endl;
509 for (int i = 0; i < tree.size(); i++) {
510 outTree << tree[i].name << '\t' << tree[i].level << '\t' << tree[i].parent << endl;
514 outTree << endl << uniqueTaxonomies.size() << endl;
515 map<int, int>::iterator it2;
516 for (it2=uniqueTaxonomies.begin(); it2!=uniqueTaxonomies.end(); it2++) { outTree << it2->first << '\t' << tree[it2->first].accessions.size() << endl; }
528 catch(exception& e) {
529 m->errorOut(e, "PhyloTree", "printTreeNodes");
533 /**************************************************************************************************/