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