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