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