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