X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=mantelcommand.cpp;h=f3259e6096c3e0c1373e1c091078c9ceac377f82;hb=006601d68abe8d0061f77e8d28323b160750e343;hp=0f6d0d7277fbaf7271f2e428b3d11cd0abd303f3;hpb=5553e33be3a45eee6bed2ac9a5c4ca0aa0e8d5e4;p=mothur.git diff --git a/mantelcommand.cpp b/mantelcommand.cpp index 0f6d0d7..f3259e6 100644 --- a/mantelcommand.cpp +++ b/mantelcommand.cpp @@ -10,51 +10,75 @@ #include "mantelcommand.h" #include "readphylipvector.h" + //********************************************************************************************************************** -vector MantelCommand::getValidParameters(){ +vector MantelCommand::setParameters(){ try { - string Array[] = {"phylip1","phylip2","method","iters","outputdir","inputdir"}; - vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); + CommandParameter pphylip1("phylip1", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pphylip1); + CommandParameter pphylip2("phylip2", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pphylip2); + CommandParameter piters("iters", "Number", "", "1000", "", "", "",false,false); parameters.push_back(piters); + CommandParameter pmethod("method", "Multiple", "pearson-spearman-kendall", "pearson", "", "", "",false,false); parameters.push_back(pmethod); + 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, "MantelCommand", "getValidParameters"); + m->errorOut(e, "MantelCommand", "setParameters"); exit(1); } } //********************************************************************************************************************** -vector MantelCommand::getRequiredParameters(){ +string MantelCommand::getHelpString(){ try { - string Array[] = {"phylip1", "phylip2"}; - vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); - return myArray; + string helpString = ""; + helpString += "Sokal, R. R., & Rohlf, F. J. (1995). Biometry, 3rd edn. New York: Freeman.\n"; + helpString += "The mantel command reads two distance matrices and calculates the mantel correlation coefficient.\n"; + helpString += "The mantel command parameters are phylip1, phylip2, iters and method. The phylip1 and phylip2 parameters are required. Matrices must be the same size and contain the same names.\n"; + helpString += "The method parameter allows you to select what method you would like to use. Options are pearson, spearman and kendall. Default=pearson.\n"; + helpString += "The iters parameter allows you to set number of randomization for the P value. The default is 1000. \n"; + helpString += "The mantel command should be in the following format: mantel(phylip1=veg.dist, phylip2=env.dist).\n"; + helpString += "The mantel command outputs a .mantel file.\n"; + helpString += "Note: No spaces between parameter labels (i.e. phylip1), '=' and parameters (i.e. veg.dist).\n"; + return helpString; } catch(exception& e) { - m->errorOut(e, "MantelCommand", "getRequiredParameters"); + m->errorOut(e, "MantelCommand", "getHelpString"); exit(1); } } //********************************************************************************************************************** -MantelCommand::MantelCommand(){ +string MantelCommand::getOutputFileNameTag(string type, string inputName=""){ try { - abort = true; calledHelp = true; - vector tempOutNames; - outputTypes["mantel"] = tempOutNames; + string outputFileName = ""; + map >::iterator it; + + //is this a type this command creates + it = outputTypes.find(type); + if (it == outputTypes.end()) { m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); } + else { + if (type == "mantel") { outputFileName = "mantel"; } + else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true; } + } + return outputFileName; } catch(exception& e) { - m->errorOut(e, "MantelCommand", "MantelCommand"); + m->errorOut(e, "MantelCommand", "getOutputFileNameTag"); exit(1); } } - //********************************************************************************************************************** -vector MantelCommand::getRequiredFiles(){ +MantelCommand::MantelCommand(){ try { - vector myArray; - return myArray; + abort = true; calledHelp = true; + setParameters(); + vector tempOutNames; + outputTypes["mantel"] = tempOutNames; } catch(exception& e) { - m->errorOut(e, "MantelCommand", "getRequiredFiles"); + m->errorOut(e, "MantelCommand", "MantelCommand"); exit(1); } } @@ -65,11 +89,10 @@ MantelCommand::MantelCommand(string option) { //allow user to run help 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[] = {"phylip1","phylip2","method","iters","outputdir","inputdir"}; - vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); + vector myArray = setParameters(); OptionParser parser(option); map parameters = parser.getParameters(); @@ -122,7 +145,7 @@ MantelCommand::MantelCommand(string option) { method = validParameter.validFile(parameters, "method", false); if (method == "not found"){ method = "pearson"; } string temp = validParameter.validFile(parameters, "iters", false); if (temp == "not found") { temp = "1000"; } - convert(temp, iters); + m->mothurConvert(temp, iters); if ((method != "pearson") && (method != "spearman") && (method != "kendall")) { m->mothurOut(method + " is not a valid method. Valid methods are pearson, spearman, and kendall."); m->mothurOutEndLine(); abort = true; } } @@ -134,27 +157,6 @@ MantelCommand::MantelCommand(string option) { } //********************************************************************************************************************** -void MantelCommand::help(){ - try { - m->mothurOut("The mantel command reads two distance matrices and calculates the mantel correlation coefficient.\n"); - m->mothurOut("The mantel command parameters are phylip1, phylip2 and method. The phylip1 and phylip2 parameters are required. Matrices must be the same size and contain the same names.\n"); - m->mothurOut("The method parameter allows you to select what method you would like to use. Options are pearson, spearman and kendall. Default=pearson.\n"); - m->mothurOut("The mantel command should be in the following format: mantel(phylip1=veg.dist, phylip2=env.dist).\n"); - m->mothurOut("The mantel command outputs a .mantel file.\n"); - m->mothurOut("Note: No spaces between parameter labels (i.e. phylip1), '=' and parameters (i.e. veg.dist).\n\n"); - } - catch(exception& e) { - m->errorOut(e, "MantelCommand", "help"); - exit(1); - } -} - -//********************************************************************************************************************** - -MantelCommand::~MantelCommand(){} - -//********************************************************************************************************************** - int MantelCommand::execute(){ try { @@ -173,82 +175,19 @@ int MantelCommand::execute(){ //read phylip2 ReadPhylipVector readMatrix2(phylipfile2); - vector temp; //seqDist - int, int, float - vector names2 = readMatrix2.read(temp); + vector< vector > matrix2; + vector names2 = readMatrix2.read(matrix2); if (m->control_pressed) { return 0; } - //fill matrix2 making sure to make sure the distances are in the same order as matrix1 - vector< vector > matrix2; + //make sure matrix2 and matrix1 are in the same order if (names1 == names2) { //then everything is in same order and same size - - //initialize space - matrix2.resize(names2.size()); - for (int i = 0; i < matrix2.size(); i++) { matrix2[i].resize(names2.size(), 0.0); } - - //fill matrix2 - for (int i = 0; i < temp.size(); i++) { - matrix2[temp[i].seq1][temp[i].seq2] = temp[i].dist; - matrix2[temp[i].seq2][temp[i].seq1] = temp[i].dist; - } - }else if (names1.size() != names2.size()) { //wrong size no need to order, abort m->mothurOut("[ERROR]: distance matrices are not the same size, aborting."); m->mothurOutEndLine(); m->control_pressed = true; }else { //sizes are the same, but either the names are different or they are in different order - - //map location of name in names1 to location of name in names2 - map names1Map; - map::iterator it; - for (int i = 0; i < names1.size(); i++) { names1Map[names1[i]] = i; } - - map names2Map; - bool nameError = false; - for (int i = 0; i < names2.size(); i++) { - - //if you find one name error stop looking - if (!nameError) { - it = names1Map.find(names2[i]); - if (it == names1Map.end()) { nameError = true; } - } - - //are names different - names2Map[names2[i]] = i; - } - - //initialize space - matrix2.resize(names2.size()); - for (int i = 0; i < matrix2.size(); i++) { matrix2[i].resize(names2.size(), 0.0); } - - //fill matrix2 - //are we comparing apples to apples? - if (nameError) { - m->mothurOut("[WARNING]: Names do not match between distance files. Comparing based on order in files."); m->mothurOutEndLine(); - - for (int i = 0; i < temp.size(); i++) { - matrix2[temp[i].seq1][temp[i].seq2] = temp[i].dist; - matrix2[temp[i].seq2][temp[i].seq1] = temp[i].dist; - } - - }else { //no name error just different orders, so reorder - - for (int i = 0; i < temp.size(); i++) { - - //what's the location of this distance comparison in matrix1 - string matrix2NameI = names2[temp[i].seq1]; - string matrix2NameJ = names2[temp[i].seq2]; - int locationI = names1Map[matrix2NameI]; - int locationJ = names1Map[matrix2NameJ]; - - matrix2[locationI][locationJ] = temp[i].dist; - matrix2[locationJ][locationI] = temp[i].dist; - } - } - - } - - //frees up space - temp.clear(); + m->mothurOut("[WARNING]: Names do not match between distance files. Comparing based on order in files."); m->mothurOutEndLine(); + } if (m->control_pressed) { return 0; } @@ -287,7 +226,7 @@ int MantelCommand::execute(){ if (m->control_pressed) { return 0; } - string outputFile = outputDir + m->getRootName(m->getSimpleName(phylipfile1)) + "mantel"; + string outputFile = outputDir + m->getRootName(m->getSimpleName(phylipfile1)) + getOutputFileNameTag("mantel"); outputNames.push_back(outputFile); outputTypes["mantel"].push_back(outputFile); ofstream out;