]> git.donarmstrong.com Git - mothur.git/blob - heatmapcommand.cpp
added smart distance feature and optimized all commands using line by line processing
[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
68                 if (format != "list") { 
69                 
70                         //as long as you are not at the end of the file or done wih the lines you want
71                         while((lookup[0] != NULL) && ((globaldata->allLines == 1) || (userLabels.size() != 0))) {
72                 
73                                 if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(lookup[0]->getLabel()) == 1){                   
74         
75                                         cout << lookup[0]->getLabel() << '\t' << count << endl;
76                                         heatmap->getPic(lookup);
77                                         
78                                         processedLabels.insert(lookup[0]->getLabel());
79                                         userLabels.erase(lookup[0]->getLabel());
80                                 }
81                                 
82                                 if ((anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLookup[0]->getLabel()) != 1)) {
83                                         cout << lastLookup[0]->getLabel() << '\t' << count << endl;
84                                         heatmap->getPic(lastLookup);
85                                         
86                                         processedLabels.insert(lastLookup[0]->getLabel());
87                                         userLabels.erase(lastLookup[0]->getLabel());
88                                 }
89                                 
90                                 //prevent memory leak
91                                 if (count != 1) { for (int i = 0; i < lastLookup.size(); i++) {  delete lastLookup[i];  } }
92                                 lastLookup = lookup;                    
93
94                                 //get next line to process
95                                 lookup = input->getSharedRAbundVectors();                               
96                                 count++;
97                         }
98                         
99                         //output error messages about any remaining user labels
100                         set<string>::iterator it;
101                         bool needToRun = false;
102                         for (it = userLabels.begin(); it != userLabels.end(); it++) {  
103                                 cout << "Your file does not include the label "<< *it; 
104                                 if (processedLabels.count(lastLookup[0]->getLabel()) != 1) {
105                                         cout << ". I will use " << lastLookup[0]->getLabel() << "." << endl;
106                                         needToRun = true;
107                                 }else {
108                                         cout << ". Please refer to " << lastLookup[0]->getLabel() << "." << endl;
109                                 }
110                         }
111                 
112                         //run last line if you need to
113                         if (needToRun == true)  {
114                                 cout << lastLookup[0]->getLabel() << '\t' << count << endl;
115                                 heatmap->getPic(lastLookup);
116                         }
117                 
118                         for (int i = 0; i < lastLookup.size(); i++) {  delete lastLookup[i];  }
119                         
120                         //reset groups parameter
121                         globaldata->Groups.clear();  
122                         
123                 }else{
124                 
125                         while((rabund != NULL) && ((globaldata->allLines == 1) || (userLabels.size() != 0))) {
126
127                                 if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(rabund->getLabel()) == 1){                      
128         
129                                         cout << rabund->getLabel() << '\t' << count << endl;
130                                         heatmap->getPic(rabund);
131                                         
132                                         processedLabels.insert(rabund->getLabel());
133                                         userLabels.erase(rabund->getLabel());
134                                 }
135                                 
136                                 if ((anyLabelsToProcess(rabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastRAbund->getLabel()) != 1)) {
137
138                                         cout << lastRAbund->getLabel() << '\t' << count << endl;
139                                         heatmap->getPic(lastRAbund);
140                                         
141                                         processedLabels.insert(lastRAbund->getLabel());
142                                         userLabels.erase(lastRAbund->getLabel());
143                                 }               
144                                 
145                                 if (count != 1) { delete lastRAbund; }
146                                 lastRAbund = rabund;                    
147
148                                 rabund = input->getRAbundVector();
149                                 count++;
150                         }
151                         
152                         //output error messages about any remaining user labels
153                         set<string>::iterator it;
154                         bool needToRun = false;
155                         for (it = userLabels.begin(); it != userLabels.end(); it++) {  
156                                 cout << "Your file does not include the label "<< *it; 
157                                 if (processedLabels.count(lastRAbund->getLabel()) != 1) {
158                                         cout << ". I will use " << lastRAbund->getLabel() << "." << endl;
159                                         needToRun = true;
160                                 }else {
161                                         cout << ". Please refer to " << lastRAbund->getLabel() << "." << endl;
162                                 }
163                         }
164                 
165                         //run last line if you need to
166                         if (needToRun == true)  {
167                                 cout << lastRAbund->getLabel() << '\t' << count << endl;
168                                 heatmap->getPic(lastRAbund);
169                         }
170                 
171                         delete lastRAbund;
172
173                 }
174                 
175                 globaldata->setGroups("");
176                 return 0;
177         }
178         catch(exception& e) {
179                 cout << "Standard Error: " << e.what() << " has occurred in the HeatMapCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
180                 exit(1);
181         }
182         catch(...) {
183                 cout << "An unknown error has occurred in the HeatMapCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
184                 exit(1);
185         }               
186 }
187
188 //**********************************************************************************************************************
189
190