]> git.donarmstrong.com Git - mothur.git/blob - venncommand.cpp
venn command with up to 4 groups
[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
12 //**********************************************************************************************************************
13
14 VennCommand::VennCommand(){
15         try {
16                 globaldata = GlobalData::getInstance();
17                 venn = new Venn();
18                 format = globaldata->getFormat();
19                 
20         }
21         catch(exception& e) {
22                 cout << "Standard Error: " << e.what() << " has occurred in the VennCommand class Function VennCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
23                 exit(1);
24         }
25         catch(...) {
26                 cout << "An unknown error has occurred in the VennCommand class function VennCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
27                 exit(1);
28         }       
29 }
30 //**********************************************************************************************************************
31
32 VennCommand::~VennCommand(){
33         delete input;
34         delete read;
35         delete venn;
36 }
37
38 //**********************************************************************************************************************
39
40 int VennCommand::execute(){
41         try {
42                 int count = 1;  
43                 
44                 if (format == "sharedfile") {
45                         //you have groups
46                         read = new ReadPhilFile(globaldata->inputFileName);     
47                         read->read(&*globaldata); 
48                         
49                         input = globaldata->ginput;
50                         order = input->getSharedOrderVector();
51                 }else if (format == "shared") {
52                         //you are using a list and a groupfile
53                         read = new ReadPhilFile(globaldata->inputFileName);     
54                         read->read(&*globaldata); 
55                 
56                         input = globaldata->ginput;
57                         SharedList = globaldata->gSharedList;
58                         order = SharedList->getSharedOrderVector();
59                 }else if (format == "list") {
60                         //you are using just a list file and have only one group
61                         read = new ReadPhilFile(globaldata->inputFileName);     
62                         read->read(&*globaldata); 
63                 
64                         ordersingle = globaldata->gorder;
65                         input = globaldata->ginput;
66                 }
67
68                 
69                 if (format != "list") { 
70                         
71                         setGroups();
72                         
73                         while(order != NULL){
74                 
75                                 if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(order->getLabel()) == 1){                       
76         
77                                         cout << order->getLabel() << '\t' << count << endl;
78                                         venn->getPic(order);
79
80                                 }
81                                                 
82                                 //get next line to process
83                                 if (format == "sharedfile") {
84                                         order = input->getSharedOrderVector();
85                                 }else {
86                                         //you are using a list and a groupfile
87                                         SharedList = input->getSharedListVector(); //get new list vector to process
88                                         if (SharedList != NULL) {
89                                                 order = SharedList->getSharedOrderVector(); //gets new order vector with group info.
90                                         }else {
91                                                 break;
92                                         }
93                                 }
94                                 count++;
95                         }
96                         
97                         //reset groups parameter
98                         globaldata->Groups.clear();  globaldata->setGroups("");
99                         
100                 }else{
101                         while(ordersingle != NULL){
102                 
103                                 if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(ordersingle->getLabel()) == 1){                 
104         
105                                         cout << ordersingle->getLabel() << '\t' << count << endl;
106                                         venn->getPic(ordersingle);
107                                         
108                                 }
109                                 
110                                 ordersingle = (input->getOrderVector());
111                                 count++;
112                         }
113                 }
114                 
115                 return 0;
116         }
117         catch(exception& e) {
118                 cout << "Standard Error: " << e.what() << " has occurred in the VennCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
119                 exit(1);
120         }
121         catch(...) {
122                 cout << "An unknown error has occurred in the VennCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
123                 exit(1);
124         }               
125 }
126
127 //**********************************************************************************************************************
128 void VennCommand::setGroups() {
129         try {
130                 //if the user has not entered specific groups to analyze then do them all
131                 if (globaldata->Groups.size() != 0) {
132                         if (globaldata->Groups[0] != "all") {
133                                 //check that groups are valid
134                                 for (int i = 0; i < globaldata->Groups.size(); i++) {
135                                         if (globaldata->gGroupmap->isValidGroup(globaldata->Groups[i]) != true) {
136                                                 cout << globaldata->Groups[i] << " is not a valid group, and will be disregarded." << endl;
137                                                 // erase the invalid group from globaldata->Groups
138                                                 globaldata->Groups.erase(globaldata->Groups.begin()+i);
139                                         }
140                                 }
141                         
142                                 //if the user only entered invalid groups
143                                 if (globaldata->Groups.size() == 0) { 
144                                         if (globaldata->gGroupmap->namesOfGroups.size() > 4) {
145                                                 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; 
146                                                 for (int i = 0; i < 4; i++) {
147                                                         globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
148                                                 }
149                                         }else {
150                                                 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; 
151                                                 for (int i = 0; i < globaldata->gGroupmap->namesOfGroups.size(); i++) {
152                                                         globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
153                                                 }
154                                         }
155
156                                 }
157                         }else{//user has enter "all" and wants the default groups
158                                 globaldata->Groups.clear();
159                                 for (int i = 0; i < globaldata->gGroupmap->namesOfGroups.size(); i++) {
160                                         globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
161                                 }
162                                 globaldata->setGroups("");
163                         }
164                 }else {
165                         for (int i = 0; i < globaldata->gGroupmap->namesOfGroups.size(); i++) {
166                                 globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
167                         }
168                 }
169                 
170                 
171                 //check to make sure their are only 3 groups
172                 if (globaldata->Groups.size() > 4) {
173                         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;
174                         for (int i = 4; i < globaldata->Groups.size(); i++) {
175                                 globaldata->Groups.erase(globaldata->Groups.begin()+i);
176                         }
177                 }
178                 
179         }
180         catch(exception& e) {
181                 cout << "Standard Error: " << e.what() << " has occurred in the VennCommand class Function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
182                 exit(1);
183         }
184         catch(...) {
185                 cout << "An unknown error has occurred in the VennCommand class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
186                 exit(1);
187         }               
188
189 }
190 /***********************************************************/
191