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