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