]> git.donarmstrong.com Git - mothur.git/blob - heatmapsimcommand.cpp
added heatmap.sim command and changed heatmap to heatmap.bin
[mothur.git] / heatmapsimcommand.cpp
1 /*
2  *  heatmapsimcommand.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 6/8/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "heatmapsimcommand.h"
11 #include "sharedjabund.h"
12 #include "sharedsorabund.h"
13 #include "sharedjclass.h"
14 #include "sharedsorclass.h"
15 #include "sharedjest.h"
16 #include "sharedsorest.h"
17 #include "sharedthetayc.h"
18 #include "sharedthetan.h"
19 #include "sharedmorisitahorn.h"
20 #include "sharedbraycurtis.h"
21
22
23 //**********************************************************************************************************************
24
25 HeatMapSimCommand::HeatMapSimCommand(){
26         try {
27                 globaldata = GlobalData::getInstance();
28                 validCalculator = new ValidCalculators();
29                 heatmap = new HeatMapSim();
30                         
31                 int i;
32                 for (i=0; i<globaldata->Estimators.size(); i++) {
33                         if (validCalculator->isValidCalculator("heat", globaldata->Estimators[i]) == true) { 
34                                 if (globaldata->Estimators[i] == "jabund") {    
35                                         heatCalculators.push_back(new JAbund());
36                                 }else if (globaldata->Estimators[i] == "sorabund") { 
37                                         heatCalculators.push_back(new SorAbund());
38                                 }else if (globaldata->Estimators[i] == "jclass") { 
39                                         heatCalculators.push_back(new Jclass());
40                                 }else if (globaldata->Estimators[i] == "sorclass") { 
41                                         heatCalculators.push_back(new SorClass());
42                                 }else if (globaldata->Estimators[i] == "jest") { 
43                                         heatCalculators.push_back(new Jest());
44                                 }else if (globaldata->Estimators[i] == "sorest") { 
45                                         heatCalculators.push_back(new SorEst());
46                                 }else if (globaldata->Estimators[i] == "thetayc") { 
47                                         heatCalculators.push_back(new ThetaYC());
48                                 }else if (globaldata->Estimators[i] == "thetan") { 
49                                         heatCalculators.push_back(new ThetaN());
50                                 }else if (globaldata->Estimators[i] == "morisitahorn") { 
51                                         heatCalculators.push_back(new MorHorn());
52                                 }else if (globaldata->Estimators[i] == "braycurtis") { 
53                                         heatCalculators.push_back(new BrayCurtis());
54                                 }
55                         }
56                 }
57                 
58                 //reset calc for next command
59                 globaldata->setCalc("");
60
61
62         }
63         catch(exception& e) {
64                 cout << "Standard Error: " << e.what() << " has occurred in the HeatMapSimCommand class Function HeatMapSimCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
65                 exit(1);
66         }
67         catch(...) {
68                 cout << "An unknown error has occurred in the HeatMapSimCommand class function HeatMapSimCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
69                 exit(1);
70         }       
71 }
72 //**********************************************************************************************************************
73
74 HeatMapSimCommand::~HeatMapSimCommand(){
75         delete input;
76         delete read;
77         delete heatmap;
78 }
79
80 //**********************************************************************************************************************
81
82 int HeatMapSimCommand::execute(){
83         try {
84                 int count = 1;  
85                 
86                 //if the users entered no valid calculators don't execute command
87                 if (heatCalculators.size() == 0) { cout << "No valid calculators." << endl; return 0; }
88                 
89                 //you have groups
90                 read = new ReadOTUFile(globaldata->inputFileName);      
91                 read->read(&*globaldata); 
92                         
93                 input = globaldata->ginput;
94                 lookup = input->getSharedRAbundVectors();
95                 vector<SharedRAbundVector*> lastLookup = lookup;
96                 
97                 if (lookup.size() < 2) { cout << "You have not provided enough valid groups.  I cannot run the command." << endl; return 0;}
98                                 
99                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
100                 set<string> processedLabels;
101                 set<string> userLabels = globaldata->labels;
102                 set<int> userLines = globaldata->lines;
103
104                 
105                 //as long as you are not at the end of the file or done wih the lines you want
106                 while((lookup[0] != NULL) && ((globaldata->allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
107                 
108                         if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(lookup[0]->getLabel()) == 1){                   
109         
110                                 cout << lookup[0]->getLabel() << '\t' << count << endl;
111                                 heatmap->getPic(lookup, heatCalculators);
112                                         
113                                 processedLabels.insert(lookup[0]->getLabel());
114                                 userLabels.erase(lookup[0]->getLabel());
115                                 userLines.erase(count);
116                         }
117                                 
118                         if ((anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLookup[0]->getLabel()) != 1)) {
119                                 cout << lastLookup[0]->getLabel() << '\t' << count << endl;
120                                 heatmap->getPic(lastLookup, heatCalculators);
121                                         
122                                 processedLabels.insert(lastLookup[0]->getLabel());
123                                 userLabels.erase(lastLookup[0]->getLabel());
124                         }
125                                 
126                         //prevent memory leak
127                         if (count != 1) { for (int i = 0; i < lastLookup.size(); i++) {  delete lastLookup[i];  } }
128                         lastLookup = lookup;                    
129
130                         //get next line to process
131                         lookup = input->getSharedRAbundVectors();                               
132                         count++;
133                 }
134                         
135                 //output error messages about any remaining user labels
136                 set<string>::iterator it;
137                 bool needToRun = false;
138                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
139                         cout << "Your file does not include the label "<< *it; 
140                         if (processedLabels.count(lastLookup[0]->getLabel()) != 1) {
141                                 cout << ". I will use " << lastLookup[0]->getLabel() << "." << endl;
142                                 needToRun = true;
143                         }else {
144                                 cout << ". Please refer to " << lastLookup[0]->getLabel() << "." << endl;
145                         }
146                 }
147                 
148                 //run last line if you need to
149                 if (needToRun == true)  {
150                         cout << lastLookup[0]->getLabel() << '\t' << count << endl;
151                         heatmap->getPic(lastLookup, heatCalculators);
152                 }
153                 
154                 for (int i = 0; i < lastLookup.size(); i++) {  delete lastLookup[i];  }
155                         
156                 //reset groups parameter
157                 globaldata->Groups.clear();  
158                 globaldata->setGroups("");
159                 
160                 return 0;
161         }
162         catch(exception& e) {
163                 cout << "Standard Error: " << e.what() << " has occurred in the HeatMapSimCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
164                 exit(1);
165         }
166         catch(...) {
167                 cout << "An unknown error has occurred in the HeatMapSimCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
168                 exit(1);
169         }               
170 }
171
172 //**********************************************************************************************************************