]> git.donarmstrong.com Git - mothur.git/blob - getrabundcommand.cpp
added logfile feature
[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                 lines.clear();
20                 labels.clear();
21                 
22                 //allow user to run help
23                 if(option == "help") { help(); abort = true; }
24                 
25                 else {
26                         //valid paramters for this command
27                         string Array[] =  {"line","label"};
28                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
29                         
30                         OptionParser parser(option);
31                         map<string,string> parameters = parser.getParameters();
32                         
33                         ValidParameters validParameter;
34                         
35                         //check to make sure all parameters are valid for command
36                         for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
37                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
38                         }
39                         
40                         //make sure the user has already run the read.otu command
41                         if (globaldata->getListFile() == "") { mothurOut("You must read a listfile before you can use the get.rabund command."); mothurOutEndLine(); abort = true; }
42                         
43                         //check for optional parameter and set defaults
44                         // ...at some point should added some additional type checking...
45                         line = validParameter.validFile(parameters, "line", false);                             
46                         if (line == "not found") { line = "";  }
47                         else { 
48                                 if(line != "all") {  splitAtDash(line, lines);  allLines = 0;  }
49                                 else { allLines = 1;  }
50                         }
51                         
52                         label = validParameter.validFile(parameters, "label", false);                   
53                         if (label == "not found") { label = ""; }
54                         else { 
55                                 if(label != "all") {  splitAtDash(label, labels);  allLines = 0;  }
56                                 else { allLines = 1;  }
57                         }
58                         
59                         //make sure user did not use both the line and label parameters
60                         if ((line != "") && (label != "")) { mothurOut("You cannot use both the line and label parameters at the same time. "); mothurOutEndLine(); abort = true; }
61                         //if the user has not specified any line or labels use the ones from read.otu
62                         else if((line == "") && (label == "")) {  
63                                 allLines = globaldata->allLines; 
64                                 labels = globaldata->labels; 
65                                 lines = globaldata->lines;
66                         }
67                                 
68                         if (abort == false) {
69                                 filename = getRootName(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 line and label.  No parameters are required, and you may not use line and label at the same time.\n");
86                 mothurOut("The line and label allow you to select what distance levels you would like included in your .rabund file, and are separated by dashes.\n");
87                 mothurOut("The get.rabund command should be in the following format: get.rabund(line=yourLines, label=yourLabels).\n");
88                 mothurOut("Example get.rabund(line=1-3-5).\n");
89                 mothurOut("The default value for line and label are all lines in your inputfile.\n");
90                 mothurOut("The get.rabund command outputs a .rabund file containing the lines you selected.\n");
91                 mothurOut("Note: No spaces between parameter labels (i.e. line), '=' and parameters (i.e.yourLines).\n\n");
92         }
93         catch(exception& e) {
94                 errorOut(e, "GetRAbundCommand", "help");
95                 exit(1);
96         }
97 }
98
99 //**********************************************************************************************************************
100
101 GetRAbundCommand::~GetRAbundCommand(){
102         if (abort == false) {  globaldata->gListVector = NULL; }
103 }
104
105 //**********************************************************************************************************************
106
107 int GetRAbundCommand::execute(){
108         try {
109         
110                 if (abort == true) { return 0; }
111                 
112                 int count = 1;
113                 
114                 //read first line
115                 read = new ReadOTUFile(globaldata->inputFileName);      
116                 read->read(&*globaldata); 
117                         
118                 input = globaldata->ginput;
119                 list = globaldata->gListVector;
120                 string lastLabel = list->getLabel();
121                 
122                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
123                 set<string> processedLabels;
124                 set<string> userLabels = labels;
125                 set<int> userLines = lines;
126
127                 
128                 while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
129                         
130                         if(allLines == 1 || lines.count(count) == 1 || labels.count(list->getLabel()) == 1){
131                                         mothurOut(list->getLabel() + "\t" + toString(count)); mothurOutEndLine();
132                                         rabund = new RAbundVector();
133                                         *rabund = (list->getRAbundVector());
134                                         rabund->print(out);
135                                         delete rabund;
136                                                                                                                         
137                                         processedLabels.insert(list->getLabel());
138                                         userLabels.erase(list->getLabel());
139                                         userLines.erase(count);
140                         }
141                         
142                         if ((anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
143                                         delete list;
144                                         list = input->getListVector(lastLabel);
145                                         
146                                         mothurOut(list->getLabel() + "\t" + toString(count)); mothurOutEndLine();
147                                         rabund = new RAbundVector();
148                                         *rabund = (list->getRAbundVector());
149                                         rabund->print(out);
150                                         delete rabund;
151
152                                         processedLabels.insert(list->getLabel());
153                                         userLabels.erase(list->getLabel());
154                         }
155                         
156                         lastLabel = list->getLabel();           
157                         
158                         delete list;
159                         list = input->getListVector();
160                         count++;
161                 }
162                 
163                 //output error messages about any remaining user labels
164                 set<string>::iterator it;
165                 bool needToRun = false;
166                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
167                         mothurOut("Your file does not include the label " + *it); 
168                         if (processedLabels.count(lastLabel) != 1) {
169                                 mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine();
170                                 needToRun = true;
171                         }else {
172                                 mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine();
173                         }
174                 }
175                 
176                 //run last line if you need to
177                 if (needToRun == true)  {
178                         delete list;
179                         list = input->getListVector(lastLabel);
180                         
181                         mothurOut(list->getLabel() + "\t" + toString(count)); mothurOutEndLine();
182                         rabund = new RAbundVector();
183                         *rabund = (list->getRAbundVector());
184                         rabund->print(out);
185                         delete rabund;
186                         delete list;
187                 }
188
189                 out.close(); 
190                 return 0;               
191         }
192
193         catch(exception& e) {
194                 errorOut(e, "GetRAbundCommand", "execute");
195                 exit(1);
196         }
197 }
198
199 //**********************************************************************************************************************
200
201