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