2 * otuhierarchycommand.cpp
5 * Created by westcott on 1/19/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "otuhierarchycommand.h"
11 #include "inputdata.h"
13 //**********************************************************************************************************************
14 vector<string> OtuHierarchyCommand::setParameters(){
16 CommandParameter poutput("output", "Multiple", "name-number", "name", "", "", "","",false,false); parameters.push_back(poutput);
17 CommandParameter plist("list", "InputTypes", "", "", "none", "none", "none","otuheirarchy",false,true,true); parameters.push_back(plist);
18 CommandParameter plabel("label", "String", "", "", "", "", "","",false,false); parameters.push_back(plabel);
19 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
20 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
22 vector<string> myArray;
23 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
27 m->errorOut(e, "OtuHierarchyCommand", "setParameters");
31 //**********************************************************************************************************************
32 string OtuHierarchyCommand::getHelpString(){
34 string helpString = "";
35 helpString += "The otu.hierarchy command is used to see how otus relate at two distances. \n";
36 helpString += "The otu.hierarchy command parameters are list, label and output. list and label parameters are required. \n";
37 helpString += "The output parameter allows you to output the names of the sequence in the OTUs or the OTU numbers. Options are name and number, default is name. \n";
38 helpString += "The otu.hierarchy command should be in the following format: \n";
39 helpString += "otu.hierarchy(list=yourListFile, label=yourLabels).\n";
40 helpString += "Example otu.hierarchy(list=amazon.fn.list, label=0.01-0.03).\n";
41 helpString += "The otu.hierarchy command outputs a .otu.hierarchy file which is described on the wiki.\n";
42 helpString += "Note: No spaces between parameter labels (i.e. list), '=' and parameters (i.e.yourListFile).\n";
46 m->errorOut(e, "OtuHierarchyCommand", "getHelpString");
50 //**********************************************************************************************************************
51 string OtuHierarchyCommand::getOutputPattern(string type) {
55 if (type == "otuheirarchy") { pattern = "[filename],[distance1],[tag],[distance2],otu.hierarchy"; }
56 else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; }
61 m->errorOut(e, "OtuHierarchyCommand", "getOutputPattern");
65 //**********************************************************************************************************************
66 OtuHierarchyCommand::OtuHierarchyCommand(){
68 abort = true; calledHelp = true;
70 vector<string> tempOutNames;
71 outputTypes["otuheirarchy"] = tempOutNames;
74 m->errorOut(e, "OtuHierarchyCommand", "OtuHierarchyCommand");
78 //**********************************************************************************************************************
79 OtuHierarchyCommand::OtuHierarchyCommand(string option) {
81 abort = false; calledHelp = false;
83 //allow user to run help
84 if(option == "help") { help(); abort = true; calledHelp = true; }
85 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
88 vector<string> myArray = setParameters();
90 OptionParser parser(option);
91 map<string,string> parameters = parser.getParameters();
93 ValidParameters validParameter;
94 map<string,string>::iterator it;
96 //check to make sure all parameters are valid for command
97 for (it = parameters.begin(); it != parameters.end(); it++) {
98 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
101 //initialize outputTypes
102 vector<string> tempOutNames;
103 outputTypes["otuheirarchy"] = tempOutNames;
105 //if the user changes the input directory command factory will send this info to us in the output parameter
106 string inputDir = validParameter.validFile(parameters, "inputdir", false);
107 if (inputDir == "not found"){ inputDir = ""; }
110 it = parameters.find("list");
111 //user has given a template file
112 if(it != parameters.end()){
113 path = m->hasPath(it->second);
114 //if the user has not given a path then, add inputdir. else leave path alone.
115 if (path == "") { parameters["list"] = inputDir + it->second; }
119 listFile = validParameter.validFile(parameters, "list", true);
120 if (listFile == "not found") {
121 listFile = m->getListFile();
122 if (listFile != "") { m->mothurOut("Using " + listFile + " as input file for the list parameter."); m->mothurOutEndLine(); }
124 m->mothurOut("No valid current list file. You must provide a list file."); m->mothurOutEndLine();
127 }else if (listFile == "not open") { abort = true; }
128 else { m->setListFile(listFile); }
130 //if the user changes the output directory command factory will send this info to us in the output parameter
131 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
133 outputDir += m->hasPath(listFile); //if user entered a file with a path then preserve it
136 //check for optional parameter and set defaults
137 // ...at some point should added some additional type checking...
138 label = validParameter.validFile(parameters, "label", false);
139 if (label == "not found") { m->mothurOut("label is a required parameter for the otu.hierarchy command."); m->mothurOutEndLine(); abort = true; }
141 m->splitAtDash(label, mylabels);
142 if (mylabels.size() != 2) { m->mothurOut("You must provide 2 labels."); m->mothurOutEndLine(); abort = true; }
145 output = validParameter.validFile(parameters, "output", false); if (output == "not found") { output = "name"; }
147 if ((output != "name") && (output != "number")) { m->mothurOut("output options are name and number. I will use name."); m->mothurOutEndLine(); output = "name"; }
151 catch(exception& e) {
152 m->errorOut(e, "OtuHierarchyCommand", "OtuHierarchyCommand");
156 //**********************************************************************************************************************
158 int OtuHierarchyCommand::execute(){
161 if (abort == true) { if (calledHelp) { return 0; } return 2; }
163 //get listvectors that correspond to labels requested, (or use smart distancing to get closest listvector)
164 vector< vector<string> > lists = getListVectors();
166 if (m->control_pressed) { outputTypes.clear(); return 0; }
168 //determine which is little and which is big, putting little first
169 if (lists.size() == 4) {
170 //if big is first swap them
171 if (lists[0].size() < lists[2].size()) {
172 vector< vector<string> > tempLists;
173 tempLists.push_back(lists[2]);
174 tempLists.push_back(lists[3]);
175 tempLists.push_back(lists[0]);
176 tempLists.push_back(lists[1]);
178 string tempLabel = list2Label;
179 list2Label = list1Label;
180 list1Label = tempLabel;
183 m->mothurOut("error getting listvectors, unable to read 2 different vectors, check your label inputs."); m->mothurOutEndLine(); return 0;
186 //map sequences to bin number in the "little" otu
187 map<string, int> littleBins;
188 vector<string> binLabels0 = lists[0];
189 for (int i = 0; i < lists[0].size(); i++) {
191 if (m->control_pressed) { return 0; }
192 string bin = lists[1][i];
193 vector<string> names; m->splitAtComma(bin, names);
194 for (int j = 0; j < names.size(); j++) { littleBins[names[j]] = i; }
198 map<string, string> variables;
199 variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(listFile));
200 variables["[distance1]"] = list1Label;
201 variables["[tag]"] = "-";
202 variables["[distance2]"] = list2Label;
203 string outputFileName = getOutputFileName("otuheirarchy",variables);
204 m->openOutputFile(outputFileName, out);
206 //go through each bin in "big" otu and output the bins in "little" otu which created it
207 vector<string> binLabels1 = lists[2];
208 for (int i = 0; i < lists[2].size(); i++) {
210 if (m->control_pressed) { outputTypes.clear(); out.close(); m->mothurRemove(outputFileName); return 0; }
212 string binnames = lists[3][i];
213 vector<string> names; m->splitAtComma(binnames, names);
216 if (output == "name") { out << binnames << '\t'; }
217 else { out << binLabels1[i] << '\t'; }
219 map<int, int> bins; //bin numbers in little that are in this bin in big
220 map<int, int>::iterator it;
223 for (int j = 0; j < names.size(); j++) { bins[littleBins[names[j]]] = littleBins[names[j]]; }
226 for (it = bins.begin(); it != bins.end(); it++) {
227 if (output == "name") { col2 += lists[1][it->first] + "\t"; }
228 else { col2 += binLabels0[it->first] + "\t"; }
237 if (m->control_pressed) { outputTypes.clear(); m->mothurRemove(outputFileName); return 0; }
239 m->mothurOutEndLine();
240 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
241 m->mothurOut(outputFileName); m->mothurOutEndLine(); outputNames.push_back(outputFileName); outputTypes["otuheirarchy"].push_back(outputFileName);
242 m->mothurOutEndLine();
246 catch(exception& e) {
247 m->errorOut(e, "OtuHierarchyCommand", "execute");
252 //**********************************************************************************************************************
253 //returns a vector of listVectors where "little" vector is first
254 vector< vector<string> > OtuHierarchyCommand::getListVectors() { //return value [0] -> otulabelsFirstLabel [1] -> binsFirstLabel [2] -> otulabelsSecondLabel [3] -> binsSecondLabel
256 vector< vector<string> > lists;
259 for (set<string>::iterator it = mylabels.begin(); it != mylabels.end(); it++) {
261 vector< vector<string> > thisList = getListVector(*it, realLabel);
263 if (m->control_pressed) { return lists; }
265 for (int i = 0; i < thisList.size(); i++) { lists.push_back(thisList[i]); }
267 if (count == 0) { list1Label = realLabel; count++; }
268 else { list2Label = realLabel; }
273 catch(exception& e) {
274 m->errorOut(e, "OtuHierarchyCommand", "getListVectors");
278 //**********************************************************************************************************************
279 vector< vector<string> > OtuHierarchyCommand::getListVector(string label, string& realLabel){ //return value [0] -> otulabels [1] -> bins
281 vector< vector<string> > myList;
283 InputData input(listFile, "list");
284 ListVector* list = input.getListVector();
285 string lastLabel = list->getLabel();
287 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
288 set<string> labels; labels.insert(label);
289 set<string> processedLabels;
290 set<string> userLabels = labels;
292 //as long as you are not at the end of the file or done wih the lines you want
293 while((list != NULL) && (userLabels.size() != 0)) {
294 if (m->control_pressed) { return myList; }
296 if(labels.count(list->getLabel()) == 1){
297 processedLabels.insert(list->getLabel());
298 userLabels.erase(list->getLabel());
302 if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
303 string saveLabel = list->getLabel();
306 list = input.getListVector(lastLabel);
308 processedLabels.insert(list->getLabel());
309 userLabels.erase(list->getLabel());
311 //restore real lastlabel to save below
312 //list->setLabel(saveLabel);
316 lastLabel = list->getLabel();
318 //get next line to process
319 //prevent memory leak
321 list = input.getListVector();
325 if (m->control_pressed) { return myList; }
327 //output error messages about any remaining user labels
328 set<string>::iterator it;
329 bool needToRun = false;
330 for (it = userLabels.begin(); it != userLabels.end(); it++) {
331 m->mothurOut("Your file does not include the label " + *it);
332 if (processedLabels.count(lastLabel) != 1) {
333 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
336 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
340 //run last label if you need to
341 if (needToRun == true) {
343 list = input.getListVector(lastLabel);
346 //at this point the list vector has the right distance
347 myList.push_back(list->getLabels());
349 for (int i = 0; i < list->getNumBins(); i++) {
350 if (m->control_pressed) { return myList; }
351 bins.push_back(list->get(i));
353 myList.push_back(bins);
354 realLabel = list->getLabel();
360 catch(exception& e) {
361 m->errorOut(e, "OtuHierarchyCommand", "getListVector");
366 //**********************************************************************************************************************