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 = 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 += 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 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 //determine which is little and which is big, putting little first
114 if (lists.size() == 2) {
115 //if big is first swap them
116 if (lists[0].getNumBins() < lists[1].getNumBins()) {
117 reverse(lists.begin(), lists.end());
120 m->mothurOut("error getting listvectors, unable to read 2 different vectors, check your label inputs."); m->mothurOutEndLine(); return 0;
123 //map sequences to bin number in the "little" otu
124 map<string, int> littleBins;
125 for (int i = 0; i < lists[0].getNumBins(); i++) {
126 string names = lists[0].get(i);
129 while (names.find_first_of(',') != -1) {
130 string name = names.substr(0,names.find_first_of(','));
131 names = names.substr(names.find_first_of(',')+1, names.length());
132 littleBins[name] = i;
136 littleBins[names] = i;
140 string outputFileName = outputDir + getRootName(getSimpleName(listFile)) + lists[0].getLabel() + "-" + lists[1].getLabel() + ".otu.hierarchy";
141 openOutputFile(outputFileName, out);
143 //go through each bin in "big" otu and output the bins in "little" otu which created it
144 for (int i = 0; i < lists[1].getNumBins(); i++) {
146 string names = lists[1].get(i);
149 if (output == "name") { out << names << '\t'; }
150 else { out << i << '\t'; }
152 map<int, int> bins; //bin numbers in little that are in this bin in big
153 map<int, int>::iterator it;
156 while (names.find_first_of(',') != -1) {
157 string name = names.substr(0,names.find_first_of(','));
158 names = names.substr(names.find_first_of(',')+1, names.length());
159 bins[littleBins[name]] = littleBins[name];
163 bins[littleBins[names]] = littleBins[names];
166 for (it = bins.begin(); it != bins.end(); it++) {
167 if (output == "name") { col2 += lists[0].get(it->first) + "\t"; }
168 else { col2 += toString(it->first) + "\t"; }
177 m->mothurOutEndLine();
178 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
179 m->mothurOut(outputFileName); m->mothurOutEndLine();
180 m->mothurOutEndLine();
184 catch(exception& e) {
185 m->errorOut(e, "OtuHierarchyCommand", "execute");
190 //**********************************************************************************************************************
191 //returns a vector of listVectors where "little" vector is first
192 vector<ListVector> OtuHierarchyCommand::getListVectors() {
195 int pos; //to use in smart distancing, position of last read in file
197 vector<ListVector> lists;
199 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
200 set<string> processedLabels;
201 set<string> userLabels = labels;
205 openInputFile(listFile, in);
207 //get first list vector in file
208 ListVector* list = NULL;
209 string lastLabel = "";
213 list = new ListVector(in);
215 lastLabel = list->getLabel();
218 while ((list != NULL) && (userLabels.size() != 0)) {
220 //is this a listvector that we want?
221 if(labels.count(list->getLabel()) == 1){
223 //make copy of listvector
224 ListVector temp(*list);
225 lists.push_back(temp);
227 processedLabels.insert(list->getLabel());
228 userLabels.erase(list->getLabel());
231 //you have a label the user want that is smaller than this label and the last label has not already been processed
232 if ((anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
233 string saveLabel = list->getLabel();
234 int savePos = in.tellg();
236 //get smart distance line
240 list = new ListVector(in);
241 }else { list = NULL; }
243 //make copy of listvector
244 ListVector temp(*list);
245 lists.push_back(temp);
247 processedLabels.insert(list->getLabel());
248 userLabels.erase(list->getLabel());
250 //restore real lastlabel to save below
251 list->setLabel(saveLabel);
255 lastLabel = list->getLabel();
262 list = new ListVector(in);
264 }else { list = NULL; }
269 //output error messages about any remaining user labels
270 set<string>::iterator it;
271 bool needToRun = false;
272 for (it = userLabels.begin(); it != userLabels.end(); it++) {
273 m->mothurOut("Your file does not include the label " + *it);
274 if (processedLabels.count(lastLabel) != 1) {
275 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
278 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
282 //run last label if you need to
283 if (needToRun == true) {
284 if (list != NULL) { delete list; }
288 list = new ListVector(in);
290 //make copy of listvector
291 ListVector temp(*list);
292 lists.push_back(temp);
301 catch(exception& e) {
302 m->errorOut(e, "OtuHierarchyCommand", "getListVectors");
307 //**********************************************************************************************************************