]> git.donarmstrong.com Git - mothur.git/blob - rarefactsharedcommand.cpp
added checks for ^C to quit command instead of program
[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                 
20                 abort = false;
21                 allLines = 1;
22                 labels.clear();
23                 Estimators.clear();
24                 Groups.clear();
25                                 
26                 //allow user to run help
27                 if(option == "help") { validCalculator = new ValidCalculators(); help(); abort = true; }
28                 
29                 else {
30                         //valid paramters for this command
31                         string Array[] =  {"iters","label","calc","groups", "jumble","outputdir","inputdir"};
32                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
33                         
34                         OptionParser parser(option);
35                         map<string,string> parameters = parser.getParameters();
36                         
37                         ValidParameters validParameter;
38                         
39                         //check to make sure all parameters are valid for command
40                         for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
41                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
42                         }
43                         
44                         //make sure the user has already run the read.otu command
45                         if (globaldata->getSharedFile() == "") {
46                                 if (globaldata->getListFile() == "") { m->mothurOut("You must read a list and a group, or a shared before you can use the collect.shared command."); m->mothurOutEndLine(); abort = true; }
47                                 else if (globaldata->getGroupFile() == "") { m->mothurOut("You must read a list and a group, or a shared before you can use the collect.shared command."); m->mothurOutEndLine(); abort = true; }
48                         }
49                         
50                         //if the user changes the output directory command factory will send this info to us in the output parameter 
51                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
52                                 outputDir = ""; 
53                                 outputDir += hasPath(globaldata->inputFileName); //if user entered a file with a path then preserve it  
54                         }
55
56                         
57                         //check for optional parameter and set defaults
58                         // ...at some point should added some additional type checking...
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                         //if the user has not specified any labels use the ones from read.otu
67                         if(label == "") {  
68                                 allLines = globaldata->allLines; 
69                                 labels = globaldata->labels; 
70                         }
71                                 
72                         calc = validParameter.validFile(parameters, "calc", false);                     
73                         if (calc == "not found") { calc = "sharedobserved";  }
74                         else { 
75                                  if (calc == "default")  {  calc = "sharedobserved";  }
76                         }
77                         splitAtDash(calc, Estimators);
78                         
79                         groups = validParameter.validFile(parameters, "groups", false);                 
80                         if (groups == "not found") { groups = ""; }
81                         else { 
82                                 splitAtDash(groups, Groups);
83                         }
84                         globaldata->Groups = Groups;
85                         
86                         string temp;
87                         temp = validParameter.validFile(parameters, "iters", false);                    if (temp == "not found") { temp = "1000"; }
88                         convert(temp, nIters); 
89                         
90                         temp = validParameter.validFile(parameters, "jumble", false);                   if (temp == "not found") { temp = "T"; }
91                         if (isTrue(temp)) { jumble = true; }
92                         else { jumble = false; }
93                         globaldata->jumble = jumble;
94                         
95                         if (abort == false) {
96                         
97                                 string fileNameRoot = outputDir + getRootName(getSimpleName(globaldata->inputFileName));
98 //                              format = globaldata->getFormat();
99
100                                 
101                                 validCalculator = new ValidCalculators();
102                                 
103                                 for (int i=0; i<Estimators.size(); i++) {
104                                         if (validCalculator->isValidCalculator("sharedrarefaction", Estimators[i]) == true) { 
105                                                 if (Estimators[i] == "sharedobserved") { 
106                                                         rDisplays.push_back(new RareDisplay(new SharedSobs(), new SharedThreeColumnFile(fileNameRoot+"shared.rarefaction", "")));
107                                                         outputNames.push_back(fileNameRoot+"shared.rarefaction");
108                                                 }else if (Estimators[i] == "sharednseqs") { 
109                                                         rDisplays.push_back(new RareDisplay(new SharedNSeqs(), new SharedThreeColumnFile(fileNameRoot+"shared.r_nseqs", "")));
110                                                         outputNames.push_back(fileNameRoot+"shared.r_nseqs");
111                                                 }
112                                         }
113                                 }
114                         }
115                                 
116                 }
117
118         }
119         catch(exception& e) {
120                 m->errorOut(e, "RareFactSharedCommand", "RareFactSharedCommand");
121                 exit(1);
122         }
123 }
124
125 //**********************************************************************************************************************
126
127 void RareFactSharedCommand::help(){
128         try {
129                 m->mothurOut("The rarefaction.shared command can only be executed after a successful read.otu command.\n");
130                 m->mothurOut("The rarefaction.shared command parameters are label, iters, groups, jumble and calc.  No parameters are required.\n");
131                 m->mothurOut("The rarefaction command should be in the following format: \n");
132                 m->mothurOut("rarefaction.shared(label=yourLabel, iters=yourIters, calc=yourEstimators, jumble=yourJumble, groups=yourGroups).\n");
133                 m->mothurOut("Example rarefaction.shared(label=unique-0.01-0.03,  iters=10000, groups=B-C, jumble=T, calc=sharedobserved).\n");
134                 m->mothurOut("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");
135                 m->mothurOut("The default value for groups is all the groups in your groupfile, and jumble is true.\n");
136                 validCalculator->printCalc("sharedrarefaction", cout);
137                 m->mothurOut("The label parameter is used to analyze specific labels in your input.\n");
138                 m->mothurOut("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");
139                 m->mothurOut("Note: No spaces between parameter labels (i.e. freq), '=' and parameters (i.e.yourFreq).\n\n");
140         }
141         catch(exception& e) {
142                 m->errorOut(e, "RareFactSharedCommand", "help");
143                 exit(1);
144         }
145 }
146
147 //**********************************************************************************************************************
148
149 RareFactSharedCommand::~RareFactSharedCommand(){
150         if (abort == false) {
151                 delete input;   globaldata->ginput = NULL;
152                 delete read;
153                 delete validCalculator;
154         }
155 }
156
157 //**********************************************************************************************************************
158
159 int RareFactSharedCommand::execute(){
160         try {
161         
162                 if (abort == true) { return 0; }
163                 
164                 //if the users entered no valid calculators don't execute command
165                 if (rDisplays.size() == 0) { return 0; }
166
167                 read = new ReadOTUFile(globaldata->inputFileName);      
168                 read->read(&*globaldata); 
169                         
170                 input = globaldata->ginput;
171                 lookup = input->getSharedRAbundVectors();
172                 string lastLabel = lookup[0]->getLabel();
173                 
174                 if (m->control_pressed) { 
175                         globaldata->Groups.clear(); 
176                         for(int i=0;i<rDisplays.size();i++){    delete rDisplays[i];    }
177                         for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str());         }
178                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
179                         return 0;
180                 }
181
182
183                 if (lookup.size() < 2) { 
184                         m->mothurOut("I cannot run the command without at least 2 valid groups."); 
185                         for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
186                         return 0;
187                 }
188                 
189                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
190                 set<string> processedLabels;
191                 set<string> userLabels = labels;
192         
193                 //as long as you are not at the end of the file or done wih the lines you want
194                 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
195                         if (m->control_pressed) { 
196                                 globaldata->Groups.clear(); 
197                                 for(int i=0;i<rDisplays.size();i++){    delete rDisplays[i];    }
198                                 for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str());         }
199                                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
200                                 return 0;
201                         }
202                         
203                         if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
204                                 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
205                                 rCurve = new Rarefact(lookup, rDisplays);
206                                 rCurve->getSharedCurve(freq, nIters);
207                                 delete rCurve;
208                         
209                                 processedLabels.insert(lookup[0]->getLabel());
210                                 userLabels.erase(lookup[0]->getLabel());
211                         }
212                         
213                         if ((anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
214                                         string saveLabel = lookup[0]->getLabel();
215                         
216                                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
217                                         lookup = input->getSharedRAbundVectors(lastLabel);
218
219                                         m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
220                                         rCurve = new Rarefact(lookup, rDisplays);
221                                         rCurve->getSharedCurve(freq, nIters);
222                                         delete rCurve;
223
224                                         processedLabels.insert(lookup[0]->getLabel());
225                                         userLabels.erase(lookup[0]->getLabel());
226                                         
227                                         //restore real lastlabel to save below
228                                         lookup[0]->setLabel(saveLabel);
229                         }
230                                 
231                         
232                         lastLabel = lookup[0]->getLabel();
233                         
234                         //get next line to process
235                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
236                         lookup = input->getSharedRAbundVectors();
237                 }
238                 
239                 if (m->control_pressed) { 
240                                 globaldata->Groups.clear(); 
241                                 for(int i=0;i<rDisplays.size();i++){    delete rDisplays[i];    }
242                                 for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str());         }
243                                 return 0;
244                 }
245                 
246                 //output error messages about any remaining user labels
247                 set<string>::iterator it;
248                 bool needToRun = false;
249                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
250                         m->mothurOut("Your file does not include the label " + *it); 
251                         if (processedLabels.count(lastLabel) != 1) {
252                                 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
253                                 needToRun = true;
254                         }else {
255                                 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
256                         }
257                 }
258                 
259                 if (m->control_pressed) { 
260                                 globaldata->Groups.clear(); 
261                                 for(int i=0;i<rDisplays.size();i++){    delete rDisplays[i];    }
262                                 for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str());         }
263                                 return 0;
264                 }
265                 
266                 //run last label if you need to
267                 if (needToRun == true)  {
268                         for (int i = 0; i < lookup.size(); i++) {  if (lookup[i] != NULL) {     delete lookup[i]; }  } 
269                         lookup = input->getSharedRAbundVectors(lastLabel);
270
271                         m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
272                         rCurve = new Rarefact(lookup, rDisplays);
273                         rCurve->getSharedCurve(freq, nIters);
274                         delete rCurve;
275                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
276                 }
277                 
278                 for(int i=0;i<rDisplays.size();i++){    delete rDisplays[i];    }       
279                 
280                 //reset groups parameter
281                 globaldata->Groups.clear();  
282                 
283                 if (m->control_pressed) { 
284                                 for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str());         }
285                                 return 0;
286                 }
287                 
288                 m->mothurOutEndLine();
289                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
290                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
291                 m->mothurOutEndLine();
292
293                 return 0;
294         }
295         catch(exception& e) {
296                 m->errorOut(e, "RareFactSharedCommand", "execute");
297                 exit(1);
298         }
299 }
300
301
302 //**********************************************************************************************************************