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