]> git.donarmstrong.com Git - mothur.git/blob - getsabundcommand.cpp
added pipeline commands which involved change to command factory and command class...
[mothur.git] / getsabundcommand.cpp
1 /*
2  *  getsabundcommand.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 "getsabundcommand.h"
11
12 //**********************************************************************************************************************
13 vector<string> GetSAbundCommand::getValidParameters(){  
14         try {
15                 string Array[] =  {"label","outputdir","inputdir"};
16                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
17                 return myArray;
18         }
19         catch(exception& e) {
20                 m->errorOut(e, "GetSAbundCommand", "getValidParameters");
21                 exit(1);
22         }
23 }
24 //**********************************************************************************************************************
25 GetSAbundCommand::GetSAbundCommand(){   
26         try {
27                 //initialize outputTypes
28                 vector<string> tempOutNames;
29                 outputTypes["sabund"] = tempOutNames;
30         }
31         catch(exception& e) {
32                 m->errorOut(e, "GetSAbundCommand", "GetSAbundCommand");
33                 exit(1);
34         }
35 }
36 //**********************************************************************************************************************
37 vector<string> GetSAbundCommand::getRequiredParameters(){       
38         try {
39                 vector<string> myArray;
40                 return myArray;
41         }
42         catch(exception& e) {
43                 m->errorOut(e, "GetSAbundCommand", "getRequiredParameters");
44                 exit(1);
45         }
46 }
47 //**********************************************************************************************************************
48 vector<string> GetSAbundCommand::getRequiredFiles(){    
49         try {
50                 string Array[] =  {"list","rabund"};
51                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
52                 return myArray;
53         }
54         catch(exception& e) {
55                 m->errorOut(e, "GetSAbundCommand", "getRequiredFiles");
56                 exit(1);
57         }
58 }
59 //**********************************************************************************************************************
60 GetSAbundCommand::GetSAbundCommand(string option)  {
61         try {
62                 globaldata = GlobalData::getInstance();
63                 abort = false;
64                 allLines = 1;
65                 labels.clear();
66                 
67                 //allow user to run help
68                 if(option == "help") { help(); abort = true; }
69                 
70                 else {
71                         //valid paramters for this command
72                         string Array[] =  {"label","outputdir","inputdir"};
73                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
74                         
75                         OptionParser parser(option);
76                         map<string,string> parameters = parser.getParameters();
77                         
78                         ValidParameters validParameter;
79                         
80                         //check to make sure all parameters are valid for command
81                         for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
82                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
83                         }
84                         
85                         //initialize outputTypes
86                         vector<string> tempOutNames;
87                         outputTypes["sabund"] = tempOutNames;
88                 
89                         //if the user changes the output directory command factory will send this info to us in the output parameter 
90                         string outputDir = validParameter.validFile(parameters, "outputdir", false);            if (outputDir == "not found"){  
91                                 outputDir = ""; 
92                                 outputDir += m->hasPath(globaldata->inputFileName); //if user entered a file with a path then preserve it       
93                         }
94
95                         //make sure the user has already run the read.otu command
96                         if ((globaldata->getListFile() == "") && (globaldata->getRabundFile() == "")) { m->mothurOut("You must read a list or rabund before you can use the get.sabund command."); m->mothurOutEndLine(); abort = true; }
97                         
98                         //check for optional parameter and set defaults
99                         // ...at some point should added some additional type checking...
100                         label = validParameter.validFile(parameters, "label", false);                   
101                         if (label == "not found") { label = ""; }
102                         else { 
103                                 if(label != "all") {  m->splitAtDash(label, labels);  allLines = 0;  }
104                                 else { allLines = 1;  }
105                         }
106                         
107                         //if the user has not specified any labels use the ones from read.otu
108                         if(label == "") {  
109                                 allLines = globaldata->allLines; 
110                                 labels = globaldata->labels; 
111                         }
112                                 
113                         if (abort == false) {
114                                 filename = outputDir + m->getRootName(m->getSimpleName(globaldata->inputFileName)) + "sabund";
115                                 m->openOutputFile(filename, out);
116                         }
117                 }
118
119         }
120         catch(exception& e) {
121                 m->errorOut(e, "GetSAbundCommand", "GetSAbundCommand");
122                 exit(1);
123         }
124 }
125 //**********************************************************************************************************************
126
127 void GetSAbundCommand::help(){
128         try {
129                 m->mothurOut("The get.sabund command can only be executed after a successful read.otu of a listfile or rabundfile.\n");
130                 m->mothurOut("The get.sabund command parameters is label.  No parameters are required.\n");
131                 m->mothurOut("The label parameter allows you to select what distance levels you would like included in your .sabund file, and are separated by dashes.\n");
132                 m->mothurOut("The get.sabund command should be in the following format: get.sabund(label=yourLabels).\n");
133                 m->mothurOut("Example get.sabund().\n");
134                 m->mothurOut("The default value for label is all labels in your inputfile.\n");
135                 m->mothurOut("The get.sabund command outputs a .sabund file containing the labels you selected.\n");
136                 m->mothurOut("Note: No spaces between parameter labels (i.e. label), '=' and parameters (i.e.yourLabel).\n\n");
137         }
138         catch(exception& e) {
139                 m->errorOut(e, "GetSAbundCommand", "help");
140                 exit(1);
141         }
142 }
143
144 //**********************************************************************************************************************
145
146 GetSAbundCommand::~GetSAbundCommand(){
147 }
148
149 //**********************************************************************************************************************
150
151 int GetSAbundCommand::execute(){
152         try {
153                 
154                 if (abort == true) { return 0; }
155         
156                 //using order vector so you don't have to distinguish between the list and rabund files
157                 read = new ReadOTUFile(globaldata->inputFileName);      
158                 read->read(&*globaldata); 
159                 
160                 order = globaldata->gorder;
161                 string lastLabel = order->getLabel();
162                 input = globaldata->ginput;
163                                                 
164                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
165                 set<string> processedLabels;
166                 set<string> userLabels = labels;
167                 
168                 if (m->control_pressed) {  outputTypes.clear(); out.close(); remove(filename.c_str());  delete order; globaldata->gorder = NULL;  return 0; }
169
170                 
171                 while((order != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
172                         
173                         if(allLines == 1 || labels.count(order->getLabel()) == 1){
174                                         m->mothurOut(order->getLabel());  m->mothurOutEndLine();
175                                         sabund = new SAbundVector();
176                                         *sabund = (order->getSAbundVector());
177                                         sabund->print(out);
178                                         delete sabund;
179                                         
180                                         if (m->control_pressed) { outputTypes.clear();  out.close(); remove(filename.c_str());  delete order; globaldata->gorder = NULL;  return 0; }
181
182                                         processedLabels.insert(order->getLabel());
183                                         userLabels.erase(order->getLabel());
184                         }
185                         
186                         if ((m->anyLabelsToProcess(order->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
187                                         string saveLabel = order->getLabel();
188                                         
189                                         delete order;           
190                                         order = (input->getOrderVector(lastLabel));
191                                         
192                                         m->mothurOut(order->getLabel());  m->mothurOutEndLine();
193                                         sabund = new SAbundVector();
194                                         *sabund = (order->getSAbundVector());
195                                         sabund->print(out);
196                                         delete sabund;
197                                         
198                                         if (m->control_pressed) {  outputTypes.clear(); out.close(); remove(filename.c_str());  delete order; globaldata->gorder = NULL;  return 0; }
199
200                                         processedLabels.insert(order->getLabel());
201                                         userLabels.erase(order->getLabel());
202                                         
203                                         //restore real lastlabel to save below
204                                         order->setLabel(saveLabel);
205                         }
206                         
207                         
208                         lastLabel = order->getLabel();  
209                         
210                         delete order;           
211                         order = (input->getOrderVector());
212                 }
213                 
214                 //output error messages about any remaining user labels
215                 set<string>::iterator it;
216                 bool needToRun = false;
217                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
218                         m->mothurOut("Your file does not include the label " + *it); 
219                         if (processedLabels.count(lastLabel) != 1) {
220                                 m->mothurOut(". I will use " + lastLabel + ".");  m->mothurOutEndLine();
221                                 needToRun = true;
222                         }else {
223                                 m->mothurOut(". Please refer to " + lastLabel + ".");  m->mothurOutEndLine();
224                         }
225                 }
226                 
227                 //run last label if you need to
228                 if (needToRun == true)  {
229                         if (order != NULL) {    delete order;   }
230                         order = (input->getOrderVector(lastLabel));
231                         
232                         m->mothurOut(order->getLabel());  m->mothurOutEndLine();
233                         sabund = new SAbundVector();
234                         *sabund = (order->getSAbundVector());
235                         sabund->print(out);
236                         delete sabund;
237                         
238                         if (m->control_pressed) {  outputTypes.clear(); out.close(); remove(filename.c_str());  delete order; globaldata->gorder = NULL;  return 0; }
239                         
240                         delete order;
241                 }
242                 globaldata->gorder = NULL;
243
244                 out.close();
245                 
246                 m->mothurOutEndLine();
247                 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
248                 m->mothurOut(filename); m->mothurOutEndLine();  outputNames.push_back(filename); outputTypes["sabund"].push_back(filename);
249                 m->mothurOutEndLine();
250                 
251                 return 0;               
252         }
253
254         catch(exception& e) {
255                 m->errorOut(e, "GetSAbundCommand", "execute");
256                 exit(1);
257         }
258 }
259
260 //**********************************************************************************************************************
261
262