]> git.donarmstrong.com Git - mothur.git/blobdiff - phylotree.cpp
Changed back to xcode build.
[mothur.git] / phylotree.cpp
index 6b32e27201b5103e077261117f21fe30b69eb31c..1fd4e5d238119bbcd6be5c54fbd7013fcdfc27bd 100644 (file)
@@ -54,22 +54,25 @@ PhyloTree::PhyloTree(ifstream& in, string filename){
                        istringstream iss (tempBuf,istringstream::in);
                        delete buffer;
                        
-                       iss >> numNodes; gobble(iss);
+                       //read version
+                       m->getline(iss); m->gobble(iss);
+                       
+                       iss >> numNodes; m->gobble(iss);
                        
                        tree.resize(numNodes);
                        
                        for (int i = 0; i < tree.size(); i++) {
-                               iss >> tree[i].name >> tree[i].level >> tree[i].parent; gobble(iss);
+                               iss >> tree[i].name >> tree[i].level >> tree[i].parent; m->gobble(iss);
                        }
                        
                        //read genus nodes
                        int numGenus = 0;
-                       iss >> numGenus; gobble(iss);
+                       iss >> numGenus; m->gobble(iss);
                        
                        int gnode, gsize;
                        totals.clear();
                        for (int i = 0; i < numGenus; i++) {
-                               iss >> gnode >> gsize; gobble(iss);
+                               iss >> gnode >> gsize; m->gobble(iss);
                                
                                uniqueTaxonomies[gnode] = gnode;
                                totals.push_back(gsize);
@@ -78,22 +81,25 @@ PhyloTree::PhyloTree(ifstream& in, string filename){
                        MPI_File_close(&inMPI);
                        
                #else
-                       in >> numNodes; gobble(in);
+                       //read version
+                       string line = m->getline(in); m->gobble(in);
+                       
+                       in >> numNodes; m->gobble(in);
                        
                        tree.resize(numNodes);
                        
                        for (int i = 0; i < tree.size(); i++) {
-                               in >> tree[i].name >> tree[i].level >> tree[i].parent; gobble(in);
+                               in >> tree[i].name >> tree[i].level >> tree[i].parent; m->gobble(in);
                        }
                        
                        //read genus nodes
                        int numGenus = 0;
-                       in >> numGenus; gobble(in);
+                       in >> numGenus; m->gobble(in);
                        
                        int gnode, gsize;
                        totals.clear();
                        for (int i = 0; i < numGenus; i++) {
-                               in >> gnode >> gsize; gobble(in);
+                               in >> gnode >> gsize; m->gobble(in);
                                
                                uniqueTaxonomies[gnode] = gnode;
                                totals.push_back(gsize);
@@ -138,7 +144,7 @@ PhyloTree::PhyloTree(string tfile){
                        MPI_File_open(MPI_COMM_WORLD, inFileName, MPI_MODE_RDONLY, MPI_INFO_NULL, &inMPI);  //comm, filename, mode, info, filepointer
 
                        if (pid == 0) {
-                               positions = setFilePosEachLine(tfile, num);
+                               positions = m->setFilePosEachLine(tfile, num);
                                
                                //send file positions to all processes
                                for(int i = 1; i < processors; i++) { 
@@ -173,19 +179,19 @@ PhyloTree::PhyloTree(string tfile){
                
                #else
                        ifstream in;
-                       openInputFile(tfile, in);
+                       m->openInputFile(tfile, in);
                        
                        //read in users taxonomy file and add sequences to tree
                        while(!in.eof()){
-                               in >> name >> tax; gobble(in);
-                               
+                               in >> name >> tax; m->gobble(in);
+                       
                                addSeqToTree(name, tax);
                        }
                        in.close();
                #endif
-               
+       
                assignHeirarchyIDs(0);
-               
+       
                //create file for summary if needed
                setUp(tfile);
        }
@@ -346,12 +352,7 @@ void PhyloTree::setUp(string tfile){
                        if (pid == 0) {  binUnclassified(taxFileNameTest);  }
                
                #else
-                       //create file needed for summary if it doesn't exist
-                       ifstream FileTest(taxFileNameTest.c_str());
-                       
-                       if (!FileTest) { 
-                               binUnclassified(taxFileNameTest); 
-                       }
+                       binUnclassified(taxFileNameTest); 
                #endif
        }
        catch(exception& e) {
@@ -364,7 +365,7 @@ void PhyloTree::binUnclassified(string file){
        try {
        
                ofstream out;
-               openOutputFile(file, out);
+               m->openOutputFile(file, out);
                
                map<string, int>::iterator itBin;
                map<string, int>::iterator childPointer;
@@ -381,9 +382,9 @@ void PhyloTree::binUnclassified(string file){
                                leafNodes[i] = i;
                        }
                }
-       //cout << "maxLevel = " << maxLevel << endl;    
-               int copyNodes = copy.size();
                
+               int copyNodes = copy.size();
+       
                //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
                map<int, int>::iterator itLeaf;
                for (itLeaf = leafNodes.begin(); itLeaf != leafNodes.end(); itLeaf++) {
@@ -432,25 +433,24 @@ void PhyloTree::binUnclassified(string file){
 /**************************************************************************************************/
 void PhyloTree::fillOutTree(int index, vector<TaxNode>& copy) {
        try {
+       
                map<string,int>::iterator it;
                
-               if (copy[index].level < maxLevel) {
-                       it = copy[index].children.find("unclassified");
-                       if (it == copy[index].children.end()) { //no unclassified at this level
-                               string taxon = "unclassified";
-                               copy.push_back(TaxNode(taxon));
-                               copy[index].children[taxon] = copy.size()-1;
-                               copy[copy.size()-1].parent = index;
-                               copy[copy.size()-1].level = copy[index].level + 1;
-                       }
+               it = copy[index].children.find("unclassified");
+               if (it == copy[index].children.end()) { //no unclassified at this level
+                       string taxon = "unclassified";
+                       copy.push_back(TaxNode(taxon));
+                       copy[index].children[taxon] = copy.size()-1;
+                       copy[copy.size()-1].parent = index;
+                       copy[copy.size()-1].level = copy[index].level + 1;
+               }
                
-                       
-                       for(it=copy[index].children.begin();it!=copy[index].children.end();it++){ //check your children
+               if (tree[index].level < maxLevel) {
+                       for(it=tree[index].children.begin();it!=tree[index].children.end();it++){ //check your children
                                fillOutTree(it->second, copy);
                        }
-                       
                }
-               
+
        }
        catch(exception& e) {
                m->errorOut(e, "PhyloTree", "fillOutTree");
@@ -480,8 +480,14 @@ string PhyloTree::getFullTaxonomy(string seqName) {
 
 void PhyloTree::print(ofstream& out, vector<TaxNode>& copy){
        try {
+       
+               //output mothur version
+               out << "#" << m->getVersion() << endl;
+               
                out << copy.size() << endl;
                
+               out << maxLevel << endl;
+               
                for (int i = 0; i < copy.size(); i++) {
        
                        out << copy[i].level << '\t'<< copy[i].name << '\t' << copy[i].children.size() << '\t';
@@ -513,7 +519,10 @@ void PhyloTree::printTreeNodes(string treefilename) {
                #endif
 
                        ofstream outTree;
-                       openOutputFile(treefilename, outTree);
+                       m->openOutputFile(treefilename, outTree);
+                       
+                       //output mothur version
+                       outTree << "#" << m->getVersion() << endl;
                        
                        //print treenodes
                        outTree << tree.size() << endl;