]> git.donarmstrong.com Git - mothur.git/blob - rarefactsharedcommand.cpp
merged pat's trim seqs edits with sarah's major overhaul of global data; also added...
[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 #include "sharednseqs.h"
13
14 //**********************************************************************************************************************
15
16 RareFactSharedCommand::RareFactSharedCommand(string option){
17         try {
18                 globaldata = GlobalData::getInstance();
19                 abort = false;
20                 allLines = 1;
21                 lines.clear();
22                 labels.clear();
23                 Estimators.clear();
24                 
25                 //allow user to run help
26                 if(option == "help") { validCalculator = new ValidCalculators(); help(); abort = true; }
27                 
28                 else {
29                         //valid paramters for this command
30                         string Array[] =  {"iters","line","label","calc","groups"};
31                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
32                         
33                         OptionParser parser(option);
34                         map<string,string> parameters = parser.getParameters();
35                         
36                         ValidParameters validParameter;
37                         
38                         //check to make sure all parameters are valid for command
39                         for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
40                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
41                         }
42                         
43                         //make sure the user has already run the read.otu command
44                         if (globaldata->getSharedFile() == "") {
45                                 if (globaldata->getListFile() == "") { cout << "You must read a list and a group, or a shared before you can use the collect.shared command." << endl; abort = true; }
46                                 else if (globaldata->getGroupFile() == "") { cout << "You must read a list and a group, or a shared before you can use the collect.shared command." << endl; abort = true; }
47                         }
48
49                         
50                         //check for optional parameter and set defaults
51                         // ...at some point should added some additional type checking...
52                         line = validParameter.validFile(parameters, "line", false);                             
53                         if (line == "not found") { line = "";  }
54                         else { 
55                                 if(line != "all") {  splitAtDash(line, lines);  allLines = 0;  }
56                                 else { allLines = 1;  }
57                         }
58                         
59                         label = validParameter.validFile(parameters, "label", false);                   
60                         if (label == "not found") { label = ""; }
61                         else { 
62                                 if(label != "all") {  splitAtDash(label, labels);  allLines = 0;  }
63                                 else { allLines = 1;  }
64                         }
65                         
66                         //make sure user did not use both the line and label parameters
67                         if ((line != "") && (label != "")) { cout << "You cannot use both the line and label parameters at the same time. " << endl; abort = true; }
68                         //if the user has not specified any line or labels use the ones from read.otu
69                         else if((line == "") && (label == "")) {  
70                                 allLines = globaldata->allLines; 
71                                 labels = globaldata->labels; 
72                                 lines = globaldata->lines;
73                         }
74                                 
75                         calc = validParameter.validFile(parameters, "calc", false);                     
76                         if (calc == "not found") { calc = "sharedobserved";  }
77                         else { 
78                                  if (calc == "default")  {  calc = "sharedobserved";  }
79                         }
80                         splitAtDash(calc, Estimators);
81                         
82                         groups = validParameter.validFile(parameters, "groups", false);                 
83                         if (groups == "not found") { groups = ""; }
84                         else { 
85                                 splitAtDash(groups, Groups);
86                                 globaldata->Groups = Groups;
87                         }
88                         
89                         string temp;
90                         temp = validParameter.validFile(parameters, "iters", false);                    if (temp == "not found") { temp = "1000"; }
91                         convert(temp, nIters); 
92                         
93                         if (abort == false) {
94                         
95                                 string fileNameRoot = getRootName(globaldata->inputFileName);
96 //                              format = globaldata->getFormat();
97
98                                 
99                                 validCalculator = new ValidCalculators();
100                                 
101                                 for (int i=0; i<Estimators.size(); i++) {
102                                         if (validCalculator->isValidCalculator("sharedrarefaction", Estimators[i]) == true) { 
103                                                 if (Estimators[i] == "sharedobserved") { 
104                                                         rDisplays.push_back(new RareDisplay(new SharedSobs(), new SharedThreeColumnFile(fileNameRoot+"shared.rarefaction", "")));
105                                                 }else if (Estimators[i] == "sharednseqs") { 
106                                                         rDisplays.push_back(new RareDisplay(new SharedNSeqs(), new SharedThreeColumnFile(fileNameRoot+"shared.r_nseqs", "")));
107                                                 }
108                                         }
109                                 }
110                         }
111                                 
112                 }
113
114         }
115         catch(exception& e) {
116                 cout << "Standard Error: " << e.what() << " has occurred in the RareFactSharedCommand class Function RareFactSharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
117                 exit(1);
118         }
119         catch(...) {
120                 cout << "An unknown error has occurred in the RareFactSharedCommand class function RareFactSharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
121                 exit(1);
122         }       
123                         
124 }
125
126 //**********************************************************************************************************************
127
128 void RareFactSharedCommand::help(){
129         try {
130                 cout << "The rarefaction.shared command can only be executed after a successful read.otu command." << "\n";
131                 cout << "The rarefaction.shared command parameters are label, line, iters, groups and calc.  No parameters are required, but you may not use " << "\n";
132                 cout << "both the line and label parameters at the same time. The rarefaction command should be in the following format: " << "\n";
133                 cout << "rarefaction.shared(label=yourLabel, line=yourLines, iters=yourIters, calc=yourEstimators, groups=yourGroups)." << "\n";
134                 cout << "Example rarefaction.shared(label=unique-.01-.03, line=0-5-10, iters=10000, groups=B-C, calc=sharedobserved)." << "\n";
135                 cout << "The default values for iters is 1000, freq is 100, and calc is sharedobserved which calculates the shared rarefaction curve for the observed richness." << "\n";
136                 cout << "The default value for groups is all the groups in your groupfile." << "\n";
137                 validCalculator->printCalc("sharedrarefaction", cout);
138                 cout << "The label and line parameters are used to analyze specific lines in your input." << "\n";
139                 cout << "The groups parameter allows you to specify which of the groups in your groupfile you would like analyzed.  You must enter at least 2 valid groups." << "\n";
140                 cout << "Note: No spaces between parameter labels (i.e. freq), '=' and parameters (i.e.yourFreq)." << "\n" << "\n";
141         }
142         catch(exception& e) {
143                 cout << "Standard Error: " << e.what() << " has occurred in the RareFactSharedCommand class Function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
144                 exit(1);
145         }
146         catch(...) {
147                 cout << "An unknown error has occurred in the RareFactSharedCommand class function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
148                 exit(1);
149         }       
150 }
151
152 //**********************************************************************************************************************
153
154 RareFactSharedCommand::~RareFactSharedCommand(){
155         delete input;
156         delete rCurve;
157         delete read;
158         delete validCalculator;
159 }
160
161 //**********************************************************************************************************************
162
163 int RareFactSharedCommand::execute(){
164         try {
165         
166                 if (abort == true) { return 0; }
167                 
168                 int count = 1;
169                 
170                 //if the users entered no valid calculators don't execute command
171                 if (rDisplays.size() == 0) { return 0; }
172
173                 read = new ReadOTUFile(globaldata->inputFileName);      
174                 read->read(&*globaldata); 
175                         
176                 input = globaldata->ginput;
177                 lookup = input->getSharedRAbundVectors();
178                 vector<SharedRAbundVector*> lastLookup = lookup;
179                 
180                 if (lookup.size() < 2) { 
181                         cout << "I cannot run the command without at least 2 valid groups."; 
182                         for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
183                         return 0;
184                 }
185                 
186                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
187                 set<string> processedLabels;
188                 set<string> userLabels = labels;
189                 set<int> userLines = lines;
190         
191                 //as long as you are not at the end of the file or done wih the lines you want
192                 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
193                         
194                         if(allLines == 1 || lines.count(count) == 1 || labels.count(lookup[0]->getLabel()) == 1){
195                                 
196                                 rCurve = new Rarefact(lookup, rDisplays);
197                                 rCurve->getSharedCurve(freq, nIters);
198                                 delete rCurve;
199                         
200                                 cout << lookup[0]->getLabel() << '\t' << count << endl;
201                                 processedLabels.insert(lookup[0]->getLabel());
202                                 userLabels.erase(lookup[0]->getLabel());
203                                 userLines.erase(count);
204                         }
205                         
206                         if ((anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLookup[0]->getLabel()) != 1)) {
207                                         cout << lastLookup[0]->getLabel() << '\t' << count << endl;
208                                         rCurve = new Rarefact(lastLookup, rDisplays);
209                                         rCurve->getSharedCurve(freq, nIters);
210                                         delete rCurve;
211
212                                         processedLabels.insert(lastLookup[0]->getLabel());
213                                         userLabels.erase(lastLookup[0]->getLabel());
214                         }
215                                 
216                         //prevent memory leak
217                         if (count != 1) { for (int i = 0; i < lastLookup.size(); i++) {  delete lastLookup[i];  } }
218                         lastLookup = lookup;
219                         
220                         //get next line to process
221                         lookup = input->getSharedRAbundVectors();
222                         count++;
223                 }
224                 
225                 //output error messages about any remaining user labels
226                 set<string>::iterator it;
227                 bool needToRun = false;
228                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
229                         cout << "Your file does not include the label "<< *it; 
230                         if (processedLabels.count(lastLookup[0]->getLabel()) != 1) {
231                                 cout << ". I will use " << lastLookup[0]->getLabel() << "." << endl;
232                                 needToRun = true;
233                         }else {
234                                 cout << ". Please refer to " << lastLookup[0]->getLabel() << "." << endl;
235                         }
236                 }
237                 
238                 //run last line if you need to
239                 if (needToRun == true)  {
240                         cout << lastLookup[0]->getLabel() << '\t' << count << endl;
241                         rCurve = new Rarefact(lastLookup, rDisplays);
242                         rCurve->getSharedCurve(freq, nIters);
243                         delete rCurve;
244                 }
245                 
246                 for (int i = 0; i < lastLookup.size(); i++) {  delete lastLookup[i];  }
247
248                 for(int i=0;i<rDisplays.size();i++){    delete rDisplays[i];    }       
249                 
250                 //reset groups parameter
251                 globaldata->Groups.clear();  
252
253                 return 0;
254         }
255         catch(exception& e) {
256                 cout << "Standard Error: " << e.what() << " has occurred in the RareFactSharedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
257                 exit(1);
258         }
259         catch(...) {
260                 cout << "An unknown error has occurred in the RareFactSharedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
261                 exit(1);
262         }       
263 }
264
265
266 //**********************************************************************************************************************