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