2 * binsequencecommand.cpp
5 * Created by Sarah Westcott on 4/3/09.
6 * Copyright 2009 Schloss Lab UMASS Amhers. All rights reserved.
10 #include "binsequencecommand.h"
12 //**********************************************************************************************************************
13 vector<string> BinSeqCommand::getValidParameters(){
15 string AlignArray[] = {"fasta","label","name", "group","outputdir","inputdir"};
16 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
20 m->errorOut(e, "BinSeqCommand", "getValidParameters");
24 //**********************************************************************************************************************
25 vector<string> BinSeqCommand::getRequiredParameters(){
27 string AlignArray[] = {"fasta"};
28 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
32 m->errorOut(e, "BinSeqCommand", "getRequiredParameters");
36 //**********************************************************************************************************************
37 vector<string> BinSeqCommand::getRequiredFiles(){
39 string AlignArray[] = {"list"};
40 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
44 m->errorOut(e, "BinSeqCommand", "getRequiredFiles");
48 //**********************************************************************************************************************
49 BinSeqCommand::BinSeqCommand(){
51 abort = true; calledHelp = true;
52 vector<string> tempOutNames;
53 outputTypes["fasta"] = tempOutNames;
56 m->errorOut(e, "BinSeqCommand", "BinSeqCommand");
60 //**********************************************************************************************************************
61 BinSeqCommand::BinSeqCommand(string option) {
63 globaldata = GlobalData::getInstance();
64 abort = false; calledHelp = false;
68 //allow user to run help
69 if(option == "help") { help(); abort = true; calledHelp = true; }
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)));
76 OptionParser parser(option);
77 map<string, string> parameters = parser.getParameters();
79 ValidParameters validParameter;
80 map<string, string>::iterator it;
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; }
87 //initialize outputTypes
88 vector<string> tempOutNames;
89 outputTypes["fasta"] = tempOutNames;
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"){
94 outputDir += m->hasPath(globaldata->getListFile()); //if user entered a file with a path then preserve it
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();
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 = ""; }
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; }
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; }
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; }
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; }
141 //check for optional parameter and set defaults
142 // ...at some point should added some additional type checking...
144 label = validParameter.validFile(parameters, "label", false);
145 if (label == "not found") { label = ""; }
147 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
148 else { allLines = 1; }
151 //if the user has not specified any labels use the ones from read.otu
153 allLines = globaldata->allLines;
154 labels = globaldata->labels;
157 namesfile = validParameter.validFile(parameters, "name", true);
158 if (namesfile == "not open") { abort = true; }
159 else if (namesfile == "not found") { namesfile = ""; }
161 groupfile = validParameter.validFile(parameters, "group", true);
162 if (groupfile == "not open") { abort = true; }
163 else if (groupfile == "not found") { groupfile = ""; }
165 if (abort == false) {
166 // m->openInputFile(fastafile, in);
167 fasta = new FastaMap();
168 if (groupfile != "") {
169 groupMap = new GroupMap(groupfile);
171 int error = groupMap->readMap();
172 if (error == 1) { delete groupMap; abort = true; }
178 catch(exception& e) {
179 m->errorOut(e, "BinSeqCommand", "BinSeqCommand");
183 //**********************************************************************************************************************
185 void BinSeqCommand::help(){
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");
197 catch(exception& e) {
198 m->errorOut(e, "BinSeqCommand", "help");
203 //**********************************************************************************************************************
205 BinSeqCommand::~BinSeqCommand(){
206 //made new in execute
207 if (abort == false) {
208 delete input; globaldata->ginput = NULL;
210 globaldata->gListVector = NULL;
212 if (groupfile != "") { delete groupMap; globaldata->gGroupmap = NULL; }
216 //**********************************************************************************************************************
218 int BinSeqCommand::execute(){
220 if (abort == true) { if (calledHelp) { return 0; } return 2; }
225 fasta->readFastaFile(fastafile);
228 //set format to list so input can get listvector
229 // globaldata->setFormat("list");
231 //if user gave a namesfile then use it
232 if (namesfile != "") {
237 read = new ReadOTUFile(globaldata->getListFile());
238 read->read(&*globaldata);
240 input = globaldata->ginput;
241 list = globaldata->gListVector;
242 string lastLabel = list->getLabel();
244 if (m->control_pressed) { return 0; }
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;
251 while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
253 if(m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
255 if(allLines == 1 || labels.count(list->getLabel()) == 1){
257 error = process(list);
258 if (error == 1) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
260 processedLabels.insert(list->getLabel());
261 userLabels.erase(list->getLabel());
264 if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
265 string saveLabel = list->getLabel();
268 list = input->getListVector(lastLabel);
270 error = process(list);
271 if (error == 1) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
273 processedLabels.insert(list->getLabel());
274 userLabels.erase(list->getLabel());
276 //restore real lastlabel to save below
277 list->setLabel(saveLabel);
280 lastLabel = list->getLabel();
283 list = input->getListVector();
286 if(m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
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();
297 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
301 //run last label if you need to
302 if (needToRun == true) {
303 if (list != NULL) { delete list; }
304 list = input->getListVector(lastLabel);
306 error = process(list);
307 if (error == 1) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
312 if(m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
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();
322 catch(exception& e) {
323 m->errorOut(e, "BinSeqCommand", "execute");
328 //**********************************************************************************************************************
329 void BinSeqCommand::readNamesFile() {
331 vector<string> dupNames;
332 m->openInputFile(namesfile, inNames);
334 string name, names, sequence;
337 inNames >> name; //read from first column A
338 inNames >> names; //read from second column A,B,C,D
342 //parse names into vector
343 m->splitAtComma(names, dupNames);
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);
356 catch(exception& e) {
357 m->errorOut(e, "BinSeqCommand", "readNamesFile");
361 //**********************************************************************************************************************
362 //return 1 if error, 0 otherwise
363 int BinSeqCommand::process(ListVector* list) {
365 string binnames, name, sequence;
367 string outputFileName = outputDir + m->getRootName(m->getSimpleName(globaldata->getListFile())) + list->getLabel() + ".fasta";
368 m->openOutputFile(outputFileName, out);
370 //save to output list of output file names
371 outputNames.push_back(outputFileName); outputTypes["fasta"].push_back(outputFileName);
373 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
375 //for each bin in the list vector
376 for (int i = 0; i < list->size(); i++) {
378 if (m->control_pressed) { return 1; }
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());
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();
399 name = name + "|" + group + "|" + toString(i+1);
400 out << ">" << name << endl;
401 out << sequence << endl;
405 m->mothurOut(name + " is missing from your fasta or name file. Please correct. "); m->mothurOutEndLine();
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();
425 binnames = binnames + "|" + group + "|" + toString(i+1);
426 out << ">" << binnames << endl;
427 out << sequence << endl;
431 m->mothurOut(binnames + " is missing from your fasta or name file. Please correct. "); m->mothurOutEndLine();
440 catch(exception& e) {
441 m->errorOut(e, "BinSeqCommand", "process");
445 //**********************************************************************************************************************