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"};
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 //make sure the user has already run the read.otu command
40 if (globaldata->getListFile() == "") { mothurOut("You must read a listfile before you can use the get.rabund command."); mothurOutEndLine(); abort = true; }
42 //check for optional parameter and set defaults
43 // ...at some point should added some additional type checking...
46 temp = validParameter.validFile(parameters, "sorted", false); if (temp == "not found") { temp = "T"; }
47 sorted = isTrue(temp);
49 label = validParameter.validFile(parameters, "label", false);
50 if (label == "not found") { label = ""; }
52 if(label != "all") { splitAtDash(label, labels); allLines = 0; }
53 else { allLines = 1; }
56 //if the user has not specified any labels use the ones from read.otu
58 allLines = globaldata->allLines;
59 labels = globaldata->labels;
63 filename = getRootName(globaldata->inputFileName) + "rabund";
64 openOutputFile(filename, out);
70 errorOut(e, "GetRAbundCommand", "GetRAbundCommand");
74 //**********************************************************************************************************************
76 void GetRAbundCommand::help(){
78 mothurOut("The get.rabund command can only be executed after a successful read.otu of a listfile.\n");
79 mothurOut("The get.rabund command parameters are label and sorted. No parameters are required.\n");
80 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");
81 mothurOut("The sorted parameters allows you to print the rabund results sorted by abundance or not. The default is sorted.\n");
82 mothurOut("The get.rabund command should be in the following format: get.rabund(label=yourLabels, sorted=yourSorted).\n");
83 mothurOut("Example get.rabund(sorted=F).\n");
84 mothurOut("The default value for label is all labels in your inputfile.\n");
85 mothurOut("The get.rabund command outputs a .rabund file containing the lines you selected.\n");
86 mothurOut("Note: No spaces between parameter labels (i.e. label), '=' and parameters (i.e.yourLabels).\n\n");
89 errorOut(e, "GetRAbundCommand", "help");
94 //**********************************************************************************************************************
96 GetRAbundCommand::~GetRAbundCommand(){
97 if (abort == false) { globaldata->gListVector = NULL; }
100 //**********************************************************************************************************************
102 int GetRAbundCommand::execute(){
105 if (abort == true) { return 0; }
108 read = new ReadOTUFile(globaldata->inputFileName);
109 read->read(&*globaldata);
111 input = globaldata->ginput;
112 list = globaldata->gListVector;
113 string lastLabel = list->getLabel();
115 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
116 set<string> processedLabels;
117 set<string> userLabels = labels;
119 while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
121 if(allLines == 1 || labels.count(list->getLabel()) == 1){
122 mothurOut(list->getLabel()); mothurOutEndLine();
123 rabund = new RAbundVector();
124 *rabund = (list->getRAbundVector());
126 if(sorted) { rabund->print(out); }
127 else { rabund->nonSortedPrint(out); }
131 processedLabels.insert(list->getLabel());
132 userLabels.erase(list->getLabel());
135 if ((anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
136 string saveLabel = list->getLabel();
139 list = input->getListVector(lastLabel);
141 mothurOut(list->getLabel()); mothurOutEndLine();
142 rabund = new RAbundVector();
143 *rabund = (list->getRAbundVector());
145 if(sorted) { rabund->print(out); }
146 else { rabund->nonSortedPrint(out); }
150 processedLabels.insert(list->getLabel());
151 userLabels.erase(list->getLabel());
153 //restore real lastlabel to save below
154 list->setLabel(saveLabel);
157 lastLabel = list->getLabel();
160 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 label if you need to
177 if (needToRun == true) {
178 if (list != NULL) { delete list; }
179 list = input->getListVector(lastLabel);
181 mothurOut(list->getLabel()); mothurOutEndLine();
182 rabund = new RAbundVector();
183 *rabund = (list->getRAbundVector());
185 if(sorted) { rabund->print(out); }
186 else { rabund->nonSortedPrint(out); }
196 catch(exception& e) {
197 errorOut(e, "GetRAbundCommand", "execute");
202 //**********************************************************************************************************************