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