X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=venncommand.cpp;h=f28800191f908ec7feabd8271228583b61c3b6a8;hb=98ea55ba2d46a429f031086b4b3272780d0ec894;hp=5357c2560a7c75fe82f3238d482a443744b6a6f5;hpb=154e089bcd37c2c2f773e53ffb88a20170b27037;p=mothur.git diff --git a/venncommand.cpp b/venncommand.cpp index 5357c25..f288001 100644 --- a/venncommand.cpp +++ b/venncommand.cpp @@ -19,76 +19,178 @@ //********************************************************************************************************************** -VennCommand::VennCommand(){ +VennCommand::VennCommand(string option){ try { globaldata = GlobalData::getInstance(); - format = globaldata->getFormat(); - validCalculator = new ValidCalculators(); - util = new SharedUtil(); - - int i; - - if (format == "list") { - for (i=0; iEstimators.size(); i++) { - if (validCalculator->isValidCalculator("vennsingle", globaldata->Estimators[i]) == true) { - if (globaldata->Estimators[i] == "sobs") { - vennCalculators.push_back(new Sobs()); - }else if (globaldata->Estimators[i] == "chao") { - vennCalculators.push_back(new Chao1()); - }else if (globaldata->Estimators[i] == "ace") { - convert(globaldata->getAbund(), abund); - if(abund < 5) - abund = 10; - vennCalculators.push_back(new Ace(abund)); - //}else if (globaldata->Estimators[i] == "jack") { - //vennCalculators.push_back(new Jackknife()); - } + abort = false; + allLines = 1; + lines.clear(); + labels.clear(); + + //allow user to run help + if(option == "help") { help(); abort = true; } + + else { + //valid paramters for this command + string AlignArray[] = {"groups","line","label","calc", "abund"}; + vector myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string))); + + OptionParser parser(option); + map parameters = parser.getParameters(); + + ValidParameters validParameter; + + //check to make sure all parameters are valid for command + for (map::iterator it = parameters.begin(); it != parameters.end(); it++) { + if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; } + } + + //make sure the user has already run the read.otu command + if ((globaldata->getListFile() == "") && (globaldata->getSharedFile() == "")) { + mothurOut("You must read a list, or a list and a group, or a shared before you can use the venn command."); mothurOutEndLine(); abort = true; + } + + //check for optional parameter and set defaults + // ...at some point should added some additional type checking... + line = validParameter.validFile(parameters, "line", false); + if (line == "not found") { line = ""; } + else { + if(line != "all") { splitAtDash(line, lines); allLines = 0; } + else { allLines = 1; } + } + + label = validParameter.validFile(parameters, "label", false); + if (label == "not found") { label = ""; } + else { + if(label != "all") { splitAtDash(label, labels); allLines = 0; } + else { allLines = 1; } + } + + //make sure user did not use both the line and label parameters + if ((line != "") && (label != "")) { mothurOut("You cannot use both the line and label parameters at the same time. "); mothurOutEndLine(); abort = true; } + //if the user has not specified any line or labels use the ones from read.otu + else if ((line == "") && (label == "")) { + allLines = globaldata->allLines; + labels = globaldata->labels; + lines = globaldata->lines; + } + + groups = validParameter.validFile(parameters, "groups", false); + if (groups == "not found") { groups = ""; } + else { + splitAtDash(groups, Groups); + globaldata->Groups = Groups; + } + + format = globaldata->getFormat(); + calc = validParameter.validFile(parameters, "calc", false); + if (calc == "not found") { + if(format == "list") { calc = "sobs"; } + else { calc = "sharedsobs"; } + } + else { + if (calc == "default") { + if(format == "list") { calc = "sobs"; } + else { calc = "sharedsobs"; } } } - }else { - for (i=0; iEstimators.size(); i++) { - if (validCalculator->isValidCalculator("vennshared", globaldata->Estimators[i]) == true) { - if (globaldata->Estimators[i] == "sharedsobs") { - vennCalculators.push_back(new SharedSobsCS()); - }else if (globaldata->Estimators[i] == "sharedchao") { - vennCalculators.push_back(new SharedChao1()); - }else if (globaldata->Estimators[i] == "sharedace") { - vennCalculators.push_back(new SharedAce()); + splitAtDash(calc, Estimators); + + string temp; + temp = validParameter.validFile(parameters, "abund", false); if (temp == "not found") { temp = "10"; } + convert(temp, abund); + + if (abort == false) { + validCalculator = new ValidCalculators(); + + int i; + + if (format == "list") { + for (i=0; iisValidCalculator("vennsingle", Estimators[i]) == true) { + if (Estimators[i] == "sobs") { + vennCalculators.push_back(new Sobs()); + }else if (Estimators[i] == "chao") { + vennCalculators.push_back(new Chao1()); + }else if (Estimators[i] == "ace") { + if(abund < 5) + abund = 10; + vennCalculators.push_back(new Ace(abund)); + } + } + } + }else { + for (i=0; iisValidCalculator("vennshared", Estimators[i]) == true) { + if (Estimators[i] == "sharedsobs") { + vennCalculators.push_back(new SharedSobsCS()); + }else if (Estimators[i] == "sharedchao") { + vennCalculators.push_back(new SharedChao1()); + }else if (Estimators[i] == "sharedace") { + vennCalculators.push_back(new SharedAce()); + } + } } } + + venn = new Venn(); } } - - venn = new Venn(); - - //reset calc for next command - globaldata->setCalc(""); + } catch(exception& e) { - cout << "Standard Error: " << e.what() << " has occurred in the VennCommand class Function VennCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + errorOut(e, "VennCommand", "VennCommand"); exit(1); } - catch(...) { - cout << "An unknown error has occurred in the VennCommand class function VennCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; +} + +//********************************************************************************************************************** + +void VennCommand::help(){ + try { + mothurOut("The venn command can only be executed after a successful read.otu command.\n"); + mothurOut("The venn command parameters are groups, calc, abund, line and label. No parameters are required, but you may not use line and label at the same time.\n"); + 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"); + mothurOut("The group names are separated by dashes. The line and label allow you to select what distance levels you would like a venn diagram created for, and are also separated by dashes.\n"); + mothurOut("The venn command should be in the following format: venn(groups=yourGroups, calc=yourCalcs, line=yourLines, label=yourLabels, abund=yourAbund).\n"); + mothurOut("Example venn(groups=A-B-C, line=1-3-5, calc=sharedsobs-sharedchao, abund=20).\n"); + mothurOut("The default value for groups is all the groups in your groupfile up to 4, and all lines in your inputfile will be used.\n"); + 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"); + 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"); + mothurOut("The only estmiator available four 4 groups is sharedsobs.\n"); + mothurOut("The venn command outputs a .svg file for each calculator you specify at each distance you choose.\n"); + mothurOut("Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n\n"); + } + catch(exception& e) { + errorOut(e, "VennCommand", "help"); exit(1); - } + } } + + //********************************************************************************************************************** VennCommand::~VennCommand(){ - delete input; - delete read; - delete venn; - delete util; + if (abort == false) { + delete input; globaldata->ginput = NULL; + delete read; + delete venn; + globaldata->sabund = NULL; + } + } //********************************************************************************************************************** int VennCommand::execute(){ try { - int count = 1; + + if (abort == true) { return 0; } + + int count = 1; + string lastLabel; //if the users entered no valid calculators don't execute command if (vennCalculators.size() == 0) { return 0; } @@ -99,83 +201,164 @@ int VennCommand::execute(){ read->read(&*globaldata); input = globaldata->ginput; - order = input->getSharedOrderVector(); - }else if (format == "shared") { - //you are using a list and a groupfile - read = new ReadOTUFile(globaldata->inputFileName); - read->read(&*globaldata); - - input = globaldata->ginput; - SharedList = globaldata->gSharedList; - order = SharedList->getSharedOrderVector(); + lookup = input->getSharedRAbundVectors(); + lastLabel = lookup[0]->getLabel(); }else if (format == "list") { //you are using just a list file and have only one group read = new ReadOTUFile(globaldata->inputFileName); read->read(&*globaldata); - ordersingle = globaldata->gorder; + sabund = globaldata->sabund; + lastLabel = sabund->getLabel(); input = globaldata->ginput; } - + + //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; + set userLines = lines; if (format != "list") { - util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups, "venn"); - globaldata->setGroups(""); - - while(order != NULL){ - - if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(order->getLabel()) == 1){ - - cout << order->getLabel() << '\t' << count << endl; - venn->getPic(order, vennCalculators); + //as long as you are not at the end of the file or done wih the lines you want + while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) { + + if(allLines == 1 || lines.count(count) == 1 || labels.count(lookup[0]->getLabel()) == 1){ + mothurOut(lookup[0]->getLabel()); mothurOutEndLine(); + processedLabels.insert(lookup[0]->getLabel()); + userLabels.erase(lookup[0]->getLabel()); + userLines.erase(count); + + if (lookup.size() > 4) { + 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."); mothurOutEndLine(); + for (int i = lookup.size(); i > 4; i--) { lookup.pop_back(); } //no memmory leak because pop_back calls destructor + } + venn->getPic(lookup, vennCalculators); + } + + if ((anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) { + for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } + lookup = input->getSharedRAbundVectors(lastLabel); + + mothurOut(lookup[0]->getLabel()); mothurOutEndLine(); + processedLabels.insert(lookup[0]->getLabel()); + userLabels.erase(lookup[0]->getLabel()); + if (lookup.size() > 4) { + 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."); mothurOutEndLine(); + for (int i = lookup.size(); i > 4; i--) { lookup.pop_back(); } //no memmory leak because pop_back calls destructor + } + venn->getPic(lookup, vennCalculators); } + + + lastLabel = lookup[0]->getLabel(); //get next line to process - if (format == "sharedfile") { - order = input->getSharedOrderVector(); - }else { - //you are using a list and a groupfile - SharedList = input->getSharedListVector(); //get new list vector to process - if (SharedList != NULL) { - order = SharedList->getSharedOrderVector(); //gets new order vector with group info. - }else { - break; - } - } + for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } + lookup = input->getSharedRAbundVectors(); count++; } + //output error messages about any remaining user labels + set::iterator it; + bool needToRun = false; + for (it = userLabels.begin(); it != userLabels.end(); it++) { + mothurOut("Your file does not include the label " + *it); + if (processedLabels.count(lastLabel) != 1) { + mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine(); + needToRun = true; + }else { + mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine(); + } + } + + //run last line if you need to + if (needToRun == true) { + for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } } + lookup = input->getSharedRAbundVectors(lastLabel); + + mothurOut(lookup[0]->getLabel()); mothurOutEndLine(); + processedLabels.insert(lookup[0]->getLabel()); + userLabels.erase(lookup[0]->getLabel()); + + if (lookup.size() > 4) { + 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."); mothurOutEndLine(); + for (int i = lookup.size(); i > 4; i--) { lookup.pop_back(); } //no memmory leak because pop_back calls destructor + } + venn->getPic(lookup, vennCalculators); + for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } + } + + //reset groups parameter globaldata->Groups.clear(); }else{ - while(ordersingle != NULL){ - if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(ordersingle->getLabel()) == 1){ + while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) { + + if(allLines == 1 || lines.count(count) == 1 || labels.count(sabund->getLabel()) == 1){ - cout << ordersingle->getLabel() << '\t' << count << endl; - venn->getPic(ordersingle, vennCalculators); + mothurOut(sabund->getLabel()); mothurOutEndLine(); + venn->getPic(sabund, vennCalculators); + processedLabels.insert(sabund->getLabel()); + userLabels.erase(sabund->getLabel()); + userLines.erase(count); } - ordersingle = (input->getOrderVector()); + if ((anyLabelsToProcess(sabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) { + delete sabund; + sabund = input->getSAbundVector(lastLabel); + + mothurOut(sabund->getLabel()); mothurOutEndLine(); + venn->getPic(sabund, vennCalculators); + + processedLabels.insert(sabund->getLabel()); + userLabels.erase(sabund->getLabel()); + } + + lastLabel = sabund->getLabel(); + + delete sabund; + sabund = input->getSAbundVector(); count++; } + + //output error messages about any remaining user labels + set::iterator it; + bool needToRun = false; + for (it = userLabels.begin(); it != userLabels.end(); it++) { + mothurOut("Your file does not include the label " + *it); + if (processedLabels.count(lastLabel) != 1) { + mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine(); + needToRun = true; + }else { + mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine(); + } + } + + //run last line if you need to + if (needToRun == true) { + if (sabund != NULL) { delete sabund; } + sabund = input->getSAbundVector(lastLabel); + + mothurOut(sabund->getLabel()); mothurOutEndLine(); + venn->getPic(sabund, vennCalculators); + delete sabund; + + } + } + for (int i = 0; i < vennCalculators.size(); i++) { delete vennCalculators[i]; } return 0; } catch(exception& e) { - cout << "Standard Error: " << e.what() << " has occurred in the VennCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + errorOut(e, "VennCommand", "execute"); exit(1); } - catch(...) { - cout << "An unknown error has occurred in the VennCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; - exit(1); - } } //********************************************************************************************************************** -