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 OtuHierarchyCommand::OtuHierarchyCommand(string option) {
17 //allow user to run help
18 if(option == "help") { help(); abort = true; }
21 //valid paramters for this command
22 string Array[] = {"list","label","output","outputdir","inputdir"};
23 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
25 OptionParser parser(option);
26 map<string,string> parameters = parser.getParameters();
28 ValidParameters validParameter;
29 map<string,string>::iterator it;
31 //check to make sure all parameters are valid for command
32 for (it = parameters.begin(); it != parameters.end(); it++) {
33 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
36 //if the user changes the input directory command factory will send this info to us in the output parameter
37 string inputDir = validParameter.validFile(parameters, "inputdir", false);
38 if (inputDir == "not found"){ inputDir = ""; }
41 it = parameters.find("list");
42 //user has given a template file
43 if(it != parameters.end()){
44 path = m->hasPath(it->second);
45 //if the user has not given a path then, add inputdir. else leave path alone.
46 if (path == "") { parameters["list"] = inputDir + it->second; }
50 listFile = validParameter.validFile(parameters, "list", true);
51 if (listFile == "not found") { m->mothurOut("list is a required parameter for the otu.hierarchy command."); m->mothurOutEndLine(); abort = true; }
52 else if (listFile == "not open") { abort = true; }
54 //if the user changes the output directory command factory will send this info to us in the output parameter
55 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
57 outputDir += m->hasPath(listFile); //if user entered a file with a path then preserve it
60 //check for optional parameter and set defaults
61 // ...at some point should added some additional type checking...
62 label = validParameter.validFile(parameters, "label", false);
63 if (label == "not found") { m->mothurOut("label is a required parameter for the otu.hierarchy command."); m->mothurOutEndLine(); abort = true; }
65 m->splitAtDash(label, labels);
66 if (labels.size() != 2) { m->mothurOut("You must provide 2 labels."); m->mothurOutEndLine(); abort = true; }
69 output = validParameter.validFile(parameters, "output", false); if (output == "not found") { output = "name"; }
71 if ((output != "name") && (output != "number")) { m->mothurOut("output options are name and number. I will use name."); m->mothurOutEndLine(); output = "name"; }
76 m->errorOut(e, "OtuHierarchyCommand", "OtuHierarchyCommand");
80 //**********************************************************************************************************************
82 void OtuHierarchyCommand::help(){
84 m->mothurOut("The otu.hierarchy command is used to see how otus relate at two distances. \n");
85 m->mothurOut("The otu.hierarchy command parameters are list, label and output. list and label parameters are required. \n");
86 m->mothurOut("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");
87 m->mothurOut("The otu.hierarchy command should be in the following format: \n");
88 m->mothurOut("otu.hierarchy(list=yourListFile, label=yourLabels).\n");
89 m->mothurOut("Example otu.hierarchy(list=amazon.fn.list, label=0.01-0.03).\n");
90 m->mothurOut("The otu.hierarchy command outputs a .otu.hierarchy file which is described on the wiki.\n");
91 m->mothurOut("Note: No spaces between parameter labels (i.e. list), '=' and parameters (i.e.yourListFile).\n\n");
94 m->errorOut(e, "OtuHierarchyCommand", "help");
99 //**********************************************************************************************************************
101 OtuHierarchyCommand::~OtuHierarchyCommand(){}
103 //**********************************************************************************************************************
105 int OtuHierarchyCommand::execute(){
108 if (abort == true) { return 0; }
110 //get listvectors that correspond to labels requested, (or use smart distancing to get closest listvector)
111 vector<ListVector> lists = getListVectors();
113 if (m->control_pressed) { return 0; }
115 //determine which is little and which is big, putting little first
116 if (lists.size() == 2) {
117 //if big is first swap them
118 if (lists[0].getNumBins() < lists[1].getNumBins()) {
119 reverse(lists.begin(), lists.end());
122 m->mothurOut("error getting listvectors, unable to read 2 different vectors, check your label inputs."); m->mothurOutEndLine(); return 0;
125 //map sequences to bin number in the "little" otu
126 map<string, int> littleBins;
127 for (int i = 0; i < lists[0].getNumBins(); i++) {
129 if (m->control_pressed) { return 0; }
131 string names = lists[0].get(i);
134 while (names.find_first_of(',') != -1) {
135 string name = names.substr(0,names.find_first_of(','));
136 names = names.substr(names.find_first_of(',')+1, names.length());
137 littleBins[name] = i;
141 littleBins[names] = i;
145 string outputFileName = outputDir + m->getRootName(m->getSimpleName(listFile)) + lists[0].getLabel() + "-" + lists[1].getLabel() + ".otu.hierarchy";
146 m->openOutputFile(outputFileName, out);
148 //go through each bin in "big" otu and output the bins in "little" otu which created it
149 for (int i = 0; i < lists[1].getNumBins(); i++) {
151 if (m->control_pressed) { out.close(); remove(outputFileName.c_str()); return 0; }
153 string names = lists[1].get(i);
156 if (output == "name") { out << names << '\t'; }
157 else { out << i << '\t'; }
159 map<int, int> bins; //bin numbers in little that are in this bin in big
160 map<int, int>::iterator it;
163 while (names.find_first_of(',') != -1) {
164 string name = names.substr(0,names.find_first_of(','));
165 names = names.substr(names.find_first_of(',')+1, names.length());
166 bins[littleBins[name]] = littleBins[name];
170 bins[littleBins[names]] = littleBins[names];
173 for (it = bins.begin(); it != bins.end(); it++) {
174 if (output == "name") { col2 += lists[0].get(it->first) + "\t"; }
175 else { col2 += toString(it->first) + "\t"; }
184 if (m->control_pressed) { remove(outputFileName.c_str()); return 0; }
186 m->mothurOutEndLine();
187 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
188 m->mothurOut(outputFileName); m->mothurOutEndLine();
189 m->mothurOutEndLine();
193 catch(exception& e) {
194 m->errorOut(e, "OtuHierarchyCommand", "execute");
199 //**********************************************************************************************************************
200 //returns a vector of listVectors where "little" vector is first
201 vector<ListVector> OtuHierarchyCommand::getListVectors() {
204 int pos; //to use in smart distancing, position of last read in file
206 vector<ListVector> lists;
208 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
209 set<string> processedLabels;
210 set<string> userLabels = labels;
214 m->openInputFile(listFile, in);
216 //get first list vector in file
217 ListVector* list = NULL;
218 string lastLabel = "";
222 list = new ListVector(in);
224 lastLabel = list->getLabel();
227 while ((list != NULL) && (userLabels.size() != 0)) {
229 if (m->control_pressed) { in.close(); delete list; return lists; }
231 //is this a listvector that we want?
232 if(labels.count(list->getLabel()) == 1){
234 //make copy of listvector
235 ListVector temp(*list);
236 lists.push_back(temp);
238 processedLabels.insert(list->getLabel());
239 userLabels.erase(list->getLabel());
242 //you have a label the user want that is smaller than this label and the last label has not already been processed
243 if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
244 string saveLabel = list->getLabel();
245 int savePos = in.tellg();
247 //get smart distance line
251 list = new ListVector(in);
252 }else { list = NULL; }
254 //make copy of listvector
255 ListVector temp(*list);
256 lists.push_back(temp);
258 processedLabels.insert(list->getLabel());
259 userLabels.erase(list->getLabel());
261 //restore real lastlabel to save below
262 list->setLabel(saveLabel);
266 lastLabel = list->getLabel();
273 list = new ListVector(in);
275 }else { list = NULL; }
278 if (m->control_pressed) { in.close(); return lists; }
280 //output error messages about any remaining user labels
281 set<string>::iterator it;
282 bool needToRun = false;
283 for (it = userLabels.begin(); it != userLabels.end(); it++) {
284 m->mothurOut("Your file does not include the label " + *it);
285 if (processedLabels.count(lastLabel) != 1) {
286 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
289 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
293 if (m->control_pressed) { in.close(); return lists; }
295 //run last label if you need to
296 if (needToRun == true) {
297 if (list != NULL) { delete list; }
301 list = new ListVector(in);
303 //make copy of listvector
304 ListVector temp(*list);
305 lists.push_back(temp);
314 catch(exception& e) {
315 m->errorOut(e, "OtuHierarchyCommand", "getListVectors");
320 //**********************************************************************************************************************