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 //if the users entered no valid calculators don't execute command
84 if (matrixCalculators.size() == 0) { cout << "No valid calculators." << endl; return 0; }
87 read = new ReadOTUFile(globaldata->inputFileName);
88 read->read(&*globaldata);
90 input = globaldata->ginput;
91 lookup = input->getSharedRAbundVectors();
92 vector<SharedRAbundVector*> lastLookup = lookup;
94 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
95 set<string> processedLabels;
96 set<string> userLabels = globaldata->labels;
97 set<int> userLines = globaldata->lines;
99 if (lookup.size() < 2) { cout << "You have not provided enough valid groups. I cannot run the command." << endl; return 0;}
101 numGroups = globaldata->Groups.size();
103 //as long as you are not at the end of the file or done wih the lines you want
104 while((lookup[0] != NULL) && ((globaldata->allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
106 if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(lookup[0]->getLabel()) == 1){
107 cout << lookup[0]->getLabel() << '\t' << count << endl;
110 processedLabels.insert(lookup[0]->getLabel());
111 userLabels.erase(lookup[0]->getLabel());
112 userLines.erase(count);
115 if ((anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLookup[0]->getLabel()) != 1)) {
116 cout << lastLookup[0]->getLabel() << '\t' << count << endl;
119 processedLabels.insert(lastLookup[0]->getLabel());
120 userLabels.erase(lastLookup[0]->getLabel());
123 //prevent memory leak
124 if (count != 1) { for (int i = 0; i < lastLookup.size(); i++) { delete lastLookup[i]; } }
127 //get next line to process
128 lookup = input->getSharedRAbundVectors();
132 //output error messages about any remaining user labels
133 set<string>::iterator it;
134 bool needToRun = false;
135 for (it = userLabels.begin(); it != userLabels.end(); it++) {
136 cout << "Your file does not include the label "<< *it;
137 if (processedLabels.count(lastLookup[0]->getLabel()) != 1) {
138 cout << ". I will use " << lastLookup[0]->getLabel() << "." << endl;
141 cout << ". Please refer to " << lastLookup[0]->getLabel() << "." << endl;
145 //run last line if you need to
146 if (needToRun == true) {
147 cout << lastLookup[0]->getLabel() << '\t' << count << endl;
151 for (int i = 0; i < lastLookup.size(); i++) { delete lastLookup[i]; }
154 //reset groups parameter
155 globaldata->Groups.clear(); globaldata->setGroups("");
159 catch(exception& e) {
160 cout << "Standard Error: " << e.what() << " has occurred in the MatrixOutputCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
164 cout << "An unknown error has occurred in the MatrixOutputCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
168 /***********************************************************/
169 void MatrixOutputCommand::printSims(ostream& out) {
172 //output column headers
173 out << simMatrix.size() << endl;
175 for (int m = 0; m < simMatrix.size(); m++) {
176 out << lookup[m]->getGroup() << '\t';
177 for (int n = 0; n < m; n++) {
178 out << simMatrix[m][n] << '\t';
184 catch(exception& e) {
185 cout << "Standard Error: " << e.what() << " has occurred in the MatrixOutputCommand class Function printSims. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
189 cout << "An unknown error has occurred in the MatrixOutputCommand class function printSims. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
193 /***********************************************************/
194 void MatrixOutputCommand::process(vector<SharedRAbundVector*> thisLookup){
198 vector<SharedRAbundVector*> subset;
200 //for each calculator
201 for(int i = 0 ; i < matrixCalculators.size(); i++) {
203 //initialize simMatrix
205 simMatrix.resize(numGroups);
206 for (int m = 0; m < simMatrix.size(); m++) {
207 for (int j = 0; j < simMatrix.size(); j++) {
208 simMatrix[m].push_back(0.0);
212 for (int k = 0; k < thisLookup.size(); k++) {
213 for (int l = k; l < thisLookup.size(); l++) {
214 if (k != l) { //we dont need to similiarity of a groups to itself
215 //get estimated similarity between 2 groups
217 subset.clear(); //clear out old pair of sharedrabunds
218 //add new pair of sharedrabunds
219 subset.push_back(thisLookup[k]); subset.push_back(thisLookup[l]);
221 data = matrixCalculators[i]->getValues(subset); //saves the calculator outputs
222 //save values in similarity matrix
223 simMatrix[k][l] = 1.0 - data[0]; //convert similiarity to distance
224 simMatrix[l][k] = 1.0 - data[0]; //convert similiarity to distance
229 exportFileName = getRootName(globaldata->inputFileName) + matrixCalculators[i]->getName() + "." + thisLookup[0]->getLabel() + ".dist";
230 openOutputFile(exportFileName, out);
239 catch(exception& e) {
240 cout << "Standard Error: " << e.what() << " has occurred in the MatrixOutputCommand class Function process. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
244 cout << "An unknown error has occurred in the MatrixOutputCommand class function process. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
248 /***********************************************************/