]> git.donarmstrong.com Git - mothur.git/blob - collectsharedcommand.cpp
added sort to heatmap to put shared otus at the top and updated some error checking
[mothur.git] / collectsharedcommand.cpp
1 /*
2  *  collectsharedcommand.cpp
3  *  Dotur
4  *
5  *  Created by Sarah Westcott on 1/2/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "collectsharedcommand.h"
11 #include "sharedsobscollectsummary.h"
12 #include "sharedchao1.h"
13 #include "sharedace.h"
14 #include "sharedjabund.h"
15 #include "sharedsorabund.h"
16 #include "sharedjclass.h"
17 #include "sharedsorclass.h"
18 #include "sharedjest.h"
19 #include "sharedsorest.h"
20 #include "sharedthetayc.h"
21 #include "sharedthetan.h"
22 #include "sharedkstest.h"
23 #include "sharedbdiversity.h"
24 #include "sharednseqs.h"
25 #include "sharedochiai.h"
26 #include "sharedanderbergs.h"
27 #include "sharedkulczynski.h"
28 #include "sharedkulczynskicody.h"
29 #include "sharedlennon.h"
30 #include "sharedmorisitahorn.h"
31 #include "sharedbraycurtis.h"
32
33
34
35 //**********************************************************************************************************************
36
37 CollectSharedCommand::CollectSharedCommand(){
38         try {
39                 globaldata = GlobalData::getInstance();
40                 string fileNameRoot;
41                 fileNameRoot = getRootName(globaldata->inputFileName);
42                 format = globaldata->getFormat();
43                 validCalculator = new ValidCalculators();
44                 
45                 int i;
46                 for (i=0; i<globaldata->Estimators.size(); i++) {
47                         if (validCalculator->isValidCalculator("shared", globaldata->Estimators[i]) == true) { 
48                                 if (globaldata->Estimators[i] == "sharedchao") { 
49                                         cDisplays.push_back(new CollectDisplay(new SharedChao1(), new SharedOneColumnFile(fileNameRoot+"shared.chao")));
50                                 }else if (globaldata->Estimators[i] == "sharedsobs") { 
51                                         cDisplays.push_back(new CollectDisplay(new SharedSobsCS(), new SharedOneColumnFile(fileNameRoot+"shared.sobs")));
52                                 }else if (globaldata->Estimators[i] == "sharedace") { 
53                                         cDisplays.push_back(new CollectDisplay(new SharedAce(), new SharedOneColumnFile(fileNameRoot+"shared.ace")));
54                                 }else if (globaldata->Estimators[i] == "sharedjabund") {        
55                                         cDisplays.push_back(new CollectDisplay(new SharedJAbund(), new SharedOneColumnFile(fileNameRoot+"shared.jabund")));
56                                 }else if (globaldata->Estimators[i] == "sharedsorensonabund") { 
57                                         cDisplays.push_back(new CollectDisplay(new SharedSorAbund(), new SharedOneColumnFile(fileNameRoot+"shared.sorabund")));
58                                 }else if (globaldata->Estimators[i] == "sharedjclass") { 
59                                         cDisplays.push_back(new CollectDisplay(new SharedJclass(), new SharedOneColumnFile(fileNameRoot+"shared.jclass")));
60                                 }else if (globaldata->Estimators[i] == "sharedsorclass") { 
61                                         cDisplays.push_back(new CollectDisplay(new SharedSorClass(), new SharedOneColumnFile(fileNameRoot+"shared.sorclass")));
62                                 }else if (globaldata->Estimators[i] == "sharedjest") { 
63                                         cDisplays.push_back(new CollectDisplay(new SharedJest(), new SharedOneColumnFile(fileNameRoot+"shared.jest")));
64                                 }else if (globaldata->Estimators[i] == "sharedsorest") { 
65                                         cDisplays.push_back(new CollectDisplay(new SharedSorEst(), new SharedOneColumnFile(fileNameRoot+"shared.sorest")));
66                                 }else if (globaldata->Estimators[i] == "sharedthetayc") { 
67                                         cDisplays.push_back(new CollectDisplay(new SharedThetaYC(), new SharedOneColumnFile(fileNameRoot+"shared.thetayc")));
68                                 }else if (globaldata->Estimators[i] == "sharedthetan") { 
69                                         cDisplays.push_back(new CollectDisplay(new SharedThetaN(), new SharedOneColumnFile(fileNameRoot+"shared.thetan")));
70                                 }else if (globaldata->Estimators[i] == "sharedkstest") { 
71                                         cDisplays.push_back(new CollectDisplay(new SharedKSTest(), new SharedOneColumnFile(fileNameRoot+"shared.kstest")));
72                                 }else if (globaldata->Estimators[i] == "sharedbdiversity") { 
73                                         cDisplays.push_back(new CollectDisplay(new SharedBDiversity(), new SharedOneColumnFile(fileNameRoot+"shared.bdiversity")));
74                                 }else if (globaldata->Estimators[i] == "sharednseqs") { 
75                                         cDisplays.push_back(new CollectDisplay(new SharedNSeqs(), new SharedOneColumnFile(fileNameRoot+"shared.nseqs")));
76                                 }else if (globaldata->Estimators[i] == "sharedochiai") { 
77                                         cDisplays.push_back(new CollectDisplay(new SharedOchiai(), new SharedOneColumnFile(fileNameRoot+"shared.ochiai")));
78                                 }else if (globaldata->Estimators[i] == "sharedanderberg") { 
79                                         cDisplays.push_back(new CollectDisplay(new SharedAnderberg(), new SharedOneColumnFile(fileNameRoot+"shared.anderberg")));
80                                 }else if (globaldata->Estimators[i] == "sharedkulczynski") { 
81                                         cDisplays.push_back(new CollectDisplay(new SharedKulczynski(), new SharedOneColumnFile(fileNameRoot+"shared.kulczynski")));
82                                 }else if (globaldata->Estimators[i] == "sharedkulczynskicody") { 
83                                         cDisplays.push_back(new CollectDisplay(new SharedKulczynskiCody(), new SharedOneColumnFile(fileNameRoot+"shared.kulczynskicody")));
84                                 }else if (globaldata->Estimators[i] == "sharedlennon") { 
85                                         cDisplays.push_back(new CollectDisplay(new SharedLennon(), new SharedOneColumnFile(fileNameRoot+"shared.lennon")));
86                                 }else if (globaldata->Estimators[i] == "sharedmorisitahorn") { 
87                                         cDisplays.push_back(new CollectDisplay(new SharedMorHorn(), new SharedOneColumnFile(fileNameRoot+"shared.morisitahorn")));
88                                 }else if (globaldata->Estimators[i] == "sharedbraycurtis") { 
89                                         cDisplays.push_back(new CollectDisplay(new SharedBrayCurtis(), new SharedOneColumnFile(fileNameRoot+"shared.braycurtis")));
90                                 }
91                         }
92                 }
93                 
94                 //reset calc for next command
95                 globaldata->setCalc("");
96
97         }
98         catch(exception& e) {
99                 cout << "Standard Error: " << e.what() << " has occurred in the CollectSharedCommand class Function CollectSharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
100                 exit(1);
101         }
102         catch(...) {
103                 cout << "An unknown error has occurred in the CollectSharedCommand class function CollectSharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
104                 exit(1);
105         }       
106                         
107 }
108
109 //**********************************************************************************************************************
110
111 CollectSharedCommand::~CollectSharedCommand(){
112         delete order;
113         delete input;
114         delete cCurve;
115         delete read;
116 }
117
118 //**********************************************************************************************************************
119
120 int CollectSharedCommand::execute(){
121         try {
122                 int count = 1;
123                 
124                 //if the users entered no valid calculators don't execute command
125                 if (cDisplays.size() == 0) { return 0; }
126                 
127                 if (format == "sharedfile") {
128                         read = new ReadPhilFile(globaldata->inputFileName);     
129                         read->read(&*globaldata); 
130                         
131                         input = globaldata->ginput;
132                         order = input->getSharedOrderVector();
133                 }else {
134                         //you are using a list and a groupfile
135                         read = new ReadPhilFile(globaldata->inputFileName);     
136                         read->read(&*globaldata); 
137                 
138                         input = globaldata->ginput;
139                         SharedList = globaldata->gSharedList;
140                         order = SharedList->getSharedOrderVector();
141                 }
142                 set<string> orderList;
143                 
144                 //set users groups
145                 setGroups();
146
147                 while(order != NULL){
148                         orderList.insert(order->getLabel());
149                         if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(order->getLabel()) == 1){
150                                 //create collectors curve
151                                 cCurve = new Collect(order, cDisplays);
152                                 convert(globaldata->getFreq(), freq);
153                                 cCurve->getSharedCurve(freq);
154                         
155                                 delete cCurve;
156                         
157                                 cout << order->getLabel() << '\t' << count << endl;
158                         }
159                         
160                         //get next line to process
161                         if (format == "sharedfile") {
162                                 order = input->getSharedOrderVector();
163                         }else {
164                                 //you are using a list and a groupfile
165                                 SharedList = input->getSharedListVector(); //get new list vector to process
166                                 if (SharedList != NULL) {
167                                         order = SharedList->getSharedOrderVector(); //gets new order vector with group info.
168                                 }else {
169                                         break;
170                                 }
171                         }
172                         
173                         count++;
174                 }
175                 set<string>::iterator i;
176                 for(i = globaldata->labels.begin(); i != globaldata->labels.end(); ++i)
177                         if(orderList.count(*i) == 0)
178                                 cout << "'" << *i << "'" << " is not a valid label.\n";
179                 for(int i=0;i<cDisplays.size();i++){    delete cDisplays[i];    }       
180                 
181                 //reset groups parameter
182                 globaldata->Groups.clear();  globaldata->setGroups("");
183                 
184                 return 0;
185         }
186         catch(exception& e) {
187                 cout << "Standard Error: " << e.what() << " has occurred in the CollectSharedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
188                 exit(1);
189         }
190         catch(...) {
191                 cout << "An unknown error has occurred in the CollectSharedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
192                 exit(1);
193         }       
194 }
195
196
197 //**********************************************************************************************************************
198 void CollectSharedCommand::setGroups() {
199         try {
200                 //if the user has not entered specific groups to analyze then do them all
201                 if (globaldata->Groups.size() != 0) {
202                         if (globaldata->Groups[0] != "all") {
203                                 //check that groups are valid
204                                 for (int i = 0; i < globaldata->Groups.size(); i++) {
205                                         if (globaldata->gGroupmap->isValidGroup(globaldata->Groups[i]) != true) {
206                                                 cout << globaldata->Groups[i] << " is not a valid group, and will be disregarded." << endl;
207                                                 // erase the invalid group from globaldata->Groups
208                                                 globaldata->Groups.erase(globaldata->Groups.begin()+i);
209                                         }
210                                 }
211                         
212                                 //if the user only entered invalid groups
213                                 if ((globaldata->Groups.size() == 0) || (globaldata->Groups.size() == 1)) { 
214                                         cout << "When using the groups parameter you must have at least 2 valid groups. I will run the command using all the groups in your groupfile." << endl; 
215                                         for (int i = 0; i < globaldata->gGroupmap->namesOfGroups.size(); i++) {
216                                                 globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
217                                         }
218                                 }
219                         }else{//user has enter "all" and wants the default groups
220                                 globaldata->Groups.clear();
221                                 for (int i = 0; i < globaldata->gGroupmap->namesOfGroups.size(); i++) {
222                                         globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
223                                 }
224                                 globaldata->setGroups("");
225                         }
226                 }else {
227                         for (int i = 0; i < globaldata->gGroupmap->namesOfGroups.size(); i++) {
228                                 globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
229                         }
230                 }
231                 
232         }
233         catch(exception& e) {
234                 cout << "Standard Error: " << e.what() << " has occurred in the CollectSharedCommand class Function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
235                 exit(1);
236         }
237         catch(...) {
238                 cout << "An unknown error has occurred in the CollectSharedCommand class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
239                 exit(1);
240         }               
241
242 }
243 /***********************************************************/
244