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