]> git.donarmstrong.com Git - mothur.git/blobdiff - venncommand.cpp
added parse.fastaq command, added permute option to venn command, fixed bug with...
[mothur.git] / venncommand.cpp
index 5fa06aa98d0eeed0783c9d53eb5f3663e0405c14..852ee8c7ccb79f2a6422448b4d46f3dd752d6c94 100644 (file)
@@ -32,7 +32,7 @@ VennCommand::VennCommand(string option)  {
                
                else {
                        //valid paramters for this command
-                       string AlignArray[] =  {"groups","label","calc", "abund","nseqs","outputdir","inputdir"};
+                       string AlignArray[] =  {"groups","label","calc","permute", "abund","nseqs","outputdir","inputdir"};
                        vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
                        
                        OptionParser parser(option);
@@ -96,9 +96,11 @@ VennCommand::VennCommand(string option)  {
                        temp = validParameter.validFile(parameters, "abund", false);            if (temp == "not found") { temp = "10"; }
                        convert(temp, abund); 
                        
-                       temp = validParameter.validFile(parameters, "nseqs", false);                    if (temp == "not found"){       temp = "f";                             }
+                       temp = validParameter.validFile(parameters, "nseqs", false);            if (temp == "not found"){       temp = "f";                             }
                        nseqs = m->isTrue(temp); 
 
+                       temp = validParameter.validFile(parameters, "permute", false);                  if (temp == "not found"){       temp = "f";                             }
+                       perm = m->isTrue(temp); 
 
                        if (abort == false) {
                                validCalculator = new ValidCalculators();
@@ -154,7 +156,7 @@ VennCommand::VennCommand(string option)  {
 void VennCommand::help(){
        try {
                m->mothurOut("The venn command can only be executed after a successful read.otu command.\n");
-               m->mothurOut("The venn command parameters are groups, calc, abund, nseqs and label.  No parameters are required.\n");
+               m->mothurOut("The venn command parameters are groups, calc, abund, nseqs, permute and label.  No parameters are required.\n");
                m->mothurOut("The groups parameter allows you to specify which of the groups in your groupfile you would like included in your venn diagram, you may only use a maximum of 4 groups.\n");
                m->mothurOut("The group names are separated by dashes. The label allows you to select what distance levels you would like a venn diagram created for, and are also separated by dashes.\n");
                m->mothurOut("The venn command should be in the following format: venn(groups=yourGroups, calc=yourCalcs, label=yourLabels, abund=yourAbund).\n");
@@ -163,6 +165,7 @@ void VennCommand::help(){
                m->mothurOut("The default value for calc is sobs if you have only read a list file or if you have selected only one group, and sharedsobs if you have multiple groups.\n");
                m->mothurOut("The default available estimators for calc are sobs, chao and ace if you have only read a list file, and sharedsobs, sharedchao and sharedace if you have read a list and group file or a shared file.\n");
                m->mothurOut("The nseqs parameter will output the number of sequences represented by the otus in the picture, default=F.\n");
+               m->mothurOut("If you have more than 4 groups, the permute parameter will find all possible combos of 4 of your groups and create pictures for them, default=F.\n");
                m->mothurOut("The only estimators available four 4 groups are sharedsobs and sharedchao.\n");
                m->mothurOut("The venn command outputs a .svg file for each calculator you specify at each distance you choose.\n");
                m->mothurOut("Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n\n");
@@ -205,6 +208,8 @@ int VennCommand::execute(){
                        input = globaldata->ginput;
                        lookup = input->getSharedRAbundVectors();
                        lastLabel = lookup[0]->getLabel();
+                       
+                       if ((lookup.size() > 4) && (perm)) { combosOfFour = findCombinations(lookup.size()); }
                }else if (format == "list") {
                        //you are using just a list file and have only one group
                        read = new ReadOTUFile(globaldata->inputFileName);      
@@ -218,7 +223,7 @@ int VennCommand::execute(){
                //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
                set<string> processedLabels;
                set<string> userLabels = labels;
-               
+
                if (format != "list") { 
                        
                        //as long as you are not at the end of the file or done wih the lines you want
@@ -237,13 +242,29 @@ int VennCommand::execute(){
                                        processedLabels.insert(lookup[0]->getLabel());
                                        userLabels.erase(lookup[0]->getLabel());
                                        
-                                       if (lookup.size() > 4) {
-                                               m->mothurOut("Error: Too many groups chosen.  You may use up to 4 groups with the venn command.  I will use the first four groups in your groupfile."); m->mothurOutEndLine();
+                                       if ((lookup.size() > 4) && (!perm)){
+                                               m->mothurOut("Error: Too many groups chosen.  You may use up to 4 groups with the venn command.  I will use the first four groups in your groupfile. If you set perm=t, I will find all possible combos of 4 groups."); m->mothurOutEndLine();
                                                for (int i = lookup.size(); i > 4; i--) { lookup.pop_back(); } //no memmory leak because pop_back calls destructor
-                                       }
                                        
-                                       vector<string> outfilenames = venn->getPic(lookup, vennCalculators);
-                                       for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]); }  }
+                                               vector<string> outfilenames = venn->getPic(lookup, vennCalculators);
+                                               for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]); }  }
+
+                                       }else if ((lookup.size() > 4) && (perm)) {
+                                               set< set<int> >::iterator it3;
+                                               set<int>::iterator it2;
+                                               for (it3 = combosOfFour.begin(); it3 != combosOfFour.end(); it3++) {  
+                       
+                                                       set<int> poss = *it3;
+                                                       vector<SharedRAbundVector*> subset;
+                                                       for (it2 = poss.begin(); it2 != poss.end(); it2++) {   subset.push_back(lookup[*it2]);   }
+                                                       
+                                                       vector<string> outfilenames = venn->getPic(subset, vennCalculators);
+                                                       for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]); }  }
+                                               }               
+                                       }else {
+                                               vector<string> outfilenames = venn->getPic(lookup, vennCalculators);
+                                               for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]); }  }
+                                       }                                       
                                }
                                
                                if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
@@ -256,13 +277,30 @@ int VennCommand::execute(){
                                        processedLabels.insert(lookup[0]->getLabel());
                                        userLabels.erase(lookup[0]->getLabel());
 
-                                       if (lookup.size() > 4) {
-                                               m->mothurOut("Error: Too many groups chosen.  You may use up to 4 groups with the venn command.  I will use the first four groups in your groupfile."); m->mothurOutEndLine();
+                                       if ((lookup.size() > 4) && (!perm)){
+                                               m->mothurOut("Error: Too many groups chosen.  You may use up to 4 groups with the venn command.  I will use the first four groups in your groupfile. If you set perm=t, I will find all possible combos of 4 groups."); m->mothurOutEndLine();
                                                for (int i = lookup.size(); i > 4; i--) { lookup.pop_back(); } //no memmory leak because pop_back calls destructor
-                                       }                               
-                                       vector<string> outfilenames = venn->getPic(lookup, vennCalculators);
-                                       for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]); }  }
                                        
+                                               vector<string> outfilenames = venn->getPic(lookup, vennCalculators);
+                                               for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]); }  }
+
+                                       }else if ((lookup.size() > 4) && (perm)) {
+                                               set< set<int> >::iterator it3;
+                                               set<int>::iterator it2;
+                                               for (it3 = combosOfFour.begin(); it3 != combosOfFour.end(); it3++) {  
+                       
+                                                       set<int> poss = *it3;
+                                                       vector<SharedRAbundVector*> subset;
+                                                       for (it2 = poss.begin(); it2 != poss.end(); it2++) {   subset.push_back(lookup[*it2]);   }
+                                                       
+                                                       vector<string> outfilenames = venn->getPic(subset, vennCalculators);
+                                                       for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]); }  }
+                                               }               
+                                       }else {
+                                               vector<string> outfilenames = venn->getPic(lookup, vennCalculators);
+                                               for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]); }  }
+                                       }
+                                                                               
                                        //restore real lastlabel to save below
                                        lookup[0]->setLabel(saveLabel);
                                }
@@ -305,13 +343,30 @@ int VennCommand::execute(){
                                        processedLabels.insert(lookup[0]->getLabel());
                                        userLabels.erase(lookup[0]->getLabel());
 
-                                       if (lookup.size() > 4) {
-                                               m->mothurOut("Error: Too many groups chosen.  You may use up to 4 groups with the venn command.  I will use the first four groups in your groupfile."); m->mothurOutEndLine();
+                                       if ((lookup.size() > 4) && (!perm)){
+                                               m->mothurOut("Error: Too many groups chosen.  You may use up to 4 groups with the venn command.  I will use the first four groups in your groupfile. If you set perm=t, I will find all possible combos of 4 groups."); m->mothurOutEndLine();
                                                for (int i = lookup.size(); i > 4; i--) { lookup.pop_back(); } //no memmory leak because pop_back calls destructor
-                                       }                               
-                                       vector<string> outfilenames = venn->getPic(lookup, vennCalculators);
-                                       for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]); }  }
+                                       
+                                               vector<string> outfilenames = venn->getPic(lookup, vennCalculators);
+                                               for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]); }  }
 
+                                       }else if ((lookup.size() > 4) && (perm)) {
+                                               set< set<int> >::iterator it3;
+                                               set<int>::iterator it2;
+                                               for (it3 = combosOfFour.begin(); it3 != combosOfFour.end(); it3++) {  
+                       
+                                                       set<int> poss = *it3;
+                                                       vector<SharedRAbundVector*> subset;
+                                                       for (it2 = poss.begin(); it2 != poss.end(); it2++) {   subset.push_back(lookup[*it2]);   }
+                                                       
+                                                       vector<string> outfilenames = venn->getPic(subset, vennCalculators);
+                                                       for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]); }  }
+                                               }               
+                                       }else {
+                                               vector<string> outfilenames = venn->getPic(lookup, vennCalculators);
+                                               for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]); }  }
+                                       }
+                                       
                                        for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
                        }
                
@@ -426,5 +481,55 @@ int VennCommand::execute(){
                exit(1);
        }
 }
+//**********************************************************************************************************************
+//returns a vector of sets containing the 4 group combinations
+set< set<int> > VennCommand::findCombinations(int lookupSize){
+       try {
+               set< set<int> > combos;
+               
+               set<int> possibles;
+               for (int i = 0; i < lookupSize; i++) {  possibles.insert(i);  }
+               
+               getCombos(possibles, combos);
+               
+               return combos;
+               
+       }
+       catch(exception& e) {
+               m->errorOut(e, "VennCommand", "findCombinations");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+//recusively finds combos of 4
+int VennCommand::getCombos(set<int> possibles, set< set<int> >& combos){
+       try {
+               
+               if (possibles.size() == 4) { //done
+                       if (combos.count(possibles) == 0) { //no dups
+                               combos.insert(possibles);
+                       }
+               }else { //we still have work to do
+                       set<int>::iterator it;
+                       set<int>::iterator it2;
+                       for (it = possibles.begin(); it != possibles.end(); it++) {  
+                               
+                               set<int> newPossibles;
+                               for (it2 = possibles.begin(); it2 != possibles.end(); it2++) {  //all possible combos of one length smaller
+                                       if (*it != *it2) { 
+                                               newPossibles.insert(*it2);
+                                       }
+                               }
+                               getCombos(newPossibles, combos);
+                       }
+               }
+               
+               return 0;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "VennCommand", "getCombos");
+               exit(1);
+       }
+}
 
 //**********************************************************************************************************************