]> git.donarmstrong.com Git - mothur.git/blob - heatmapcommand.cpp
added get.rabund and get.sabund command and fixed bug introduced by line by line...
[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(){
16         try {
17                 globaldata = GlobalData::getInstance();
18                 heatmap = new HeatMap();
19                 format = globaldata->getFormat();
20         }
21         catch(exception& e) {
22                 cout << "Standard Error: " << e.what() << " has occurred in the HeatMapCommand class Function HeatMapCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
23                 exit(1);
24         }
25         catch(...) {
26                 cout << "An unknown error has occurred in the HeatMapCommand class function HeatMapCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
27                 exit(1);
28         }       
29 }
30 //**********************************************************************************************************************
31
32 HeatMapCommand::~HeatMapCommand(){
33         delete input;
34         delete read;
35         delete heatmap;
36 }
37
38 //**********************************************************************************************************************
39
40 int HeatMapCommand::execute(){
41         try {
42                 int count = 1;  
43                 RAbundVector* lastRAbund;
44                 vector<SharedRAbundVector*> lastLookup;
45         
46                 if (format == "sharedfile") {
47                         //you have groups
48                         read = new ReadOTUFile(globaldata->inputFileName);      
49                         read->read(&*globaldata); 
50                         
51                         input = globaldata->ginput;
52                         lookup = input->getSharedRAbundVectors();
53                         lastLookup = lookup;
54                 }else if (format == "list") {
55                         //you are using just a list file and have only one group
56                         read = new ReadOTUFile(globaldata->inputFileName);      
57                         read->read(&*globaldata); 
58                         
59                         rabund = globaldata->rabund;
60                         lastRAbund = globaldata->rabund;
61                         input = globaldata->ginput;             
62                 }
63                 
64                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
65                 set<string> processedLabels;
66                 set<string> userLabels = globaldata->labels;
67                 set<int> userLines = globaldata->lines;
68
69                 if (format != "list") { 
70                 
71                         //as long as you are not at the end of the file or done wih the lines you want
72                         while((lookup[0] != NULL) && ((globaldata->allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
73                 
74                                 if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(lookup[0]->getLabel()) == 1){                   
75         
76                                         cout << lookup[0]->getLabel() << '\t' << count << endl;
77                                         heatmap->getPic(lookup);
78                                         
79                                         processedLabels.insert(lookup[0]->getLabel());
80                                         userLabels.erase(lookup[0]->getLabel());
81                                         userLines.erase(count);
82                                 }
83                                 
84                                 if ((anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLookup[0]->getLabel()) != 1)) {
85                                         cout << lastLookup[0]->getLabel() << '\t' << count << endl;
86                                         heatmap->getPic(lastLookup);
87                                         
88                                         processedLabels.insert(lastLookup[0]->getLabel());
89                                         userLabels.erase(lastLookup[0]->getLabel());
90                                 }
91                                 
92                                 //prevent memory leak
93                                 if (count != 1) { for (int i = 0; i < lastLookup.size(); i++) {  delete lastLookup[i];  } }
94                                 lastLookup = lookup;                    
95
96                                 //get next line to process
97                                 lookup = input->getSharedRAbundVectors();                               
98                                 count++;
99                         }
100                         
101                         //output error messages about any remaining user labels
102                         set<string>::iterator it;
103                         bool needToRun = false;
104                         for (it = userLabels.begin(); it != userLabels.end(); it++) {  
105                                 cout << "Your file does not include the label "<< *it; 
106                                 if (processedLabels.count(lastLookup[0]->getLabel()) != 1) {
107                                         cout << ". I will use " << lastLookup[0]->getLabel() << "." << endl;
108                                         needToRun = true;
109                                 }else {
110                                         cout << ". Please refer to " << lastLookup[0]->getLabel() << "." << endl;
111                                 }
112                         }
113                 
114                         //run last line if you need to
115                         if (needToRun == true)  {
116                                 cout << lastLookup[0]->getLabel() << '\t' << count << endl;
117                                 heatmap->getPic(lastLookup);
118                         }
119                 
120                         for (int i = 0; i < lastLookup.size(); i++) {  delete lastLookup[i];  }
121                         
122                         //reset groups parameter
123                         globaldata->Groups.clear();  
124                         
125                 }else{
126                 
127                         while((rabund != NULL) && ((globaldata->allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
128
129                                 if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(rabund->getLabel()) == 1){                      
130         
131                                         cout << rabund->getLabel() << '\t' << count << endl;
132                                         heatmap->getPic(rabund);
133                                         
134                                         processedLabels.insert(rabund->getLabel());
135                                         userLabels.erase(rabund->getLabel());
136                                         userLines.erase(count);
137                                 }
138                                 
139                                 if ((anyLabelsToProcess(rabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastRAbund->getLabel()) != 1)) {
140
141                                         cout << lastRAbund->getLabel() << '\t' << count << endl;
142                                         heatmap->getPic(lastRAbund);
143                                         
144                                         processedLabels.insert(lastRAbund->getLabel());
145                                         userLabels.erase(lastRAbund->getLabel());
146                                 }               
147                                 
148                                 if (count != 1) { delete lastRAbund; }
149                                 lastRAbund = rabund;                    
150
151                                 rabund = input->getRAbundVector();
152                                 count++;
153                         }
154                         
155                         //output error messages about any remaining user labels
156                         set<string>::iterator it;
157                         bool needToRun = false;
158                         for (it = userLabels.begin(); it != userLabels.end(); it++) {  
159                                 cout << "Your file does not include the label "<< *it; 
160                                 if (processedLabels.count(lastRAbund->getLabel()) != 1) {
161                                         cout << ". I will use " << lastRAbund->getLabel() << "." << endl;
162                                         needToRun = true;
163                                 }else {
164                                         cout << ". Please refer to " << lastRAbund->getLabel() << "." << endl;
165                                 }
166                         }
167                 
168                         //run last line if you need to
169                         if (needToRun == true)  {
170                                 cout << lastRAbund->getLabel() << '\t' << count << endl;
171                                 heatmap->getPic(lastRAbund);
172                         }
173                 
174                         delete lastRAbund;
175
176                 }
177                 
178                 globaldata->setGroups("");
179                 return 0;
180         }
181         catch(exception& e) {
182                 cout << "Standard Error: " << e.what() << " has occurred in the HeatMapCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
183                 exit(1);
184         }
185         catch(...) {
186                 cout << "An unknown error has occurred in the HeatMapCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
187                 exit(1);
188         }               
189 }
190
191 //**********************************************************************************************************************
192
193