]> git.donarmstrong.com Git - mothur.git/blob - matrixoutputcommand.cpp
This contains Pat's bug fixes and updates. It represents mothur v.1.3.0
[mothur.git] / matrixoutputcommand.cpp
1 /*
2  *  matrixoutputcommand.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 5/20/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
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"
21
22
23 //**********************************************************************************************************************
24
25 MatrixOutputCommand::MatrixOutputCommand(){
26         try {
27                 globaldata = GlobalData::getInstance();
28                 validCalculator = new ValidCalculators();
29                         
30                 int i;
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());
53                                 }
54                         }
55                 }
56                 
57                 //reset calc for next command
58                 globaldata->setCalc("");
59
60         }
61         catch(exception& e) {
62                 cout << "Standard Error: " << e.what() << " has occurred in the MatrixOutputCommand class Function MatrixOutputCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
63                 exit(1);
64         }
65         catch(...) {
66                 cout << "An unknown error has occurred in the MatrixOutputCommand class function MatrixOutputCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
67                 exit(1);
68         }       
69 }
70 //**********************************************************************************************************************
71
72 MatrixOutputCommand::~MatrixOutputCommand(){
73         delete input;
74         delete read;
75 }
76
77 //**********************************************************************************************************************
78
79 int MatrixOutputCommand::execute(){
80         try {
81                 int count = 1;  
82                 EstOutput data;
83                 vector<SharedRAbundVector*> subset;
84                 
85                 //if the users entered no valid calculators don't execute command
86                 if (matrixCalculators.size() == 0) { cout << "No valid calculators." << endl; return 0; }
87
88                 //you have groups
89                 read = new ReadOTUFile(globaldata->inputFileName);      
90                 read->read(&*globaldata); 
91                         
92                 input = globaldata->ginput;
93                 lookup = input->getSharedRAbundVectors();
94                                 
95                 if (lookup.size() < 2) { cout << "You have not provided enough valid groups.  I cannot run the command." << endl; return 0;}
96                 
97                 numGroups = globaldata->Groups.size();
98                                 
99                 while(lookup[0] != NULL){
100                 
101                         if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(lookup[0]->getLabel()) == 1){                   
102                                 
103                                 cout << lookup[0]->getLabel() << '\t' << count << endl;
104                                 
105                                 //for each calculator                                                                                           
106                                 for(int i = 0 ; i < matrixCalculators.size(); i++) {
107                                         
108                                         //initialize simMatrix
109                                         simMatrix.clear();
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);
114                                                 }
115                                         }
116                                 
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
121                                                                 
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]); 
125                                                                 
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
130                                                         }
131                                                 }
132                                         }
133                                         
134                                         exportFileName = getRootName(globaldata->inputFileName) + matrixCalculators[i]->getName() + "." + lookup[0]->getLabel() + ".dist";
135                                         openOutputFile(exportFileName, out);
136                                         printSims(out);
137                                         out.close();
138                                         
139                                 }
140                         }
141                         
142                         //prevent memory leak
143                         for (int i = 0; i < lookup.size(); i++) {       delete lookup[i];       }
144                         
145                         //get next line to process
146                         lookup = input->getSharedRAbundVectors();
147                         count++;
148                 }
149                 
150                 //reset groups parameter
151                 globaldata->Groups.clear();  globaldata->setGroups("");
152
153                 return 0;
154         }
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";
157                 exit(1);
158         }
159         catch(...) {
160                 cout << "An unknown error has occurred in the MatrixOutputCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
161                 exit(1);
162         }               
163 }
164 /***********************************************************/
165 void MatrixOutputCommand::printSims(ostream& out) {
166         try {
167                 
168                 //output column headers
169                 out << simMatrix.size() << endl;
170                 
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'; 
175                         }
176                         out << endl;
177                 }
178
179         }
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";
182                 exit(1);
183         }
184         catch(...) {
185                 cout << "An unknown error has occurred in the MatrixOutputCommand class function printSims. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
186                 exit(1);
187         }               
188 }
189 /***********************************************************/
190
191