2 * matrixoutputcommand.cpp
5 * Created by Sarah Westcott on 5/20/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "matrixoutputcommand.h"
11 #include "sharedjabund.h"
12 #include "sharedsorabund.h"
13 #include "sharedjclass.h"
14 #include "sharedsorclass.h"
15 #include "sharedjest.h"
16 #include "sharedsorest.h"
17 #include "sharedthetayc.h"
18 #include "sharedthetan.h"
19 #include "sharedmorisitahorn.h"
20 #include "sharedbraycurtis.h"
23 //**********************************************************************************************************************
25 MatrixOutputCommand::MatrixOutputCommand(){
27 globaldata = GlobalData::getInstance();
28 validCalculator = new ValidCalculators();
31 for (i=0; i<globaldata->Estimators.size(); i++) {
32 if (validCalculator->isValidCalculator("matrix", globaldata->Estimators[i]) == true) {
33 if (globaldata->Estimators[i] == "jabund") {
34 matrixCalculators.push_back(new JAbund());
35 }else if (globaldata->Estimators[i] == "sorabund") {
36 matrixCalculators.push_back(new SorAbund());
37 }else if (globaldata->Estimators[i] == "jclass") {
38 matrixCalculators.push_back(new Jclass());
39 }else if (globaldata->Estimators[i] == "sorclass") {
40 matrixCalculators.push_back(new SorClass());
41 }else if (globaldata->Estimators[i] == "jest") {
42 matrixCalculators.push_back(new Jest());
43 }else if (globaldata->Estimators[i] == "sorest") {
44 matrixCalculators.push_back(new SorEst());
45 }else if (globaldata->Estimators[i] == "thetayc") {
46 matrixCalculators.push_back(new ThetaYC());
47 }else if (globaldata->Estimators[i] == "thetan") {
48 matrixCalculators.push_back(new ThetaN());
49 }else if (globaldata->Estimators[i] == "morisitahorn") {
50 matrixCalculators.push_back(new MorHorn());
51 }else if (globaldata->Estimators[i] == "braycurtis") {
52 matrixCalculators.push_back(new BrayCurtis());
57 //reset calc for next command
58 globaldata->setCalc("");
62 cout << "Standard Error: " << e.what() << " has occurred in the MatrixOutputCommand class Function MatrixOutputCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
66 cout << "An unknown error has occurred in the MatrixOutputCommand class function MatrixOutputCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
70 //**********************************************************************************************************************
72 MatrixOutputCommand::~MatrixOutputCommand(){
77 //**********************************************************************************************************************
79 int MatrixOutputCommand::execute(){
83 vector<SharedRAbundVector*> subset;
85 //if the users entered no valid calculators don't execute command
86 if (matrixCalculators.size() == 0) { cout << "No valid calculators." << endl; return 0; }
89 read = new ReadOTUFile(globaldata->inputFileName);
90 read->read(&*globaldata);
92 input = globaldata->ginput;
93 lookup = input->getSharedRAbundVectors();
95 if (lookup.size() < 2) { cout << "You have not provided enough valid groups. I cannot run the command." << endl; return 0;}
97 numGroups = globaldata->Groups.size();
99 while(lookup[0] != NULL){
101 if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(lookup[0]->getLabel()) == 1){
103 cout << lookup[0]->getLabel() << '\t' << count << endl;
105 //for each calculator
106 for(int i = 0 ; i < matrixCalculators.size(); i++) {
108 //initialize simMatrix
110 simMatrix.resize(numGroups);
111 for (int m = 0; m < simMatrix.size(); m++) {
112 for (int j = 0; j < simMatrix.size(); j++) {
113 simMatrix[m].push_back(0.0);
117 for (int k = 0; k < lookup.size(); k++) {
118 for (int l = k; l < lookup.size(); l++) {
119 if (k != l) { //we dont need to similiarity of a groups to itself
120 //get estimated similarity between 2 groups
122 subset.clear(); //clear out old pair of sharedrabunds
123 //add new pair of sharedrabunds
124 subset.push_back(lookup[k]); subset.push_back(lookup[l]);
126 data = matrixCalculators[i]->getValues(subset); //saves the calculator outputs
127 //save values in similarity matrix
128 simMatrix[k][l] = 1.0 - data[0]; //convert similiarity to distance
129 simMatrix[l][k] = 1.0 - data[0]; //convert similiarity to distance
134 exportFileName = getRootName(globaldata->inputFileName) + matrixCalculators[i]->getName() + "." + lookup[0]->getLabel() + ".dist";
135 openOutputFile(exportFileName, out);
142 //prevent memory leak
143 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
145 //get next line to process
146 lookup = input->getSharedRAbundVectors();
150 //reset groups parameter
151 globaldata->Groups.clear(); globaldata->setGroups("");
155 catch(exception& e) {
156 cout << "Standard Error: " << e.what() << " has occurred in the MatrixOutputCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
160 cout << "An unknown error has occurred in the MatrixOutputCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
164 /***********************************************************/
165 void MatrixOutputCommand::printSims(ostream& out) {
168 //output column headers
169 out << simMatrix.size() << endl;
171 for (int m = 0; m < simMatrix.size(); m++) {
172 out << lookup[m]->getGroup() << '\t';
173 for (int n = 0; n < m; n++) {
174 out << simMatrix[m][n] << '\t';
180 catch(exception& e) {
181 cout << "Standard Error: " << e.what() << " has occurred in the MatrixOutputCommand class Function printSims. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
185 cout << "An unknown error has occurred in the MatrixOutputCommand class function printSims. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
189 /***********************************************************/