]> git.donarmstrong.com Git - mothur.git/blob - venncommand.cpp
venn command with up to 4 groups using sharedsobs, sharedchao and sharedace estimator...
[mothur.git] / venncommand.cpp
1 /*
2  *  venncommand.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 3/30/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "venncommand.h"
11 #include "ace.h"
12 #include "sobs.h"
13 #include "chao1.h"
14 #include "jackknife.h"
15 #include "sharedsobscollectsummary.h"
16 #include "sharedchao1.h"
17 #include "sharedace.h"
18
19
20 //**********************************************************************************************************************
21
22 VennCommand::VennCommand(){
23         try {
24                 globaldata = GlobalData::getInstance();
25                 format = globaldata->getFormat();
26                 validCalculator = new ValidCalculators();
27                 
28                 int i;
29                 
30                 if (format == "list") {
31                         for (i=0; i<globaldata->Estimators.size(); i++) {
32                                 if (validCalculator->isValidCalculator("vennsingle", globaldata->Estimators[i]) == true) { 
33                                         if (globaldata->Estimators[i] == "sobs") { 
34                                                 vennCalculators.push_back(new Sobs());
35                                         }else if (globaldata->Estimators[i] == "chao") { 
36                                                 vennCalculators.push_back(new Chao1());
37                                         }else if (globaldata->Estimators[i] == "ace") {
38                                                 convert(globaldata->getAbund(), abund);
39                                                 if(abund < 5)
40                                                         abund = 10;
41                                                 vennCalculators.push_back(new Ace(abund));
42                                         }else if (globaldata->Estimators[i] == "jack") {        
43                                                 vennCalculators.push_back(new Jackknife());
44                                         }
45                                 }
46                         }
47                 }else {
48                         for (i=0; i<globaldata->Estimators.size(); i++) {
49                                 if (validCalculator->isValidCalculator("vennshared", globaldata->Estimators[i]) == true) { 
50                                         if (globaldata->Estimators[i] == "sharedsobs") { 
51                                                 vennCalculators.push_back(new SharedSobsCS());
52                                         }else if (globaldata->Estimators[i] == "sharedchao") { 
53                                                 vennCalculators.push_back(new SharedChao1());
54                                         }else if (globaldata->Estimators[i] == "sharedace") { 
55                                                 vennCalculators.push_back(new SharedAce());
56                                         }
57                                 }
58                         }
59                 }
60                 
61                 venn = new Venn();
62                 
63                 //reset calc for next command
64                 globaldata->setCalc("");
65
66                 
67         }
68         catch(exception& e) {
69                 cout << "Standard Error: " << e.what() << " has occurred in the VennCommand class Function VennCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
70                 exit(1);
71         }
72         catch(...) {
73                 cout << "An unknown error has occurred in the VennCommand class function VennCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
74                 exit(1);
75         }       
76 }
77 //**********************************************************************************************************************
78
79 VennCommand::~VennCommand(){
80         delete input;
81         delete read;
82         delete venn;
83 }
84
85 //**********************************************************************************************************************
86
87 int VennCommand::execute(){
88         try {
89                 int count = 1;  
90                 
91                 //if the users entered no valid calculators don't execute command
92                 if (vennCalculators.size() == 0) { return 0; }
93                 
94                 if (format == "sharedfile") {
95                         //you have groups
96                         read = new ReadPhilFile(globaldata->inputFileName);     
97                         read->read(&*globaldata); 
98                         
99                         input = globaldata->ginput;
100                         order = input->getSharedOrderVector();
101                 }else if (format == "shared") {
102                         //you are using a list and a groupfile
103                         read = new ReadPhilFile(globaldata->inputFileName);     
104                         read->read(&*globaldata); 
105                 
106                         input = globaldata->ginput;
107                         SharedList = globaldata->gSharedList;
108                         order = SharedList->getSharedOrderVector();
109                 }else if (format == "list") {
110                         //you are using just a list file and have only one group
111                         read = new ReadPhilFile(globaldata->inputFileName);     
112                         read->read(&*globaldata); 
113                 
114                         ordersingle = globaldata->gorder;
115                         input = globaldata->ginput;
116                 }
117
118                 
119                 if (format != "list") { 
120                         
121                         setGroups();
122                         
123                         while(order != NULL){
124                 
125                                 if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(order->getLabel()) == 1){                       
126         
127                                         cout << order->getLabel() << '\t' << count << endl;
128                                         venn->getPic(order, vennCalculators);
129
130                                 }
131                                                 
132                                 //get next line to process
133                                 if (format == "sharedfile") {
134                                         order = input->getSharedOrderVector();
135                                 }else {
136                                         //you are using a list and a groupfile
137                                         SharedList = input->getSharedListVector(); //get new list vector to process
138                                         if (SharedList != NULL) {
139                                                 order = SharedList->getSharedOrderVector(); //gets new order vector with group info.
140                                         }else {
141                                                 break;
142                                         }
143                                 }
144                                 count++;
145                         }
146                         
147                         //reset groups parameter
148                         globaldata->Groups.clear();  globaldata->setGroups("");
149                         
150                 }else{
151                         while(ordersingle != NULL){
152                 
153                                 if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(ordersingle->getLabel()) == 1){                 
154         
155                                         cout << ordersingle->getLabel() << '\t' << count << endl;
156                                         venn->getPic(ordersingle, vennCalculators);
157                                         
158                                 }
159                                 
160                                 ordersingle = (input->getOrderVector());
161                                 count++;
162                         }
163                 }
164                 
165                 return 0;
166         }
167         catch(exception& e) {
168                 cout << "Standard Error: " << e.what() << " has occurred in the VennCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
169                 exit(1);
170         }
171         catch(...) {
172                 cout << "An unknown error has occurred in the VennCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
173                 exit(1);
174         }               
175 }
176
177 //**********************************************************************************************************************
178 void VennCommand::setGroups() {
179         try {
180                 //if the user has not entered specific groups to analyze then do them all
181                 if (globaldata->Groups.size() != 0) {
182                         if (globaldata->Groups[0] != "all") {
183                                 //check that groups are valid
184                                 for (int i = 0; i < globaldata->Groups.size(); i++) {
185                                         if (globaldata->gGroupmap->isValidGroup(globaldata->Groups[i]) != true) {
186                                                 cout << globaldata->Groups[i] << " is not a valid group, and will be disregarded." << endl;
187                                                 // erase the invalid group from globaldata->Groups
188                                                 globaldata->Groups.erase(globaldata->Groups.begin()+i);
189                                         }
190                                 }
191                         
192                                 //if the user only entered invalid groups
193                                 if (globaldata->Groups.size() == 0) { 
194                                         if (globaldata->gGroupmap->namesOfGroups.size() > 4) {
195                                                 cout << "When using the groups parameter you must have at least 1 valid group. I will run the command using the first four groups in your groupfile." << endl; 
196                                                 for (int i = 0; i < 4; i++) {
197                                                         globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
198                                                 }
199                                         }else {
200                                                 cout << "When using the groups parameter you must have at least 1 valid group. I will run the command using all the groups in your groupfile." << endl; 
201                                                 for (int i = 0; i < globaldata->gGroupmap->namesOfGroups.size(); i++) {
202                                                         globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
203                                                 }
204                                         }
205
206                                 }
207                         }else{//user has enter "all" and wants the default groups
208                                 globaldata->Groups.clear();
209                                 for (int i = 0; i < globaldata->gGroupmap->namesOfGroups.size(); i++) {
210                                         globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
211                                 }
212                                 globaldata->setGroups("");
213                         }
214                 }else {
215                         for (int i = 0; i < globaldata->gGroupmap->namesOfGroups.size(); i++) {
216                                 globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
217                         }
218                 }
219                 
220                 
221                 //check to make sure their are only 3 groups
222                 if (globaldata->Groups.size() > 4) {
223                         cout << "You may only use up to 4 groups at a time with this command.  I will choose the first four and disregard the rest." << endl;
224                         for (int i = 4; i < globaldata->Groups.size(); i++) {
225                                 globaldata->Groups.erase(globaldata->Groups.begin()+i);
226                         }
227                 }
228                 
229         }
230         catch(exception& e) {
231                 cout << "Standard Error: " << e.what() << " has occurred in the VennCommand class Function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
232                 exit(1);
233         }
234         catch(...) {
235                 cout << "An unknown error has occurred in the VennCommand class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
236                 exit(1);
237         }               
238
239 }
240 /***********************************************************/
241