]> git.donarmstrong.com Git - mothur.git/blobdiff - unifracunweightedcommand.cpp
added tree reader class to handle reading trees. Reworked the tree map to tree class...
[mothur.git] / unifracunweightedcommand.cpp
index eef37bd93b4dcc98fbf2bcadee93231be66b52ab..de8cb5637fe64a5a63e88852a26935512ff53806 100644 (file)
@@ -8,6 +8,7 @@
  */
 
 #include "unifracunweightedcommand.h"
+#include "treereader.h"
 
 //**********************************************************************************************************************
 vector<string> UnifracUnweightedCommand::setParameters(){      
@@ -80,6 +81,7 @@ UnifracUnweightedCommand::UnifracUnweightedCommand(string option)  {
                        
                //allow user to run help
                if(option == "help") { help(); abort = true; calledHelp = true; }
+               else if(option == "citation") { citation(); abort = true; calledHelp = true;}
                
                else {
                        vector<string> myArray = setParameters();
@@ -132,25 +134,25 @@ UnifracUnweightedCommand::UnifracUnweightedCommand(string option)  {
                                }
                        }
                        
-                       m->runParse = true;
-                       
-                       //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
                                treefile = m->getTreeFile(); 
                                if (treefile != "") { m->mothurOut("Using " + treefile + " as input file for the tree parameter."); m->mothurOutEndLine(); }
                                else {  m->mothurOut("You have no current tree file and the tree parameter is required."); m->mothurOutEndLine(); abort = true; }                                                               
-                       }       
+                       }else { m->setTreeFile(treefile); }     
                        
                        //check for required parameters
                        groupfile = validParameter.validFile(parameters, "group", true);
                        if (groupfile == "not open") { abort = true; }
                        else if (groupfile == "not found") { groupfile = ""; }
+                       else { m->setGroupFile(groupfile); }
                        
                        namefile = validParameter.validFile(parameters, "name", true);
-                       if (namefile == "not open") { abort = true; }
+                       if (namefile == "not open") { namefile = ""; abort = true; }
                        else if (namefile == "not found") { namefile = ""; }
+                       else { m->setNameFile(namefile); }
                        
                        outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = ""; }
                        
@@ -160,11 +162,11 @@ UnifracUnweightedCommand::UnifracUnweightedCommand(string option)  {
                        if (groups == "not found") { groups = ""; }
                        else { 
                                m->splitAtDash(groups, Groups);
-                               m->Groups = Groups;
+                               m->setGroups(Groups);
                        }
                                
                        itersString = validParameter.validFile(parameters, "iters", false);                             if (itersString == "not found") { itersString = "1000"; }
-                       convert(itersString, iters); 
+                       m->mothurConvert(itersString, iters); 
                        
                        string temp = validParameter.validFile(parameters, "distance", false);                  
                        if (temp == "not found") { phylip = false; outputForm = ""; }
@@ -181,7 +183,7 @@ UnifracUnweightedCommand::UnifracUnweightedCommand(string option)  {
                        
                        temp = validParameter.validFile(parameters, "processors", false);       if (temp == "not found"){       temp = m->getProcessors();      }
                        m->setProcessors(temp);
-                       convert(temp, processors); 
+                       m->mothurConvert(temp, processors); 
                        
                        if (!random) {  iters = 0;  } //turn off random calcs
                        
@@ -189,7 +191,12 @@ UnifracUnweightedCommand::UnifracUnweightedCommand(string option)  {
                        if ((phylip) && (Groups.size() == 0)) {
                                groups = "all";
                                m->splitAtDash(groups, Groups);
-                               m->Groups = Groups;
+                               m->setGroups(Groups);
+                       }
+                       
+                       if (namefile == "") {
+                               vector<string> files; files.push_back(treefile);
+                               parser.getNameFile(files);
                        }
                }
                
@@ -206,80 +213,28 @@ int UnifracUnweightedCommand::execute() {
                
                if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
                
-               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;
+               m->setTreeFile(treefile);
                
-               //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++) {  remove(outputNames[i].c_str()); } outputTypes.clear();
-                                       m->Groups.clear();
-                                       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();
-               util->setGroups(m->Groups, tmap->namesOfGroups, allGroups, numGroups, "unweighted");    //sets the groups the user wants to analyze
-               util->getCombos(groupComb, m->Groups, numComp);
-               delete util;
+               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);
+               m->setGroups(Groups);
        
                if (numGroups == 1) { numComp++; groupComb.push_back(allGroups); }
                
-               unweighted = new Unweighted(tmap, includeRoot);
+               Unweighted unweighted(includeRoot);
                
                int start = time(NULL);
                
@@ -289,16 +244,18 @@ int UnifracUnweightedCommand::execute() {
                
                if (numComp < processors) { processors = numComp;  }
                
-               outSum << "Tree#" << '\t' << "Groups" << '\t'  <<  "UWScore" <<'\t' << "UWSig" <<  endl;
-               m->mothurOut("Tree#\tGroups\tUWScore\tUWSig"); m->mothurOutEndLine();
+               outSum << "Tree#" << '\t' << "Groups" << '\t'  <<  "UWScore" <<'\t';
+               m->mothurOut("Tree#\tGroups\tUWScore\t");
+               if (random) { outSum << "UWSig"; m->mothurOut("UWSig"); }
+               outSum << endl; m->mothurOutEndLine();
         
                //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++) {  remove(outputNames[i].c_str());  }
+                               for (int i = 0; i < outputNames.size(); i++) {  m->mothurRemove(outputNames[i]);  }
                                return 0; 
                        }
                        
@@ -317,10 +274,10 @@ 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;
-                               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++) {      remove(outputNames[i].c_str());  }return 0; }
+                       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
                        for(int k = 0; k < numComp; k++) {
@@ -335,10 +292,10 @@ 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;
-                                       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++) {       remove(outputNames[i].c_str());  } return 0; }
+                               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++) {      
                                        //add trees unweighted score to map of scores
@@ -375,8 +332,8 @@ int UnifracUnweightedCommand::execute() {
        
                        }
                        
-                       if (m->control_pressed) { delete tmap; delete unweighted;
-                               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++) {       remove(outputNames[i].c_str());  } return 0;  }
+                       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
                        printUWSummaryFile(i);
@@ -392,11 +349,10 @@ int UnifracUnweightedCommand::execute() {
                
 
                outSum.close();
-               m->Groups.clear();
-               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++) {        remove(outputNames[i].c_str());  }      return 0; }
+               if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        m->mothurRemove(outputNames[i]);  }     return 0; }
                
                m->mothurOut("It took " + toString(time(NULL) - start) + " secs to run unifrac.unweighted."); m->mothurOutEndLine();
                
@@ -476,9 +432,9 @@ void UnifracUnweightedCommand::printUWSummaryFile(int i) {
                                        m->mothurOutJustToLog(groupComb[a]  + "\t" + toString(utreeScores[a][0])  + "\t<" + toString((1/float(iters))) + "\n"); 
                                }
                        }else{
-                               outSum << setprecision(6) << groupComb[a]  << '\t' << utreeScores[a][0] << '\t' << "0.00" << endl;
-                               cout << setprecision(6)  << groupComb[a]  << '\t' << utreeScores[a][0] << '\t' << "0.00" << endl; 
-                               m->mothurOutJustToLog(groupComb[a]  + "\t" + toString(utreeScores[a][0])  + "\t0.00\n");
+                               outSum << setprecision(6) << groupComb[a]  << '\t' << utreeScores[a][0]  << endl;
+                               cout << setprecision(6)  << groupComb[a]  << '\t' << utreeScores[a][0]  << endl; 
+                               m->mothurOutJustToLog(groupComb[a]  + "\t" + toString(utreeScores[a][0]) + "\n");
                        }
                }
                
@@ -505,18 +461,18 @@ void UnifracUnweightedCommand::createPhylipFile(int i) {
                
                if ((outputForm == "lt") || (outputForm == "square")) {
                        //output numSeqs
-                       out << m->Groups.size() << endl;
+                       out << m->getNumGroups() << endl;
                }
                
                //make matrix with scores in it
-               vector< vector<float> > dists;  dists.resize(m->Groups.size());
-               for (int i = 0; i < m->Groups.size(); i++) {
-                       dists[i].resize(m->Groups.size(), 0.0);
+               vector< vector<float> > dists;  dists.resize(m->getNumGroups());
+               for (int i = 0; i < m->getNumGroups(); i++) {
+                       dists[i].resize(m->getNumGroups(), 0.0);
                }
                
                //flip it so you can print it
                int count = 0;
-               for (int r=0; r<m->Groups.size(); r++) { 
+               for (int r=0; r<m->getNumGroups(); r++) { 
                        for (int l = 0; l < r; l++) {
                                dists[r][l] = utreeScores[count][0];
                                dists[l][r] = utreeScores[count][0];
@@ -525,9 +481,9 @@ void UnifracUnweightedCommand::createPhylipFile(int i) {
                }
                
                //output to file
-               for (int r=0; r<m->Groups.size(); r++) { 
+               for (int r=0; r<m->getNumGroups(); r++) { 
                        //output name
-                       string name = m->Groups[r];
+                       string name = (m->getGroups())[r];
                        if (name.length() < 10) { //pad with spaces to make compatible
                                while (name.length() < 10) {  name += " ";  }
                        }
@@ -542,12 +498,12 @@ void UnifracUnweightedCommand::createPhylipFile(int i) {
                                out << name << '\t';
                                
                                //output distances
-                               for (int l = 0; l < m->Groups.size(); l++) {    out << dists[r][l] << '\t';  }
+                               for (int l = 0; l < m->getNumGroups(); l++) {   out << dists[r][l] << '\t';  }
                                out << endl;
                        }else{
                                //output distances
                                for (int l = 0; l < r; l++) {   
-                                       string otherName = m->Groups[l];
+                                       string otherName = (m->getGroups())[l];
                                        if (otherName.length() < 10) { //pad with spaces to make compatible
                                                while (otherName.length() < 10) {  otherName += " ";  }
                                        }
@@ -562,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);
-       }
 }
 /***********************************************************/