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