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() == "") { m->mothurOut("You must read a listfile before you can use the get.rabund command."); m->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 m->errorOut(e, "GetRAbundCommand", "GetRAbundCommand");
80 //**********************************************************************************************************************
82 void GetRAbundCommand::help(){
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");
95 m->errorOut(e, "GetRAbundCommand", "help");
100 //**********************************************************************************************************************
102 GetRAbundCommand::~GetRAbundCommand(){}
104 //**********************************************************************************************************************
106 int GetRAbundCommand::execute(){
109 if (abort == true) { return 0; }
112 read = new ReadOTUFile(globaldata->inputFileName);
113 read->read(&*globaldata);
115 input = globaldata->ginput;
116 list = globaldata->gListVector;
117 string lastLabel = list->getLabel();
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;
123 if (m->control_pressed) { out.close(); remove(filename.c_str()); delete read; delete input; delete list; globaldata->gListVector = NULL; return 0; }
125 while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
127 if(allLines == 1 || labels.count(list->getLabel()) == 1){
128 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
129 rabund = new RAbundVector();
130 *rabund = (list->getRAbundVector());
132 if (m->control_pressed) { out.close(); remove(filename.c_str()); delete read; delete input; delete list; delete rabund; globaldata->gListVector = NULL; return 0; }
135 if(sorted) { rabund->print(out); }
136 else { rabund->nonSortedPrint(out); }
140 processedLabels.insert(list->getLabel());
141 userLabels.erase(list->getLabel());
144 if ((anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
145 string saveLabel = list->getLabel();
148 list = input->getListVector(lastLabel);
150 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
151 rabund = new RAbundVector();
152 *rabund = (list->getRAbundVector());
154 if (m->control_pressed) { out.close(); remove(filename.c_str()); delete read; delete input; delete list; delete rabund; globaldata->gListVector = NULL; return 0; }
156 if(sorted) { rabund->print(out); }
157 else { rabund->nonSortedPrint(out); }
161 processedLabels.insert(list->getLabel());
162 userLabels.erase(list->getLabel());
164 //restore real lastlabel to save below
165 list->setLabel(saveLabel);
168 lastLabel = list->getLabel();
171 list = input->getListVector();
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();
183 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
187 //run last label if you need to
188 if (needToRun == true) {
189 if (list != NULL) { delete list; }
190 list = input->getListVector(lastLabel);
192 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
193 rabund = new RAbundVector();
194 *rabund = (list->getRAbundVector());
196 if (m->control_pressed) { out.close(); remove(filename.c_str()); delete read; delete input; delete list; delete rabund; globaldata->gListVector = NULL; return 0; }
198 if(sorted) { rabund->print(out); }
199 else { rabund->nonSortedPrint(out); }
205 m->mothurOutEndLine();
206 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
207 m->mothurOut(filename); m->mothurOutEndLine();
208 m->mothurOutEndLine();
212 delete read; delete input;
213 globaldata->gListVector = NULL;
218 catch(exception& e) {
219 m->errorOut(e, "GetRAbundCommand", "execute");
224 //**********************************************************************************************************************