2 * otuhierarchycommand.cpp
5 * Created by westcott on 1/19/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "otuhierarchycommand.h"
12 //**********************************************************************************************************************
13 vector<string> OtuHierarchyCommand::setParameters(){
15 CommandParameter poutput("output", "Multiple", "name-number", "name", "", "", "",false,false); parameters.push_back(poutput);
16 CommandParameter plist("list", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(plist);
17 CommandParameter plabel("label", "String", "", "", "", "", "",false,false); parameters.push_back(plabel);
18 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
19 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
21 vector<string> myArray;
22 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
26 m->errorOut(e, "OtuHierarchyCommand", "setParameters");
30 //**********************************************************************************************************************
31 string OtuHierarchyCommand::getHelpString(){
33 string helpString = "";
34 helpString += "The otu.hierarchy command is used to see how otus relate at two distances. \n";
35 helpString += "The otu.hierarchy command parameters are list, label and output. list and label parameters are required. \n";
36 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";
37 helpString += "The otu.hierarchy command should be in the following format: \n";
38 helpString += "otu.hierarchy(list=yourListFile, label=yourLabels).\n";
39 helpString += "Example otu.hierarchy(list=amazon.fn.list, label=0.01-0.03).\n";
40 helpString += "The otu.hierarchy command outputs a .otu.hierarchy file which is described on the wiki.\n";
41 helpString += "Note: No spaces between parameter labels (i.e. list), '=' and parameters (i.e.yourListFile).\n";
45 m->errorOut(e, "OtuHierarchyCommand", "getHelpString");
50 //**********************************************************************************************************************
51 OtuHierarchyCommand::OtuHierarchyCommand(){
53 abort = true; calledHelp = true;
55 vector<string> tempOutNames;
56 outputTypes["otuheirarchy"] = tempOutNames;
59 m->errorOut(e, "OtuHierarchyCommand", "OtuHierarchyCommand");
63 //**********************************************************************************************************************
64 OtuHierarchyCommand::OtuHierarchyCommand(string option) {
66 abort = false; calledHelp = false;
68 //allow user to run help
69 if(option == "help") { help(); abort = true; calledHelp = true; }
72 vector<string> myArray = setParameters();
74 OptionParser parser(option);
75 map<string,string> parameters = parser.getParameters();
77 ValidParameters validParameter;
78 map<string,string>::iterator it;
80 //check to make sure all parameters are valid for command
81 for (it = parameters.begin(); it != parameters.end(); it++) {
82 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
85 //initialize outputTypes
86 vector<string> tempOutNames;
87 outputTypes["otuheirarchy"] = tempOutNames;
89 //if the user changes the input directory command factory will send this info to us in the output parameter
90 string inputDir = validParameter.validFile(parameters, "inputdir", false);
91 if (inputDir == "not found"){ inputDir = ""; }
94 it = parameters.find("list");
95 //user has given a template file
96 if(it != parameters.end()){
97 path = m->hasPath(it->second);
98 //if the user has not given a path then, add inputdir. else leave path alone.
99 if (path == "") { parameters["list"] = inputDir + it->second; }
103 listFile = validParameter.validFile(parameters, "list", true);
104 if (listFile == "not found") {
105 listFile = m->getListFile();
106 if (listFile != "") { m->mothurOut("Using " + listFile + " as input file for the list parameter."); m->mothurOutEndLine(); }
108 m->mothurOut("No valid current list file. You must provide a list file."); m->mothurOutEndLine();
111 }else if (listFile == "not open") { abort = true; }
113 //if the user changes the output directory command factory will send this info to us in the output parameter
114 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
116 outputDir += m->hasPath(listFile); //if user entered a file with a path then preserve it
119 //check for optional parameter and set defaults
120 // ...at some point should added some additional type checking...
121 label = validParameter.validFile(parameters, "label", false);
122 if (label == "not found") { m->mothurOut("label is a required parameter for the otu.hierarchy command."); m->mothurOutEndLine(); abort = true; }
124 m->splitAtDash(label, labels);
125 if (labels.size() != 2) { m->mothurOut("You must provide 2 labels."); m->mothurOutEndLine(); abort = true; }
128 output = validParameter.validFile(parameters, "output", false); if (output == "not found") { output = "name"; }
130 if ((output != "name") && (output != "number")) { m->mothurOut("output options are name and number. I will use name."); m->mothurOutEndLine(); output = "name"; }
134 catch(exception& e) {
135 m->errorOut(e, "OtuHierarchyCommand", "OtuHierarchyCommand");
139 //**********************************************************************************************************************
141 int OtuHierarchyCommand::execute(){
144 if (abort == true) { if (calledHelp) { return 0; } return 2; }
146 //get listvectors that correspond to labels requested, (or use smart distancing to get closest listvector)
147 vector<ListVector> lists = getListVectors();
149 if (m->control_pressed) { outputTypes.clear(); return 0; }
151 //determine which is little and which is big, putting little first
152 if (lists.size() == 2) {
153 //if big is first swap them
154 if (lists[0].getNumBins() < lists[1].getNumBins()) {
155 reverse(lists.begin(), lists.end());
158 m->mothurOut("error getting listvectors, unable to read 2 different vectors, check your label inputs."); m->mothurOutEndLine(); return 0;
161 //map sequences to bin number in the "little" otu
162 map<string, int> littleBins;
163 for (int i = 0; i < lists[0].getNumBins(); i++) {
165 if (m->control_pressed) { return 0; }
167 string names = lists[0].get(i);
170 while (names.find_first_of(',') != -1) {
171 string name = names.substr(0,names.find_first_of(','));
172 names = names.substr(names.find_first_of(',')+1, names.length());
173 littleBins[name] = i;
177 littleBins[names] = i;
181 string outputFileName = outputDir + m->getRootName(m->getSimpleName(listFile)) + lists[0].getLabel() + "-" + lists[1].getLabel() + ".otu.hierarchy";
182 m->openOutputFile(outputFileName, out);
184 //go through each bin in "big" otu and output the bins in "little" otu which created it
185 for (int i = 0; i < lists[1].getNumBins(); i++) {
187 if (m->control_pressed) { outputTypes.clear(); out.close(); remove(outputFileName.c_str()); return 0; }
189 string names = lists[1].get(i);
192 if (output == "name") { out << names << '\t'; }
193 else { out << i << '\t'; }
195 map<int, int> bins; //bin numbers in little that are in this bin in big
196 map<int, int>::iterator it;
199 while (names.find_first_of(',') != -1) {
200 string name = names.substr(0,names.find_first_of(','));
201 names = names.substr(names.find_first_of(',')+1, names.length());
202 bins[littleBins[name]] = littleBins[name];
206 bins[littleBins[names]] = littleBins[names];
209 for (it = bins.begin(); it != bins.end(); it++) {
210 if (output == "name") { col2 += lists[0].get(it->first) + "\t"; }
211 else { col2 += toString(it->first) + "\t"; }
220 if (m->control_pressed) { outputTypes.clear(); remove(outputFileName.c_str()); return 0; }
222 m->mothurOutEndLine();
223 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
224 m->mothurOut(outputFileName); m->mothurOutEndLine(); outputNames.push_back(outputFileName); outputTypes["otuheirarchy"].push_back(outputFileName);
225 m->mothurOutEndLine();
229 catch(exception& e) {
230 m->errorOut(e, "OtuHierarchyCommand", "execute");
235 //**********************************************************************************************************************
236 //returns a vector of listVectors where "little" vector is first
237 vector<ListVector> OtuHierarchyCommand::getListVectors() {
240 int pos; //to use in smart distancing, position of last read in file
242 vector<ListVector> lists;
244 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
245 set<string> processedLabels;
246 set<string> userLabels = labels;
250 m->openInputFile(listFile, in);
252 //get first list vector in file
253 ListVector* list = NULL;
254 string lastLabel = "";
258 list = new ListVector(in);
260 lastLabel = list->getLabel();
263 while ((list != NULL) && (userLabels.size() != 0)) {
265 if (m->control_pressed) { in.close(); delete list; return lists; }
267 //is this a listvector that we want?
268 if(labels.count(list->getLabel()) == 1){
270 //make copy of listvector
271 ListVector temp(*list);
272 lists.push_back(temp);
274 processedLabels.insert(list->getLabel());
275 userLabels.erase(list->getLabel());
278 //you have a label the user want that is smaller than this label and the last label has not already been processed
279 if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
280 string saveLabel = list->getLabel();
281 int savePos = in.tellg();
283 //get smart distance line
287 list = new ListVector(in);
288 }else { list = NULL; }
290 //make copy of listvector
291 ListVector temp(*list);
292 lists.push_back(temp);
294 processedLabels.insert(list->getLabel());
295 userLabels.erase(list->getLabel());
297 //restore real lastlabel to save below
298 list->setLabel(saveLabel);
302 lastLabel = list->getLabel();
309 list = new ListVector(in);
311 }else { list = NULL; }
314 if (m->control_pressed) { in.close(); return lists; }
316 //output error messages about any remaining user labels
317 set<string>::iterator it;
318 bool needToRun = false;
319 for (it = userLabels.begin(); it != userLabels.end(); it++) {
320 m->mothurOut("Your file does not include the label " + *it);
321 if (processedLabels.count(lastLabel) != 1) {
322 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
325 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
329 if (m->control_pressed) { in.close(); return lists; }
331 //run last label if you need to
332 if (needToRun == true) {
333 if (list != NULL) { delete list; }
337 list = new ListVector(in);
339 //make copy of listvector
340 ListVector temp(*list);
341 lists.push_back(temp);
350 catch(exception& e) {
351 m->errorOut(e, "OtuHierarchyCommand", "getListVectors");
356 //**********************************************************************************************************************