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() == "") { 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 //check for optional parameter and set defaults
49 // ...at some point should added some additional type checking...
50 line = validParameter.validFile(parameters, "line", false);
51 if (line == "not found") { line = ""; }
53 if(line != "all") { splitAtDash(line, lines); allLines = 0; }
54 else { allLines = 1; }
57 label = validParameter.validFile(parameters, "label", false);
58 if (label == "not found") { label = ""; }
60 if(label != "all") { splitAtDash(label, labels); allLines = 0; }
61 else { allLines = 1; }
64 //make sure user did not use both the line and label parameters
65 if ((line != "") && (label != "")) { cout << "You cannot use both the line and label parameters at the same time. " << endl; abort = true; }
66 //if the user has not specified any line or labels use the ones from read.otu
67 else if ((line == "") && (label == "")) {
68 allLines = globaldata->allLines;
69 labels = globaldata->labels;
70 lines = globaldata->lines;
73 namesfile = validParameter.validFile(parameters, "name", true);
74 if (namesfile == "not open") { abort = true; }
75 else if (namesfile == "not found") { namesfile = ""; }
77 groupfile = validParameter.validFile(parameters, "group", true);
78 if (groupfile == "not open") { abort = true; }
79 else if (groupfile == "not found") { groupfile = ""; }
82 // openInputFile(fastafile, in);
83 fasta = new FastaMap();
84 if (groupfile != "") {
85 groupMap = new GroupMap(groupfile);
93 cout << "Standard Error: " << e.what() << " has occurred in the BinSeqCommand class Function BinSeqCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
97 cout << "An unknown error has occurred in the BinSeqCommand class function BinSeqCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
101 //**********************************************************************************************************************
103 void BinSeqCommand::help(){
105 cout << "The bin.seqs command can only be executed after a successful read.otu command of a listfile." << "\n";
106 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";
107 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";
108 cout << "The bin.seqs command should be in the following format: bin.seqs(fasta=yourFastaFile, name=yourNamesFile, group=yourGroupFile, line=yourLines, label=yourLabels)." << "\n";
109 cout << "Example bin.seqs(fasta=amazon.fasta, group=amazon.groups, line=1-3-5, name=amazon.names)." << "\n";
110 cout << "The default value for line and label are all lines in your inputfile." << "\n";
111 cout << "The bin.seqs command outputs a .fasta file for each distance you specify appending the OTU number to each name." << "\n";
112 cout << "If you provide a groupfile, then it also appends the sequences group to the name." << "\n";
113 cout << "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFile)." << "\n" << "\n";
115 catch(exception& e) {
116 cout << "Standard Error: " << e.what() << " has occurred in the BinSeqCommand class Function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
120 cout << "An unknown error has occurred in the BinSeqCommand class function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
127 //**********************************************************************************************************************
129 BinSeqCommand::~BinSeqCommand(){
130 //made new in execute
131 if (abort == false) {
132 delete input; globaldata->ginput = NULL;
134 globaldata->gListVector = NULL;
136 if (groupfile != "") { delete groupMap; globaldata->gGroupmap = NULL; }
140 //**********************************************************************************************************************
142 int BinSeqCommand::execute(){
144 if (abort == true) { return 0; }
150 fasta->readFastaFile(fastafile);
153 //set format to list so input can get listvector
154 // globaldata->setFormat("list");
156 //if user gave a namesfile then use it
157 if (namesfile != "") {
162 read = new ReadOTUFile(globaldata->getListFile());
163 read->read(&*globaldata);
165 input = globaldata->ginput;
166 list = globaldata->gListVector;
167 ListVector* lastList = list;
169 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
170 set<string> processedLabels;
171 set<string> userLabels = labels;
172 set<int> userLines = lines;
175 while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
177 if(allLines == 1 || lines.count(count) == 1 || labels.count(list->getLabel()) == 1){
179 error = process(list, count);
180 if (error == 1) { return 0; }
182 processedLabels.insert(list->getLabel());
183 userLabels.erase(list->getLabel());
184 userLines.erase(count);
187 if ((anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastList->getLabel()) != 1)) {
189 error = process(lastList, count);
190 if (error == 1) { return 0; }
192 processedLabels.insert(lastList->getLabel());
193 userLabels.erase(lastList->getLabel());
197 if (count != 1) { delete lastList; }
200 list = input->getListVector();
205 //output error messages about any remaining user labels
206 set<string>::iterator it;
207 bool needToRun = false;
208 for (it = userLabels.begin(); it != userLabels.end(); it++) {
209 cout << "Your file does not include the label "<< *it;
210 if (processedLabels.count(lastList->getLabel()) != 1) {
211 cout << ". I will use " << lastList->getLabel() << "." << endl;
214 cout << ". Please refer to " << lastList->getLabel() << "." << endl;
218 //run last line if you need to
219 if (needToRun == true) {
220 error = process(lastList, count);
221 if (error == 1) { return 0; }
227 catch(exception& e) {
228 cout << "Standard Error: " << e.what() << " has occurred in the BinSeqCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
232 cout << "An unknown error has occurred in the BinSeqCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
237 //**********************************************************************************************************************
238 void BinSeqCommand::readNamesFile() {
240 vector<string> dupNames;
241 openInputFile(namesfile, inNames);
243 string name, names, sequence;
246 inNames >> name; //read from first column A
247 inNames >> names; //read from second column A,B,C,D
251 //parse names into vector
252 splitAtComma(names, dupNames);
254 //store names in fasta map
255 sequence = fasta->getSequence(name);
256 for (int i = 0; i < dupNames.size(); i++) {
257 fasta->push_back(dupNames[i], sequence);
265 catch(exception& e) {
266 cout << "Standard Error: " << e.what() << " has occurred in the BinSeqCommand class Function readNamesFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
270 cout << "An unknown error has occurred in the BinSeqCommand class function readNamesFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
274 //**********************************************************************************************************************
275 //return 1 if error, 0 otherwise
276 int BinSeqCommand::process(ListVector* list, int count) {
278 string binnames, name, sequence;
279 string outputFileName = getRootName(globaldata->getListFile()) + list->getLabel() + ".fasta";
280 openOutputFile(outputFileName, out);
282 cout << list->getLabel() << '\t' << count << endl;
284 //for each bin in the list vector
285 for (int i = 0; i < list->size(); i++) {
287 binnames = list->get(i);
288 while (binnames.find_first_of(',') != -1) {
289 name = binnames.substr(0,binnames.find_first_of(','));
290 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
292 //do work for that name
293 sequence = fasta->getSequence(name);
294 if (sequence != "not found") {
295 //if you don't have groups
296 if (groupfile == "") {
297 name = name + "|" + toString(i+1);
298 out << ">" << name << endl;
299 out << sequence << endl;
300 }else {//if you do have groups
301 string group = groupMap->getGroup(name);
302 if (group == "not found") {
303 cout << name << " is missing from your group file. Please correct. " << endl;
304 remove(outputFileName.c_str());
307 name = name + "|" + group + "|" + toString(i+1);
308 out << ">" << name << endl;
309 out << sequence << endl;
313 cout << name << " is missing from your fasta or name file. Please correct. " << endl;
314 remove(outputFileName.c_str());
321 sequence = fasta->getSequence(binnames);
322 if (sequence != "not found") {
323 //if you don't have groups
324 if (groupfile == "") {
325 binnames = binnames + "|" + toString(i+1);
326 out << ">" << binnames << endl;
327 out << sequence << endl;
328 }else {//if you do have groups
329 string group = groupMap->getGroup(binnames);
330 if (group == "not found") {
331 cout << binnames << " is missing from your group file. Please correct. " << endl;
332 remove(outputFileName.c_str());
335 binnames = binnames + "|" + group + "|" + toString(i+1);
336 out << ">" << binnames << endl;
337 out << sequence << endl;
341 cout << binnames << " is missing from your fasta or name file. Please correct. " << endl;
342 remove(outputFileName.c_str());
351 catch(exception& e) {
352 cout << "Standard Error: " << e.what() << " has occurred in the BinSeqCommand class Function readNamesFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
356 cout << "An unknown error has occurred in the BinSeqCommand class function readNamesFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
360 //**********************************************************************************************************************