]> git.donarmstrong.com Git - mothur.git/blob - binsequencecommand.cpp
added citation function to commands
[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                         
142                         listfile = validParameter.validFile(parameters, "list", true);
143                         if (listfile == "not found") {                  
144                                 listfile = m->getListFile(); 
145                                 if (listfile != "") { m->mothurOut("Using " + listfile + " as input file for the list parameter."); m->mothurOutEndLine(); }
146                                 else {  m->mothurOut("You have no current list file and the list parameter is required."); m->mothurOutEndLine(); abort = true; }
147                         }
148                         else if (listfile == "not open") { listfile = ""; abort = true; }       
149                         
150                         //if the user changes the output directory command factory will send this info to us in the output parameter 
151                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
152                                 outputDir = ""; 
153                                 outputDir += m->hasPath(listfile); //if user entered a file with a path then preserve it        
154                         }
155                         
156                 
157                         //check for optional parameter and set defaults
158                         // ...at some point should added some additional type checking...
159                         
160                         label = validParameter.validFile(parameters, "label", false);                   
161                         if (label == "not found") { label = ""; }
162                         else { 
163                                 if(label != "all") {  m->splitAtDash(label, labels);  allLines = 0;  }
164                                 else { allLines = 1;  }
165                         }
166                         
167                         namesfile = validParameter.validFile(parameters, "name", true);
168                         if (namesfile == "not open") { abort = true; }  
169                         else if (namesfile == "not found") { namesfile = ""; }
170
171                         groupfile = validParameter.validFile(parameters, "group", true);
172                         if (groupfile == "not open") { abort = true; }
173                         else if (groupfile == "not found") { groupfile = ""; }
174                         
175                 }
176         }
177         catch(exception& e) {
178                 m->errorOut(e, "BinSeqCommand", "BinSeqCommand");
179                 exit(1);
180         }
181 }
182 //**********************************************************************************************************************
183
184 BinSeqCommand::~BinSeqCommand(){}
185 //**********************************************************************************************************************
186
187 int BinSeqCommand::execute(){
188         try {
189                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
190         
191                 int error = 0;
192                 
193                 fasta = new FastaMap();
194                 if (groupfile != "") {
195                         groupMap = new GroupMap(groupfile);
196                         groupMap->readMap();
197                 }
198                 
199                 //read fastafile
200                 fasta->readFastaFile(fastafile);
201                 
202                 //if user gave a namesfile then use it
203                 if (namesfile != "") {
204                         readNamesFile();
205                 }
206                 
207                 input = new InputData(listfile, "list");
208                 list = input->getListVector();
209                 string lastLabel = list->getLabel();
210                 
211                 if (m->control_pressed) {  delete input;  delete fasta; if (groupfile != "") {  delete groupMap;   } return 0; }
212                 
213                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
214                 set<string> processedLabels;
215                 set<string> userLabels = labels;
216
217                                 
218                 while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
219                         
220                         if(m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str());         } delete input;  delete fasta; if (groupfile != "") {  delete groupMap;   } return 0; } 
221                         
222                         if(allLines == 1 || labels.count(list->getLabel()) == 1){
223                                 
224                                 error = process(list);  
225                                 if (error == 1) { for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str());         } delete input;  delete fasta; if (groupfile != "") {  delete groupMap;   } return 0; } 
226                                                         
227                                 processedLabels.insert(list->getLabel());
228                                 userLabels.erase(list->getLabel());
229                         }
230                         
231                         if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
232                                 string saveLabel = list->getLabel();
233                                 
234                                 delete list;
235                                 list = input->getListVector(lastLabel);
236                                 
237                                 error = process(list);  
238                                 if (error == 1) { for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str());         } delete input;  delete fasta; if (groupfile != "") {  delete groupMap;   } return 0; }
239                                                                                                         
240                                 processedLabels.insert(list->getLabel());
241                                 userLabels.erase(list->getLabel());
242                                 
243                                 //restore real lastlabel to save below
244                                 list->setLabel(saveLabel);
245                         }
246                         
247                         lastLabel = list->getLabel();                   
248                         
249                         delete list;
250                         list = input->getListVector();
251                 }
252                 
253                 if(m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str());         } delete input;  delete fasta; if (groupfile != "") {  delete groupMap;   } return 0; } 
254
255                 //output error messages about any remaining user labels
256                 set<string>::iterator it;
257                 bool needToRun = false;
258                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
259                         m->mothurOut("Your file does not include the label " + *it); 
260                         if (processedLabels.count(lastLabel) != 1) {
261                                 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
262                                 needToRun = true;
263                         }else {
264                                 m->mothurOut(". Please refer to " + lastLabel + ".");  m->mothurOutEndLine();
265                         }
266                 }
267                 
268                 //run last label if you need to
269                 if (needToRun == true)  {
270                         if (list != NULL) {             delete list;    }
271                         list = input->getListVector(lastLabel);
272                                 
273                         error = process(list);  
274                         if (error == 1) { for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str());         } delete input;  delete fasta; if (groupfile != "") {  delete groupMap;   } return 0; }
275                         
276                         delete list;  
277                 }
278                 
279                 delete input;  
280                 delete fasta; 
281                 if (groupfile != "") {  delete groupMap;   } 
282                 
283                 if(m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str());         }  return 0; }  
284                 
285                 m->mothurOutEndLine();
286                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
287                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
288                 m->mothurOutEndLine();
289
290                 
291                 return 0;
292         }
293         catch(exception& e) {
294                 m->errorOut(e, "BinSeqCommand", "execute");
295                 exit(1);
296         }
297 }
298
299 //**********************************************************************************************************************
300 void BinSeqCommand::readNamesFile() {
301         try {
302                 vector<string> dupNames;
303                 m->openInputFile(namesfile, inNames);
304                 
305                 string name, names, sequence;
306         
307                 while(inNames){
308                         inNames >> name;                        //read from first column  A
309                         inNames >> names;               //read from second column  A,B,C,D
310                         
311                         dupNames.clear();
312                         
313                         //parse names into vector
314                         m->splitAtComma(names, dupNames);
315                         
316                         //store names in fasta map
317                         sequence = fasta->getSequence(name);
318                         for (int i = 0; i < dupNames.size(); i++) {
319                                 fasta->push_back(dupNames[i], sequence);
320                         }
321                 
322                         m->gobble(inNames);
323                 }
324                 inNames.close();
325
326         }
327         catch(exception& e) {
328                 m->errorOut(e, "BinSeqCommand", "readNamesFile");
329                 exit(1);
330         }
331 }
332 //**********************************************************************************************************************
333 //return 1 if error, 0 otherwise
334 int BinSeqCommand::process(ListVector* list) {
335         try {
336                                 string binnames, name, sequence;
337                                 
338                                 string outputFileName = outputDir + m->getRootName(m->getSimpleName(listfile)) + list->getLabel() + ".fasta";
339                                 m->openOutputFile(outputFileName, out);
340                                 
341                                 //save to output list of output file names
342                                 outputNames.push_back(outputFileName);  outputTypes["fasta"].push_back(outputFileName);
343
344                                 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
345                                 
346                                 //for each bin in the list vector
347                                 for (int i = 0; i < list->size(); i++) {
348                                         
349                                         if (m->control_pressed) {  return 1; }
350                                         
351                                         binnames = list->get(i);
352                                         while (binnames.find_first_of(',') != -1) { 
353                                                 name = binnames.substr(0,binnames.find_first_of(','));
354                                                 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
355                                                 
356                                                 //do work for that name
357                                                 sequence = fasta->getSequence(name);
358                                                 if (sequence != "not found") {
359                                                         //if you don't have groups
360                                                         if (groupfile == "") {
361                                                                 name = name + "\t" + toString(i+1);
362                                                                 out << ">" << name << endl;
363                                                                 out << sequence << endl;
364                                                         }else {//if you do have groups
365                                                                 string group = groupMap->getGroup(name);
366                                                                 if (group == "not found") {  
367                                                                         m->mothurOut(name + " is missing from your group file. Please correct. ");  m->mothurOutEndLine();
368                                                                         return 1;
369                                                                 }else{
370                                                                         name = name + "\t" + group + "\t" + toString(i+1);
371                                                                         out << ">" << name << endl;
372                                                                         out << sequence << endl;
373                                                                 }
374                                                         }
375                                                 }else { 
376                                                         m->mothurOut(name + " is missing from your fasta or name file. Please correct. "); m->mothurOutEndLine();
377                                                         return 1;
378                                                 }
379                                                 
380                                         }
381                                         
382                                         //get last name
383                                         sequence = fasta->getSequence(binnames);
384                                         if (sequence != "not found") {
385                                                 //if you don't have groups
386                                                 if (groupfile == "") {
387                                                         binnames = binnames + "\t" + toString(i+1);
388                                                         out << ">" << binnames << endl;
389                                                         out << sequence << endl;
390                                                 }else {//if you do have groups
391                                                         string group = groupMap->getGroup(binnames);
392                                                         if (group == "not found") {  
393                                                                 m->mothurOut(binnames + " is missing from your group file. Please correct. "); m->mothurOutEndLine();
394                                                                 return 1;
395                                                         }else{
396                                                                 binnames = binnames + "\t" + group + "\t" + toString(i+1);
397                                                                 out << ">" << binnames << endl;
398                                                                 out << sequence << endl;
399                                                         }
400                                                 }
401                                         }else { 
402                                                 m->mothurOut(binnames + " is missing from your fasta or name file. Please correct. "); m->mothurOutEndLine();
403                                                 return 1;
404                                         }
405                                 }
406                                         
407                                 out.close();
408                                 return 0;
409
410         }
411         catch(exception& e) {
412                 m->errorOut(e, "BinSeqCommand", "process");
413                 exit(1);
414         }
415 }
416 //**********************************************************************************************************************
417
418