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