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