]> git.donarmstrong.com Git - mothur.git/blob - heatmapsimcommand.cpp
fixed some bugs
[mothur.git] / heatmapsimcommand.cpp
1 /*
2  *  heatmapsimcommand.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 6/8/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "heatmapsimcommand.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 HeatMapSimCommand::HeatMapSimCommand(string option){
26         try {
27                 globaldata = GlobalData::getInstance();
28                 abort = false;
29                 allLines = 1;
30                 lines.clear();
31                 labels.clear();
32                 Groups.clear();
33                 Estimators.clear();
34                 
35                 //allow user to run help
36                 if(option == "help") { validCalculator = new ValidCalculators(); help(); abort = true; }
37                 
38                 else {
39                         //valid paramters for this command
40                         string AlignArray[] =  {"groups","line","label", "calc"};
41                         vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
42                         
43                         OptionParser parser(option);
44                         map<string,string> parameters = parser.getParameters();
45                         
46                         ValidParameters validParameter;
47                         
48                         //check to make sure all parameters are valid for command
49                         for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
50                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
51                         }
52                         
53                         //make sure the user has already run the read.otu command
54                         if (globaldata->getSharedFile() == "") {
55                                  cout << "You must read a list and a group, or a shared before you can use the heatmap.sim command." << endl; abort = true; 
56                         }
57
58                         //check for optional parameter and set defaults
59                         // ...at some point should added some additional type checking...
60                         line = validParameter.validFile(parameters, "line", false);                             
61                         if (line == "not found") { line = "";  }
62                         else { 
63                                 if(line != "all") {  splitAtDash(line, lines);  allLines = 0;  }
64                                 else { allLines = 1;  }
65                         }
66                         
67                         label = validParameter.validFile(parameters, "label", false);                   
68                         if (label == "not found") { label = ""; }
69                         else { 
70                                 if(label != "all") {  splitAtDash(label, labels);  allLines = 0;  }
71                                 else { allLines = 1;  }
72                         }
73                         
74                         //make sure user did not use both the line and label parameters
75                         if ((line != "") && (label != "")) { cout << "You cannot use both the line and label parameters at the same time. " << endl; abort = true; }
76                         //if the user has not specified any line or labels use the ones from read.otu
77                         else if ((line == "") && (label == "")) {  
78                                 allLines = globaldata->allLines; 
79                                 labels = globaldata->labels; 
80                                 lines = globaldata->lines;
81                         }
82                         
83                         calc = validParameter.validFile(parameters, "calc", false);                     
84                         if (calc == "not found") { calc = "jest-thetayc";  }
85                         else { 
86                                  if (calc == "default")  {  calc = "jest-thetayc";  }
87                         }
88                         splitAtDash(calc, Estimators);
89                         
90                         groups = validParameter.validFile(parameters, "groups", false);                 
91                         if (groups == "not found") { groups = ""; }
92                         else { 
93                                 splitAtDash(groups, Groups);
94                                 globaldata->Groups = Groups;
95                         }
96                         
97                         if (abort == false) {
98                                 validCalculator = new ValidCalculators();
99                                 heatmap = new HeatMapSim();
100                         
101                                 int i;
102                                 for (i=0; i<Estimators.size(); i++) {
103                                         if (validCalculator->isValidCalculator("heat", Estimators[i]) == true) { 
104                                                 if (Estimators[i] == "jabund") {        
105                                                         heatCalculators.push_back(new JAbund());
106                                                 }else if (Estimators[i] == "sorabund") { 
107                                                         heatCalculators.push_back(new SorAbund());
108                                                 }else if (Estimators[i] == "jclass") { 
109                                                         heatCalculators.push_back(new Jclass());
110                                                 }else if (Estimators[i] == "sorclass") { 
111                                                         heatCalculators.push_back(new SorClass());
112                                                 }else if (Estimators[i] == "jest") { 
113                                                         heatCalculators.push_back(new Jest());
114                                                 }else if (Estimators[i] == "sorest") { 
115                                                         heatCalculators.push_back(new SorEst());
116                                                 }else if (Estimators[i] == "thetayc") { 
117                                                         heatCalculators.push_back(new ThetaYC());
118                                                 }else if (Estimators[i] == "thetan") { 
119                                                         heatCalculators.push_back(new ThetaN());
120                                                 }else if (Estimators[i] == "morisitahorn") { 
121                                                         heatCalculators.push_back(new MorHorn());
122                                                 }else if (Estimators[i] == "braycurtis") { 
123                                                         heatCalculators.push_back(new BrayCurtis());
124                                                 }
125                                         }
126                                 }
127                                 
128                         }
129                 }
130
131                                 
132
133         }
134         catch(exception& e) {
135                 cout << "Standard Error: " << e.what() << " has occurred in the HeatMapSimCommand class Function HeatMapSimCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
136                 exit(1);
137         }
138         catch(...) {
139                 cout << "An unknown error has occurred in the HeatMapSimCommand class function HeatMapSimCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
140                 exit(1);
141         }       
142 }
143
144 //**********************************************************************************************************************
145
146 void HeatMapSimCommand::help(){
147         try {
148                 cout << "The heatmap.sim command can only be executed after a successful read.otu command." << "\n";
149                 cout << "The heatmap.sim command parameters are groups, calc, line and label.  No parameters are required, but you may not use line and label at the same time." << "\n";
150                 cout << "The groups parameter allows you to specify which of the groups in your groupfile you would like included in your heatmap." << "\n";
151                 cout << "The group names are separated by dashes. The line and label allow you to select what distance levels you would like a heatmap created for, and are also separated by dashes." << "\n";
152                 cout << "The heatmap.sim command should be in the following format: heatmap.sim(groups=yourGroups, calc=yourCalc, line=yourLines, label=yourLabels)." << "\n";
153                 cout << "Example heatmap.sim(groups=A-B-C, line=1-3-5, calc=jabund)." << "\n";
154                 cout << "The default value for groups is all the groups in your groupfile, and all lines in your inputfile will be used." << "\n";
155                 validCalculator->printCalc("heat", cout);
156                 cout << "The default value for calc is jclass-thetayc." << "\n";
157                 cout << "The heatmap.sim command outputs a .svg file for each calculator you choose at each line or label you specify." << "\n";
158                 cout << "Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups)." << "\n" << "\n";
159
160         }
161         catch(exception& e) {
162                 cout << "Standard Error: " << e.what() << " has occurred in the HeatMapSimCommand class Function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
163                 exit(1);
164         }
165         catch(...) {
166                 cout << "An unknown error has occurred in the HeatMapSimCommand class function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
167                 exit(1);
168         }       
169 }
170
171 //**********************************************************************************************************************
172
173 HeatMapSimCommand::~HeatMapSimCommand(){
174         if (abort == false) {
175                 delete input;  globaldata->ginput = NULL;
176                 delete read;
177                 delete heatmap;
178                 delete validCalculator;
179         }
180 }
181
182 //**********************************************************************************************************************
183
184 int HeatMapSimCommand::execute(){
185         try {
186         
187                 if (abort == true)  { return 0; }
188                 
189                 int count = 1;  
190                 
191                 //if the users entered no valid calculators don't execute command
192                 if (heatCalculators.size() == 0) { cout << "No valid calculators." << endl; return 0; }
193                 
194                 //you have groups
195                 read = new ReadOTUFile(globaldata->inputFileName);      
196                 read->read(&*globaldata); 
197                         
198                 input = globaldata->ginput;
199                 lookup = input->getSharedRAbundVectors();
200                 vector<SharedRAbundVector*> lastLookup = lookup;
201                 
202                 if (lookup.size() < 2) { cout << "You have not provided enough valid groups.  I cannot run the command." << endl; return 0;}
203                                 
204                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
205                 set<string> processedLabels;
206                 set<string> userLabels = labels;
207                 set<int> userLines = lines;
208
209                 
210                 //as long as you are not at the end of the file or done wih the lines you want
211                 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
212                 
213                         if(allLines == 1 || lines.count(count) == 1 || labels.count(lookup[0]->getLabel()) == 1){                       
214         
215                                 cout << lookup[0]->getLabel() << '\t' << count << endl;
216                                 heatmap->getPic(lookup, heatCalculators);
217                                         
218                                 processedLabels.insert(lookup[0]->getLabel());
219                                 userLabels.erase(lookup[0]->getLabel());
220                                 userLines.erase(count);
221                         }
222                                 
223                         if ((anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLookup[0]->getLabel()) != 1)) {
224                                 cout << lastLookup[0]->getLabel() << '\t' << count << endl;
225                                 heatmap->getPic(lastLookup, heatCalculators);
226                                         
227                                 processedLabels.insert(lastLookup[0]->getLabel());
228                                 userLabels.erase(lastLookup[0]->getLabel());
229                         }
230                                 
231                         //prevent memory leak
232                         if (count != 1) { for (int i = 0; i < lastLookup.size(); i++) {  delete lastLookup[i];  } }
233                         lastLookup = lookup;                    
234
235                         //get next line to process
236                         lookup = input->getSharedRAbundVectors();                               
237                         count++;
238                 }
239                         
240                 //output error messages about any remaining user labels
241                 set<string>::iterator it;
242                 bool needToRun = false;
243                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
244                         cout << "Your file does not include the label "<< *it; 
245                         if (processedLabels.count(lastLookup[0]->getLabel()) != 1) {
246                                 cout << ". I will use " << lastLookup[0]->getLabel() << "." << endl;
247                                 needToRun = true;
248                         }else {
249                                 cout << ". Please refer to " << lastLookup[0]->getLabel() << "." << endl;
250                         }
251                 }
252                 
253                 //run last line if you need to
254                 if (needToRun == true)  {
255                         cout << lastLookup[0]->getLabel() << '\t' << count << endl;
256                         heatmap->getPic(lastLookup, heatCalculators);
257                 }
258                 
259                 for (int i = 0; i < lastLookup.size(); i++) {  delete lastLookup[i];  }
260                         
261                 //reset groups parameter
262                 globaldata->Groups.clear();  
263                 
264                 return 0;
265         }
266         catch(exception& e) {
267                 cout << "Standard Error: " << e.what() << " has occurred in the HeatMapSimCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
268                 exit(1);
269         }
270         catch(...) {
271                 cout << "An unknown error has occurred in the HeatMapSimCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
272                 exit(1);
273         }               
274 }
275
276 //**********************************************************************************************************************