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"
13 //**********************************************************************************************************************
14 vector<string> BinSeqCommand::setParameters(){
16 CommandParameter plist("list", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(plist);
17 CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pfasta);
18 CommandParameter pname("name", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pname);
19 CommandParameter pgroup("group", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pgroup);
20 CommandParameter plabel("label", "String", "", "", "", "", "",false,false); parameters.push_back(plabel);
21 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
22 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
24 vector<string> myArray;
25 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
29 m->errorOut(e, "BinSeqCommand", "setParameters");
33 //**********************************************************************************************************************
34 string BinSeqCommand::getHelpString(){
36 string helpString = "";
37 helpString += "The bin.seqs command parameters are list, fasta, name, label and group. The fasta and list are required, unless you have a valid current list and fasta file.\n";
38 helpString += "The label parameter allows you to select what distance levels you would like a output files created for, and are separated by dashes.\n";
39 helpString += "The bin.seqs command should be in the following format: bin.seqs(fasta=yourFastaFile, name=yourNamesFile, group=yourGroupFile, label=yourLabels).\n";
40 helpString += "Example bin.seqs(fasta=amazon.fasta, group=amazon.groups, name=amazon.names).\n";
41 helpString += "The default value for label is all lines in your inputfile.\n";
42 helpString += "The bin.seqs command outputs a .fasta file for each distance you specify appending the OTU number to each name.\n";
43 helpString += "If you provide a groupfile, then it also appends the sequences group to the name.\n";
44 helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFile).\n";
48 m->errorOut(e, "BinSeqCommand", "getHelpString");
52 //**********************************************************************************************************************
53 BinSeqCommand::BinSeqCommand(){
55 abort = true; calledHelp = true;
57 vector<string> tempOutNames;
58 outputTypes["fasta"] = tempOutNames;
61 m->errorOut(e, "BinSeqCommand", "BinSeqCommand");
65 //**********************************************************************************************************************
66 BinSeqCommand::BinSeqCommand(string option) {
68 abort = false; calledHelp = false;
72 //allow user to run help
73 if(option == "help") { help(); abort = true; calledHelp = true; }
76 vector<string> myArray = setParameters();
78 OptionParser parser(option);
79 map<string, string> parameters = parser.getParameters();
81 ValidParameters validParameter;
82 map<string, string>::iterator it;
84 //check to make sure all parameters are valid for command
85 for (it = parameters.begin(); it != parameters.end(); it++) {
86 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
89 //initialize outputTypes
90 vector<string> tempOutNames;
91 outputTypes["fasta"] = tempOutNames;
93 //if the user changes the input directory command factory will send this info to us in the output parameter
94 string inputDir = validParameter.validFile(parameters, "inputdir", false);
95 if (inputDir == "not found"){ inputDir = ""; }
98 it = parameters.find("fasta");
99 //user has given a template file
100 if(it != parameters.end()){
101 path = m->hasPath(it->second);
102 //if the user has not given a path then, add inputdir. else leave path alone.
103 if (path == "") { parameters["fasta"] = inputDir + it->second; }
106 it = parameters.find("list");
107 //user has given a template file
108 if(it != parameters.end()){
109 path = m->hasPath(it->second);
110 //if the user has not given a path then, add inputdir. else leave path alone.
111 if (path == "") { parameters["list"] = inputDir + it->second; }
114 it = parameters.find("name");
115 //user has given a template file
116 if(it != parameters.end()){
117 path = m->hasPath(it->second);
118 //if the user has not given a path then, add inputdir. else leave path alone.
119 if (path == "") { parameters["name"] = inputDir + it->second; }
122 it = parameters.find("group");
123 //user has given a template file
124 if(it != parameters.end()){
125 path = m->hasPath(it->second);
126 //if the user has not given a path then, add inputdir. else leave path alone.
127 if (path == "") { parameters["group"] = inputDir + it->second; }
132 //check for required parameters
133 fastafile = validParameter.validFile(parameters, "fasta", true);
134 if (fastafile == "not found") { //if there is a current phylip file, use it
135 fastafile = m->getFastaFile();
136 if (fastafile != "") { m->mothurOut("Using " + fastafile + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
137 else { m->mothurOut("You have no current fasta file and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
139 else if (fastafile == "not open") { abort = true; }
141 listfile = validParameter.validFile(parameters, "list", true);
142 if (listfile == "not found") {
143 listfile = m->getListFile();
144 if (listfile != "") { m->mothurOut("Using " + listfile + " as input file for the list parameter."); m->mothurOutEndLine(); }
145 else { m->mothurOut("You have no current list file and the list parameter is required."); m->mothurOutEndLine(); abort = true; }
147 else if (listfile == "not open") { listfile = ""; abort = true; }
149 //if the user changes the output directory command factory will send this info to us in the output parameter
150 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
152 outputDir += m->hasPath(listfile); //if user entered a file with a path then preserve it
156 //check for optional parameter and set defaults
157 // ...at some point should added some additional type checking...
159 label = validParameter.validFile(parameters, "label", false);
160 if (label == "not found") { label = ""; }
162 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
163 else { allLines = 1; }
166 namesfile = validParameter.validFile(parameters, "name", true);
167 if (namesfile == "not open") { abort = true; }
168 else if (namesfile == "not found") { namesfile = ""; }
170 groupfile = validParameter.validFile(parameters, "group", true);
171 if (groupfile == "not open") { abort = true; }
172 else if (groupfile == "not found") { groupfile = ""; }
176 catch(exception& e) {
177 m->errorOut(e, "BinSeqCommand", "BinSeqCommand");
181 //**********************************************************************************************************************
183 BinSeqCommand::~BinSeqCommand(){}
184 //**********************************************************************************************************************
186 int BinSeqCommand::execute(){
188 if (abort == true) { if (calledHelp) { return 0; } return 2; }
192 fasta = new FastaMap();
193 if (groupfile != "") {
194 groupMap = new GroupMap(groupfile);
199 fasta->readFastaFile(fastafile);
201 //if user gave a namesfile then use it
202 if (namesfile != "") {
206 input = new InputData(listfile, "list");
207 list = input->getListVector();
208 string lastLabel = list->getLabel();
210 if (m->control_pressed) { delete input; delete fasta; if (groupfile != "") { delete groupMap; } return 0; }
212 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
213 set<string> processedLabels;
214 set<string> userLabels = labels;
217 while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
219 if(m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } delete input; delete fasta; if (groupfile != "") { delete groupMap; } return 0; }
221 if(allLines == 1 || labels.count(list->getLabel()) == 1){
223 error = process(list);
224 if (error == 1) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } delete input; delete fasta; if (groupfile != "") { delete groupMap; } return 0; }
226 processedLabels.insert(list->getLabel());
227 userLabels.erase(list->getLabel());
230 if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
231 string saveLabel = list->getLabel();
234 list = input->getListVector(lastLabel);
236 error = process(list);
237 if (error == 1) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } delete input; delete fasta; if (groupfile != "") { delete groupMap; } return 0; }
239 processedLabels.insert(list->getLabel());
240 userLabels.erase(list->getLabel());
242 //restore real lastlabel to save below
243 list->setLabel(saveLabel);
246 lastLabel = list->getLabel();
249 list = input->getListVector();
252 if(m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } delete input; delete fasta; if (groupfile != "") { delete groupMap; } return 0; }
254 //output error messages about any remaining user labels
255 set<string>::iterator it;
256 bool needToRun = false;
257 for (it = userLabels.begin(); it != userLabels.end(); it++) {
258 m->mothurOut("Your file does not include the label " + *it);
259 if (processedLabels.count(lastLabel) != 1) {
260 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
263 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
267 //run last label if you need to
268 if (needToRun == true) {
269 if (list != NULL) { delete list; }
270 list = input->getListVector(lastLabel);
272 error = process(list);
273 if (error == 1) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } delete input; delete fasta; if (groupfile != "") { delete groupMap; } return 0; }
280 if (groupfile != "") { delete groupMap; }
282 if(m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
284 m->mothurOutEndLine();
285 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
286 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
287 m->mothurOutEndLine();
292 catch(exception& e) {
293 m->errorOut(e, "BinSeqCommand", "execute");
298 //**********************************************************************************************************************
299 void BinSeqCommand::readNamesFile() {
301 vector<string> dupNames;
302 m->openInputFile(namesfile, inNames);
304 string name, names, sequence;
307 inNames >> name; //read from first column A
308 inNames >> names; //read from second column A,B,C,D
312 //parse names into vector
313 m->splitAtComma(names, dupNames);
315 //store names in fasta map
316 sequence = fasta->getSequence(name);
317 for (int i = 0; i < dupNames.size(); i++) {
318 fasta->push_back(dupNames[i], sequence);
326 catch(exception& e) {
327 m->errorOut(e, "BinSeqCommand", "readNamesFile");
331 //**********************************************************************************************************************
332 //return 1 if error, 0 otherwise
333 int BinSeqCommand::process(ListVector* list) {
335 string binnames, name, sequence;
337 string outputFileName = outputDir + m->getRootName(m->getSimpleName(listfile)) + list->getLabel() + ".fasta";
338 m->openOutputFile(outputFileName, out);
340 //save to output list of output file names
341 outputNames.push_back(outputFileName); outputTypes["fasta"].push_back(outputFileName);
343 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
345 //for each bin in the list vector
346 for (int i = 0; i < list->size(); i++) {
348 if (m->control_pressed) { return 1; }
350 binnames = list->get(i);
351 while (binnames.find_first_of(',') != -1) {
352 name = binnames.substr(0,binnames.find_first_of(','));
353 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
355 //do work for that name
356 sequence = fasta->getSequence(name);
357 if (sequence != "not found") {
358 //if you don't have groups
359 if (groupfile == "") {
360 name = name + "\t" + toString(i+1);
361 out << ">" << name << endl;
362 out << sequence << endl;
363 }else {//if you do have groups
364 string group = groupMap->getGroup(name);
365 if (group == "not found") {
366 m->mothurOut(name + " is missing from your group file. Please correct. "); m->mothurOutEndLine();
369 name = name + "\t" + group + "\t" + toString(i+1);
370 out << ">" << name << endl;
371 out << sequence << endl;
375 m->mothurOut(name + " is missing from your fasta or name file. Please correct. "); m->mothurOutEndLine();
382 sequence = fasta->getSequence(binnames);
383 if (sequence != "not found") {
384 //if you don't have groups
385 if (groupfile == "") {
386 binnames = binnames + "\t" + toString(i+1);
387 out << ">" << binnames << endl;
388 out << sequence << endl;
389 }else {//if you do have groups
390 string group = groupMap->getGroup(binnames);
391 if (group == "not found") {
392 m->mothurOut(binnames + " is missing from your group file. Please correct. "); m->mothurOutEndLine();
395 binnames = binnames + "\t" + group + "\t" + toString(i+1);
396 out << ">" << binnames << endl;
397 out << sequence << endl;
401 m->mothurOut(binnames + " is missing from your fasta or name file. Please correct. "); m->mothurOutEndLine();
410 catch(exception& e) {
411 m->errorOut(e, "BinSeqCommand", "process");
415 //**********************************************************************************************************************