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