]> git.donarmstrong.com Git - mothur.git/blob - matrixoutputcommand.cpp
added pipeline commands which involved change to command factory and command class...
[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 vector<string> MatrixOutputCommand::getValidParameters(){       
24         try {
25                 string Array[] =  {"label","calc","groups","outputdir","inputdir", "output"};
26                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
27                 return myArray;
28         }
29         catch(exception& e) {
30                 m->errorOut(e, "MatrixOutputCommand", "getValidParameters");
31                 exit(1);
32         }
33 }
34 //**********************************************************************************************************************
35 MatrixOutputCommand::MatrixOutputCommand(){     
36         try {
37                 //initialize outputTypes
38                 vector<string> tempOutNames;
39                 outputTypes["phylip"] = tempOutNames;
40         }
41         catch(exception& e) {
42                 m->errorOut(e, "MatrixOutputCommand", "MatrixOutputCommand");
43                 exit(1);
44         }
45 }
46 //**********************************************************************************************************************
47 vector<string> MatrixOutputCommand::getRequiredParameters(){    
48         try {
49                 vector<string> myArray;
50                 return myArray;
51         }
52         catch(exception& e) {
53                 m->errorOut(e, "MatrixOutputCommand", "getRequiredParameters");
54                 exit(1);
55         }
56 }
57 //**********************************************************************************************************************
58 vector<string> MatrixOutputCommand::getRequiredFiles(){ 
59         try {
60                 string Array[] =  {"shared"};
61                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
62                 return myArray;
63         }
64         catch(exception& e) {
65                 m->errorOut(e, "MatrixOutputCommand", "getRequiredFiles");
66                 exit(1);
67         }
68 }
69 //**********************************************************************************************************************
70
71 MatrixOutputCommand::MatrixOutputCommand(string option)  {
72         try {
73                 globaldata = GlobalData::getInstance();
74                 abort = false;
75                 allLines = 1;
76                 labels.clear();
77                 Groups.clear();
78                 Estimators.clear();
79                 
80                 //allow user to run help
81                 if(option == "help") { validCalculator = new ValidCalculators(); help(); abort = true; }
82                 
83                 else {
84                         //valid paramters for this command
85                         string Array[] =  {"label","calc","groups","outputdir","inputdir", "output"};
86                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
87                         
88                         OptionParser parser(option);
89                         map<string,string> parameters  = parser.getParameters();
90                         
91                         ValidParameters validParameter;
92                 
93                         //check to make sure all parameters are valid for command
94                         for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
95                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
96                         }
97                         
98                         //initialize outputTypes
99                         vector<string> tempOutNames;
100                         outputTypes["phylip"] = tempOutNames;
101                         
102                         //if the user changes the output directory command factory will send this info to us in the output parameter 
103                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
104                                 outputDir = ""; 
105                                 outputDir += m->hasPath(globaldata->inputFileName); //if user entered a file with a path then preserve it       
106                         }
107                         
108                         //make sure the user has already run the read.otu command
109                         if (globaldata->getSharedFile() == "") {
110                                 if (globaldata->getListFile() == "") { m->mothurOut("You must read a list and a group, or a shared before you can use the dist.shared command."); m->mothurOutEndLine(); abort = true; }
111                                 else if (globaldata->getGroupFile() == "") { m->mothurOut("You must read a list and a group, or a shared before you can use the dist.shared command."); m->mothurOutEndLine(); abort = true; }
112                         }
113                         
114                         //check for optional parameter and set defaults
115                         // ...at some point should added some additional type checking...
116                         label = validParameter.validFile(parameters, "label", false);                   
117                         if (label == "not found") { label = ""; }
118                         else { 
119                                 if(label != "all") {  m->splitAtDash(label, labels);  allLines = 0;  }
120                                 else { allLines = 1;  }
121                         }
122                         
123                         output = validParameter.validFile(parameters, "output", false);         if(output == "not found"){      output = "lt"; }
124                         if ((output != "lt") && (output != "square")) { m->mothurOut(output + " is not a valid output form. Options are lt and square. I will use lt."); m->mothurOutEndLine(); output = "lt"; }
125                         
126                         //if the user has not specified any labels use the ones from read.otu
127                         if (label == "") {  
128                                 allLines = globaldata->allLines; 
129                                 labels = globaldata->labels; 
130                         }
131                                 
132                         groups = validParameter.validFile(parameters, "groups", false);                 
133                         if (groups == "not found") { groups = ""; }
134                         else { 
135                                 m->splitAtDash(groups, Groups);
136                                 globaldata->Groups = Groups;
137                         }
138                                 
139                         calc = validParameter.validFile(parameters, "calc", false);                     
140                         if (calc == "not found") { calc = "jclass-thetayc";  }
141                         else { 
142                                  if (calc == "default")  {  calc = "jclass-thetayc";  }
143                         }
144                         m->splitAtDash(calc, Estimators);
145
146                         if (abort == false) {
147                         
148                                 validCalculator = new ValidCalculators();
149                                 
150                                 int i;
151                                 for (i=0; i<Estimators.size(); i++) {
152                                         if (validCalculator->isValidCalculator("matrix", Estimators[i]) == true) { 
153                                                 if (Estimators[i] == "jabund") {        
154                                                         matrixCalculators.push_back(new JAbund());
155                                                 }else if (Estimators[i] == "sorabund") { 
156                                                         matrixCalculators.push_back(new SorAbund());
157                                                 }else if (Estimators[i] == "jclass") { 
158                                                         matrixCalculators.push_back(new Jclass());
159                                                 }else if (Estimators[i] == "sorclass") { 
160                                                         matrixCalculators.push_back(new SorClass());
161                                                 }else if (Estimators[i] == "jest") { 
162                                                         matrixCalculators.push_back(new Jest());
163                                                 }else if (Estimators[i] == "sorest") { 
164                                                         matrixCalculators.push_back(new SorEst());
165                                                 }else if (Estimators[i] == "thetayc") { 
166                                                         matrixCalculators.push_back(new ThetaYC());
167                                                 }else if (Estimators[i] == "thetan") { 
168                                                         matrixCalculators.push_back(new ThetaN());
169                                                 }else if (Estimators[i] == "morisitahorn") { 
170                                                         matrixCalculators.push_back(new MorHorn());
171                                                 }else if (Estimators[i] == "braycurtis") { 
172                                                         matrixCalculators.push_back(new BrayCurtis());
173                                                 }
174                                         }
175                                 }
176                                 
177                         }
178                 }
179                 
180         }
181         catch(exception& e) {
182                 m->errorOut(e, "MatrixOutputCommand", "MatrixOutputCommand");
183                 exit(1);
184         }
185 }
186
187 //**********************************************************************************************************************
188
189 void MatrixOutputCommand::help(){
190         try {
191                 m->mothurOut("The dist.shared command can only be executed after a successful read.otu command.\n");
192                 m->mothurOut("The dist.shared command parameters are groups, calc, output and label.  No parameters are required.\n");
193                 m->mothurOut("The groups parameter allows you to specify which of the groups in your groupfile you would like included used.\n");
194                 m->mothurOut("The group names are separated by dashes. The label parameter allows you to select what distance levels you would like distance matrices created for, and is also separated by dashes.\n");
195                 m->mothurOut("The dist.shared command should be in the following format: dist.shared(groups=yourGroups, calc=yourCalcs, label=yourLabels).\n");
196                 m->mothurOut("The output parameter allows you to specify format of your distance matrix. Options are lt, and square. The default is lt.\n");
197                 m->mothurOut("Example dist.shared(groups=A-B-C, calc=jabund-sorabund).\n");
198                 m->mothurOut("The default value for groups is all the groups in your groupfile.\n");
199                 m->mothurOut("The default value for calc is jclass and thetayc.\n");
200                 validCalculator->printCalc("matrix", cout);
201                 m->mothurOut("The dist.shared command outputs a .dist file for each calculator you specify at each distance you choose.\n");
202                 m->mothurOut("Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n\n");
203         }
204         catch(exception& e) {
205                 m->errorOut(e, "MatrixOutputCommand", "help");
206                 exit(1);
207         }
208 }
209
210
211 //**********************************************************************************************************************
212
213 MatrixOutputCommand::~MatrixOutputCommand(){
214         if (abort == false) {
215                 delete input; globaldata->ginput = NULL;
216                 delete read;
217                 delete validCalculator;
218         }
219 }
220
221 //**********************************************************************************************************************
222
223 int MatrixOutputCommand::execute(){
224         try {
225                 
226                 if (abort == true) {    return 0;       }
227                         
228                 //if the users entered no valid calculators don't execute command
229                 if (matrixCalculators.size() == 0) { m->mothurOut("No valid calculators."); m->mothurOutEndLine();  return 0; }
230
231                 //you have groups
232                 read = new ReadOTUFile(globaldata->inputFileName);      
233                 read->read(&*globaldata); 
234                         
235                 input = globaldata->ginput;
236                 lookup = input->getSharedRAbundVectors();
237                 string lastLabel = lookup[0]->getLabel();
238                 
239                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
240                 set<string> processedLabels;
241                 set<string> userLabels = labels;
242                                         
243                 if (lookup.size() < 2) { m->mothurOut("You have not provided enough valid groups.  I cannot run the command."); m->mothurOutEndLine(); return 0;}
244                 
245                 numGroups = lookup.size();
246                 
247                 if (m->control_pressed) { delete read; delete input; globaldata->ginput = NULL; for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } globaldata->Groups.clear(); return 0;  }
248                                 
249                 //as long as you are not at the end of the file or done wih the lines you want
250                 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
251                 
252                         if (m->control_pressed) { outputTypes.clear(); delete read; delete input; globaldata->ginput = NULL; for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } for (int i = 0; i < outputNames.size(); i++) {     remove(outputNames[i].c_str()); } globaldata->Groups.clear(); return 0;  }
253                 
254                         if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){                  
255                                 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
256                                 process(lookup);
257                                 
258                                 processedLabels.insert(lookup[0]->getLabel());
259                                 userLabels.erase(lookup[0]->getLabel());
260                         }
261                         
262                         if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
263                                 string saveLabel = lookup[0]->getLabel();
264                                 
265                                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
266                                 lookup = input->getSharedRAbundVectors(lastLabel);
267
268                                 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
269                                 process(lookup);
270                                 
271                                 processedLabels.insert(lookup[0]->getLabel());
272                                 userLabels.erase(lookup[0]->getLabel());
273                                 
274                                 //restore real lastlabel to save below
275                                 lookup[0]->setLabel(saveLabel);
276                         }
277
278                         lastLabel = lookup[0]->getLabel();                      
279                         
280                         //get next line to process
281                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
282                         lookup = input->getSharedRAbundVectors();
283                 }
284                 
285                 if (m->control_pressed) { outputTypes.clear(); delete read; delete input; globaldata->ginput = NULL; for (int i = 0; i < outputNames.size(); i++) {     remove(outputNames[i].c_str()); } globaldata->Groups.clear(); return 0;  }
286
287                 //output error messages about any remaining user labels
288                 set<string>::iterator it;
289                 bool needToRun = false;
290                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
291                         m->mothurOut("Your file does not include the label " + *it);  
292                         if (processedLabels.count(lastLabel) != 1) {
293                                 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
294                                 needToRun = true;
295                         }else {
296                                 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
297                         }
298                 }
299                 
300                 if (m->control_pressed) { outputTypes.clear(); delete read; delete input; globaldata->ginput = NULL;  for (int i = 0; i < outputNames.size(); i++) {    remove(outputNames[i].c_str()); } globaldata->Groups.clear(); return 0;  }
301
302                 //run last label if you need to
303                 if (needToRun == true)  {
304                         for (int i = 0; i < lookup.size(); i++) {  if (lookup[i] != NULL) {  delete lookup[i]; }  } 
305                         lookup = input->getSharedRAbundVectors(lastLabel);
306
307                         m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
308                         process(lookup);
309                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
310                 }
311                 
312                 if (m->control_pressed) { outputTypes.clear();  delete read; delete input; globaldata->ginput = NULL;  for (int i = 0; i < outputNames.size(); i++) {   remove(outputNames[i].c_str()); } globaldata->Groups.clear(); return 0;  }
313                 
314                 //reset groups parameter
315                 globaldata->Groups.clear();  
316                 
317                 m->mothurOutEndLine();
318                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
319                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
320                 m->mothurOutEndLine();
321
322
323                 return 0;
324         }
325         catch(exception& e) {
326                 m->errorOut(e, "MatrixOutputCommand", "execute");
327                 exit(1);
328         }
329 }
330 /***********************************************************/
331 void MatrixOutputCommand::printSims(ostream& out) {
332         try {
333                 
334                 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
335                 
336                 //output num seqs
337                 out << simMatrix.size() << endl;
338                 
339                 if (output == "lt") {
340                         for (int m = 0; m < simMatrix.size(); m++)      {
341                                 out << lookup[m]->getGroup() << '\t';
342                                 for (int n = 0; n < m; n++)     {
343                                         out << simMatrix[m][n] << '\t'; 
344                                 }
345                                 out << endl;
346                         }
347                 }else{
348                         for (int m = 0; m < simMatrix.size(); m++)      {
349                                 out << lookup[m]->getGroup() << '\t';
350                                 for (int n = 0; n < simMatrix[m].size(); n++)   {
351                                         out << simMatrix[m][n] << '\t'; 
352                                 }
353                                 out << endl;
354                         }
355                 }
356         }
357         catch(exception& e) {
358                 m->errorOut(e, "MatrixOutputCommand", "printSims");
359                 exit(1);
360         }
361 }
362 /***********************************************************/
363 int MatrixOutputCommand::process(vector<SharedRAbundVector*> thisLookup){
364         try {
365         
366                                 EstOutput data;
367                                 vector<SharedRAbundVector*> subset;
368
369                                 //for each calculator                                                                                           
370                                 for(int i = 0 ; i < matrixCalculators.size(); i++) {
371                                         
372                                         //initialize simMatrix
373                                         simMatrix.clear();
374                                         simMatrix.resize(numGroups);
375                                         for (int p = 0; p < simMatrix.size(); p++)      {
376                                                 for (int j = 0; j < simMatrix.size(); j++)      {
377                                                         simMatrix[p].push_back(0.0);
378                                                 }
379                                         }
380                                 
381                                         for (int k = 0; k < thisLookup.size(); k++) { 
382                                                 for (int l = k; l < thisLookup.size(); l++) {
383                                                         if (k != l) { //we dont need to similiarity of a groups to itself
384                                                                 //get estimated similarity between 2 groups
385                                                                 
386                                                                 if (m->control_pressed) { return 0; }
387                                                                 
388                                                                 subset.clear(); //clear out old pair of sharedrabunds
389                                                                 //add new pair of sharedrabunds
390                                                                 subset.push_back(thisLookup[k]); subset.push_back(thisLookup[l]); 
391                                                                 
392                                                                 data = matrixCalculators[i]->getValues(subset); //saves the calculator outputs
393                                                                 //save values in similarity matrix
394                                                                 simMatrix[k][l] = 1.0 - data[0];  //convert similiarity to distance
395                                                                 simMatrix[l][k] = 1.0 - data[0];  //convert similiarity to distance
396                                                         }
397                                                 }
398                                         }
399                                         
400                                         exportFileName = outputDir + m->getRootName(m->getSimpleName(globaldata->inputFileName)) + matrixCalculators[i]->getName() + "." + thisLookup[0]->getLabel() + "." + output + ".dist";
401                                         m->openOutputFile(exportFileName, out);
402                                         outputNames.push_back(exportFileName); outputTypes["phylip"].push_back(exportFileName);
403                                         
404                                         printSims(out);
405                                         out.close();
406                                         
407                                 }
408
409                                 return 0;
410                 
411         }
412         catch(exception& e) {
413                 m->errorOut(e, "MatrixOutputCommand", "process");
414                 exit(1);
415         }
416 }
417 /***********************************************************/
418
419
420