]> git.donarmstrong.com Git - mothur.git/blob - rarefactsharedcommand.cpp
1.10.0
[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","freq","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, "freq", false);                     if (temp == "not found") { temp = "100"; }
88                         convert(temp, freq); 
89                         
90                         temp = validParameter.validFile(parameters, "iters", false);                    if (temp == "not found") { temp = "1000"; }
91                         convert(temp, nIters); 
92                         
93                         temp = validParameter.validFile(parameters, "jumble", false);                   if (temp == "not found") { temp = "T"; }
94                         if (isTrue(temp)) { jumble = true; }
95                         else { jumble = false; }
96                         globaldata->jumble = jumble;
97                         
98                         if (abort == false) {
99                         
100                                 string fileNameRoot = outputDir + getRootName(getSimpleName(globaldata->inputFileName));
101 //                              format = globaldata->getFormat();
102
103                                 
104                                 validCalculator = new ValidCalculators();
105                                 
106                                 for (int i=0; i<Estimators.size(); i++) {
107                                         if (validCalculator->isValidCalculator("sharedrarefaction", Estimators[i]) == true) { 
108                                                 if (Estimators[i] == "sharedobserved") { 
109                                                         rDisplays.push_back(new RareDisplay(new SharedSobs(), new SharedThreeColumnFile(fileNameRoot+"shared.rarefaction", "")));
110                                                         outputNames.push_back(fileNameRoot+"shared.rarefaction");
111                                                 }else if (Estimators[i] == "sharednseqs") { 
112                                                         rDisplays.push_back(new RareDisplay(new SharedNSeqs(), new SharedThreeColumnFile(fileNameRoot+"shared.r_nseqs", "")));
113                                                         outputNames.push_back(fileNameRoot+"shared.r_nseqs");
114                                                 }
115                                         }
116                                 }
117                         }
118                                 
119                 }
120
121         }
122         catch(exception& e) {
123                 m->errorOut(e, "RareFactSharedCommand", "RareFactSharedCommand");
124                 exit(1);
125         }
126 }
127
128 //**********************************************************************************************************************
129
130 void RareFactSharedCommand::help(){
131         try {
132                 m->mothurOut("The rarefaction.shared command can only be executed after a successful read.otu command.\n");
133                 m->mothurOut("The rarefaction.shared command parameters are label, iters, groups, jumble and calc.  No parameters are required.\n");
134                 m->mothurOut("The rarefaction command should be in the following format: \n");
135                 m->mothurOut("rarefaction.shared(label=yourLabel, iters=yourIters, calc=yourEstimators, jumble=yourJumble, groups=yourGroups).\n");
136                 m->mothurOut("The freq parameter is used indicate when to output your data, by default it is set to 100. But you can set it to a percentage of the number of sequence. For example freq=0.10, means 10%. \n");
137                 m->mothurOut("Example rarefaction.shared(label=unique-0.01-0.03,  iters=10000, groups=B-C, jumble=T, calc=sharedobserved).\n");
138                 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");
139                 m->mothurOut("The default value for groups is all the groups in your groupfile, and jumble is true.\n");
140                 validCalculator->printCalc("sharedrarefaction", cout);
141                 m->mothurOut("The label parameter is used to analyze specific labels in your input.\n");
142                 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");
143                 m->mothurOut("Note: No spaces between parameter labels (i.e. freq), '=' and parameters (i.e.yourFreq).\n\n");
144         }
145         catch(exception& e) {
146                 m->errorOut(e, "RareFactSharedCommand", "help");
147                 exit(1);
148         }
149 }
150
151 //**********************************************************************************************************************
152
153 RareFactSharedCommand::~RareFactSharedCommand(){
154         if (abort == false) {
155                 delete input;   globaldata->ginput = NULL;
156                 delete read;
157                 delete validCalculator;
158         }
159 }
160
161 //**********************************************************************************************************************
162
163 int RareFactSharedCommand::execute(){
164         try {
165         
166                 if (abort == true) { return 0; }
167                 
168                 //if the users entered no valid calculators don't execute command
169                 if (rDisplays.size() == 0) { return 0; }
170
171                 read = new ReadOTUFile(globaldata->inputFileName);      
172                 read->read(&*globaldata); 
173                         
174                 input = globaldata->ginput;
175                 lookup = input->getSharedRAbundVectors();
176                 string lastLabel = lookup[0]->getLabel();
177                 
178                 if (m->control_pressed) { 
179                         globaldata->Groups.clear(); 
180                         for(int i=0;i<rDisplays.size();i++){    delete rDisplays[i];    }
181                         for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str());         }
182                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
183                         return 0;
184                 }
185
186
187                 if (lookup.size() < 2) { 
188                         m->mothurOut("I cannot run the command without at least 2 valid groups."); 
189                         for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
190                         return 0;
191                 }
192                 
193                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
194                 set<string> processedLabels;
195                 set<string> userLabels = labels;
196         
197                 //as long as you are not at the end of the file or done wih the lines you want
198                 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
199                         if (m->control_pressed) { 
200                                 globaldata->Groups.clear(); 
201                                 for(int i=0;i<rDisplays.size();i++){    delete rDisplays[i];    }
202                                 for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str());         }
203                                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
204                                 return 0;
205                         }
206                         
207                         if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
208                                 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
209                                 rCurve = new Rarefact(lookup, rDisplays);
210                                 rCurve->getSharedCurve(freq, nIters);
211                                 delete rCurve;
212                         
213                                 processedLabels.insert(lookup[0]->getLabel());
214                                 userLabels.erase(lookup[0]->getLabel());
215                         }
216                         
217                         if ((anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
218                                         string saveLabel = lookup[0]->getLabel();
219                         
220                                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
221                                         lookup = input->getSharedRAbundVectors(lastLabel);
222
223                                         m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
224                                         rCurve = new Rarefact(lookup, rDisplays);
225                                         rCurve->getSharedCurve(freq, nIters);
226                                         delete rCurve;
227
228                                         processedLabels.insert(lookup[0]->getLabel());
229                                         userLabels.erase(lookup[0]->getLabel());
230                                         
231                                         //restore real lastlabel to save below
232                                         lookup[0]->setLabel(saveLabel);
233                         }
234                                 
235                         
236                         lastLabel = lookup[0]->getLabel();
237                         
238                         //get next line to process
239                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
240                         lookup = input->getSharedRAbundVectors();
241                 }
242                 
243                 if (m->control_pressed) { 
244                                 globaldata->Groups.clear(); 
245                                 for(int i=0;i<rDisplays.size();i++){    delete rDisplays[i];    }
246                                 for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str());         }
247                                 return 0;
248                 }
249                 
250                 //output error messages about any remaining user labels
251                 set<string>::iterator it;
252                 bool needToRun = false;
253                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
254                         m->mothurOut("Your file does not include the label " + *it); 
255                         if (processedLabels.count(lastLabel) != 1) {
256                                 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
257                                 needToRun = true;
258                         }else {
259                                 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
260                         }
261                 }
262                 
263                 if (m->control_pressed) { 
264                                 globaldata->Groups.clear(); 
265                                 for(int i=0;i<rDisplays.size();i++){    delete rDisplays[i];    }
266                                 for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str());         }
267                                 return 0;
268                 }
269                 
270                 //run last label if you need to
271                 if (needToRun == true)  {
272                         for (int i = 0; i < lookup.size(); i++) {  if (lookup[i] != NULL) {     delete lookup[i]; }  } 
273                         lookup = input->getSharedRAbundVectors(lastLabel);
274
275                         m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
276                         rCurve = new Rarefact(lookup, rDisplays);
277                         rCurve->getSharedCurve(freq, nIters);
278                         delete rCurve;
279                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
280                 }
281                 
282                 for(int i=0;i<rDisplays.size();i++){    delete rDisplays[i];    }       
283                 
284                 //reset groups parameter
285                 globaldata->Groups.clear();  
286                 
287                 if (m->control_pressed) { 
288                                 for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str());         }
289                                 return 0;
290                 }
291                 
292                 m->mothurOutEndLine();
293                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
294                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
295                 m->mothurOutEndLine();
296
297                 return 0;
298         }
299         catch(exception& e) {
300                 m->errorOut(e, "RareFactSharedCommand", "execute");
301                 exit(1);
302         }
303 }
304
305
306 //**********************************************************************************************************************