#include "corraxescommand.h"
#include "sharedutilities.h"
+#include "linearalgebra.h"
//**********************************************************************************************************************
vector<string> CorrAxesCommand::setParameters(){
try {
- CommandParameter paxes("axes", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(paxes);
- 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 pmetadata("metadata", "InputTypes", "", "", "SharedRelMeta", "SharedRelMeta", "none",false,false); parameters.push_back(pmetadata);
- CommandParameter pnumaxes("numaxes", "Number", "", "3", "", "", "",false,false); parameters.push_back(pnumaxes);
- 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);
+ CommandParameter paxes("axes", "InputTypes", "", "", "none", "none", "none","corraxes",false,true,true); parameters.push_back(paxes);
+ CommandParameter pshared("shared", "InputTypes", "", "", "SharedRelMeta", "SharedRelMeta", "none","",false,false,true); parameters.push_back(pshared);
+ CommandParameter prelabund("relabund", "InputTypes", "", "", "SharedRelMeta", "SharedRelMeta", "none","",false,false,true); parameters.push_back(prelabund);
+ CommandParameter pmetadata("metadata", "InputTypes", "", "", "SharedRelMeta", "SharedRelMeta", "none","",false,false); parameters.push_back(pmetadata);
+ CommandParameter pnumaxes("numaxes", "Number", "", "3", "", "", "","",false,false); parameters.push_back(pnumaxes);
+ 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<string> myArray;
for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
exit(1);
}
}
+//**********************************************************************************************************************
+string CorrAxesCommand::getOutputPattern(string type) {
+ try {
+ string pattern = "";
+
+ if (type == "corraxes") { pattern = "[filename],[tag],corr.axes"; }
+ else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; }
+
+ return pattern;
+ }
+ catch(exception& e) {
+ m->errorOut(e, "CorrAxesCommand", "getOutputPattern");
+ exit(1);
+ }
+}
+
//**********************************************************************************************************************
CorrAxesCommand::CorrAxesCommand(){
try {
abort = true; calledHelp = true;
setParameters();
vector<string> tempOutNames;
- outputTypes["corr.axes"] = tempOutNames;
+ outputTypes["corraxes"] = tempOutNames;
}
catch(exception& e) {
m->errorOut(e, "CorrAxesCommand", "CorrAxesCommand");
}
vector<string> tempOutNames;
- outputTypes["corr.axes"] = tempOutNames;
+ outputTypes["corraxes"] = 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);
pickedGroups = true;
m->splitAtDash(groups, Groups);
}
- m->Groups = Groups;
+ m->setGroups(Groups);
outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(inputFileName); }
}
string temp;
temp = validParameter.validFile(parameters, "numaxes", false); if (temp == "not found"){ temp = "3"; }
- convert(temp, numaxes);
+ m->mothurConvert(temp, numaxes);
method = validParameter.validFile(parameters, "method", false); if (method == "not found"){ method = "pearson"; }
/*************************************************************************************/
// calc the r values //
/************************************************************************************/
-
- string outputFileName = outputDir + m->getRootName(m->getSimpleName(inputFileName)) + method + ".corr.axes";
- outputNames.push_back(outputFileName); outputTypes["corr.axes"].push_back(outputFileName);
+ map<string, string> variables;
+ variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(inputFileName));
+ variables["[tag]"] = method;
+ string outputFileName = getOutputFileName("corraxes", variables);
+ outputNames.push_back(outputFileName); outputTypes["corraxes"].push_back(outputFileName);
ofstream out;
m->openOutputFile(outputFileName, out);
out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
if (metadatafile == "") { out << "OTU"; }
else { out << "Feature"; }
- for (int i = 0; i < numaxes; i++) { out << '\t' << "axis" << (i+1); }
+ for (int i = 0; i < numaxes; i++) { out << '\t' << "axis" << (i+1) << "\tp-value"; }
out << "\tlength" << endl;
if (method == "pearson") { calcPearson(axes, out); }
int CorrAxesCommand::calcPearson(map<string, vector<float> >& axes, ofstream& out) {
try {
+ LinearAlgebra linear;
+
//find average of each axis - X
vector<float> averageAxes; averageAxes.resize(numaxes, 0.0);
for (map<string, vector<float> >::iterator it = axes.begin(); it != axes.end(); it++) {
//for each otu
for (int i = 0; i < lookupFloat[0]->getNumBins(); i++) {
- if (metadatafile == "") { out << i+1; }
+ if (metadatafile == "") { out << m->currentBinLabels[i]; }
else { out << metadataLabels[i]; }
//find the averages this otu - Y
double denom = (sqrt(denomTerm1) * sqrt(denomTerm2));
r = numerator / denom;
+
+ if (isnan(r) || isinf(r)) { r = 0.0; }
+
rValues[k] = r;
out << '\t' << r;
+
+ double sig = linear.calcPearsonSig(lookupFloat.size(), r);
+
+ out << '\t' << sig;
}
double sum = 0;
int CorrAxesCommand::calcSpearman(map<string, vector<float> >& axes, ofstream& out) {
try {
+ LinearAlgebra linear;
+ vector<double> sf;
+
//format data
vector< map<float, int> > tableX; tableX.resize(numaxes);
map<float, int>::iterator itTable;
vector<spearmanRank> ties;
int rankTotal = 0;
+ double sfTemp = 0.0;
for (int j = 0; j < scores[i].size(); j++) {
rankTotal += (j+1);
ties.push_back(scores[i][j]);
float thisrank = rankTotal / (float) ties.size();
rankAxes[ties[k].name].push_back(thisrank);
}
+ int t = ties.size();
+ sfTemp += (t*t*t-t);
ties.clear();
rankTotal = 0;
}
}
}
}
+ sf.push_back(sfTemp);
}
//for each otu
for (int i = 0; i < lookupFloat[0]->getNumBins(); i++) {
- if (metadatafile == "") { out << i+1; }
+ if (metadatafile == "") { out << m->currentBinLabels[i]; }
else { out << metadataLabels[i]; }
//find the ranks of this otu - Y
sort(otuScores.begin(), otuScores.end(), compareSpearman);
+ double sg = 0.0;
map<string, float> rankOtus;
vector<spearmanRank> ties;
int rankTotal = 0;
float thisrank = rankTotal / (float) ties.size();
rankOtus[ties[k].name] = thisrank;
}
+ int t = ties.size();
+ sg += (t*t*t-t);
ties.clear();
rankTotal = 0;
}
p = (SX2 + SY2 - di) / (2.0 * sqrt((SX2*SY2)));
+ if (isnan(p) || isinf(p)) { p = 0.0; }
+
out << '\t' << p;
pValues[j] = p;
+
+ double sig = linear.calcSpearmanSig(n, sf[j], sg, di);
+ out << '\t' << sig;
+
}
double sum = 0;
int CorrAxesCommand::calcKendall(map<string, vector<float> >& axes, ofstream& out) {
try {
+ LinearAlgebra linear;
+
//format data
vector< vector<spearmanRank> > scores; scores.resize(numaxes);
for (map<string, vector<float> >::iterator it = axes.begin(); it != axes.end(); it++) {
//for each otu
for (int i = 0; i < lookupFloat[0]->getNumBins(); i++) {
- if (metadatafile == "") { out << i+1; }
+ if (metadatafile == "") { out << m->currentBinLabels[i]; }
else { out << metadataLabels[i]; }
//find the ranks of this otu - Y
}
double p = (numCoor - numDisCoor) / (float) count;
-
+ if (isnan(p) || isinf(p)) { p = 0.0; }
+
out << '\t' << p;
pValues[j] = p;
-
+
+ double sig = linear.calcKendallSig(scores[j].size(), p);
+
+ out << '\t' << sig;
}
double sum = 0;
//for each bin
vector<string> newBinLabels;
+ string snumBins = toString(thislookup[0]->getNumBins());
for (int i = 0; i < thislookup[0]->getNumBins(); i++) {
if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) { delete newLookup[j]; } return 0; }
}
//if there is a bin label use it otherwise make one
- string binLabel = "Otu" + (i+1);
+ string binLabel = "Otu";
+ string sbinNumber = toString(i+1);
+ if (sbinNumber.length() < snumBins.length()) {
+ int diff = snumBins.length() - sbinNumber.length();
+ for (int h = 0; h < diff; h++) { binLabel += "0"; }
+ }
+ binLabel += sbinNumber;
if (i < m->currentBinLabels.size()) { binLabel = m->currentBinLabels[i]; }
newBinLabels.push_back(binLabel);
m->openInputFile(metadatafile, in);
string headerLine = m->getline(in); m->gobble(in);
- istringstream iss (headerLine,istringstream::in);
-
- //read the first label, because it refers to the groups
- string columnLabel;
- iss >> columnLabel; m->gobble(iss);
+ vector<string> pieces = m->splitWhiteSpace(headerLine);
//save names of columns you are reading
- while (!iss.eof()) {
- iss >> columnLabel; m->gobble(iss);
- metadataLabels.push_back(columnLabel);
+ for (int i = 1; i < pieces.size(); i++) {
+ metadataLabels.push_back(pieces[i]);
}
int count = metadataLabels.size();
//remove any groups the user does not want, and set globaldata->groups with only valid groups
SharedUtil* util;
util = new SharedUtil();
-
- util->setGroups(m->Groups, groupNames);
+ Groups = m->getGroups();
+ util->setGroups(Groups, groupNames);
+ m->setGroups(Groups);
for (int i = 0; i < lookupFloat.size(); i++) {
//if this sharedrabund is not from a group the user wants then delete it.
- if (util->isValidGroup(lookupFloat[i]->getGroup(), m->Groups) == false) {
+ if (util->isValidGroup(lookupFloat[i]->getGroup(), m->getGroups()) == false) {
delete lookupFloat[i]; lookupFloat[i] = NULL;
lookupFloat.erase(lookupFloat.begin()+i);
i--;