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();
21 //allow user to run help
22 if(option == "help") { help(); abort = true; }
25 //valid paramters for this command
26 string AlignArray[] = {"fasta","line","label","name", "group"};
27 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
29 OptionParser parser(option);
30 map<string, string> parameters = parser.getParameters();
32 ValidParameters validParameter;
34 //check to make sure all parameters are valid for command
35 for (map<string, string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
36 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
39 //make sure the user has already run the read.otu command
40 if (globaldata->getListFile() == "") {
41 mothurOut("You must read a listfile before running the bin.seqs command.");
47 //check for required parameters
48 fastafile = validParameter.validFile(parameters, "fasta", true);
49 if (fastafile == "not found") { mothurOut("fasta is a required parameter for the bin.seqs command."); mothurOutEndLine(); abort = true; }
50 else if (fastafile == "not open") { abort = true; }
52 //check for optional parameter and set defaults
53 // ...at some point should added some additional type checking...
54 line = validParameter.validFile(parameters, "line", false);
55 if (line == "not found") { line = ""; }
57 if(line != "all") { splitAtDash(line, lines); allLines = 0; }
58 else { allLines = 1; }
61 label = validParameter.validFile(parameters, "label", false);
62 if (label == "not found") { label = ""; }
64 if(label != "all") { splitAtDash(label, labels); allLines = 0; }
65 else { allLines = 1; }
68 //make sure user did not use both the line and label parameters
69 if ((line != "") && (label != "")) { mothurOut("You cannot use both the line and label parameters at the same time. "); mothurOutEndLine(); abort = true; }
70 //if the user has not specified any line or labels use the ones from read.otu
71 else if ((line == "") && (label == "")) {
72 allLines = globaldata->allLines;
73 labels = globaldata->labels;
74 lines = globaldata->lines;
77 namesfile = validParameter.validFile(parameters, "name", true);
78 if (namesfile == "not open") { abort = true; }
79 else if (namesfile == "not found") { namesfile = ""; }
81 groupfile = validParameter.validFile(parameters, "group", true);
82 if (groupfile == "not open") { abort = true; }
83 else if (groupfile == "not found") { groupfile = ""; }
86 // openInputFile(fastafile, in);
87 fasta = new FastaMap();
88 if (groupfile != "") {
89 groupMap = new GroupMap(groupfile);
97 errorOut(e, "BinSeqCommand", "BinSeqCommand");
101 //**********************************************************************************************************************
103 void BinSeqCommand::help(){
105 mothurOut("The bin.seqs command can only be executed after a successful read.otu command of a listfile.\n");
106 mothurOut("The bin.seqs command parameters are fasta, name, line, label and group. The fasta parameter is required, and you may not use line and label at the same time.\n");
107 mothurOut("The line and label allow you to select what distance levels you would like a output files created for, and are separated by dashes.\n");
108 mothurOut("The bin.seqs command should be in the following format: bin.seqs(fasta=yourFastaFile, name=yourNamesFile, group=yourGroupFile, line=yourLines, label=yourLabels).\n");
109 mothurOut("Example bin.seqs(fasta=amazon.fasta, group=amazon.groups, line=1-3-5, name=amazon.names).\n");
110 mothurOut("The default value for line and label are all lines in your inputfile.\n");
111 mothurOut("The bin.seqs command outputs a .fasta file for each distance you specify appending the OTU number to each name.\n");
112 mothurOut("If you provide a groupfile, then it also appends the sequences group to the name.\n");
113 mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFile).\n\n");
115 catch(exception& e) {
116 errorOut(e, "BinSeqCommand", "help");
121 //**********************************************************************************************************************
123 BinSeqCommand::~BinSeqCommand(){
124 //made new in execute
125 if (abort == false) {
126 delete input; globaldata->ginput = NULL;
128 globaldata->gListVector = NULL;
130 if (groupfile != "") { delete groupMap; globaldata->gGroupmap = NULL; }
134 //**********************************************************************************************************************
136 int BinSeqCommand::execute(){
138 if (abort == true) { return 0; }
144 fasta->readFastaFile(fastafile);
147 //set format to list so input can get listvector
148 // globaldata->setFormat("list");
150 //if user gave a namesfile then use it
151 if (namesfile != "") {
156 read = new ReadOTUFile(globaldata->getListFile());
157 read->read(&*globaldata);
159 input = globaldata->ginput;
160 list = globaldata->gListVector;
161 string lastLabel = list->getLabel();
163 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
164 set<string> processedLabels;
165 set<string> userLabels = labels;
166 set<int> userLines = lines;
169 while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
171 if(allLines == 1 || lines.count(count) == 1 || labels.count(list->getLabel()) == 1){
173 error = process(list);
174 if (error == 1) { return 0; }
176 processedLabels.insert(list->getLabel());
177 userLabels.erase(list->getLabel());
178 userLines.erase(count);
181 if ((anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
183 list = input->getListVector(lastLabel);
185 error = process(list);
186 if (error == 1) { return 0; }
188 processedLabels.insert(list->getLabel());
189 userLabels.erase(list->getLabel());
193 lastLabel = list->getLabel();
196 list = input->getListVector();
201 //output error messages about any remaining user labels
202 set<string>::iterator it;
203 bool needToRun = false;
204 for (it = userLabels.begin(); it != userLabels.end(); it++) {
205 mothurOut("Your file does not include the label " + *it);
206 if (processedLabels.count(lastLabel) != 1) {
207 mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine();
210 mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine();
214 //run last line if you need to
215 if (needToRun == true) {
217 list = input->getListVector(lastLabel);
219 error = process(list);
220 if (error == 1) { return 0; }
227 catch(exception& e) {
228 errorOut(e, "BinSeqCommand", "execute");
233 //**********************************************************************************************************************
234 void BinSeqCommand::readNamesFile() {
236 vector<string> dupNames;
237 openInputFile(namesfile, inNames);
239 string name, names, sequence;
242 inNames >> name; //read from first column A
243 inNames >> names; //read from second column A,B,C,D
247 //parse names into vector
248 splitAtComma(names, dupNames);
250 //store names in fasta map
251 sequence = fasta->getSequence(name);
252 for (int i = 0; i < dupNames.size(); i++) {
253 fasta->push_back(dupNames[i], sequence);
261 catch(exception& e) {
262 errorOut(e, "BinSeqCommand", "readNamesFile");
266 //**********************************************************************************************************************
267 //return 1 if error, 0 otherwise
268 int BinSeqCommand::process(ListVector* list) {
270 string binnames, name, sequence;
271 string outputFileName = getRootName(globaldata->getListFile()) + list->getLabel() + ".fasta";
272 openOutputFile(outputFileName, out);
274 mothurOut(list->getLabel()); mothurOutEndLine();
276 //for each bin in the list vector
277 for (int i = 0; i < list->size(); i++) {
279 binnames = list->get(i);
280 while (binnames.find_first_of(',') != -1) {
281 name = binnames.substr(0,binnames.find_first_of(','));
282 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
284 //do work for that name
285 sequence = fasta->getSequence(name);
286 if (sequence != "not found") {
287 //if you don't have groups
288 if (groupfile == "") {
289 name = name + "|" + toString(i+1);
290 out << ">" << name << endl;
291 out << sequence << endl;
292 }else {//if you do have groups
293 string group = groupMap->getGroup(name);
294 if (group == "not found") {
295 mothurOut(name + " is missing from your group file. Please correct. "); mothurOutEndLine();
296 remove(outputFileName.c_str());
299 name = name + "|" + group + "|" + toString(i+1);
300 out << ">" << name << endl;
301 out << sequence << endl;
305 mothurOut(name + " is missing from your fasta or name file. Please correct. "); mothurOutEndLine();
306 remove(outputFileName.c_str());
313 sequence = fasta->getSequence(binnames);
314 if (sequence != "not found") {
315 //if you don't have groups
316 if (groupfile == "") {
317 binnames = binnames + "|" + toString(i+1);
318 out << ">" << binnames << endl;
319 out << sequence << endl;
320 }else {//if you do have groups
321 string group = groupMap->getGroup(binnames);
322 if (group == "not found") {
323 mothurOut(binnames + " is missing from your group file. Please correct. "); mothurOutEndLine();
324 remove(outputFileName.c_str());
327 binnames = binnames + "|" + group + "|" + toString(i+1);
328 out << ">" << binnames << endl;
329 out << sequence << endl;
333 mothurOut(binnames + " is missing from your fasta or name file. Please correct. "); mothurOutEndLine();
334 remove(outputFileName.c_str());
343 catch(exception& e) {
344 errorOut(e, "BinSeqCommand", "process");
348 //**********************************************************************************************************************