X-Git-Url: https://git.donarmstrong.com/?p=mothur.git;a=blobdiff_plain;f=clusterdoturcommand.cpp;h=dadc9cc485dedeec55a1057474f2eab19bf0d4ab;hp=4987d4e9305b86496e9d7655a62d27e6a1c779a2;hb=499f4ac6e321f9f03d4c3aa25c3b6880892c8b83;hpb=09ce41fb228c00d0f49896c66b2f2c5760914769 diff --git a/clusterdoturcommand.cpp b/clusterdoturcommand.cpp index 4987d4e..dadc9cc 100644 --- a/clusterdoturcommand.cpp +++ b/clusterdoturcommand.cpp @@ -11,52 +11,73 @@ #include "clusterclassic.h" //********************************************************************************************************************** -vector ClusterDoturCommand::getValidParameters(){ +vector ClusterDoturCommand::setParameters(){ try { - string AlignArray[] = {"phylip","name","hard","cutoff","precision","method","outputdir","inputdir"}; - vector myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string))); + CommandParameter pphylip("phylip", "InputTypes", "", "", "none", "none", "none","list",false,true,true); parameters.push_back(pphylip); + CommandParameter pname("name", "InputTypes", "", "", "namecount", "none", "none","rabund-sabund",false,false,true); parameters.push_back(pname); + CommandParameter pcount("count", "InputTypes", "", "", "namecount", "none", "none","",false,false,true); parameters.push_back(pcount); + CommandParameter pcutoff("cutoff", "Number", "", "10", "", "", "","",false,false,true); parameters.push_back(pcutoff); + CommandParameter pprecision("precision", "Number", "", "100", "", "", "","",false,false); parameters.push_back(pprecision); + CommandParameter pmethod("method", "Multiple", "furthest-nearest-average-weighted", "average", "", "", "","",false,false); parameters.push_back(pmethod); + CommandParameter phard("hard", "Boolean", "", "T", "", "", "","",false,false); parameters.push_back(phard); + CommandParameter psim("sim", "Boolean", "", "F", "", "", "","",false,false); parameters.push_back(psim); + CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir); + CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir); + + vector myArray; + for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); } return myArray; } catch(exception& e) { - m->errorOut(e, "ClusterDoturCommand", "getValidParameters"); + m->errorOut(e, "ClusterDoturCommand", "setParameters"); exit(1); } } //********************************************************************************************************************** -ClusterDoturCommand::ClusterDoturCommand(){ +string ClusterDoturCommand::getHelpString(){ try { - abort = true; - //initialize outputTypes - vector tempOutNames; - outputTypes["list"] = tempOutNames; - outputTypes["rabund"] = tempOutNames; - outputTypes["sabund"] = tempOutNames; + string helpString = ""; + helpString += "The cluster.classic command clusters using the algorithm from dotur. \n"; + helpString += "The cluster.classic command parameter options are phylip, name, count, method, cuttoff, hard, sim, precision. Phylip is required, unless you have a valid current file.\n"; + helpString += "The cluster.classic command should be in the following format: \n"; + helpString += "cluster.classic(phylip=yourDistanceMatrix, method=yourMethod, cutoff=yourCutoff, precision=yourPrecision) \n"; + helpString += "The acceptable cluster methods are furthest, nearest, weighted and average. If no method is provided then average is assumed.\n"; + return helpString; } catch(exception& e) { - m->errorOut(e, "ClusterDoturCommand", "ClusterCommand"); + m->errorOut(e, "ClusterDoturCommand", "getHelpString"); exit(1); } } //********************************************************************************************************************** -vector ClusterDoturCommand::getRequiredParameters(){ - try { - string Array[] = {"phylip"}; - vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); - return myArray; - } - catch(exception& e) { - m->errorOut(e, "ClusterDoturCommand", "getRequiredParameters"); - exit(1); - } +string ClusterDoturCommand::getOutputPattern(string type) { + try { + string pattern = ""; + + if (type == "list") { pattern = "[filename],[clustertag],list-[filename],[clustertag],[tag2],list"; } + else if (type == "rabund") { pattern = "[filename],[clustertag],rabund"; } + else if (type == "sabund") { pattern = "[filename],[clustertag],sabund"; } + else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; } + + return pattern; + } + catch(exception& e) { + m->errorOut(e, "ClusterDoturCommand", "getOutputPattern"); + exit(1); + } } //********************************************************************************************************************** -vector ClusterDoturCommand::getRequiredFiles(){ +ClusterDoturCommand::ClusterDoturCommand(){ try { - vector myArray; - return myArray; + abort = true; calledHelp = true; + setParameters(); + vector tempOutNames; + outputTypes["list"] = tempOutNames; + outputTypes["rabund"] = tempOutNames; + outputTypes["sabund"] = tempOutNames; } catch(exception& e) { - m->errorOut(e, "ClusterDoturCommand", "getRequiredFiles"); + m->errorOut(e, "ClusterDoturCommand", "ClusterCommand"); exit(1); } } @@ -65,15 +86,14 @@ vector ClusterDoturCommand::getRequiredFiles(){ ClusterDoturCommand::ClusterDoturCommand(string option) { try{ - abort = false; + abort = false; calledHelp = false; //allow user to run help - if(option == "help") { help(); abort = true; } + if(option == "help") { help(); abort = true; calledHelp = true; } + else if(option == "citation") { citation(); abort = true; calledHelp = true;} else { - //valid paramters for this command - string Array[] = {"phylip","name","cutoff","hard","precision","method","outputdir","inputdir"}; - vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); + vector myArray = setParameters(); OptionParser parser(option); map parameters = parser.getParameters(); @@ -108,7 +128,14 @@ ClusterDoturCommand::ClusterDoturCommand(string option) { //if the user has not given a path then, add inputdir. else leave path alone. if (path == "") { parameters["name"] = inputDir + it->second; } } - + + it = parameters.find("count"); + //user has given a template file + if(it != parameters.end()){ + path = m->hasPath(it->second); + //if the user has not given a path then, add inputdir. else leave path alone. + if (path == "") { parameters["count"] = inputDir + it->second; } + } } //initialize outputTypes @@ -123,31 +150,49 @@ ClusterDoturCommand::ClusterDoturCommand(string option) { //check for required parameters phylipfile = validParameter.validFile(parameters, "phylip", true); if (phylipfile == "not open") { abort = true; } - else if (phylipfile == "not found") { phylipfile = ""; m->mothurOut("When executing the cluster.dotur command you must enter a phylip file."); m->mothurOutEndLine(); abort = true; } + else if (phylipfile == "not found") { + phylipfile = m->getPhylipFile(); + if (phylipfile != "") { m->mothurOut("Using " + phylipfile + " as input file for the phylip parameter."); m->mothurOutEndLine(); } + else { + m->mothurOut("You need to provide a phylip file with the cluster.classic command."); m->mothurOutEndLine(); + abort = true; + } + }else { m->setPhylipFile(phylipfile); } //check for optional parameter and set defaults namefile = validParameter.validFile(parameters, "name", true); - if (namefile == "not open") { abort = true; } + if (namefile == "not open") { abort = true; namefile = ""; } else if (namefile == "not found") { namefile = ""; } + else { m->setNameFile(namefile); } + + countfile = validParameter.validFile(parameters, "count", true); + if (countfile == "not open") { abort = true; countfile = ""; } + else if (countfile == "not found") { countfile = ""; } + else { m->setCountTableFile(countfile); } + if ((countfile != "") && (namefile != "")) { m->mothurOut("When executing a cluster.classic command you must enter ONLY ONE of the following: count or name."); m->mothurOutEndLine(); abort = true; } + string temp; temp = validParameter.validFile(parameters, "precision", false); if (temp == "not found") { temp = "100"; } //saves precision legnth for formatting below length = temp.length(); - convert(temp, precision); + m->mothurConvert(temp, precision); temp = validParameter.validFile(parameters, "cutoff", false); if (temp == "not found") { temp = "10"; } - convert(temp, cutoff); + m->mothurConvert(temp, cutoff); cutoff += (5 / (precision * 10.0)); - temp = validParameter.validFile(parameters, "hard", false); if (temp == "not found") { temp = "F"; } + temp = validParameter.validFile(parameters, "hard", false); if (temp == "not found") { temp = "T"; } hard = m->isTrue(temp); + temp = validParameter.validFile(parameters, "sim", false); if (temp == "not found") { temp = "F"; } + sim = m->isTrue(temp); + method = validParameter.validFile(parameters, "method", false); - if (method == "not found") { method = "furthest"; } + if (method == "not found") { method = "average"; } if ((method == "furthest") || (method == "nearest") || (method == "average") || (method == "weighted")) { if (method == "furthest") { tag = "fn"; } @@ -162,61 +207,59 @@ ClusterDoturCommand::ClusterDoturCommand(string option) { exit(1); } } - -//********************************************************************************************************************** - -void ClusterDoturCommand::help(){ - try { - m->mothurOut("The cluster.classic command clusters using the algorithm from dotur. \n"); - m->mothurOut("The cluster.classic command parameter options are phylip, name, method, cuttoff, hard, precision. Phylip is required.\n"); - m->mothurOut("The cluster.classic command should be in the following format: \n"); - m->mothurOut("cluster.classic(phylip=yourDistanceMatrix, method=yourMethod, cutoff=yourCutoff, precision=yourPrecision) \n"); - m->mothurOut("The acceptable cluster methods are furthest, nearest, weighted and average. If no method is provided then furthest is assumed.\n\n"); - - } - catch(exception& e) { - m->errorOut(e, "ClusterDoturCommand", "help"); - exit(1); - } -} - -//********************************************************************************************************************** - -ClusterDoturCommand::~ClusterDoturCommand(){} - //********************************************************************************************************************** int ClusterDoturCommand::execute(){ try { - if (abort == true) { return 0; } + if (abort == true) { if (calledHelp) { return 0; } return 2; } - if(namefile != ""){ + + ClusterClassic* cluster = new ClusterClassic(cutoff, method, sim); + + NameAssignment* nameMap = NULL; + CountTable* ct = NULL; + if(namefile != "") { nameMap = new NameAssignment(namefile); nameMap->readMap(); - }else{ - nameMap = NULL; - } - - //reads phylip file storing data in 2D vector, also fills list and rabund - ClusterClassic* cluster = new ClusterClassic(cutoff, method); - cluster->readPhylipFile(phylipfile, nameMap); - - if (m->control_pressed) { delete cluster; delete list; delete rabund; return 0; } + cluster->readPhylipFile(phylipfile, nameMap); + delete nameMap; + }else if (countfile != "") { + ct = new CountTable(); + ct->readTable(countfile, false, false); + cluster->readPhylipFile(phylipfile, ct); + delete ct; + }else { + cluster->readPhylipFile(phylipfile, nameMap); + } + tag = cluster->getTag(); + + if (m->control_pressed) { delete cluster; return 0; } list = cluster->getListVector(); rabund = cluster->getRAbundVector(); - + if (outputDir == "") { outputDir += m->hasPath(phylipfile); } fileroot = outputDir + m->getRootName(m->getSimpleName(phylipfile)); - m->openOutputFile(fileroot+ tag + ".sabund", sabundFile); - m->openOutputFile(fileroot+ tag + ".rabund", rabundFile); - m->openOutputFile(fileroot+ tag + ".list", listFile); - - outputNames.push_back(fileroot+ tag + ".sabund"); outputTypes["sabund"].push_back(fileroot+ tag + ".sabund"); - outputNames.push_back(fileroot+ tag + ".rabund"); outputTypes["rabund"].push_back(fileroot+ tag + ".rabund"); - outputNames.push_back(fileroot+ tag + ".list"); outputTypes["list"].push_back(fileroot+ tag + ".list"); + map variables; + variables["[filename]"] = fileroot; + variables["[clustertag]"] = tag; + string sabundFileName = getOutputFileName("sabund", variables); + string rabundFileName = getOutputFileName("rabund", variables); + if (countfile != "") { variables["[tag2]"] = "unique_list"; } + string listFileName = getOutputFileName("list", variables); + + if (countfile == "") { + m->openOutputFile(sabundFileName, sabundFile); + m->openOutputFile(rabundFileName, rabundFile); + outputNames.push_back(sabundFileName); outputTypes["sabund"].push_back(sabundFileName); + outputNames.push_back(rabundFileName); outputTypes["rabund"].push_back(rabundFileName); + + } + m->openOutputFile(listFileName, listFile); + outputNames.push_back(listFileName); outputTypes["list"].push_back(listFileName); + list->printHeaders(listFile); float previousDist = 0.00000; float rndPreviousDist = 0.00000; @@ -227,8 +270,9 @@ int ClusterDoturCommand::execute(){ int estart = time(NULL); - while ((cluster->getSmallDist() < cutoff) && (cluster->getNSeqs() > 0)){ - if (m->control_pressed) { delete cluster; delete list; delete rabund; sabundFile.close();rabundFile.close();listFile.close(); for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } outputTypes.clear(); return 0; } + while ((cluster->getSmallDist() < cutoff) && (cluster->getNSeqs() > 1)){ + if (m->control_pressed) { delete cluster; delete list; delete rabund; if(countfile == "") {rabundFile.close(); sabundFile.close(); m->mothurRemove((fileroot+ tag + ".rabund")); m->mothurRemove((fileroot+ tag + ".sabund")); } + listFile.close(); m->mothurRemove((fileroot+ tag + ".list")); outputTypes.clear(); return 0; } cluster->update(cutoff); @@ -259,18 +303,33 @@ int ClusterDoturCommand::execute(){ else if(rndPreviousDistceilDist(saveCutoff, precision); } - // else { saveCutoff = m->roundDist(saveCutoff, precision); } - // m->mothurOut("changed cutoff to " + toString(cutoff)); m->mothurOutEndLine(); - //} + delete cluster; delete list; delete rabund; + + //set list file as new current listfile + string current = ""; + itTypes = outputTypes.find("list"); + if (itTypes != outputTypes.end()) { + if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); } + } + + //set rabund file as new current rabundfile + itTypes = outputTypes.find("rabund"); + if (itTypes != outputTypes.end()) { + if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setRabundFile(current); } + } + + //set sabund file as new current sabundfile + itTypes = outputTypes.find("sabund"); + if (itTypes != outputTypes.end()) { + if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSabundFile(current); } + } m->mothurOutEndLine(); m->mothurOut("Output File Names: "); m->mothurOutEndLine(); @@ -291,11 +350,12 @@ int ClusterDoturCommand::execute(){ void ClusterDoturCommand::printData(string label){ try { - - oldRAbund.setLabel(label); - oldRAbund.print(rabundFile); - oldRAbund.getSAbundVector().print(sabundFile); - + oldRAbund.setLabel(label); + if (countfile == "") { + oldRAbund.print(rabundFile); + oldRAbund.getSAbundVector().print(sabundFile); + } + oldRAbund.getSAbundVector().print(cout); oldList.setLabel(label);