#include "corraxescommand.h"
#include "sharedutilities.h"
+#include "linearalgebra.h"
//**********************************************************************************************************************
vector<string> CorrAxesCommand::setParameters(){
}
}
//**********************************************************************************************************************
+string CorrAxesCommand::getOutputFileNameTag(string type, string inputName=""){
+ try {
+ string outputFileName = "";
+ map<string, vector<string> >::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 == "corraxes") { outputFileName = "corr.axes"; }
+ 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, "CorrAxesCommand", "getOutputFileNameTag");
+ 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);
// 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);
+ string outputFileName = outputDir + m->getRootName(m->getSimpleName(inputFileName)) + method + "." + getOutputFileNameTag("corraxes");
+ 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);
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
rValues[k] = r;
out << '\t' << r;
- //signifigance calc - http://faculty.vassar.edu/lowry/ch4apx.html
- double temp = (1- (r*r)) / (double) (lookupFloat.size()-2);
- temp = sqrt(temp);
- double sig = r / temp;
- if (isnan(sig) || isinf(sig)) { sig = 0.0; }
+ double sig = linear.calcPearsonSig(lookupFloat.size(), r);
out << '\t' << sig;
}
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;
}
pValues[j] = p;
- //signifigance calc - http://en.wikipedia.org/wiki/Spearman%27s_rank_correlation_coefficient
- double temp = (lookupFloat.size()-2) / (double) (1- (p*p));
- temp = sqrt(temp);
- double sig = p*temp;
- if (isnan(sig) || isinf(sig)) { sig = 0.0; }
-
+ double sig = linear.calcSpearmanSig(n, sf[j], sg, di);
out << '\t' << sig;
}
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
out << '\t' << p;
pValues[j] = p;
- //calc signif - zA - http://en.wikipedia.org/wiki/Kendall_tau_rank_correlation_coefficient#Significance_tests
- double numer = 3.0 * (numCoor - numDisCoor);
- int n = scores[j].size();
- double denom = n * (n-1) * (2*n + 5) / (double) 2.0;
- denom = sqrt(denom);
- double sig = numer / denom;
-
- if (isnan(sig) || isinf(sig)) { sig = 0.0; }
+ double sig = linear.calcKendallSig(scores[j].size(), p);
out << '\t' << sig;
}