5 * Created by Sarah Westcott on 6/2/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "getsabundcommand.h"
12 //**********************************************************************************************************************
14 GetSAbundCommand::GetSAbundCommand(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","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() == "") && (globaldata->getRabundFile() == "")) { mothurOut("You must read a list or rabund before you can use the get.sabund command."); mothurOutEndLine(); abort = true; }
48 //check for optional parameter and set defaults
49 // ...at some point should added some additional type checking...
50 label = validParameter.validFile(parameters, "label", false);
51 if (label == "not found") { label = ""; }
53 if(label != "all") { splitAtDash(label, labels); allLines = 0; }
54 else { allLines = 1; }
57 //if the user has not specified any labels use the ones from read.otu
59 allLines = globaldata->allLines;
60 labels = globaldata->labels;
64 filename = outputDir + getRootName(getSimpleName(globaldata->inputFileName)) + "sabund";
65 openOutputFile(filename, out);
71 errorOut(e, "GetSAbundCommand", "GetSAbundCommand");
75 //**********************************************************************************************************************
77 void GetSAbundCommand::help(){
79 mothurOut("The get.sabund command can only be executed after a successful read.otu of a listfile or rabundfile.\n");
80 mothurOut("The get.sabund command parameters is label. No parameters are required.\n");
81 mothurOut("The label parameter allows you to select what distance levels you would like included in your .sabund file, and are separated by dashes.\n");
82 mothurOut("The get.sabund command should be in the following format: get.sabund(label=yourLabels).\n");
83 mothurOut("Example get.sabund().\n");
84 mothurOut("The default value for label is all labels in your inputfile.\n");
85 mothurOut("The get.sabund command outputs a .sabund file containing the labels you selected.\n");
86 mothurOut("Note: No spaces between parameter labels (i.e. label), '=' and parameters (i.e.yourLabel).\n\n");
89 errorOut(e, "GetSAbundCommand", "help");
94 //**********************************************************************************************************************
96 GetSAbundCommand::~GetSAbundCommand(){
99 //**********************************************************************************************************************
101 int GetSAbundCommand::execute(){
104 if (abort == true) { return 0; }
106 //using order vector so you don't have to distinguish between the list and rabund files
107 read = new ReadOTUFile(globaldata->inputFileName);
108 read->read(&*globaldata);
110 order = globaldata->gorder;
111 string lastLabel = order->getLabel();
112 input = globaldata->ginput;
114 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
115 set<string> processedLabels;
116 set<string> userLabels = labels;
118 while((order != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
120 if(allLines == 1 || labels.count(order->getLabel()) == 1){
121 mothurOut(order->getLabel()); mothurOutEndLine();
122 sabund = new SAbundVector();
123 *sabund = (order->getSAbundVector());
127 processedLabels.insert(order->getLabel());
128 userLabels.erase(order->getLabel());
131 if ((anyLabelsToProcess(order->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
132 string saveLabel = order->getLabel();
135 order = (input->getOrderVector(lastLabel));
137 mothurOut(order->getLabel()); mothurOutEndLine();
138 sabund = new SAbundVector();
139 *sabund = (order->getSAbundVector());
143 processedLabels.insert(order->getLabel());
144 userLabels.erase(order->getLabel());
146 //restore real lastlabel to save below
147 order->setLabel(saveLabel);
151 lastLabel = order->getLabel();
154 order = (input->getOrderVector());
157 //output error messages about any remaining user labels
158 set<string>::iterator it;
159 bool needToRun = false;
160 for (it = userLabels.begin(); it != userLabels.end(); it++) {
161 mothurOut("Your file does not include the label " + *it);
162 if (processedLabels.count(lastLabel) != 1) {
163 mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine();
166 mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine();
170 //run last label if you need to
171 if (needToRun == true) {
172 if (order != NULL) { delete order; }
173 order = (input->getOrderVector(lastLabel));
175 mothurOut(order->getLabel()); mothurOutEndLine();
176 sabund = new SAbundVector();
177 *sabund = (order->getSAbundVector());
182 globaldata->gorder = NULL;
188 catch(exception& e) {
189 errorOut(e, "GetSAbundCommand", "execute");
194 //**********************************************************************************************************************