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"};
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...
45 line = validParameter.validFile(parameters, "line", false);
46 if (line == "not found") { line = ""; }
48 if(line != "all") { splitAtDash(line, lines); allLines = 0; }
49 else { allLines = 1; }
52 label = validParameter.validFile(parameters, "label", false);
53 if (label == "not found") { label = ""; }
55 if(label != "all") { splitAtDash(label, labels); allLines = 0; }
56 else { allLines = 1; }
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;
69 filename = getRootName(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 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");
94 errorOut(e, "GetRAbundCommand", "help");
99 //**********************************************************************************************************************
101 GetRAbundCommand::~GetRAbundCommand(){
102 if (abort == false) { globaldata->gListVector = NULL; }
105 //**********************************************************************************************************************
107 int GetRAbundCommand::execute(){
110 if (abort == true) { return 0; }
115 read = new ReadOTUFile(globaldata->inputFileName);
116 read->read(&*globaldata);
118 input = globaldata->ginput;
119 list = globaldata->gListVector;
120 string lastLabel = list->getLabel();
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;
128 while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
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());
137 processedLabels.insert(list->getLabel());
138 userLabels.erase(list->getLabel());
139 userLines.erase(count);
142 if ((anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
144 list = input->getListVector(lastLabel);
146 mothurOut(list->getLabel() + "\t" + toString(count)); mothurOutEndLine();
147 rabund = new RAbundVector();
148 *rabund = (list->getRAbundVector());
152 processedLabels.insert(list->getLabel());
153 userLabels.erase(list->getLabel());
156 lastLabel = list->getLabel();
159 list = input->getListVector();
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();
172 mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine();
176 //run last line if you need to
177 if (needToRun == true) {
179 list = input->getListVector(lastLabel);
181 mothurOut(list->getLabel() + "\t" + toString(count)); mothurOutEndLine();
182 rabund = new RAbundVector();
183 *rabund = (list->getRAbundVector());
193 catch(exception& e) {
194 errorOut(e, "GetRAbundCommand", "execute");
199 //**********************************************************************************************************************