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