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();
21 //allow user to run help
22 if(option == "help") { help(); abort = true; }
25 //valid paramters for this command
26 string Array[] = {"label","sorted","outputdir","inputdir"};
27 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
29 OptionParser parser(option);
30 map<string,string> parameters = parser.getParameters();
32 ValidParameters validParameter;
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; }
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"){
42 outputDir += hasPath(globaldata->inputFileName); //if user entered a file with a path then preserve it
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; }
48 //check for optional parameter and set defaults
49 // ...at some point should added some additional type checking...
52 temp = validParameter.validFile(parameters, "sorted", false); if (temp == "not found") { temp = "T"; }
53 sorted = isTrue(temp);
55 label = validParameter.validFile(parameters, "label", false);
56 if (label == "not found") { label = ""; }
58 if(label != "all") { splitAtDash(label, labels); allLines = 0; }
59 else { allLines = 1; }
62 //if the user has not specified any labels use the ones from read.otu
64 allLines = globaldata->allLines;
65 labels = globaldata->labels;
69 filename = outputDir + getRootName(getSimpleName(globaldata->inputFileName)) + "rabund";
70 openOutputFile(filename, out);
76 errorOut(e, "GetRAbundCommand", "GetRAbundCommand");
80 //**********************************************************************************************************************
82 void GetRAbundCommand::help(){
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");
95 errorOut(e, "GetRAbundCommand", "help");
100 //**********************************************************************************************************************
102 GetRAbundCommand::~GetRAbundCommand(){
103 if (abort == false) { globaldata->gListVector = NULL; }
106 //**********************************************************************************************************************
108 int GetRAbundCommand::execute(){
111 if (abort == true) { return 0; }
114 read = new ReadOTUFile(globaldata->inputFileName);
115 read->read(&*globaldata);
117 input = globaldata->ginput;
118 list = globaldata->gListVector;
119 string lastLabel = list->getLabel();
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;
125 while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
127 if(allLines == 1 || labels.count(list->getLabel()) == 1){
128 mothurOut(list->getLabel()); mothurOutEndLine();
129 rabund = new RAbundVector();
130 *rabund = (list->getRAbundVector());
132 if(sorted) { rabund->print(out); }
133 else { rabund->nonSortedPrint(out); }
137 processedLabels.insert(list->getLabel());
138 userLabels.erase(list->getLabel());
141 if ((anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
142 string saveLabel = list->getLabel();
145 list = input->getListVector(lastLabel);
147 mothurOut(list->getLabel()); mothurOutEndLine();
148 rabund = new RAbundVector();
149 *rabund = (list->getRAbundVector());
151 if(sorted) { rabund->print(out); }
152 else { rabund->nonSortedPrint(out); }
156 processedLabels.insert(list->getLabel());
157 userLabels.erase(list->getLabel());
159 //restore real lastlabel to save below
160 list->setLabel(saveLabel);
163 lastLabel = list->getLabel();
166 list = input->getListVector();
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();
178 mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine();
182 //run last label if you need to
183 if (needToRun == true) {
184 if (list != NULL) { delete list; }
185 list = input->getListVector(lastLabel);
187 mothurOut(list->getLabel()); mothurOutEndLine();
188 rabund = new RAbundVector();
189 *rabund = (list->getRAbundVector());
191 if(sorted) { rabund->print(out); }
192 else { rabund->nonSortedPrint(out); }
202 catch(exception& e) {
203 errorOut(e, "GetRAbundCommand", "execute");
208 //**********************************************************************************************************************