]> git.donarmstrong.com Git - mothur.git/blob - collectsharedcommand.cpp
added ability for user to select which groups to analyze with the collect.shared...
[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
23
24 //**********************************************************************************************************************
25
26 CollectSharedCommand::CollectSharedCommand(){
27         try {
28                 globaldata = GlobalData::getInstance();
29                 string fileNameRoot;
30                 fileNameRoot = getRootName(globaldata->inputFileName);
31                 format = globaldata->getFormat();
32                 validCalculator = new ValidCalculators();
33                 
34                 //set users groups
35                 setGroups();
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                                 }
63                         }
64                 }
65                 
66                 //reset calc for next command
67                 globaldata->setCalc("");
68
69         }
70         catch(exception& e) {
71                 cout << "Standard Error: " << e.what() << " has occurred in the CollectSharedCommand class Function CollectSharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
72                 exit(1);
73         }
74         catch(...) {
75                 cout << "An unknown error has occurred in the CollectSharedCommand class function CollectSharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
76                 exit(1);
77         }       
78                         
79 }
80
81 //**********************************************************************************************************************
82
83 CollectSharedCommand::~CollectSharedCommand(){
84         delete order;
85         delete input;
86         delete cCurve;
87         delete read;
88 }
89
90 //**********************************************************************************************************************
91
92 int CollectSharedCommand::execute(){
93         try {
94                 int count = 1;
95                 
96                 //if the users entered no valid calculators don't execute command
97                 if (cDisplays.size() == 0) { return 0; }
98                 
99                 if (format == "sharedfile") {
100                         read = new ReadPhilFile(globaldata->inputFileName);     
101                         read->read(&*globaldata); 
102                         
103                         input = globaldata->ginput;
104                         order = input->getSharedOrderVector();
105                 }else {
106                         //you are using a list and a groupfile
107                         read = new ReadPhilFile(globaldata->inputFileName);     
108                         read->read(&*globaldata); 
109                 
110                         input = globaldata->ginput;
111                         SharedList = globaldata->gSharedList;
112                         order = SharedList->getSharedOrderVector();
113                 }
114                 set<string> orderList;
115
116                 while(order != NULL){
117                         orderList.insert(order->getLabel());
118                         if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(order->getLabel()) == 1){
119                                 //create collectors curve
120                                 cCurve = new Collect(order, cDisplays);
121                                 convert(globaldata->getFreq(), freq);
122                                 cCurve->getSharedCurve(freq);
123                         
124                                 delete cCurve;
125                         
126                                 cout << order->getLabel() << '\t' << count << endl;
127                         }
128                         
129                         //get next line to process
130                         if (format == "sharedfile") {
131                                 order = input->getSharedOrderVector();
132                         }else {
133                                 //you are using a list and a groupfile
134                                 SharedList = input->getSharedListVector(); //get new list vector to process
135                                 if (SharedList != NULL) {
136                                         order = SharedList->getSharedOrderVector(); //gets new order vector with group info.
137                                 }else {
138                                         break;
139                                 }
140                         }
141                         
142                         count++;
143                 }
144                 set<string>::iterator i;
145                 for(i = globaldata->labels.begin(); i != globaldata->labels.end(); ++i)
146                         if(orderList.count(*i) == 0)
147                                 cout << "'" << *i << "'" << " is not a valid label.\n";
148                 for(int i=0;i<cDisplays.size();i++){    delete cDisplays[i];    }       
149                 
150                 //reset groups parameter
151                 globaldata->Groups.clear();  globaldata->setGroups("");
152                 
153                 return 0;
154         }
155         catch(exception& e) {
156                 cout << "Standard Error: " << e.what() << " has occurred in the CollectSharedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
157                 exit(1);
158         }
159         catch(...) {
160                 cout << "An unknown error has occurred in the CollectSharedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
161                 exit(1);
162         }       
163 }
164
165
166 //**********************************************************************************************************************
167 void CollectSharedCommand::setGroups() {
168         try {
169                 //if the user has not entered specific groups to analyze then do them all
170                 if (globaldata->Groups.size() != 0) {
171                         if (globaldata->Groups[0] != "all") {
172                                 //check that groups are valid
173                                 for (int i = 0; i < globaldata->Groups.size(); i++) {
174                                         if (globaldata->gGroupmap->isValidGroup(globaldata->Groups[i]) != true) {
175                                                 cout << globaldata->Groups[i] << " is not a valid group, and will be disregarded." << endl;
176                                                 // erase the invalid group from globaldata->Groups
177                                                 globaldata->Groups.erase(globaldata->Groups.begin()+i);
178                                         }
179                                 }
180                         
181                                 //if the user only entered invalid groups
182                                 if ((globaldata->Groups.size() == 0) || (globaldata->Groups.size() == 1)) { 
183                                         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; 
184                                         for (int i = 0; i < globaldata->gGroupmap->namesOfGroups.size(); i++) {
185                                                 globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
186                                         }
187                                 }
188                         }else{//user has enter "all" and wants the default groups
189                                 globaldata->Groups.clear();
190                                 for (int i = 0; i < globaldata->gGroupmap->namesOfGroups.size(); i++) {
191                                         globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
192                                 }
193                                 globaldata->setGroups("");
194                         }
195                 }else {
196                         for (int i = 0; i < globaldata->gGroupmap->namesOfGroups.size(); i++) {
197                                 globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
198                         }
199                 }
200                 
201         }
202         catch(exception& e) {
203                 cout << "Standard Error: " << e.what() << " has occurred in the CollectSharedCommand class Function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
204                 exit(1);
205         }
206         catch(...) {
207                 cout << "An unknown error has occurred in the CollectSharedCommand class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
208                 exit(1);
209         }               
210
211 }
212 /***********************************************************/
213