X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=getlistcountcommand.cpp;h=1ecef653e11fe15d206b41fd958834be194455ff;hb=4956d362c5bc6a47cf7a6290258f0e763e3a406e;hp=fcc42ced634b80790d6aa3f5977ea6ef3121f77a;hpb=315e38cf393c82be238da5b32574f225a020d25c;p=mothur.git diff --git a/getlistcountcommand.cpp b/getlistcountcommand.cpp index fcc42ce..1ecef65 100644 --- a/getlistcountcommand.cpp +++ b/getlistcountcommand.cpp @@ -10,19 +10,67 @@ #include "getlistcountcommand.h" //********************************************************************************************************************** -GetListCountCommand::GetListCountCommand(string option){ +vector GetListCountCommand::getValidParameters(){ + try { + string Array[] = {"list","label","sort","outputdir","inputdir"}; + vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); + return myArray; + } + catch(exception& e) { + m->errorOut(e, "GetListCountCommand", "getValidParameters"); + exit(1); + } +} +//********************************************************************************************************************** +GetListCountCommand::GetListCountCommand(){ + try { + abort = true; + //initialize outputTypes + vector tempOutNames; + outputTypes["otu"] = tempOutNames; + } + catch(exception& e) { + m->errorOut(e, "GetListCountCommand", "GetListCountCommand"); + exit(1); + } +} +//********************************************************************************************************************** +vector GetListCountCommand::getRequiredParameters(){ + try { + string Array[] = {"list"}; + vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); + return myArray; + } + catch(exception& e) { + m->errorOut(e, "GetListCountCommand", "getRequiredParameters"); + exit(1); + } +} +//********************************************************************************************************************** +vector GetListCountCommand::getRequiredFiles(){ + try { + vector myArray; + return myArray; + } + catch(exception& e) { + m->errorOut(e, "GetListCountCommand", "getRequiredFiles"); + exit(1); + } +} +//********************************************************************************************************************** +GetListCountCommand::GetListCountCommand(string option) { try { globaldata = GlobalData::getInstance(); abort = false; allLines = 1; labels.clear(); - + //allow user to run help if(option == "help") { help(); abort = true; } else { //valid paramters for this command - string AlignArray[] = {"list","label","outputdir","inputdir"}; + string AlignArray[] = {"list","label","sort","outputdir","inputdir"}; vector myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string))); OptionParser parser(option); @@ -36,6 +84,10 @@ GetListCountCommand::GetListCountCommand(string option){ if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; } } + //initialize outputTypes + vector tempOutNames; + outputTypes["otu"] = tempOutNames; + string ranRead = globaldata->getListFile(); //if the user changes the input directory command factory will send this info to us in the output parameter @@ -46,7 +98,7 @@ GetListCountCommand::GetListCountCommand(string option){ it = parameters.find("list"); //user has given a template file if(it != parameters.end()){ - path = hasPath(it->second); + path = m->hasPath(it->second); //if the user has not given a path then, add inputdir. else leave path alone. if (path == "") { parameters["list"] = inputDir + it->second; } } @@ -57,18 +109,20 @@ GetListCountCommand::GetListCountCommand(string option){ //check for required parameters listfile = validParameter.validFile(parameters, "list", true); - if ((listfile == "not found") && (globaldata->getListFile() == "")) { mothurOut("You must read a listfile before running the get.listcount command."); mothurOutEndLine(); abort = true; } + if ((listfile == "not found") && (globaldata->getListFile() == "")) { m->mothurOut("You must read a listfile before running the get.listcount command."); m->mothurOutEndLine(); abort = true; } else if ((listfile == "not found") && (globaldata->getListFile() != "")) { listfile = globaldata->getListFile(); } else if (listfile == "not open") { abort = true; } else { globaldata->setListFile(listfile); } //check for optional parameter and set defaults // ...at some point should added some additional type checking... + sort = validParameter.validFile(parameters, "sort", false); if (sort == "not found") { sort = "otu"; } + if ((sort != "otu") && (sort != "name")) { m->mothurOut( sort + " is not a valid sort option. Options are otu and name. I will use otu."); m->mothurOutEndLine(); sort = "otu"; } label = validParameter.validFile(parameters, "label", false); if (label == "not found") { label = ""; } else { - if(label != "all") { splitAtDash(label, labels); allLines = 0; } + if(label != "all") { m->splitAtDash(label, labels); allLines = 0; } else { allLines = 1; } } @@ -80,7 +134,7 @@ GetListCountCommand::GetListCountCommand(string option){ } } catch(exception& e) { - errorOut(e, "GetListCountCommand", "GetListCountCommand"); + m->errorOut(e, "GetListCountCommand", "GetListCountCommand"); exit(1); } } @@ -88,17 +142,20 @@ GetListCountCommand::GetListCountCommand(string option){ void GetListCountCommand::help(){ try { - mothurOut("The get.listcount command can only be executed after a successful read.otu command of a listfile or providing a list file using the list parameter.\n"); - mothurOut("The get.listcount command parameters are list and label. No parameters are required.\n"); - mothurOut("The label parameter allows you to select what distance levels you would like a output files created for, and are separated by dashes.\n"); - mothurOut("The get.listcount command should be in the following format: get.listcount(list=yourlistFile, label=yourLabels).\n"); - mothurOut("Example get.listcount(list=amazon.fn.list, label=0.10).\n"); - mothurOut("The default value for label is all lines in your inputfile.\n"); - mothurOut("The get.listcount command outputs a .otu file for each distance you specify listing the bin number and the names of the sequences in that bin.\n"); - mothurOut("Note: No spaces between parameter labels (i.e. list), '=' and parameters (i.e.yourListFile).\n\n"); + m->mothurOut("The get.otulist command can only be executed after a successful read.otu command of a listfile or providing a list file using the list parameter.\n"); + m->mothurOut("The get.otulist command parameters are list, sort and label. No parameters are required.\n"); + m->mothurOut("The label parameter allows you to select what distance levels you would like a output files created for, and are separated by dashes.\n"); + m->mothurOut("The sort parameter allows you to select how you want the output displayed. Options are otu and name.\n"); + m->mothurOut("If otu is selected the output will be otu number followed by the list of names in that otu.\n"); + m->mothurOut("If name is selected the output will be a sequence name followed by its otu number.\n"); + m->mothurOut("The get.otulist command should be in the following format: get.otulist(list=yourlistFile, label=yourLabels).\n"); + m->mothurOut("Example get.otulist(list=amazon.fn.list, label=0.10).\n"); + m->mothurOut("The default value for label is all lines in your inputfile.\n"); + m->mothurOut("The get.otulist command outputs a .otu file for each distance you specify listing the bin number and the names of the sequences in that bin.\n"); + m->mothurOut("Note: No spaces between parameter labels (i.e. list), '=' and parameters (i.e.yourListFile).\n\n"); } catch(exception& e) { - errorOut(e, "GetListCountCommand", "help"); + m->errorOut(e, "GetListCountCommand", "help"); exit(1); } } @@ -126,24 +183,51 @@ int GetListCountCommand::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 (m->control_pressed) { + delete read; + delete input; + delete list; + globaldata->gListVector = NULL; + for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } + return 0; + } + while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0))) { if(allLines == 1 || labels.count(list->getLabel()) == 1){ process(list); + + if (m->control_pressed) { + delete read; + delete input; + delete list; + globaldata->gListVector = NULL; + for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } outputTypes.clear(); + return 0; + } processedLabels.insert(list->getLabel()); userLabels.erase(list->getLabel()); } - if ((anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) { + if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) { string saveLabel = list->getLabel(); delete list; list = input->getListVector(lastLabel); process(list); + + if (m->control_pressed) { + delete read; + delete input; + delete list; + globaldata->gListVector = NULL; + for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } outputTypes.clear(); + return 0; + } processedLabels.insert(list->getLabel()); userLabels.erase(list->getLabel()); @@ -163,12 +247,12 @@ int GetListCountCommand::execute(){ set::iterator it; bool needToRun = false; for (it = userLabels.begin(); it != userLabels.end(); it++) { - mothurOut("Your file does not include the label " + *it); + m->mothurOut("Your file does not include the label " + *it); if (processedLabels.count(lastLabel) != 1) { - mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine(); + m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine(); needToRun = true; }else { - mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine(); + m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine(); } } @@ -177,17 +261,33 @@ int GetListCountCommand::execute(){ if (list != NULL) { delete list; } list = input->getListVector(lastLabel); - process(list); + process(list); + + if (m->control_pressed) { + delete read; + delete input; + delete list; + globaldata->gListVector = NULL; + for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } outputTypes.clear(); + return 0; + } + delete list; } delete read; + delete input; globaldata->gListVector = NULL; + m->mothurOutEndLine(); + m->mothurOut("Output File Names: "); m->mothurOutEndLine(); + for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); } + m->mothurOutEndLine(); + return 0; } catch(exception& e) { - errorOut(e, "GetListCountCommand", "execute"); + m->errorOut(e, "GetListCountCommand", "execute"); exit(1); } } @@ -196,23 +296,36 @@ int GetListCountCommand::execute(){ //return 1 if error, 0 otherwise void GetListCountCommand::process(ListVector* list) { try { - string binnames, name, sequence; - if (outputDir == "") { outputDir += hasPath(listfile); } - string outputFileName = outputDir + getRootName(getSimpleName(listfile)) + list->getLabel() + ".otu"; - openOutputFile(outputFileName, out); + string binnames; + if (outputDir == "") { outputDir += m->hasPath(listfile); } + string outputFileName = outputDir + m->getRootName(m->getSimpleName(listfile)) + list->getLabel() + ".otu"; + m->openOutputFile(outputFileName, out); + outputNames.push_back(outputFileName); outputTypes["otu"].push_back(outputFileName); - mothurOut(list->getLabel()); mothurOutEndLine(); + m->mothurOut(list->getLabel()); m->mothurOutEndLine(); //for each bin in the list vector for (int i = 0; i < list->getNumBins(); i++) { + if (m->control_pressed) { break; } + binnames = list->get(i); - out << i+1 << '\t' << binnames << endl; + + if (sort == "otu") { + out << i+1 << '\t' << binnames << endl; + }else{ //sort = name + vector names; + m->splitAtComma(binnames, names); + + for (int j = 0; j < names.size(); j++) { + out << names[j] << '\t' << i+1 << endl; + } + } } out.close(); } catch(exception& e) { - errorOut(e, "GetListCountCommand", "process"); + m->errorOut(e, "GetListCountCommand", "process"); exit(1); } }