]> git.donarmstrong.com Git - mothur.git/blob - rarefactsharedcommand.cpp
added ability for user to select which groups to analyze with the collect.shared...
[mothur.git] / rarefactsharedcommand.cpp
1 /*
2  *  rarefactsharedcommand.cpp
3  *  Dotur
4  *
5  *  Created by Sarah Westcott on 1/6/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "rarefactsharedcommand.h"
11 #include "sharedsobs.h"
12
13 //**********************************************************************************************************************
14
15 RareFactSharedCommand::RareFactSharedCommand(){
16         try {
17                 globaldata = GlobalData::getInstance();
18                 string fileNameRoot;
19                 fileNameRoot = getRootName(globaldata->inputFileName);
20                 format = globaldata->getFormat();
21                 validCalculator = new ValidCalculators();
22                 
23                 setGroups();
24                                 
25                 int i;
26                 for (i=0; i<globaldata->Estimators.size(); i++) {
27                         if (validCalculator->isValidCalculator("sharedrarefaction", globaldata->Estimators[i]) == true) { 
28                                 if (globaldata->Estimators[i] == "sharedobserved") { 
29                                         rDisplays.push_back(new RareDisplay(new SharedSobs(), new SharedThreeColumnFile(fileNameRoot+"shared.rarefaction", "")));
30                                 }
31                         }
32                 }
33                 
34                 //reset calc for next command
35                 globaldata->setCalc("");
36
37         }
38         catch(exception& e) {
39                 cout << "Standard Error: " << e.what() << " has occurred in the RareFactSharedCommand class Function RareFactSharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
40                 exit(1);
41         }
42         catch(...) {
43                 cout << "An unknown error has occurred in the RareFactSharedCommand class function RareFactSharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
44                 exit(1);
45         }       
46                         
47 }
48
49 //**********************************************************************************************************************
50
51 RareFactSharedCommand::~RareFactSharedCommand(){
52         delete order;
53         delete input;
54         delete rCurve;
55         delete read;
56 }
57
58 //**********************************************************************************************************************
59
60 int RareFactSharedCommand::execute(){
61         try {
62                 int count = 1;
63                 
64                 //if the users entered no valid calculators don't execute command
65                 if (rDisplays.size() == 0) { return 0; }
66
67                 if (format == "sharedfile") {
68                         read = new ReadPhilFile(globaldata->inputFileName);     
69                         read->read(&*globaldata); 
70                         
71                         input = globaldata->ginput;
72                         order = input->getSharedOrderVector();
73                 }else {
74                         //you are using a list and a groupfile
75                         read = new ReadPhilFile(globaldata->inputFileName);     
76                         read->read(&*globaldata); 
77                 
78                         input = globaldata->ginput;
79                         SharedList = globaldata->gSharedList;
80                         order = SharedList->getSharedOrderVector();
81                 }
82                 
83                 while(order != NULL){
84                 
85                         if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(order->getLabel()) == 1){
86                                 //create collectors curve
87                                 rCurve = new Rarefact(order, rDisplays);
88                                 convert(globaldata->getFreq(), freq);
89                                 convert(globaldata->getIters(), nIters);
90                                 rCurve->getSharedCurve(freq, nIters);
91                         
92                                 delete rCurve;
93                         
94                                 cout << order->getLabel() << '\t' << count << endl;
95                         }
96                         
97                         //get next line to process
98                         if (format == "sharedfile") {
99                                 order = input->getSharedOrderVector();
100                         }else {
101                                 //you are using a list and a groupfile
102                                 SharedList = input->getSharedListVector(); //get new list vector to process
103                                 if (SharedList != NULL) {
104                                         order = SharedList->getSharedOrderVector(); //gets new order vector with group info.
105                                 }else {
106                                         break;
107                                 }
108                         }
109                         
110                         count++;
111                 }
112         
113                 for(int i=0;i<rDisplays.size();i++){    delete rDisplays[i];    }       
114                 
115                 //reset groups parameter
116                 globaldata->Groups.clear();  globaldata->setGroups("");
117
118                 return 0;
119         }
120         catch(exception& e) {
121                 cout << "Standard Error: " << e.what() << " has occurred in the RareFactSharedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
122                 exit(1);
123         }
124         catch(...) {
125                 cout << "An unknown error has occurred in the RareFactSharedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
126                 exit(1);
127         }       
128 }
129
130
131 //**********************************************************************************************************************
132
133 void RareFactSharedCommand::setGroups() {
134         try {
135                 //if the user has not entered specific groups to analyze then do them all
136                 if (globaldata->Groups.size() != 0) {
137                         if (globaldata->Groups[0] != "all") {
138                                 //check that groups are valid
139                                 for (int i = 0; i < globaldata->Groups.size(); i++) {
140                                         if (globaldata->gGroupmap->isValidGroup(globaldata->Groups[i]) != true) {
141                                                 cout << globaldata->Groups[i] << " is not a valid group, and will be disregarded." << endl;
142                                                 // erase the invalid group from globaldata->Groups
143                                                 globaldata->Groups.erase(globaldata->Groups.begin()+i);
144                                         }
145                                 }
146                         
147                                 //if the user only entered invalid groups
148                                 if ((globaldata->Groups.size() == 0) || (globaldata->Groups.size() == 1)) { 
149                                         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; 
150                                         for (int i = 0; i < globaldata->gGroupmap->namesOfGroups.size(); i++) {
151                                                 globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
152                                         }
153                                 }
154                         }else{//user has enter "all" and wants the default groups
155                                 globaldata->Groups.clear();
156                                 for (int i = 0; i < globaldata->gGroupmap->namesOfGroups.size(); i++) {
157                                         globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
158                                 }
159                                 globaldata->setGroups("");
160                         }
161                 }else {
162                         for (int i = 0; i < globaldata->gGroupmap->namesOfGroups.size(); i++) {
163                                 globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
164                         }
165                 }
166                 
167         }
168         catch(exception& e) {
169                 cout << "Standard Error: " << e.what() << " has occurred in the RareFactSharedCommand class Function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
170                 exit(1);
171         }
172         catch(...) {
173                 cout << "An unknown error has occurred in the RareFactSharedCommand class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
174                 exit(1);
175         }               
176
177 }
178 /***********************************************************/
179