]> git.donarmstrong.com Git - mothur.git/blob - heatmapcommand.cpp
some changes while testing 1.9
[mothur.git] / heatmapcommand.cpp
1 /*
2  *  heatmapcommand.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 3/25/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "heatmapcommand.h"
11
12
13 //**********************************************************************************************************************
14
15 HeatMapCommand::HeatMapCommand(string option) {
16         try {
17                 globaldata = GlobalData::getInstance();
18                 abort = false;
19                 allLines = 1;
20                 labels.clear();
21                 
22                 //allow user to run help
23                 if(option == "help") { help(); abort = true; }
24                 
25                 else {
26                         //valid paramters for this command
27                         string AlignArray[] =  {"groups","label","sorted","scale","outputdir","inputdir"};
28                         vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
29                         
30                         OptionParser parser(option);
31                         map<string,string> parameters = parser.getParameters();
32                         
33                         ValidParameters validParameter;
34                         
35                         //check to make sure all parameters are valid for command
36                         for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
37                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
38                         }
39                         
40                         //if the user changes the output directory command factory will send this info to us in the output parameter 
41                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
42                                 outputDir = ""; 
43                                 outputDir += hasPath(globaldata->inputFileName); //if user entered a file with a path then preserve it  
44                         }
45                         
46                         //make sure the user has already run the read.otu command
47                         if ((globaldata->getListFile() == "") && (globaldata->getRabundFile() == "") && (globaldata->getSabundFile() == "") && (globaldata->getSharedFile() == "")) {
48                                  m->mothurOut("You must read a list, rabund, sabund, or a list and a group, or a shared before you can use the heatmap.bin command."); m->mothurOutEndLine(); abort = true; 
49                         }
50
51                         //check for optional parameter and set defaults
52                         // ...at some point should added some additional type checking...
53                         label = validParameter.validFile(parameters, "label", false);                   
54                         if (label == "not found") { label = ""; }
55                         else { 
56                                 if(label != "all") {  splitAtDash(label, labels);  allLines = 0;  }
57                                 else { allLines = 1;  }
58                         }
59                         
60                         //if the user has not specified any labels use the ones from read.otu
61                         if (label == "") {  
62                                 allLines = globaldata->allLines; 
63                                 labels = globaldata->labels; 
64                         }
65                         
66                         groups = validParameter.validFile(parameters, "groups", false);                 
67                         if (groups == "not found") { groups = ""; }
68                         else { 
69                                 splitAtDash(groups, Groups);
70                                 globaldata->Groups = Groups;
71                         }
72                         
73                         sorted = validParameter.validFile(parameters, "sorted", false);                 if (sorted == "not found") { sorted = "T"; }
74                  
75                         scale = validParameter.validFile(parameters, "scale", false);                           if (scale == "not found") { scale = "log10"; }
76                         
77                 }
78
79         }
80         catch(exception& e) {
81                 m->errorOut(e, "HeatMapCommand", "HeatMapCommand");
82                 exit(1);
83         }
84 }
85
86 //**********************************************************************************************************************
87
88 void HeatMapCommand::help(){
89         try {
90                 m->mothurOut("The heatmap.bin command can only be executed after a successful read.otu command.\n");
91                 m->mothurOut("The heatmap.bin command parameters are groups, sorted, scale label.  No parameters are required.\n");
92                 m->mothurOut("The groups parameter allows you to specify which of the groups in your groupfile you would like included in your heatmap.\n");
93                 m->mothurOut("The sorted parameter allows you to choose to see the file with the shared otus at the top or the otus in the order they appear in your input file. \n");
94                 m->mothurOut("The scale parameter allows you to choose the range of color your bin information will be displayed with.\n");
95                 m->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 are also separated by dashes.\n");
96                 m->mothurOut("The heatmap.bin command should be in the following format: heatmap.bin(groups=yourGroups, sorted=yourSorted, label=yourLabels).\n");
97                 m->mothurOut("Example heatmap.bin(groups=A-B-C, sorted=F, scale=log10).\n");
98                 m->mothurOut("The default value for groups is all the groups in your groupfile, and all labels in your inputfile will be used.\n");
99                 m->mothurOut("The default value for sorted is T meaning you want the shared otus on top, you may change it to F meaning the exact representation of your input file.\n");
100                 m->mothurOut("The default value for scale is log10; your other options are log2 and linear.\n");
101                 m->mothurOut("The heatmap.bin command outputs a .svg file for each label you specify.\n");
102                 m->mothurOut("Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n\n");
103
104         }
105         catch(exception& e) {
106                 m->errorOut(e, "HeatMapCommand", "help");
107                 exit(1);
108         }
109 }
110
111 //**********************************************************************************************************************
112
113 HeatMapCommand::~HeatMapCommand(){
114 }
115
116 //**********************************************************************************************************************
117
118 int HeatMapCommand::execute(){
119         try {
120         
121                 if (abort == true) { return 0; }
122                 
123                 heatmap = new HeatMap(sorted, scale, outputDir);
124                 format = globaldata->getFormat();
125
126                 string lastLabel;
127                 
128                 read = new ReadOTUFile(globaldata->inputFileName);      
129                 read->read(&*globaldata); 
130                 input = globaldata->ginput;
131                 
132                 if (format == "sharedfile") {
133                         //you have groups
134                         lookup = input->getSharedRAbundVectors();
135                         lastLabel = lookup[0]->getLabel();
136         
137                 }else if ((format == "list") || (format == "rabund") || (format == "sabund")) {
138                         //you are using just a list file and have only one group
139                         rabund = globaldata->rabund;
140                         lastLabel = rabund->getLabel();
141                 }
142                 
143                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
144                 set<string> processedLabels;
145                 set<string> userLabels = labels;
146
147                 if ((format != "list") && (format != "rabund") && (format != "sabund")) {       
148                 
149                         //as long as you are not at the end of the file or done wih the lines you want
150                         while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
151                                 if (m->control_pressed) {
152                                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
153                                         for (int i = 0; i < outputNames.size(); i++) {  if (outputNames[i] != "control") {  remove(outputNames[i].c_str());  } }
154                                         globaldata->Groups.clear(); 
155                                         delete read; delete heatmap; return 0;
156                                 }
157                 
158                                 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){                  
159         
160                                         m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
161                                         outputNames.push_back(heatmap->getPic(lookup));
162                                         
163                                         processedLabels.insert(lookup[0]->getLabel());
164                                         userLabels.erase(lookup[0]->getLabel());
165                                 }
166                                 
167                                 if ((anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
168                                         string saveLabel = lookup[0]->getLabel();
169                                 
170                                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }  
171                                         lookup = input->getSharedRAbundVectors(lastLabel);
172                                         m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
173                                         
174                                         outputNames.push_back(heatmap->getPic(lookup));
175                                         
176                                         processedLabels.insert(lookup[0]->getLabel());
177                                         userLabels.erase(lookup[0]->getLabel());
178                                         
179                                         //restore real lastlabel to save below
180                                         lookup[0]->setLabel(saveLabel);
181                                 }
182                                 
183                                 lastLabel = lookup[0]->getLabel();
184                                 //prevent memory leak
185                                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i]; lookup[i] = NULL; }
186                                                         
187                                 //get next line to process
188                                 lookup = input->getSharedRAbundVectors();                               
189                         }
190                         
191                         
192                         if (m->control_pressed) {
193                                 for (int i = 0; i < outputNames.size(); i++) {  if (outputNames[i] != "control") {  remove(outputNames[i].c_str());  } }
194                                 globaldata->Groups.clear(); 
195                                 delete read; delete heatmap; return 0;
196                         }
197
198                         //output error messages about any remaining user labels
199                         set<string>::iterator it;
200                         bool needToRun = false;
201                         for (it = userLabels.begin(); it != userLabels.end(); it++) {  
202                                 m->mothurOut("Your file does not include the label " + *it); 
203                                 if (processedLabels.count(lastLabel) != 1) {
204                                         m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
205                                         needToRun = true;
206                                 }else {
207                                         m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
208                                 }
209                         }
210                 
211                         //run last label if you need to
212                         if (needToRun == true)  {
213                                 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }  
214                                 lookup = input->getSharedRAbundVectors(lastLabel);
215                                 
216                                 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
217                                 outputNames.push_back(heatmap->getPic(lookup));
218                                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
219                         }
220                 
221                         //reset groups parameter
222                         globaldata->Groups.clear();  
223                         
224                 }else{
225         
226                         while((rabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
227                                 if (m->control_pressed) {   
228                                         for (int i = 0; i < outputNames.size(); i++) {  if (outputNames[i] != "control") {  remove(outputNames[i].c_str());  } }
229                                         delete rabund;  delete read; delete heatmap; return 0;  
230                                 }
231
232                                 if(allLines == 1 || labels.count(rabund->getLabel()) == 1){                     
233         
234                                         m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
235                                         outputNames.push_back(heatmap->getPic(rabund));
236                                         
237                                         processedLabels.insert(rabund->getLabel());
238                                         userLabels.erase(rabund->getLabel());
239                                 }
240                                 
241                                 if ((anyLabelsToProcess(rabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
242                                         string saveLabel = rabund->getLabel();
243                                         
244                                         delete rabund;
245                                         rabund = input->getRAbundVector(lastLabel);
246                                         m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
247                                         
248                                         outputNames.push_back(heatmap->getPic(rabund));
249                                         
250                                         processedLabels.insert(rabund->getLabel());
251                                         userLabels.erase(rabund->getLabel());
252                                         
253                                         //restore real lastlabel to save below
254                                         rabund->setLabel(saveLabel);
255                                 }               
256                                 
257                                                                 
258                                                                 
259                                 lastLabel = rabund->getLabel();                 
260                                 delete rabund;
261                                 rabund = input->getRAbundVector();
262                         }
263                         
264                         if (m->control_pressed) {
265                                 for (int i = 0; i < outputNames.size(); i++) {  if (outputNames[i] != "control") {  remove(outputNames[i].c_str());  } }
266                                 delete read; delete heatmap; return 0;
267                         }
268
269                         //output error messages about any remaining user labels
270                         set<string>::iterator it;
271                         bool needToRun = false;
272                         for (it = userLabels.begin(); it != userLabels.end(); it++) {  
273                                 m->mothurOut("Your file does not include the label " + *it); 
274                                 if (processedLabels.count(lastLabel) != 1) {
275                                         m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
276                                         needToRun = true;
277                                 }else {
278                                         m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
279                                 }
280                         }
281                 
282                         //run last label if you need to
283                         if (needToRun == true)  {
284                 
285                                 if (rabund != NULL) {   delete rabund;  }
286                                 rabund = input->getRAbundVector(lastLabel);
287                                 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
288                                         
289                                 outputNames.push_back(heatmap->getPic(rabund));
290                                 delete rabund; globaldata->rabund = NULL;
291                         }
292                 
293                 }
294                 
295                 globaldata->rabund = NULL;
296                 delete input; globaldata->ginput = NULL;
297                 
298                 if (m->control_pressed) {
299                         for (int i = 0; i < outputNames.size(); i++) {  if (outputNames[i] != "control") {  remove(outputNames[i].c_str());  } }
300                         delete read; delete heatmap; return 0;
301                 }
302                 
303                 m->mothurOutEndLine();
304                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
305                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
306                 m->mothurOutEndLine();
307                 
308                 delete read;
309                 delete heatmap;
310
311                 return 0;
312         }
313         catch(exception& e) {
314                 m->errorOut(e, "HeatMapCommand", "execute");
315                 exit(1);
316         }
317 }
318
319 //**********************************************************************************************************************
320
321