5 * Created by Sarah Westcott on 6/2/09.
6 * Copyright 2009 Schloss Lab Umass Amherst. All rights reserved.
10 #include "getrabundcommand.h"
12 //**********************************************************************************************************************
14 GetRAbundCommand::GetRAbundCommand(string option){
16 globaldata = GlobalData::getInstance();
22 //allow user to run help
23 if(option == "help") { help(); abort = true; }
26 //valid paramters for this command
27 string Array[] = {"line","label","sorted"};
28 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
30 OptionParser parser(option);
31 map<string,string> parameters = parser.getParameters();
33 ValidParameters validParameter;
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; }
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; }
43 //check for optional parameter and set defaults
44 // ...at some point should added some additional type checking...
47 temp = validParameter.validFile(parameters, "sorted", false); if (temp == "not found") { temp = "T"; }
48 sorted = isTrue(temp);
50 line = validParameter.validFile(parameters, "line", false);
51 if (line == "not found") { line = ""; }
53 if(line != "all") { splitAtDash(line, lines); allLines = 0; }
54 else { allLines = 1; }
57 label = validParameter.validFile(parameters, "label", false);
58 if (label == "not found") { label = ""; }
60 if(label != "all") { splitAtDash(label, labels); allLines = 0; }
61 else { allLines = 1; }
64 //make sure user did not use both the line and label parameters
65 if ((line != "") && (label != "")) { mothurOut("You cannot use both the line and label parameters at the same time. "); mothurOutEndLine(); abort = true; }
66 //if the user has not specified any line or labels use the ones from read.otu
67 else if((line == "") && (label == "")) {
68 allLines = globaldata->allLines;
69 labels = globaldata->labels;
70 lines = globaldata->lines;
74 filename = getRootName(globaldata->inputFileName) + "rabund";
75 openOutputFile(filename, out);
81 errorOut(e, "GetRAbundCommand", "GetRAbundCommand");
85 //**********************************************************************************************************************
87 void GetRAbundCommand::help(){
89 mothurOut("The get.rabund command can only be executed after a successful read.otu of a listfile.\n");
90 mothurOut("The get.rabund command parameters are line, label and sorted. No parameters are required, and you may not use line and label at the same time.\n");
91 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");
92 mothurOut("The sorted parameters allows you to print the rabund results sorted by abundance or not. The default is sorted.\n");
93 mothurOut("The get.rabund command should be in the following format: get.rabund(line=yourLines, label=yourLabels, sorted=yourSorted).\n");
94 mothurOut("Example get.rabund(line=1-3-5, sorted=F).\n");
95 mothurOut("The default value for line and label are all lines in your inputfile.\n");
96 mothurOut("The get.rabund command outputs a .rabund file containing the lines you selected.\n");
97 mothurOut("Note: No spaces between parameter labels (i.e. line), '=' and parameters (i.e.yourLines).\n\n");
100 errorOut(e, "GetRAbundCommand", "help");
105 //**********************************************************************************************************************
107 GetRAbundCommand::~GetRAbundCommand(){
108 if (abort == false) { globaldata->gListVector = NULL; }
111 //**********************************************************************************************************************
113 int GetRAbundCommand::execute(){
116 if (abort == true) { return 0; }
121 read = new ReadOTUFile(globaldata->inputFileName);
122 read->read(&*globaldata);
124 input = globaldata->ginput;
125 list = globaldata->gListVector;
126 string lastLabel = list->getLabel();
128 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
129 set<string> processedLabels;
130 set<string> userLabels = labels;
131 set<int> userLines = lines;
134 while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
136 if(allLines == 1 || lines.count(count) == 1 || labels.count(list->getLabel()) == 1){
137 mothurOut(list->getLabel()); mothurOutEndLine();
138 rabund = new RAbundVector();
139 *rabund = (list->getRAbundVector());
141 if(sorted) { rabund->print(out); }
142 else { rabund->nonSortedPrint(out); }
146 processedLabels.insert(list->getLabel());
147 userLabels.erase(list->getLabel());
148 userLines.erase(count);
151 if ((anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
153 list = input->getListVector(lastLabel);
155 mothurOut(list->getLabel()); mothurOutEndLine();
156 rabund = new RAbundVector();
157 *rabund = (list->getRAbundVector());
159 if(sorted) { rabund->print(out); }
160 else { rabund->nonSortedPrint(out); }
164 processedLabels.insert(list->getLabel());
165 userLabels.erase(list->getLabel());
168 lastLabel = list->getLabel();
171 list = input->getListVector();
175 //output error messages about any remaining user labels
176 set<string>::iterator it;
177 bool needToRun = false;
178 for (it = userLabels.begin(); it != userLabels.end(); it++) {
179 mothurOut("Your file does not include the label " + *it);
180 if (processedLabels.count(lastLabel) != 1) {
181 mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine();
184 mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine();
188 //run last line if you need to
189 if (needToRun == true) {
190 if (list != NULL) { delete list; }
191 list = input->getListVector(lastLabel);
193 mothurOut(list->getLabel()); mothurOutEndLine();
194 rabund = new RAbundVector();
195 *rabund = (list->getRAbundVector());
197 if(sorted) { rabund->print(out); }
198 else { rabund->nonSortedPrint(out); }
208 catch(exception& e) {
209 errorOut(e, "GetRAbundCommand", "execute");
214 //**********************************************************************************************************************