]> git.donarmstrong.com Git - mothur.git/blobdiff - corraxescommand.cpp
added code to check path for uchime and catchall executables
[mothur.git] / corraxescommand.cpp
index 791ec4978fd82d2fb692723fc105dfd5b6c041f0..c27eb4b9ac335112f19244c708cee9bb8630feda 100644 (file)
@@ -9,6 +9,7 @@
 
 #include "corraxescommand.h"
 #include "sharedutilities.h"
+#include "linearalgebra.h"
 
 //**********************************************************************************************************************
 vector<string> CorrAxesCommand::setParameters(){       
@@ -140,12 +141,12 @@ CorrAxesCommand::CorrAxesCommand(string option)  {
                        sharedfile = validParameter.validFile(parameters, "shared", true);
                        if (sharedfile == "not open") { abort = true; }
                        else if (sharedfile == "not found") { sharedfile = ""; }
-                       else { inputFileName = 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; }
+                       else { inputFileName = relabundfile; m->setRelAbundFile(relabundfile); }
                        
                        metadatafile = validParameter.validFile(parameters, "metadata", true);
                        if (metadatafile == "not open") { abort = true; }
@@ -158,7 +159,7 @@ CorrAxesCommand::CorrAxesCommand(string option)  {
                                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);  }
                        
@@ -187,7 +188,7 @@ CorrAxesCommand::CorrAxesCommand(string option)  {
                        }
                        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";             }
                        
@@ -275,7 +276,7 @@ int CorrAxesCommand::execute(){
                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);      }
@@ -304,6 +305,8 @@ int CorrAxesCommand::execute(){
 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++) {
@@ -318,7 +321,7 @@ int CorrAxesCommand::calcPearson(map<string, vector<float> >& axes, ofstream& ou
           //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
@@ -349,8 +352,15 @@ int CorrAxesCommand::calcPearson(map<string, vector<float> >& axes, ofstream& ou
                           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;
@@ -371,6 +381,9 @@ int CorrAxesCommand::calcPearson(map<string, vector<float> >& axes, ofstream& ou
 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;
@@ -410,6 +423,7 @@ int CorrAxesCommand::calcSpearman(map<string, vector<float> >& axes, ofstream& o
                        
                        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]);
@@ -421,6 +435,8 @@ int CorrAxesCommand::calcSpearman(map<string, vector<float> >& axes, ofstream& o
                                                        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;
                                        }
@@ -433,13 +449,14 @@ int CorrAxesCommand::calcSpearman(map<string, vector<float> >& axes, ofstream& o
                                        }
                                }
                        }
+            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
@@ -467,6 +484,7 @@ int CorrAxesCommand::calcSpearman(map<string, vector<float> >& axes, ofstream& o
                        
                        sort(otuScores.begin(), otuScores.end(), compareSpearman);
                        
+            double sg = 0.0;
                        map<string, float> rankOtus;
                        vector<spearmanRank> ties;
                        int rankTotal = 0;
@@ -481,6 +499,8 @@ int CorrAxesCommand::calcSpearman(map<string, vector<float> >& axes, ofstream& o
                                                        float thisrank = rankTotal / (float) ties.size();
                                                        rankOtus[ties[k].name] = thisrank;
                                                }
+                        int t = ties.size();
+                        sg += (t*t*t-t);
                                                ties.clear();
                                                rankTotal = 0;
                                        }
@@ -515,9 +535,15 @@ int CorrAxesCommand::calcSpearman(map<string, vector<float> >& axes, ofstream& o
                                
                                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;
@@ -538,6 +564,8 @@ int CorrAxesCommand::calcSpearman(map<string, vector<float> >& axes, ofstream& o
 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++) {
@@ -581,7 +609,7 @@ int CorrAxesCommand::calcKendall(map<string, vector<float> >& axes, ofstream& ou
                //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
@@ -651,10 +679,14 @@ int CorrAxesCommand::calcKendall(map<string, vector<float> >& axes, ofstream& ou
                                }
                                
                                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;
@@ -759,6 +791,8 @@ int CorrAxesCommand::eliminateZeroOTUS(vector<SharedRAbundFloatVector*>& thisloo
                }
                
                //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; }
                        
@@ -773,12 +807,25 @@ int CorrAxesCommand::eliminateZeroOTUS(vector<SharedRAbundFloatVector*>& thisloo
                                for (int j = 0; j < thislookup.size(); j++) {
                                        newLookup[j]->push_back(thislookup[j]->getAbundance(i), thislookup[j]->getGroup());
                                }
+                               
+                               //if there is a bin label use it otherwise make one
+                               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);
                        }
                }
                
                for (int j = 0; j < thislookup.size(); j++) {  delete thislookup[j];  }
                
                thislookup = newLookup;
+               m->currentBinLabels = newBinLabels;
                
                return 0;
                
@@ -863,14 +910,14 @@ int CorrAxesCommand::getMetadata(){
                //read the first label, because it refers to the groups
                string columnLabel;
                iss >> columnLabel; m->gobble(iss); 
-
+               
                //save names of columns you are reading
                while (!iss.eof()) {
                        iss >> columnLabel; m->gobble(iss);
                        metadataLabels.push_back(columnLabel);
                }
                int count = metadataLabels.size();
-               
+                       
                //read rest of file
                while (!in.eof()) {
                        
@@ -879,7 +926,7 @@ int CorrAxesCommand::getMetadata(){
                        string group = "";
                        in >> group; m->gobble(in);
                        groupNames.push_back(group);
-                       
+                               
                        SharedRAbundFloatVector* tempLookup = new SharedRAbundFloatVector();
                        tempLookup->setGroup(group);
                        tempLookup->setLabel("1");
@@ -887,7 +934,6 @@ int CorrAxesCommand::getMetadata(){
                        for (int i = 0; i < count; i++) {
                                float temp = 0.0;
                                in >> temp; 
-                               
                                tempLookup->push_back(temp, group);
                        }
                        
@@ -900,12 +946,13 @@ int CorrAxesCommand::getMetadata(){
                //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--;