From 8dd3c225255d7084e3aff8740aa4f1f1cabb367a Mon Sep 17 00:00:00 2001 From: westcott Date: Wed, 25 Jan 2012 10:09:47 -0500 Subject: [PATCH] added otu.association command. added calcSpearman, calcKendall and calcPearson functions to linear algebra classes to allow for passing single dimension vectors. --- Mothur.xcodeproj/project.pbxproj | 7 + commandfactory.cpp | 5 + linearalgebra.cpp | 309 ++++++++++++++++++++- linearalgebra.h | 5 + otuassociationcommand.cpp | 460 +++++++++++++++++++++++++++++++ otuassociationcommand.h | 52 ++++ 6 files changed, 837 insertions(+), 1 deletion(-) create mode 100644 otuassociationcommand.cpp create mode 100644 otuassociationcommand.h diff --git a/Mothur.xcodeproj/project.pbxproj b/Mothur.xcodeproj/project.pbxproj index 99454a9..545f7b3 100644 --- a/Mothur.xcodeproj/project.pbxproj +++ b/Mothur.xcodeproj/project.pbxproj @@ -53,6 +53,7 @@ A79234D713C74BF6002B08E2 /* mothurfisher.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A79234D613C74BF6002B08E2 /* mothurfisher.cpp */; }; A795840D13F13CD900F201D5 /* countgroupscommand.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A795840C13F13CD900F201D5 /* countgroupscommand.cpp */; }; A799F5B91309A3E000AEEFA0 /* makefastqcommand.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A799F5B81309A3E000AEEFA0 /* makefastqcommand.cpp */; }; + A7A3C8C914D041AD00B1BFBE /* otuassociationcommand.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A7A3C8C714D041AD00B1BFBE /* otuassociationcommand.cpp */; }; A7A61F2D130062E000E05B6B /* amovacommand.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A7A61F2C130062E000E05B6B /* amovacommand.cpp */; }; A7BF221414587886000AD524 /* myPerseus.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A7BF221214587886000AD524 /* myPerseus.cpp */; }; A7BF2232145879B2000AD524 /* chimeraperseuscommand.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A7BF2231145879B2000AD524 /* chimeraperseuscommand.cpp */; }; @@ -471,6 +472,8 @@ A795840C13F13CD900F201D5 /* countgroupscommand.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = countgroupscommand.cpp; sourceTree = ""; }; A799F5B71309A3E000AEEFA0 /* makefastqcommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = makefastqcommand.h; sourceTree = ""; }; A799F5B81309A3E000AEEFA0 /* makefastqcommand.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = makefastqcommand.cpp; sourceTree = ""; }; + A7A3C8C714D041AD00B1BFBE /* otuassociationcommand.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = otuassociationcommand.cpp; sourceTree = ""; }; + A7A3C8C814D041AD00B1BFBE /* otuassociationcommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = otuassociationcommand.h; sourceTree = ""; }; A7A61F1A130035C800E05B6B /* LICENSE */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = LICENSE; sourceTree = ""; }; A7A61F2B130062E000E05B6B /* amovacommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = amovacommand.h; sourceTree = ""; }; A7A61F2C130062E000E05B6B /* amovacommand.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = amovacommand.cpp; sourceTree = ""; }; @@ -1414,6 +1417,8 @@ A7E9B76B12D37EC400DA6239 /* normalizesharedcommand.cpp */, A713EBEB12DC7C5E000092AC /* nmdscommand.h */, A713EBEC12DC7C5E000092AC /* nmdscommand.cpp */, + A7A3C8C814D041AD00B1BFBE /* otuassociationcommand.h */, + A7A3C8C714D041AD00B1BFBE /* otuassociationcommand.cpp */, A7E9B77A12D37EC400DA6239 /* otuhierarchycommand.h */, A7E9B77912D37EC400DA6239 /* otuhierarchycommand.cpp */, A7E9B77E12D37EC400DA6239 /* pairwiseseqscommand.h */, @@ -1912,6 +1917,7 @@ }; buildConfigurationList = 1DEB928908733DD80010E9CD /* Build configuration list for PBXProject "Mothur" */; compatibilityVersion = "Xcode 3.1"; + developmentRegion = English; hasScannedForEncodings = 1; knownRegions = ( English, @@ -2270,6 +2276,7 @@ A7FA2AD114A0E881007C09A6 /* stxwx.f in Sources */, A7FA2B1614A0EBEA007C09A6 /* sslvrg.f in Sources */, A7FA2B5B14A0F0C2007C09A6 /* intrv.f in Sources */, + A7A3C8C914D041AD00B1BFBE /* otuassociationcommand.cpp in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/commandfactory.cpp b/commandfactory.cpp index 8c8fe3c..d813e1b 100644 --- a/commandfactory.cpp +++ b/commandfactory.cpp @@ -126,6 +126,7 @@ #include "chimeraperseuscommand.h" #include "shhhseqscommand.h" #include "summaryqualcommand.h" +#include "otuassociationcommand.h" /*******************************************************/ @@ -273,6 +274,7 @@ CommandFactory::CommandFactory(){ commands["summary.tax"] = "summary.tax"; commands["summary.qual"] = "summary.qual"; commands["shhh.seqs"] = "shhh.seqs"; + commands["otu.association"] = "otu.association"; commands["quit"] = "MPIEnabled"; } @@ -433,6 +435,7 @@ Command* CommandFactory::getCommand(string commandName, string optionString){ else if(commandName == "summary.qual") { command = new SummaryQualCommand(optionString); } else if(commandName == "chimera.perseus") { command = new ChimeraPerseusCommand(optionString); } else if(commandName == "shhh.seqs") { command = new ShhhSeqsCommand(optionString); } + else if(commandName == "otu.association") { command = new OTUAssociationCommand(optionString); } else { command = new NoCommand(optionString); } return command; @@ -577,6 +580,7 @@ Command* CommandFactory::getCommand(string commandName, string optionString, str else if(commandName == "summary.qual") { pipecommand = new SummaryQualCommand(optionString); } else if(commandName == "chimera.perseus") { pipecommand = new ChimeraPerseusCommand(optionString); } else if(commandName == "shhh.seqs") { pipecommand = new ShhhSeqsCommand(optionString); } + else if(commandName == "otu.association") { pipecommand = new OTUAssociationCommand(optionString); } else { pipecommand = new NoCommand(optionString); } return pipecommand; @@ -709,6 +713,7 @@ Command* CommandFactory::getCommand(string commandName){ else if(commandName == "summary.qual") { shellcommand = new SummaryQualCommand(); } else if(commandName == "chimera.perseus") { shellcommand = new ChimeraPerseusCommand(); } else if(commandName == "shhh.seqs") { shellcommand = new ShhhSeqsCommand(); } + else if(commandName == "otu.association") { shellcommand = new OTUAssociationCommand(); } else { shellcommand = new NoCommand(); } return shellcommand; diff --git a/linearalgebra.cpp b/linearalgebra.cpp index e198619..0269fc4 100644 --- a/linearalgebra.cpp +++ b/linearalgebra.cpp @@ -693,7 +693,314 @@ double LinearAlgebra::calcKendall(vector< vector >& euclidDists, vector< exit(1); } } - +/*********************************************************************************************************************************/ +double LinearAlgebra::calcKendall(vector& x, vector& y, double& sig){ + try { + if (x.size() != y.size()) { m->mothurOut("[ERROR]: vector size mismatch."); m->mothurOutEndLine(); return 0.0; } + + //format data + vector xscores; + for (int i = 0; i < x.size(); i++) { + spearmanRank member(toString(i), x[i]); + xscores.push_back(member); + } + + //sort xscores + sort(xscores.begin(), xscores.end(), compareSpearman); + + //convert scores to ranks of x + vector ties; + int rankTotal = 0; + for (int j = 0; j < xscores.size(); j++) { + rankTotal += (j+1); + ties.push_back(&(xscores[j])); + + if (j != xscores.size()-1) { // you are not the last so you can look ahead + if (xscores[j].score != xscores[j+1].score) { // you are done with ties, rank them and continue + for (int k = 0; k < ties.size(); k++) { + float thisrank = rankTotal / (float) ties.size(); + (*ties[k]).score = thisrank; + } + ties.clear(); + rankTotal = 0; + } + }else { // you are the last one + for (int k = 0; k < ties.size(); k++) { + float thisrank = rankTotal / (float) ties.size(); + (*ties[k]).score = thisrank; + } + } + } + + + //format data + vector yscores; + for (int j = 0; j < y.size(); j++) { + spearmanRank member(toString(j), y[j]); + yscores.push_back(member); + } + + //sort yscores + sort(yscores.begin(), yscores.end(), compareSpearman); + + //convert to ranks + map rank; + vector yties; + rankTotal = 0; + for (int j = 0; j < yscores.size(); j++) { + rankTotal += (j+1); + yties.push_back(yscores[j]); + + if (j != yscores.size()-1) { // you are not the last so you can look ahead + if (yscores[j].score != yscores[j+1].score) { // you are done with ties, rank them and continue + for (int k = 0; k < yties.size(); k++) { + float thisrank = rankTotal / (float) yties.size(); + rank[yties[k].name] = thisrank; + } + yties.clear(); + rankTotal = 0; + } + }else { // you are the last one + for (int k = 0; k < yties.size(); k++) { + float thisrank = rankTotal / (float) yties.size(); + rank[yties[k].name] = thisrank; + } + } + } + + + int numCoor = 0; + int numDisCoor = 0; + + //associate x and y + vector otus; + for (int l = 0; l < xscores.size(); l++) { + spearmanRank member(xscores[l].name, rank[xscores[l].name]); + otus.push_back(member); + } + + int count = 0; + for (int l = 0; l < xscores.size(); l++) { + + int numWithHigherRank = 0; + int numWithLowerRank = 0; + float thisrank = otus[l].score; + + for (int u = l+1; u < xscores.size(); u++) { + if (otus[u].score > thisrank) { numWithHigherRank++; } + else if (otus[u].score < thisrank) { numWithLowerRank++; } + count++; + } + + numCoor += numWithHigherRank; + numDisCoor += numWithLowerRank; + } + + double p = (numCoor - numDisCoor) / (float) count; + + //calc signif - zA - http://en.wikipedia.org/wiki/Kendall_tau_rank_correlation_coefficient#Significance_tests + double numer = 3.0 * (numCoor - numDisCoor); + int n = xscores.size(); + double denom = n * (n-1) * (2*n + 5) / (double) 2.0; + denom = sqrt(denom); + sig = numer / denom; + + if (isnan(sig) || isinf(sig)) { sig = 0.0; } + + return p; + } + catch(exception& e) { + m->errorOut(e, "LinearAlgebra", "calcKendall"); + exit(1); + } +} +/*********************************************************************************************************************************/ +double LinearAlgebra::calcSpearman(vector& x, vector& y, double& sig){ + try { + if (x.size() != y.size()) { m->mothurOut("[ERROR]: vector size mismatch."); m->mothurOutEndLine(); return 0.0; } + + //format data + map tableX; + map::iterator itTable; + vector xscores; + + for (int i = 0; i < x.size(); i++) { + spearmanRank member(toString(i), x[i]); + xscores.push_back(member); + + //count number of repeats + itTable = tableX.find(x[i]); + if (itTable == tableX.end()) { + tableX[x[i]] = 1; + }else { + tableX[x[i]]++; + } + } + + + //calc LX + double Lx = 0.0; + for (itTable = tableX.begin(); itTable != tableX.end(); itTable++) { + double tx = (double) itTable->second; + Lx += ((pow(tx, 3.0) - tx) / 12.0); + } + + + //sort x + sort(xscores.begin(), xscores.end(), compareSpearman); + + //convert scores to ranks of x + //convert to ranks + map rankx; + vector xties; + int rankTotal = 0; + for (int j = 0; j < xscores.size(); j++) { + rankTotal += (j+1); + xties.push_back(xscores[j]); + + if (j != xscores.size()-1) { // you are not the last so you can look ahead + if (xscores[j].score != xscores[j+1].score) { // you are done with ties, rank them and continue + for (int k = 0; k < xties.size(); k++) { + float thisrank = rankTotal / (float) xties.size(); + rankx[xties[k].name] = thisrank; + } + xties.clear(); + rankTotal = 0; + } + }else { // you are the last one + for (int k = 0; k < xties.size(); k++) { + float thisrank = rankTotal / (float) xties.size(); + rankx[xties[k].name] = thisrank; + } + } + } + + //format x + vector yscores; + map tableY; + for (int j = 0; j < y.size(); j++) { + spearmanRank member(toString(j), y[j]); + yscores.push_back(member); + + itTable = tableY.find(member.score); + if (itTable == tableY.end()) { + tableY[member.score] = 1; + }else { + tableY[member.score]++; + } + + } + + //calc Ly + double Ly = 0.0; + for (itTable = tableY.begin(); itTable != tableY.end(); itTable++) { + double ty = (double) itTable->second; + Ly += ((pow(ty, 3.0) - ty) / 12.0); + } + + sort(yscores.begin(), yscores.end(), compareSpearman); + + //convert to ranks + map rank; + vector yties; + rankTotal = 0; + for (int j = 0; j < yscores.size(); j++) { + rankTotal += (j+1); + yties.push_back(yscores[j]); + + if (j != yscores.size()-1) { // you are not the last so you can look ahead + if (yscores[j].score != yscores[j+1].score) { // you are done with ties, rank them and continue + for (int k = 0; k < yties.size(); k++) { + float thisrank = rankTotal / (float) yties.size(); + rank[yties[k].name] = thisrank; + } + yties.clear(); + rankTotal = 0; + } + }else { // you are the last one + for (int k = 0; k < yties.size(); k++) { + float thisrank = rankTotal / (float) yties.size(); + rank[yties[k].name] = thisrank; + } + } + } + + double di = 0.0; + for (int k = 0; k < x.size(); k++) { + + float xi = rankx[toString(k)]; + float yi = rank[toString(k)]; + + di += ((xi - yi) * (xi - yi)); + } + + double p = 0.0; + + double n = (double) x.size(); + double SX2 = ((pow(n, 3.0) - n) / 12.0) - Lx; + double SY2 = ((pow(n, 3.0) - n) / 12.0) - Ly; + + p = (SX2 + SY2 - di) / (2.0 * sqrt((SX2*SY2))); + + //signifigance calc - http://en.wikipedia.org/wiki/Spearman%27s_rank_correlation_coefficient + double temp = (x.size()-2) / (double) (1- (p*p)); + temp = sqrt(temp); + sig = p*temp; + if (isnan(sig) || isinf(sig)) { sig = 0.0; } + + return p; + } + catch(exception& e) { + m->errorOut(e, "LinearAlgebra", "calcSpearman"); + exit(1); + } +} +/*********************************************************************************************************************************/ +double LinearAlgebra::calcPearson(vector& x, vector& y, double& sig){ + try { + if (x.size() != y.size()) { m->mothurOut("[ERROR]: vector size mismatch."); m->mothurOutEndLine(); return 0.0; } + + //find average X + float averageX = 0.0; + for (int i = 0; i < x.size(); i++) { averageX += x[i]; } + averageX = averageX / (float) x.size(); + + //find average Y + float sumY = 0.0; + for (int j = 0; j < y.size(); j++) { sumY += y[j]; } + float Ybar = sumY / (float) y.size(); + + double r = 0.0; + double numerator = 0.0; + double denomTerm1 = 0.0; + double denomTerm2 = 0.0; + + for (int j = 0; j < x.size(); j++) { + float Yi = y[j]; + float Xi = x[j]; + + numerator += ((Xi - averageX) * (Yi - Ybar)); + denomTerm1 += ((Xi - averageX) * (Xi - averageX)); + denomTerm2 += ((Yi - Ybar) * (Yi - Ybar)); + } + + double denom = (sqrt(denomTerm1) * sqrt(denomTerm2)); + + r = numerator / denom; + + //signifigance calc - http://faculty.vassar.edu/lowry/ch4apx.html + double temp = (1- (r*r)) / (double) (x.size()-2); + temp = sqrt(temp); + sig = r / temp; + if (isnan(sig) || isinf(sig)) { sig = 0.0; } + + return r; + } + catch(exception& e) { + m->errorOut(e, "LinearAlgebra", "calcPearson"); + exit(1); + } +} /*********************************************************************************************************************************/ vector > LinearAlgebra::getObservedEuclideanDistance(vector >& relAbundData){ diff --git a/linearalgebra.h b/linearalgebra.h index 58a60da..321f46a 100644 --- a/linearalgebra.h +++ b/linearalgebra.h @@ -29,6 +29,11 @@ public: double calcPearson(vector >&, vector >&); double calcSpearman(vector >&, vector >&); double calcKendall(vector >&, vector >&); + + double calcPearson(vector&, vector&, double&); + double calcSpearman(vector&, vector&, double&); + double calcKendall(vector&, vector&, double&); + private: MothurOut* m; diff --git a/otuassociationcommand.cpp b/otuassociationcommand.cpp new file mode 100644 index 0000000..c82510c --- /dev/null +++ b/otuassociationcommand.cpp @@ -0,0 +1,460 @@ +/* + * otuassociationcommand.cpp + * Mothur + * + * Created by westcott on 1/19/12. + * Copyright 2012 Schloss Lab. All rights reserved. + * + */ + +#include "otuassociationcommand.h" +#include "linearalgebra.h" + +//********************************************************************************************************************** +vector OTUAssociationCommand::setParameters(){ + try { + CommandParameter pshared("shared", "InputTypes", "", "", "SharedRelMeta", "SharedRelMeta", "none",false,false); parameters.push_back(pshared); + CommandParameter prelabund("relabund", "InputTypes", "", "", "SharedRelMeta", "SharedRelMeta", "none",false,false); parameters.push_back(prelabund); + CommandParameter plabel("label", "String", "", "", "", "", "",false,false); parameters.push_back(plabel); + CommandParameter pgroups("groups", "String", "", "", "", "", "",false,false); parameters.push_back(pgroups); + 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, "OTUAssociationCommand", "setParameters"); + exit(1); + } +} +//********************************************************************************************************************** +string OTUAssociationCommand::getHelpString(){ + try { + string helpString = ""; + helpString += "The otu.association command reads a shared or relabund file and calculates the correlation coefficients between otus.\n"; + helpString += "The otu.association command parameters are shared, relabund, groups, method and label. The shared or relabund parameter is required.\n"; + helpString += "The groups parameter allows you to specify which of the groups you would like included. The group names are separated by dashes.\n"; + helpString += "The label parameter allows you to select what distances level you would like used, and are also separated by dashes.\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 otu.association command should be in the following format: otu.association(shared=yourSharedFile, method=yourMethod).\n"; + helpString += "Example otu.association(shared=genus.pool.shared, method=kendall).\n"; + helpString += "The otu.association command outputs a .otu.corr file.\n"; + helpString += "Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n"; + return helpString; + } + catch(exception& e) { + m->errorOut(e, "OTUAssociationCommand", "getHelpString"); + exit(1); + } +} +//********************************************************************************************************************** +OTUAssociationCommand::OTUAssociationCommand(){ + try { + abort = true; calledHelp = true; + setParameters(); + vector tempOutNames; + outputTypes["otu.corr"] = tempOutNames; + } + catch(exception& e) { + m->errorOut(e, "OTUAssociationCommand", "OTUAssociationCommand"); + exit(1); + } +} +//********************************************************************************************************************** +OTUAssociationCommand::OTUAssociationCommand(string option) { + try { + abort = false; calledHelp = false; + allLines = 1; + + //allow user to run help + if(option == "help") { help(); abort = true; calledHelp = true; } + else if(option == "citation") { citation(); abort = true; calledHelp = true;} + + else { + vector myArray = setParameters(); + + OptionParser parser(option); + map parameters = parser.getParameters(); + + ValidParameters validParameter; + map::iterator it; + + //check to make sure all parameters are valid for command + for (it = parameters.begin(); it != parameters.end(); it++) { + if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; } + } + + vector tempOutNames; + outputTypes["otu.corr"] = tempOutNames; + + //if the user changes the input directory command factory will send this info to us in the output parameter + string inputDir = validParameter.validFile(parameters, "inputdir", false); + if (inputDir == "not found"){ inputDir = ""; } + else { + string path; + it = parameters.find("shared"); + //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["shared"] = inputDir + it->second; } + } + + it = parameters.find("relabund"); + //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["relabund"] = inputDir + it->second; } + } + } + + + //check for required parameters + sharedfile = validParameter.validFile(parameters, "shared", true); + if (sharedfile == "not open") { abort = true; } + else if (sharedfile == "not found") { sharedfile = ""; } + else { inputFileName = sharedfile; m->setSharedFile(sharedfile); } + + relabundfile = validParameter.validFile(parameters, "relabund", true); + if (relabundfile == "not open") { abort = true; } + else if (relabundfile == "not found") { relabundfile = ""; } + else { inputFileName = relabundfile; m->setRelAbundFile(relabundfile); } + + groups = validParameter.validFile(parameters, "groups", false); + if (groups == "not found") { groups = ""; pickedGroups = false; } + else { + pickedGroups = true; + m->splitAtDash(groups, Groups); + } + m->setGroups(Groups); + + outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(inputFileName); } + + label = validParameter.validFile(parameters, "label", false); + if (label == "not found") { label = ""; } + else { + if(label != "all") { m->splitAtDash(label, labels); allLines = 0; } + else { allLines = 1; } + } + + if ((relabundfile == "") && (sharedfile == "")) { + //is there are current file available for any of these? + //give priority to shared, then relabund + //if there is a current shared file, use it + sharedfile = m->getSharedFile(); + if (sharedfile != "") { inputFileName = sharedfile; m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); } + else { + relabundfile = m->getRelAbundFile(); + if (relabundfile != "") { inputFileName = relabundfile; m->mothurOut("Using " + relabundfile + " as input file for the relabund parameter."); m->mothurOutEndLine(); } + else { + m->mothurOut("You must provide either a shared or relabund file."); m->mothurOutEndLine(); abort = true; + } + } + } + + + if ((relabundfile != "") && (sharedfile != "")) { m->mothurOut("You may only use one of the following : shared or relabund file."); m->mothurOutEndLine(); abort = true; } + + method = validParameter.validFile(parameters, "method", false); if (method == "not found"){ method = "pearson"; } + + 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; } + + } + } + catch(exception& e) { + m->errorOut(e, "OTUAssociationCommand", "OTUAssociationCommand"); + exit(1); + } +} +//********************************************************************************************************************** + +int OTUAssociationCommand::execute(){ + try { + + if (abort == true) { if (calledHelp) { return 0; } return 2; } + + //function are identical just different datatypes + if (sharedfile != "") { processShared(); } + else if (relabundfile != "") { processRelabund(); } + + if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; } + + 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) { + m->errorOut(e, "OTUAssociationCommand", "execute"); + exit(1); + } +} +//********************************************************************************************************************** +int OTUAssociationCommand::processShared(){ + try { + InputData* input = new InputData(sharedfile, "sharedfile"); + vector lookup = input->getSharedRAbundVectors(); + string lastLabel = lookup[0]->getLabel(); + + //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; + + //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))) { + + if (m->control_pressed) { delete input; return 0; } + + if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){ + processedLabels.insert(lookup[0]->getLabel()); + userLabels.erase(lookup[0]->getLabel()); + + m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine(); + process(lookup); + } + + if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) { + string saveLabel = lookup[0]->getLabel(); + + for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } + lookup = input->getSharedRAbundVectors(lastLabel); + + processedLabels.insert(lookup[0]->getLabel()); + userLabels.erase(lookup[0]->getLabel()); + + //restore real lastlabel to save below + lookup[0]->setLabel(saveLabel); + + m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine(); + process(lookup); + } + + lastLabel = lookup[0]->getLabel(); + + //get next line to process + //prevent memory leak + for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } + lookup = input->getSharedRAbundVectors(); + } + + + if (m->control_pressed) { delete input; return 0; } + + //output error messages about any remaining user labels + set::iterator it; + bool needToRun = false; + for (it = userLabels.begin(); it != userLabels.end(); it++) { + m->mothurOut("Your file does not include the label " + *it); + if (processedLabels.count(lastLabel) != 1) { + m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine(); + needToRun = true; + }else { + m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine(); + } + } + + //run last label 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); + + m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine(); + process(lookup); + } + + delete input; + + return 0; + } + catch(exception& e) { + m->errorOut(e, "OTUAssociationCommand", "processShared"); + exit(1); + } +} +//********************************************************************************************************************** +int OTUAssociationCommand::process(vector& lookup){ + try { + + string outputFileName = outputDir + m->getRootName(m->getSimpleName(inputFileName)) + lookup[0]->getLabel() + "." + method + ".otu.corr"; + outputNames.push_back(outputFileName); outputTypes["shared"].push_back(outputFileName); + + ofstream out; + m->openOutputFile(outputFileName, out); + out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint); + + //column headings + out << "OTUA\tOTUB\t" << method << "Coef\tSignificance\n"; + + vector< vector > xy; xy.resize(lookup[0]->getNumBins()); + for (int i = 0; i < lookup[0]->getNumBins(); i++) { for (int j = 0; j < lookup.size(); j++) { xy[i].push_back(lookup[j]->getAbundance(i)); } } + + LinearAlgebra linear; + for (int i = 0; i < xy.size(); i++) { + + for (int k = 0; k < i; k++) { + + if (m->control_pressed) { out.close(); return 0; } + + double coef = 0.0; + double sig = 0.0; + if (method == "spearman") { coef = linear.calcSpearman(xy[i], xy[k], sig); } + else if (method == "pearson") { coef = linear.calcPearson(xy[i], xy[k], sig); } + else if (method == "kendall") { coef = linear.calcKendall(xy[i], xy[k], sig); } + else { m->mothurOut("[ERROR]: invalid method, choices are spearman, pearson or kendall."); m->mothurOutEndLine(); m->control_pressed = true; } + + out << m->binLabelsInFile[i] << '\t' << m->binLabelsInFile[k] << '\t' << coef << '\t' << sig << endl; + } + } + + out.close(); + + return 0; + + } + catch(exception& e) { + m->errorOut(e, "OTUAssociationCommand", "process"); + exit(1); + } +} +//********************************************************************************************************************** +int OTUAssociationCommand::processRelabund(){ + try { + InputData* input = new InputData(relabundfile, "relabund"); + vector lookup = input->getSharedRAbundFloatVectors(); + string lastLabel = lookup[0]->getLabel(); + + //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; + + //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))) { + + if (m->control_pressed) { delete input; return 0; } + + if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){ + processedLabels.insert(lookup[0]->getLabel()); + userLabels.erase(lookup[0]->getLabel()); + + m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine(); + process(lookup); + } + + if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) { + string saveLabel = lookup[0]->getLabel(); + + for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } + lookup = input->getSharedRAbundFloatVectors(lastLabel); + + processedLabels.insert(lookup[0]->getLabel()); + userLabels.erase(lookup[0]->getLabel()); + + //restore real lastlabel to save below + lookup[0]->setLabel(saveLabel); + + m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine(); + process(lookup); + } + + lastLabel = lookup[0]->getLabel(); + + //get next line to process + //prevent memory leak + for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } + lookup = input->getSharedRAbundFloatVectors(); + } + + + if (m->control_pressed) { delete input; return 0; } + + //output error messages about any remaining user labels + set::iterator it; + bool needToRun = false; + for (it = userLabels.begin(); it != userLabels.end(); it++) { + m->mothurOut("Your file does not include the label " + *it); + if (processedLabels.count(lastLabel) != 1) { + m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine(); + needToRun = true; + }else { + m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine(); + } + } + + //run last label if you need to + if (needToRun == true) { + for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } } + lookup = input->getSharedRAbundFloatVectors(lastLabel); + + m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine(); + process(lookup); + } + + delete input; + + return 0; + } + catch(exception& e) { + m->errorOut(e, "OTUAssociationCommand", "processRelabund"); + exit(1); + } +} +//********************************************************************************************************************** +int OTUAssociationCommand::process(vector& lookup){ + try { + + string outputFileName = outputDir + m->getRootName(m->getSimpleName(inputFileName)) + lookup[0]->getLabel() + "." + method + ".otu.corr"; + outputNames.push_back(outputFileName); outputTypes["shared"].push_back(outputFileName); + + ofstream out; + m->openOutputFile(outputFileName, out); + out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint); + + //column headings + out << "OTUA\tOTUB\t" << method << "Coef\tSignificance\n"; + + vector< vector > xy; xy.resize(lookup[0]->getNumBins()); + for (int i = 0; i < lookup[0]->getNumBins(); i++) { for (int j = 0; j < lookup.size(); j++) { xy[i].push_back(lookup[j]->getAbundance(i)); } } + + LinearAlgebra linear; + for (int i = 0; i < xy.size(); i++) { + + for (int k = 0; k < i; k++) { + + if (m->control_pressed) { out.close(); return 0; } + + double coef = 0.0; + double sig = 0.0; + if (method == "spearman") { coef = linear.calcSpearman(xy[i], xy[k], sig); } + else if (method == "pearson") { coef = linear.calcPearson(xy[i], xy[k], sig); } + else if (method == "kendall") { coef = linear.calcKendall(xy[i], xy[k], sig); } + else { m->mothurOut("[ERROR]: invalid method, choices are spearman, pearson or kendall."); m->mothurOutEndLine(); m->control_pressed = true; } + + out << m->binLabelsInFile[i] << '\t' << m->binLabelsInFile[k] << coef << '\t' << sig << endl; + } + } + + out.close(); + + return 0; + + } + catch(exception& e) { + m->errorOut(e, "OTUAssociationCommand", "process"); + exit(1); + } +} +/*****************************************************************/ + + + + + + + + diff --git a/otuassociationcommand.h b/otuassociationcommand.h new file mode 100644 index 0000000..7aaa88a --- /dev/null +++ b/otuassociationcommand.h @@ -0,0 +1,52 @@ +#ifndef OTUASSOCIATIONCOMMAND_H +#define OTUASSOCIATIONCOMMAND_H + +/* + * otuassociationcommand.h + * Mothur + * + * Created by westcott on 1/19/12. + * Copyright 2012 Schloss Lab. All rights reserved. + * + */ + + +#include "command.hpp" +#include "sharedrabundfloatvector.h" +#include "inputdata.h" + + +class OTUAssociationCommand : public Command { +public: + OTUAssociationCommand(string); + OTUAssociationCommand(); + ~OTUAssociationCommand(){} + + vector setParameters(); + string getCommandName() { return "otu.association"; } + string getCommandCategory() { return "Hypothesis Testing"; } + string getHelpString(); + string getCitation() { return "http://www.mothur.org/wiki/Otu.association"; } + string getDescription() { return "calculate the correlation coefficient for the otus in a shared/relabund file"; } + + int execute(); + void help() { m->mothurOut(getHelpString()); } +private: + + string sharedfile, relabundfile, groups, label, inputFileName, outputDir, method; + bool abort, pickedGroups, allLines; + set labels; + + vector outputNames, Groups; + int processShared(); + int process(vector&); + int processRelabund(); + int process(vector&); + +}; + + +#endif + + + -- 2.39.2