]> git.donarmstrong.com Git - mothur.git/blob - matrixoutputcommand.cpp
removed read.dist, read.otu, read.tree and globaldata. added current to defaults...
[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 "sharedsobscollectsummary.h"
12 #include "sharedchao1.h"
13 #include "sharedace.h"
14 #include "sharednseqs.h"
15 #include "sharedjabund.h"
16 #include "sharedsorabund.h"
17 #include "sharedjclass.h"
18 #include "sharedsorclass.h"
19 #include "sharedjest.h"
20 #include "sharedsorest.h"
21 #include "sharedthetayc.h"
22 #include "sharedthetan.h"
23 #include "sharedkstest.h"
24 #include "whittaker.h"
25 #include "sharedochiai.h"
26 #include "sharedanderbergs.h"
27 #include "sharedkulczynski.h"
28 #include "sharedkulczynskicody.h"
29 #include "sharedlennon.h"
30 #include "sharedmorisitahorn.h"
31 #include "sharedbraycurtis.h"
32 #include "sharedjackknife.h"
33 #include "whittaker.h"
34 #include "odum.h"
35 #include "canberra.h"
36 #include "structeuclidean.h"
37 #include "structchord.h"
38 #include "hellinger.h"
39 #include "manhattan.h"
40 #include "structpearson.h"
41 #include "soergel.h"
42 #include "spearman.h"
43 #include "structkulczynski.h"
44 #include "structchi2.h"
45 #include "speciesprofile.h"
46 #include "hamming.h"
47 #include "gower.h"
48 #include "memchi2.h"
49 #include "memchord.h"
50 #include "memeuclidean.h"
51 #include "mempearson.h"
52
53 //**********************************************************************************************************************
54 vector<string> MatrixOutputCommand::setParameters(){    
55         try {
56                 CommandParameter pshared("shared", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pshared);
57                 CommandParameter plabel("label", "String", "", "", "", "", "",false,false); parameters.push_back(plabel);
58                 CommandParameter pgroups("groups", "String", "", "", "", "", "",false,false); parameters.push_back(pgroups);
59                 CommandParameter pcalc("calc", "Multiple", "sharedsobs-sharedchao-sharedace-jabund-sorabund-jclass-sorclass-jest-sorest-thetayc-thetan-kstest-sharednseqs-ochiai-anderberg-kulczynski-kulczynskicody-lennon-morisitahorn-braycurtis-whittaker-odum-canberra-structeuclidean-structchord-hellinger-manhattan-structpearson-soergel-spearman-structkulczynski-speciesprofile-hamming-structchi2-gower-memchi2-memchord-memeuclidean-mempearson", "jclass-thetayc", "", "", "",true,false); parameters.push_back(pcalc);
60                 CommandParameter poutput("output", "Multiple", "lt-square", "lt", "", "", "",false,false); parameters.push_back(poutput);
61                 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
62                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
63                 
64                 vector<string> myArray;
65                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
66                 return myArray;
67         }
68         catch(exception& e) {
69                 m->errorOut(e, "MatrixOutputCommand", "setParameters");
70                 exit(1);
71         }
72 }
73 //**********************************************************************************************************************
74 string MatrixOutputCommand::getHelpString(){    
75         try {
76                 string helpString = "";
77                 helpString += "The dist.shared command parameters are shared, groups, calc, output and label.  shared is a required, unless you have a valid current file.\n";
78                 helpString += "The groups parameter allows you to specify which of the groups in your groupfile you would like included used.\n";
79                 helpString += "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";
80                 helpString += "The dist.shared command should be in the following format: dist.shared(groups=yourGroups, calc=yourCalcs, label=yourLabels).\n";
81                 helpString += "The output parameter allows you to specify format of your distance matrix. Options are lt, and square. The default is lt.\n";
82                 helpString += "Example dist.shared(groups=A-B-C, calc=jabund-sorabund).\n";
83                 helpString += "The default value for groups is all the groups in your groupfile.\n";
84                 helpString += "The default value for calc is jclass and thetayc.\n";
85                 helpString += validCalculator->printCalc("matrix");
86                 helpString += "The dist.shared command outputs a .dist file for each calculator you specify at each distance you choose.\n";
87                 helpString += "Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n\n";
88                 return helpString;
89         }
90         catch(exception& e) {
91                 m->errorOut(e, "MatrixOutputCommand", "getHelpString");
92                 exit(1);
93         }
94 }
95 //**********************************************************************************************************************
96 MatrixOutputCommand::MatrixOutputCommand(){     
97         try {
98                 abort = true; calledHelp = true; 
99                 setParameters();
100                 vector<string> tempOutNames;
101                 outputTypes["phylip"] = tempOutNames;
102         }
103         catch(exception& e) {
104                 m->errorOut(e, "MatrixOutputCommand", "MatrixOutputCommand");
105                 exit(1);
106         }
107 }
108 //**********************************************************************************************************************
109
110 MatrixOutputCommand::MatrixOutputCommand(string option)  {
111         try {
112                 abort = false; calledHelp = false;   
113                 allLines = 1;
114                                 
115                 //allow user to run help
116                 if(option == "help") { validCalculator = new ValidCalculators(); help(); abort = true; calledHelp = true; }
117                 
118                 else {
119                         vector<string> myArray = setParameters();
120                         
121                         OptionParser parser(option);
122                         map<string,string> parameters  = parser.getParameters();
123                         map<string,string>::iterator it;
124                         
125                         ValidParameters validParameter;
126                 
127                         //check to make sure all parameters are valid for command
128                         for (it = parameters.begin(); it != parameters.end(); it++) { 
129                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
130                         }
131                         
132                         //initialize outputTypes
133                         vector<string> tempOutNames;
134                         outputTypes["phylip"] = tempOutNames;
135                         
136                         //if the user changes the input directory command factory will send this info to us in the output parameter 
137                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
138                         if (inputDir == "not found"){   inputDir = "";          }
139                         else {
140                                 string path;
141                                 it = parameters.find("shared");
142                                 //user has given a template file
143                                 if(it != parameters.end()){ 
144                                         path = m->hasPath(it->second);
145                                         //if the user has not given a path then, add inputdir. else leave path alone.
146                                         if (path == "") {       parameters["shared"] = inputDir + it->second;           }
147                                 }
148                         }
149                         
150                         sharedfile = validParameter.validFile(parameters, "shared", true);
151                         if (sharedfile == "not found") {                        
152                                 //if there is a current shared file, use it
153                                 sharedfile = m->getSharedFile(); 
154                                 if (sharedfile != "") { m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
155                                 else {  m->mothurOut("You have no current sharedfile and the shared parameter is required."); m->mothurOutEndLine(); abort = true; }
156                         }else if (sharedfile == "not open") { sharedfile = ""; abort = true; }
157                         
158                         //if the user changes the output directory command factory will send this info to us in the output parameter 
159                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
160                                 outputDir = ""; 
161                                 outputDir += m->hasPath(sharedfile); //if user entered a file with a path then preserve it      
162                         }
163                         
164                         //check for optional parameter and set defaults
165                         // ...at some point should added some additional type checking...
166                         label = validParameter.validFile(parameters, "label", false);                   
167                         if (label == "not found") { label = ""; }
168                         else { 
169                                 if(label != "all") {  m->splitAtDash(label, labels);  allLines = 0;  }
170                                 else { allLines = 1;  }
171                         }
172                         
173                         output = validParameter.validFile(parameters, "output", false);         if(output == "not found"){      output = "lt"; }
174                         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"; }
175                         
176                         groups = validParameter.validFile(parameters, "groups", false);                 
177                         if (groups == "not found") { groups = ""; }
178                         else { 
179                                 m->splitAtDash(groups, Groups);
180                                 m->Groups = Groups;
181                         }
182                                 
183                         calc = validParameter.validFile(parameters, "calc", false);                     
184                         if (calc == "not found") { calc = "jclass-thetayc";  }
185                         else { 
186                                  if (calc == "default")  {  calc = "jclass-thetayc";  }
187                         }
188                         m->splitAtDash(calc, Estimators);
189
190                         if (abort == false) {
191                         
192                                 validCalculator = new ValidCalculators();
193                                 
194                                 int i;
195                                 for (i=0; i<Estimators.size(); i++) {
196                                         if (validCalculator->isValidCalculator("matrix", Estimators[i]) == true) { 
197                                                 if (Estimators[i] == "sharedsobs") { 
198                                                         matrixCalculators.push_back(new SharedSobsCS());
199                                                 }else if (Estimators[i] == "sharedchao") { 
200                                                         matrixCalculators.push_back(new SharedChao1());
201                                                 }else if (Estimators[i] == "sharedace") { 
202                                                         matrixCalculators.push_back(new SharedAce());
203                                                 }else if (Estimators[i] == "jabund") {  
204                                                         matrixCalculators.push_back(new JAbund());
205                                                 }else if (Estimators[i] == "sorabund") { 
206                                                         matrixCalculators.push_back(new SorAbund());
207                                                 }else if (Estimators[i] == "jclass") { 
208                                                         matrixCalculators.push_back(new Jclass());
209                                                 }else if (Estimators[i] == "sorclass") { 
210                                                         matrixCalculators.push_back(new SorClass());
211                                                 }else if (Estimators[i] == "jest") { 
212                                                         matrixCalculators.push_back(new Jest());
213                                                 }else if (Estimators[i] == "sorest") { 
214                                                         matrixCalculators.push_back(new SorEst());
215                                                 }else if (Estimators[i] == "thetayc") { 
216                                                         matrixCalculators.push_back(new ThetaYC());
217                                                 }else if (Estimators[i] == "thetan") { 
218                                                         matrixCalculators.push_back(new ThetaN());
219                                                 }else if (Estimators[i] == "kstest") { 
220                                                         matrixCalculators.push_back(new KSTest());
221                                                 }else if (Estimators[i] == "sharednseqs") { 
222                                                         matrixCalculators.push_back(new SharedNSeqs());
223                                                 }else if (Estimators[i] == "ochiai") { 
224                                                         matrixCalculators.push_back(new Ochiai());
225                                                 }else if (Estimators[i] == "anderberg") { 
226                                                         matrixCalculators.push_back(new Anderberg());
227                                                 }else if (Estimators[i] == "kulczynski") { 
228                                                         matrixCalculators.push_back(new Kulczynski());
229                                                 }else if (Estimators[i] == "kulczynskicody") { 
230                                                         matrixCalculators.push_back(new KulczynskiCody());
231                                                 }else if (Estimators[i] == "lennon") { 
232                                                         matrixCalculators.push_back(new Lennon());
233                                                 }else if (Estimators[i] == "morisitahorn") { 
234                                                         matrixCalculators.push_back(new MorHorn());
235                                                 }else if (Estimators[i] == "braycurtis") { 
236                                                         matrixCalculators.push_back(new BrayCurtis());
237                                                 }else if (Estimators[i] == "whittaker") { 
238                                                         matrixCalculators.push_back(new Whittaker());
239                                                 }else if (Estimators[i] == "odum") { 
240                                                         matrixCalculators.push_back(new Odum());
241                                                 }else if (Estimators[i] == "canberra") { 
242                                                         matrixCalculators.push_back(new Canberra());
243                                                 }else if (Estimators[i] == "structeuclidean") { 
244                                                         matrixCalculators.push_back(new StructEuclidean());
245                                                 }else if (Estimators[i] == "structchord") { 
246                                                         matrixCalculators.push_back(new StructChord());
247                                                 }else if (Estimators[i] == "hellinger") { 
248                                                         matrixCalculators.push_back(new Hellinger());
249                                                 }else if (Estimators[i] == "manhattan") { 
250                                                         matrixCalculators.push_back(new Manhattan());
251                                                 }else if (Estimators[i] == "structpearson") { 
252                                                         matrixCalculators.push_back(new StructPearson());
253                                                 }else if (Estimators[i] == "soergel") { 
254                                                         matrixCalculators.push_back(new Soergel());
255                                                 }else if (Estimators[i] == "spearman") { 
256                                                         matrixCalculators.push_back(new Spearman());
257                                                 }else if (Estimators[i] == "structkulczynski") { 
258                                                         matrixCalculators.push_back(new StructKulczynski());
259                                                 }else if (Estimators[i] == "speciesprofile") { 
260                                                         matrixCalculators.push_back(new SpeciesProfile());
261                                                 }else if (Estimators[i] == "hamming") { 
262                                                         matrixCalculators.push_back(new Hamming());
263                                                 }else if (Estimators[i] == "structchi2") { 
264                                                         matrixCalculators.push_back(new StructChi2());
265                                                 }else if (Estimators[i] == "gower") { 
266                                                         matrixCalculators.push_back(new Gower());
267                                                 }else if (Estimators[i] == "memchi2") { 
268                                                         matrixCalculators.push_back(new MemChi2());
269                                                 }else if (Estimators[i] == "memchord") { 
270                                                         matrixCalculators.push_back(new MemChord());
271                                                 }else if (Estimators[i] == "memeuclidean") { 
272                                                         matrixCalculators.push_back(new MemEuclidean());
273                                                 }else if (Estimators[i] == "mempearson") { 
274                                                         matrixCalculators.push_back(new MemPearson());
275                                                 }
276                                         }
277                                 }
278                                 
279                         }
280                 }
281                 
282         }
283         catch(exception& e) {
284                 m->errorOut(e, "MatrixOutputCommand", "MatrixOutputCommand");
285                 exit(1);
286         }
287 }
288
289 //**********************************************************************************************************************
290
291 MatrixOutputCommand::~MatrixOutputCommand(){
292         if (abort == false) {
293                 delete validCalculator;
294         }
295 }
296
297 //**********************************************************************************************************************
298
299 int MatrixOutputCommand::execute(){
300         try {
301                 
302                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
303                         
304                 //if the users entered no valid calculators don't execute command
305                 if (matrixCalculators.size() == 0) { m->mothurOut("No valid calculators."); m->mothurOutEndLine();  return 0; }
306                         
307                 input = new InputData(sharedfile, "sharedfile");
308                 lookup = input->getSharedRAbundVectors();
309                 string lastLabel = lookup[0]->getLabel();
310                 
311                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
312                 set<string> processedLabels;
313                 set<string> userLabels = labels;
314                                         
315                 if (lookup.size() < 2) { m->mothurOut("You have not provided enough valid groups.  I cannot run the command."); m->mothurOutEndLine(); delete input; for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } return 0;}
316                 
317                 numGroups = lookup.size();
318                 
319                 if (m->control_pressed) { delete input; for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } m->Groups.clear(); return 0;  }
320                                 
321                 //as long as you are not at the end of the file or done wih the lines you want
322                 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
323                 
324                         if (m->control_pressed) { outputTypes.clear(); delete input; for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } for (int i = 0; i < outputNames.size(); i++) {     remove(outputNames[i].c_str()); } m->Groups.clear(); return 0;  }
325                 
326                         if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){                  
327                                 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
328                                 process(lookup);
329                                 
330                                 processedLabels.insert(lookup[0]->getLabel());
331                                 userLabels.erase(lookup[0]->getLabel());
332                         }
333                         
334                         if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
335                                 string saveLabel = lookup[0]->getLabel();
336                                 
337                                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
338                                 lookup = input->getSharedRAbundVectors(lastLabel);
339
340                                 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
341                                 process(lookup);
342                                 
343                                 processedLabels.insert(lookup[0]->getLabel());
344                                 userLabels.erase(lookup[0]->getLabel());
345                                 
346                                 //restore real lastlabel to save below
347                                 lookup[0]->setLabel(saveLabel);
348                         }
349
350                         lastLabel = lookup[0]->getLabel();                      
351                         
352                         //get next line to process
353                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
354                         lookup = input->getSharedRAbundVectors();
355                 }
356                 
357                 if (m->control_pressed) { outputTypes.clear(); delete input; for (int i = 0; i < outputNames.size(); i++) {     remove(outputNames[i].c_str()); } m->Groups.clear(); return 0;  }
358
359                 //output error messages about any remaining user labels
360                 set<string>::iterator it;
361                 bool needToRun = false;
362                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
363                         m->mothurOut("Your file does not include the label " + *it);  
364                         if (processedLabels.count(lastLabel) != 1) {
365                                 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
366                                 needToRun = true;
367                         }else {
368                                 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
369                         }
370                 }
371                 
372                 if (m->control_pressed) { outputTypes.clear(); delete input;  for (int i = 0; i < outputNames.size(); i++) {    remove(outputNames[i].c_str()); } m->Groups.clear(); return 0;  }
373
374                 //run last label if you need to
375                 if (needToRun == true)  {
376                         for (int i = 0; i < lookup.size(); i++) {  if (lookup[i] != NULL) {  delete lookup[i]; }  } 
377                         lookup = input->getSharedRAbundVectors(lastLabel);
378
379                         m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
380                         process(lookup);
381                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
382                 }
383                 
384                 if (m->control_pressed) { outputTypes.clear();  delete input;  for (int i = 0; i < outputNames.size(); i++) {   remove(outputNames[i].c_str()); } m->Groups.clear(); return 0;  }
385                 
386                 //reset groups parameter
387                 m->Groups.clear();  
388                 
389                 //set phylip file as new current phylipfile
390                 string current = "";
391                 itTypes = outputTypes.find("phylip");
392                 if (itTypes != outputTypes.end()) {
393                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setPhylipFile(current); }
394                 }
395                 
396                 m->mothurOutEndLine();
397                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
398                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
399                 m->mothurOutEndLine();
400
401
402                 return 0;
403         }
404         catch(exception& e) {
405                 m->errorOut(e, "MatrixOutputCommand", "execute");
406                 exit(1);
407         }
408 }
409 /***********************************************************/
410 void MatrixOutputCommand::printSims(ostream& out) {
411         try {
412                 
413                 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
414                 
415                 //output num seqs
416                 out << simMatrix.size() << endl;
417                 
418                 if (output == "lt") {
419                         for (int m = 0; m < simMatrix.size(); m++)      {
420                                 out << lookup[m]->getGroup() << '\t';
421                                 for (int n = 0; n < m; n++)     {
422                                         out << simMatrix[m][n] << '\t'; 
423                                 }
424                                 out << endl;
425                         }
426                 }else{
427                         for (int m = 0; m < simMatrix.size(); m++)      {
428                                 out << lookup[m]->getGroup() << '\t';
429                                 for (int n = 0; n < simMatrix[m].size(); n++)   {
430                                         out << simMatrix[m][n] << '\t'; 
431                                 }
432                                 out << endl;
433                         }
434                 }
435         }
436         catch(exception& e) {
437                 m->errorOut(e, "MatrixOutputCommand", "printSims");
438                 exit(1);
439         }
440 }
441 /***********************************************************/
442 int MatrixOutputCommand::process(vector<SharedRAbundVector*> thisLookup){
443         try {
444         
445                                 EstOutput data;
446                                 vector<SharedRAbundVector*> subset;
447
448                                 //for each calculator                                                                                           
449                                 for(int i = 0 ; i < matrixCalculators.size(); i++) {
450                                         
451                                         //initialize simMatrix
452                                         simMatrix.clear();
453                                         simMatrix.resize(numGroups);
454                                         for (int p = 0; p < simMatrix.size(); p++)      {
455                                                 for (int j = 0; j < simMatrix.size(); j++)      {
456                                                         simMatrix[p].push_back(0.0);
457                                                 }
458                                         }
459                                 
460                                         for (int k = 0; k < thisLookup.size(); k++) { 
461                                                 for (int l = k; l < thisLookup.size(); l++) {
462                                                         if (k != l) { //we dont need to similiarity of a groups to itself
463                                                                 //get estimated similarity between 2 groups
464                                                                 
465                                                                 if (m->control_pressed) { return 0; }
466                                                                 
467                                                                 subset.clear(); //clear out old pair of sharedrabunds
468                                                                 //add new pair of sharedrabunds
469                                                                 subset.push_back(thisLookup[k]); subset.push_back(thisLookup[l]); 
470                                                                 
471                                                                 //if this calc needs all groups to calculate the pair load all groups
472                                                                 if (matrixCalculators[i]->getNeedsAll()) { 
473                                                                         //load subset with rest of lookup for those calcs that need everyone to calc for a pair
474                                                                         for (int w = 0; w < thisLookup.size(); w++) {
475                                                                                 if ((w != k) && (w != l)) { subset.push_back(thisLookup[w]); }
476                                                                         }
477                                                                 }
478                                                                 
479                                                                 data = matrixCalculators[i]->getValues(subset); //saves the calculator outputs
480                                                                 //save values in similarity matrix
481                                                                 simMatrix[k][l] = 1.0 - data[0];  //convert similiarity to distance
482                                                                 simMatrix[l][k] = 1.0 - data[0];  //convert similiarity to distance
483                                                         }
484                                                 }
485                                         }
486                                         
487                                         exportFileName = outputDir + m->getRootName(m->getSimpleName(sharedfile)) + matrixCalculators[i]->getName() + "." + thisLookup[0]->getLabel() + "." + output + ".dist";
488                                         m->openOutputFile(exportFileName, out);
489                                         outputNames.push_back(exportFileName); outputTypes["phylip"].push_back(exportFileName);
490                                         
491                                         printSims(out);
492                                         out.close();
493                                         
494                                 }
495
496                                 return 0;
497                 
498         }
499         catch(exception& e) {
500                 m->errorOut(e, "MatrixOutputCommand", "process");
501                 exit(1);
502         }
503 }
504 /***********************************************************/
505
506
507