]> git.donarmstrong.com Git - mothur.git/blob - otuhierarchycommand.cpp
added out.hierarchy command
[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"};
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                 
29                         //check to make sure all parameters are valid for command
30                         for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
31                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
32                         }
33                         
34                         listFile = validParameter.validFile(parameters, "list", true);
35                         if (listFile == "not found") { mothurOut("list is a required parameter for the otu.hierarchy command."); mothurOutEndLine(); abort = true; }
36                         else if (listFile == "not open") { abort = true; }      
37
38                         
39                         //check for optional parameter and set defaults
40                         // ...at some point should added some additional type checking...
41                         label = validParameter.validFile(parameters, "label", false);                   
42                         if (label == "not found") { mothurOut("label is a required parameter for the otu.hierarchy command."); mothurOutEndLine(); abort = true; }
43                         else { 
44                                 splitAtDash(label, labels);
45                                 if (labels.size() != 2) { mothurOut("You must provide 2 labels."); mothurOutEndLine(); abort = true; }
46                         }                               
47                 }
48                 
49         }
50         catch(exception& e) {
51                 errorOut(e, "OtuHierarchyCommand", "OtuHierarchyCommand");
52                 exit(1);
53         }                       
54 }
55 //**********************************************************************************************************************
56
57 void OtuHierarchyCommand::help(){
58         try {
59                 mothurOut("The otu.hierarchy command is used to see how otus relate at two distances. \n");
60                 mothurOut("The otu.hierarchy command parameters are list and label.  Both parameters are required. \n");
61                 mothurOut("The otu.hierarchy command should be in the following format: \n");
62                 mothurOut("otu.hierarchy(list=yourListFile, label=yourLabels).\n");
63                 mothurOut("Example otu.hierarchy(list=amazon.fn.list, label=0.01-0.03).\n");
64                 mothurOut("The otu.hierarchy command outputs a .otu.hierarchy file which is described on the wiki.\n");
65                 mothurOut("Note: No spaces between parameter labels (i.e. list), '=' and parameters (i.e.yourListFile).\n\n");
66         }
67         catch(exception& e) {
68                 errorOut(e, "OtuHierarchyCommand", "help");
69                 exit(1);
70         }
71 }
72
73 //**********************************************************************************************************************
74
75 OtuHierarchyCommand::~OtuHierarchyCommand(){}
76
77 //**********************************************************************************************************************
78
79 int OtuHierarchyCommand::execute(){
80         try {
81                 
82                 if (abort == true) { return 0; }
83                 
84                 //get listvectors that correspond to labels requested, (or use smart distancing to get closest listvector)
85                 vector<ListVector> lists = getListVectors();
86                 
87                 //determine which is little and which is big, putting little first
88                 if (lists.size() == 2) {
89                         //if big is first swap them
90                         if (lists[0].getNumBins() < lists[1].getNumBins()) {
91                                 reverse(lists.begin(), lists.end());
92                         }
93                 }else{
94                         mothurOut("error getting listvectors, unable to read 2 different vectors, check your label inputs."); mothurOutEndLine(); return 0;
95                 }
96                 
97                 //map sequences to bin number in the "little" otu
98                 map<string, int> littleBins; 
99                 for (int i = 0; i < lists[0].getNumBins(); i++) {
100                         string names = lists[0].get(i); 
101                         
102                         //parse bin
103                         while (names.find_first_of(',') != -1) { 
104                                 string name = names.substr(0,names.find_first_of(','));
105                                 names = names.substr(names.find_first_of(',')+1, names.length());
106                                 littleBins[name] = i;  
107                         }
108                         
109                         //get last name
110                         littleBins[names] = i;
111                 }
112                 
113                 ofstream out;
114                 string outputFileName = getRootName(listFile) + lists[0].getLabel() + "-" + lists[1].getLabel() + ".otu.hierarchy";
115                 openOutputFile(outputFileName, out);
116                 
117                 //go through each bin in "big" otu and output the bins in "little" otu which created it
118                 for (int i = 0; i < lists[1].getNumBins(); i++) {
119                 
120                         string names = lists[1].get(i);
121                         
122                         //output column 1
123                         out << names << '\t';
124                         
125                         map<int, int> bins; //bin numbers in little that are in this bin in big
126                         map<int, int>::iterator it;
127                         
128                         //parse bin
129                         while (names.find_first_of(',') != -1) { 
130                                 string name = names.substr(0,names.find_first_of(','));
131                                 names = names.substr(names.find_first_of(',')+1, names.length());
132                                 bins[littleBins[name]] = littleBins[name];  
133                         }
134                         
135                         //get last name
136                         bins[littleBins[names]] = littleBins[names]; 
137                         
138                         string col2 = "";
139                         for (it = bins.begin(); it != bins.end(); it++) {
140                                 col2 += lists[0].get(it->first) + "\t";
141                         }
142                         
143                         //output column 2
144                         out << col2 << endl;
145                 }
146                 
147                 out.close();
148                 
149                 return 0;
150         }
151         catch(exception& e) {
152                 errorOut(e, "OtuHierarchyCommand", "execute");
153                 exit(1);
154         }
155 }
156
157 //**********************************************************************************************************************
158 //returns a vector of listVectors where "little" vector is first
159 vector<ListVector> OtuHierarchyCommand::getListVectors() {
160         try {
161                 
162                 int pos; //to use in smart distancing, position of last read in file
163                 int lastPos;
164                 vector<ListVector> lists;
165                 
166                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
167                 set<string> processedLabels;
168                 set<string> userLabels = labels;
169
170                 //open file
171                 ifstream in;
172                 openInputFile(listFile, in);
173                 
174                 //get first list vector in file
175                 ListVector* list = NULL;
176                 string lastLabel = "";
177                 if (!in.eof())  {
178                         pos = in.tellg();
179                         lastPos = pos;
180                         list = new ListVector(in);  
181                         gobble(in);
182                         lastLabel = list->getLabel();
183                 }
184                 
185                 while ((list != NULL) && (userLabels.size() != 0)) {
186                         
187                         //is this a listvector that we want?
188                         if(labels.count(list->getLabel()) == 1){
189                                 
190                                 //make copy of listvector
191                                 ListVector temp(*list);
192                                 lists.push_back(temp);
193                         
194                                 processedLabels.insert(list->getLabel());
195                                 userLabels.erase(list->getLabel());
196                         }
197                 
198                         //you have a label the user want that is smaller than this label and the last label has not already been processed 
199                         if ((anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
200                                 string saveLabel = list->getLabel();
201                                 int savePos = in.tellg();
202                                 
203                                 //get smart distance line
204                                 delete list;
205                                 in.seekg(lastPos);
206                                 if (!in.eof())  {       
207                                         list = new ListVector(in);  
208                                 }else { list = NULL; }
209                                 
210                                 //make copy of listvector
211                                 ListVector temp(*list);
212                                 lists.push_back(temp);
213                                         
214                                 processedLabels.insert(list->getLabel());
215                                 userLabels.erase(list->getLabel());                                     
216                                                                                 
217                                 //restore real lastlabel to save below
218                                 list->setLabel(saveLabel);
219                                 in.seekg(savePos);
220                         }
221                         
222                         lastLabel = list->getLabel();
223                         lastPos = pos;
224                         
225                         //get next line
226                         delete list;
227                         if (!in.eof())  {       
228                                 pos = in.tellg();
229                                 list = new ListVector(in);  
230                                 gobble(in);
231                         }else { list = NULL; }
232                 }
233                 
234                                                 
235                 
236                 //output error messages about any remaining user labels
237                 set<string>::iterator it;
238                 bool needToRun = false;
239                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
240                         mothurOut("Your file does not include the label " + *it); 
241                         if (processedLabels.count(lastLabel) != 1) {
242                                 mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine();
243                                 needToRun = true;
244                         }else {
245                                 mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine();
246                         }
247                 }
248                 
249                 //run last label if you need to
250                 if (needToRun == true)  {
251                         if (list != NULL) {     delete list;    }
252                         
253                         in.seekg(lastPos);
254                         if (!in.eof())  {       
255                                 list = new ListVector(in); 
256                                 
257                                 //make copy of listvector
258                                 ListVector temp(*list);
259                                 lists.push_back(temp);
260                                 
261                                 delete list;
262                         }
263                 }
264                 
265                 
266                 return lists;
267         }
268         catch(exception& e) {
269                 errorOut(e, "OtuHierarchyCommand", "getListVectors");
270                 exit(1);
271         }
272 }
273
274 //**********************************************************************************************************************
275
276
277
278
279