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(){
28 abort = true; calledHelp = true;
29 vector<string> tempOutNames;
30 outputTypes["fasta"] = tempOutNames;
33 m->errorOut(e, "DeUniqueSeqsCommand", "DeconvoluteCommand");
37 //**********************************************************************************************************************
38 vector<string> DeUniqueSeqsCommand::getRequiredParameters(){
40 string Array[] = {"fasta","name"};
41 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
45 m->errorOut(e, "DeUniqueSeqsCommand", "getRequiredParameters");
49 //**********************************************************************************************************************
50 vector<string> DeUniqueSeqsCommand::getRequiredFiles(){
52 vector<string> myArray;
56 m->errorOut(e, "DeUniqueSeqsCommand", "getRequiredFiles");
60 /**************************************************************************************/
61 DeUniqueSeqsCommand::DeUniqueSeqsCommand(string option) {
63 abort = false; calledHelp = false;
65 //allow user to run help
66 if(option == "help") { help(); abort = true; calledHelp = true; }
69 //valid paramters for this command
70 string Array[] = {"fasta", "name","outputdir","inputdir"};
71 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
73 OptionParser parser(option);
74 map<string,string> parameters = parser.getParameters();
76 ValidParameters validParameter;
77 map<string, string>::iterator it;
79 //check to make sure all parameters are valid for command
80 for (it = parameters.begin(); it != parameters.end(); it++) {
81 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
84 //initialize outputTypes
85 vector<string> tempOutNames;
86 outputTypes["fasta"] = tempOutNames;
88 //if the user changes the input directory command factory will send this info to us in the output parameter
89 string inputDir = validParameter.validFile(parameters, "inputdir", false);
90 if (inputDir == "not found"){ inputDir = ""; }
93 it = parameters.find("fasta");
94 //user has given a template file
95 if(it != parameters.end()){
96 path = m->hasPath(it->second);
97 //if the user has not given a path then, add inputdir. else leave path alone.
98 if (path == "") { parameters["fasta"] = inputDir + it->second; }
101 it = parameters.find("name");
102 //user has given a template file
103 if(it != parameters.end()){
104 path = m->hasPath(it->second);
105 //if the user has not given a path then, add inputdir. else leave path alone.
106 if (path == "") { parameters["name"] = inputDir + it->second; }
111 //check for required parameters
112 fastaFile = validParameter.validFile(parameters, "fasta", true);
113 if (fastaFile == "not open") { abort = true; }
114 else if (fastaFile == "not found") { fastaFile = ""; m->mothurOut("fasta is a required parameter for the deunique.seqs command."); m->mothurOutEndLine(); abort = true; }
116 //if the user changes the output directory command factory will send this info to us in the output parameter
117 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
119 outputDir += m->hasPath(fastaFile); //if user entered a file with a path then preserve it
122 nameFile = validParameter.validFile(parameters, "name", true);
123 if (nameFile == "not open") { abort = true; }
124 else if (nameFile == "not found"){ nameFile = ""; m->mothurOut("name is a required parameter for the deunique.seqs command."); m->mothurOutEndLine(); abort = true; }
128 catch(exception& e) {
129 m->errorOut(e, "DeUniqueSeqsCommand", "DeUniqueSeqsCommand");
133 //**********************************************************************************************************************
135 void DeUniqueSeqsCommand::help(){
137 m->mothurOut("The deunique.seqs command reads a fastafile and namefile, and creates a fastafile containing all the sequences.\n");
138 m->mothurOut("The deunique.seqs command parameters are fasta and name, both are required.\n");
139 m->mothurOut("The deunique.seqs command should be in the following format: \n");
140 m->mothurOut("deunique.seqs(fasta=yourFastaFile, name=yourNameFile) \n");
141 m->mothurOut("Example deunique.seqs(fasta=abrecovery.unique.fasta, name=abrecovery.names).\n");
142 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
145 catch(exception& e) {
146 m->errorOut(e, "DeUniqueSeqsCommand", "help");
151 /**************************************************************************************/
152 int DeUniqueSeqsCommand::execute() {
155 if (abort == true) { if (calledHelp) { return 0; } return 2; }
157 //prepare filenames and open files
159 string outFastaFile = m->getRootName(m->getSimpleName(fastaFile));
160 int pos = outFastaFile.find("unique");
161 if (pos != string::npos) {
162 outFastaFile = outputDir + outFastaFile.substr(0, pos) + "redundant" + m->getExtension(fastaFile);
164 outFastaFile = outputDir + m->getRootName(m->getSimpleName(fastaFile)) + "redundant" + m->getExtension(fastaFile);
166 m->openOutputFile(outFastaFile, out);
169 if (m->control_pressed) { out.close(); outputTypes.clear(); remove(outFastaFile.c_str()); return 0; }
172 m->openInputFile(fastaFile, in);
176 if (m->control_pressed) { in.close(); out.close(); outputTypes.clear(); remove(outFastaFile.c_str()); return 0; }
178 Sequence seq(in); m->gobble(in);
180 if (seq.getName() != "") {
182 //look for sequence name in nameMap
183 map<string, string>::iterator it = nameMap.find(seq.getName());
185 if (it == nameMap.end()) { m->mothurOut("[ERROR]: Your namefile does not contain " + seq.getName() + ", aborting."); m->mothurOutEndLine(); m->control_pressed = true; }
187 vector<string> names;
188 m->splitAtComma(it->second, names);
191 for (int i = 0; i < names.size(); i++) {
192 out << ">" << names[i] << endl;
193 out << seq.getAligned() << endl;
196 //remove seq from name map so we can check for seqs in namefile not in fastafile later
204 if (nameMap.size() != 0) { //then there are names in the namefile not in the fastafile
205 for (map<string, string>::iterator it = nameMap.begin(); it != nameMap.end(); it++) {
206 m->mothurOut(it->first + " is not in your fasta file, but is in your name file. Please correct."); m->mothurOutEndLine();
210 if (m->control_pressed) { outputTypes.clear(); remove(outFastaFile.c_str()); return 0; }
212 m->mothurOutEndLine();
213 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
214 m->mothurOut(outFastaFile); m->mothurOutEndLine();
215 outputNames.push_back(outFastaFile); outputTypes["fasta"].push_back(outFastaFile);
216 m->mothurOutEndLine();
220 catch(exception& e) {
221 m->errorOut(e, "DeUniqueSeqsCommand", "execute");
225 //**********************************************************************************************************************
226 int DeUniqueSeqsCommand::readNamesFile() {
230 m->openInputFile(nameFile, inNames);
233 map<string, string>::iterator it;
237 if(m->control_pressed) { break; }
239 inNames >> name; m->gobble(inNames);
242 it = nameMap.find(name);
244 if (it == nameMap.end()) { nameMap[name] = names; }
245 else { m->mothurOut("[ERROR]: Your namefile already contains " + name + ", aborting."); m->mothurOutEndLine(); m->control_pressed = true; }
254 catch(exception& e) {
255 m->errorOut(e, "DeUniqueSeqsCommand", "readNamesFile");
260 /**************************************************************************************/