X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=venncommand.cpp;h=5cc0fea3affdfa6d021ff93bef964f478400f5bc;hb=0bcfddf7bc721a334bdae42d86a580019303537d;hp=608c0a06a93b955c6021dbb565df7b3d98cb13ea;hpb=260ae19c36cb11a53ddc5a75b5e507f8dd8b31d6;p=mothur.git diff --git a/venncommand.cpp b/venncommand.cpp index 608c0a0..5cc0fea 100644 --- a/venncommand.cpp +++ b/venncommand.cpp @@ -15,23 +15,71 @@ #include "sharedsobscollectsummary.h" #include "sharedchao1.h" #include "sharedace.h" +#include "nseqs.h" +//********************************************************************************************************************** +vector VennCommand::getValidParameters(){ + try { + string Array[] = {"groups","label","calc","permute", "abund","nseqs","outputdir","inputdir"}; + vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); + return myArray; + } + catch(exception& e) { + m->errorOut(e, "VennCommand", "getValidParameters"); + exit(1); + } +} +//********************************************************************************************************************** +VennCommand::VennCommand(){ + try { + abort = true; calledHelp = true; + vector tempOutNames; + outputTypes["svg"] = tempOutNames; + } + catch(exception& e) { + m->errorOut(e, "VennCommand", "VennCommand"); + exit(1); + } +} +//********************************************************************************************************************** +vector VennCommand::getRequiredParameters(){ + try { + vector myArray; + return myArray; + } + catch(exception& e) { + m->errorOut(e, "VennCommand", "getRequiredParameters"); + exit(1); + } +} +//********************************************************************************************************************** +vector VennCommand::getRequiredFiles(){ + try { + string Array[] = {"list","shared","or"}; + vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); + return myArray; + } + catch(exception& e) { + m->errorOut(e, "VennCommand", "getRequiredFiles"); + exit(1); + } +} //********************************************************************************************************************** VennCommand::VennCommand(string option) { try { globaldata = GlobalData::getInstance(); - abort = false; + abort = false; calledHelp = false; allLines = 1; labels.clear(); //allow user to run help - if(option == "help") { help(); abort = true; } + if(option == "help") { help(); abort = true; calledHelp = true; } else { //valid paramters for this command - string AlignArray[] = {"groups","label","calc", "abund","outputdir","inputdir"}; + string AlignArray[] = {"groups","label","calc","permute", "abund","nseqs","outputdir","inputdir"}; vector myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string))); OptionParser parser(option); @@ -94,6 +142,12 @@ VennCommand::VennCommand(string option) { string temp; 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"; } + 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(); @@ -130,7 +184,7 @@ VennCommand::VennCommand(string option) { //if the users entered no valid calculators don't execute command if (vennCalculators.size() == 0) { m->mothurOut("No valid calculators given, please correct."); m->mothurOutEndLine(); abort = true; } - else { venn = new Venn(outputDir); } + else { venn = new Venn(outputDir, nseqs); } } } @@ -149,7 +203,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 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"); @@ -157,7 +211,9 @@ void VennCommand::help(){ m->mothurOut("The default value for groups is all the groups in your groupfile up to 4, and all labels in your inputfile will be used.\n"); 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 only estmiator available four 4 groups is sharedsobs.\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"); } @@ -186,10 +242,9 @@ VennCommand::~VennCommand(){ int VennCommand::execute(){ try { - if (abort == true) { return 0; } + if (abort == true) { if (calledHelp) { return 0; } return 2; } string lastLabel; - vector outputNames; if (format == "sharedfile") { //you have groups @@ -199,6 +254,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); @@ -212,7 +269,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 processedLabels; set userLabels = labels; - + if (format != "list") { //as long as you are not at the end of the file or done wih the lines you want @@ -231,13 +288,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 outfilenames = venn->getPic(lookup, vennCalculators); - for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]); } } + vector outfilenames = venn->getPic(lookup, vennCalculators); + for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]); outputTypes["svg"].push_back(outfilenames[i]); } } + + }else if ((lookup.size() > 4) && (perm)) { + set< set >::iterator it3; + set::iterator it2; + for (it3 = combosOfFour.begin(); it3 != combosOfFour.end(); it3++) { + + set poss = *it3; + vector subset; + for (it2 = poss.begin(); it2 != poss.end(); it2++) { subset.push_back(lookup[*it2]); } + + vector outfilenames = venn->getPic(subset, vennCalculators); + for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]); outputTypes["svg"].push_back(outfilenames[i]); } } + } + }else { + vector outfilenames = venn->getPic(lookup, vennCalculators); + for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]); outputTypes["svg"].push_back(outfilenames[i]); } } + } } if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) { @@ -250,13 +323,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 outfilenames = venn->getPic(lookup, vennCalculators); - for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]); } } + vector outfilenames = venn->getPic(lookup, vennCalculators); + for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]); outputTypes["svg"].push_back(outfilenames[i]); } } + + }else if ((lookup.size() > 4) && (perm)) { + set< set >::iterator it3; + set::iterator it2; + for (it3 = combosOfFour.begin(); it3 != combosOfFour.end(); it3++) { + + set poss = *it3; + vector subset; + for (it2 = poss.begin(); it2 != poss.end(); it2++) { subset.push_back(lookup[*it2]); } + + vector outfilenames = venn->getPic(subset, vennCalculators); + for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]); outputTypes["svg"].push_back(outfilenames[i]); } } + } + }else { + vector outfilenames = venn->getPic(lookup, vennCalculators); + for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]); outputTypes["svg"].push_back(outfilenames[i]); } } + } + //restore real lastlabel to save below lookup[0]->setLabel(saveLabel); } @@ -299,13 +389,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 outfilenames = venn->getPic(lookup, vennCalculators); - for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]); } } + + vector outfilenames = venn->getPic(lookup, vennCalculators); + for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]); outputTypes["svg"].push_back(outfilenames[i]); } } + }else if ((lookup.size() > 4) && (perm)) { + set< set >::iterator it3; + set::iterator it2; + for (it3 = combosOfFour.begin(); it3 != combosOfFour.end(); it3++) { + + set poss = *it3; + vector subset; + for (it2 = poss.begin(); it2 != poss.end(); it2++) { subset.push_back(lookup[*it2]); } + + vector outfilenames = venn->getPic(subset, vennCalculators); + for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]); outputTypes["svg"].push_back(outfilenames[i]); } } + } + }else { + vector outfilenames = venn->getPic(lookup, vennCalculators); + for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]); outputTypes["svg"].push_back(outfilenames[i]); } } + } + for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } } @@ -335,7 +442,7 @@ int VennCommand::execute(){ m->mothurOut(sabund->getLabel()); m->mothurOutEndLine(); vector outfilenames = venn->getPic(sabund, vennCalculators); - for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]); } } + for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]); outputTypes["svg"].push_back(outfilenames[i]); } } processedLabels.insert(sabund->getLabel()); @@ -350,7 +457,7 @@ int VennCommand::execute(){ m->mothurOut(sabund->getLabel()); m->mothurOutEndLine(); vector outfilenames = venn->getPic(sabund, vennCalculators); - for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]); } } + for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]); outputTypes["svg"].push_back(outfilenames[i]); } } processedLabels.insert(sabund->getLabel()); @@ -392,7 +499,7 @@ int VennCommand::execute(){ m->mothurOut(sabund->getLabel()); m->mothurOutEndLine(); vector outfilenames = venn->getPic(sabund, vennCalculators); - for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]); } } + for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]); outputTypes["svg"].push_back(outfilenames[i]); } } delete sabund; @@ -420,5 +527,55 @@ int VennCommand::execute(){ exit(1); } } +//********************************************************************************************************************** +//returns a vector of sets containing the 4 group combinations +set< set > VennCommand::findCombinations(int lookupSize){ + try { + set< set > combos; + + set 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 possibles, set< set >& 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::iterator it; + set::iterator it2; + for (it = possibles.begin(); it != possibles.end(); it++) { + + set 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); + } +} //**********************************************************************************************************************