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"};
26 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
28 OptionParser parser(option);
29 map<string, string> parameters = parser.getParameters();
31 ValidParameters validParameter;
33 //check to make sure all parameters are valid for command
34 for (map<string, string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
35 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
38 //make sure the user has already run the read.otu command
39 if (globaldata->getListFile() == "") {
40 mothurOut("You must read a listfile before running the bin.seqs command.");
46 //check for required parameters
47 fastafile = validParameter.validFile(parameters, "fasta", true);
48 if (fastafile == "not found") { mothurOut("fasta is a required parameter for the bin.seqs command."); mothurOutEndLine(); abort = true; }
49 else if (fastafile == "not open") { abort = true; }
51 //check for optional parameter and set defaults
52 // ...at some point should added some additional type checking...
54 label = validParameter.validFile(parameters, "label", false);
55 if (label == "not found") { label = ""; }
57 if(label != "all") { splitAtDash(label, labels); allLines = 0; }
58 else { allLines = 1; }
61 //if the user has not specified any labels use the ones from read.otu
63 allLines = globaldata->allLines;
64 labels = globaldata->labels;
67 namesfile = validParameter.validFile(parameters, "name", true);
68 if (namesfile == "not open") { abort = true; }
69 else if (namesfile == "not found") { namesfile = ""; }
71 groupfile = validParameter.validFile(parameters, "group", true);
72 if (groupfile == "not open") { abort = true; }
73 else if (groupfile == "not found") { groupfile = ""; }
76 // openInputFile(fastafile, in);
77 fasta = new FastaMap();
78 if (groupfile != "") {
79 groupMap = new GroupMap(groupfile);
87 errorOut(e, "BinSeqCommand", "BinSeqCommand");
91 //**********************************************************************************************************************
93 void BinSeqCommand::help(){
95 mothurOut("The bin.seqs command can only be executed after a successful read.otu command of a listfile.\n");
96 mothurOut("The bin.seqs command parameters are fasta, name, label and group. The fasta parameter is required.\n");
97 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");
98 mothurOut("The bin.seqs command should be in the following format: bin.seqs(fasta=yourFastaFile, name=yourNamesFile, group=yourGroupFile, label=yourLabels).\n");
99 mothurOut("Example bin.seqs(fasta=amazon.fasta, group=amazon.groups, name=amazon.names).\n");
100 mothurOut("The default value for label is all lines in your inputfile.\n");
101 mothurOut("The bin.seqs command outputs a .fasta file for each distance you specify appending the OTU number to each name.\n");
102 mothurOut("If you provide a groupfile, then it also appends the sequences group to the name.\n");
103 mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFile).\n\n");
105 catch(exception& e) {
106 errorOut(e, "BinSeqCommand", "help");
111 //**********************************************************************************************************************
113 BinSeqCommand::~BinSeqCommand(){
114 //made new in execute
115 if (abort == false) {
116 delete input; globaldata->ginput = NULL;
118 globaldata->gListVector = NULL;
120 if (groupfile != "") { delete groupMap; globaldata->gGroupmap = NULL; }
124 //**********************************************************************************************************************
126 int BinSeqCommand::execute(){
128 if (abort == true) { return 0; }
133 fasta->readFastaFile(fastafile);
136 //set format to list so input can get listvector
137 // globaldata->setFormat("list");
139 //if user gave a namesfile then use it
140 if (namesfile != "") {
145 read = new ReadOTUFile(globaldata->getListFile());
146 read->read(&*globaldata);
148 input = globaldata->ginput;
149 list = globaldata->gListVector;
150 string lastLabel = list->getLabel();
152 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
153 set<string> processedLabels;
154 set<string> userLabels = labels;
157 while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
159 if(allLines == 1 || labels.count(list->getLabel()) == 1){
161 error = process(list);
162 if (error == 1) { return 0; }
164 processedLabels.insert(list->getLabel());
165 userLabels.erase(list->getLabel());
168 if ((anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
169 string saveLabel = list->getLabel();
172 list = input->getListVector(lastLabel);
174 error = process(list);
175 if (error == 1) { return 0; }
177 processedLabels.insert(list->getLabel());
178 userLabels.erase(list->getLabel());
180 //restore real lastlabel to save below
181 list->setLabel(saveLabel);
184 lastLabel = list->getLabel();
187 list = input->getListVector();
191 //output error messages about any remaining user labels
192 set<string>::iterator it;
193 bool needToRun = false;
194 for (it = userLabels.begin(); it != userLabels.end(); it++) {
195 mothurOut("Your file does not include the label " + *it);
196 if (processedLabels.count(lastLabel) != 1) {
197 mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine();
200 mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine();
204 //run last label if you need to
205 if (needToRun == true) {
206 if (list != NULL) { delete list; }
207 list = input->getListVector(lastLabel);
209 error = process(list);
210 if (error == 1) { return 0; }
217 catch(exception& e) {
218 errorOut(e, "BinSeqCommand", "execute");
223 //**********************************************************************************************************************
224 void BinSeqCommand::readNamesFile() {
226 vector<string> dupNames;
227 openInputFile(namesfile, inNames);
229 string name, names, sequence;
232 inNames >> name; //read from first column A
233 inNames >> names; //read from second column A,B,C,D
237 //parse names into vector
238 splitAtComma(names, dupNames);
240 //store names in fasta map
241 sequence = fasta->getSequence(name);
242 for (int i = 0; i < dupNames.size(); i++) {
243 fasta->push_back(dupNames[i], sequence);
251 catch(exception& e) {
252 errorOut(e, "BinSeqCommand", "readNamesFile");
256 //**********************************************************************************************************************
257 //return 1 if error, 0 otherwise
258 int BinSeqCommand::process(ListVector* list) {
260 string binnames, name, sequence;
261 string outputFileName = getRootName(globaldata->getListFile()) + list->getLabel() + ".fasta";
262 openOutputFile(outputFileName, out);
264 mothurOut(list->getLabel()); mothurOutEndLine();
266 //for each bin in the list vector
267 for (int i = 0; i < list->size(); i++) {
269 binnames = list->get(i);
270 while (binnames.find_first_of(',') != -1) {
271 name = binnames.substr(0,binnames.find_first_of(','));
272 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
274 //do work for that name
275 sequence = fasta->getSequence(name);
276 if (sequence != "not found") {
277 //if you don't have groups
278 if (groupfile == "") {
279 name = name + "|" + toString(i+1);
280 out << ">" << name << endl;
281 out << sequence << endl;
282 }else {//if you do have groups
283 string group = groupMap->getGroup(name);
284 if (group == "not found") {
285 mothurOut(name + " is missing from your group file. Please correct. "); mothurOutEndLine();
286 remove(outputFileName.c_str());
289 name = name + "|" + group + "|" + toString(i+1);
290 out << ">" << name << endl;
291 out << sequence << endl;
295 mothurOut(name + " is missing from your fasta or name file. Please correct. "); mothurOutEndLine();
296 remove(outputFileName.c_str());
303 sequence = fasta->getSequence(binnames);
304 if (sequence != "not found") {
305 //if you don't have groups
306 if (groupfile == "") {
307 binnames = binnames + "|" + toString(i+1);
308 out << ">" << binnames << endl;
309 out << sequence << endl;
310 }else {//if you do have groups
311 string group = groupMap->getGroup(binnames);
312 if (group == "not found") {
313 mothurOut(binnames + " is missing from your group file. Please correct. "); mothurOutEndLine();
314 remove(outputFileName.c_str());
317 binnames = binnames + "|" + group + "|" + toString(i+1);
318 out << ">" << binnames << endl;
319 out << sequence << endl;
323 mothurOut(binnames + " is missing from your fasta or name file. Please correct. "); mothurOutEndLine();
324 remove(outputFileName.c_str());
333 catch(exception& e) {
334 errorOut(e, "BinSeqCommand", "process");
338 //**********************************************************************************************************************