]> git.donarmstrong.com Git - mothur.git/blob - getrabundcommand.cpp
added checks for ^C to quit command instead of program
[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
14 GetRAbundCommand::GetRAbundCommand(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","sorted","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 += 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() == "") { m->mothurOut("You must read a listfile before you can use the get.rabund 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                         
51                         string temp;
52                         temp = validParameter.validFile(parameters, "sorted", false);                   if (temp == "not found") { temp = "T"; }
53                         sorted = isTrue(temp);
54                         
55                         label = validParameter.validFile(parameters, "label", false);                   
56                         if (label == "not found") { label = ""; }
57                         else { 
58                                 if(label != "all") {  splitAtDash(label, labels);  allLines = 0;  }
59                                 else { allLines = 1;  }
60                         }
61                         
62                         //if the user has not specified any labels use the ones from read.otu
63                         if(label == "") {  
64                                 allLines = globaldata->allLines; 
65                                 labels = globaldata->labels; 
66                         }
67                                 
68                         if (abort == false) {
69                                 filename = outputDir + getRootName(getSimpleName(globaldata->inputFileName)) + "rabund";
70                                 openOutputFile(filename, out);
71                         }
72                 }
73
74         }
75         catch(exception& e) {
76                 m->errorOut(e, "GetRAbundCommand", "GetRAbundCommand");
77                 exit(1);
78         }                       
79 }
80 //**********************************************************************************************************************
81
82 void GetRAbundCommand::help(){
83         try {
84                 m->mothurOut("The get.rabund command can only be executed after a successful read.otu of a listfile.\n");
85                 m->mothurOut("The get.rabund command parameters are label and sorted.  No parameters are required.\n");
86                 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");
87                 m->mothurOut("The sorted parameters allows you to print the rabund results sorted by abundance or not.  The default is sorted.\n");
88                 m->mothurOut("The get.rabund command should be in the following format: get.rabund(label=yourLabels, sorted=yourSorted).\n");
89                 m->mothurOut("Example get.rabund(sorted=F).\n");
90                 m->mothurOut("The default value for label is all labels in your inputfile.\n");
91                 m->mothurOut("The get.rabund command outputs a .rabund file containing the lines you selected.\n");
92                 m->mothurOut("Note: No spaces between parameter labels (i.e. label), '=' and parameters (i.e.yourLabels).\n\n");
93         }
94         catch(exception& e) {
95                 m->errorOut(e, "GetRAbundCommand", "help");
96                 exit(1);
97         }
98 }
99
100 //**********************************************************************************************************************
101
102 GetRAbundCommand::~GetRAbundCommand(){}
103
104 //**********************************************************************************************************************
105
106 int GetRAbundCommand::execute(){
107         try {
108         
109                 if (abort == true) { return 0; }
110                 
111                 //read first line
112                 read = new ReadOTUFile(globaldata->inputFileName);      
113                 read->read(&*globaldata); 
114                         
115                 input = globaldata->ginput;
116                 list = globaldata->gListVector;
117                 string lastLabel = list->getLabel();
118                 
119                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
120                 set<string> processedLabels;
121                 set<string> userLabels = labels;
122                 
123                 if (m->control_pressed) {  out.close(); remove(filename.c_str()); delete read; delete input; delete list; globaldata->gListVector = NULL;  return 0; }
124                 
125                 while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
126                         
127                         if(allLines == 1 || labels.count(list->getLabel()) == 1){
128                                         m->mothurOut(list->getLabel()); m->mothurOutEndLine();
129                                         rabund = new RAbundVector();                            
130                                         *rabund = (list->getRAbundVector());
131                                         
132                                         if (m->control_pressed) {  out.close(); remove(filename.c_str()); delete read; delete input; delete list; delete rabund; globaldata->gListVector = NULL;  return 0; }
133
134                                         
135                                         if(sorted)      {   rabund->print(out);                         }
136                                         else            {       rabund->nonSortedPrint(out);    }
137                                         
138                                         delete rabund;
139                                                                                                                         
140                                         processedLabels.insert(list->getLabel());
141                                         userLabels.erase(list->getLabel());
142                         }
143                         
144                         if ((anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
145                                         string saveLabel = list->getLabel();
146                                         
147                                         delete list;
148                                         list = input->getListVector(lastLabel);
149                                         
150                                         m->mothurOut(list->getLabel()); m->mothurOutEndLine();
151                                         rabund = new RAbundVector();
152                                         *rabund = (list->getRAbundVector());
153                                         
154                                         if (m->control_pressed) {  out.close(); remove(filename.c_str()); delete read; delete input; delete list; delete rabund; globaldata->gListVector = NULL;  return 0; }
155                                         
156                                         if(sorted)      {   rabund->print(out);                         }
157                                         else            {       rabund->nonSortedPrint(out);    }
158
159                                         delete rabund;
160
161                                         processedLabels.insert(list->getLabel());
162                                         userLabels.erase(list->getLabel());
163                                         
164                                         //restore real lastlabel to save below
165                                         list->setLabel(saveLabel);
166                         }
167                         
168                         lastLabel = list->getLabel();           
169                         
170                         delete list;
171                         list = input->getListVector();
172                 }
173                 
174                 //output error messages about any remaining user labels
175                 set<string>::iterator it;
176                 bool needToRun = false;
177                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
178                         m->mothurOut("Your file does not include the label " + *it); 
179                         if (processedLabels.count(lastLabel) != 1) {
180                                 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
181                                 needToRun = true;
182                         }else {
183                                 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
184                         }
185                 }
186                 
187                 //run last label if you need to
188                 if (needToRun == true)  {
189                         if (list != NULL) {     delete list;    }
190                         list = input->getListVector(lastLabel);
191                         
192                         m->mothurOut(list->getLabel()); m->mothurOutEndLine();
193                         rabund = new RAbundVector();
194                         *rabund = (list->getRAbundVector());
195                         
196                         if (m->control_pressed) {  out.close(); remove(filename.c_str()); delete read; delete input; delete list; delete rabund; globaldata->gListVector = NULL;  return 0; }
197                         
198                         if(sorted)      {   rabund->print(out);                         }
199                         else            {       rabund->nonSortedPrint(out);    }
200
201                         delete rabund;
202                         delete list;
203                 }
204                 
205                 m->mothurOutEndLine();
206                 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
207                 m->mothurOut(filename); m->mothurOutEndLine();  
208                 m->mothurOutEndLine();
209
210                 
211                 out.close(); 
212                 delete read; delete input;
213                 globaldata->gListVector = NULL; 
214                 
215                 return 0;               
216         }
217
218         catch(exception& e) {
219                 m->errorOut(e, "GetRAbundCommand", "execute");
220                 exit(1);
221         }
222 }
223
224 //**********************************************************************************************************************
225
226