]> git.donarmstrong.com Git - mothur.git/blob - binsequencecommand.cpp
added [ERROR] flag if command aborts
[mothur.git] / binsequencecommand.cpp
1 /*
2  *  binsequencecommand.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 4/3/09.
6  *  Copyright 2009 Schloss Lab UMASS Amhers. All rights reserved.
7  *
8  */
9
10 #include "binsequencecommand.h"
11
12 //**********************************************************************************************************************
13 vector<string> BinSeqCommand::getValidParameters(){     
14         try {
15                 string AlignArray[] =  {"fasta","label","name", "group","outputdir","inputdir"};
16                 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
17                 return myArray;
18         }
19         catch(exception& e) {
20                 m->errorOut(e, "BinSeqCommand", "getValidParameters");
21                 exit(1);
22         }
23 }
24 //**********************************************************************************************************************
25 vector<string> BinSeqCommand::getRequiredParameters(){  
26         try {
27                 string AlignArray[] =  {"fasta"};
28                 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
29                 return myArray;
30         }
31         catch(exception& e) {
32                 m->errorOut(e, "BinSeqCommand", "getRequiredParameters");
33                 exit(1);
34         }
35 }
36 //**********************************************************************************************************************
37 vector<string> BinSeqCommand::getRequiredFiles(){       
38         try {
39                 string AlignArray[] =  {"list"};
40                 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
41                 return myArray;
42         }
43         catch(exception& e) {
44                 m->errorOut(e, "BinSeqCommand", "getRequiredFiles");
45                 exit(1);
46         }
47 }
48 //**********************************************************************************************************************
49 BinSeqCommand::BinSeqCommand(){ 
50         try {
51                 abort = true; calledHelp = true; 
52                 vector<string> tempOutNames;
53                 outputTypes["fasta"] = tempOutNames;
54         }
55         catch(exception& e) {
56                 m->errorOut(e, "BinSeqCommand", "BinSeqCommand");
57                 exit(1);
58         }
59 }
60 //**********************************************************************************************************************
61 BinSeqCommand::BinSeqCommand(string option) {
62         try {
63                 globaldata = GlobalData::getInstance();
64                 abort = false; calledHelp = false;   
65                 allLines = 1;
66                 labels.clear();
67                 
68                 //allow user to run help
69                 if(option == "help") { help(); abort = true; calledHelp = true; }
70                 
71                 else {
72                         //valid paramters for this command
73                         string AlignArray[] =  {"fasta","label","name", "group","outputdir","inputdir"};
74                         vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
75                         
76                         OptionParser parser(option);
77                         map<string, string> parameters = parser.getParameters();
78                         
79                         ValidParameters validParameter;
80                         map<string, string>::iterator it;
81                 
82                         //check to make sure all parameters are valid for command
83                         for (it = parameters.begin(); it != parameters.end(); it++) { 
84                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
85                         }
86                         
87                         //initialize outputTypes
88                         vector<string> tempOutNames;
89                         outputTypes["fasta"] = tempOutNames;
90                         
91                         //if the user changes the output directory command factory will send this info to us in the output parameter 
92                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
93                                 outputDir = ""; 
94                                 outputDir += m->hasPath(globaldata->getListFile()); //if user entered a file with a path then preserve it       
95                         }
96
97                         
98                         //make sure the user has already run the read.otu command
99                         if (globaldata->getListFile() == "") { 
100                                 m->mothurOut("You must read a listfile before running the bin.seqs command."); 
101                                 m->mothurOutEndLine(); 
102                                 abort = true; 
103                         }
104                         
105                         //if the user changes the input directory command factory will send this info to us in the output parameter 
106                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
107                         if (inputDir == "not found"){   inputDir = "";          }
108                         else {
109                                 string path;
110                                 it = parameters.find("fasta");
111                                 //user has given a template file
112                                 if(it != parameters.end()){ 
113                                         path = m->hasPath(it->second);
114                                         //if the user has not given a path then, add inputdir. else leave path alone.
115                                         if (path == "") {       parameters["fasta"] = inputDir + it->second;            }
116                                 }
117                                 
118                                 it = parameters.find("name");
119                                 //user has given a template file
120                                 if(it != parameters.end()){ 
121                                         path = m->hasPath(it->second);
122                                         //if the user has not given a path then, add inputdir. else leave path alone.
123                                         if (path == "") {       parameters["name"] = inputDir + it->second;             }
124                                 }
125                                 
126                                 it = parameters.find("group");
127                                 //user has given a template file
128                                 if(it != parameters.end()){ 
129                                         path = m->hasPath(it->second);
130                                         //if the user has not given a path then, add inputdir. else leave path alone.
131                                         if (path == "") {       parameters["group"] = inputDir + it->second;            }
132                                 }
133                         }
134
135                         
136                         //check for required parameters
137                         fastafile = validParameter.validFile(parameters, "fasta", true);
138                         if (fastafile == "not found") { m->mothurOut("fasta is a required parameter for the bin.seqs command.");  m->mothurOutEndLine(); abort = true; }
139                         else if (fastafile == "not open") { abort = true; }     
140                 
141                         //check for optional parameter and set defaults
142                         // ...at some point should added some additional type checking...
143                         
144                         label = validParameter.validFile(parameters, "label", false);                   
145                         if (label == "not found") { label = ""; }
146                         else { 
147                                 if(label != "all") {  m->splitAtDash(label, labels);  allLines = 0;  }
148                                 else { allLines = 1;  }
149                         }
150                         
151                         //if the user has not specified any labels use the ones from read.otu
152                         if (label == "") {  
153                                 allLines = globaldata->allLines; 
154                                 labels = globaldata->labels; 
155                         }
156                         
157                         namesfile = validParameter.validFile(parameters, "name", true);
158                         if (namesfile == "not open") { abort = true; }  
159                         else if (namesfile == "not found") { namesfile = ""; }
160
161                         groupfile = validParameter.validFile(parameters, "group", true);
162                         if (groupfile == "not open") { abort = true; }
163                         else if (groupfile == "not found") { groupfile = ""; }
164                         
165                         if (abort == false) { 
166 //                              m->openInputFile(fastafile, in);
167                                 fasta = new FastaMap();
168                                 if (groupfile != "") {
169                                         groupMap = new GroupMap(groupfile);
170                                         
171                                         int error = groupMap->readMap();
172                                         if (error == 1) { delete groupMap; abort = true; }
173                                 }
174                         }
175         
176                 }
177         }
178         catch(exception& e) {
179                 m->errorOut(e, "BinSeqCommand", "BinSeqCommand");
180                 exit(1);
181         }
182 }
183 //**********************************************************************************************************************
184
185 void BinSeqCommand::help(){
186         try {
187                 m->mothurOut("The bin.seqs command can only be executed after a successful read.otu command of a listfile.\n");
188                 m->mothurOut("The bin.seqs command parameters are fasta, name, label and group.  The fasta parameter is required.\n");
189                 m->mothurOut("The label parameter allows you to select what distance levels you would like a output files created for, and are separated by dashes.\n");
190                 m->mothurOut("The bin.seqs command should be in the following format: bin.seqs(fasta=yourFastaFile, name=yourNamesFile, group=yourGroupFile, label=yourLabels).\n");
191                 m->mothurOut("Example bin.seqs(fasta=amazon.fasta, group=amazon.groups, name=amazon.names).\n");
192                 m->mothurOut("The default value for label is all lines in your inputfile.\n");
193                 m->mothurOut("The bin.seqs command outputs a .fasta file for each distance you specify appending the OTU number to each name.\n");
194                 m->mothurOut("If you provide a groupfile, then it also appends the sequences group to the name.\n");
195                 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFile).\n\n");
196         }
197         catch(exception& e) {
198                 m->errorOut(e, "BinSeqCommand", "help");
199                 exit(1);
200         }
201 }
202
203 //**********************************************************************************************************************
204
205 BinSeqCommand::~BinSeqCommand(){
206         //made new in execute
207         if (abort == false) {
208                 delete input;  globaldata->ginput = NULL;
209                 delete read;
210                 globaldata->gListVector = NULL;
211                 delete fasta;
212                 if (groupfile != "") {  delete groupMap;  globaldata->gGroupmap = NULL; }
213         }
214 }
215
216 //**********************************************************************************************************************
217
218 int BinSeqCommand::execute(){
219         try {
220                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
221         
222                 int error = 0;
223                 
224                 //read fastafile
225                 fasta->readFastaFile(fastafile);
226                 
227                 
228                 //set format to list so input can get listvector
229 //              globaldata->setFormat("list");
230                 
231                 //if user gave a namesfile then use it
232                 if (namesfile != "") {
233                         readNamesFile();
234                 }
235                 
236                 //read list file
237                 read = new ReadOTUFile(globaldata->getListFile());      
238                 read->read(&*globaldata); 
239                 
240                 input = globaldata->ginput;
241                 list = globaldata->gListVector;
242                 string lastLabel = list->getLabel();
243                 
244                 if (m->control_pressed) {  return 0; }
245                 
246                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
247                 set<string> processedLabels;
248                 set<string> userLabels = labels;
249
250                                 
251                 while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
252                         
253                         if(m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str());         } return 0; }   
254                         
255                         if(allLines == 1 || labels.count(list->getLabel()) == 1){
256                                 
257                                 error = process(list);  
258                                 if (error == 1) { for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str());         } return 0; }   
259                                                         
260                                 processedLabels.insert(list->getLabel());
261                                 userLabels.erase(list->getLabel());
262                         }
263                         
264                         if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
265                                 string saveLabel = list->getLabel();
266                                 
267                                 delete list;
268                                 list = input->getListVector(lastLabel);
269                                 
270                                 error = process(list);  
271                                 if (error == 1) { for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str());         } return 0; }
272                                                                                                         
273                                 processedLabels.insert(list->getLabel());
274                                 userLabels.erase(list->getLabel());
275                                 
276                                 //restore real lastlabel to save below
277                                 list->setLabel(saveLabel);
278                         }
279                         
280                         lastLabel = list->getLabel();                   
281                         
282                         delete list;
283                         list = input->getListVector();
284                 }
285                 
286                 if(m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str());         } return 0; }   
287
288                 //output error messages about any remaining user labels
289                 set<string>::iterator it;
290                 bool needToRun = false;
291                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
292                         m->mothurOut("Your file does not include the label " + *it); 
293                         if (processedLabels.count(lastLabel) != 1) {
294                                 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
295                                 needToRun = true;
296                         }else {
297                                 m->mothurOut(". Please refer to " + lastLabel + ".");  m->mothurOutEndLine();
298                         }
299                 }
300                 
301                 //run last label if you need to
302                 if (needToRun == true)  {
303                         if (list != NULL) {             delete list;    }
304                         list = input->getListVector(lastLabel);
305                                 
306                         error = process(list);  
307                         if (error == 1) { for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str());         } return 0; }
308                         
309                         delete list;  
310                 }
311                 
312                 if(m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str());         } return 0; }   
313
314                 m->mothurOutEndLine();
315                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
316                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
317                 m->mothurOutEndLine();
318
319                 
320                 return 0;
321         }
322         catch(exception& e) {
323                 m->errorOut(e, "BinSeqCommand", "execute");
324                 exit(1);
325         }
326 }
327
328 //**********************************************************************************************************************
329 void BinSeqCommand::readNamesFile() {
330         try {
331                 vector<string> dupNames;
332                 m->openInputFile(namesfile, inNames);
333                 
334                 string name, names, sequence;
335         
336                 while(inNames){
337                         inNames >> name;                        //read from first column  A
338                         inNames >> names;               //read from second column  A,B,C,D
339                         
340                         dupNames.clear();
341                         
342                         //parse names into vector
343                         m->splitAtComma(names, dupNames);
344                         
345                         //store names in fasta map
346                         sequence = fasta->getSequence(name);
347                         for (int i = 0; i < dupNames.size(); i++) {
348                                 fasta->push_back(dupNames[i], sequence);
349                         }
350                 
351                         m->gobble(inNames);
352                 }
353                 inNames.close();
354
355         }
356         catch(exception& e) {
357                 m->errorOut(e, "BinSeqCommand", "readNamesFile");
358                 exit(1);
359         }
360 }
361 //**********************************************************************************************************************
362 //return 1 if error, 0 otherwise
363 int BinSeqCommand::process(ListVector* list) {
364         try {
365                                 string binnames, name, sequence;
366                                 
367                                 string outputFileName = outputDir + m->getRootName(m->getSimpleName(globaldata->getListFile())) + list->getLabel() + ".fasta";
368                                 m->openOutputFile(outputFileName, out);
369                                 
370                                 //save to output list of output file names
371                                 outputNames.push_back(outputFileName);  outputTypes["fasta"].push_back(outputFileName);
372
373                                 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
374                                 
375                                 //for each bin in the list vector
376                                 for (int i = 0; i < list->size(); i++) {
377                                         
378                                         if (m->control_pressed) {  return 1; }
379                                         
380                                         binnames = list->get(i);
381                                         while (binnames.find_first_of(',') != -1) { 
382                                                 name = binnames.substr(0,binnames.find_first_of(','));
383                                                 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
384                                                 
385                                                 //do work for that name
386                                                 sequence = fasta->getSequence(name);
387                                                 if (sequence != "not found") {
388                                                         //if you don't have groups
389                                                         if (groupfile == "") {
390                                                                 name = name + "|" + toString(i+1);
391                                                                 out << ">" << name << endl;
392                                                                 out << sequence << endl;
393                                                         }else {//if you do have groups
394                                                                 string group = groupMap->getGroup(name);
395                                                                 if (group == "not found") {  
396                                                                         m->mothurOut(name + " is missing from your group file. Please correct. ");  m->mothurOutEndLine();
397                                                                         return 1;
398                                                                 }else{
399                                                                         name = name + "|" + group + "|" + toString(i+1);
400                                                                         out << ">" << name << endl;
401                                                                         out << sequence << endl;
402                                                                 }
403                                                         }
404                                                 }else { 
405                                                         m->mothurOut(name + " is missing from your fasta or name file. Please correct. "); m->mothurOutEndLine();
406                                                         return 1;
407                                                 }
408                                                 
409                                         }
410                                         
411                                         //get last name
412                                         sequence = fasta->getSequence(binnames);
413                                         if (sequence != "not found") {
414                                                 //if you don't have groups
415                                                 if (groupfile == "") {
416                                                         binnames = binnames + "|" + toString(i+1);
417                                                         out << ">" << binnames << endl;
418                                                         out << sequence << endl;
419                                                 }else {//if you do have groups
420                                                         string group = groupMap->getGroup(binnames);
421                                                         if (group == "not found") {  
422                                                                 m->mothurOut(binnames + " is missing from your group file. Please correct. "); m->mothurOutEndLine();
423                                                                 return 1;
424                                                         }else{
425                                                                 binnames = binnames + "|" + group + "|" + toString(i+1);
426                                                                 out << ">" << binnames << endl;
427                                                                 out << sequence << endl;
428                                                         }
429                                                 }
430                                         }else { 
431                                                 m->mothurOut(binnames + " is missing from your fasta or name file. Please correct. "); m->mothurOutEndLine();
432                                                 return 1;
433                                         }
434                                 }
435                                         
436                                 out.close();
437                                 return 0;
438
439         }
440         catch(exception& e) {
441                 m->errorOut(e, "BinSeqCommand", "process");
442                 exit(1);
443         }
444 }
445 //**********************************************************************************************************************
446
447