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",false,true); parameters.push_back(pfasta);
17 CommandParameter pname("name", "InputTypes", "", "", "none", "none", "none",false,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::getOutputFileNameTag(string type, string inputName=""){
50 string outputFileName = "";
51 map<string, vector<string> >::iterator it;
53 //is this a type this command creates
54 it = outputTypes.find(type);
55 if (it == outputTypes.end()) { m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
57 if (type == "fasta") { outputFileName = "redundant.fasta"; }
58 else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true; }
60 return outputFileName;
63 m->errorOut(e, "DeUniqueSeqsCommand", "getOutputFileNameTag");
67 //**********************************************************************************************************************
68 DeUniqueSeqsCommand::DeUniqueSeqsCommand(){
70 abort = true; calledHelp = true;
72 vector<string> tempOutNames;
73 outputTypes["fasta"] = tempOutNames;
76 m->errorOut(e, "DeUniqueSeqsCommand", "DeconvoluteCommand");
80 /**************************************************************************************/
81 DeUniqueSeqsCommand::DeUniqueSeqsCommand(string option) {
83 abort = false; calledHelp = false;
85 //allow user to run help
86 if(option == "help") { help(); abort = true; calledHelp = true; }
87 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
90 vector<string> myArray = setParameters();
92 OptionParser parser(option);
93 map<string,string> parameters = parser.getParameters();
95 ValidParameters validParameter;
96 map<string, string>::iterator it;
98 //check to make sure all parameters are valid for command
99 for (it = parameters.begin(); it != parameters.end(); it++) {
100 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
103 //initialize outputTypes
104 vector<string> tempOutNames;
105 outputTypes["fasta"] = tempOutNames;
107 //if the user changes the input directory command factory will send this info to us in the output parameter
108 string inputDir = validParameter.validFile(parameters, "inputdir", false);
109 if (inputDir == "not found"){ inputDir = ""; }
112 it = parameters.find("fasta");
113 //user has given a template file
114 if(it != parameters.end()){
115 path = m->hasPath(it->second);
116 //if the user has not given a path then, add inputdir. else leave path alone.
117 if (path == "") { parameters["fasta"] = inputDir + it->second; }
120 it = parameters.find("name");
121 //user has given a template file
122 if(it != parameters.end()){
123 path = m->hasPath(it->second);
124 //if the user has not given a path then, add inputdir. else leave path alone.
125 if (path == "") { parameters["name"] = inputDir + it->second; }
130 //check for required parameters
131 fastaFile = validParameter.validFile(parameters, "fasta", true);
132 if (fastaFile == "not open") { abort = true; }
133 else if (fastaFile == "not found") {
134 fastaFile = m->getFastaFile();
135 if (fastaFile != "") { m->mothurOut("Using " + fastaFile + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
136 else { m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
137 }else { m->setFastaFile(fastaFile); }
139 //if the user changes the output directory command factory will send this info to us in the output parameter
140 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
142 outputDir += m->hasPath(fastaFile); //if user entered a file with a path then preserve it
145 nameFile = validParameter.validFile(parameters, "name", true);
146 if (nameFile == "not open") { abort = true; }
147 else if (nameFile == "not found"){
148 nameFile = m->getNameFile();
149 if (nameFile != "") { m->mothurOut("Using " + nameFile + " as input file for the name parameter."); m->mothurOutEndLine(); }
150 else { m->mothurOut("You have no current namefile and the name parameter is required."); m->mothurOutEndLine(); abort = true; }
151 }else { m->setNameFile(nameFile); }
155 catch(exception& e) {
156 m->errorOut(e, "DeUniqueSeqsCommand", "DeUniqueSeqsCommand");
160 /**************************************************************************************/
161 int DeUniqueSeqsCommand::execute() {
164 if (abort == true) { if (calledHelp) { return 0; } return 2; }
166 //prepare filenames and open files
168 string outFastaFile = m->getRootName(m->getSimpleName(fastaFile));
169 int pos = outFastaFile.find("unique");
170 if (pos != string::npos) {
171 outFastaFile = outputDir + outFastaFile.substr(0, pos) + getOutputFileNameTag("fasta");
173 outFastaFile = outputDir + m->getRootName(m->getSimpleName(fastaFile)) + getOutputFileNameTag("fasta");
175 m->openOutputFile(outFastaFile, out);
178 if (m->control_pressed) { out.close(); outputTypes.clear(); m->mothurRemove(outFastaFile); return 0; }
181 m->openInputFile(fastaFile, in);
185 if (m->control_pressed) { in.close(); out.close(); outputTypes.clear(); m->mothurRemove(outFastaFile); return 0; }
187 Sequence seq(in); m->gobble(in);
189 if (seq.getName() != "") {
191 //look for sequence name in nameMap
192 map<string, string>::iterator it = nameMap.find(seq.getName());
194 if (it == nameMap.end()) { m->mothurOut("[ERROR]: Your namefile does not contain " + seq.getName() + ", aborting."); m->mothurOutEndLine(); m->control_pressed = true; }
196 vector<string> names;
197 m->splitAtComma(it->second, names);
200 for (int i = 0; i < names.size(); i++) {
201 out << ">" << names[i] << endl;
202 out << seq.getAligned() << endl;
205 //remove seq from name map so we can check for seqs in namefile not in fastafile later
213 if (nameMap.size() != 0) { //then there are names in the namefile not in the fastafile
214 for (map<string, string>::iterator it = nameMap.begin(); it != nameMap.end(); it++) {
215 m->mothurOut(it->first + " is not in your fasta file, but is in your name file. Please correct."); m->mothurOutEndLine();
219 if (m->control_pressed) { outputTypes.clear(); m->mothurRemove(outFastaFile); return 0; }
221 m->mothurOutEndLine();
222 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
223 m->mothurOut(outFastaFile); m->mothurOutEndLine();
224 outputNames.push_back(outFastaFile); outputTypes["fasta"].push_back(outFastaFile);
225 m->mothurOutEndLine();
227 //set fasta file as new current fastafile
229 itTypes = outputTypes.find("fasta");
230 if (itTypes != outputTypes.end()) {
231 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
236 catch(exception& e) {
237 m->errorOut(e, "DeUniqueSeqsCommand", "execute");
241 //**********************************************************************************************************************
242 int DeUniqueSeqsCommand::readNamesFile() {
246 m->openInputFile(nameFile, inNames);
249 map<string, string>::iterator it;
253 if(m->control_pressed) { break; }
255 inNames >> name; m->gobble(inNames);
258 it = nameMap.find(name);
260 if (it == nameMap.end()) { nameMap[name] = names; }
261 else { m->mothurOut("[ERROR]: Your namefile already contains " + name + ", aborting."); m->mothurOutEndLine(); m->control_pressed = true; }
270 catch(exception& e) {
271 m->errorOut(e, "DeUniqueSeqsCommand", "readNamesFile");
276 /**************************************************************************************/