]> git.donarmstrong.com Git - mothur.git/blob - matrixoutputcommand.cpp
added get.rabund and get.sabund command and fixed bug introduced by line by line...
[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                                 
83                 //if the users entered no valid calculators don't execute command
84                 if (matrixCalculators.size() == 0) { cout << "No valid calculators." << endl; return 0; }
85
86                 //you have groups
87                 read = new ReadOTUFile(globaldata->inputFileName);      
88                 read->read(&*globaldata); 
89                         
90                 input = globaldata->ginput;
91                 lookup = input->getSharedRAbundVectors();
92                 vector<SharedRAbundVector*> lastLookup = lookup;
93                 
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;
98                                 
99                 if (lookup.size() < 2) { cout << "You have not provided enough valid groups.  I cannot run the command." << endl; return 0;}
100                 
101                 numGroups = globaldata->Groups.size();
102                                 
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))) {
105                 
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;
108                                 process(lookup);
109                                 
110                                 processedLabels.insert(lookup[0]->getLabel());
111                                 userLabels.erase(lookup[0]->getLabel());
112                                 userLines.erase(count);
113                         }
114                         
115                         if ((anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLookup[0]->getLabel()) != 1)) {
116                                 cout << lastLookup[0]->getLabel() << '\t' << count << endl;
117                                 process(lastLookup);
118                                 
119                                 processedLabels.insert(lastLookup[0]->getLabel());
120                                 userLabels.erase(lastLookup[0]->getLabel());
121                         }
122
123                         //prevent memory leak
124                         if (count != 1) { for (int i = 0; i < lastLookup.size(); i++) {  delete lastLookup[i];  } }
125                         lastLookup = lookup;                    
126                         
127                         //get next line to process
128                         lookup = input->getSharedRAbundVectors();
129                         count++;
130                 }
131                 
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;
139                                 needToRun = true;
140                         }else {
141                                 cout << ". Please refer to " << lastLookup[0]->getLabel() << "." << endl;
142                         }
143                 }
144                 
145                 //run last line if you need to
146                 if (needToRun == true)  {
147                         cout << lastLookup[0]->getLabel() << '\t' << count << endl;
148                         process(lastLookup);            
149                 }
150                 
151                 for (int i = 0; i < lastLookup.size(); i++) {  delete lastLookup[i];  }
152
153                 
154                 //reset groups parameter
155                 globaldata->Groups.clear();  globaldata->setGroups("");
156
157                 return 0;
158         }
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";
161                 exit(1);
162         }
163         catch(...) {
164                 cout << "An unknown error has occurred in the MatrixOutputCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
165                 exit(1);
166         }               
167 }
168 /***********************************************************/
169 void MatrixOutputCommand::printSims(ostream& out) {
170         try {
171                 
172                 //output column headers
173                 out << simMatrix.size() << endl;
174                 
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'; 
179                         }
180                         out << endl;
181                 }
182
183         }
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";
186                 exit(1);
187         }
188         catch(...) {
189                 cout << "An unknown error has occurred in the MatrixOutputCommand class function printSims. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
190                 exit(1);
191         }               
192 }
193 /***********************************************************/
194 void MatrixOutputCommand::process(vector<SharedRAbundVector*> thisLookup){
195         try {
196         
197                                 EstOutput data;
198                                 vector<SharedRAbundVector*> subset;
199
200                                 //for each calculator                                                                                           
201                                 for(int i = 0 ; i < matrixCalculators.size(); i++) {
202                                         
203                                         //initialize simMatrix
204                                         simMatrix.clear();
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);
209                                                 }
210                                         }
211                                 
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
216                                                                 
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]); 
220                                                                 
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
225                                                         }
226                                                 }
227                                         }
228                                         
229                                         exportFileName = getRootName(globaldata->inputFileName) + matrixCalculators[i]->getName() + "." + thisLookup[0]->getLabel() + ".dist";
230                                         openOutputFile(exportFileName, out);
231                                         printSims(out);
232                                         out.close();
233                                         
234                                 }
235
236         
237                 
238         }
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";
241                 exit(1);
242         }
243         catch(...) {
244                 cout << "An unknown error has occurred in the MatrixOutputCommand class function process. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
245                 exit(1);
246         }               
247 }
248 /***********************************************************/
249
250
251