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