]> git.donarmstrong.com Git - mothur.git/blob - collectsharedcommand.cpp
added get.rabund and get.sabund command and fixed bug introduced by line by line...
[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 "whittaker.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 #include "sharedjackknife.h"
33 #include "whittaker.h"
34
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                 convert(globaldata->getFreq(), freq);
46                 validCalculator = new ValidCalculators();
47                 util = new SharedUtil();
48                 
49                 int i;
50                 for (i=0; i<globaldata->Estimators.size(); i++) {
51                         if (validCalculator->isValidCalculator("shared", globaldata->Estimators[i]) == true) { 
52                                 if (globaldata->Estimators[i] == "sharedchao") { 
53                                         cDisplays.push_back(new CollectDisplay(new SharedChao1(), new SharedOneColumnFile(fileNameRoot+"shared.chao")));
54                                 }else if (globaldata->Estimators[i] == "sharedsobs") { 
55                                         cDisplays.push_back(new CollectDisplay(new SharedSobsCS(), new SharedOneColumnFile(fileNameRoot+"shared.sobs")));
56                                 }else if (globaldata->Estimators[i] == "sharedace") { 
57                                         cDisplays.push_back(new CollectDisplay(new SharedAce(), new SharedOneColumnFile(fileNameRoot+"shared.ace")));
58                                 }else if (globaldata->Estimators[i] == "jabund") {      
59                                         cDisplays.push_back(new CollectDisplay(new JAbund(), new SharedOneColumnFile(fileNameRoot+"jabund")));
60                                 }else if (globaldata->Estimators[i] == "sorabund") { 
61                                         cDisplays.push_back(new CollectDisplay(new SorAbund(), new SharedOneColumnFile(fileNameRoot+"sorabund")));
62                                 }else if (globaldata->Estimators[i] == "jclass") { 
63                                         cDisplays.push_back(new CollectDisplay(new Jclass(), new SharedOneColumnFile(fileNameRoot+"jclass")));
64                                 }else if (globaldata->Estimators[i] == "sorclass") { 
65                                         cDisplays.push_back(new CollectDisplay(new SorClass(), new SharedOneColumnFile(fileNameRoot+"sorclass")));
66                                 }else if (globaldata->Estimators[i] == "jest") { 
67                                         cDisplays.push_back(new CollectDisplay(new Jest(), new SharedOneColumnFile(fileNameRoot+"jest")));
68                                 }else if (globaldata->Estimators[i] == "sorest") { 
69                                         cDisplays.push_back(new CollectDisplay(new SorEst(), new SharedOneColumnFile(fileNameRoot+"sorest")));
70                                 }else if (globaldata->Estimators[i] == "thetayc") { 
71                                         cDisplays.push_back(new CollectDisplay(new ThetaYC(), new SharedOneColumnFile(fileNameRoot+"thetayc")));
72                                 }else if (globaldata->Estimators[i] == "thetan") { 
73                                         cDisplays.push_back(new CollectDisplay(new ThetaN(), new SharedOneColumnFile(fileNameRoot+"thetan")));
74                                 }else if (globaldata->Estimators[i] == "kstest") { 
75                                         cDisplays.push_back(new CollectDisplay(new KSTest(), new SharedOneColumnFile(fileNameRoot+"kstest")));
76                                 }else if (globaldata->Estimators[i] == "whittaker") { 
77                                         cDisplays.push_back(new CollectDisplay(new Whittaker(), new SharedOneColumnFile(fileNameRoot+"whittaker")));
78                                 }else if (globaldata->Estimators[i] == "sharednseqs") { 
79                                         cDisplays.push_back(new CollectDisplay(new SharedNSeqs(), new SharedOneColumnFile(fileNameRoot+"shared.nseqs")));
80
81                                 }else if (globaldata->Estimators[i] == "ochiai") { 
82                                         cDisplays.push_back(new CollectDisplay(new Ochiai(), new SharedOneColumnFile(fileNameRoot+"ochiai")));
83                                 }else if (globaldata->Estimators[i] == "anderberg") { 
84                                         cDisplays.push_back(new CollectDisplay(new Anderberg(), new SharedOneColumnFile(fileNameRoot+"anderberg")));
85                                 }else if (globaldata->Estimators[i] == "skulczynski") { 
86                                         cDisplays.push_back(new CollectDisplay(new Kulczynski(), new SharedOneColumnFile(fileNameRoot+"kulczynski")));
87                                 }else if (globaldata->Estimators[i] == "kulczynskicody") { 
88                                         cDisplays.push_back(new CollectDisplay(new KulczynskiCody(), new SharedOneColumnFile(fileNameRoot+"kulczynskicody")));
89                                 }else if (globaldata->Estimators[i] == "lennon") { 
90                                         cDisplays.push_back(new CollectDisplay(new Lennon(), new SharedOneColumnFile(fileNameRoot+"lennon")));
91                                 }else if (globaldata->Estimators[i] == "morisitahorn") { 
92                                         cDisplays.push_back(new CollectDisplay(new MorHorn(), new SharedOneColumnFile(fileNameRoot+"morisitahorn")));
93                                 }else if (globaldata->Estimators[i] == "braycurtis") { 
94                                         cDisplays.push_back(new CollectDisplay(new BrayCurtis(), new SharedOneColumnFile(fileNameRoot+"braycurtis")));
95                                 }
96                         }
97                 }
98                 
99                 //reset calc for next command
100                 globaldata->setCalc("");
101
102         }
103         catch(exception& e) {
104                 cout << "Standard Error: " << e.what() << " has occurred in the CollectSharedCommand class Function CollectSharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
105                 exit(1);
106         }
107         catch(...) {
108                 cout << "An unknown error has occurred in the CollectSharedCommand class function CollectSharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
109                 exit(1);
110         }       
111                         
112 }
113
114 //**********************************************************************************************************************
115
116 CollectSharedCommand::~CollectSharedCommand(){
117         delete order;
118         delete input;
119         delete cCurve;
120         delete read;
121         delete util;
122 }
123
124 //**********************************************************************************************************************
125
126 int CollectSharedCommand::execute(){
127         try {
128                 int count = 1;
129                 
130                 //if the users entered no valid calculators don't execute command
131                 if (cDisplays.size() == 0) { return 0; }
132                 
133                 read = new ReadOTUFile(globaldata->inputFileName);      
134                 read->read(&*globaldata); 
135                         
136                 input = globaldata->ginput;
137                 order = input->getSharedOrderVector();
138                 SharedOrderVector* lastOrder = order;
139                 
140                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
141                 set<string> processedLabels;
142                 set<string> userLabels = globaldata->labels;
143                 set<int> userLines = globaldata->lines;
144                                 
145                 //set users groups
146                 util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups, "collect");
147                 util->updateGroupIndex(globaldata->Groups, globaldata->gGroupmap->groupIndex);
148
149                 while((order != NULL) && ((globaldata->allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
150
151                         if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(order->getLabel()) == 1){
152                                 
153                                 //create collectors curve
154                                 cCurve = new Collect(order, cDisplays);
155                                 cCurve->getSharedCurve(freq);
156                                 delete cCurve;
157                         
158                                 cout << order->getLabel() << '\t' << count << endl;
159                                 processedLabels.insert(order->getLabel());
160                                 userLabels.erase(order->getLabel());
161                                 userLines.erase(count);
162
163                         //you have a label the user want that is smaller than this line and the last line has not already been processed 
164                         }
165                         
166                         if ((anyLabelsToProcess(order->getLabel(), userLabels, "") == true) && (processedLabels.count(lastOrder->getLabel()) != 1)) {
167                                 //create collectors curve
168                                 cCurve = new Collect(lastOrder, cDisplays);
169                                 cCurve->getSharedCurve(freq);
170                                 delete cCurve;
171                         
172                                 cout << lastOrder->getLabel() << '\t' << count << endl;
173                                 processedLabels.insert(lastOrder->getLabel());
174                                 userLabels.erase(lastOrder->getLabel());
175                         }
176                         
177                         if (count != 1) { delete lastOrder; }
178                         lastOrder = order;                      
179                         
180                         //get next line to process
181                         order = input->getSharedOrderVector();
182                         count++;
183                 }
184                 
185                 //output error messages about any remaining user labels
186                 set<string>::iterator it;
187                 bool needToRun = false;
188                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
189                         cout << "Your file does not include the label "<< *it; 
190                         if (processedLabels.count(lastOrder->getLabel()) != 1) {
191                                 cout << ". I will use " << lastOrder->getLabel() << "." << endl;
192                                 needToRun = true;
193                         }else {
194                                 cout << ". Please refer to " << lastOrder->getLabel() << "." << endl;
195                         }
196                 }
197                 
198                 //run last line if you need to
199                 if (needToRun == true)  {
200                         cCurve = new Collect(lastOrder, cDisplays);
201                         cCurve->getCurve(freq);
202                         delete cCurve;
203                         
204                         cout << lastOrder->getLabel() << '\t' << count << endl;
205                 }
206                 
207                 delete lastOrder;
208                 for(int i=0;i<cDisplays.size();i++){    delete cDisplays[i];    }       
209                 
210                 //reset groups parameter
211                 globaldata->Groups.clear();  globaldata->setGroups("");
212                 
213                 return 0;
214         }
215         catch(exception& e) {
216                 cout << "Standard Error: " << e.what() << " has occurred in the CollectSharedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
217                 exit(1);
218         }
219         catch(...) {
220                 cout << "An unknown error has occurred in the CollectSharedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
221                 exit(1);
222         }       
223 }
224
225 /***********************************************************/
226