]> git.donarmstrong.com Git - mothur.git/blob - otuhierarchycommand.cpp
working on chimeras
[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                 //allow user to run help
17                 if(option == "help") {  help(); abort = true; }
18                 
19                 else {
20                         //valid paramters for this command
21                         string Array[] =  {"list","label","output","outputdir","inputdir"};
22                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
23                         
24                         OptionParser parser(option);
25                         map<string,string> parameters = parser.getParameters();
26                         
27                         ValidParameters validParameter;
28                         map<string,string>::iterator it;
29                 
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;  }
33                         }
34                         
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 = "";          }
38                         else {
39                                 string path;
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;             }
46                                 }
47                         }
48
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; }      
52                         
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"){  
55                                 outputDir = ""; 
56                                 outputDir += hasPath(listFile); //if user entered a file with a path then preserve it   
57                         }
58                         
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; }
63                         else { 
64                                 splitAtDash(label, labels);
65                                 if (labels.size() != 2) { mothurOut("You must provide 2 labels."); mothurOutEndLine(); abort = true; }
66                         }       
67                         
68                         output = validParameter.validFile(parameters, "output", false);                 if (output == "not found") { output = "name"; }
69                         
70                         if ((output != "name") && (output != "number")) { mothurOut("output options are name and number. I will use name."); mothurOutEndLine(); output = "name"; }
71                 }
72                 
73         }
74         catch(exception& e) {
75                 errorOut(e, "OtuHierarchyCommand", "OtuHierarchyCommand");
76                 exit(1);
77         }                       
78 }
79 //**********************************************************************************************************************
80
81 void OtuHierarchyCommand::help(){
82         try {
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");
91         }
92         catch(exception& e) {
93                 errorOut(e, "OtuHierarchyCommand", "help");
94                 exit(1);
95         }
96 }
97
98 //**********************************************************************************************************************
99
100 OtuHierarchyCommand::~OtuHierarchyCommand(){}
101
102 //**********************************************************************************************************************
103
104 int OtuHierarchyCommand::execute(){
105         try {
106                 
107                 if (abort == true) { return 0; }
108                 
109                 //get listvectors that correspond to labels requested, (or use smart distancing to get closest listvector)
110                 vector<ListVector> lists = getListVectors();
111                 
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());
117                         }
118                 }else{
119                         mothurOut("error getting listvectors, unable to read 2 different vectors, check your label inputs."); mothurOutEndLine(); return 0;
120                 }
121                 
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); 
126                         
127                         //parse bin
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;  
132                         }
133                         
134                         //get last name
135                         littleBins[names] = i;
136                 }
137                 
138                 ofstream out;
139                 string outputFileName = outputDir + getRootName(getSimpleName(listFile)) + lists[0].getLabel() + "-" + lists[1].getLabel() + ".otu.hierarchy";
140                 openOutputFile(outputFileName, out);
141                 
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++) {
144                 
145                         string names = lists[1].get(i);
146                         
147                         //output column 1
148                         if (output == "name")   {   out << names << '\t';       }
149                         else                                    {       out << i << '\t';               }
150                         
151                         map<int, int> bins; //bin numbers in little that are in this bin in big
152                         map<int, int>::iterator it;
153                         
154                         //parse bin
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];  
159                         }
160                         
161                         //get last name
162                         bins[littleBins[names]] = littleBins[names]; 
163                         
164                         string col2 = "";
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";             }
168                         }
169                         
170                         //output column 2
171                         out << col2 << endl;
172                 }
173                 
174                 out.close();
175                 
176                 return 0;
177         }
178         catch(exception& e) {
179                 errorOut(e, "OtuHierarchyCommand", "execute");
180                 exit(1);
181         }
182 }
183
184 //**********************************************************************************************************************
185 //returns a vector of listVectors where "little" vector is first
186 vector<ListVector> OtuHierarchyCommand::getListVectors() {
187         try {
188                 
189                 int pos; //to use in smart distancing, position of last read in file
190                 int lastPos;
191                 vector<ListVector> lists;
192                 
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;
196
197                 //open file
198                 ifstream in;
199                 openInputFile(listFile, in);
200                 
201                 //get first list vector in file
202                 ListVector* list = NULL;
203                 string lastLabel = "";
204                 if (!in.eof())  {
205                         pos = in.tellg();
206                         lastPos = pos;
207                         list = new ListVector(in);  
208                         gobble(in);
209                         lastLabel = list->getLabel();
210                 }
211                 
212                 while ((list != NULL) && (userLabels.size() != 0)) {
213                         
214                         //is this a listvector that we want?
215                         if(labels.count(list->getLabel()) == 1){
216                                 
217                                 //make copy of listvector
218                                 ListVector temp(*list);
219                                 lists.push_back(temp);
220                         
221                                 processedLabels.insert(list->getLabel());
222                                 userLabels.erase(list->getLabel());
223                         }
224                 
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();
229                                 
230                                 //get smart distance line
231                                 delete list;
232                                 in.seekg(lastPos);
233                                 if (!in.eof())  {       
234                                         list = new ListVector(in);  
235                                 }else { list = NULL; }
236                                 
237                                 //make copy of listvector
238                                 ListVector temp(*list);
239                                 lists.push_back(temp);
240                                         
241                                 processedLabels.insert(list->getLabel());
242                                 userLabels.erase(list->getLabel());                                     
243                                                                                 
244                                 //restore real lastlabel to save below
245                                 list->setLabel(saveLabel);
246                                 in.seekg(savePos);
247                         }
248                         
249                         lastLabel = list->getLabel();
250                         lastPos = pos;
251                         
252                         //get next line
253                         delete list;
254                         if (!in.eof())  {       
255                                 pos = in.tellg();
256                                 list = new ListVector(in);  
257                                 gobble(in);
258                         }else { list = NULL; }
259                 }
260                 
261                                                 
262                 
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();
270                                 needToRun = true;
271                         }else {
272                                 mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine();
273                         }
274                 }
275                 
276                 //run last label if you need to
277                 if (needToRun == true)  {
278                         if (list != NULL) {     delete list;    }
279                         
280                         in.seekg(lastPos);
281                         if (!in.eof())  {       
282                                 list = new ListVector(in); 
283                                 
284                                 //make copy of listvector
285                                 ListVector temp(*list);
286                                 lists.push_back(temp);
287                                 
288                                 delete list;
289                         }
290                 }
291                 
292                 
293                 return lists;
294         }
295         catch(exception& e) {
296                 errorOut(e, "OtuHierarchyCommand", "getListVectors");
297                 exit(1);
298         }
299 }
300
301 //**********************************************************************************************************************
302
303
304
305
306