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 BinSeqCommand::BinSeqCommand(string option) {
15 globaldata = GlobalData::getInstance();
20 //allow user to run help
21 if(option == "help") { help(); abort = true; }
24 //valid paramters for this command
25 string AlignArray[] = {"fasta","label","name", "group","outputdir","inputdir"};
26 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
28 OptionParser parser(option);
29 map<string, string> parameters = parser.getParameters();
31 ValidParameters validParameter;
32 map<string, string>::iterator it;
34 //check to make sure all parameters are valid for command
35 for (it = parameters.begin(); it != parameters.end(); it++) {
36 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
39 //if the user changes the output directory command factory will send this info to us in the output parameter
40 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
42 outputDir += hasPath(globaldata->getListFile()); //if user entered a file with a path then preserve it
46 //make sure the user has already run the read.otu command
47 if (globaldata->getListFile() == "") {
48 m->mothurOut("You must read a listfile before running the bin.seqs command.");
49 m->mothurOutEndLine();
53 //if the user changes the input directory command factory will send this info to us in the output parameter
54 string inputDir = validParameter.validFile(parameters, "inputdir", false);
55 if (inputDir == "not found"){ inputDir = ""; }
58 it = parameters.find("fasta");
59 //user has given a template file
60 if(it != parameters.end()){
61 path = hasPath(it->second);
62 //if the user has not given a path then, add inputdir. else leave path alone.
63 if (path == "") { parameters["fasta"] = inputDir + it->second; }
66 it = parameters.find("name");
67 //user has given a template file
68 if(it != parameters.end()){
69 path = hasPath(it->second);
70 //if the user has not given a path then, add inputdir. else leave path alone.
71 if (path == "") { parameters["name"] = inputDir + it->second; }
74 it = parameters.find("group");
75 //user has given a template file
76 if(it != parameters.end()){
77 path = hasPath(it->second);
78 //if the user has not given a path then, add inputdir. else leave path alone.
79 if (path == "") { parameters["group"] = inputDir + it->second; }
84 //check for required parameters
85 fastafile = validParameter.validFile(parameters, "fasta", true);
86 if (fastafile == "not found") { m->mothurOut("fasta is a required parameter for the bin.seqs command."); m->mothurOutEndLine(); abort = true; }
87 else if (fastafile == "not open") { abort = true; }
89 //check for optional parameter and set defaults
90 // ...at some point should added some additional type checking...
92 label = validParameter.validFile(parameters, "label", false);
93 if (label == "not found") { label = ""; }
95 if(label != "all") { splitAtDash(label, labels); allLines = 0; }
96 else { allLines = 1; }
99 //if the user has not specified any labels use the ones from read.otu
101 allLines = globaldata->allLines;
102 labels = globaldata->labels;
105 namesfile = validParameter.validFile(parameters, "name", true);
106 if (namesfile == "not open") { abort = true; }
107 else if (namesfile == "not found") { namesfile = ""; }
109 groupfile = validParameter.validFile(parameters, "group", true);
110 if (groupfile == "not open") { abort = true; }
111 else if (groupfile == "not found") { groupfile = ""; }
113 if (abort == false) {
114 // openInputFile(fastafile, in);
115 fasta = new FastaMap();
116 if (groupfile != "") {
117 groupMap = new GroupMap(groupfile);
119 int error = groupMap->readMap();
120 if (error == 1) { delete groupMap; abort = true; }
126 catch(exception& e) {
127 m->errorOut(e, "BinSeqCommand", "BinSeqCommand");
131 //**********************************************************************************************************************
133 void BinSeqCommand::help(){
135 m->mothurOut("The bin.seqs command can only be executed after a successful read.otu command of a listfile.\n");
136 m->mothurOut("The bin.seqs command parameters are fasta, name, label and group. The fasta parameter is required.\n");
137 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");
138 m->mothurOut("The bin.seqs command should be in the following format: bin.seqs(fasta=yourFastaFile, name=yourNamesFile, group=yourGroupFile, label=yourLabels).\n");
139 m->mothurOut("Example bin.seqs(fasta=amazon.fasta, group=amazon.groups, name=amazon.names).\n");
140 m->mothurOut("The default value for label is all lines in your inputfile.\n");
141 m->mothurOut("The bin.seqs command outputs a .fasta file for each distance you specify appending the OTU number to each name.\n");
142 m->mothurOut("If you provide a groupfile, then it also appends the sequences group to the name.\n");
143 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFile).\n\n");
145 catch(exception& e) {
146 m->errorOut(e, "BinSeqCommand", "help");
151 //**********************************************************************************************************************
153 BinSeqCommand::~BinSeqCommand(){
154 //made new in execute
155 if (abort == false) {
156 delete input; globaldata->ginput = NULL;
158 globaldata->gListVector = NULL;
160 if (groupfile != "") { delete groupMap; globaldata->gGroupmap = NULL; }
164 //**********************************************************************************************************************
166 int BinSeqCommand::execute(){
168 if (abort == true) { return 0; }
173 fasta->readFastaFile(fastafile);
176 //set format to list so input can get listvector
177 // globaldata->setFormat("list");
179 //if user gave a namesfile then use it
180 if (namesfile != "") {
185 read = new ReadOTUFile(globaldata->getListFile());
186 read->read(&*globaldata);
188 input = globaldata->ginput;
189 list = globaldata->gListVector;
190 string lastLabel = list->getLabel();
192 if (m->control_pressed) { return 0; }
194 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
195 set<string> processedLabels;
196 set<string> userLabels = labels;
199 while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
201 if(m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
203 if(allLines == 1 || labels.count(list->getLabel()) == 1){
205 error = process(list);
206 if (error == 1) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
208 processedLabels.insert(list->getLabel());
209 userLabels.erase(list->getLabel());
212 if ((anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
213 string saveLabel = list->getLabel();
216 list = input->getListVector(lastLabel);
218 error = process(list);
219 if (error == 1) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
221 processedLabels.insert(list->getLabel());
222 userLabels.erase(list->getLabel());
224 //restore real lastlabel to save below
225 list->setLabel(saveLabel);
228 lastLabel = list->getLabel();
231 list = input->getListVector();
234 if(m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
236 //output error messages about any remaining user labels
237 set<string>::iterator it;
238 bool needToRun = false;
239 for (it = userLabels.begin(); it != userLabels.end(); it++) {
240 m->mothurOut("Your file does not include the label " + *it);
241 if (processedLabels.count(lastLabel) != 1) {
242 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
245 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
249 //run last label if you need to
250 if (needToRun == true) {
251 if (list != NULL) { delete list; }
252 list = input->getListVector(lastLabel);
254 error = process(list);
255 if (error == 1) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
260 if(m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
262 m->mothurOutEndLine();
263 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
264 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
265 m->mothurOutEndLine();
270 catch(exception& e) {
271 m->errorOut(e, "BinSeqCommand", "execute");
276 //**********************************************************************************************************************
277 void BinSeqCommand::readNamesFile() {
279 vector<string> dupNames;
280 openInputFile(namesfile, inNames);
282 string name, names, sequence;
285 inNames >> name; //read from first column A
286 inNames >> names; //read from second column A,B,C,D
290 //parse names into vector
291 splitAtComma(names, dupNames);
293 //store names in fasta map
294 sequence = fasta->getSequence(name);
295 for (int i = 0; i < dupNames.size(); i++) {
296 fasta->push_back(dupNames[i], sequence);
304 catch(exception& e) {
305 m->errorOut(e, "BinSeqCommand", "readNamesFile");
309 //**********************************************************************************************************************
310 //return 1 if error, 0 otherwise
311 int BinSeqCommand::process(ListVector* list) {
313 string binnames, name, sequence;
315 string outputFileName = outputDir + getRootName(getSimpleName(globaldata->getListFile())) + list->getLabel() + ".fasta";
316 openOutputFile(outputFileName, out);
318 //save to output list of output file names
319 outputNames.push_back(outputFileName);
321 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
323 //for each bin in the list vector
324 for (int i = 0; i < list->size(); i++) {
326 if (m->control_pressed) { return 1; }
328 binnames = list->get(i);
329 while (binnames.find_first_of(',') != -1) {
330 name = binnames.substr(0,binnames.find_first_of(','));
331 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
333 //do work for that name
334 sequence = fasta->getSequence(name);
335 if (sequence != "not found") {
336 //if you don't have groups
337 if (groupfile == "") {
338 name = name + "|" + toString(i+1);
339 out << ">" << name << endl;
340 out << sequence << endl;
341 }else {//if you do have groups
342 string group = groupMap->getGroup(name);
343 if (group == "not found") {
344 m->mothurOut(name + " is missing from your group file. Please correct. "); m->mothurOutEndLine();
347 name = name + "|" + group + "|" + toString(i+1);
348 out << ">" << name << endl;
349 out << sequence << endl;
353 m->mothurOut(name + " is missing from your fasta or name file. Please correct. "); m->mothurOutEndLine();
360 sequence = fasta->getSequence(binnames);
361 if (sequence != "not found") {
362 //if you don't have groups
363 if (groupfile == "") {
364 binnames = binnames + "|" + toString(i+1);
365 out << ">" << binnames << endl;
366 out << sequence << endl;
367 }else {//if you do have groups
368 string group = groupMap->getGroup(binnames);
369 if (group == "not found") {
370 m->mothurOut(binnames + " is missing from your group file. Please correct. "); m->mothurOutEndLine();
373 binnames = binnames + "|" + group + "|" + toString(i+1);
374 out << ">" << binnames << endl;
375 out << sequence << endl;
379 m->mothurOut(binnames + " is missing from your fasta or name file. Please correct. "); m->mothurOutEndLine();
388 catch(exception& e) {
389 m->errorOut(e, "BinSeqCommand", "process");
393 //**********************************************************************************************************************