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; }
74 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
77 vector<string> myArray = setParameters();
79 OptionParser parser(option);
80 map<string, string> parameters = parser.getParameters();
82 ValidParameters validParameter;
83 map<string, string>::iterator it;
85 //check to make sure all parameters are valid for command
86 for (it = parameters.begin(); it != parameters.end(); it++) {
87 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
90 //initialize outputTypes
91 vector<string> tempOutNames;
92 outputTypes["fasta"] = tempOutNames;
94 //if the user changes the input directory command factory will send this info to us in the output parameter
95 string inputDir = validParameter.validFile(parameters, "inputdir", false);
96 if (inputDir == "not found"){ inputDir = ""; }
99 it = parameters.find("fasta");
100 //user has given a template file
101 if(it != parameters.end()){
102 path = m->hasPath(it->second);
103 //if the user has not given a path then, add inputdir. else leave path alone.
104 if (path == "") { parameters["fasta"] = inputDir + it->second; }
107 it = parameters.find("list");
108 //user has given a template file
109 if(it != parameters.end()){
110 path = m->hasPath(it->second);
111 //if the user has not given a path then, add inputdir. else leave path alone.
112 if (path == "") { parameters["list"] = inputDir + it->second; }
115 it = parameters.find("name");
116 //user has given a template file
117 if(it != parameters.end()){
118 path = m->hasPath(it->second);
119 //if the user has not given a path then, add inputdir. else leave path alone.
120 if (path == "") { parameters["name"] = inputDir + it->second; }
123 it = parameters.find("group");
124 //user has given a template file
125 if(it != parameters.end()){
126 path = m->hasPath(it->second);
127 //if the user has not given a path then, add inputdir. else leave path alone.
128 if (path == "") { parameters["group"] = inputDir + it->second; }
133 //check for required parameters
134 fastafile = validParameter.validFile(parameters, "fasta", true);
135 if (fastafile == "not found") { //if there is a current phylip file, use it
136 fastafile = m->getFastaFile();
137 if (fastafile != "") { m->mothurOut("Using " + fastafile + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
138 else { m->mothurOut("You have no current fasta file and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
140 else if (fastafile == "not open") { abort = true; }
141 else { m->setFastaFile(fastafile); }
143 listfile = validParameter.validFile(parameters, "list", true);
144 if (listfile == "not found") {
145 listfile = m->getListFile();
146 if (listfile != "") { m->mothurOut("Using " + listfile + " as input file for the list parameter."); m->mothurOutEndLine(); }
147 else { m->mothurOut("You have no current list file and the list parameter is required."); m->mothurOutEndLine(); abort = true; }
149 else if (listfile == "not open") { listfile = ""; abort = true; }
150 else { m->setListFile(listfile); }
152 //if the user changes the output directory command factory will send this info to us in the output parameter
153 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
155 outputDir += m->hasPath(listfile); //if user entered a file with a path then preserve it
159 //check for optional parameter and set defaults
160 // ...at some point should added some additional type checking...
162 label = validParameter.validFile(parameters, "label", false);
163 if (label == "not found") { label = ""; }
165 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
166 else { allLines = 1; }
169 namesfile = validParameter.validFile(parameters, "name", true);
170 if (namesfile == "not open") { abort = true; }
171 else if (namesfile == "not found") { namesfile = ""; }
172 else { m->setNameFile(namesfile); }
174 groupfile = validParameter.validFile(parameters, "group", true);
175 if (groupfile == "not open") { abort = true; }
176 else if (groupfile == "not found") { groupfile = ""; }
177 else { m->setGroupFile(groupfile); }
181 catch(exception& e) {
182 m->errorOut(e, "BinSeqCommand", "BinSeqCommand");
186 //**********************************************************************************************************************
188 BinSeqCommand::~BinSeqCommand(){}
189 //**********************************************************************************************************************
191 int BinSeqCommand::execute(){
193 if (abort == true) { if (calledHelp) { return 0; } return 2; }
197 fasta = new FastaMap();
198 if (groupfile != "") {
199 groupMap = new GroupMap(groupfile);
204 fasta->readFastaFile(fastafile);
206 //if user gave a namesfile then use it
207 if (namesfile != "") {
211 input = new InputData(listfile, "list");
212 list = input->getListVector();
213 string lastLabel = list->getLabel();
215 if (m->control_pressed) { delete input; delete fasta; if (groupfile != "") { delete groupMap; } return 0; }
217 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
218 set<string> processedLabels;
219 set<string> userLabels = labels;
222 while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
224 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; }
226 if(allLines == 1 || labels.count(list->getLabel()) == 1){
228 error = process(list);
229 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; }
231 processedLabels.insert(list->getLabel());
232 userLabels.erase(list->getLabel());
235 if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
236 string saveLabel = list->getLabel();
239 list = input->getListVector(lastLabel);
241 error = process(list);
242 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; }
244 processedLabels.insert(list->getLabel());
245 userLabels.erase(list->getLabel());
247 //restore real lastlabel to save below
248 list->setLabel(saveLabel);
251 lastLabel = list->getLabel();
254 list = input->getListVector();
257 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; }
259 //output error messages about any remaining user labels
260 set<string>::iterator it;
261 bool needToRun = false;
262 for (it = userLabels.begin(); it != userLabels.end(); it++) {
263 m->mothurOut("Your file does not include the label " + *it);
264 if (processedLabels.count(lastLabel) != 1) {
265 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
268 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
272 //run last label if you need to
273 if (needToRun == true) {
274 if (list != NULL) { delete list; }
275 list = input->getListVector(lastLabel);
277 error = process(list);
278 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; }
285 if (groupfile != "") { delete groupMap; }
287 if(m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
289 m->mothurOutEndLine();
290 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
291 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
292 m->mothurOutEndLine();
297 catch(exception& e) {
298 m->errorOut(e, "BinSeqCommand", "execute");
303 //**********************************************************************************************************************
304 void BinSeqCommand::readNamesFile() {
306 vector<string> dupNames;
307 m->openInputFile(namesfile, inNames);
309 string name, names, sequence;
312 inNames >> name; //read from first column A
313 inNames >> names; //read from second column A,B,C,D
317 //parse names into vector
318 m->splitAtComma(names, dupNames);
320 //store names in fasta map
321 sequence = fasta->getSequence(name);
322 for (int i = 0; i < dupNames.size(); i++) {
323 fasta->push_back(dupNames[i], sequence);
331 catch(exception& e) {
332 m->errorOut(e, "BinSeqCommand", "readNamesFile");
336 //**********************************************************************************************************************
337 //return 1 if error, 0 otherwise
338 int BinSeqCommand::process(ListVector* list) {
340 string binnames, name, sequence;
342 string outputFileName = outputDir + m->getRootName(m->getSimpleName(listfile)) + list->getLabel() + ".fasta";
343 m->openOutputFile(outputFileName, out);
345 //save to output list of output file names
346 outputNames.push_back(outputFileName); outputTypes["fasta"].push_back(outputFileName);
348 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
350 //for each bin in the list vector
351 for (int i = 0; i < list->size(); i++) {
353 if (m->control_pressed) { return 1; }
355 binnames = list->get(i);
356 while (binnames.find_first_of(',') != -1) {
357 name = binnames.substr(0,binnames.find_first_of(','));
358 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
360 //do work for that name
361 sequence = fasta->getSequence(name);
362 if (sequence != "not found") {
363 //if you don't have groups
364 if (groupfile == "") {
365 name = name + "\t" + toString(i+1);
366 out << ">" << name << endl;
367 out << sequence << endl;
368 }else {//if you do have groups
369 string group = groupMap->getGroup(name);
370 if (group == "not found") {
371 m->mothurOut(name + " is missing from your group file. Please correct. "); m->mothurOutEndLine();
374 name = name + "\t" + group + "\t" + toString(i+1);
375 out << ">" << name << endl;
376 out << sequence << endl;
380 m->mothurOut(name + " is missing from your fasta or name file. Please correct. "); m->mothurOutEndLine();
387 sequence = fasta->getSequence(binnames);
388 if (sequence != "not found") {
389 //if you don't have groups
390 if (groupfile == "") {
391 binnames = binnames + "\t" + toString(i+1);
392 out << ">" << binnames << endl;
393 out << sequence << endl;
394 }else {//if you do have groups
395 string group = groupMap->getGroup(binnames);
396 if (group == "not found") {
397 m->mothurOut(binnames + " is missing from your group file. Please correct. "); m->mothurOutEndLine();
400 binnames = binnames + "\t" + group + "\t" + toString(i+1);
401 out << ">" << binnames << endl;
402 out << sequence << endl;
406 m->mothurOut(binnames + " is missing from your fasta or name file. Please correct. "); m->mothurOutEndLine();
415 catch(exception& e) {
416 m->errorOut(e, "BinSeqCommand", "process");
420 //**********************************************************************************************************************