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