]> git.donarmstrong.com Git - mothur.git/commitdiff
added tree reader class to handle reading trees. Reworked the tree map to tree class...
authorSarah Westcott <mothur.westcott@gmail.com>
Fri, 13 Apr 2012 14:43:24 +0000 (10:43 -0400)
committerSarah Westcott <mothur.westcott@gmail.com>
Fri, 13 Apr 2012 14:43:24 +0000 (10:43 -0400)
37 files changed:
Mothur.xcodeproj/project.pbxproj
classifytreecommand.cpp
classifytreecommand.h
consensus.cpp
consensus.h
deuniquetreecommand.cpp
deuniquetreecommand.h
engine.cpp
indicatorcommand.cpp
mothurout.cpp
mothurout.h
parsimony.cpp
parsimony.h
parsimonycommand.cpp
parsimonycommand.h
phylodiversity.cpp [deleted file]
phylodiversity.h [deleted file]
phylodiversitycommand.cpp
phylodiversitycommand.h
readtree.cpp
readtree.h
removegroupscommand.cpp
tree.cpp
tree.h
treegroupscommand.cpp
treemap.cpp
treemap.h
treereader.cpp [new file with mode: 0644]
treereader.h [new file with mode: 0644]
unifracunweightedcommand.cpp
unifracunweightedcommand.h
unifracweightedcommand.cpp
unifracweightedcommand.h
unweighted.cpp
unweighted.h
weighted.cpp
weighted.h

index 0e170bca666d877d35499134d28bc26e595f350c..7169f256a410d344ed7655736676bf7d41cb62f4 100644 (file)
@@ -63,6 +63,7 @@
                A7BF2232145879B2000AD524 /* chimeraperseuscommand.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A7BF2231145879B2000AD524 /* chimeraperseuscommand.cpp */; };
                A7C3DC0B14FE457500FE1924 /* cooccurrencecommand.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A7C3DC0914FE457500FE1924 /* cooccurrencecommand.cpp */; };
                A7C3DC0F14FE469500FE1924 /* trialSwap2.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A7C3DC0D14FE469500FE1924 /* trialSwap2.cpp */; };
+               A7D755DA1535F679009BF21A /* treereader.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A7D755D91535F679009BF21A /* treereader.cpp */; };
                A7E9B88112D37EC400DA6239 /* ace.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A7E9B64F12D37EC300DA6239 /* ace.cpp */; };
                A7E9B88212D37EC400DA6239 /* aligncommand.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A7E9B65112D37EC300DA6239 /* aligncommand.cpp */; };
                A7E9B88312D37EC400DA6239 /* alignment.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A7E9B65312D37EC300DA6239 /* alignment.cpp */; };
                A7E9B91312D37EC400DA6239 /* parsimony.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A7E9B78312D37EC400DA6239 /* parsimony.cpp */; };
                A7E9B91412D37EC400DA6239 /* parsimonycommand.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A7E9B78512D37EC400DA6239 /* parsimonycommand.cpp */; };
                A7E9B91512D37EC400DA6239 /* pcoacommand.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A7E9B78712D37EC400DA6239 /* pcoacommand.cpp */; };
-               A7E9B91612D37EC400DA6239 /* phylodiversity.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A7E9B78912D37EC400DA6239 /* phylodiversity.cpp */; };
                A7E9B91712D37EC400DA6239 /* phylodiversitycommand.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A7E9B78B12D37EC400DA6239 /* phylodiversitycommand.cpp */; };
                A7E9B91812D37EC400DA6239 /* phylosummary.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A7E9B78D12D37EC400DA6239 /* phylosummary.cpp */; };
                A7E9B91912D37EC400DA6239 /* phylotree.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A7E9B78F12D37EC400DA6239 /* phylotree.cpp */; };
                A7C3DC0A14FE457500FE1924 /* cooccurrencecommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = cooccurrencecommand.h; sourceTree = "<group>"; };
                A7C3DC0D14FE469500FE1924 /* trialSwap2.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = trialSwap2.cpp; sourceTree = "<group>"; };
                A7C3DC0E14FE469500FE1924 /* trialswap2.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = trialswap2.h; sourceTree = "<group>"; };
+               A7D755D71535F665009BF21A /* treereader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = treereader.h; sourceTree = "<group>"; };
+               A7D755D91535F679009BF21A /* treereader.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = treereader.cpp; sourceTree = "<group>"; };
                A7DAAFA3133A254E003956EB /* commandparameter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = commandparameter.h; sourceTree = "<group>"; };
                A7E9B64F12D37EC300DA6239 /* ace.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ace.cpp; sourceTree = "<group>"; };
                A7E9B65012D37EC300DA6239 /* ace.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ace.h; sourceTree = "<group>"; };
                A7E9B78612D37EC400DA6239 /* parsimonycommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = parsimonycommand.h; sourceTree = "<group>"; };
                A7E9B78712D37EC400DA6239 /* pcoacommand.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = pcoacommand.cpp; sourceTree = "<group>"; };
                A7E9B78812D37EC400DA6239 /* pcoacommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = pcoacommand.h; sourceTree = "<group>"; };
-               A7E9B78912D37EC400DA6239 /* phylodiversity.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = phylodiversity.cpp; sourceTree = "<group>"; };
-               A7E9B78A12D37EC400DA6239 /* phylodiversity.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = phylodiversity.h; sourceTree = "<group>"; };
                A7E9B78B12D37EC400DA6239 /* phylodiversitycommand.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = phylodiversitycommand.cpp; sourceTree = "<group>"; };
                A7E9B78C12D37EC400DA6239 /* phylodiversitycommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = phylodiversitycommand.h; sourceTree = "<group>"; };
                A7E9B78D12D37EC400DA6239 /* phylosummary.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = phylosummary.cpp; sourceTree = "<group>"; };
                                A7E9B68F12D37EC400DA6239 /* classify.h */,
                                A7E9B73812D37EC400DA6239 /* knn.h */,
                                A7E9B73712D37EC400DA6239 /* knn.cpp */,
-                               A7E9B78912D37EC400DA6239 /* phylodiversity.cpp */,
-                               A7E9B78A12D37EC400DA6239 /* phylodiversity.h */,
                                A7E9B78D12D37EC400DA6239 /* phylosummary.cpp */,
                                A7E9B78E12D37EC400DA6239 /* phylosummary.h */,
                                A7E9B78F12D37EC400DA6239 /* phylotree.cpp */,
                                A713EBAB12DC7613000092AC /* readphylipvector.cpp */,
                                A7E9B84312D37EC400DA6239 /* splitmatrix.cpp */,
                                A7E9B84412D37EC400DA6239 /* splitmatrix.h */,
+                               A7D755D71535F665009BF21A /* treereader.h */,
+                               A7D755D91535F679009BF21A /* treereader.cpp */,
                        );
                        name = read;
                        sourceTree = "<group>";
                                A7E9B91312D37EC400DA6239 /* parsimony.cpp in Sources */,
                                A7E9B91412D37EC400DA6239 /* parsimonycommand.cpp in Sources */,
                                A7E9B91512D37EC400DA6239 /* pcoacommand.cpp in Sources */,
-                               A7E9B91612D37EC400DA6239 /* phylodiversity.cpp in Sources */,
                                A7E9B91712D37EC400DA6239 /* phylodiversitycommand.cpp in Sources */,
                                A7E9B91812D37EC400DA6239 /* phylosummary.cpp in Sources */,
                                A7E9B91912D37EC400DA6239 /* phylotree.cpp in Sources */,
                                A76CDD821510F143004C8458 /* prcseqscommand.cpp in Sources */,
                                A77EBD2F1523709100ED407C /* createdatabasecommand.cpp in Sources */,
                                A7876A26152A017C00A0AE86 /* subsample.cpp in Sources */,
+                               A7D755DA1535F679009BF21A /* treereader.cpp in Sources */,
                        );
                        runOnlyForDeploymentPostprocessing = 0;
                };
index 9ec4e6f89a40d12a1661144f8835cd6e1a4ccc84..bcf27698ce2bf68bf3dc6a3909228a9d4ccd5cb2 100644 (file)
@@ -8,6 +8,7 @@
 
 #include "classifytreecommand.h"
 #include "phylotree.h"
+#include "treereader.h"
 
 //**********************************************************************************************************************
 vector<string> ClassifyTreeCommand::setParameters(){   
@@ -86,12 +87,6 @@ ClassifyTreeCommand::ClassifyTreeCommand(string option)  {
                                if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
                        }
                        
-                       m->runParse = true;
-                       m->clearGroups();
-                       m->clearAllGroups();
-                       m->Treenames.clear();
-                       m->names.clear();
-                       
                        vector<string> tempOutNames;
                        outputTypes["tree"] = tempOutNames;
                        outputTypes["summary"] = tempOutNames;
@@ -195,74 +190,19 @@ int ClassifyTreeCommand::execute(){
                //    reading tree info                                                    //
                /***************************************************/
         m->setTreeFile(treefile);
-        if (groupfile != "") {
-                       //read in group map info.
-                       tmap = new TreeMap(groupfile);
-                       tmap->readMap();
-               }else{ //fake out by putting everyone in one group
-                       Tree* tree = new Tree(treefile); delete tree;  //extracts names from tree to make faked out groupmap
-                       tmap = new TreeMap();
-                       
-                       for (int i = 0; i < m->Treenames.size(); i++) { tmap->addSeq(m->Treenames[i], "Group1"); }
-               }
-               
-               if (namefile != "") { readNamesFile(); }
-               
-               read = new ReadNewickTree(treefile);
-               int readOk = read->read(tmap); 
-               
-               if (readOk != 0) { m->mothurOut("Read Terminated."); m->mothurOutEndLine(); delete tmap; delete read; return 0; }
-               
-               read->AssembleTrees();
-               vector<Tree*> T = read->getTrees();
-        Tree* outputTree = T[0]; 
-               delete read;
-               
-               //make sure all files match
-               //if you provide a namefile we will use the numNames in the namefile as long as the number of unique match the tree names size.
-               int numNamesInTree;
-               if (namefile != "")  {  
-                       if (numUniquesInName == m->Treenames.size()) {  numNamesInTree = nameMap.size();  }
-                       else {   numNamesInTree = m->Treenames.size();  }
-               }else {  numNamesInTree = m->Treenames.size();  }
-               
-               
-               //output any names that are in group file but not in tree
-               if (numNamesInTree < tmap->getNumSeqs()) {
-                       for (int i = 0; i < tmap->namesOfSeqs.size(); i++) {
-                               //is that name in the tree?
-                               int count = 0;
-                               for (int j = 0; j < m->Treenames.size(); j++) {
-                                       if (tmap->namesOfSeqs[i] == m->Treenames[j]) { break; } //found it
-                                       count++;
-                               }
-                               
-                               if (m->control_pressed) { 
-                                       delete tmap; for (int i = 0; i < T.size(); i++) { delete T[i]; }
-                                       for (int i = 0; i < outputNames.size(); i++) {  m->mothurRemove(outputNames[i]); } outputTypes.clear();
-                                       m->clearGroups();
-                                       return 0;
-                               }
-                               
-                               //then you did not find it so report it 
-                               if (count == m->Treenames.size()) { 
-                                       //if it is in your namefile then don't remove
-                                       map<string, string>::iterator it = nameMap.find(tmap->namesOfSeqs[i]);
-                                       
-                                       if (it == nameMap.end()) {
-                                               m->mothurOut(tmap->namesOfSeqs[i] + " is in your groupfile and not in your tree. It will be disregarded."); m->mothurOutEndLine();
-                                               tmap->removeSeq(tmap->namesOfSeqs[i]);
-                                               i--; //need this because removeSeq removes name from namesOfSeqs
-                                       }
-                               }
-                       }
-               }
+        
+        TreeReader* reader = new TreeReader(treefile, groupfile, namefile);
+        vector<Tree*> T = reader->getTrees();
+        TreeMap* tmap = T[0]->getTreeMap();
+        Tree* outputTree = T[0];
+        delete reader;
+
+        if (namefile != "") { readNamesFile(); }
                         
-        if (m->control_pressed) { delete outputTree; delete tmap;  return 0; }
+        if (m->control_pressed) { delete tmap;  delete outputTree;  return 0; }
                
         readTaxonomyFile();
         
-        
         /***************************************************/
         //             get concensus taxonomies                    //
         /***************************************************/
@@ -484,6 +424,7 @@ map<string, set<string> > ClassifyTreeCommand::getDescendantList(Tree*& T, int i
                
                int lc = T->tree[i].getLChild();
                int rc = T->tree[i].getRChild();
+        TreeMap* tmap = T->getTreeMap();
                
                if (lc == -1) { //you are a leaf your only descendant is yourself
             string group = tmap->getGroup(T->tree[i].getName());
index 026e4bae414d227dab7750d623999b86d4aa4a73..30957af065686f1f8958f7ea84d4be49c187debe 100644 (file)
@@ -30,8 +30,6 @@ public:
        void help() { m->mothurOut(getHelpString()); }  
        
 private:
-       ReadTree* read;
-    TreeMap* tmap;
        string treefile, taxonomyfile, groupfile, namefile, outputDir;
        bool abort;
        vector<string> outputNames;
index 04671f87913b1fad40f060654942cdaae8d5ff89..1be052f3aee81f6f0aded053de8249dd958118dd 100644 (file)
@@ -10,7 +10,7 @@
 #include "consensus.h"
 
 //**********************************************************************************************************************
-Tree* Consensus::getTree(vector<Tree*>& t, TreeMap* tmap){
+Tree* Consensus::getTree(vector<Tree*>& t){
        try {
                numNodes = t[0]->getNumNodes();
                numLeaves = t[0]->getNumLeaves();
@@ -21,7 +21,7 @@ Tree* Consensus::getTree(vector<Tree*>& t, TreeMap* tmap){
                
                if (m->control_pressed) { return 0; }
                
-               consensusTree = new Tree(tmap);
+               consensusTree = new Tree(t[0]->getTreeMap());
                
                it2 = nodePairs.find(treeSet);
                
@@ -35,11 +35,12 @@ Tree* Consensus::getTree(vector<Tree*>& t, TreeMap* tmap){
                
                buildConsensusTree(treeSet);
                
-               if (m->control_pressed) { delete consensusTree; return 0; }
+               if (m->control_pressed) {  delete consensusTree; return 0; }
                
-               consensusTree->assembleTree();
+        map<string, string> empty;
+               consensusTree->assembleTree(empty);
                
-               if (m->control_pressed) { delete consensusTree; return 0; }
+               if (m->control_pressed) {  delete consensusTree; return 0; }
                                
                return consensusTree; 
                
index 630ee8d527e0abe2e9456f4f663ea041d25aa728..faa4e42990316c6f9a441335c531279a8c031ec9 100644 (file)
@@ -25,7 +25,7 @@ public:
        Consensus() { m = MothurOut::getInstance(); }   
        ~Consensus() {}
        
-    Tree* getTree(vector<Tree*>&, TreeMap*);
+    Tree* getTree(vector<Tree*>&);
                
 private:
     MothurOut* m;
index 64ea9b700d3e7193fb3502ae2bd5dd160cb26297..c33c8e4ce19014c0059a89469217993a3f2fe59d 100644 (file)
@@ -8,6 +8,7 @@
  */
 
 #include "deuniquetreecommand.h"
+#include "treereader.h"
 
 //**********************************************************************************************************************
 vector<string> DeuniqueTreeCommand::setParameters(){   
@@ -103,13 +104,7 @@ DeuniqueTreeCommand::DeuniqueTreeCommand(string option)  {
                                }
                        }
                        
-                       m->runParse = true;
-                       m->clearGroups();
-                       m->clearAllGroups();
-                       m->Treenames.clear();
-                       m->names.clear();
-                       
-                       //check for required parameters
+            //check for required parameters
                        treefile = validParameter.validFile(parameters, "tree", true);
                        if (treefile == "not open") { abort = true; }
                        else if (treefile == "not found") {                             //if there is a current design file, use it
@@ -144,72 +139,21 @@ int DeuniqueTreeCommand::execute() {
                
                m->setTreeFile(treefile);
                
-               //extracts names from tree to make faked out groupmap
-               Tree* tree = new Tree(treefile); delete tree;  
-               tmap = new TreeMap();
-               for (int i = 0; i < m->Treenames.size(); i++) { tmap->addSeq(m->Treenames[i], "Group1"); }
-               
-               if (m->control_pressed) {  delete tmap;  return 0; }
-               
-               readNamesFile(); 
-               
-               if (m->control_pressed) {  delete tmap;  return 0; }
-               
-               ReadTree* read = new ReadNewickTree(treefile);
-               int readOk = read->read(tmap); 
-               if (readOk != 0) { m->mothurOut("Read Terminated."); m->mothurOutEndLine(); delete tmap; delete read; return 0; }
-               
-               read->AssembleTrees();
-               vector<Tree*> T = read->getTrees();
-               delete read;
-               
-               //make sure all files match
-               //if you provide a namefile we will use the numNames in the namefile as long as the number of unique match the tree names size.
-               int numNamesInTree;
-               if (numUniquesInName == m->Treenames.size()) {  numNamesInTree = nameMap.size();  }
-               else {   numNamesInTree = m->Treenames.size();  }
-               
-               //output any names that are in group file but not in tree
-               if (numNamesInTree < tmap->getNumSeqs()) {
-                       for (int i = 0; i < tmap->namesOfSeqs.size(); i++) {
-                               //is that name in the tree?
-                               int count = 0;
-                               for (int j = 0; j < m->Treenames.size(); j++) {
-                                       if (tmap->namesOfSeqs[i] == m->Treenames[j]) { break; } //found it
-                                       count++;
-                               }
-                               
-                               if (m->control_pressed) { 
-                                       delete tmap; for (int i = 0; i < T.size(); i++) { delete T[i]; }
-                                       for (int i = 0; i < outputNames.size(); i++) {  m->mothurRemove(outputNames[i]); } outputTypes.clear();
-                                       m->clearGroups();
-                                       return 0;
-                               }
-                               
-                               //then you did not find it so report it 
-                               if (count == m->Treenames.size()) { 
-                                       //if it is in your namefile then don't remove
-                                       map<string, string>::iterator it = nameMap.find(tmap->namesOfSeqs[i]);
-                                       
-                                       if (it == nameMap.end()) {
-                                               m->mothurOut(tmap->namesOfSeqs[i] + " is in your groupfile and not in your tree. It will be disregarded."); m->mothurOutEndLine();
-                                               tmap->removeSeq(tmap->namesOfSeqs[i]);
-                                               i--; //need this because removeSeq removes name from namesOfSeqs
-                                       }
-                               }
-                       }
-               }
-               
+               TreeReader* reader = new TreeReader(treefile, "", namefile);
+        vector<Tree*> T = reader->getTrees();
+        map<string, string> nameMap = reader->getNameMap();
+        delete reader;         
                
                //print new Tree
                string outputFile = outputDir + m->getRootName(m->getSimpleName(treefile)) + "deunique.tre";
                outputNames.push_back(outputFile); outputTypes["tree"].push_back(outputFile);
                ofstream out;
                m->openOutputFile(outputFile, out);
-               T[0]->print(out, "deunique");
+               T[0]->print(out, nameMap);
                out.close();
                
-               delete tmap; for (int i = 0; i < T.size(); i++) { delete T[i]; }
+        delete (T[0]->getTreeMap());
+               for (int i = 0; i < T.size(); i++) { delete T[i]; }
                                
                //set phylip file as new current phylipfile
                string current = "";
@@ -231,46 +175,6 @@ int DeuniqueTreeCommand::execute() {
                exit(1);
        }
 }
-/*****************************************************************/
-int DeuniqueTreeCommand::readNamesFile() {
-       try {
-               m->names.clear();
-               numUniquesInName = 0;
-               
-               ifstream in;
-               m->openInputFile(namefile, in);
-               
-               string first, second;
-               map<string, string>::iterator itNames;
-               
-               while(!in.eof()) {
-                       in >> first >> second; m->gobble(in);
-                       
-                       numUniquesInName++;
-                       
-                       itNames = m->names.find(first);
-                       if (itNames == m->names.end()) {  
-                               m->names[first] = second; 
-                               
-                               //we need a list of names in your namefile to use above when removing extra seqs above so we don't remove them
-                               vector<string> dupNames;
-                               m->splitAtComma(second, dupNames);
-                               
-                               for (int i = 0; i < dupNames.size(); i++) {     
-                                       nameMap[dupNames[i]] = dupNames[i]; 
-                                       if (i != 0) { tmap->addSeq(dupNames[i], "Group1"); } 
-                               }
-                       }else {  m->mothurOut(first + " has already been seen in namefile, aborting."); m->mothurOutEndLine(); in.close(); m->names.clear(); m->control_pressed = true; return 1; }                     
-               }
-               in.close();
-               
-               return 0;
-       }
-       catch(exception& e) {
-               m->errorOut(e, "DeuniqueTreeCommand", "readNamesFile");
-               exit(1);
-       }
-}
 /***********************************************************/
 
 
index 6d72253bad9b1556aed865cae0a1e8b52c91cdea..18a6e5e95491603f051b847467db80512d3ad517 100644 (file)
@@ -12,7 +12,6 @@
 
 
 #include "command.hpp"
-#include "treemap.h"
 #include "sharedutilities.h"
 #include "readtree.h"
 
@@ -35,7 +34,6 @@ public:
        
        
 private:
-       TreeMap* tmap;
        int numUniquesInName;
        
        bool abort;
index ffbe324acd490ec6eb621ce0fe8d26b3b38d3ac0..24adcb53b59f39f049269d74b4da953a5f387262 100644 (file)
@@ -183,7 +183,6 @@ bool InteractEngine::getInput(){
                                        mout->clearGroups();
                                        mout->clearAllGroups();
                                        mout->Treenames.clear();
-                                       mout->names.clear();
                                        mout->saveNextLabel = "";
                                        mout->printedHeaders = false;
                                        mout->commandInputsConvertError = false;
@@ -369,7 +368,6 @@ bool BatchEngine::getInput(){
                                        mout->clearGroups();
                                        mout->clearAllGroups();
                                        mout->Treenames.clear();
-                                       mout->names.clear();
                                        mout->saveNextLabel = "";
                                        mout->printedHeaders = false;
                                        mout->commandInputsConvertError = false;
@@ -538,7 +536,6 @@ bool ScriptEngine::getInput(){
                                        mout->clearGroups();
                                        mout->clearAllGroups();
                                        mout->Treenames.clear();
-                                       mout->names.clear();
                                        mout->saveNextLabel = "";
                                        mout->printedHeaders = false;
                                        mout->commandInputsConvertError = false;
index fe99c46847ff08fa8359f6899a4ddf9a095126ea..94d5c84344f86eb4676f0e71111e898c5c28596d 100644 (file)
@@ -99,7 +99,6 @@ IndicatorCommand::IndicatorCommand(string option)  {
                        m->clearGroups();
                        m->clearAllGroups();
                        m->Treenames.clear();
-                       m->names.clear();
                        
                        vector<string> tempOutNames;
                        outputTypes["tree"] = tempOutNames;
@@ -236,11 +235,10 @@ int IndicatorCommand::execute(){
                        designMap->readDesignMap();
                        
                        //fill Groups - checks for "all" and for any typo groups
-                       SharedUtil* util = new SharedUtil();
+                       SharedUtil util;
                        vector<string> nameGroups = designMap->getNamesOfGroups();
-                       util->setGroups(Groups, nameGroups);
+                       util.setGroups(Groups, nameGroups);
                        designMap->setNamesOfGroups(nameGroups);
-                       delete util;
                        
                        //loop through the Groups and fill Globaldata's Groups with the design file info
                        vector<string> namesSeqs = designMap->getNamesSeqs(Groups);
@@ -320,8 +318,9 @@ int IndicatorCommand::execute(){
                                else { for (int i = 0; i < lookupFloat.size(); i++) {  delete lookupFloat[i];  } }
                                for (int i = 0; i < T.size(); i++) {  delete T[i];  }  delete treeMap; return 0; 
                        }
-                               
-                       T[0]->assembleTree();
+            
+                       map<string, string> nameMap;    
+                       T[0]->assembleTree(nameMap);
                                        
                        /***************************************************/
                        //    create ouptut tree - respecting pickedGroups //
@@ -329,7 +328,7 @@ int IndicatorCommand::execute(){
                        Tree* outputTree = new Tree(m->getNumGroups(), treeMap); 
                        
                        outputTree->getSubTree(T[0], m->getGroups());
-                       outputTree->assembleTree();
+                       outputTree->assembleTree(nameMap);
                                
                        //no longer need original tree, we have output tree to use and label
                        for (int i = 0; i < T.size(); i++) {  delete T[i];  } 
index d5f1534f7535052c75888b04d28211fd522a0410..ee809fafc6e39f6c5cc02d78b513d82704864aa8 100644 (file)
@@ -1014,7 +1014,8 @@ int MothurOut::appendFiles(string temp, string filename) {
                
                int numLines = 0;
                if (ableToOpen == 0) { //you opened it
-                       while(char c = input.get()){
+                       while(!input.eof()){
+                char c = input.get();
                                if(input.eof())         {       break;                  }
                                else                            {       output << c;    if (c == '\n') {numLines++;} }
                        }
index 0659e4e3628ccb28fdfc024427fe6cebf5af5446..2a6ba2de6316eb24ff281d703f8e27d4c94a45a3 100644 (file)
@@ -65,7 +65,7 @@ class MothurOut {
        
                vector<string> getAllGroups() { sort(namesOfGroups.begin(), namesOfGroups.end()); return namesOfGroups; }
                vector<string> Treenames;
-               map<string, string> names;
+               //map<string, string> names;
                vector<string> binLabelsInFile;
                vector<string> currentBinLabels;
                string saveNextLabel, argv, sharedHeaderMode;
index d26bc270efcdd7fff7cb1292ddc2bfb786a48665..bbb929d78d9aa6550c587c77eccec294cbe3c715 100644 (file)
@@ -15,6 +15,7 @@ EstOutput Parsimony::getValues(Tree* t, int p, string o) {
        try {
                processors = p;
                outputDir = o;
+        TreeMap* tmap = t->getTreeMap();
                
                //if the users enters no groups then give them the score of all groups
                vector<string> mGroups = m->getGroups();
@@ -56,7 +57,7 @@ EstOutput Parsimony::getValues(Tree* t, int p, string o) {
                
        #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
                if(processors == 1){
-                       data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size());
+                       data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size(), tmap);
                }else{
                        lines.clear();
                        int numPairs = namesOfGroupCombos.size();
@@ -73,7 +74,7 @@ EstOutput Parsimony::getValues(Tree* t, int p, string o) {
                                lines.push_back(linePair(startPos, numPairsPerProcessor));
                        }
                        
-                       data = createProcesses(t, namesOfGroupCombos);
+                       data = createProcesses(t, namesOfGroupCombos, tmap);
                }
        #else
                data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size());
@@ -89,7 +90,7 @@ EstOutput Parsimony::getValues(Tree* t, int p, string o) {
 }
 /**************************************************************************************************/
 
-EstOutput Parsimony::createProcesses(Tree* t, vector< vector<string> > namesOfGroupCombos) {
+EstOutput Parsimony::createProcesses(Tree* t, vector< vector<string> > namesOfGroupCombos, TreeMap* tmap) {
        try {
 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
                int process = 1;
@@ -106,7 +107,7 @@ EstOutput Parsimony::createProcesses(Tree* t, vector< vector<string> > namesOfGr
                                process++;
                        }else if (pid == 0){
                                EstOutput myresults;
-                               myresults = driver(t, namesOfGroupCombos, lines[process].start, lines[process].num);
+                               myresults = driver(t, namesOfGroupCombos, lines[process].start, lines[process].num, tmap);
                                
                                if (m->control_pressed) { exit(0); }
                                
@@ -126,7 +127,7 @@ EstOutput Parsimony::createProcesses(Tree* t, vector< vector<string> > namesOfGr
                        }
                }
                
-               results = driver(t, namesOfGroupCombos, lines[0].start, lines[0].num);
+               results = driver(t, namesOfGroupCombos, lines[0].start, lines[0].num, tmap);
                
                //force parent to wait until all the processes are done
                for (int i=0;i<processIDS.size();i++) { 
@@ -169,7 +170,7 @@ EstOutput Parsimony::createProcesses(Tree* t, vector< vector<string> > namesOfGr
        }
 }
 /**************************************************************************************************/
-EstOutput Parsimony::driver(Tree* t, vector< vector<string> > namesOfGroupCombos, int start, int num) { 
+EstOutput Parsimony::driver(Tree* t, vector< vector<string> > namesOfGroupCombos, int start, int num, TreeMap* tmap) { 
        try {
                
                EstOutput results; results.resize(num);
index b116aa2d1a55e0ba84571f1f4c9500c6be5d8fcc..7316d508dd52729c8ddec87c34a08c76586cd4d1 100644 (file)
 class Parsimony : public TreeCalculator  {
        
        public:
-               Parsimony(TreeMap* t) : tmap(t) {};
+               Parsimony() {};
                ~Parsimony() {};
                EstOutput getValues(Tree*, int, string);
-               //EstOutput getValues(Tree*, string, string) { return data; }
                
        private:
                struct linePair {
@@ -33,12 +32,11 @@ class Parsimony : public TreeCalculator  {
                vector<linePair> lines;
        
                EstOutput data;
-               TreeMap* tmap;
                int processors;
                string outputDir;
        
-               EstOutput driver(Tree*, vector< vector<string> >, int, int); 
-               EstOutput createProcesses(Tree*, vector< vector<string> >);
+               EstOutput driver(Tree*, vector< vector<string> >, int, int, TreeMap*); 
+               EstOutput createProcesses(Tree*, vector< vector<string> >, TreeMap*);
 };
 
 /***********************************************************************/
index 2d46efc9efebcfbe7807292cb814cf58686f9ac4..50e1bfa7e590d6e2fa47b4c184700b440cdb9621 100644 (file)
@@ -8,6 +8,7 @@
  */
 
 #include "parsimonycommand.h"
+#include "treereader.h"
 
 //**********************************************************************************************************************
 vector<string> ParsimonyCommand::setParameters(){      
@@ -125,12 +126,6 @@ ParsimonyCommand::ParsimonyCommand(string option)  {
                                }
                        }
                        
-                       m->runParse = true;
-                       m->clearGroups();
-                       m->clearAllGroups();
-                       m->Treenames.clear();
-                       m->names.clear();
-                       
                        outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = ""; }
                        
                        randomtree = validParameter.validFile(parameters, "random", false);             if (randomtree == "not found") { randomtree = ""; }
@@ -203,68 +198,11 @@ int ParsimonyCommand::execute() {
                        
                        m->setTreeFile(treefile);
                        
-                       if (groupfile != "") {
-                               //read in group map info.
-                               tmap = new TreeMap(groupfile);
-                               tmap->readMap();
-                       }else{ //fake out by putting everyone in one group
-                               Tree* tree = new Tree(treefile); delete tree;  //extracts names from tree to make faked out groupmap
-                               tmap = new TreeMap();
-                               
-                               for (int i = 0; i < m->Treenames.size(); i++) { tmap->addSeq(m->Treenames[i], "Group1"); }
-                       }
-                       
-                       if (namefile != "") { readNamesFile(); }
-                       
-                       read = new ReadNewickTree(treefile);
-                       int readOk = read->read(tmap); 
-                       
-                       if (readOk != 0) { m->mothurOut("Read Terminated."); m->mothurOutEndLine(); delete tmap; delete read; return 0; }
-                       
-                       read->AssembleTrees();
-                       T = read->getTrees();
-                       delete read;
-
-                       //make sure all files match
-                       //if you provide a namefile we will use the numNames in the namefile as long as the number of unique match the tree names size.
-                       int numNamesInTree;
-                       if (namefile != "")  {  
-                               if (numUniquesInName == m->Treenames.size()) {  numNamesInTree = nameMap.size();  }
-                               else {   numNamesInTree = m->Treenames.size();  }
-                       }else {  numNamesInTree = m->Treenames.size();  }
-                       
-                       
-                       //output any names that are in group file but not in tree
-                       if (numNamesInTree < tmap->getNumSeqs()) {
-                               for (int i = 0; i < tmap->namesOfSeqs.size(); i++) {
-                                       //is that name in the tree?
-                                       int count = 0;
-                                       for (int j = 0; j < m->Treenames.size(); j++) {
-                                               if (tmap->namesOfSeqs[i] == m->Treenames[j]) { break; } //found it
-                                               count++;
-                                       }
-                                       
-                                       if (m->control_pressed) { 
-                                               delete tmap; for (int i = 0; i < T.size(); i++) { delete T[i]; }
-                                               for (int i = 0; i < outputNames.size(); i++) {  m->mothurRemove(outputNames[i]); } outputTypes.clear();
-                                               m->clearGroups();
-                                               return 0;
-                                       }
-                                       
-                                       //then you did not find it so report it 
-                                       if (count == m->Treenames.size()) { 
-                                               //if it is in your namefile then don't remove
-                                               map<string, string>::iterator it = nameMap.find(tmap->namesOfSeqs[i]);
-                                               
-                                               if (it == nameMap.end()) {
-                                                       m->mothurOut(tmap->namesOfSeqs[i] + " is in your groupfile and not in your tree. It will be disregarded."); m->mothurOutEndLine();
-                                                       tmap->removeSeq(tmap->namesOfSeqs[i]);
-                                                       i--; //need this because removeSeq removes name from namesOfSeqs
-                                               }
-                                       }
-                               }
-                       }
-                               
+            TreeReader* reader = new TreeReader(treefile, groupfile, namefile);
+            T = reader->getTrees();
+            tmap = T[0]->getTreeMap();
+            delete reader;
+       
                        if(outputDir == "") { outputDir += m->hasPath(treefile); }
                        output = new ColumnFile(outputDir + m->getSimpleName(treefile)  +  ".parsimony", itersString);
                        outputNames.push_back(outputDir + m->getSimpleName(treefile)  +  ".parsimony");
@@ -284,24 +222,23 @@ int ParsimonyCommand::execute() {
                }
                        
                //set users groups to analyze
-               util = new SharedUtil();
+               SharedUtil util;
                vector<string> mGroups = m->getGroups();
                vector<string> tGroups = tmap->getNamesOfGroups();
-               util->setGroups(mGroups, tGroups, allGroups, numGroups, "parsimony");   //sets the groups the user wants to analyze
-               util->getCombos(groupComb, mGroups, numComp);
+               util.setGroups(mGroups, tGroups, allGroups, numGroups, "parsimony");    //sets the groups the user wants to analyze
+               util.getCombos(groupComb, mGroups, numComp);
                m->setGroups(mGroups);
-               delete util;
                        
                if (numGroups == 1) { numComp++; groupComb.push_back(allGroups); }
                        
-               pars = new Parsimony(tmap);
+               Parsimony pars;
                counter = 0;
        
                Progress* reading;
                reading = new Progress("Comparing to random:", iters);
                
                if (m->control_pressed) { 
-                       delete reading; delete pars; delete output;
+                       delete reading; delete output;
                        delete tmap; for (int i = 0; i < T.size(); i++) { delete T[i]; }
                        if (randomtree == "") {  outSum.close();  }
                        for (int i = 0; i < outputNames.size(); i++) {  m->mothurRemove(outputNames[i]); } outputTypes.clear();
@@ -323,10 +260,10 @@ int ParsimonyCommand::execute() {
                if (randomtree == "") {
                        //get pscores for users trees
                        for (int i = 0; i < T.size(); i++) {
-                               userData = pars->getValues(T[i], processors, outputDir);  //data = AB, AC, BC, ABC.
+                               userData = pars.getValues(T[i], processors, outputDir);  //data = AB, AC, BC, ABC.
                                
                                if (m->control_pressed) { 
-                                       delete reading; delete pars; delete output;
+                                       delete reading; delete output;
                                        delete tmap; for (int i = 0; i < T.size(); i++) { delete T[i]; }
                                        if (randomtree == "") {  outSum.close();  }
                                        for (int i = 0; i < outputNames.size(); i++) {  m->mothurRemove(outputNames[i]); } outputTypes.clear();
@@ -362,10 +299,10 @@ int ParsimonyCommand::execute() {
                                randT->assembleRandomTree();
 
                                //get pscore of random tree
-                               randomData = pars->getValues(randT, processors, outputDir);
+                               randomData = pars.getValues(randT, processors, outputDir);
                                
                                if (m->control_pressed) { 
-                                       delete reading; delete pars; delete output; delete randT;
+                                       delete reading;  delete output; delete randT;
                                        if (randomtree == "") {  outSum.close();  }
                                        for (int i = 0; i < outputNames.size(); i++) {  m->mothurRemove(outputNames[i]); } outputTypes.clear();
                                        delete tmap; for (int i = 0; i < T.size(); i++) { delete T[i]; }
@@ -403,23 +340,17 @@ int ParsimonyCommand::execute() {
                                randT->assembleRandomTree();
                                
                                if (m->control_pressed) { 
-                                       delete reading; delete pars; delete output; delete randT;
-                                       delete tmap; 
-                                       for (int i = 0; i < outputNames.size(); i++) {  m->mothurRemove(outputNames[i]); } outputTypes.clear();
-                                       m->clearGroups();
-                                       return 0;
+                                       delete reading; delete output; delete randT; delete tmap; 
+                                       for (int i = 0; i < outputNames.size(); i++) {  m->mothurRemove(outputNames[i]); } outputTypes.clear(); return 0;
                                }
 
 
                                //get pscore of random tree
-                               randomData = pars->getValues(randT, processors, outputDir);
+                               randomData = pars.getValues(randT, processors, outputDir);
                                
                                if (m->control_pressed) { 
-                                       delete reading; delete pars;  delete output; delete randT;
-                                       delete tmap; 
-                                       for (int i = 0; i < outputNames.size(); i++) {  m->mothurRemove(outputNames[i]); } outputTypes.clear();
-                                       m->clearGroups();
-                                       return 0;
+                                       delete reading; delete output; delete randT; delete tmap; 
+                                       for (int i = 0; i < outputNames.size(); i++) {  m->mothurRemove(outputNames[i]); } outputTypes.clear(); return 0;
                                }
                        
                                for(int r = 0; r < numComp; r++) {
@@ -471,27 +402,21 @@ int ParsimonyCommand::execute() {
                }
                
                if (m->control_pressed) { 
-                               delete reading; delete pars; delete output;
+                               delete reading; delete output;
                                delete tmap; for (int i = 0; i < T.size(); i++) { delete T[i]; }
                                if (randomtree == "") {  outSum.close();  }
                                for (int i = 0; i < outputNames.size(); i++) {  m->mothurRemove(outputNames[i]); } outputTypes.clear();
-                               m->clearGroups();
                                return 0;
                }
                
                //finish progress bar
                reading->finish();
                delete reading;
-
                
                printParsimonyFile();
                if (randomtree == "") { printUSummaryFile(); }
-               
-               //reset groups parameter
-               m->clearGroups(); 
-               
-               delete pars; delete output; 
-               delete tmap; for (int i = 0; i < T.size(); i++) { delete T[i]; }
+                               
+        delete output; delete tmap; for (int i = 0; i < T.size(); i++) { delete T[i]; }
                
                if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        m->mothurRemove(outputNames[i]); } outputTypes.clear(); return 0;}
                
@@ -623,46 +548,6 @@ void ParsimonyCommand::getUserInput() {
                exit(1);
        }
 }
-/*****************************************************************/
-int ParsimonyCommand::readNamesFile() {
-       try {
-               m->names.clear();
-               numUniquesInName = 0;
-               
-               ifstream in;
-               m->openInputFile(namefile, in);
-               
-               string first, second;
-               map<string, string>::iterator itNames;
-               
-               while(!in.eof()) {
-                       in >> first >> second; m->gobble(in);
-                       
-                       numUniquesInName++;
-                       
-                       itNames = m->names.find(first);
-                       if (itNames == m->names.end()) {  
-                               m->names[first] = second; 
-                               
-                               //we need a list of names in your namefile to use above when removing extra seqs above so we don't remove them
-                               vector<string> dupNames;
-                               m->splitAtComma(second, dupNames);
-                               
-                               for (int i = 0; i < dupNames.size(); i++) {     
-                                       nameMap[dupNames[i]] = dupNames[i]; 
-                                       if ((groupfile == "") && (i != 0)) { tmap->addSeq(dupNames[i], "Group1"); } 
-                               }
-                       }else {  m->mothurOut(first + " has already been seen in namefile, disregarding names file."); m->mothurOutEndLine(); in.close(); m->names.clear(); namefile = ""; return 1; }                  
-               }
-               in.close();
-               
-               return 0;
-       }
-       catch(exception& e) {
-               m->errorOut(e, "ParsimonyCommand", "readNamesFile");
-               exit(1);
-       }
-}
 /***********************************************************/
 
 
index 81fa99ba06c691f9e0ee06f4b73521655b046e8c..917255696166a505ae5734e20f0aaab47573876c 100644 (file)
@@ -36,15 +36,12 @@ public:
        void help() { m->mothurOut(getHelpString()); }
        
 private:
-       ReadTree* read;
-       SharedUtil* util;
        FileOutput* output;
        vector<Tree*> T;           //user trees
        Tree* randT;  //random tree
        Tree* copyUserTree; 
        TreeMap* tmap; 
        TreeMap* savetmap;
-       Parsimony* pars;
        vector<string> groupComb; // AB. AC, BC...
        string sumFile, randomtree, allGroups, outputDir, treefile, groupfile, namefile;
        int iters, numGroups, numComp, counter, processors, numUniquesInName;
diff --git a/phylodiversity.cpp b/phylodiversity.cpp
deleted file mode 100644 (file)
index d5ccaf9..0000000
+++ /dev/null
@@ -1,81 +0,0 @@
-/*
- *  phylodiversity.cpp
- *  Mothur
- *
- *  Created by westcott on 4/30/10.
- *  Copyright 2010 Schloss Lab. All rights reserved.
- *
- */
-
-#include "phylodiversity.h"
-
-/**************************************************************************************************
-EstOutput PhyloDiversity::getValues(Tree* t, vector<int> treeNodes, vector< vector<float> >& data) {
-    try {
-               
-               map<string, float> DScore;
-               float totalLength = 0.0;
-               data.clear();
-               
-               //initialize Dscore
-               for (int i=0; i<globaldata->Groups.size(); i++) {               DScore[globaldata->Groups[i]] = 0.0;    }
-       
-               ********************************************************
-               //calculate a D value for each group 
-               for(int v=0;v<treeNodes.size();v++){
-                               
-                       if (m->control_pressed) { return data; }
-                       
-                       //calc the branch length
-                       //while you aren't at root
-                       float sum = 0.0;
-                       int index = treeNodes[v];
-
-                       while(t->tree[index].getParent() != -1){
-                               
-                               //if you have a BL
-                               if(t->tree[index].getBranchLength() != -1){
-                                       sum += abs(t->tree[index].getBranchLength());
-                               }
-                               index = t->tree[index].getParent();
-                       }
-                               
-                       //get last breanch length added
-                       if(t->tree[index].getBranchLength() != -1){
-                               sum += abs(t->tree[index].getBranchLength());
-                       }
-                               
-                       //for each group in the groups update the total branch length accounting for the names file
-                       vector<string> groups = t->tree[treeNodes[v]].getGroup();
-                       for (int j = 0; j < groups.size(); j++) {
-                               int numSeqsInGroupJ = 0;
-                               map<string, int>::iterator it;
-                               it = t->tree[treeNodes[v]].pcount.find(groups[j]);
-                               if (it != t->tree[treeNodes[v]].pcount.end()) { //this leaf node contains seqs from group j
-                                       numSeqsInGroupJ = it->second;
-                               }
-
-                               //add branch length to total for group
-                               DScore[groups[j]] += (numSeqsInGroupJ * sum);
-                       }
-                       
-               }
-               
-       
-               for (int i=0; i<globaldata->Groups.size(); i++) {   
-                       float percent = DScore[globaldata->Groups[i]]; 
-                       data.push_back(percent);  
-                       
-               }
-               
-               return data;
-       }
-       catch(exception& e) {
-               m->errorOut(e, "PhyloDiversity", "getValues");
-               exit(1);
-       }
-}
-**************************************************************************************************/
-
-
-
diff --git a/phylodiversity.h b/phylodiversity.h
deleted file mode 100644 (file)
index a789efa..0000000
+++ /dev/null
@@ -1,38 +0,0 @@
-#ifndef PHYLODIVERSITY_H
-#define PHYLODIVERSITY_H
-
-
-/*
- *  phylodiversity.h
- *  Mothur
- *
- *  Created by westcott on 4/30/10.
- *  Copyright 2010 Schloss Lab. All rights reserved.
- *
- */
-
-#include "treemap.h"
-#include "mothurout.h"
-
-
-/***********************************************************************/
-
-class PhyloDiversity  {
-       
-       public:
-               PhyloDiversity(TreeMap* t) : tmap(t) {  m = MothurOut::getInstance(); }
-               ~PhyloDiversity() {};
-               
-               //int getValues(Tree*, vector<int>, vector< vector< float> >&);
-               
-               
-       private:
-               MothurOut* m;
-               TreeMap* tmap;
-};
-
-/***********************************************************************/
-
-
-#endif
-
index abf9591f4060482a158df9b008e0f357d6c6b069..3db101a89f3c60e93ebb7961ac75bdea5524f630 100644 (file)
@@ -8,6 +8,7 @@
  */
 
 #include "phylodiversitycommand.h"
+#include "treereader.h"
 
 //**********************************************************************************************************************
 vector<string> PhyloDiversityCommand::setParameters(){ 
@@ -136,12 +137,6 @@ PhyloDiversityCommand::PhyloDiversityCommand(string option)  {
                                }
                        }
                        
-                       m->runParse = true;
-                       m->clearGroups();
-                       m->clearAllGroups();
-                       m->Treenames.clear();
-                       m->names.clear();
-                       
                        //check for required parameters
                        treefile = validParameter.validFile(parameters, "tree", true);
                        if (treefile == "not open") { treefile = ""; abort = true; }
@@ -218,74 +213,15 @@ int PhyloDiversityCommand::execute(){
                if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
                
                m->setTreeFile(treefile);
-               
-               if (groupfile != "") {
-                       //read in group map info.
-                       tmap = new TreeMap(groupfile);
-                       tmap->readMap();
-               }else{ //fake out by putting everyone in one group
-                       Tree* tree = new Tree(treefile); delete tree;  //extracts names from tree to make faked out groupmap
-                       tmap = new TreeMap();
-                       
-                       for (int i = 0; i < m->Treenames.size(); i++) { tmap->addSeq(m->Treenames[i], "Group1"); }
-               }
-               
-               if (namefile != "") { readNamesFile(); }
-               
-               read = new ReadNewickTree(treefile);
-               int readOk = read->read(tmap); 
-               
-               if (readOk != 0) { m->mothurOut("Read Terminated."); m->mothurOutEndLine(); delete tmap; delete read; return 0; }
-               
-               read->AssembleTrees();
-               vector<Tree*> trees = read->getTrees();
-               delete read;
-               
-               //make sure all files match
-               //if you provide a namefile we will use the numNames in the namefile as long as the number of unique match the tree names size.
-               int numNamesInTree;
-               if (namefile != "")  {  
-                       if (numUniquesInName == m->Treenames.size()) {  numNamesInTree = nameMap.size();  }
-                       else {   numNamesInTree = m->Treenames.size();  }
-               }else {  numNamesInTree = m->Treenames.size();  }
-               
-               
-               //output any names that are in group file but not in tree
-               if (numNamesInTree < tmap->getNumSeqs()) {
-                       for (int i = 0; i < tmap->namesOfSeqs.size(); i++) {
-                               //is that name in the tree?
-                               int count = 0;
-                               for (int j = 0; j < m->Treenames.size(); j++) {
-                                       if (tmap->namesOfSeqs[i] == m->Treenames[j]) { break; } //found it
-                                       count++;
-                               }
-                               
-                               if (m->control_pressed) { 
-                                       delete tmap; for (int i = 0; i < trees.size(); i++) { delete trees[i]; }
-                                       for (int i = 0; i < outputNames.size(); i++) {  m->mothurRemove(outputNames[i]); } outputTypes.clear();
-                                       m->clearGroups();
-                                       return 0;
-                               }
-                               
-                               //then you did not find it so report it 
-                               if (count == m->Treenames.size()) { 
-                                       //if it is in your namefile then don't remove
-                                       map<string, string>::iterator it = nameMap.find(tmap->namesOfSeqs[i]);
-                                       
-                                       if (it == nameMap.end()) {
-                                               m->mothurOut(tmap->namesOfSeqs[i] + " is in your groupfile and not in your tree. It will be disregarded."); m->mothurOutEndLine();
-                                               tmap->removeSeq(tmap->namesOfSeqs[i]);
-                                               i--; //need this because removeSeq removes name from namesOfSeqs
-                                       }
-                               }
-                       }
-               }
-               
-               SharedUtil* util = new SharedUtil();
+        TreeReader* reader = new TreeReader(treefile, groupfile, namefile);
+        vector<Tree*> trees = reader->getTrees();
+        tmap = trees[0]->getTreeMap();
+        delete reader;
+
+               SharedUtil util;
                vector<string> mGroups = m->getGroups();
                vector<string> tGroups = tmap->getNamesOfGroups();
-               util->setGroups(mGroups, tGroups, "phylo.diversity");   //sets the groups the user wants to analyze
-               delete util;
+               util.setGroups(mGroups, tGroups, "phylo.diversity");    //sets the groups the user wants to analyze
                
                //incase the user had some mismatches between the tree and group files we don't want group xxx to be analyzed
                for (int i = 0; i < mGroups.size(); i++) { if (mGroups[i] == "xxx") { mGroups.erase(mGroups.begin()+i);  break; }  }
@@ -711,47 +647,6 @@ vector<float> PhyloDiversityCommand::calcBranchLength(Tree* t, int leaf, map< st
                exit(1);
        }
 }
-/*****************************************************************/
-int PhyloDiversityCommand::readNamesFile() {
-       try {
-               m->names.clear();
-               numUniquesInName = 0;
-               
-               ifstream in;
-               m->openInputFile(namefile, in);
-               
-               string first, second;
-               map<string, string>::iterator itNames;
-               
-               while(!in.eof()) {
-                       in >> first >> second; m->gobble(in);
-                       
-                       numUniquesInName++;
-                       
-                       itNames = m->names.find(first);
-                       if (itNames == m->names.end()) {  
-                               m->names[first] = second; 
-                               
-                               //we need a list of names in your namefile to use above when removing extra seqs above so we don't remove them
-                               vector<string> dupNames;
-                               m->splitAtComma(second, dupNames);
-                               
-                               for (int i = 0; i < dupNames.size(); i++) {     
-                                       nameMap[dupNames[i]] = dupNames[i]; 
-                                       if ((groupfile == "") && (i != 0)) { tmap->addSeq(dupNames[i], "Group1"); } 
-                               }
-                       }else {  m->mothurOut(first + " has already been seen in namefile, disregarding names file."); m->mothurOutEndLine(); in.close(); m->names.clear(); namefile = ""; return 1; }                  
-               }
-               in.close();
-               
-               return 0;
-       }
-       catch(exception& e) {
-               m->errorOut(e, "PhyloDiversityCommand", "readNamesFile");
-               exit(1);
-       }
-}
-
 //**********************************************************************************************************************
 
 
index b44e6caed27037ddb177acdd1416df40550dee5c..5d0cccf249099ac51d86b58e41e5098793ef3f3c 100644 (file)
@@ -12,9 +12,8 @@
 
 #include "command.hpp"
 #include "treemap.h"
-#include "readtree.h"
 #include "sharedutilities.h"
-
+#include "tree.h"
 
 class PhyloDiversityCommand : public Command {
        
@@ -33,14 +32,12 @@ class PhyloDiversityCommand : public Command {
                int execute();
                void help() { m->mothurOut(getHelpString()); }
 private:
-               ReadTree* read;
                TreeMap* tmap;
                float freq;
                int iters, processors, numUniquesInName;  
                bool abort, rarefy, summary, collect, scale;
                string groups, outputDir, treefile, groupfile, namefile;
                vector<string> Groups, outputNames; //holds groups to be used, and outputFile names
-               map<string, string> nameMap;
                
                int readNamesFile();
                void printData(set<int>&, map< string, vector<float> >&, ofstream&, int);
index 74b4268a8d75997edc28f4795866177057f9f813..6fa4c3da814347cc60c0807bd16cb373fff8c962 100644 (file)
@@ -20,12 +20,12 @@ ReadTree::ReadTree() {
        }
 }
 /***********************************************************************/
-int ReadTree::AssembleTrees() {
+int ReadTree::AssembleTrees(map<string, string> nameMap) {
         try {
                 //assemble users trees
                 for (int i = 0; i < Trees.size(); i++) {
                         if (m->control_pressed) { return 0;  }
-                        Trees[i]->assembleTree();
+                        Trees[i]->assembleTree(nameMap);
                 }
                 return 0;
         }
index b5b26ed0b81ee7d3ad5a87a3515e54881b5daf78..6b074de839070a86bd3e010bbf222a7ec4011dce 100644 (file)
@@ -30,7 +30,7 @@ class ReadTree {
                float readBranchLength(istream& f);
        
                vector<Tree*> getTrees() { return Trees; }
-               int AssembleTrees();
+               int AssembleTrees(map<string, string>);
                
        protected:
                vector<Tree*> Trees;
index 49674f57ea9c11a46bd987e587e3678b10fb1c4b..801029c06383e7811e5cfb14ea0c5cd629b66b67 100644 (file)
@@ -465,7 +465,6 @@ int RemoveGroupsCommand::readShared(){
                delete tempInput;
                m->setGroups(groupsToKeep);
                m->clearAllGroups();
-               m->names.clear();
                m->saveNextLabel = "";
                m->printedHeaders = false;
                m->currentBinLabels.clear();
index 432811ecaabf1e50161061e291985ca080ed7773..7c9671bf9a91e02b23cfd40b44804c38e3e4354d 100644 (file)
--- a/tree.cpp
+++ b/tree.cpp
@@ -16,7 +16,7 @@ Tree::Tree(int num, TreeMap* t) : tmap(t) {
                
                numLeaves = num;  
                numNodes = 2*numLeaves - 1;
-               
+        
                tree.resize(numNodes);
        }
        catch(exception& e) {
@@ -28,9 +28,6 @@ Tree::Tree(int num, TreeMap* t) : tmap(t) {
 Tree::Tree(string g) { //do not use tree generated by this its just to extract the treenames, its a chicken before the egg thing that needs to be revisited.
        try {
                m = MothurOut::getInstance();
-               
-               tmap = NULL;
-               
                parseTreeFile();  m->runParse = false;  
        }
        catch(exception& e) {
@@ -196,7 +193,8 @@ Tree::Tree(TreeMap* t, vector< vector<double> >& sims) : tmap(t) {
                //adjust tree to make sure root to tip length is .5
                int root = findRoot();
                tree[root].setBranchLength((0.5 - tree[root].getLengthToLeaves()));
-       }
+        
+    }
        catch(exception& e) {
                m->errorOut(e, "Tree", "Tree");
                exit(1);
@@ -329,12 +327,13 @@ void Tree::setIndex(string searchName, int index) {
        }
 }
 /*****************************************************************/
-int Tree::assembleTree() {
+int Tree::assembleTree(map<string, string> nameMap) {
        try {
-               //float A = clock();
+               //save for later
+        names = nameMap;
 
                //if user has given a names file we want to include that info in the pgroups and pcount info.
-               if(m->names.size() != 0) {  addNamesToCounts(m->names);  }
+               if(nameMap.size() != 0) {  addNamesToCounts(nameMap);  }
                
                //build the pGroups in non leaf nodes to be used in the parsimony calcs.
                for (int i = numLeaves; i < numNodes; i++) {
@@ -343,8 +342,7 @@ int Tree::assembleTree() {
                        tree[i].pGroups = (mergeGroups(i));
                        tree[i].pcount = (mergeGcounts(i));
                }
-               //float B = clock();
-               //cout << "assembleTree\t" << (B-A) / CLOCKS_PER_SEC << endl;
+               
                return 0;
        }
        catch(exception& e) {
@@ -352,7 +350,7 @@ int Tree::assembleTree() {
                exit(1);
        }
 }
-/*****************************************************************/
+/*****************************************************************
 int Tree::assembleTree(string n) {
        try {
                
@@ -380,7 +378,8 @@ void Tree::getSubTree(Tree* Ctree, vector<string> Groups) {
         //copy Tree since we are going to destroy it
         Tree* copy = new Tree(tmap);
         copy->getCopy(Ctree);
-        copy->assembleTree("nonames");
+        map<string, string> empty;
+        copy->assembleTree(empty);
         
                //we want to select some of the leaf nodes to create the output tree
                //go through the input Tree starting at parents of leaves
@@ -840,21 +839,23 @@ void Tree::randomBlengths()  {
 /*************************************************************************************************/
 void Tree::assembleRandomUnifracTree(vector<string> g) {
        randomLabels(g);
-       assembleTree("noNameCounts");
+    map<string, string> empty;
+       assembleTree(empty);
 }
 /*************************************************************************************************/
 void Tree::assembleRandomUnifracTree(string groupA, string groupB) {
-
        vector<string> temp; temp.push_back(groupA); temp.push_back(groupB);
        randomLabels(temp);
-       assembleTree("noNameCounts");
+    map<string, string> empty;
+       assembleTree(empty);
 }
 
 /*************************************************************************************************/
 //for now it's just random topology but may become random labels as well later that why this is such a simple function now...
 void Tree::assembleRandomTree() {
        randomTopology();
-       assembleTree();
+    map<string, string> empty;
+       assembleTree(empty);
 }
 /**************************************************************************************************/
 
@@ -907,6 +908,18 @@ void Tree::print(ostream& out) {
        }
 }
 /*****************************************************************/
+void Tree::print(ostream& out, map<string, string> nameMap) {
+       try {
+               int root = findRoot();
+               printBranch(root, out, nameMap);
+               out << ";" << endl;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "Tree", "print");
+               exit(1);
+       }
+}
+/*****************************************************************/
 void Tree::print(ostream& out, string mode) {
        try {
                int root = findRoot();
@@ -959,10 +972,82 @@ int Tree::findRoot() {
        }
 }
 /*****************************************************************/
-void Tree::printBranch(int node, ostream& out, string mode) {
+void Tree::printBranch(int node, ostream& out, map<string, string> names) {
 try {
 
 // you are not a leaf
+               if (tree[node].getLChild() != -1) {
+                       out << "(";
+                       printBranch(tree[node].getLChild(), out, names);
+                       out << ",";
+                       printBranch(tree[node].getRChild(), out, names);
+                       out << ")";
+                       
+            //if there is a branch length then print it
+            if (tree[node].getBranchLength() != -1) {
+                out << ":" << tree[node].getBranchLength();
+            }
+                       
+               }else { //you are a leaf
+            map<string, string>::iterator itNames = names.find(tree[node].getName());
+            
+            string outputString = "";
+            if (itNames != names.end()) { 
+                
+                vector<string> dupNames;
+                m->splitAtComma((itNames->second), dupNames);
+                
+                if (dupNames.size() == 1) {
+                    outputString += tree[node].getName();
+                    if (tree[node].getBranchLength() != -1) {
+                        outputString += ":" + toString(tree[node].getBranchLength());
+                    }
+                }else {
+                    outputString += "(";
+                    
+                    for (int u = 0; u < dupNames.size()-1; u++) {
+                        outputString += dupNames[u];
+                        
+                        if (tree[node].getBranchLength() != -1) {
+                            outputString += ":" + toString(0.0);
+                        }
+                        outputString += ",";
+                    }
+                    
+                    outputString += dupNames[dupNames.size()-1];
+                    if (tree[node].getBranchLength() != -1) {
+                        outputString += ":" + toString(0.0);
+                    }
+                    
+                    outputString += ")";
+                    if (tree[node].getBranchLength() != -1) {
+                        outputString += ":" + toString(tree[node].getBranchLength());
+                    }
+                }
+            }else { 
+                outputString = tree[node].getName();
+                //if there is a branch length then print it
+                if (tree[node].getBranchLength() != -1) {
+                    outputString += ":" + toString(tree[node].getBranchLength());
+                }
+                
+                m->mothurOut("[ERROR]: " + tree[node].getName() + " is not in your namefile, please correct."); m->mothurOutEndLine(); 
+            }
+                
+            out << outputString;
+               }
+               
+       }
+       catch(exception& e) {
+               m->errorOut(e, "Tree", "printBranch");
+               exit(1);
+       }
+}
+/*****************************************************************/
+void Tree::printBranch(int node, ostream& out, string mode) {
+    try {
+        
+        // you are not a leaf
                if (tree[node].getLChild() != -1) {
                        out << "(";
                        printBranch(tree[node].getLChild(), out, mode);
@@ -987,11 +1072,6 @@ try {
                                if (tree[node].getBranchLength() != -1) {
                                        out << ":" << tree[node].getBranchLength();
                                }
-                       }else if (mode == "deunique") {
-                               //if there is a branch length then print it
-                               if (tree[node].getBranchLength() != -1) {
-                                       out << ":" << tree[node].getBranchLength();
-                               }
                        }
                }else { //you are a leaf
                        string leafGroup = tmap->getGroup(tree[node].getName());
@@ -1017,53 +1097,6 @@ try {
                                if (tree[node].getBranchLength() != -1) {
                                        out << ":" << tree[node].getBranchLength();
                                }
-                       }else if (mode == "deunique") {
-                               map<string, string>::iterator itNames = m->names.find(tree[node].getName());
-                               
-                               string outputString = "";
-                               if (itNames != m->names.end()) { 
-                                       
-                                       vector<string> dupNames;
-                                       m->splitAtComma((itNames->second), dupNames);
-                                       
-                                       if (dupNames.size() == 1) {
-                                               outputString += tree[node].getName();
-                                               if (tree[node].getBranchLength() != -1) {
-                                                       outputString += ":" + toString(tree[node].getBranchLength());
-                                               }
-                                       }else {
-                                               outputString += "(";
-                                               
-                                               for (int u = 0; u < dupNames.size()-1; u++) {
-                                                       outputString += dupNames[u];
-                                                       
-                                                       if (tree[node].getBranchLength() != -1) {
-                                                               outputString += ":" + toString(0.0);
-                                                       }
-                                                       outputString += ",";
-                                               }
-                                               
-                                               outputString += dupNames[dupNames.size()-1];
-                                               if (tree[node].getBranchLength() != -1) {
-                                                       outputString += ":" + toString(0.0);
-                                               }
-                                               
-                                               outputString += ")";
-                                               if (tree[node].getBranchLength() != -1) {
-                                                       outputString += ":" + toString(tree[node].getBranchLength());
-                                               }
-                                       }
-                               }else { 
-                                       outputString = tree[node].getName();
-                                       //if there is a branch length then print it
-                                       if (tree[node].getBranchLength() != -1) {
-                                               outputString += ":" + toString(tree[node].getBranchLength());
-                                       }
-                                       
-                                       m->mothurOut("[ERROR]: " + tree[node].getName() + " is not in your namefile, please correct."); m->mothurOutEndLine(); 
-                               }
-                                       
-                               out << outputString;
                        }
                }
                
diff --git a/tree.h b/tree.h
index 0b61c6e5db4e2eb09638aa44d6b84ccbb07ce1c8..0660e8a181632ae09668d1484236276e5898ed39 100644 (file)
--- a/tree.h
+++ b/tree.h
@@ -22,6 +22,7 @@ public:
     Tree(TreeMap*, vector< vector<double> >&); //create tree from sim matrix
        ~Tree();
        
+    TreeMap* getTreeMap() { return tmap; }
        void getCopy(Tree*);  //makes tree a copy of the one passed in.
        void getSubTree(Tree*, vector<string>);  //makes tree a that contains only the names passed in.
     int getSubTree(Tree* originalToCopy, vector<string> seqToInclude, map<string, string> nameMap);  //used with (int, TreeMap) constructor. SeqsToInclude contains subsample wanted - assumes these are unique seqs and size of vector=numLeaves passed into constructor. nameMap is unique -> redundantList can be empty if no namesfile was provided. 
@@ -39,11 +40,11 @@ public:
        void printTree();
        void print(ostream&);
        void print(ostream&, string);
+    void print(ostream&, map<string, string>);
        int findRoot();  //return index of root node
        
        //this function takes the leaf info and populates the non leaf nodes
-       int assembleTree();     
-       int assembleTree(string);       
+       int assembleTree(map<string, string>);  
        
        vector<Node> tree;              //the first n nodes are the leaves, where n is the number of sequences.
        map< string, vector<int> > groupNodeInfo;       //maps group to indexes of leaf nodes with that group, different groups may contain same node because of names file.
@@ -54,6 +55,7 @@ private:
        ofstream out;
        string filename;
        
+    map<string, string> names;
        map<string, int>::iterator it, it2;
        map<string, int> mergeGroups(int);  //returns a map with a groupname and the number of times that group was seen in the children
        map<string,int> mergeGcounts(int);
@@ -63,7 +65,8 @@ private:
        void randomBlengths();
        void randomLabels(vector<string>);
        //void randomLabels(string, string);
-       void printBranch(int, ostream&, string);  //recursively print out tree
+       void printBranch(int, ostream&, map<string, string>);  //recursively print out tree
+    void printBranch(int, ostream&, string);
        void parseTreeFile();   //parses through tree file to find names of nodes and number of them
                                                        //this is required in case user has sequences in the names file that are
                                                        //not included in the tree. 
index 4a77211d2bd5e8d90585a9f19e6383db6e5f16fc..3eeeae292e674dbc7823522ce8e35d8d531ab70e 100644 (file)
@@ -484,74 +484,13 @@ Tree* TreeGroupCommand::createTree(vector< vector<double> >& simMatrix){
                //create tree
                t = new Tree(tmap, simMatrix);
         
-       /* //initialize index
-        map<int, int> index;  //maps row in simMatrix to vector index in the tree
-        for (int g = 0; g < numGroups; g++) {  index[g] = g;   }
+        if (m->control_pressed) { delete t; t = NULL; return t; }
                
-               //do merges and create tree structure by setting parents and children
-               //there are numGroups - 1 merges to do
-               for (int i = 0; i < (numGroups - 1); i++) {
-                       float largest = -1000.0;
-                       
-                       if (m->control_pressed) { delete t; t = NULL; return t; }
-                       
-                       int row, column;
-                       //find largest value in sims matrix by searching lower triangle
-                       for (int j = 1; j < simMatrix.size(); j++) {
-                               for (int k = 0; k < j; k++) {
-                                       if (simMatrix[j][k] > largest) {  largest = simMatrix[j][k]; row = j; column = k;  }
-                               }
-                       }
+        //assemble tree
+        map<string, string> empty;
+               t->assembleTree(empty);
 
-                       //set non-leaf node info and update leaves to know their parents
-                       //non-leaf
-                       t->tree[numGroups + i].setChildren(index[row], index[column]);
-                       
-                       //parents
-                       t->tree[index[row]].setParent(numGroups + i);
-                       t->tree[index[column]].setParent(numGroups + i);
-                       
-                       //blength = distance / 2;
-                       float blength = ((1.0 - largest) / 2);
-                       
-                       //branchlengths
-                       t->tree[index[row]].setBranchLength(blength - t->tree[index[row]].getLengthToLeaves());
-                       t->tree[index[column]].setBranchLength(blength - t->tree[index[column]].getLengthToLeaves());
-                       
-                       //set your length to leaves to your childs length plus branchlength
-                       t->tree[numGroups + i].setLengthToLeaves(t->tree[index[row]].getLengthToLeaves() + t->tree[index[row]].getBranchLength());
-                       
-                       
-                       //update index 
-                       index[row] = numGroups+i;
-                       index[column] = numGroups+i;
-                       
-                       //remove highest value that caused the merge.
-                       simMatrix[row][column] = -1000.0;
-                       simMatrix[column][row] = -1000.0;
-                       
-                       //merge values in simsMatrix
-                       for (int n = 0; n < simMatrix.size(); n++)      {
-                               //row becomes merge of 2 groups
-                               simMatrix[row][n] = (simMatrix[row][n] + simMatrix[column][n]) / 2;
-                               simMatrix[n][row] = simMatrix[row][n];
-                               //delete column
-                               simMatrix[column][n] = -1000.0;
-                               simMatrix[n][column] = -1000.0;
-                       }
-               }
-               
-               //adjust tree to make sure root to tip length is .5
-               int root = t->findRoot();
-               t->tree[root].setBranchLength((0.5 - t->tree[root].getLengthToLeaves()));
-               */
-               //assemble tree
-               t->assembleTree();
-               
-               if (m->control_pressed) { delete t; t = NULL; return t; }
-               
                return t;
-       
        }
        catch(exception& e) {
                m->errorOut(e, "TreeGroupCommand", "createTree");
@@ -1006,7 +945,7 @@ int TreeGroupCommand::process(vector<SharedRAbundVector*> thisLookup) {
                 Consensus consensus;
                 //clear old tree names if any
                 m->Treenames.clear(); m->Treenames = m->getGroups(); //may have changed if subsample eliminated groups
-                Tree* conTree = consensus.getTree(trees, tmap);
+                Tree* conTree = consensus.getTree(trees);
                 
                 //create a new filename
                 string conFile = outputDir + m->getRootName(m->getSimpleName(inputfile)) + treeCalculators[i]->getName() + "." + thisLookup[0]->getLabel() + ".cons.tre";                              
index 450d8fba67b8beafe7c824b6a18b479705b3875c..c228162aab1d511af3fc5318aaf3e5eb5b39610c 100644 (file)
 
 /************************************************************/
  TreeMap::~TreeMap(){}
+/************************************************************/
+int TreeMap::readMap(string gf) {
+    
+    groupFileName = gf;
+       m->openInputFile(gf, fileHandle);
+    
+    string seqName, seqGroup;
+    int error = 0;
+    
+    while(fileHandle){
+        fileHandle >> seqName;       m->gobble(fileHandle);    //read from first column
+        fileHandle >> seqGroup;                        //read from second column
+        
+        if (m->control_pressed) {  fileHandle.close();  return 1; }
+        
+        setNamesOfGroups(seqGroup);
+        
+        map<string, GroupIndex>::iterator itCheck = treemap.find(seqName);
+        if (itCheck != treemap.end()) { error = 1; m->mothurOut("[WARNING]: Your groupfile contains more than 1 sequence named " + seqName + ", sequence names must be unique. Please correct."); m->mothurOutEndLine();  }
+        else {
+            namesOfSeqs.push_back(seqName);
+            treemap[seqName].groupname = seqGroup;     //store data in map
+            
+            it2 = seqsPerGroup.find(seqGroup);
+            if (it2 == seqsPerGroup.end()) { //if it's a new group
+                seqsPerGroup[seqGroup] = 1;
+            }else {//it's a group we already have
+                seqsPerGroup[seqGroup]++;
+            }                          
+        }
+        
+        m->gobble(fileHandle);
+    }
+    fileHandle.close();
+    
+    return error;
+}
 
 /************************************************************/
 int TreeMap::readMap() {
@@ -26,7 +63,7 @@ int TreeMap::readMap() {
                int error = 0;
                
                while(fileHandle){
-                       fileHandle >> seqName;          //read from first column
+                       fileHandle >> seqName;           m->gobble(fileHandle); //read from first column
                        fileHandle >> seqGroup;                 //read from second column
                        
                        if (m->control_pressed) {  fileHandle.close();  return 1; }
@@ -230,14 +267,14 @@ void TreeMap::makeSim(ListVector* list) {
        }
 }
 /************************************************************/
-int TreeMap::getCopy(TreeMap* copy){
+int TreeMap::getCopy(TreeMap& copy){
        try {
          
-        namesOfGroups = copy->getNamesOfGroups();
-               numGroups = copy->getNumGroups();
-        namesOfSeqs = copy->namesOfSeqs;
-        seqsPerGroup = copy->seqsPerGroup;
-        treemap = copy->treemap;
+        namesOfGroups = copy.getNamesOfGroups();
+               numGroups = copy.getNumGroups();
+        namesOfSeqs = copy.namesOfSeqs;
+        seqsPerGroup = copy.seqsPerGroup;
+        treemap = copy.treemap;
         
         return 0;
        }
index fc9c3690fef8900ad3484147ae3fa0487a514b73..57822e02d4b8c3a57f7d0ddf26947c470bf9400b 100644 (file)
--- a/treemap.h
+++ b/treemap.h
@@ -10,7 +10,6 @@
  */
 
 #include "mothur.h"
-#include "groupmap.h"
 #include "listvector.hpp"
 
 /* This class is used by the read.tree command to build the tree container. */
@@ -20,15 +19,14 @@ struct GroupIndex {
        int             vectorIndex;
 };
 
-class GroupMap;
-class ListVector;
-
 class TreeMap {
 public:
        TreeMap() { m = MothurOut::getInstance(); }
        TreeMap(string);
        ~TreeMap();
+    
        int readMap();
+    int readMap(string);
        int getNumGroups();
        int getNumSeqs();
        void setIndex(string, int);  //sequencename, index
@@ -48,7 +46,7 @@ public:
        void makeSim(ListVector*);  //takes listvector info and fills treemap for use by tree.shared command.   
     vector<string> getNamesSeqs();
        vector<string> getNamesSeqs(vector<string>); //get names of seqs belonging to a group or set of groups
-    int getCopy(TreeMap*);
+    int getCopy(TreeMap&);
     
     vector<string> namesOfSeqs;
     map<string,int> seqsPerGroup;      //groupname, number of seqs in that group.
diff --git a/treereader.cpp b/treereader.cpp
new file mode 100644 (file)
index 0000000..b385d21
--- /dev/null
@@ -0,0 +1,158 @@
+//
+//  treereader.cpp
+//  Mothur
+//
+//  Created by Sarah Westcott on 4/11/12.
+//  Copyright (c) 2012 Schloss Lab. All rights reserved.
+//
+
+#include "treereader.h"
+#include "readtree.h"
+
+/***********************************************************************/
+
+TreeReader::TreeReader(string tf) : treefile(tf)  { 
+    try {
+        m = MothurOut::getInstance();
+        namefile = "";
+        groupfile = "";
+        readTrees();
+    }
+       catch(exception& e) {
+               m->errorOut(e, "TreeReader", "TreeReader");
+               exit(1);
+       }
+}
+/***********************************************************************/
+
+TreeReader::TreeReader(string tf, string gf) : treefile(tf),  groupfile(gf)  { 
+    try {
+        m = MothurOut::getInstance();
+        namefile = "";
+        readTrees();
+    }
+       catch(exception& e) {
+               m->errorOut(e, "TreeReader", "TreeReader");
+               exit(1);
+       }
+}
+/***********************************************************************/
+TreeReader::TreeReader(string tf, string gf, string nf) : treefile(tf),  groupfile(gf), namefile(nf)  { 
+    try {
+        m = MothurOut::getInstance();
+        readTrees();
+    }
+       catch(exception& e) {
+               m->errorOut(e, "TreeReader", "TreeReader");
+               exit(1);
+       }
+}
+/***********************************************************************/
+bool TreeReader::readTrees()  { 
+    try {
+        
+        tmap = new TreeMap();
+        if (groupfile != "") {      tmap->readMap(groupfile);        }
+               else{ //fake out by putting everyone in one group
+                       Tree* tree = new Tree(treefile); delete tree;  //extracts names from tree to make faked out groupmap
+                       for (int i = 0; i < m->Treenames.size(); i++) { tmap->addSeq(m->Treenames[i], "Group1"); }
+               }
+               
+        int numUniquesInName = 0;
+               if (namefile != "") { numUniquesInName = readNamesFile(); }
+               
+               ReadTree* read = new ReadNewickTree(treefile);
+               int readOk = read->read(tmap); 
+               
+               if (readOk != 0) { m->mothurOut("Read Terminated."); m->mothurOutEndLine();  delete read; m->control_pressed=true; return 0; }
+               
+               read->AssembleTrees(names);
+               trees = read->getTrees();
+               delete read;
+        
+               //make sure all files match
+               //if you provide a namefile we will use the numNames in the namefile as long as the number of unique match the tree names size.
+               int numNamesInTree;
+               if (namefile != "")  {  
+                       if (numUniquesInName == m->Treenames.size()) {  numNamesInTree = nameMap.size();  }
+                       else {   numNamesInTree = m->Treenames.size();  }
+               }else {  numNamesInTree = m->Treenames.size();  }
+               
+               
+               //output any names that are in group file but not in tree
+               if (numNamesInTree < tmap->getNumSeqs()) {
+                       for (int i = 0; i < tmap->namesOfSeqs.size(); i++) {
+                               //is that name in the tree?
+                               int count = 0;
+                               for (int j = 0; j < m->Treenames.size(); j++) {
+                                       if (tmap->namesOfSeqs[i] == m->Treenames[j]) { break; } //found it
+                                       count++;
+                               }
+                               
+                               if (m->control_pressed) { for (int i = 0; i < trees.size(); i++) { delete trees[i]; } return 0; }
+                               
+                               //then you did not find it so report it 
+                               if (count == m->Treenames.size()) { 
+                                       //if it is in your namefile then don't remove
+                                       map<string, string>::iterator it = nameMap.find(tmap->namesOfSeqs[i]);
+                                       
+                                       if (it == nameMap.end()) {
+                                               m->mothurOut(tmap->namesOfSeqs[i] + " is in your groupfile and not in your tree. It will be disregarded."); m->mothurOutEndLine();
+                                               tmap->removeSeq(tmap->namesOfSeqs[i]);
+                                               i--; //need this because removeSeq removes name from namesOfSeqs
+                                       }
+                               }
+                       }
+               }
+        
+        return true;
+    }
+       catch(exception& e) {
+               m->errorOut(e, "TreeReader", "readTrees");
+               exit(1);
+       }
+}
+/*****************************************************************/
+int TreeReader::readNamesFile() {
+       try {
+               nameMap.clear();
+        names.clear();
+               int numUniquesInName = 0;
+               
+               ifstream in;
+               m->openInputFile(namefile, in);
+               
+               string first, second;
+               map<string, string>::iterator itNames;
+               
+               while(!in.eof()) {
+                       in >> first >> second; m->gobble(in);
+                       
+                       numUniquesInName++;
+                       
+                       itNames = nameMap.find(first);
+                       if (itNames == nameMap.end()) {  
+                               names[first] = second; 
+                               
+                               //we need a list of names in your namefile to use above when removing extra seqs above so we don't remove them
+                               vector<string> dupNames;
+                               m->splitAtComma(second, dupNames);
+                               
+                               for (int i = 0; i < dupNames.size(); i++) {     
+                                       nameMap[dupNames[i]] = first; 
+                                       if ((groupfile == "") && (i != 0)) { tmap->addSeq(dupNames[i], "Group1"); } 
+                               }
+                       }else {  m->mothurOut(first + " has already been seen in namefile, disregarding names file."); m->mothurOutEndLine(); in.close(); nameMap.clear(); names.clear(); namefile = ""; return 1; }                    
+               }
+               in.close();
+               
+               return numUniquesInName;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "TreeReader", "readNamesFile");
+               exit(1);
+       }
+}
+/***********************************************************************/
+
+
diff --git a/treereader.h b/treereader.h
new file mode 100644 (file)
index 0000000..fb9c791
--- /dev/null
@@ -0,0 +1,44 @@
+#ifndef Mothur_treereader_h
+#define Mothur_treereader_h
+
+//
+//  treereader.h
+//  Mothur
+//
+//  Created by Sarah Westcott on 4/11/12.
+//  Copyright (c) 2012 Schloss Lab. All rights reserved.
+//
+
+#include "mothurout.h"
+#include "tree.h"
+
+class TreeReader {
+    
+public:
+    
+    TreeReader(string tf);
+       TreeReader(string tf, string gf);
+    TreeReader(string tf, string gf, string nf);
+       ~TreeReader() {}        
+    
+    vector<Tree*> getTrees()            { return trees;     }
+    map<string, string> getNames()      { return nameMap;   } //dups -> unique
+    map<string, string> getNameMap()    { return names;     } //unique -> dups list
+    
+    
+private:
+    MothurOut* m;
+       vector<Tree*> trees;
+    TreeMap* tmap;
+    map<string, string> nameMap; //dupName -> uniqueName
+    map<string, string> names;
+    
+    string treefile, groupfile, namefile;
+    
+    bool readTrees();
+    int readNamesFile();
+};
+
+
+
+#endif
index a404f79222ac83cf41d42c04ceb614df1e01dceb..de8cb5637fe64a5a63e88852a26935512ff53806 100644 (file)
@@ -8,6 +8,7 @@
  */
 
 #include "unifracunweightedcommand.h"
+#include "treereader.h"
 
 //**********************************************************************************************************************
 vector<string> UnifracUnweightedCommand::setParameters(){      
@@ -133,13 +134,7 @@ UnifracUnweightedCommand::UnifracUnweightedCommand(string option)  {
                                }
                        }
                        
-                       m->runParse = true;
-                       m->clearGroups();
-                       m->clearAllGroups();
-                       m->Treenames.clear();
-                       m->names.clear();
-                       
-                       //check for required parameters
+            //check for required parameters
                        treefile = validParameter.validFile(parameters, "tree", true);
                        if (treefile == "not open") { abort = true; }
                        else if (treefile == "not found") {                             //if there is a current design file, use it
@@ -220,83 +215,26 @@ int UnifracUnweightedCommand::execute() {
                
                m->setTreeFile(treefile);
                
-               if (groupfile != "") {
-                       //read in group map info.
-                       tmap = new TreeMap(groupfile);
-                       tmap->readMap();
-               }else{ //fake out by putting everyone in one group
-                       Tree* tree = new Tree(treefile); delete tree;  //extracts names from tree to make faked out groupmap
-                       tmap = new TreeMap();
-                       
-                       for (int i = 0; i < m->Treenames.size(); i++) { tmap->addSeq(m->Treenames[i], "Group1"); }
-               }
-               
-               if (namefile != "") { readNamesFile(); }
-               
-               read = new ReadNewickTree(treefile);
-               int readOk = read->read(tmap); 
-               
-               if (readOk != 0) { m->mothurOut("Read Terminated."); m->mothurOutEndLine(); delete tmap; delete read; return 0; }
-               
-               read->AssembleTrees();
-               T = read->getTrees();
-               delete read;
-               
-               //make sure all files match
-               //if you provide a namefile we will use the numNames in the namefile as long as the number of unique match the tree names size.
-               int numNamesInTree;
-               if (namefile != "")  {  
-                       if (numUniquesInName == m->Treenames.size()) {  numNamesInTree = nameMap.size();  }
-                       else {   numNamesInTree = m->Treenames.size();  }
-               }else {  numNamesInTree = m->Treenames.size();  }
-               
-               
-               //output any names that are in group file but not in tree
-               if (numNamesInTree < tmap->getNumSeqs()) {
-                       for (int i = 0; i < tmap->namesOfSeqs.size(); i++) {
-                               //is that name in the tree?
-                               int count = 0;
-                               for (int j = 0; j < m->Treenames.size(); j++) {
-                                       if (tmap->namesOfSeqs[i] == m->Treenames[j]) { break; } //found it
-                                       count++;
-                               }
-                               
-                               if (m->control_pressed) { 
-                                       delete tmap; for (int i = 0; i < T.size(); i++) { delete T[i]; }
-                                       for (int i = 0; i < outputNames.size(); i++) {  m->mothurRemove(outputNames[i]); } outputTypes.clear();
-                                       m->clearGroups();
-                                       return 0;
-                               }
-                               
-                               //then you did not find it so report it 
-                               if (count == m->Treenames.size()) { 
-                                       //if it is in your namefile then don't remove
-                                       map<string, string>::iterator it = nameMap.find(tmap->namesOfSeqs[i]);
-                                       
-                                       if (it == nameMap.end()) {
-                                               m->mothurOut(tmap->namesOfSeqs[i] + " is in your groupfile and not in your tree. It will be disregarded."); m->mothurOutEndLine();
-                                               tmap->removeSeq(tmap->namesOfSeqs[i]);
-                                               i--; //need this because removeSeq removes name from namesOfSeqs
-                                       }
-                               }
-                       }
-               }
-       
+               TreeReader* reader = new TreeReader(treefile, groupfile, namefile);
+        T = reader->getTrees();
+        tmap = T[0]->getTreeMap();
+        map<string, string> nameMap = reader->getNameMap();
+        delete reader; 
+        
                sumFile = outputDir + m->getSimpleName(treefile) + ".uwsummary";
                outputNames.push_back(sumFile); outputTypes["uwsummary"].push_back(sumFile);
                m->openOutputFile(sumFile, outSum);
                
-               util = new SharedUtil();
+               SharedUtil util;
                Groups = m->getGroups();
                vector<string> namesGroups = tmap->getNamesOfGroups();
-               util->setGroups(Groups, namesGroups, allGroups, numGroups, "unweighted");       //sets the groups the user wants to analyze
-               util->getCombos(groupComb, Groups, numComp);
+               util.setGroups(Groups, namesGroups, allGroups, numGroups, "unweighted");        //sets the groups the user wants to analyze
+               util.getCombos(groupComb, Groups, numComp);
                m->setGroups(Groups);
-               delete util;
        
                if (numGroups == 1) { numComp++; groupComb.push_back(allGroups); }
                
-               unweighted = new Unweighted(tmap, includeRoot);
+               Unweighted unweighted(includeRoot);
                
                int start = time(NULL);
                
@@ -314,7 +252,7 @@ int UnifracUnweightedCommand::execute() {
                //get pscores for users trees
                for (int i = 0; i < T.size(); i++) {
                        if (m->control_pressed) { 
-                               delete tmap; delete unweighted;
+                               delete tmap;
                                for (int i = 0; i < T.size(); i++) { delete T[i]; }
                                outSum.close();
                                for (int i = 0; i < outputNames.size(); i++) {  m->mothurRemove(outputNames[i]);  }
@@ -336,9 +274,9 @@ int UnifracUnweightedCommand::execute() {
                        utreeScores.resize(numComp);  
                        UWScoreSig.resize(numComp); 
 
-                       userData = unweighted->getValues(T[i], processors, outputDir);  //userData[0] = unweightedscore
+                       userData = unweighted.getValues(T[i], processors, outputDir);  //userData[0] = unweightedscore
                
-                       if (m->control_pressed) { delete tmap; delete unweighted;
+                       if (m->control_pressed) { delete tmap; 
                                for (int i = 0; i < T.size(); i++) { delete T[i]; }if (random) { delete output;  } outSum.close();  for (int i = 0; i < outputNames.size(); i++) {      m->mothurRemove(outputNames[i]);  }return 0; }
                        
                        //output scores for each combination
@@ -354,9 +292,9 @@ int UnifracUnweightedCommand::execute() {
                        for (int j = 0; j < iters; j++) {
                
                                //we need a different getValues because when we swap the labels we only want to swap those in each pairwise comparison
-                               randomData = unweighted->getValues(T[i], "", "", processors, outputDir);
+                               randomData = unweighted.getValues(T[i], "", "", processors, outputDir);
                                
-                               if (m->control_pressed) { delete tmap; delete unweighted;
+                               if (m->control_pressed) { delete tmap; 
                                        for (int i = 0; i < T.size(); i++) { delete T[i]; }if (random) { delete output;  } outSum.close(); for (int i = 0; i < outputNames.size(); i++) {       m->mothurRemove(outputNames[i]);  } return 0; }
                        
                                for(int k = 0; k < numComp; k++) {      
@@ -394,7 +332,7 @@ int UnifracUnweightedCommand::execute() {
        
                        }
                        
-                       if (m->control_pressed) { delete tmap; delete unweighted;
+                       if (m->control_pressed) { delete tmap; 
                                for (int i = 0; i < T.size(); i++) { delete T[i]; }if (random) { delete output;  } outSum.close(); for (int i = 0; i < outputNames.size(); i++) {       m->mothurRemove(outputNames[i]);  } return 0;  }
                        
                        //print output files
@@ -411,8 +349,7 @@ int UnifracUnweightedCommand::execute() {
                
 
                outSum.close();
-               m->clearGroups();
-               delete tmap; delete unweighted;
+               delete tmap; 
                for (int i = 0; i < T.size(); i++) { delete T[i]; }
                
                if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        m->mothurRemove(outputNames[i]);  }     return 0; }
@@ -581,45 +518,6 @@ void UnifracUnweightedCommand::createPhylipFile(int i) {
                m->errorOut(e, "UnifracUnweightedCommand", "createPhylipFile");
                exit(1);
        }
-}/*****************************************************************/
-int UnifracUnweightedCommand::readNamesFile() {
-       try {
-               m->names.clear();
-               numUniquesInName = 0;
-               
-               ifstream in;
-               m->openInputFile(namefile, in);
-               
-               string first, second;
-               map<string, string>::iterator itNames;
-               
-               while(!in.eof()) {
-                       in >> first >> second; m->gobble(in);
-                       
-                       numUniquesInName++;
-                       
-                       itNames = m->names.find(first);
-                       if (itNames == m->names.end()) {  
-                               m->names[first] = second; 
-                               
-                               //we need a list of names in your namefile to use above when removing extra seqs above so we don't remove them
-                               vector<string> dupNames;
-                               m->splitAtComma(second, dupNames);
-                               
-                               for (int i = 0; i < dupNames.size(); i++) {     
-                                       nameMap[dupNames[i]] = dupNames[i]; 
-                                       if ((groupfile == "") && (i != 0)) { tmap->addSeq(dupNames[i], "Group1"); } 
-                               }
-                       }else {  m->mothurOut(first + " has already been seen in namefile, disregarding names file."); m->mothurOutEndLine(); in.close(); m->names.clear(); namefile = ""; return 1; }                  
-               }
-               in.close();
-               
-               return 0;
-       }
-       catch(exception& e) {
-               m->errorOut(e, "UnifracUnweightedCommand", "readNamesFile");
-               exit(1);
-       }
 }
 /***********************************************************/
 
index cd8d51dcad76a44dbc0ca064bd79a5ef3e0fbe80..8b243964fe4c427768a7c8076f58daf975caa99f 100644 (file)
@@ -36,12 +36,9 @@ class UnifracUnweightedCommand : public Command {
        
        
        private:
-               ReadTree* read;
-               SharedUtil* util;
                FileOutput* output;
                vector<Tree*> T;           //user trees
                TreeMap* tmap;
-               Unweighted* unweighted;
                string sumFile, allGroups;
                vector<string> groupComb; // AB. AC, BC...
                int iters, numGroups, numComp, counter, processors, numUniquesInName;
@@ -59,13 +56,10 @@ class UnifracUnweightedCommand : public Command {
 
                ofstream outSum, out;
                ifstream inFile;
-               map<string, string> nameMap;
                
                void printUWSummaryFile(int);
                void printUnweightedFile();
                void createPhylipFile(int);
-               int readNamesFile();
-                
                
 };
 
index e596db230ee24d80597cfa903a49ed9ee6a7f25b..a4a1bc3defc98beb608b38645471e46541a0b10f 100644 (file)
@@ -10,6 +10,7 @@
 #include "unifracweightedcommand.h"
 #include "consensus.h"
 #include "subsample.h"
+#include "treereader.h"
 
 //**********************************************************************************************************************
 vector<string> UnifracWeightedCommand::setParameters(){        
@@ -141,12 +142,6 @@ UnifracWeightedCommand::UnifracWeightedCommand(string option) {
                                }
                        }
                        
-                       m->runParse = true;
-                       m->clearGroups();
-                       m->clearAllGroups();
-                       m->Treenames.clear();
-                       m->names.clear();
-                       
                        //check for required parameters
                        treefile = validParameter.validFile(parameters, "tree", true);
                        if (treefile == "not open") { treefile = ""; abort = true; }
@@ -238,7 +233,13 @@ int UnifracWeightedCommand::execute() {
                
                m->setTreeFile(treefile);
                
-        readTrees();  if (m->control_pressed) {  delete tmap; for (int i = 0; i < T.size(); i++) { delete T[i]; } return 0; }
+        TreeReader* reader = new TreeReader(treefile, groupfile, namefile);
+        T = reader->getTrees();
+        tmap = T[0]->getTreeMap();
+        map<string, string> nameMap = reader->getNames();
+        delete reader;
+    
+        if (m->control_pressed) {  delete tmap; for (int i = 0; i < T.size(); i++) { delete T[i]; } return 0; }
                                
                sumFile = outputDir + m->getSimpleName(treefile) + ".wsummary";
                m->openOutputFile(sumFile, outSum);
@@ -253,7 +254,7 @@ int UnifracWeightedCommand::execute() {
                
         if (m->control_pressed) {  delete tmap; for (int i = 0; i < T.size(); i++) { delete T[i]; } return 0; }
         
-               Weighted weighted(tmap, includeRoot);
+               Weighted weighted(includeRoot);
                        
                int start = time(NULL);
             
@@ -333,16 +334,16 @@ int UnifracWeightedCommand::execute() {
                 
                 if (m->control_pressed) { break; }
                 
-                //copy to preserve old one - would do this in subsample but tree needs it and memory cleanup becomes messy.
+                //copy to preserve old one - would do this in subsample but memory cleanup becomes messy.
                 TreeMap* newTmap = new TreeMap();
-                newTmap->getCopy(tmap);
+                newTmap->getCopy(*tmap);
                 
                 SubSample sample;
                 Tree* subSampleTree = sample.getSample(T[i], newTmap, nameMap, subsampleSize);
                    
                 //call new weighted function
                 vector<double> iterData; iterData.resize(numComp,0);
-                Weighted thisWeighted(newTmap, includeRoot);
+                Weighted thisWeighted(includeRoot);
                 iterData = thisWeighted.getValues(subSampleTree, processors, outputDir); //userData[0] = weightedscore
                 
                 //save data to make ave dist, std dist
@@ -350,6 +351,8 @@ int UnifracWeightedCommand::execute() {
                 
                 delete newTmap;
                 delete subSampleTree;
+                
+                if((thisIter+1) % 100 == 0){   m->mothurOut(toString(thisIter+1)); m->mothurOutEndLine();              }
             }
             
             if (m->control_pressed) { delete tmap; for (int i = 0; i < T.size(); i++) { delete T[i]; } if (random) { delete output; } outSum.close(); for (int i = 0; i < outputNames.size(); i++) {   m->mothurRemove(outputNames[i]);  } return 0; }
@@ -525,8 +528,8 @@ int UnifracWeightedCommand::getConsensusTrees(vector< vector<double> >& dists, i
         m->runParse = false;
         
         //create treemap class from groupmap for tree class to use
-        TreeMap* newTmap = new TreeMap();
-        newTmap->makeSim(m->getGroups());
+        TreeMap newTmap;
+        newTmap.makeSim(m->getGroups());
         
         //clear  old tree names if any
         m->Treenames.clear();
@@ -536,10 +539,10 @@ int UnifracWeightedCommand::getConsensusTrees(vector< vector<double> >& dists, i
         
         vector<Tree*> newTrees = buildTrees(dists, treeNum, newTmap); //also creates .all.tre file containing the trees created
         
-        if (m->control_pressed) { delete newTmap; return 0; }
+        if (m->control_pressed) { return 0; }
         
         Consensus con;
-        Tree* conTree = con.getTree(newTrees, newTmap);
+        Tree* conTree = con.getTree(newTrees);
         
         //create a new filename
         string conFile = outputDir + m->getRootName(m->getSimpleName(treefile)) + toString(treeNum+1) + ".weighted.cons.tre";                          
@@ -549,7 +552,6 @@ int UnifracWeightedCommand::getConsensusTrees(vector< vector<double> >& dists, i
         
         if (conTree != NULL) { conTree->print(outTree, "boot"); delete conTree; }
         outTree.close();
-        delete newTmap;
         
         return 0;
 
@@ -561,7 +563,7 @@ int UnifracWeightedCommand::getConsensusTrees(vector< vector<double> >& dists, i
 }
 /**************************************************************************************************/
 
-vector<Tree*> UnifracWeightedCommand::buildTrees(vector< vector<double> >& dists, int treeNum, TreeMap* mytmap) {
+vector<Tree*> UnifracWeightedCommand::buildTrees(vector< vector<double> >& dists, int treeNum, TreeMap& mytmap) {
        try {
         
         vector<Tree*> trees;
@@ -595,8 +597,9 @@ vector<Tree*> UnifracWeightedCommand::buildTrees(vector< vector<double> >& dists
                        }
 
             //create tree
-            Tree* tempTree = new Tree(mytmap, sims);
-            tempTree->assembleTree();
+            Tree* tempTree = new Tree(&mytmap, sims);
+            map<string, string> empty;
+            tempTree->assembleTree(empty);
             
             trees.push_back(tempTree);
             
@@ -617,80 +620,6 @@ vector<Tree*> UnifracWeightedCommand::buildTrees(vector< vector<double> >& dists
 }
 /**************************************************************************************************/
 
-int UnifracWeightedCommand::readTrees() {
-       try {
-        
-        if (groupfile != "") {
-                       //read in group map info.
-                       tmap = new TreeMap(groupfile);
-                       tmap->readMap();
-               }else{ //fake out by putting everyone in one group
-                       Tree* tree = new Tree(treefile); delete tree;  //extracts names from tree to make faked out groupmap
-                       tmap = new TreeMap();
-                       
-                       for (int i = 0; i < m->Treenames.size(); i++) { tmap->addSeq(m->Treenames[i], "Group1"); }
-               }
-               
-               if (namefile != "") { readNamesFile(); }
-               
-               read = new ReadNewickTree(treefile);
-               int readOk = read->read(tmap); 
-               
-               if (readOk != 0) { m->mothurOut("Read Terminated."); m->mothurOutEndLine(); delete tmap; delete read; return 0; }
-               
-               read->AssembleTrees();
-               T = read->getTrees();
-               delete read;
-               
-               //make sure all files match
-               //if you provide a namefile we will use the numNames in the namefile as long as the number of unique match the tree names size.
-               int numNamesInTree;
-               if (namefile != "")  {  
-                       if (numUniquesInName == m->Treenames.size()) {  numNamesInTree = nameMap.size();  }
-                       else {   numNamesInTree = m->Treenames.size();  }
-               }else {  numNamesInTree = m->Treenames.size();  }
-               
-               
-               //output any names that are in group file but not in tree
-               if (numNamesInTree < tmap->getNumSeqs()) {
-                       for (int i = 0; i < tmap->namesOfSeqs.size(); i++) {
-                               //is that name in the tree?
-                               int count = 0;
-                               for (int j = 0; j < m->Treenames.size(); j++) {
-                                       if (tmap->namesOfSeqs[i] == m->Treenames[j]) { break; } //found it
-                                       count++;
-                               }
-                               
-                               if (m->control_pressed) { 
-                                       delete tmap; for (int i = 0; i < T.size(); i++) { delete T[i]; }
-                                       for (int i = 0; i < outputNames.size(); i++) {  m->mothurRemove(outputNames[i]); } outputTypes.clear();
-                                       m->clearGroups();
-                                       return 0;
-                               }
-                               
-                               //then you did not find it so report it 
-                               if (count == m->Treenames.size()) { 
-                                       //if it is in your namefile then don't remove
-                                       map<string, string>::iterator it = nameMap.find(tmap->namesOfSeqs[i]);
-                                       
-                                       if (it == nameMap.end()) {
-                                               m->mothurOut(tmap->namesOfSeqs[i] + " is in your groupfile and not in your tree. It will be disregarded."); m->mothurOutEndLine();
-                                               tmap->removeSeq(tmap->namesOfSeqs[i]);
-                                               i--; //need this because removeSeq removes name from namesOfSeqs
-                                       }
-                               }
-                       }
-               }
-
-        return 0;
-    }
-       catch(exception& e) {
-               m->errorOut(e, "UnifracWeightedCommand", "readTrees");
-               exit(1);
-       }
-}
-/**************************************************************************************************/
-
 int UnifracWeightedCommand::runRandomCalcs(Tree* thisTree, vector<double> usersScores) {
        try {
         
@@ -839,7 +768,7 @@ int UnifracWeightedCommand::driver(Tree* t, vector< vector<string> > namesOfGrou
  try {
                Tree* randT = new Tree(tmap);
      
-        Weighted weighted(tmap, includeRoot);
+        Weighted weighted(includeRoot);
      
                for (int h = start; h < (start+num); h++) {
        
@@ -1079,46 +1008,6 @@ void UnifracWeightedCommand::calculateFreqsCumuls() {
                exit(1);
        }
 }
-/*****************************************************************/
-int UnifracWeightedCommand::readNamesFile() {
-       try {
-               m->names.clear();
-               numUniquesInName = 0;
-               
-               ifstream in;
-               m->openInputFile(namefile, in);
-               
-               string first, second;
-               map<string, string>::iterator itNames;
-               
-               while(!in.eof()) {
-                       in >> first >> second; m->gobble(in);
-                       
-                       numUniquesInName++;
-                       
-                       itNames = m->names.find(first);
-                       if (itNames == m->names.end()) {  
-                               m->names[first] = second; 
-                               
-                               //we need a list of names in your namefile to use above when removing extra seqs above so we don't remove them
-                               vector<string> dupNames;
-                               m->splitAtComma(second, dupNames);
-                               
-                               for (int i = 0; i < dupNames.size(); i++) {     
-                                       nameMap[dupNames[i]] = first; 
-                                       if ((groupfile == "") && (i != 0)) { tmap->addSeq(dupNames[i], "Group1"); } 
-                               }
-                       }else {  m->mothurOut(first + " has already been seen in namefile, disregarding names file."); m->mothurOutEndLine(); in.close(); m->names.clear(); namefile = ""; return 1; }                  
-               }
-               in.close();
-               
-               return 0;
-       }
-       catch(exception& e) {
-               m->errorOut(e, "UnifracWeightedCommand", "readNamesFile");
-               exit(1);
-       }
-}
 /***********************************************************/
 
 
index e1ebd16c6151e373eb86971f53295d2036b4e4c2..ddbdc8548dad7e0545d75f8e98cd92fdb76a3419 100644 (file)
@@ -42,15 +42,12 @@ class UnifracWeightedCommand : public Command {
                        linePair(int i, int j) : start(i), num(j) {}
                };
                vector<linePair> lines;
-               
-               ReadTree* read;
-               SharedUtil* util;
+        TreeMap* tmap;
                FileOutput* output;
                vector<Tree*> T;           //user trees
                vector<double> utreeScores;  //user tree unweighted scores
                vector<double> WScoreSig;  //tree weighted score signifigance when compared to random trees - percentage of random trees with that score or lower.
                vector<string> groupComb; // AB. AC, BC...
-               TreeMap* tmap;
                string sumFile, outputDir;
                int iters, numGroups, numComp, counter;
                vector< vector<double> > rScores;  //vector<weighted scores for random trees.> each group comb has an entry
@@ -74,10 +71,8 @@ class UnifracWeightedCommand : public Command {
                void calculateFreqsCumuls();
                int createProcesses(Tree*,  vector< vector<string> >,  vector< vector<double> >&);
                int driver(Tree*, vector< vector<string> >, int, int,  vector< vector<double> >&);
-               int readNamesFile();
         int runRandomCalcs(Tree*, vector<double>);
-        int readTrees();
-        vector<Tree*> buildTrees(vector< vector<double> >&, int, TreeMap*);
+        vector<Tree*> buildTrees(vector< vector<double> >&, int, TreeMap&);
         int getConsensusTrees(vector< vector<double> >&, int);
         int getAverageSTDMatrices(vector< vector<double> >&, int);
                
index d4fd32731336913a5ab767736cf52ab1ffc2cd0a..864a9f8bab16f5d1ea52480c37c141ddc934e199 100644 (file)
@@ -15,7 +15,9 @@ EstOutput Unweighted::getValues(Tree* t, int p, string o) {
        try {
                processors = p;
                outputDir = o;
-                       
+        
+        TreeMap* tmap = t->getTreeMap();
+        
                //if the users enters no groups then give them the score of all groups
                int numGroups = m->getNumGroups();
                
@@ -50,7 +52,7 @@ EstOutput Unweighted::getValues(Tree* t, int p, string o) {
 
                #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
                        if(processors == 1){
-                               data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size());
+                               data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size(), tmap);
                        }else{
                                int numPairs = namesOfGroupCombos.size();
                                
@@ -65,11 +67,11 @@ EstOutput Unweighted::getValues(Tree* t, int p, string o) {
                
                                        lines.push_back(linePair(startPos, numPairsPerProcessor));
                                }
-                               data = createProcesses(t, namesOfGroupCombos);
+                               data = createProcesses(t, namesOfGroupCombos, tmap);
                                lines.clear();
                        }
                #else
-                       data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size());
+                       data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size(), tmap);
                #endif
                
                return data;
@@ -81,7 +83,7 @@ EstOutput Unweighted::getValues(Tree* t, int p, string o) {
 }
 /**************************************************************************************************/
 
-EstOutput Unweighted::createProcesses(Tree* t, vector< vector<string> > namesOfGroupCombos) {
+EstOutput Unweighted::createProcesses(Tree* t, vector< vector<string> > namesOfGroupCombos, TreeMap* tmap) {
        try {
 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
                int process = 1;
@@ -98,7 +100,7 @@ EstOutput Unweighted::createProcesses(Tree* t, vector< vector<string> > namesOfG
                                process++;
                        }else if (pid == 0){
                                EstOutput myresults;
-                               myresults = driver(t, namesOfGroupCombos, lines[process].start, lines[process].num);
+                               myresults = driver(t, namesOfGroupCombos, lines[process].start, lines[process].num, tmap);
                                
                                if (m->control_pressed) { exit(0); }
                                
@@ -120,7 +122,7 @@ EstOutput Unweighted::createProcesses(Tree* t, vector< vector<string> > namesOfG
                        }
                }
                
-               results = driver(t, namesOfGroupCombos, lines[0].start, lines[0].num);
+               results = driver(t, namesOfGroupCombos, lines[0].start, lines[0].num, tmap);
                
                //force parent to wait until all the processes are done
                for (int i=0;i<(processors-1);i++) { 
@@ -165,7 +167,7 @@ EstOutput Unweighted::createProcesses(Tree* t, vector< vector<string> > namesOfG
        }
 }
 /**************************************************************************************************/
-EstOutput Unweighted::driver(Tree* t, vector< vector<string> > namesOfGroupCombos, int start, int num) { 
+EstOutput Unweighted::driver(Tree* t, vector< vector<string> > namesOfGroupCombos, int start, int num, TreeMap* tmap) { 
  try {
        
         
@@ -259,6 +261,8 @@ EstOutput Unweighted::getValues(Tree* t, string groupA, string groupB, int p, st
                processors = p;
                outputDir = o;
                
+        TreeMap* tmap = t->getTreeMap();
+     
                //if the users enters no groups then give them the score of all groups
                int numGroups = m->getNumGroups();
                
@@ -293,7 +297,7 @@ EstOutput Unweighted::getValues(Tree* t, string groupA, string groupB, int p, st
 
                #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
                        if(processors == 1){
-                               data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size(), true);
+                               data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size(), true, tmap);
                        }else{
                                int numPairs = namesOfGroupCombos.size();
                                
@@ -307,12 +311,12 @@ EstOutput Unweighted::getValues(Tree* t, string groupA, string groupB, int p, st
                                        lines.push_back(linePair(startPos, numPairsPerProcessor));
                                }
                                        
-                               data = createProcesses(t, namesOfGroupCombos, true);
+                               data = createProcesses(t, namesOfGroupCombos, true, tmap);
                                
                                lines.clear();
                        }
                #else
-                       data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size(), true);
+                       data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size(), true, tmap);
                #endif
        
                return data;
@@ -324,7 +328,7 @@ EstOutput Unweighted::getValues(Tree* t, string groupA, string groupB, int p, st
 }
 /**************************************************************************************************/
 
-EstOutput Unweighted::createProcesses(Tree* t, vector< vector<string> > namesOfGroupCombos, bool usingGroups) {
+EstOutput Unweighted::createProcesses(Tree* t, vector< vector<string> > namesOfGroupCombos, bool usingGroups, TreeMap* tmap) {
        try {
 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
                int process = 1;
@@ -341,7 +345,7 @@ EstOutput Unweighted::createProcesses(Tree* t, vector< vector<string> > namesOfG
                                process++;
                        }else if (pid == 0){
                                EstOutput myresults;
-                               myresults = driver(t, namesOfGroupCombos, lines[process].start, lines[process].num, usingGroups);
+                               myresults = driver(t, namesOfGroupCombos, lines[process].start, lines[process].num, usingGroups, tmap);
                                
                                if (m->control_pressed) { exit(0); }
                                
@@ -361,7 +365,7 @@ EstOutput Unweighted::createProcesses(Tree* t, vector< vector<string> > namesOfG
                        }
                }
                
-               results = driver(t, namesOfGroupCombos, lines[0].start, lines[0].num, usingGroups);
+               results = driver(t, namesOfGroupCombos, lines[0].start, lines[0].num, usingGroups, tmap);
                
                //force parent to wait until all the processes are done
                for (int i=0;i<(processors-1);i++) { 
@@ -405,7 +409,7 @@ EstOutput Unweighted::createProcesses(Tree* t, vector< vector<string> > namesOfG
        }
 }
 /**************************************************************************************************/
-EstOutput Unweighted::driver(Tree* t, vector< vector<string> > namesOfGroupCombos, int start, int num, bool usingGroups) { 
+EstOutput Unweighted::driver(Tree* t, vector< vector<string> > namesOfGroupCombos, int start, int num, bool usingGroups, TreeMap* tmap) { 
  try {
                
                EstOutput results; results.resize(num);
index e751d2e79d19f17a2dd3a9af34ea11be95586ef4..c6c13bb3a7abd14c824dfe6b495871790809833d 100644 (file)
@@ -19,7 +19,7 @@
 class Unweighted : public TreeCalculator  {
        
        public:
-               Unweighted(TreeMap* t, bool r) : tmap(t), includeRoot(r) {};
+        Unweighted(bool r) : includeRoot(r) {};
                ~Unweighted() {};
                EstOutput getValues(Tree*, int, string);
                EstOutput getValues(Tree*, string, string, int, string);
@@ -33,16 +33,15 @@ class Unweighted : public TreeCalculator  {
                vector<linePair> lines;
                
                EstOutput data;
-               TreeMap* tmap;
                int processors;
                string outputDir;
                map< vector<string>, set<int> > rootForGrouping;  //maps a grouping combo to the roots for that combo
                bool includeRoot;
                
-               EstOutput driver(Tree*, vector< vector<string> >, int, int); 
-               EstOutput createProcesses(Tree*, vector< vector<string> >);
-               EstOutput driver(Tree*, vector< vector<string> >, int, int, bool); 
-               EstOutput createProcesses(Tree*, vector< vector<string> >, bool);
+               EstOutput driver(Tree*, vector< vector<string> >, int, int, TreeMap*); 
+               EstOutput createProcesses(Tree*, vector< vector<string> >, TreeMap*);
+               EstOutput driver(Tree*, vector< vector<string> >, int, int, bool, TreeMap*); 
+               EstOutput createProcesses(Tree*, vector< vector<string> >, bool, TreeMap*);
                int getRoot(Tree*, int, vector<string>);
 };
 
index b0a642b1b5024710530e95de5b2a57cc87a7accd..85eed5207ff20d586bd999f670d59a4c8e840c67 100644 (file)
@@ -18,6 +18,8 @@ EstOutput Weighted::getValues(Tree* t, int p, string o) {
                vector<double> D;
                processors = p;
                outputDir = o;
+        
+        TreeMap* tmap = t->getTreeMap();
                
                numGroups = m->getNumGroups();
                
@@ -36,7 +38,7 @@ EstOutput Weighted::getValues(Tree* t, int p, string o) {
                
                #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
                        if(processors == 1){
-                               data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size());
+                               data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size(), tmap);
                        }else{
                                int numPairs = namesOfGroupCombos.size();
                                
@@ -50,12 +52,12 @@ EstOutput Weighted::getValues(Tree* t, int p, string o) {
                                        lines.push_back(linePair(startPos, numPairsPerProcessor));
                                }
 
-                               data = createProcesses(t, namesOfGroupCombos);
+                               data = createProcesses(t, namesOfGroupCombos, tmap);
                                
                                lines.clear();
                        }
                #else
-                       data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size());
+                       data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size(), tmap);
                #endif
                
                return data;
@@ -67,7 +69,7 @@ EstOutput Weighted::getValues(Tree* t, int p, string o) {
 }
 /**************************************************************************************************/
 
-EstOutput Weighted::createProcesses(Tree* t, vector< vector<string> > namesOfGroupCombos) {
+EstOutput Weighted::createProcesses(Tree* t, vector< vector<string> > namesOfGroupCombos, TreeMap* tmap) {
        try {
 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
                int process = 1;
@@ -85,7 +87,7 @@ EstOutput Weighted::createProcesses(Tree* t, vector< vector<string> > namesOfGro
                        }else if (pid == 0){
        
                                EstOutput Myresults;
-                               Myresults = driver(t, namesOfGroupCombos, lines[process].start, lines[process].num);
+                               Myresults = driver(t, namesOfGroupCombos, lines[process].start, lines[process].num, tmap);
                        
                                //m->mothurOut("Merging results."); m->mothurOutEndLine();
                                
@@ -108,7 +110,7 @@ EstOutput Weighted::createProcesses(Tree* t, vector< vector<string> > namesOfGro
                        }
                }
        
-               results = driver(t, namesOfGroupCombos, lines[0].start, lines[0].num);
+               results = driver(t, namesOfGroupCombos, lines[0].start, lines[0].num, tmap);
        
                //force parent to wait until all the processes are done
                for (int i=0;i<(processors-1);i++) { 
@@ -153,7 +155,7 @@ EstOutput Weighted::createProcesses(Tree* t, vector< vector<string> > namesOfGro
        }
 }
 /**************************************************************************************************/
-EstOutput Weighted::driver(Tree* t, vector< vector<string> > namesOfGroupCombos, int start, int num) { 
+EstOutput Weighted::driver(Tree* t, vector< vector<string> > namesOfGroupCombos, int start, int num, TreeMap* tmap) { 
  try {
                EstOutput results;
                vector<double> D;
@@ -267,6 +269,8 @@ EstOutput Weighted::getValues(Tree* t, string groupA, string groupB) {
  try {
                
                data.clear(); //clear out old values
+     
+        TreeMap* tmap = t->getTreeMap();
                
                if (m->control_pressed) { return data; }
                
index c23a11599b4b8d74fdfacd05a5a02ae727834455..180409ce2dbad5da67b570a7ca4a3666c61ae3b3 100644 (file)
@@ -19,7 +19,7 @@
 class Weighted : public TreeCalculator  {
        
        public:
-               Weighted(TreeMap* t, bool r) : tmap(t), includeRoot(r) {};
+        Weighted( bool r) : includeRoot(r) {};
                ~Weighted() {};
                
                EstOutput getValues(Tree*, string, string);
@@ -34,7 +34,6 @@ class Weighted : public TreeCalculator  {
                vector<linePair> lines;
 
                EstOutput data;
-               TreeMap* tmap;
                map<string, int>::iterator it;
                map<string, double> WScore; //a score for each group combination i.e. AB, AC, BC.
                int processors;
@@ -42,8 +41,8 @@ class Weighted : public TreeCalculator  {
                map< vector<string>, set<int> > rootForGrouping;  //maps a grouping combo to the root for that combo
                bool includeRoot;
                
-               EstOutput driver(Tree*, vector< vector<string> >, int, int); 
-               EstOutput createProcesses(Tree*, vector< vector<string> >);
+               EstOutput driver(Tree*, vector< vector<string> >, int, int, TreeMap*); 
+               EstOutput createProcesses(Tree*, vector< vector<string> >, TreeMap*);
                double getLengthToRoot(Tree*, int, string, string);
 };