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