]> git.donarmstrong.com Git - mothur.git/blob - getrabundcommand.cpp
Revert to previous commit
[mothur.git] / getrabundcommand.cpp
1 /*
2  *  getrabundcommand.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 6/2/09.
6  *  Copyright 2009 Schloss Lab Umass Amherst. All rights reserved.
7  *
8  */
9
10 #include "getrabundcommand.h"
11
12 //**********************************************************************************************************************
13 vector<string> GetRAbundCommand::setParameters(){       
14         try {
15                 CommandParameter plist("list", "InputTypes", "", "", "LRSS", "LRSS", "none",false,false); parameters.push_back(plist);
16                 CommandParameter psabund("sabund", "InputTypes", "", "", "LRSS", "LRSS", "none",false,false); parameters.push_back(psabund);            
17                 CommandParameter psorted("sorted", "Boolean", "", "T", "", "", "",false,false); parameters.push_back(psorted);
18                 CommandParameter plabel("label", "String", "", "", "", "", "",false,false); parameters.push_back(plabel);
19                 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
20                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
21                 
22                 vector<string> myArray;
23                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
24                 return myArray;
25         }
26         catch(exception& e) {
27                 m->errorOut(e, "GetRAbundCommand", "setParameters");
28                 exit(1);
29         }
30 }
31 //**********************************************************************************************************************
32 string GetRAbundCommand::getHelpString(){       
33         try {
34                 string helpString = "";
35                 helpString += "The get.rabund command parameters are list, sabund, label and sorted.  list or sabund parameters are required, unless you have valid current files.\n";
36                 helpString += "The label parameter allows you to select what distance levels you would like included in your .rabund file, and are separated by dashes.\n";
37                 helpString += "The sorted parameters allows you to print the rabund results sorted by abundance or not.  The default is sorted.\n";
38                 helpString += "The get.rabund command should be in the following format: get.rabund(label=yourLabels, sorted=yourSorted).\n";
39                 helpString += "Example get.rabund(sorted=F).\n";
40                 helpString += "The default value for label is all labels in your inputfile.\n";
41                 helpString += "The get.rabund command outputs a .rabund file containing the lines you selected.\n";
42                 helpString += "Note: No spaces between parameter labels (i.e. label), '=' and parameters (i.e.yourLabels).\n";
43                 return helpString;
44         }
45         catch(exception& e) {
46                 m->errorOut(e, "GetRAbundCommand", "getHelpString");
47                 exit(1);
48         }
49 }
50
51 //**********************************************************************************************************************
52 GetRAbundCommand::GetRAbundCommand(){   
53         try {
54                 abort = true; calledHelp = true; 
55                 setParameters();
56                 vector<string> tempOutNames;
57                 outputTypes["rabund"] = tempOutNames;
58         }
59         catch(exception& e) {
60                 m->errorOut(e, "GetRAbundCommand", "GetRAbundCommand");
61                 exit(1);
62         }
63 }
64 //**********************************************************************************************************************
65 GetRAbundCommand::GetRAbundCommand(string option)  {
66         try {
67                 abort = false; calledHelp = false;   
68                 allLines = 1;
69                 
70                 //allow user to run help
71                 if(option == "help") { help(); abort = true; calledHelp = true; }
72                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
73                 
74                 else {
75                         vector<string> myArray = setParameters();
76                         
77                         OptionParser parser(option);
78                         map<string,string> parameters = parser.getParameters();
79                         map<string,string>::iterator it;
80                         
81                         ValidParameters validParameter;
82                         
83                         //check to make sure all parameters are valid for command
84                         for (it = parameters.begin(); it != parameters.end(); it++) { 
85                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
86                         }
87                         
88                         //initialize outputTypes
89                         vector<string> tempOutNames;
90                         outputTypes["rabund"] = tempOutNames;
91                         
92                         //if the user changes the input directory command factory will send this info to us in the output parameter 
93                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
94                         if (inputDir == "not found"){   inputDir = "";          }
95                         else {
96                                 string path;
97                                 it = parameters.find("list");
98                                 //user has given a template file
99                                 if(it != parameters.end()){ 
100                                         path = m->hasPath(it->second);
101                                         //if the user has not given a path then, add inputdir. else leave path alone.
102                                         if (path == "") {       parameters["list"] = inputDir + it->second;             }
103                                 }
104                                 
105                                 it = parameters.find("sabund");
106                                 //user has given a template file
107                                 if(it != parameters.end()){ 
108                                         path = m->hasPath(it->second);
109                                         //if the user has not given a path then, add inputdir. else leave path alone.
110                                         if (path == "") {       parameters["sabund"] = inputDir + it->second;           }
111                                 }
112                         }
113                         
114                         
115                         //check for required parameters
116                         listfile = validParameter.validFile(parameters, "list", true);
117                         if (listfile == "not open") { listfile = ""; abort = true; }
118                         else if (listfile == "not found") { listfile = ""; }
119                         else {  format = "list"; inputfile = listfile; m->setListFile(listfile); }
120                         
121                         sabundfile = validParameter.validFile(parameters, "sabund", true);
122                         if (sabundfile == "not open") { sabundfile = ""; abort = true; }        
123                         else if (sabundfile == "not found") { sabundfile = ""; }
124                         else {  format = "sabund"; inputfile = sabundfile; m->setSabundFile(sabundfile); }
125                         
126                         
127                         //check for optional parameter and set defaults
128                         // ...at some point should added some additional type checking...
129                         string temp;
130                         temp = validParameter.validFile(parameters, "sorted", false);                   if (temp == "not found") { temp = "T"; }
131                         sorted = m->isTrue(temp);
132                         
133                         label = validParameter.validFile(parameters, "label", false);                   
134                         if (label == "not found") { label = ""; }
135                         else { 
136                                 if(label != "all") {  m->splitAtDash(label, labels);  allLines = 0;  }
137                                 else { allLines = 1;  }
138                         }
139                         
140                         if ((listfile == "") && (sabundfile == "")) { 
141                                 //is there are current file available for any of these?
142                                 //give priority to shared, then list, then rabund, then sabund
143                                 //if there is a current shared file, use it
144                                 listfile = m->getListFile(); 
145                                 if (listfile != "") { inputfile = listfile; format = "list"; m->mothurOut("Using " + listfile + " as input file for the list parameter."); m->mothurOutEndLine(); }
146                                 else { 
147                                         sabundfile = m->getSabundFile(); 
148                                         if (sabundfile != "") { inputfile = sabundfile; format = "sabund"; m->mothurOut("Using " + sabundfile + " as input file for the sabund parameter."); m->mothurOutEndLine(); }
149                                         else { 
150                                                 m->mothurOut("No valid current files. You must provide a list or sabund file."); m->mothurOutEndLine(); 
151                                                 abort = true;
152                                         }
153                                 }
154                         }
155                         
156                         
157                         //if the user changes the output directory command factory will send this info to us in the output parameter 
158                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = m->hasPath(inputfile);      }                       
159                         
160                 }
161                         
162
163         }
164         catch(exception& e) {
165                 m->errorOut(e, "GetRAbundCommand", "GetRAbundCommand");
166                 exit(1);
167         }                       
168 }
169 //**********************************************************************************************************************
170
171 int GetRAbundCommand::execute(){
172         try {
173         
174                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
175                 
176                 filename = outputDir + m->getRootName(m->getSimpleName(inputfile)) + "rabund";
177                 m->openOutputFile(filename, out);
178                 
179                 input = new InputData(inputfile, format);
180                 rabund = input->getRAbundVector();
181                 string lastLabel = rabund->getLabel();
182                 
183                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
184                 set<string> processedLabels;
185                 set<string> userLabels = labels;
186                 
187                 if (m->control_pressed) {  outputTypes.clear();  out.close(); m->mothurRemove(filename); delete rabund; delete input; return 0; }
188                 
189                 while((rabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
190                         
191                         if(allLines == 1 || labels.count(rabund->getLabel()) == 1){
192                                         m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
193                                         
194                                         if (m->control_pressed) {   outputTypes.clear(); out.close(); m->mothurRemove(filename);  delete input; delete rabund;  return 0;  }
195                                         
196                                         if(sorted)      {   rabund->print(out);                         }
197                                         else            {       rabund->nonSortedPrint(out);    }
198                                                                                                                         
199                                         processedLabels.insert(rabund->getLabel());
200                                         userLabels.erase(rabund->getLabel());
201                         }
202                         
203                         if ((m->anyLabelsToProcess(rabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
204                                         string saveLabel = rabund->getLabel();
205                                         
206                                         delete rabund;
207                                         rabund = input->getRAbundVector(lastLabel);
208                                         
209                                         m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
210                                         
211                                         if (m->control_pressed) {   outputTypes.clear(); out.close(); m->mothurRemove(filename);  delete input; delete rabund;  return 0;  }
212                                         
213                                         if(sorted)      {   rabund->print(out);                         }
214                                         else            {       rabund->nonSortedPrint(out);    }
215
216                                         processedLabels.insert(rabund->getLabel());
217                                         userLabels.erase(rabund->getLabel());
218                                         
219                                         //restore real lastlabel to save below
220                                         rabund->setLabel(saveLabel);
221                         }
222                         
223                         lastLabel = rabund->getLabel();         
224                         
225                         delete rabund;
226                         rabund = input->getRAbundVector();
227                 }
228                 
229                 //output error messages about any remaining user labels
230                 set<string>::iterator it;
231                 bool needToRun = false;
232                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
233                         m->mothurOut("Your file does not include the label " + *it); 
234                         if (processedLabels.count(lastLabel) != 1) {
235                                 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
236                                 needToRun = true;
237                         }else {
238                                 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
239                         }
240                 }
241                 
242                 //run last label if you need to
243                 if (needToRun == true)  {
244                         if (rabund != NULL) {   delete rabund;  }
245                         rabund = input->getRAbundVector(lastLabel);
246                         
247                         m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
248                                         
249                         if (m->control_pressed) {  outputTypes.clear(); out.close(); m->mothurRemove(filename);  delete input; delete rabund;  return 0; }
250                         
251                         if(sorted)      {   rabund->print(out);                         }
252                         else            {       rabund->nonSortedPrint(out);    }
253
254                         delete rabund;
255                 }
256                 
257                 m->mothurOutEndLine();
258                 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
259                 m->mothurOut(filename); m->mothurOutEndLine();  outputNames.push_back(filename); outputTypes["rabund"].push_back(filename);
260                 m->mothurOutEndLine();
261                 
262                 out.close(); 
263                                 
264                 //set rabund file as new current rabundfile
265                 string current = "";
266                 itTypes = outputTypes.find("rabund");
267                 if (itTypes != outputTypes.end()) {
268                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setRabundFile(current); }
269                 }
270                 
271                 return 0;               
272         }
273
274         catch(exception& e) {
275                 m->errorOut(e, "GetRAbundCommand", "execute");
276                 exit(1);
277         }
278 }
279
280 //**********************************************************************************************************************
281
282