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