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 openInputFile(fastafile, in);
49 fasta = new FastaMap();
53 //check for optional parameter and set defaults
54 // ...at some point should added some additional type checking...
55 line = validParameter.validFile(parameters, "line", false);
56 if (line == "not found") { line = ""; }
58 if(line != "all") { splitAtDash(line, lines); allLines = 0; }
59 else { allLines = 1; }
62 label = validParameter.validFile(parameters, "label", false);
63 if (label == "not found") { label = ""; }
65 if(label != "all") { splitAtDash(label, labels); allLines = 0; }
66 else { allLines = 1; }
69 //make sure user did not use both the line and label parameters
70 if ((line != "") && (label != "")) { cout << "You cannot use both the line and label parameters at the same time. " << endl; abort = true; }
71 //if the user has not specified any line or labels use the ones from read.otu
72 else if ((line == "") && (label == "")) {
73 allLines = globaldata->allLines;
74 labels = globaldata->labels;
75 lines = globaldata->lines;
78 namesfile = validParameter.validFile(parameters, "name", true);
79 if (namesfile == "not open") { abort = true; }
80 else if (namesfile == "not found") { namesfile = ""; }
82 groupfile = validParameter.validFile(parameters, "group", true);
83 if (groupfile == "not open") { abort = true; }
84 else if (groupfile == "not found") { groupfile = ""; }
86 //read in group map info.
87 groupMap = new GroupMap(groupfile);
94 cout << "Standard Error: " << e.what() << " has occurred in the BinSeqCommand class Function BinSeqCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
98 cout << "An unknown error has occurred in the BinSeqCommand class function BinSeqCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
102 //**********************************************************************************************************************
104 void BinSeqCommand::help(){
106 cout << "The bin.seqs command can only be executed after a successful read.otu command of a listfile." << "\n";
107 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";
108 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";
109 cout << "The bin.seqs command should be in the following format: bin.seqs(fasta=yourFastaFile, name=yourNamesFile, group=yourGroupFile, line=yourLines, label=yourLabels)." << "\n";
110 cout << "Example bin.seqs(fasta=amazon.fasta, group=amazon.groups, line=1-3-5, name=amazon.names)." << "\n";
111 cout << "The default value for line and label are all lines in your inputfile." << "\n";
112 cout << "The bin.seqs command outputs a .fasta file for each distance you specify appending the OTU number to each name." << "\n";
113 cout << "If you provide a groupfile, then it also appends the sequences group to the name." << "\n";
114 cout << "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFile)." << "\n" << "\n";
116 catch(exception& e) {
117 cout << "Standard Error: " << e.what() << " has occurred in the BinSeqCommand class Function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
121 cout << "An unknown error has occurred in the BinSeqCommand class function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
128 //**********************************************************************************************************************
130 BinSeqCommand::~BinSeqCommand(){
131 //made new in execute
132 if (abort == false) {
138 //made new in constructor
140 if (groupfile != "") {
146 //**********************************************************************************************************************
148 int BinSeqCommand::execute(){
150 if (abort == true) { return 0; }
156 fasta->readFastaFile(in);
158 //set format to list so input can get listvector
159 // globaldata->setFormat("list");
161 //if user gave a namesfile then use it
162 if (namesfile != "") {
167 read = new ReadOTUFile(globaldata->getListFile());
168 read->read(&*globaldata);
170 input = globaldata->ginput;
171 list = globaldata->gListVector;
172 ListVector* lastList = list;
174 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
175 set<string> processedLabels;
176 set<string> userLabels = labels;
177 set<int> userLines = lines;
180 while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
182 if(allLines == 1 || lines.count(count) == 1 || labels.count(list->getLabel()) == 1){
184 error = process(list, count);
185 if (error == 1) { return 0; }
187 processedLabels.insert(list->getLabel());
188 userLabels.erase(list->getLabel());
189 userLines.erase(count);
192 if ((anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastList->getLabel()) != 1)) {
194 error = process(lastList, count);
195 if (error == 1) { return 0; }
197 processedLabels.insert(lastList->getLabel());
198 userLabels.erase(lastList->getLabel());
202 if (count != 1) { delete lastList; }
205 list = input->getListVector();
210 //output error messages about any remaining user labels
211 set<string>::iterator it;
212 bool needToRun = false;
213 for (it = userLabels.begin(); it != userLabels.end(); it++) {
214 cout << "Your file does not include the label "<< *it;
215 if (processedLabels.count(lastList->getLabel()) != 1) {
216 cout << ". I will use " << lastList->getLabel() << "." << endl;
219 cout << ". Please refer to " << lastList->getLabel() << "." << endl;
223 //run last line if you need to
224 if (needToRun == true) {
225 error = process(lastList, count);
226 if (error == 1) { return 0; }
232 catch(exception& e) {
233 cout << "Standard Error: " << e.what() << " has occurred in the BinSeqCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
237 cout << "An unknown error has occurred in the BinSeqCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
242 //**********************************************************************************************************************
243 void BinSeqCommand::readNamesFile() {
245 vector<string> dupNames;
246 openInputFile(namesfile, inNames);
248 string name, names, sequence;
251 inNames >> name; //read from first column A
252 inNames >> names; //read from second column A,B,C,D
256 //parse names into vector
257 splitAtComma(names, dupNames);
259 //store names in fasta map
260 sequence = fasta->getSequence(name);
261 for (int i = 0; i < dupNames.size(); i++) {
262 fasta->push_back(dupNames[i], sequence);
270 catch(exception& e) {
271 cout << "Standard Error: " << e.what() << " has occurred in the BinSeqCommand class Function readNamesFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
275 cout << "An unknown error has occurred in the BinSeqCommand class function readNamesFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
279 //**********************************************************************************************************************
280 //return 1 if error, 0 otherwise
281 int BinSeqCommand::process(ListVector* list, int count) {
283 string binnames, name, sequence;
284 string outputFileName = getRootName(globaldata->getListFile()) + list->getLabel() + ".fasta";
285 openOutputFile(outputFileName, out);
287 cout << list->getLabel() << '\t' << count << endl;
289 //for each bin in the list vector
290 for (int i = 0; i < list->size(); i++) {
292 binnames = list->get(i);
293 while (binnames.find_first_of(',') != -1) {
294 name = binnames.substr(0,binnames.find_first_of(','));
295 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
297 //do work for that name
298 sequence = fasta->getSequence(name);
299 if (sequence != "not found") {
300 //if you don't have groups
301 if (groupfile == "") {
302 name = name + "|" + toString(i+1);
303 out << ">" << name << endl;
304 out << sequence << endl;
305 }else {//if you do have groups
306 string group = groupMap->getGroup(name);
307 if (group == "not found") {
308 cout << name << " is missing from your group file. Please correct. " << endl;
309 remove(outputFileName.c_str());
312 name = name + "|" + group + "|" + toString(i+1);
313 out << ">" << name << endl;
314 out << sequence << endl;
318 cout << name << " is missing from your fasta or name file. Please correct. " << endl;
319 remove(outputFileName.c_str());
326 sequence = fasta->getSequence(binnames);
327 if (sequence != "not found") {
328 //if you don't have groups
329 if (groupfile == "") {
330 binnames = binnames + "|" + toString(i+1);
331 out << ">" << binnames << endl;
332 out << sequence << endl;
333 }else {//if you do have groups
334 string group = groupMap->getGroup(binnames);
335 if (group == "not found") {
336 cout << binnames << " is missing from your group file. Please correct. " << endl;
337 remove(outputFileName.c_str());
340 binnames = binnames + "|" + group + "|" + toString(i+1);
341 out << ">" << binnames << endl;
342 out << sequence << endl;
346 cout << binnames << " is missing from your fasta or name file. Please correct. " << endl;
347 remove(outputFileName.c_str());
356 catch(exception& e) {
357 cout << "Standard Error: " << e.what() << " has occurred in the BinSeqCommand class Function readNamesFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
361 cout << "An unknown error has occurred in the BinSeqCommand class function readNamesFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
365 //**********************************************************************************************************************