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){
16 //allow user to run help
17 if(option == "help") { help(); abort = true; }
20 //valid paramters for this command
21 string Array[] = {"list","label","output","outputdir","inputdir"};
22 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
24 OptionParser parser(option);
25 map<string,string> parameters = parser.getParameters();
27 ValidParameters validParameter;
28 map<string,string>::iterator it;
30 //check to make sure all parameters are valid for command
31 for (it = parameters.begin(); it != parameters.end(); it++) {
32 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
35 //if the user changes the input directory command factory will send this info to us in the output parameter
36 string inputDir = validParameter.validFile(parameters, "inputdir", false);
37 if (inputDir == "not found"){ inputDir = ""; }
40 it = parameters.find("list");
41 //user has given a template file
42 if(it != parameters.end()){
43 path = hasPath(it->second);
44 //if the user has not given a path then, add inputdir. else leave path alone.
45 if (path == "") { parameters["list"] = inputDir + it->second; }
49 listFile = validParameter.validFile(parameters, "list", true);
50 if (listFile == "not found") { mothurOut("list is a required parameter for the otu.hierarchy command."); mothurOutEndLine(); abort = true; }
51 else if (listFile == "not open") { abort = true; }
53 //if the user changes the output directory command factory will send this info to us in the output parameter
54 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
56 outputDir += hasPath(listFile); //if user entered a file with a path then preserve it
59 //check for optional parameter and set defaults
60 // ...at some point should added some additional type checking...
61 label = validParameter.validFile(parameters, "label", false);
62 if (label == "not found") { mothurOut("label is a required parameter for the otu.hierarchy command."); mothurOutEndLine(); abort = true; }
64 splitAtDash(label, labels);
65 if (labels.size() != 2) { mothurOut("You must provide 2 labels."); mothurOutEndLine(); abort = true; }
68 output = validParameter.validFile(parameters, "output", false); if (output == "not found") { output = "name"; }
70 if ((output != "name") && (output != "number")) { mothurOut("output options are name and number. I will use name."); mothurOutEndLine(); output = "name"; }
75 errorOut(e, "OtuHierarchyCommand", "OtuHierarchyCommand");
79 //**********************************************************************************************************************
81 void OtuHierarchyCommand::help(){
83 mothurOut("The otu.hierarchy command is used to see how otus relate at two distances. \n");
84 mothurOut("The otu.hierarchy command parameters are list, label and output. list and label parameters are required. \n");
85 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");
86 mothurOut("The otu.hierarchy command should be in the following format: \n");
87 mothurOut("otu.hierarchy(list=yourListFile, label=yourLabels).\n");
88 mothurOut("Example otu.hierarchy(list=amazon.fn.list, label=0.01-0.03).\n");
89 mothurOut("The otu.hierarchy command outputs a .otu.hierarchy file which is described on the wiki.\n");
90 mothurOut("Note: No spaces between parameter labels (i.e. list), '=' and parameters (i.e.yourListFile).\n\n");
93 errorOut(e, "OtuHierarchyCommand", "help");
98 //**********************************************************************************************************************
100 OtuHierarchyCommand::~OtuHierarchyCommand(){}
102 //**********************************************************************************************************************
104 int OtuHierarchyCommand::execute(){
107 if (abort == true) { return 0; }
109 //get listvectors that correspond to labels requested, (or use smart distancing to get closest listvector)
110 vector<ListVector> lists = getListVectors();
112 //determine which is little and which is big, putting little first
113 if (lists.size() == 2) {
114 //if big is first swap them
115 if (lists[0].getNumBins() < lists[1].getNumBins()) {
116 reverse(lists.begin(), lists.end());
119 mothurOut("error getting listvectors, unable to read 2 different vectors, check your label inputs."); mothurOutEndLine(); return 0;
122 //map sequences to bin number in the "little" otu
123 map<string, int> littleBins;
124 for (int i = 0; i < lists[0].getNumBins(); i++) {
125 string names = lists[0].get(i);
128 while (names.find_first_of(',') != -1) {
129 string name = names.substr(0,names.find_first_of(','));
130 names = names.substr(names.find_first_of(',')+1, names.length());
131 littleBins[name] = i;
135 littleBins[names] = i;
139 string outputFileName = outputDir + getRootName(getSimpleName(listFile)) + lists[0].getLabel() + "-" + lists[1].getLabel() + ".otu.hierarchy";
140 openOutputFile(outputFileName, out);
142 //go through each bin in "big" otu and output the bins in "little" otu which created it
143 for (int i = 0; i < lists[1].getNumBins(); i++) {
145 string names = lists[1].get(i);
148 if (output == "name") { out << names << '\t'; }
149 else { out << i << '\t'; }
151 map<int, int> bins; //bin numbers in little that are in this bin in big
152 map<int, int>::iterator it;
155 while (names.find_first_of(',') != -1) {
156 string name = names.substr(0,names.find_first_of(','));
157 names = names.substr(names.find_first_of(',')+1, names.length());
158 bins[littleBins[name]] = littleBins[name];
162 bins[littleBins[names]] = littleBins[names];
165 for (it = bins.begin(); it != bins.end(); it++) {
166 if (output == "name") { col2 += lists[0].get(it->first) + "\t"; }
167 else { col2 += toString(it->first) + "\t"; }
178 catch(exception& e) {
179 errorOut(e, "OtuHierarchyCommand", "execute");
184 //**********************************************************************************************************************
185 //returns a vector of listVectors where "little" vector is first
186 vector<ListVector> OtuHierarchyCommand::getListVectors() {
189 int pos; //to use in smart distancing, position of last read in file
191 vector<ListVector> lists;
193 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
194 set<string> processedLabels;
195 set<string> userLabels = labels;
199 openInputFile(listFile, in);
201 //get first list vector in file
202 ListVector* list = NULL;
203 string lastLabel = "";
207 list = new ListVector(in);
209 lastLabel = list->getLabel();
212 while ((list != NULL) && (userLabels.size() != 0)) {
214 //is this a listvector that we want?
215 if(labels.count(list->getLabel()) == 1){
217 //make copy of listvector
218 ListVector temp(*list);
219 lists.push_back(temp);
221 processedLabels.insert(list->getLabel());
222 userLabels.erase(list->getLabel());
225 //you have a label the user want that is smaller than this label and the last label has not already been processed
226 if ((anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
227 string saveLabel = list->getLabel();
228 int savePos = in.tellg();
230 //get smart distance line
234 list = new ListVector(in);
235 }else { list = NULL; }
237 //make copy of listvector
238 ListVector temp(*list);
239 lists.push_back(temp);
241 processedLabels.insert(list->getLabel());
242 userLabels.erase(list->getLabel());
244 //restore real lastlabel to save below
245 list->setLabel(saveLabel);
249 lastLabel = list->getLabel();
256 list = new ListVector(in);
258 }else { list = NULL; }
263 //output error messages about any remaining user labels
264 set<string>::iterator it;
265 bool needToRun = false;
266 for (it = userLabels.begin(); it != userLabels.end(); it++) {
267 mothurOut("Your file does not include the label " + *it);
268 if (processedLabels.count(lastLabel) != 1) {
269 mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine();
272 mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine();
276 //run last label if you need to
277 if (needToRun == true) {
278 if (list != NULL) { delete list; }
282 list = new ListVector(in);
284 //make copy of listvector
285 ListVector temp(*list);
286 lists.push_back(temp);
295 catch(exception& e) {
296 errorOut(e, "OtuHierarchyCommand", "getListVectors");
301 //**********************************************************************************************************************