]> git.donarmstrong.com Git - mothur.git/blobdiff - treegroupscommand.cpp
working on pca
[mothur.git] / treegroupscommand.cpp
index eb3c33481d4ef68199f2b42e452ea93c1679bbf4..07999f6a1ff89499fd92b9429191704b51545f61 100644 (file)
@@ -8,6 +8,10 @@
  */
 
 #include "treegroupscommand.h"
+#include "sharedsobscollectsummary.h"
+#include "sharedchao1.h"
+#include "sharedace.h"
+#include "sharednseqs.h"
 #include "sharedjabund.h"
 #include "sharedsorabund.h"
 #include "sharedjclass.h"
 #include "sharedsorest.h"
 #include "sharedthetayc.h"
 #include "sharedthetan.h"
+#include "sharedkstest.h"
+#include "whittaker.h"
+#include "sharedochiai.h"
+#include "sharedanderbergs.h"
+#include "sharedkulczynski.h"
+#include "sharedkulczynskicody.h"
+#include "sharedlennon.h"
 #include "sharedmorisitahorn.h"
 #include "sharedbraycurtis.h"
+#include "sharedjackknife.h"
+#include "whittaker.h"
+#include "odum.h"
+#include "canberra.h"
+#include "structeuclidean.h"
+#include "structchord.h"
+#include "hellinger.h"
+#include "manhattan.h"
+#include "structpearson.h"
+#include "soergel.h"
+#include "spearman.h"
+#include "structkulczynski.h"
+#include "structchi2.h"
+#include "speciesprofile.h"
+#include "hamming.h"
+#include "gower.h"
+#include "memchi2.h"
+#include "memchord.h"
+#include "memeuclidean.h"
+#include "mempearson.h"
 
-
+//**********************************************************************************************************************
+vector<string> TreeGroupCommand::getValidParameters(){ 
+       try {
+               string Array[] =  {"label","calc","groups", "phylip", "column", "name", "precision","cutoff","outputdir","inputdir"};
+               vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
+               return myArray;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "TreeGroupCommand", "getValidParameters");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+TreeGroupCommand::TreeGroupCommand(){  
+       try {
+               abort = true; calledHelp = true;
+               globaldata = GlobalData::getInstance();
+               //initialize outputTypes
+               vector<string> tempOutNames;
+               outputTypes["tree"] = tempOutNames;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "TreeGroupCommand", "TreeGroupCommand");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+vector<string> TreeGroupCommand::getRequiredParameters(){      
+       try {
+               vector<string> myArray;
+               return myArray;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "TreeGroupCommand", "getRequiredParameters");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+vector<string> TreeGroupCommand::getRequiredFiles(){   
+       try {
+               string Array[] =  {"phylip","column","shared","or"};
+               vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
+               return myArray;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "TreeGroupCommand", "getRequiredFiles");
+               exit(1);
+       }
+}
 //**********************************************************************************************************************
 
 TreeGroupCommand::TreeGroupCommand(string option)  {
        try {
                globaldata = GlobalData::getInstance();
-               abort = false;
+               abort = false; calledHelp = false;   
                allLines = 1;
                labels.clear();
                Groups.clear();
                Estimators.clear();
                
                //allow user to run help
-               if(option == "help") { validCalculator = new ValidCalculators(); help(); abort = true; }
+               if(option == "help") { validCalculator = new ValidCalculators(); help(); abort = true; calledHelp = true; }
                
                else {
                        //valid paramters for this command
@@ -50,6 +129,10 @@ TreeGroupCommand::TreeGroupCommand(string option)  {
                                if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
                        }
                        
+                       //initialize outputTypes
+                       vector<string> tempOutNames;
+                       outputTypes["tree"] = tempOutNames;
+                       
                        //if the user changes the input directory command factory will send this info to us in the output parameter 
                        string inputDir = validParameter.validFile(parameters, "inputdir", false);              
                        if (inputDir == "not found"){   inputDir = "";          }
@@ -86,12 +169,12 @@ TreeGroupCommand::TreeGroupCommand(string option)  {
                        phylipfile = validParameter.validFile(parameters, "phylip", true);
                        if (phylipfile == "not open") { abort = true; }
                        else if (phylipfile == "not found") { phylipfile = ""; }        
-                       else {  format = "phylip";  globaldata->setPhylipFile(phylipfile);      }
+                       else {  globaldata->newRead(); format = "phylip";  globaldata->setPhylipFile(phylipfile);       }
                        
                        columnfile = validParameter.validFile(parameters, "column", true);
                        if (columnfile == "not open") { abort = true; } 
                        else if (columnfile == "not found") { columnfile = ""; }
-                       else {  format = "column"; globaldata->setColumnFile(columnfile);       }
+                       else {  globaldata->newRead(); format = "column"; globaldata->setColumnFile(columnfile);        }
                        
                        namefile = validParameter.validFile(parameters, "name", true);
                        if (namefile == "not open") { abort = true; }   
@@ -158,7 +241,13 @@ TreeGroupCommand::TreeGroupCommand(string option)  {
                                        int i;
                                        for (i=0; i<Estimators.size(); i++) {
                                                if (validCalculator->isValidCalculator("treegroup", Estimators[i]) == true) { 
-                                                       if (Estimators[i] == "jabund") {        
+                                                       if (Estimators[i] == "sharedsobs") { 
+                                                               treeCalculators.push_back(new SharedSobsCS());
+                                                       }else if (Estimators[i] == "sharedchao") { 
+                                                               treeCalculators.push_back(new SharedChao1());
+                                                       }else if (Estimators[i] == "sharedace") { 
+                                                               treeCalculators.push_back(new SharedAce());
+                                                       }else if (Estimators[i] == "jabund") {  
                                                                treeCalculators.push_back(new JAbund());
                                                        }else if (Estimators[i] == "sorabund") { 
                                                                treeCalculators.push_back(new SorAbund());
@@ -174,10 +263,62 @@ TreeGroupCommand::TreeGroupCommand(string option)  {
                                                                treeCalculators.push_back(new ThetaYC());
                                                        }else if (Estimators[i] == "thetan") { 
                                                                treeCalculators.push_back(new ThetaN());
+                                                       }else if (Estimators[i] == "kstest") { 
+                                                               treeCalculators.push_back(new KSTest());
+                                                       }else if (Estimators[i] == "sharednseqs") { 
+                                                               treeCalculators.push_back(new SharedNSeqs());
+                                                       }else if (Estimators[i] == "ochiai") { 
+                                                               treeCalculators.push_back(new Ochiai());
+                                                       }else if (Estimators[i] == "anderberg") { 
+                                                               treeCalculators.push_back(new Anderberg());
+                                                       }else if (Estimators[i] == "kulczynski") { 
+                                                               treeCalculators.push_back(new Kulczynski());
+                                                       }else if (Estimators[i] == "kulczynskicody") { 
+                                                               treeCalculators.push_back(new KulczynskiCody());
+                                                       }else if (Estimators[i] == "lennon") { 
+                                                               treeCalculators.push_back(new Lennon());
                                                        }else if (Estimators[i] == "morisitahorn") { 
                                                                treeCalculators.push_back(new MorHorn());
                                                        }else if (Estimators[i] == "braycurtis") { 
                                                                treeCalculators.push_back(new BrayCurtis());
+                                                       }else if (Estimators[i] == "whittaker") { 
+                                                               treeCalculators.push_back(new Whittaker());
+                                                       }else if (Estimators[i] == "odum") { 
+                                                               treeCalculators.push_back(new Odum());
+                                                       }else if (Estimators[i] == "canberra") { 
+                                                               treeCalculators.push_back(new Canberra());
+                                                       }else if (Estimators[i] == "structeuclidean") { 
+                                                               treeCalculators.push_back(new StructEuclidean());
+                                                       }else if (Estimators[i] == "structchord") { 
+                                                               treeCalculators.push_back(new StructChord());
+                                                       }else if (Estimators[i] == "hellinger") { 
+                                                               treeCalculators.push_back(new Hellinger());
+                                                       }else if (Estimators[i] == "manhattan") { 
+                                                               treeCalculators.push_back(new Manhattan());
+                                                       }else if (Estimators[i] == "structpearson") { 
+                                                               treeCalculators.push_back(new StructPearson());
+                                                       }else if (Estimators[i] == "soergel") { 
+                                                               treeCalculators.push_back(new Soergel());
+                                                       }else if (Estimators[i] == "spearman") { 
+                                                               treeCalculators.push_back(new Spearman());
+                                                       }else if (Estimators[i] == "structkulczynski") { 
+                                                               treeCalculators.push_back(new StructKulczynski());
+                                                       }else if (Estimators[i] == "speciesprofile") { 
+                                                               treeCalculators.push_back(new SpeciesProfile());
+                                                       }else if (Estimators[i] == "hamming") { 
+                                                               treeCalculators.push_back(new Hamming());
+                                                       }else if (Estimators[i] == "structchi2") { 
+                                                               treeCalculators.push_back(new StructChi2());
+                                                       }else if (Estimators[i] == "gower") { 
+                                                               treeCalculators.push_back(new Gower());
+                                                       }else if (Estimators[i] == "memchi2") { 
+                                                               treeCalculators.push_back(new MemChi2());
+                                                       }else if (Estimators[i] == "memchord") { 
+                                                               treeCalculators.push_back(new MemChord());
+                                                       }else if (Estimators[i] == "memeuclidean") { 
+                                                               treeCalculators.push_back(new MemEuclidean());
+                                                       }else if (Estimators[i] == "mempearson") { 
+                                                               treeCalculators.push_back(new MemPearson());
                                                        }
                                                }
                                        }
@@ -239,12 +380,13 @@ TreeGroupCommand::~TreeGroupCommand(){
 int TreeGroupCommand::execute(){
        try {
        
-               if (abort == true) { return 0; }
+               if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
                
                if (format == "sharedfile") {
                        //if the users entered no valid calculators don't execute command
                        if (treeCalculators.size() == 0) { m->mothurOut("You have given no valid calculators."); m->mothurOutEndLine(); return 0; }
-
+                       
+                       if (globaldata->gGroupmap != NULL) {  delete globaldata->gGroupmap;   globaldata->gGroupmap = NULL;  }
                        //you have groups
                        read = new ReadOTUFile(globaldata->inputFileName);      
                        read->read(&*globaldata); 
@@ -258,17 +400,17 @@ int TreeGroupCommand::execute(){
                        //used in tree constructor 
                        globaldata->runParse = false;
                        
-                       //clear globaldatas old tree names if any
-                       globaldata->Treenames.clear();
-               
-                       //fills globaldatas tree names
-                       globaldata->Treenames = globaldata->Groups;
-               
                        //create treemap class from groupmap for tree class to use
                        tmap = new TreeMap();
                        tmap->makeSim(globaldata->gGroupmap);
                        globaldata->gTreemap = tmap;
                        
+                       //clear globaldatas old tree names if any
+                       globaldata->Treenames.clear();
+                       
+                       //fills globaldatas tree names
+                       globaldata->Treenames = globaldata->Groups;
+               
                        if (m->control_pressed) { return 0; }
                        
                        //create tree file
@@ -323,7 +465,7 @@ int TreeGroupCommand::execute(){
 
                        //create a new filename
                        outputFile = outputDir + m->getRootName(m->getSimpleName(globaldata->inputFileName)) + "tre";   
-                       outputNames.push_back(outputFile);
+                       outputNames.push_back(outputFile); outputTypes["tree"].push_back(outputFile);
                                
                        createTree();
                        
@@ -334,7 +476,14 @@ int TreeGroupCommand::execute(){
                }
                                
                //reset groups parameter
-               globaldata->Groups.clear();  
+               globaldata->Groups.clear(); 
+               
+               //set tree file as new current treefile
+               string current = "";
+               itTypes = outputTypes.find("tree");
+               if (itTypes != outputTypes.end()) {
+                       if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setTreeFile(current); }
+               }
                
                m->mothurOutEndLine();
                m->mothurOut("Output File Names: "); m->mothurOutEndLine();
@@ -594,7 +743,7 @@ int TreeGroupCommand::process(vector<SharedRAbundVector*> thisLookup) {
                
                                        //create a new filename
                                        outputFile = outputDir + m->getRootName(m->getSimpleName(globaldata->inputFileName)) + treeCalculators[i]->getName() + "." + thisLookup[0]->getLabel() + ".tre";                                
-                                       outputNames.push_back(outputFile);
+                                       outputNames.push_back(outputFile); outputTypes["tree"].push_back(outputFile); 
                                                                                                
                                        for (int k = 0; k < thisLookup.size(); k++) { 
                                                for (int l = k; l < thisLookup.size(); l++) {
@@ -605,8 +754,16 @@ int TreeGroupCommand::process(vector<SharedRAbundVector*> thisLookup) {
                                                                //add new pair of sharedrabunds
                                                                subset.push_back(thisLookup[k]); subset.push_back(thisLookup[l]); 
                                                                
-                                                               data = treeCalculators[i]->getValues(subset); //saves the calculator outputs
+                                                               //if this calc needs all groups to calculate the pair load all groups
+                                                               if (treeCalculators[i]->getNeedsAll()) { 
+                                                                       //load subset with rest of lookup for those calcs that need everyone to calc for a pair
+                                                                       for (int w = 0; w < thisLookup.size(); w++) {
+                                                                               if ((w != k) && (w != l)) { subset.push_back(thisLookup[w]); }
+                                                                       }
+                                                               }
                                                                
+                                                               data = treeCalculators[i]->getValues(subset); //saves the calculator outputs
+                                               //cout << thisLookup[k]->getGroup() << '\t' << thisLookup[l]->getGroup() << '\t' << (1.0 - data[0]) << endl;
                                                                if (m->control_pressed) { return 1; }
                                                                
                                                                //save values in similarity matrix
@@ -616,6 +773,21 @@ int TreeGroupCommand::process(vector<SharedRAbundVector*> thisLookup) {
                                                }
                                        }
                                        
+                                       //createdistance file from simMatrix
+                                       /*string o = outputDir + m->getRootName(m->getSimpleName(globaldata->inputFileName)) + treeCalculators[i]->getName() + "." + thisLookup[0]->getLabel() + ".dist";
+                                       ofstream outDist;
+                                       m->openOutputFile(o, outDist);
+                                       outDist << simMatrix.size() << endl;
+                                       for (int k = 0; k < simMatrix.size(); k++) {
+                                               outDist << thisLookup[k]->getGroup() << '\t';
+                                               for (int l = 0; l < k; l++) {
+                                                       outDist << (1.0-simMatrix[k][l]) << '\t';
+                                               }
+                                               outDist << endl;
+                                       }
+                                       outDist.close();*/
+
+                                       
                                        if (m->control_pressed) { return 1; }
                                        //creates tree from similarity matrix and write out file
                                        createTree();