]> git.donarmstrong.com Git - mothur.git/blob - heatmapcommand.cpp
broke up globaldata and moved error checking and help into commands
[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                 lines.clear();
21                 labels.clear();
22                 
23                 //allow user to run help
24                 if(option == "help") { help(); abort = true; }
25                 
26                 else {
27                         //valid paramters for this command
28                         string AlignArray[] =  {"groups","line","label","sorted","scale"};
29                         vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
30                         
31                         parser = new OptionParser();
32                         parser->parse(option, parameters);  delete parser;
33                         
34                         ValidParameters* validParameter = new ValidParameters();
35                 
36                         //check to make sure all parameters are valid for command
37                         for (it = parameters.begin(); it != parameters.end(); it++) { 
38                                 if (validParameter->isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
39                         }
40                         
41                         //make sure the user has already run the read.otu command
42                         if ((globaldata->getListFile() == "") && (globaldata->getSharedFile() == "")) {
43                                  cout << "You must read a list, or a list and a group, or a shared before you can use the heatmap.bin command." << endl; abort = true; 
44                         }
45
46                         //check for optional parameter and set defaults
47                         // ...at some point should added some additional type checking...
48                         line = validParameter->validFile(parameters, "line", false);                            
49                         if (line == "not found") { line = "";  }
50                         else { 
51                                 if(line != "all") {  splitAtDash(line, lines);  allLines = 0;  }
52                                 else { allLines = 1;  }
53                         }
54                         
55                         label = validParameter->validFile(parameters, "label", false);                  
56                         if (label == "not found") { label = ""; }
57                         else { 
58                                 if(label != "all") {  splitAtDash(label, labels);  allLines = 0;  }
59                                 else { allLines = 1;  }
60                         }
61                         
62                         //make sure user did not use both the line and label parameters
63                         if ((line != "") && (label != "")) { cout << "You cannot use both the line and label parameters at the same time. " << endl; abort = true; }
64                         //if the user has not specified any line or labels use the ones from read.otu
65                         else if ((line == "") && (label == "")) {  
66                                 allLines = globaldata->allLines; 
67                                 labels = globaldata->labels; 
68                                 lines = globaldata->lines;
69                         }
70                         
71                         groups = validParameter->validFile(parameters, "groups", false);                        
72                         if (groups == "not found") { groups = ""; }
73                         else { 
74                                 splitAtDash(groups, Groups);
75                                 globaldata->Groups = Groups;
76                         }
77                         
78                         sorted = validParameter->validFile(parameters, "sorted", false);                        if (sorted == "not found") { sorted = "T"; }
79                  
80                         scale = validParameter->validFile(parameters, "scale", false);                          if (scale == "not found") { scale = "log10"; }
81                   
82
83                         delete validParameter;
84                         
85                         if (abort == false) {
86                                 heatmap = new HeatMap(sorted, scale);
87                                 format = globaldata->getFormat();
88                         }
89                 }
90
91         }
92         catch(exception& e) {
93                 cout << "Standard Error: " << e.what() << " has occurred in the HeatMapCommand class Function HeatMapCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
94                 exit(1);
95         }
96         catch(...) {
97                 cout << "An unknown error has occurred in the HeatMapCommand class function HeatMapCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
98                 exit(1);
99         }       
100 }
101
102 //**********************************************************************************************************************
103
104 void HeatMapCommand::help(){
105         try {
106                 cout << "The heatmap.bin command can only be executed after a successful read.otu command." << "\n";
107                 cout << "The heatmap.bin command parameters are groups, sorted, scale, line and label.  No parameters are required, but you may not use line and label at the same time." << "\n";
108                 cout << "The groups parameter allows you to specify which of the groups in your groupfile you would like included in your heatmap." << "\n";
109                 cout << "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";
110                 cout << "The scale parameter allows you to choose the range of color your bin information will be displayed with." << "\n";
111                 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";
112                 cout << "The heatmap.bin command should be in the following format: heatmap.bin(groups=yourGroups, sorted=yourSorted, line=yourLines, label=yourLabels)." << "\n";
113                 cout << "Example heatmap.bin(groups=A-B-C, line=1-3-5, sorted=F, scale=log10)." << "\n";
114                 cout << "The default value for groups is all the groups in your groupfile, and all lines in your inputfile will be used." << "\n";
115                 cout << "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";
116                 cout << "The default value for scale is log10; your other options are log2 and linear." << "\n";
117                 cout << "The heatmap.bin command outputs a .svg file for each line or label you specify." << "\n";
118                 cout << "Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups)." << "\n" << "\n";
119
120         }
121         catch(exception& e) {
122                 cout << "Standard Error: " << e.what() << " has occurred in the HeatMapCommand class Function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
123                 exit(1);
124         }
125         catch(...) {
126                 cout << "An unknown error has occurred in the HeatMapCommand class function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
127                 exit(1);
128         }       
129 }
130
131 //**********************************************************************************************************************
132
133 HeatMapCommand::~HeatMapCommand(){
134         delete input;
135         delete read;
136         delete heatmap;
137 }
138
139 //**********************************************************************************************************************
140
141 int HeatMapCommand::execute(){
142         try {
143         
144                 if (abort == true) { return 0; }
145         
146                 int count = 1;  
147                 RAbundVector* lastRAbund;
148                 vector<SharedRAbundVector*> lastLookup;
149         
150                 if (format == "sharedfile") {
151                         //you have groups
152                         read = new ReadOTUFile(globaldata->inputFileName);      
153                         read->read(&*globaldata); 
154                         
155                         input = globaldata->ginput;
156                         lookup = input->getSharedRAbundVectors();
157                         lastLookup = lookup;
158                 }else if (format == "list") {
159                         //you are using just a list file and have only one group
160                         read = new ReadOTUFile(globaldata->inputFileName);      
161                         read->read(&*globaldata); 
162                         
163                         rabund = globaldata->rabund;
164                         lastRAbund = globaldata->rabund;
165                         input = globaldata->ginput;             
166                 }
167                 
168                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
169                 set<string> processedLabels;
170                 set<string> userLabels = labels;
171                 set<int> userLines = lines;
172
173                 if (format != "list") { 
174                 
175                         //as long as you are not at the end of the file or done wih the lines you want
176                         while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
177                 
178                                 if(allLines == 1 || lines.count(count) == 1 || labels.count(lookup[0]->getLabel()) == 1){                       
179         
180                                         cout << lookup[0]->getLabel() << '\t' << count << endl;
181                                         heatmap->getPic(lookup);
182                                         
183                                         processedLabels.insert(lookup[0]->getLabel());
184                                         userLabels.erase(lookup[0]->getLabel());
185                                         userLines.erase(count);
186                                 }
187                                 
188                                 if ((anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLookup[0]->getLabel()) != 1)) {
189                                         cout << lastLookup[0]->getLabel() << '\t' << count << endl;
190                                         heatmap->getPic(lastLookup);
191                                         
192                                         processedLabels.insert(lastLookup[0]->getLabel());
193                                         userLabels.erase(lastLookup[0]->getLabel());
194                                 }
195                                 
196                                 //prevent memory leak
197                                 if (count != 1) { for (int i = 0; i < lastLookup.size(); i++) {  delete lastLookup[i];  } }
198                                 lastLookup = lookup;                    
199
200                                 //get next line to process
201                                 lookup = input->getSharedRAbundVectors();                               
202                                 count++;
203                         }
204                         
205                         //output error messages about any remaining user labels
206                         set<string>::iterator it;
207                         bool needToRun = false;
208                         for (it = userLabels.begin(); it != userLabels.end(); it++) {  
209                                 cout << "Your file does not include the label "<< *it; 
210                                 if (processedLabels.count(lastLookup[0]->getLabel()) != 1) {
211                                         cout << ". I will use " << lastLookup[0]->getLabel() << "." << endl;
212                                         needToRun = true;
213                                 }else {
214                                         cout << ". Please refer to " << lastLookup[0]->getLabel() << "." << endl;
215                                 }
216                         }
217                 
218                         //run last line if you need to
219                         if (needToRun == true)  {
220                                 cout << lastLookup[0]->getLabel() << '\t' << count << endl;
221                                 heatmap->getPic(lastLookup);
222                         }
223                 
224                         for (int i = 0; i < lastLookup.size(); i++) {  delete lastLookup[i];  }
225                         
226                         //reset groups parameter
227                         globaldata->Groups.clear();  
228                         
229                 }else{
230                 
231                         while((rabund != NULL) && ((allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
232
233                                 if(allLines == 1 || lines.count(count) == 1 || labels.count(rabund->getLabel()) == 1){                  
234         
235                                         cout << rabund->getLabel() << '\t' << count << endl;
236                                         heatmap->getPic(rabund);
237                                         
238                                         processedLabels.insert(rabund->getLabel());
239                                         userLabels.erase(rabund->getLabel());
240                                         userLines.erase(count);
241                                 }
242                                 
243                                 if ((anyLabelsToProcess(rabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastRAbund->getLabel()) != 1)) {
244
245                                         cout << lastRAbund->getLabel() << '\t' << count << endl;
246                                         heatmap->getPic(lastRAbund);
247                                         
248                                         processedLabels.insert(lastRAbund->getLabel());
249                                         userLabels.erase(lastRAbund->getLabel());
250                                 }               
251                                 
252                                 if (count != 1) { delete lastRAbund; }
253                                 lastRAbund = rabund;                    
254
255                                 rabund = input->getRAbundVector();
256                                 count++;
257                         }
258                         
259                         //output error messages about any remaining user labels
260                         set<string>::iterator it;
261                         bool needToRun = false;
262                         for (it = userLabels.begin(); it != userLabels.end(); it++) {  
263                                 cout << "Your file does not include the label "<< *it; 
264                                 if (processedLabels.count(lastRAbund->getLabel()) != 1) {
265                                         cout << ". I will use " << lastRAbund->getLabel() << "." << endl;
266                                         needToRun = true;
267                                 }else {
268                                         cout << ". Please refer to " << lastRAbund->getLabel() << "." << endl;
269                                 }
270                         }
271                 
272                         //run last line if you need to
273                         if (needToRun == true)  {
274                                 cout << lastRAbund->getLabel() << '\t' << count << endl;
275                                 heatmap->getPic(lastRAbund);
276                         }
277                 
278                         delete lastRAbund;
279
280                 }
281                 
282                 return 0;
283         }
284         catch(exception& e) {
285                 cout << "Standard Error: " << e.what() << " has occurred in the HeatMapCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
286                 exit(1);
287         }
288         catch(...) {
289                 cout << "An unknown error has occurred in the HeatMapCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
290                 exit(1);
291         }               
292 }
293
294 //**********************************************************************************************************************
295
296