]> git.donarmstrong.com Git - mothur.git/blob - heatmapcommand.cpp
created mothurOut class to handle logfiles
[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                         if (abort == false) {
78                                 heatmap = new HeatMap(sorted, scale, outputDir);
79                                 format = globaldata->getFormat();
80                         }
81                 }
82
83         }
84         catch(exception& e) {
85                 m->errorOut(e, "HeatMapCommand", "HeatMapCommand");
86                 exit(1);
87         }
88 }
89
90 //**********************************************************************************************************************
91
92 void HeatMapCommand::help(){
93         try {
94                 m->mothurOut("The heatmap.bin command can only be executed after a successful read.otu command.\n");
95                 m->mothurOut("The heatmap.bin command parameters are groups, sorted, scale label.  No parameters are required.\n");
96                 m->mothurOut("The groups parameter allows you to specify which of the groups in your groupfile you would like included in your heatmap.\n");
97                 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");
98                 m->mothurOut("The scale parameter allows you to choose the range of color your bin information will be displayed with.\n");
99                 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");
100                 m->mothurOut("The heatmap.bin command should be in the following format: heatmap.bin(groups=yourGroups, sorted=yourSorted, label=yourLabels).\n");
101                 m->mothurOut("Example heatmap.bin(groups=A-B-C, sorted=F, scale=log10).\n");
102                 m->mothurOut("The default value for groups is all the groups in your groupfile, and all labels in your inputfile will be used.\n");
103                 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");
104                 m->mothurOut("The default value for scale is log10; your other options are log2 and linear.\n");
105                 m->mothurOut("The heatmap.bin command outputs a .svg file for each label you specify.\n");
106                 m->mothurOut("Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n\n");
107
108         }
109         catch(exception& e) {
110                 m->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                                         m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
165                                         outputNames.push_back(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                                         m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
177                                         
178                                         outputNames.push_back(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                                 m->mothurOut("Your file does not include the label " + *it); 
200                                 if (processedLabels.count(lastLabel) != 1) {
201                                         m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
202                                         needToRun = true;
203                                 }else {
204                                         m->mothurOut(". Please refer to " + lastLabel + "."); m->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                                 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
214                                 outputNames.push_back(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                                         m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
230                                         outputNames.push_back(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                                         m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
242                                         
243                                         outputNames.push_back(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                                 m->mothurOut("Your file does not include the label " + *it); 
264                                 if (processedLabels.count(lastLabel) != 1) {
265                                         m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
266                                         needToRun = true;
267                                 }else {
268                                         m->mothurOut(". Please refer to " + lastLabel + "."); m->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                                 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
278                                         
279                                 outputNames.push_back(heatmap->getPic(rabund));
280                                 delete rabund; globaldata->rabund = NULL;
281                         }
282                 
283                 }
284                 
285                 globaldata->rabund = NULL;
286                 delete input; globaldata->ginput = NULL;
287                 
288                 m->mothurOutEndLine();
289                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
290                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
291                 m->mothurOutEndLine();
292
293                 return 0;
294         }
295         catch(exception& e) {
296                 m->errorOut(e, "HeatMapCommand", "execute");
297                 exit(1);
298         }
299 }
300
301 //**********************************************************************************************************************
302
303