]> git.donarmstrong.com Git - mothur.git/blob - otuhierarchycommand.cpp
added checks for ^C to quit command instead of program
[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 OtuHierarchyCommand::OtuHierarchyCommand(string option) {
14         try {
15                 abort = false;
16                 
17                 //allow user to run help
18                 if(option == "help") {  help(); abort = true; }
19                 
20                 else {
21                         //valid paramters for this command
22                         string Array[] =  {"list","label","output","outputdir","inputdir"};
23                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
24                         
25                         OptionParser parser(option);
26                         map<string,string> parameters = parser.getParameters();
27                         
28                         ValidParameters validParameter;
29                         map<string,string>::iterator it;
30                 
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;  }
34                         }
35                         
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 = "";          }
39                         else {
40                                 string path;
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;             }
47                                 }
48                         }
49
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; }      
53                         
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"){  
56                                 outputDir = ""; 
57                                 outputDir += hasPath(listFile); //if user entered a file with a path then preserve it   
58                         }
59                         
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; }
64                         else { 
65                                 splitAtDash(label, labels);
66                                 if (labels.size() != 2) { m->mothurOut("You must provide 2 labels."); m->mothurOutEndLine(); abort = true; }
67                         }       
68                         
69                         output = validParameter.validFile(parameters, "output", false);                 if (output == "not found") { output = "name"; }
70                         
71                         if ((output != "name") && (output != "number")) { m->mothurOut("output options are name and number. I will use name."); m->mothurOutEndLine(); output = "name"; }
72                 }
73                 
74         }
75         catch(exception& e) {
76                 m->errorOut(e, "OtuHierarchyCommand", "OtuHierarchyCommand");
77                 exit(1);
78         }                       
79 }
80 //**********************************************************************************************************************
81
82 void OtuHierarchyCommand::help(){
83         try {
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");
92         }
93         catch(exception& e) {
94                 m->errorOut(e, "OtuHierarchyCommand", "help");
95                 exit(1);
96         }
97 }
98
99 //**********************************************************************************************************************
100
101 OtuHierarchyCommand::~OtuHierarchyCommand(){}
102
103 //**********************************************************************************************************************
104
105 int OtuHierarchyCommand::execute(){
106         try {
107                 
108                 if (abort == true) { return 0; }
109                 
110                 //get listvectors that correspond to labels requested, (or use smart distancing to get closest listvector)
111                 vector<ListVector> lists = getListVectors();
112                 
113                 if (m->control_pressed) { return 0; }
114                 
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());
120                         }
121                 }else{
122                         m->mothurOut("error getting listvectors, unable to read 2 different vectors, check your label inputs."); m->mothurOutEndLine(); return 0;
123                 }
124                 
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++) {
128                 
129                         if (m->control_pressed) {  return 0; }
130                         
131                         string names = lists[0].get(i); 
132                         
133                         //parse bin
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;  
138                         }
139                         
140                         //get last name
141                         littleBins[names] = i;
142                 }
143                 
144                 ofstream out;
145                 string outputFileName = outputDir + getRootName(getSimpleName(listFile)) + lists[0].getLabel() + "-" + lists[1].getLabel() + ".otu.hierarchy";
146                 openOutputFile(outputFileName, out);
147                 
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++) {
150                 
151                         if (m->control_pressed) { out.close(); remove(outputFileName.c_str()); return 0; }
152                         
153                         string names = lists[1].get(i);
154                         
155                         //output column 1
156                         if (output == "name")   {   out << names << '\t';       }
157                         else                                    {       out << i << '\t';               }
158                         
159                         map<int, int> bins; //bin numbers in little that are in this bin in big
160                         map<int, int>::iterator it;
161                         
162                         //parse bin
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];  
167                         }
168                         
169                         //get last name
170                         bins[littleBins[names]] = littleBins[names]; 
171                         
172                         string col2 = "";
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";             }
176                         }
177                         
178                         //output column 2
179                         out << col2 << endl;
180                 }
181                 
182                 out.close();
183                 
184                 if (m->control_pressed) { remove(outputFileName.c_str()); return 0; }
185                 
186                 m->mothurOutEndLine();
187                 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
188                 m->mothurOut(outputFileName); m->mothurOutEndLine();    
189                 m->mothurOutEndLine();
190                 
191                 return 0;
192         }
193         catch(exception& e) {
194                 m->errorOut(e, "OtuHierarchyCommand", "execute");
195                 exit(1);
196         }
197 }
198
199 //**********************************************************************************************************************
200 //returns a vector of listVectors where "little" vector is first
201 vector<ListVector> OtuHierarchyCommand::getListVectors() {
202         try {
203                 
204                 int pos; //to use in smart distancing, position of last read in file
205                 int lastPos;
206                 vector<ListVector> lists;
207                 
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;
211
212                 //open file
213                 ifstream in;
214                 openInputFile(listFile, in);
215                 
216                 //get first list vector in file
217                 ListVector* list = NULL;
218                 string lastLabel = "";
219                 if (!in.eof())  {
220                         pos = in.tellg();
221                         lastPos = pos;
222                         list = new ListVector(in);  
223                         gobble(in);
224                         lastLabel = list->getLabel();
225                 }
226                 
227                 while ((list != NULL) && (userLabels.size() != 0)) {
228                 
229                         if (m->control_pressed) {  in.close(); delete list;  return lists; }
230                         
231                         //is this a listvector that we want?
232                         if(labels.count(list->getLabel()) == 1){
233                                 
234                                 //make copy of listvector
235                                 ListVector temp(*list);
236                                 lists.push_back(temp);
237                         
238                                 processedLabels.insert(list->getLabel());
239                                 userLabels.erase(list->getLabel());
240                         }
241                 
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 ((anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
244                                 string saveLabel = list->getLabel();
245                                 int savePos = in.tellg();
246                                 
247                                 //get smart distance line
248                                 delete list;
249                                 in.seekg(lastPos);
250                                 if (!in.eof())  {       
251                                         list = new ListVector(in);  
252                                 }else { list = NULL; }
253                                 
254                                 //make copy of listvector
255                                 ListVector temp(*list);
256                                 lists.push_back(temp);
257                                         
258                                 processedLabels.insert(list->getLabel());
259                                 userLabels.erase(list->getLabel());                                     
260                                                                                 
261                                 //restore real lastlabel to save below
262                                 list->setLabel(saveLabel);
263                                 in.seekg(savePos);
264                         }
265                         
266                         lastLabel = list->getLabel();
267                         lastPos = pos;
268                         
269                         //get next line
270                         delete list;
271                         if (!in.eof())  {       
272                                 pos = in.tellg();
273                                 list = new ListVector(in);  
274                                 gobble(in);
275                         }else { list = NULL; }
276                 }
277                 
278                 if (m->control_pressed) { in.close();  return lists; }                          
279                 
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();
287                                 needToRun = true;
288                         }else {
289                                 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
290                         }
291                 }
292                 
293                 if (m->control_pressed) {  in.close(); return lists; }
294                 
295                 //run last label if you need to
296                 if (needToRun == true)  {
297                         if (list != NULL) {     delete list;    }
298                         
299                         in.seekg(lastPos);
300                         if (!in.eof())  {       
301                                 list = new ListVector(in); 
302                                 
303                                 //make copy of listvector
304                                 ListVector temp(*list);
305                                 lists.push_back(temp);
306                                 
307                                 delete list;
308                         }
309                 }
310                 
311                 in.close();
312                 return lists;
313         }
314         catch(exception& e) {
315                 m->errorOut(e, "OtuHierarchyCommand", "getListVectors");
316                 exit(1);
317         }
318 }
319
320 //**********************************************************************************************************************
321
322
323
324
325