]> git.donarmstrong.com Git - mothur.git/blob - otuhierarchycommand.cpp
working on pam
[mothur.git] / otuhierarchycommand.cpp
1 /*
2  *  otuhierarchycommand.cpp
3  *  Mothur
4  *
5  *  Created by westcott on 1/19/10.
6  *  Copyright 2010 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "otuhierarchycommand.h"
11
12 //**********************************************************************************************************************
13 vector<string> OtuHierarchyCommand::setParameters(){    
14         try {
15                 CommandParameter poutput("output", "Multiple", "name-number", "name", "", "", "","",false,false); parameters.push_back(poutput);
16                 CommandParameter plist("list", "InputTypes", "", "", "none", "none", "none","otuheirarchy",false,true,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);
20                 
21                 vector<string> myArray;
22                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
23                 return myArray;
24         }
25         catch(exception& e) {
26                 m->errorOut(e, "OtuHierarchyCommand", "setParameters");
27                 exit(1);
28         }
29 }
30 //**********************************************************************************************************************
31 string OtuHierarchyCommand::getHelpString(){    
32         try {
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";
42                 return helpString;
43         }
44         catch(exception& e) {
45                 m->errorOut(e, "OtuHierarchyCommand", "getHelpString");
46                 exit(1);
47         }
48 }
49 //**********************************************************************************************************************
50 string OtuHierarchyCommand::getOutputPattern(string type) {
51     try {
52         string pattern = "";
53         
54         if (type == "otuheirarchy") {  pattern = "[filename],[distance1],[tag],[distance2],otu.hierarchy"; } 
55         else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true;  }
56         
57         return pattern;
58     }
59     catch(exception& e) {
60         m->errorOut(e, "OtuHierarchyCommand", "getOutputPattern");
61         exit(1);
62     }
63 }
64 //**********************************************************************************************************************
65 OtuHierarchyCommand::OtuHierarchyCommand(){     
66         try {
67                 abort = true; calledHelp = true; 
68                 setParameters();
69                 vector<string> tempOutNames;
70                 outputTypes["otuheirarchy"] = tempOutNames;
71         }
72         catch(exception& e) {
73                 m->errorOut(e, "OtuHierarchyCommand", "OtuHierarchyCommand");
74                 exit(1);
75         }
76 }
77 //**********************************************************************************************************************
78 OtuHierarchyCommand::OtuHierarchyCommand(string option) {
79         try {
80                 abort = false; calledHelp = false;   
81                 
82                 //allow user to run help
83                 if(option == "help") {  help(); abort = true; calledHelp = true; }
84                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
85                 
86                 else {
87                         vector<string> myArray = setParameters();
88                         
89                         OptionParser parser(option);
90                         map<string,string> parameters = parser.getParameters();
91                         
92                         ValidParameters validParameter;
93                         map<string,string>::iterator it;
94                 
95                         //check to make sure all parameters are valid for command
96                         for (it = parameters.begin(); it != parameters.end(); it++) { 
97                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
98                         }
99                         
100                         //initialize outputTypes
101                         vector<string> tempOutNames;
102                         outputTypes["otuheirarchy"] = tempOutNames;
103                         
104                         //if the user changes the input directory command factory will send this info to us in the output parameter 
105                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
106                         if (inputDir == "not found"){   inputDir = "";          }
107                         else {
108                                 string path;
109                                 it = parameters.find("list");
110                                 //user has given a template file
111                                 if(it != parameters.end()){ 
112                                         path = m->hasPath(it->second);
113                                         //if the user has not given a path then, add inputdir. else leave path alone.
114                                         if (path == "") {       parameters["list"] = inputDir + it->second;             }
115                                 }
116                         }
117
118                         listFile = validParameter.validFile(parameters, "list", true);
119                         if (listFile == "not found") { 
120                                 listFile = m->getListFile(); 
121                                 if (listFile != "") {  m->mothurOut("Using " + listFile + " as input file for the list parameter."); m->mothurOutEndLine(); }
122                                 else { 
123                                         m->mothurOut("No valid current list file. You must provide a list file."); m->mothurOutEndLine(); 
124                                         abort = true;
125                                 }
126                         }else if (listFile == "not open") { abort = true; }     
127                         else { m->setListFile(listFile); }
128                         
129                         //if the user changes the output directory command factory will send this info to us in the output parameter 
130                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
131                                 outputDir = ""; 
132                                 outputDir += m->hasPath(listFile); //if user entered a file with a path then preserve it        
133                         }
134                         
135                         //check for optional parameter and set defaults
136                         // ...at some point should added some additional type checking...
137                         label = validParameter.validFile(parameters, "label", false);                   
138                         if (label == "not found") { m->mothurOut("label is a required parameter for the otu.hierarchy command."); m->mothurOutEndLine(); abort = true; }
139                         else { 
140                                 m->splitAtDash(label, labels);
141                                 if (labels.size() != 2) { m->mothurOut("You must provide 2 labels."); m->mothurOutEndLine(); abort = true; }
142                         }       
143                         
144                         output = validParameter.validFile(parameters, "output", false);                 if (output == "not found") { output = "name"; }
145                         
146                         if ((output != "name") && (output != "number")) { m->mothurOut("output options are name and number. I will use name."); m->mothurOutEndLine(); output = "name"; }
147                 }
148                 
149         }
150         catch(exception& e) {
151                 m->errorOut(e, "OtuHierarchyCommand", "OtuHierarchyCommand");
152                 exit(1);
153         }                       
154 }
155 //**********************************************************************************************************************
156
157 int OtuHierarchyCommand::execute(){
158         try {
159                 
160                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
161                 
162                 //get listvectors that correspond to labels requested, (or use smart distancing to get closest listvector)
163                 vector<ListVector> lists = getListVectors();
164                 
165                 if (m->control_pressed) { outputTypes.clear(); return 0; }
166                 
167                 //determine which is little and which is big, putting little first
168                 if (lists.size() == 2) {
169                         //if big is first swap them
170                         if (lists[0].getNumBins() < lists[1].getNumBins()) {
171                                 reverse(lists.begin(), lists.end());
172                         }
173                 }else{
174                         m->mothurOut("error getting listvectors, unable to read 2 different vectors, check your label inputs."); m->mothurOutEndLine(); return 0;
175                 }
176                 
177                 //map sequences to bin number in the "little" otu
178                 map<string, int> littleBins;
179         vector<string> binLabels0 = lists[0].getLabels();
180                 for (int i = 0; i < lists[0].getNumBins(); i++) {
181                 
182                         if (m->control_pressed) {  return 0; }
183                         string bin = lists[0].get(i);
184             vector<string> names; m->splitAtComma(bin, names);
185                         for (int j = 0; j < names.size(); j++) { littleBins[names[j]] = i; }
186         }
187                 
188                 ofstream out;
189         map<string, string> variables; 
190         variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(listFile));
191         variables["[distance1]"] = lists[0].getLabel();
192         variables["[tag]"] = "-"; 
193         variables["[distance2]"] = lists[1].getLabel();
194                 string outputFileName = getOutputFileName("otuheirarchy",variables);
195                 m->openOutputFile(outputFileName, out);
196                 
197                 //go through each bin in "big" otu and output the bins in "little" otu which created it
198         vector<string> binLabels1 = lists[1].getLabels();
199                 for (int i = 0; i < lists[1].getNumBins(); i++) {
200                 
201                         if (m->control_pressed) { outputTypes.clear(); out.close(); m->mothurRemove(outputFileName); return 0; }
202                         
203                         string binnames = lists[1].get(i);
204             vector<string> names; m->splitAtComma(binnames, names);
205                         
206                         //output column 1
207                         if (output == "name")   {   out << binnames << '\t';    }
208                         else                                    {       out << binLabels1[i] << '\t';           }
209                         
210                         map<int, int> bins; //bin numbers in little that are in this bin in big
211                         map<int, int>::iterator it;
212                         
213                         //parse bin
214                         for (int j = 0; j < names.size(); j++) { bins[littleBins[names[j]]] = littleBins[names[j]];   }
215                         
216                         string col2 = "";
217                         for (it = bins.begin(); it != bins.end(); it++) {
218                                 if (output == "name")   {   col2 += lists[0].get(it->first) + "\t";     }
219                                 else                                    {       col2 += binLabels0[it->first] + "\t";           }
220                         }
221                         
222                         //output column 2
223                         out << col2 << endl;
224                 }
225                 
226                 out.close();
227                 
228                 if (m->control_pressed) { outputTypes.clear(); m->mothurRemove(outputFileName); return 0; }
229                 
230                 m->mothurOutEndLine();
231                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
232                 m->mothurOut(outputFileName); m->mothurOutEndLine();    outputNames.push_back(outputFileName); outputTypes["otuheirarchy"].push_back(outputFileName); 
233                 m->mothurOutEndLine();
234                 
235                 return 0;
236         }
237         catch(exception& e) {
238                 m->errorOut(e, "OtuHierarchyCommand", "execute");
239                 exit(1);
240         }
241 }
242
243 //**********************************************************************************************************************
244 //returns a vector of listVectors where "little" vector is first
245 vector<ListVector> OtuHierarchyCommand::getListVectors() {
246         try {
247                 
248                 int pos; //to use in smart distancing, position of last read in file
249                 int lastPos;
250                 vector<ListVector> lists;
251                 
252                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
253                 set<string> processedLabels;
254                 set<string> userLabels = labels;
255
256                 //open file
257                 ifstream in;
258                 m->openInputFile(listFile, in);
259                 
260                 //get first list vector in file
261                 ListVector* list = NULL;
262                 string lastLabel = "";
263                 if (!in.eof())  {
264                         pos = in.tellg();
265                         lastPos = pos;
266                         list = new ListVector(in);  
267                         m->gobble(in);
268                         lastLabel = list->getLabel();
269                 }
270                 
271                 while ((list != NULL) && (userLabels.size() != 0)) {
272                 
273                         if (m->control_pressed) {  in.close(); delete list;  return lists; }
274                         
275                         //is this a listvector that we want?
276                         if(labels.count(list->getLabel()) == 1){
277                                 
278                                 //make copy of listvector
279                                 ListVector temp(*list);
280                                 lists.push_back(temp);
281                         
282                                 processedLabels.insert(list->getLabel());
283                                 userLabels.erase(list->getLabel());
284                         }
285                 
286                         //you have a label the user want that is smaller than this label and the last label has not already been processed 
287                         if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
288                                 string saveLabel = list->getLabel();
289                                 int savePos = in.tellg();
290                                 
291                                 //get smart distance line
292                                 delete list;
293                                 in.seekg(lastPos);
294                                 if (!in.eof())  {       
295                                         list = new ListVector(in);  
296                                 }else { list = NULL; }
297                                 
298                                 //make copy of listvector
299                                 ListVector temp(*list);
300                                 lists.push_back(temp);
301                                         
302                                 processedLabels.insert(list->getLabel());
303                                 userLabels.erase(list->getLabel());                                     
304                                                                                 
305                                 //restore real lastlabel to save below
306                                 list->setLabel(saveLabel);
307                                 in.seekg(savePos);
308                         }
309                         
310                         lastLabel = list->getLabel();
311                         lastPos = pos;
312                         
313                         //get next line
314                         delete list;
315                         if (!in.eof())  {       
316                                 pos = in.tellg();
317                                 list = new ListVector(in);  
318                                 m->gobble(in);
319                         }else { list = NULL; }
320                 }
321                 
322                 if (m->control_pressed) { in.close();  return lists; }                          
323                 
324                 //output error messages about any remaining user labels
325                 set<string>::iterator it;
326                 bool needToRun = false;
327                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
328                         m->mothurOut("Your file does not include the label " + *it); 
329                         if (processedLabels.count(lastLabel) != 1) {
330                                 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
331                                 needToRun = true;
332                         }else {
333                                 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
334                         }
335                 }
336                 
337                 if (m->control_pressed) {  in.close(); return lists; }
338                 
339                 //run last label if you need to
340                 if (needToRun == true)  {
341                         if (list != NULL) {     delete list;    }
342                         
343                         in.seekg(lastPos);
344                         if (!in.eof())  {       
345                                 list = new ListVector(in); 
346                                 
347                                 //make copy of listvector
348                                 ListVector temp(*list);
349                                 lists.push_back(temp);
350                                 
351                                 delete list;
352                         }
353                 }
354                 
355                 in.close();
356                 return lists;
357         }
358         catch(exception& e) {
359                 m->errorOut(e, "OtuHierarchyCommand", "getListVectors");
360                 exit(1);
361         }
362 }
363
364 //**********************************************************************************************************************
365
366
367
368
369