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 parser = new OptionParser();
30 parser->parse(option, parameters); delete parser;
32 ValidParameters* validParameter = new ValidParameters();
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 //make sure the user has already run the read.otu command
40 if (globaldata->getListFile() == "") { cout << "You must read a listfile before running the bin.seqs command." << endl; abort = true; }
43 //check for required parameters
44 fastafile = validParameter->validFile(parameters, "fasta", true);
45 if (fastafile == "not found") { cout << "fasta is a required parameter for the bin.seqs command." << endl; abort = true; }
46 else if (fastafile == "not open") { abort = true; }
48 globaldata->setFastaFile(fastafile);
49 openInputFile(fastafile, in);
50 fasta = new FastaMap();
54 //check for optional parameter and set defaults
55 // ...at some point should added some additional type checking...
56 line = validParameter->validFile(parameters, "line", false);
57 if (line == "not found") { line = ""; }
59 if(line != "all") { splitAtDash(line, lines); allLines = 0; }
60 else { allLines = 1; }
63 label = validParameter->validFile(parameters, "label", false);
64 if (label == "not found") { label = ""; }
66 if(label != "all") { splitAtDash(label, labels); allLines = 0; }
67 else { allLines = 1; }
70 //make sure user did not use both the line and label parameters
71 if ((line != "") && (label != "")) { cout << "You cannot use both the line and label parameters at the same time. " << endl; abort = true; }
72 //if the user has not specified any line or labels use the ones from read.otu
73 else if ((line == "") && (label == "")) {
74 allLines = globaldata->allLines;
75 labels = globaldata->labels;
76 lines = globaldata->lines;
79 namesfile = validParameter->validFile(parameters, "name", true);
80 if (namesfile == "not open") { abort = true; }
81 else if (namesfile == "not found") { namesfile = ""; }
83 groupfile = validParameter->validFile(parameters, "group", true);
84 if (groupfile == "not open") { abort = true; }
85 else if (groupfile == "not found") { groupfile = ""; }
87 //read in group map info.
88 groupMap = new GroupMap(groupfile);
92 delete validParameter;
96 cout << "Standard Error: " << e.what() << " has occurred in the BinSeqCommand class Function BinSeqCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
100 cout << "An unknown error has occurred in the BinSeqCommand class function BinSeqCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
104 //**********************************************************************************************************************
106 void BinSeqCommand::help(){
108 cout << "The bin.seqs command can only be executed after a successful read.otu command of a listfile." << "\n";
109 cout << "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";
110 cout << "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";
111 cout << "The bin.seqs command should be in the following format: bin.seqs(fasta=yourFastaFile, name=yourNamesFile, group=yourGroupFile, line=yourLines, label=yourLabels)." << "\n";
112 cout << "Example bin.seqs(fasta=amazon.fasta, group=amazon.groups, line=1-3-5, name=amazon.names)." << "\n";
113 cout << "The default value for line and label are all lines in your inputfile." << "\n";
114 cout << "The bin.seqs command outputs a .fasta file for each distance you specify appending the OTU number to each name." << "\n";
115 cout << "If you provide a groupfile, then it also appends the sequences group to the name." << "\n";
116 cout << "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFile)." << "\n" << "\n";
118 catch(exception& e) {
119 cout << "Standard Error: " << e.what() << " has occurred in the BinSeqCommand class Function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
123 cout << "An unknown error has occurred in the BinSeqCommand class function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
130 //**********************************************************************************************************************
132 BinSeqCommand::~BinSeqCommand(){
133 //made new in execute
134 if (abort == false) {
140 //made new in constructor
142 if (groupfile != "") {
148 //**********************************************************************************************************************
150 int BinSeqCommand::execute(){
153 if (abort == true) { return 0; }
159 fasta->readFastaFile(in);
161 //set format to list so input can get listvector
162 globaldata->setFormat("list");
164 //if user gave a namesfile then use it
165 if (namesfile != "") {
170 read = new ReadOTUFile(globaldata->getListFile());
171 read->read(&*globaldata);
173 input = globaldata->ginput;
174 list = globaldata->gListVector;
175 ListVector* lastList = list;
177 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
178 set<string> processedLabels;
179 set<string> userLabels = labels;
180 set<int> userLines = lines;
183 while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
185 if(allLines == 1 || lines.count(count) == 1 || labels.count(list->getLabel()) == 1){
187 error = process(list, count);
188 if (error == 1) { return 0; }
190 processedLabels.insert(list->getLabel());
191 userLabels.erase(list->getLabel());
192 userLines.erase(count);
195 if ((anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastList->getLabel()) != 1)) {
197 error = process(lastList, count);
198 if (error == 1) { return 0; }
200 processedLabels.insert(lastList->getLabel());
201 userLabels.erase(lastList->getLabel());
205 if (count != 1) { delete lastList; }
208 list = input->getListVector();
213 //output error messages about any remaining user labels
214 set<string>::iterator it;
215 bool needToRun = false;
216 for (it = userLabels.begin(); it != userLabels.end(); it++) {
217 cout << "Your file does not include the label "<< *it;
218 if (processedLabels.count(lastList->getLabel()) != 1) {
219 cout << ". I will use " << lastList->getLabel() << "." << endl;
222 cout << ". Please refer to " << lastList->getLabel() << "." << endl;
226 //run last line if you need to
227 if (needToRun == true) {
228 error = process(lastList, count);
229 if (error == 1) { return 0; }
235 catch(exception& e) {
236 cout << "Standard Error: " << e.what() << " has occurred in the BinSeqCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
240 cout << "An unknown error has occurred in the BinSeqCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
245 //**********************************************************************************************************************
246 void BinSeqCommand::readNamesFile() {
248 vector<string> dupNames;
249 openInputFile(namesfile, inNames);
251 string name, names, sequence;
254 inNames >> name; //read from first column A
255 inNames >> names; //read from second column A,B,C,D
259 //parse names into vector
260 splitAtComma(names, dupNames);
262 //store names in fasta map
263 sequence = fasta->getSequence(name);
264 for (int i = 0; i < dupNames.size(); i++) {
265 fasta->push_back(dupNames[i], sequence);
273 catch(exception& e) {
274 cout << "Standard Error: " << e.what() << " has occurred in the BinSeqCommand class Function readNamesFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
278 cout << "An unknown error has occurred in the BinSeqCommand class function readNamesFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
282 //**********************************************************************************************************************
283 //return 1 if error, 0 otherwise
284 int BinSeqCommand::process(ListVector* list, int count) {
286 string binnames, name, sequence;
287 string outputFileName = getRootName(globaldata->getListFile()) + list->getLabel() + ".fasta";
288 openOutputFile(outputFileName, out);
290 cout << list->getLabel() << '\t' << count << endl;
292 //for each bin in the list vector
293 for (int i = 0; i < list->size(); i++) {
295 binnames = list->get(i);
296 while (binnames.find_first_of(',') != -1) {
297 name = binnames.substr(0,binnames.find_first_of(','));
298 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
300 //do work for that name
301 sequence = fasta->getSequence(name);
302 if (sequence != "not found") {
303 //if you don't have groups
304 if (groupfile == "") {
305 name = name + "|" + toString(i+1);
306 out << ">" << name << endl;
307 out << sequence << endl;
308 }else {//if you do have groups
309 string group = groupMap->getGroup(name);
310 if (group == "not found") {
311 cout << name << " is missing from your group file. Please correct. " << endl;
312 remove(outputFileName.c_str());
315 name = name + "|" + group + "|" + toString(i+1);
316 out << ">" << name << endl;
317 out << sequence << endl;
321 cout << name << " is missing from your fasta or name file. Please correct. " << endl;
322 remove(outputFileName.c_str());
329 sequence = fasta->getSequence(binnames);
330 if (sequence != "not found") {
331 //if you don't have groups
332 if (groupfile == "") {
333 binnames = binnames + "|" + toString(i+1);
334 out << ">" << binnames << endl;
335 out << sequence << endl;
336 }else {//if you do have groups
337 string group = groupMap->getGroup(binnames);
338 if (group == "not found") {
339 cout << binnames << " is missing from your group file. Please correct. " << endl;
340 remove(outputFileName.c_str());
343 binnames = binnames + "|" + group + "|" + toString(i+1);
344 out << ">" << binnames << endl;
345 out << sequence << endl;
349 cout << binnames << " is missing from your fasta or name file. Please correct. " << endl;
350 remove(outputFileName.c_str());
359 catch(exception& e) {
360 cout << "Standard Error: " << e.what() << " has occurred in the BinSeqCommand class Function readNamesFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
364 cout << "An unknown error has occurred in the BinSeqCommand class function readNamesFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
368 //**********************************************************************************************************************