2 * deuniqueseqscommand.cpp
5 * Created by westcott on 10/19/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "deuniqueseqscommand.h"
11 #include "sequence.hpp"
13 //**********************************************************************************************************************
14 vector<string> DeUniqueSeqsCommand::setParameters(){
16 CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "none", "none","fasta",false,true,true); parameters.push_back(pfasta);
17 CommandParameter pname("name", "InputTypes", "", "", "none", "none", "none","",false,true,true); parameters.push_back(pname);
18 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
19 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
21 vector<string> myArray;
22 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
26 m->errorOut(e, "DeUniqueSeqsCommand", "setParameters");
30 //**********************************************************************************************************************
31 string DeUniqueSeqsCommand::getHelpString(){
33 string helpString = "";
34 helpString += "The deunique.seqs command reads a fastafile and namefile, and creates a fastafile containing all the sequences.\n";
35 helpString += "The deunique.seqs command parameters are fasta and name, both are required, unless you have valid current name and fasta files.\n";
36 helpString += "The deunique.seqs command should be in the following format: \n";
37 helpString += "deunique.seqs(fasta=yourFastaFile, name=yourNameFile) \n";
38 helpString += "Example deunique.seqs(fasta=abrecovery.unique.fasta, name=abrecovery.names).\n";
39 helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n";
43 m->errorOut(e, "DeUniqueSeqsCommand", "getHelpString");
47 //**********************************************************************************************************************
48 string DeUniqueSeqsCommand::getOutputPattern(string type) {
52 if (type == "fasta") { pattern = "[filename],redundant.fasta"; }
53 else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; }
58 m->errorOut(e, "DeUniqueSeqsCommand", "getOutputPattern");
62 //**********************************************************************************************************************
63 DeUniqueSeqsCommand::DeUniqueSeqsCommand(){
65 abort = true; calledHelp = true;
67 vector<string> tempOutNames;
68 outputTypes["fasta"] = tempOutNames;
71 m->errorOut(e, "DeUniqueSeqsCommand", "DeconvoluteCommand");
75 /**************************************************************************************/
76 DeUniqueSeqsCommand::DeUniqueSeqsCommand(string option) {
78 abort = false; calledHelp = false;
80 //allow user to run help
81 if(option == "help") { help(); abort = true; calledHelp = true; }
82 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
85 vector<string> myArray = setParameters();
87 OptionParser parser(option);
88 map<string,string> parameters = parser.getParameters();
90 ValidParameters validParameter;
91 map<string, string>::iterator it;
93 //check to make sure all parameters are valid for command
94 for (it = parameters.begin(); it != parameters.end(); it++) {
95 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
98 //initialize outputTypes
99 vector<string> tempOutNames;
100 outputTypes["fasta"] = tempOutNames;
102 //if the user changes the input directory command factory will send this info to us in the output parameter
103 string inputDir = validParameter.validFile(parameters, "inputdir", false);
104 if (inputDir == "not found"){ inputDir = ""; }
107 it = parameters.find("fasta");
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["fasta"] = 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; }
125 //check for required parameters
126 fastaFile = validParameter.validFile(parameters, "fasta", true);
127 if (fastaFile == "not open") { abort = true; }
128 else if (fastaFile == "not found") {
129 fastaFile = m->getFastaFile();
130 if (fastaFile != "") { m->mothurOut("Using " + fastaFile + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
131 else { m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
132 }else { m->setFastaFile(fastaFile); }
134 //if the user changes the output directory command factory will send this info to us in the output parameter
135 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
137 outputDir += m->hasPath(fastaFile); //if user entered a file with a path then preserve it
140 nameFile = validParameter.validFile(parameters, "name", true);
141 if (nameFile == "not open") { abort = true; }
142 else if (nameFile == "not found"){
143 nameFile = m->getNameFile();
144 if (nameFile != "") { m->mothurOut("Using " + nameFile + " as input file for the name parameter."); m->mothurOutEndLine(); }
145 else { m->mothurOut("You have no current namefile and the name parameter is required."); m->mothurOutEndLine(); abort = true; }
146 }else { m->setNameFile(nameFile); }
150 catch(exception& e) {
151 m->errorOut(e, "DeUniqueSeqsCommand", "DeUniqueSeqsCommand");
155 /**************************************************************************************/
156 int DeUniqueSeqsCommand::execute() {
159 if (abort == true) { if (calledHelp) { return 0; } return 2; }
161 //prepare filenames and open files
163 string outFastaFile = m->getRootName(m->getSimpleName(fastaFile));
164 int pos = outFastaFile.find("unique");
165 if (pos != string::npos) { outFastaFile = outputDir + outFastaFile.substr(0, pos); }
166 else { outFastaFile = outputDir + outFastaFile; }
167 map<string, string> variables;
168 variables["[filename]"] = outFastaFile;
169 outFastaFile = getOutputFileName("fasta", variables);
170 m->openOutputFile(outFastaFile, out);
173 if (m->control_pressed) { out.close(); outputTypes.clear(); m->mothurRemove(outFastaFile); return 0; }
176 m->openInputFile(fastaFile, in);
180 if (m->control_pressed) { in.close(); out.close(); outputTypes.clear(); m->mothurRemove(outFastaFile); return 0; }
182 Sequence seq(in); m->gobble(in);
184 if (seq.getName() != "") {
186 //look for sequence name in nameMap
187 map<string, string>::iterator it = nameMap.find(seq.getName());
189 if (it == nameMap.end()) { m->mothurOut("[ERROR]: Your namefile does not contain " + seq.getName() + ", aborting."); m->mothurOutEndLine(); m->control_pressed = true; }
191 vector<string> names;
192 m->splitAtComma(it->second, names);
195 for (int i = 0; i < names.size(); i++) {
196 out << ">" << names[i] << endl;
197 out << seq.getAligned() << endl;
200 //remove seq from name map so we can check for seqs in namefile not in fastafile later
208 if (nameMap.size() != 0) { //then there are names in the namefile not in the fastafile
209 for (map<string, string>::iterator it = nameMap.begin(); it != nameMap.end(); it++) {
210 m->mothurOut(it->first + " is not in your fasta file, but is in your name file. Please correct."); m->mothurOutEndLine();
214 if (m->control_pressed) { outputTypes.clear(); m->mothurRemove(outFastaFile); return 0; }
216 m->mothurOutEndLine();
217 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
218 m->mothurOut(outFastaFile); m->mothurOutEndLine();
219 outputNames.push_back(outFastaFile); outputTypes["fasta"].push_back(outFastaFile);
220 m->mothurOutEndLine();
222 //set fasta file as new current fastafile
224 itTypes = outputTypes.find("fasta");
225 if (itTypes != outputTypes.end()) {
226 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
231 catch(exception& e) {
232 m->errorOut(e, "DeUniqueSeqsCommand", "execute");
236 //**********************************************************************************************************************
237 int DeUniqueSeqsCommand::readNamesFile() {
241 m->openInputFile(nameFile, inNames);
244 map<string, string>::iterator it;
248 if(m->control_pressed) { break; }
250 inNames >> name; m->gobble(inNames);
253 it = nameMap.find(name);
255 if (it == nameMap.end()) { nameMap[name] = names; }
256 else { m->mothurOut("[ERROR]: Your namefile already contains " + name + ", aborting."); m->mothurOutEndLine(); m->control_pressed = true; }
265 catch(exception& e) {
266 m->errorOut(e, "DeUniqueSeqsCommand", "readNamesFile");
271 /**************************************************************************************/