void CorrAxesCommand::help(){
try {
- m->mothurOut("The corr.axes command reads a shared or relabund file as well as a pcoa file and calculates the correlation coefficient.\n");
+ m->mothurOut("The corr.axes command reads a shared, relabund or metadata file as well as an axes file and calculates the correlation coefficient.\n");
m->mothurOut("The corr.axes command parameters are shared, relabund, axes, metadata, groups, method, numaxes and label. The shared, relabund or metadata and axes parameters are required. If shared is given the relative abundance is calculated.\n");
m->mothurOut("The groups parameter allows you to specify which of the groups you would like included. The group names are separated by dashes.\n");
m->mothurOut("The label parameter allows you to select what distance level you would like used, if none is given the first distance is used.\n");
// use smart distancing to get right sharedRabund and convert to relabund if needed //
/************************************************************************************/
if (sharedfile != "") {
- getShared();
- if (m->control_pressed) { for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } return 0; }
- if (lookup[0] == NULL) { m->mothurOut("[ERROR] reading shared file."); m->mothurOutEndLine(); return 0; }
+ InputData* input = new InputData(sharedfile, "sharedfile");
+ getSharedFloat(input);
+ delete input;
- //fills lookupFloat with relative abundance values from lookup
- convertToRelabund();
-
- for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
+ if (m->control_pressed) { for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; } return 0; }
+ if (lookupFloat[0] == NULL) { m->mothurOut("[ERROR] reading relabund file."); m->mothurOutEndLine(); return 0; }
}else if (relabundfile != "") {
- getSharedFloat();
+ InputData* input = new InputData(relabundfile, "relabund");
+ getSharedFloat(input);
+ delete input;
+
if (m->control_pressed) { for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; } return 0; }
if (lookupFloat[0] == NULL) { m->mothurOut("[ERROR] reading relabund file."); m->mothurOutEndLine(); return 0; }
- if (pickedGroups) { eliminateZeroOTUS(lookupFloat); }
-
}else if (metadatafile != "") {
getMetadata(); //reads metadata file and store in lookupFloat, saves column headings in metadataLabels for later
if (m->control_pressed) { for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; } return 0; }
out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
//output headings
- if (metadatafile == "") { out << "OTU\t"; }
- else { out << "Feature\t"; }
+ if (metadatafile == "") { out << "OTU"; }
+ else { out << "Feature"; }
- for (int i = 0; i < numaxes; i++) { out << "axis" << (i+1) << '\t'; }
- out << endl;
+ for (int i = 0; i < numaxes; i++) { out << '\t' << "axis" << (i+1); }
+ out << "\tlength" << endl;
if (method == "pearson") { calcPearson(axes, out); }
else if (method == "spearman") { calcSpearman(axes, out); }
//for each otu
for (int i = 0; i < lookupFloat[0]->getNumBins(); i++) {
- if (metadatafile == "") { out << i+1 << '\t'; }
- else { out << metadataLabels[i] << '\t'; }
-
+ if (metadatafile == "") { out << i+1; }
+ else { out << metadataLabels[i]; }
+
//find the averages this otu - Y
float sumOtu = 0.0;
for (int j = 0; j < lookupFloat.size(); j++) {
}
float Ybar = sumOtu / (float) lookupFloat.size();
+ vector<float> rValues(averageAxes.size());
+
//find r value for each axis
for (int k = 0; k < averageAxes.size(); k++) {
double denom = (sqrt(denomTerm1) * sqrt(denomTerm2));
r = numerator / denom;
-
- out << r << '\t';
+ rValues[k] = r;
+ out << '\t' << r;
}
- out << endl;
+ double sum = 0;
+ for(int k=0;k<rValues.size();k++){
+ sum += rValues[k] * rValues[k];
+ }
+ out << '\t' << sqrt(sum) << endl;
}
return 0;
int CorrAxesCommand::calcSpearman(map<string, vector<float> >& axes, ofstream& out) {
try {
+ /*axes.clear();
+ axes["1a"].push_back(1);
+ axes["1b"].push_back(1);
+ axes["1c"].push_back(1);
+ axes["2a"].push_back(2);
+ axes["2b"].push_back(2);
+ axes["2c"].push_back(2);
+ axes["3a"].push_back(3);
+ axes["3b"].push_back(3);
+ axes["3c"].push_back(3);
+ axes["4a"].push_back(4);
+ axes["4b"].push_back(4);
+ axes["4c"].push_back(4);
+ axes["5a"].push_back(5);
+ axes["5b"].push_back(5);
+ axes["5c"].push_back(5);*/
+
//format data
+ vector< map<float, int> > tableX; tableX.resize(numaxes);
+ map<float, int>::iterator itTable;
vector< vector<spearmanRank> > scores; scores.resize(numaxes);
for (map<string, vector<float> >::iterator it = axes.begin(); it != axes.end(); it++) {
vector<float> temp = it->second;
for (int i = 0; i < temp.size(); i++) {
spearmanRank member(it->first, temp[i]);
scores[i].push_back(member);
+
+ //count number of repeats
+ itTable = tableX[i].find(temp[i]);
+ if (itTable == tableX[i].end()) {
+ tableX[i][temp[i]] = 1;
+ }else {
+ tableX[i][temp[i]]++;
+ }
+ }
+ }
+
+ //calc LX
+ //for each axis
+ vector<double> Lx; Lx.resize(numaxes, 0.0);
+ for (int i = 0; i < numaxes; i++) {
+ for (itTable = tableX[i].begin(); itTable != tableX[i].end(); itTable++) {
+ double tx = (double) itTable->second;
+ Lx[i] += ((pow(tx, 3.0) - tx) / 12.0);
}
}
vector<spearmanRank> ties;
int rankTotal = 0;
for (int j = 0; j < scores[i].size(); j++) {
- rankTotal += j;
+ rankTotal += (j+1);
ties.push_back(scores[i][j]);
- if (j != scores.size()-1) { // you are not the last so you can look ahead
+ if (j != (scores[i].size()-1)) { // you are not the last so you can look ahead
if (scores[i][j].score != scores[i][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();
rankAxes[ties[k].name].push_back(thisrank);
rankTotal = 0;
}
}else { // you are the last one
+
for (int k = 0; k < ties.size(); k++) {
float thisrank = rankTotal / (float) ties.size();
rankAxes[ties[k].name].push_back(thisrank);
+
}
}
}
}
-
+ /*cout << endl;
+ lookupFloat.clear();
+ lookupFloat.resize(15);
+ for (int i = 0; i < lookupFloat.size(); i++) {
+ lookupFloat[i] = new SharedRAbundFloatVector();
+ }
+ lookupFloat[0]->push_back(0.2288227, "1a"); lookupFloat[0]->setGroup("1a");
+ lookupFloat[1]->push_back(0.7394062, "1b"); lookupFloat[1]->setGroup("1b");
+ lookupFloat[2]->push_back(0.4521187, "1c"); lookupFloat[2]->setGroup("1c");
+ lookupFloat[3]->push_back(0.1598630, "2a"); lookupFloat[3]->setGroup("2a");
+ lookupFloat[4]->push_back(0.09588156, "2b"); lookupFloat[4]->setGroup("2b");
+
+ lookupFloat[5]->push_back(0.933174, "2c"); lookupFloat[5]->setGroup("2c");
+ lookupFloat[6]->push_back(0.3958304, "3a"); lookupFloat[6]->setGroup("3a");;
+ lookupFloat[7]->push_back(0.2364419, "3b"); lookupFloat[7]->setGroup("3b");
+ lookupFloat[8]->push_back(0.1697712, "3c"); lookupFloat[8]->setGroup("3c");
+ lookupFloat[9]->push_back(0.4077173, "4a"); lookupFloat[9]->setGroup("4a");
+
+ lookupFloat[10]->push_back(0.6116547, "4b"); lookupFloat[10]->setGroup("4b");
+ lookupFloat[11]->push_back(0.9374322, "4c"); lookupFloat[11]->setGroup("4c");
+ lookupFloat[12]->push_back(0.852184, "5a"); lookupFloat[12]->setGroup("5a");
+ lookupFloat[13]->push_back(0.845094, "5b"); lookupFloat[13]->setGroup("5b");
+ lookupFloat[14]->push_back(0.5795778, "5c"); lookupFloat[14]->setGroup("5c");*/
//for each otu
for (int i = 0; i < lookupFloat[0]->getNumBins(); i++) {
- if (metadatafile == "") { out << i+1 << '\t'; }
- else { out << metadataLabels[i] << '\t'; }
+ if (metadatafile == "") { out << i+1; }
+ else { out << metadataLabels[i]; }
//find the ranks of this otu - Y
vector<spearmanRank> otuScores;
+ map<float, int> tableY;
for (int j = 0; j < lookupFloat.size(); j++) {
spearmanRank member(lookupFloat[j]->getGroup(), lookupFloat[j]->getAbundance(i));
otuScores.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(otuScores.begin(), otuScores.end(), compareSpearman);
vector<spearmanRank> ties;
int rankTotal = 0;
for (int j = 0; j < otuScores.size(); j++) {
- rankTotal += j;
+ rankTotal += (j+1);
ties.push_back(otuScores[j]);
- if (j != scores.size()-1) { // you are not the last so you can look ahead
+ if (j != (otuScores.size()-1)) { // you are not the last so you can look ahead
if (otuScores[j].score != otuScores[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();
rankOtus[ties[k].name] = thisrank;
rankTotal = 0;
}
}else { // you are the last one
+
for (int k = 0; k < ties.size(); k++) {
float thisrank = rankTotal / (float) ties.size();
rankOtus[ties[k].name] = thisrank;
}
}
}
-
+ vector<double> pValues(numaxes);
+
//calc spearman ranks for each axis for this otu
for (int j = 0; j < numaxes; j++) {
di += ((xi - yi) * (xi - yi));
}
- int n = lookupFloat.size();
- double p = 1.0 - ((6 * di) / (float) (n * ((n*n) - 1)));
+ double p = 0.0;
- out << p << '\t';
+ double n = (double) lookupFloat.size();
+
+ double SX2 = ((pow(n, 3.0) - n) / 12.0) - Lx[j];
+ double SY2 = ((pow(n, 3.0) - n) / 12.0) - Ly;
+
+ p = (SX2 + SY2 - di) / (2.0 * sqrt((SX2*SY2)));
+
+ out << '\t' << p;
+
+ pValues[j] = p;
}
-
-
- out << endl;
+
+ double sum = 0;
+ for(int k=0;k<numaxes;k++){
+ sum += pValues[k] * pValues[k];
+ }
+ out << '\t' << sqrt(sum) << endl;
}
return 0;
//for each otu
for (int i = 0; i < lookupFloat[0]->getNumBins(); i++) {
- if (metadatafile == "") { out << i+1 << '\t'; }
- else { out << metadataLabels[i] << '\t'; }
+ if (metadatafile == "") { out << i+1; }
+ else { out << metadataLabels[i]; }
//find the ranks of this otu - Y
vector<spearmanRank> otuScores;
}
}
}
+ vector<double> pValues(numaxes);
//calc spearman ranks for each axis for this otu
for (int j = 0; j < numaxes; j++) {
double p = ( (4 * P) / (float) (n * (n - 1)) ) - 1.0;
- out << p << '\t';
- }
-
- out << endl;
- }
-
- return 0;
- }
- catch(exception& e) {
- m->errorOut(e, "CorrAxesCommand", "calcKendall");
- exit(1);
- }
-}
-//**********************************************************************************************************************
-int CorrAxesCommand::getShared(){
- try {
- InputData* input = new InputData(sharedfile, "sharedfile");
- lookup = input->getSharedRAbundVectors();
- string lastLabel = lookup[0]->getLabel();
-
- if (label == "") { label = lastLabel; delete input; return 0; }
-
- //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
- set<string> labels; labels.insert(label);
- set<string> processedLabels;
- set<string> 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) && (userLabels.size() != 0)) {
- if (m->control_pressed) { delete input; return 0; }
-
- if(labels.count(lookup[0]->getLabel()) == 1){
- processedLabels.insert(lookup[0]->getLabel());
- userLabels.erase(lookup[0]->getLabel());
- break;
- }
-
- 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);
- break;
+ out << '\t' << p;
+ pValues[j] = p;
+
}
- 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<string>::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();
+ double sum = 0;
+ for(int k=0;k<numaxes;k++){
+ sum += pValues[k] * pValues[k];
}
+ out << '\t' << sqrt(sum) << endl;
}
- //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);
- }
-
- delete input;
return 0;
}
catch(exception& e) {
- m->errorOut(e, "CorrAxesCommand", "getShared");
+ m->errorOut(e, "CorrAxesCommand", "calcKendall");
exit(1);
}
}
//**********************************************************************************************************************
-int CorrAxesCommand::getSharedFloat(){
+int CorrAxesCommand::getSharedFloat(InputData* input){
try {
- InputData* input = new InputData(relabundfile, "relabund");
lookupFloat = input->getSharedRAbundFloatVectors();
string lastLabel = lookupFloat[0]->getLabel();
- if (label == "") { label = lastLabel; delete input; return 0; }
+ if (label == "") { label = lastLabel; return 0; }
//if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
set<string> labels; labels.insert(label);
//as long as you are not at the end of the file or done wih the lines you want
while((lookupFloat[0] != NULL) && (userLabels.size() != 0)) {
- if (m->control_pressed) { delete input; return 0; }
+ if (m->control_pressed) { return 0; }
if(labels.count(lookupFloat[0]->getLabel()) == 1){
processedLabels.insert(lookupFloat[0]->getLabel());
}
- if (m->control_pressed) { delete input; return 0; }
+ if (m->control_pressed) { return 0; }
//output error messages about any remaining user labels
set<string>::iterator it;
lookupFloat = input->getSharedRAbundFloatVectors(lastLabel);
}
- delete input;
return 0;
}
catch(exception& e) {
exit(1);
}
}
-/*****************************************************************/
-int CorrAxesCommand::convertToRelabund(){
- try {
-
- vector<SharedRAbundFloatVector*> newLookup;
- for (int i = 0; i < lookup.size(); i++) {
- SharedRAbundFloatVector* temp = new SharedRAbundFloatVector();
- temp->setLabel(lookup[i]->getLabel());
- temp->setGroup(lookup[i]->getGroup());
- newLookup.push_back(temp);
- }
-
- for (int i = 0; i < lookup.size(); i++) {
-
- for (int j = 0; j < lookup[i]->getNumBins(); j++) {
-
- if (m->control_pressed) { return 0; }
-
- int abund = lookup[i]->getAbundance(j);
-
- float relabund = abund / (float) lookup[i]->getNumSeqs();
-
- newLookup[i]->push_back(relabund, lookup[i]->getGroup());
- }
- }
-
- if (pickedGroups) { eliminateZeroOTUS(newLookup); }
-
- lookupFloat = newLookup;
-
- return 0;
- }
- catch(exception& e) {
- m->errorOut(e, "CorrAxesCommand", "convertToRelabund");
- exit(1);
- }
-}
//**********************************************************************************************************************
int CorrAxesCommand::eliminateZeroOTUS(vector<SharedRAbundFloatVector*>& thislookup) {
try {