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::getValidParameters(){
16 string Array[] = {"fasta", "name","outputdir","inputdir"};
17 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
21 m->errorOut(e, "DeUniqueSeqsCommand", "getValidParameters");
25 //**********************************************************************************************************************
26 DeUniqueSeqsCommand::DeUniqueSeqsCommand(){
29 //initialize outputTypes
30 vector<string> tempOutNames;
31 outputTypes["fasta"] = tempOutNames;
34 m->errorOut(e, "DeUniqueSeqsCommand", "DeconvoluteCommand");
38 //**********************************************************************************************************************
39 vector<string> DeUniqueSeqsCommand::getRequiredParameters(){
41 string Array[] = {"fasta","name"};
42 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
46 m->errorOut(e, "DeUniqueSeqsCommand", "getRequiredParameters");
50 //**********************************************************************************************************************
51 vector<string> DeUniqueSeqsCommand::getRequiredFiles(){
53 vector<string> myArray;
57 m->errorOut(e, "DeUniqueSeqsCommand", "getRequiredFiles");
61 /**************************************************************************************/
62 DeUniqueSeqsCommand::DeUniqueSeqsCommand(string option) {
66 //allow user to run help
67 if(option == "help") { help(); abort = true; }
70 //valid paramters for this command
71 string Array[] = {"fasta", "name","outputdir","inputdir"};
72 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
74 OptionParser parser(option);
75 map<string,string> parameters = parser.getParameters();
77 ValidParameters validParameter;
78 map<string, string>::iterator it;
80 //check to make sure all parameters are valid for command
81 for (it = parameters.begin(); it != parameters.end(); it++) {
82 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
85 //initialize outputTypes
86 vector<string> tempOutNames;
87 outputTypes["fasta"] = tempOutNames;
89 //if the user changes the input directory command factory will send this info to us in the output parameter
90 string inputDir = validParameter.validFile(parameters, "inputdir", false);
91 if (inputDir == "not found"){ inputDir = ""; }
94 it = parameters.find("fasta");
95 //user has given a template file
96 if(it != parameters.end()){
97 path = m->hasPath(it->second);
98 //if the user has not given a path then, add inputdir. else leave path alone.
99 if (path == "") { parameters["fasta"] = inputDir + it->second; }
102 it = parameters.find("name");
103 //user has given a template file
104 if(it != parameters.end()){
105 path = m->hasPath(it->second);
106 //if the user has not given a path then, add inputdir. else leave path alone.
107 if (path == "") { parameters["name"] = inputDir + it->second; }
112 //check for required parameters
113 fastaFile = validParameter.validFile(parameters, "fasta", true);
114 if (fastaFile == "not open") { abort = true; }
115 else if (fastaFile == "not found") { fastaFile = ""; m->mothurOut("fasta is a required parameter for the deunique.seqs command."); m->mothurOutEndLine(); abort = true; }
117 //if the user changes the output directory command factory will send this info to us in the output parameter
118 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
120 outputDir += m->hasPath(fastaFile); //if user entered a file with a path then preserve it
123 nameFile = validParameter.validFile(parameters, "name", true);
124 if (nameFile == "not open") { abort = true; }
125 else if (nameFile == "not found"){ nameFile = ""; m->mothurOut("name is a required parameter for the deunique.seqs command."); m->mothurOutEndLine(); abort = true; }
129 catch(exception& e) {
130 m->errorOut(e, "DeUniqueSeqsCommand", "DeUniqueSeqsCommand");
134 //**********************************************************************************************************************
136 void DeUniqueSeqsCommand::help(){
138 m->mothurOut("The deunique.seqs command reads a fastafile and namefile, and creates a fastafile containing all the sequences.\n");
139 m->mothurOut("The deunique.seqs command parameters are fasta and name, both are required.\n");
140 m->mothurOut("The deunique.seqs command should be in the following format: \n");
141 m->mothurOut("deunique.seqs(fasta=yourFastaFile, name=yourNameFile) \n");
142 m->mothurOut("Example deunique.seqs(fasta=abrecovery.unique.fasta, name=abrecovery.names).\n");
143 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
146 catch(exception& e) {
147 m->errorOut(e, "DeUniqueSeqsCommand", "help");
152 /**************************************************************************************/
153 int DeUniqueSeqsCommand::execute() {
156 if (abort == true) { return 0; }
158 //prepare filenames and open files
160 string outFastaFile = m->getRootName(m->getSimpleName(fastaFile));
161 int pos = outFastaFile.find("unique");
162 if (pos != string::npos) {
163 outFastaFile = outputDir + outFastaFile.substr(0, pos) + "redundant" + m->getExtension(fastaFile);
165 outFastaFile = outputDir + m->getRootName(m->getSimpleName(fastaFile)) + "redundant" + m->getExtension(fastaFile);
167 m->openOutputFile(outFastaFile, out);
170 if (m->control_pressed) { out.close(); outputTypes.clear(); remove(outFastaFile.c_str()); return 0; }
173 m->openInputFile(fastaFile, in);
177 if (m->control_pressed) { in.close(); out.close(); outputTypes.clear(); remove(outFastaFile.c_str()); return 0; }
179 Sequence seq(in); m->gobble(in);
181 if (seq.getName() != "") {
183 //look for sequence name in nameMap
184 map<string, string>::iterator it = nameMap.find(seq.getName());
186 if (it == nameMap.end()) { m->mothurOut("[ERROR]: Your namefile does not contain " + seq.getName() + ", aborting."); m->mothurOutEndLine(); m->control_pressed = true; }
188 vector<string> names;
189 m->splitAtComma(it->second, names);
192 for (int i = 0; i < names.size(); i++) {
193 out << ">" << names[i] << endl;
194 out << seq.getAligned() << endl;
197 //remove seq from name map so we can check for seqs in namefile not in fastafile later
205 if (nameMap.size() != 0) { //then there are names in the namefile not in the fastafile
206 for (map<string, string>::iterator it = nameMap.begin(); it != nameMap.end(); it++) {
207 m->mothurOut(it->first + " is not in your fasta file, but is in your name file. Please correct."); m->mothurOutEndLine();
211 if (m->control_pressed) { outputTypes.clear(); remove(outFastaFile.c_str()); return 0; }
213 m->mothurOutEndLine();
214 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
215 m->mothurOut(outFastaFile); m->mothurOutEndLine();
216 outputNames.push_back(outFastaFile); outputTypes["fasta"].push_back(outFastaFile);
217 m->mothurOutEndLine();
221 catch(exception& e) {
222 m->errorOut(e, "DeUniqueSeqsCommand", "execute");
226 //**********************************************************************************************************************
227 int DeUniqueSeqsCommand::readNamesFile() {
231 m->openInputFile(nameFile, inNames);
234 map<string, string>::iterator it;
238 if(m->control_pressed) { break; }
240 inNames >> name; m->gobble(inNames);
243 it = nameMap.find(name);
245 if (it == nameMap.end()) { nameMap[name] = names; }
246 else { m->mothurOut("[ERROR]: Your namefile already contains " + name + ", aborting."); m->mothurOutEndLine(); m->control_pressed = true; }
255 catch(exception& e) {
256 m->errorOut(e, "DeUniqueSeqsCommand", "readNamesFile");
261 /**************************************************************************************/