]> git.donarmstrong.com Git - mothur.git/blob - parselistscommand.cpp
fixes while testing
[mothur.git] / parselistscommand.cpp
1 /*
2  *  parselistcommand.cpp
3  *  Mothur
4  *
5  *  Created by westcott on 2/24/10.
6  *  Copyright 2010 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "parselistscommand.h"
11
12 //**********************************************************************************************************************
13 vector<string> ParseListCommand::getValidParameters(){  
14         try {
15                 string Array[] =  {"list","group", "label", "outputdir","inputdir"};
16                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
17                 return myArray;
18         }
19         catch(exception& e) {
20                 m->errorOut(e, "ParseListCommand", "getValidParameters");
21                 exit(1);
22         }
23 }
24
25 //**********************************************************************************************************************
26 ParseListCommand::ParseListCommand(){   
27         try {
28                 abort = true;
29                 //initialize outputTypes
30                 vector<string> tempOutNames;
31                 outputTypes["list"] = tempOutNames;
32         }
33         catch(exception& e) {
34                 m->errorOut(e, "ParseListCommand", "ParseListCommand");
35                 exit(1);
36         }
37 }
38 //**********************************************************************************************************************
39 vector<string> ParseListCommand::getRequiredParameters(){       
40         try {
41                 string Array[] =  {"list","group"};
42                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
43                 return myArray;
44         }
45         catch(exception& e) {
46                 m->errorOut(e, "ParseListCommand", "getRequiredParameters");
47                 exit(1);
48         }
49 }
50 //**********************************************************************************************************************
51 vector<string> ParseListCommand::getRequiredFiles(){    
52         try {
53                 vector<string> myArray;
54                 return myArray;
55         }
56         catch(exception& e) {
57                 m->errorOut(e, "ParseListCommand", "getRequiredFiles");
58                 exit(1);
59         }
60 }
61 //**********************************************************************************************************************
62 ParseListCommand::ParseListCommand(string option)  {
63         try {
64                 abort = false;
65                 allLines = 1;
66                         
67                 //allow user to run help
68                 if(option == "help") { help(); abort = true; }
69                 
70                 else {
71                         //valid paramters for this command
72                         string Array[] =  {"list","group", "label", "outputdir","inputdir"};
73                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
74                         
75                         OptionParser parser(option);
76                         map<string, string> parameters = parser.getParameters();
77                         
78                         ValidParameters validParameter;
79                         map<string, string>::iterator it;
80                 
81                         //check to make sure all parameters are valid for command
82                         for (it = parameters.begin(); it != parameters.end(); it++) { 
83                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
84                         }
85                         
86                         //initialize outputTypes
87                         vector<string> tempOutNames;
88                         outputTypes["list"] = tempOutNames;                     
89                                                                                                 
90                         //if the user changes the input directory command factory will send this info to us in the output parameter 
91                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
92                         if (inputDir == "not found"){   inputDir = "";          }
93                         else {
94                                 string path;
95                                 it = parameters.find("list");
96                                 //user has given a template file
97                                 if(it != parameters.end()){ 
98                                         path = m->hasPath(it->second);
99                                         //if the user has not given a path then, add inputdir. else leave path alone.
100                                         if (path == "") {       parameters["list"] = inputDir + it->second;             }
101                                 }
102                                 
103                                 it = parameters.find("group");
104                                 //user has given a template file
105                                 if(it != parameters.end()){ 
106                                         path = m->hasPath(it->second);
107                                         //if the user has not given a path then, add inputdir. else leave path alone.
108                                         if (path == "") {       parameters["group"] = inputDir + it->second;            }
109                                 }
110                         }
111
112                         
113                         //if the user changes the output directory command factory will send this info to us in the output parameter 
114                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = ""; }
115
116                         //check for required parameters
117                         listfile = validParameter.validFile(parameters, "list", true);
118                         if (listfile == "not open") { abort = true; }
119                         else if (listfile == "not found") { listfile = ""; }    
120                         
121                         groupfile = validParameter.validFile(parameters, "group", true);
122                         if (groupfile == "not open") { abort = true; }  
123                         else if (groupfile == "not found") { groupfile = ""; }
124                         else {  
125                                 groupMap = new GroupMap(groupfile);
126                                 
127                                 int error = groupMap->readMap();
128                                 if (error == 1) { abort = true; }
129                         }
130                         
131                         //do you have all files needed
132                         if ((listfile == "") || (groupfile == "")) { m->mothurOut("You must enter both a listfile and groupfile for the parse.list command. "); m->mothurOutEndLine(); abort = true;  }
133                         
134                         //check for optional parameter and set defaults
135                         // ...at some point should added some additional type checking...
136                         label = validParameter.validFile(parameters, "label", false);                   
137                         if (label == "not found") { label = "";  allLines = 1; }
138                         else { 
139                                 if(label != "all") {  m->splitAtDash(label, labels);  allLines = 0;  }
140                                 else { allLines = 1;  }
141                         }
142                 }
143
144         }
145         catch(exception& e) {
146                 m->errorOut(e, "ParseListCommand", "ParseListCommand");
147                 exit(1);
148         }
149 }
150 //**********************************************************************************************************************
151 void ParseListCommand::help(){
152         try {
153                 m->mothurOut("The parse.list command reads a list and group file and generates a list file for each group in the groupfile. \n");
154                 m->mothurOut("The parse.list command parameters are list, group and label.\n");
155                 m->mothurOut("The list and group parameters are required.\n");
156                 m->mothurOut("The label parameter is used to read specific labels in your input you want to use.\n");
157                 m->mothurOut("The parse.list command should be used in the following format: parse.list(list=yourListFile, group=yourGroupFile, label=yourLabels).\n");
158                 m->mothurOut("Example: parse.list(list=abrecovery.fn.list, group=abrecovery.groups, label=0.03).\n");
159                 m->mothurOut("Note: No spaces between parameter labels (i.e. list), '=' and parameters (i.e.yourListfile).\n\n");
160
161         }
162         catch(exception& e) {
163                 m->errorOut(e, "ParseListCommand", "help");
164                 exit(1);
165         }
166 }
167 //**********************************************************************************************************************
168 ParseListCommand::~ParseListCommand(){}
169 //**********************************************************************************************************************
170 int ParseListCommand::execute(){
171         try {
172         
173                 if (abort == true) {    return 0;       }
174                 
175                 //set fileroot
176                 string fileroot = outputDir + m->getRootName(m->getSimpleName(listfile));
177                 
178                 //fill filehandles with neccessary ofstreams
179                 int i;
180                 ofstream* temp;
181                 for (i=0; i<groupMap->namesOfGroups.size(); i++) {
182                         temp = new ofstream;
183                         filehandles[groupMap->namesOfGroups[i]] = temp;
184                         
185                         string filename = fileroot +  groupMap->namesOfGroups[i] + ".list";
186                         outputNames.push_back(filename); outputTypes["list"].push_back(filename);
187                         m->openOutputFile(filename, *temp);
188                 }
189                 
190                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
191                 set<string> processedLabels;
192                 set<string> userLabels = labels;        
193         
194                 input = new InputData(listfile, "list");
195                 list = input->getListVector();
196                 string lastLabel = list->getLabel();
197                 
198                 if (m->control_pressed) { 
199                         delete input; delete list; delete groupMap;
200                         for (i=0; i<groupMap->namesOfGroups.size(); i++) {  (*(filehandles[groupMap->namesOfGroups[i]])).close();  delete filehandles[groupMap->namesOfGroups[i]]; } 
201                         for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str()); } outputTypes.clear();
202                         return 0;
203                 }
204                 
205                 while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
206                 
207                         if (m->control_pressed) { 
208                                 delete input; delete list; delete groupMap;
209                                 for (i=0; i<groupMap->namesOfGroups.size(); i++) {  (*(filehandles[groupMap->namesOfGroups[i]])).close();  delete filehandles[groupMap->namesOfGroups[i]]; } 
210                                 for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str()); } outputTypes.clear();
211                                 return 0;
212                         }
213                         
214                         if(allLines == 1 || labels.count(list->getLabel()) == 1){
215                                         
216                                         m->mothurOut(list->getLabel()); m->mothurOutEndLine();
217                                         parse(list);
218                                                                                 
219                                         processedLabels.insert(list->getLabel());
220                                         userLabels.erase(list->getLabel());
221                         }
222                         
223                         if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
224                                         string saveLabel = list->getLabel();
225                                         
226                                         delete list;
227                                         list = input->getListVector(lastLabel); //get new list vector to process
228                                         
229                                         m->mothurOut(list->getLabel()); m->mothurOutEndLine();
230                                         parse(list);
231                                         
232                                         processedLabels.insert(list->getLabel());
233                                         userLabels.erase(list->getLabel());
234                                         
235                                         //restore real lastlabel to save below
236                                         list->setLabel(saveLabel);
237                         }
238                         
239                 
240                         lastLabel = list->getLabel();
241                                 
242                         delete list;
243                         list = input->getListVector(); //get new list vector to process
244                 }
245                 
246                 if (m->control_pressed) { 
247                                 delete input; delete groupMap;
248                                 for (i=0; i<groupMap->namesOfGroups.size(); i++) { (*(filehandles[groupMap->namesOfGroups[i]])).close();  delete filehandles[groupMap->namesOfGroups[i]]; } 
249                                 for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str()); } outputTypes.clear();
250                                 return 0;
251                 }
252                 
253                 //output error messages about any remaining user labels
254                 set<string>::iterator it;
255                 bool needToRun = false;
256                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
257                         m->mothurOut("Your file does not include the label " + *it); 
258                         if (processedLabels.count(lastLabel) != 1) {
259                                 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
260                                 needToRun = true;
261                         }else {
262                                 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
263                         }
264
265                 }
266                 
267                 if (m->control_pressed) { 
268                                 delete input; delete groupMap;
269                                 for (i=0; i<groupMap->namesOfGroups.size(); i++) {  (*(filehandles[groupMap->namesOfGroups[i]])).close();  delete filehandles[groupMap->namesOfGroups[i]]; } 
270                                 for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str()); } outputTypes.clear();
271                                 return 0;
272                 }
273                 
274                 //run last label if you need to
275                 if (needToRun == true)  {
276                         if (list != NULL) {     delete list;    }
277                         list = input->getListVector(lastLabel); //get new list vector to process
278                         
279                         m->mothurOut(list->getLabel()); m->mothurOutEndLine();
280                         parse(list);            
281                         
282                         delete list;
283                 }
284                 
285                 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
286                         (*(filehandles[it3->first])).close();
287                         delete it3->second;
288                 }
289                 
290                 
291                 delete groupMap;
292                 delete input;
293                 
294                 if (m->control_pressed) { 
295                         for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str()); } outputTypes.clear();
296                         return 0;
297                 }
298                 
299                 m->mothurOutEndLine();
300                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
301                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
302                 m->mothurOutEndLine();
303                 
304                 return 0;
305         }
306         catch(exception& e) {
307                 m->errorOut(e, "ParseListCommand", "execute");
308                 exit(1);
309         }
310 }
311 /**********************************************************************************************************************/
312 int ParseListCommand::parse(ListVector* thisList) {
313         try {
314         
315                 map<string, string> groupVector;
316                 map<string, string>::iterator itGroup;
317                 map<string, int> groupNumBins;
318                 
319                 //print label
320                 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
321                         groupNumBins[it3->first] = 0;
322                         groupVector[it3->first] = "";
323                 }
324
325                 
326                 for (int i = 0; i < thisList->getNumBins(); i++) {
327                         if (m->control_pressed) { return 0; }
328                         
329                         map<string, string> groupBins;
330                         string bin = list->get(i); 
331                         
332                         vector<string> names;
333                         m->splitAtComma(bin, names);  //parses bin into individual sequence names
334                         
335                         //parse bin into list of sequences in each group
336                         for (int j = 0; j < names.size(); j++) {
337                                 string group = groupMap->getGroup(names[j]);
338                                 
339                                 if (group == "not found") { m->mothurOut(names[j] + " is not in your groupfile. please correct."); m->mothurOutEndLine(); exit(1); }
340                                 
341                                 itGroup = groupBins.find(group);
342                                 if(itGroup == groupBins.end()) {
343                                         groupBins[group] = names[j];  //add first name
344                                         groupNumBins[group]++;
345                                 }else{ //add another name
346                                         groupBins[group] = groupBins[group] + "," + names[j];
347                                 }
348                         }
349                         
350                         //print parsed bin info to files
351                         for (itGroup = groupBins.begin(); itGroup != groupBins.end(); itGroup++) {
352                                 groupVector[itGroup->first] +=  itGroup->second + '\t'; 
353                         }
354                 
355                 }
356                 
357                 //end list vector
358                 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
359                         (*(filehandles[it3->first])) << thisList->getLabel() << '\t' << groupNumBins[it3->first] << '\t' << groupVector[it3->first] << endl;  // label numBins  listvector for that group
360                 }
361                 
362                 return 0;
363
364         }
365         catch(exception& e) {
366                 m->errorOut(e, "ParseListCommand", "parse");
367                 exit(1);
368         }
369 }
370
371 /**********************************************************************************************************************/
372
373