From 21805b1c466a341d64a5a451f156a56d12c75dca Mon Sep 17 00:00:00 2001 From: westcott Date: Mon, 10 May 2010 18:28:14 +0000 Subject: [PATCH] added chop.seqs command --- Mothur.xcodeproj/project.pbxproj | 6 +- chopseqscommand.cpp | 144 +++++++++++++++++++++++++++++++ chopseqscommand.h | 33 +++++++ commandfactory.cpp | 3 + listseqscommand.cpp | 1 + makefile | 14 ++- makegroupcommand.cpp | 6 +- 7 files changed, 201 insertions(+), 6 deletions(-) create mode 100644 chopseqscommand.cpp create mode 100644 chopseqscommand.h diff --git a/Mothur.xcodeproj/project.pbxproj b/Mothur.xcodeproj/project.pbxproj index 29de1db..f9fd39e 100644 --- a/Mothur.xcodeproj/project.pbxproj +++ b/Mothur.xcodeproj/project.pbxproj @@ -13,6 +13,8 @@ A72B3A63118B37FD004B9F8D /* phylodiversitycommand.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = phylodiversitycommand.cpp; sourceTree = ""; }; A72B3A7B118B4D1B004B9F8D /* phylodiversity.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = phylodiversity.h; sourceTree = ""; }; A72B3A7C118B4D1B004B9F8D /* phylodiversity.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = phylodiversity.cpp; sourceTree = ""; }; + A73953DA11987ED100B0B160 /* chopseqscommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = chopseqscommand.h; sourceTree = ""; }; + A73953DB11987ED100B0B160 /* chopseqscommand.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = chopseqscommand.cpp; sourceTree = ""; }; A747E79B1163442A00FB9042 /* chimeracheckcommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = chimeracheckcommand.h; sourceTree = ""; }; A747E79C1163442A00FB9042 /* chimeracheckcommand.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = chimeracheckcommand.cpp; sourceTree = ""; }; A747E81C116365E000FB9042 /* chimeraslayercommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = chimeraslayercommand.h; sourceTree = ""; }; @@ -643,17 +645,19 @@ A7DA2007113FECD400BF472F /* bootstrapsharedcommand.cpp */, A7DA2008113FECD400BF472F /* bootstrapsharedcommand.h */, A7DA2017113FECD400BF472F /* chimeraseqscommand.cpp */, + A78434881162224F00100BE0 /* chimeraccodecommand.h */, A7DA2018113FECD400BF472F /* chimeraseqscommand.h */, A7825502116519F70002E2DD /* chimerabellerophoncommand.h */, A7825503116519F70002E2DD /* chimerabellerophoncommand.cpp */, A747E79B1163442A00FB9042 /* chimeracheckcommand.h */, A747E79C1163442A00FB9042 /* chimeracheckcommand.cpp */, - A78434881162224F00100BE0 /* chimeraccodecommand.h */, A78434891162224F00100BE0 /* chimeraccodecommand.cpp */, A78254461164D7790002E2DD /* chimerapintailcommand.h */, A78254471164D7790002E2DD /* chimerapintailcommand.cpp */, A747E81C116365E000FB9042 /* chimeraslayercommand.h */, A747E81D116365E000FB9042 /* chimeraslayercommand.cpp */, + A73953DA11987ED100B0B160 /* chopseqscommand.h */, + A73953DB11987ED100B0B160 /* chopseqscommand.cpp */, A7DA201E113FECD400BF472F /* classifyseqscommand.h */, A7DA201D113FECD400BF472F /* classifyseqscommand.cpp */, A7DA2021113FECD400BF472F /* clustercommand.cpp */, diff --git a/chopseqscommand.cpp b/chopseqscommand.cpp new file mode 100644 index 0000000..adf0910 --- /dev/null +++ b/chopseqscommand.cpp @@ -0,0 +1,144 @@ +/* + * chopseqscommand.cpp + * Mothur + * + * Created by westcott on 5/10/10. + * Copyright 2010 Schloss Lab. All rights reserved. + * + */ + +#include "chopseqscommand.h" +#include "sequence.hpp" + +//********************************************************************************************************************** + +ChopSeqsCommand::ChopSeqsCommand(string option) { + try { + abort = false; + + //allow user to run help + if(option == "help") { help(); abort = true; } + + else { + //valid paramters for this command + string Array[] = {"fasta","end","outputdir","inputdir"}; + vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); + + OptionParser parser(option); + map parameters = parser.getParameters(); + + ValidParameters validParameter; + map::iterator it; + + //check to make sure all parameters are valid for command + for (map::iterator it = parameters.begin(); it != parameters.end(); it++) { + if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; } + } + + //if the user changes the output directory command factory will send this info to us in the output parameter + outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; } + + //if the user changes the input directory command factory will send this info to us in the output parameter + string inputDir = validParameter.validFile(parameters, "inputdir", false); + if (inputDir == "not found"){ inputDir = ""; } + else { + string path; + it = parameters.find("fasta"); + //user has given a template file + if(it != parameters.end()){ + path = hasPath(it->second); + //if the user has not given a path then, add inputdir. else leave path alone. + if (path == "") { parameters["fasta"] = inputDir + it->second; } + } + } + + //check for required parameters + fastafile = validParameter.validFile(parameters, "fasta", true); + if (fastafile == "not open") { abort = true; } + else if (fastafile == "not found") { m->mothurOut("You must provide a fasta file."); m->mothurOutEndLine(); abort = true; } + + string temp = validParameter.validFile(parameters, "end", false); + if (temp == "not found") { m->mothurOut("You must provide an end for the chops.seqs command."); m->mothurOutEndLine(); abort = true; } + else { + convert(temp, end); + if (end < 0) { m->mothurOut("End must be positive."); m->mothurOutEndLine(); abort = true; } + } + + } + + } + catch(exception& e) { + m->errorOut(e, "ChopSeqsCommand", "ChopSeqsCommand"); + exit(1); + } +} +//********************************************************************************************************************** + +void ChopSeqsCommand::help(){ + try { + m->mothurOut("The chop.seqs command reads a fasta file and outputs a .chop.fasta with sequences trimmed to the end position.\n"); + m->mothurOut("The chop.seqs command parameters are fasta and end, both are required.\n"); + m->mothurOut("The chop.seqs command should be in the following format: chop.seqs(fasta=yourFasta, end=yourEnd).\n"); + m->mothurOut("Example chop.seqs(fasta=amazon.fasta, end=200).\n"); + m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n"); + } + catch(exception& e) { + m->errorOut(e, "ChopSeqsCommand", "help"); + exit(1); + } +} + +//********************************************************************************************************************** + +int ChopSeqsCommand::execute(){ + try { + + if (abort == true) { return 0; } + + string outputFileName = outputDir + getRootName(getSimpleName(fastafile)) + "chop.fasta"; + + ofstream out; + openOutputFile(outputFileName, out); + + ifstream in; + openInputFile(fastafile, in); + + while (!in.eof()) { + + Sequence seq(in, "no align"); + + if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; } + + if (seq.getName() != "") { + string temp = seq.getUnaligned(); + + //output sequence name + out << ">" << seq.getName() << endl; + + //if needed trim sequence + if (temp.length() > end) { temp = temp.substr(0, end); } + + //output trimmed sequence + out << temp << endl; + } + } + in.close(); + out.close(); + + m->mothurOutEndLine(); + m->mothurOut("Output File Name: "); m->mothurOutEndLine(); + m->mothurOut(outputFileName); m->mothurOutEndLine(); + m->mothurOutEndLine(); + + return 0; + } + + catch(exception& e) { + m->errorOut(e, "ChopSeqsCommand", "execute"); + exit(1); + } +} + +//********************************************************************************************************************** + + diff --git a/chopseqscommand.h b/chopseqscommand.h new file mode 100644 index 0000000..5d8d6c4 --- /dev/null +++ b/chopseqscommand.h @@ -0,0 +1,33 @@ +#ifndef CHOPSEQSCOMMAND_H +#define CHOPSEQSCOMMAND_H + +/* + * chopseqscommand.h + * Mothur + * + * Created by westcott on 5/10/10. + * Copyright 2010 Schloss Lab. All rights reserved. + * + */ + + +#include "command.hpp" + +class ChopSeqsCommand : public Command { + + public: + + ChopSeqsCommand(string); + ~ChopSeqsCommand(){}; + int execute(); + void help(); + + private: + string fastafile, outputDir; + bool abort; + int end; +}; + +#endif + + diff --git a/commandfactory.cpp b/commandfactory.cpp index e2d114a..2ac61e3 100644 --- a/commandfactory.cpp +++ b/commandfactory.cpp @@ -74,6 +74,7 @@ #include "setlogfilecommand.h" #include "phylodiversitycommand.h" #include "makegroupcommand.h" +#include "chopseqscommand.h" /*******************************************************/ @@ -155,6 +156,7 @@ CommandFactory::CommandFactory(){ commands["set.logfile"] = "set.logfile"; commands["phylo.diversity"] = "phylo.diversity"; commands["make.group"] = "make.group"; + commands["chop.seqs"] = "chop.seqs"; commands["classify.seqs"] = "MPIEnabled"; commands["dist.seqs"] = "MPIEnabled"; commands["filter.seqs"] = "MPIEnabled"; @@ -273,6 +275,7 @@ Command* CommandFactory::getCommand(string commandName, string optionString){ else if(commandName == "parse.sff") { command = new ParseSFFCommand(optionString); } else if(commandName == "phylo.diversity") { command = new PhyloDiversityCommand(optionString); } else if(commandName == "make.group") { command = new MakeGroupCommand(optionString); } + else if(commandName == "chop.seqs") { command = new ChopSeqsCommand(optionString); } else { command = new NoCommand(optionString); } return command; diff --git a/listseqscommand.cpp b/listseqscommand.cpp index d23949f..23c71ec 100644 --- a/listseqscommand.cpp +++ b/listseqscommand.cpp @@ -111,6 +111,7 @@ ListSeqsCommand::ListSeqsCommand(string option) { int okay = 1; if (outputDir != "") { okay++; } + if (inputDir != "") { okay++; } if (parameters.size() > okay) { m->mothurOut("You may only enter one file."); m->mothurOutEndLine(); abort = true; } } diff --git a/makefile b/makefile index ea9f9ac..ecdbcf0 100644 --- a/makefile +++ b/makefile @@ -146,7 +146,8 @@ mothur : \ ./globaldata.o\ ./groupmap.o\ ./helpcommand.o\ - ./makegroupcommand.o\ + ./makegroupcommand.o\ + ./chopseqscommand.o\ ./inputdata.o\ ./jackknife.o\ ./kmer.o\ @@ -348,7 +349,8 @@ mothur : \ ./globaldata.o\ ./groupmap.o\ ./helpcommand.o\ - ./makegroupcommand.o\ + ./makegroupcommand.o\ + ./chopseqscommand.o\ ./inputdata.o\ ./jackknife.o\ ./kmer.o\ @@ -553,7 +555,8 @@ clean : ./globaldata.o\ ./groupmap.o\ ./helpcommand.o\ - ./makegroupcommand.o\ + ./makegroupcommand.o\ + ./chopseqscommand.o\ ./inputdata.o\ ./jackknife.o\ ./kmer.o\ @@ -1655,6 +1658,9 @@ install : mothur # Item # 201 -- makegroupcommand -- ./makegroupcommand.o : makegroupcommand.cpp $(CC) $(CC_OPTIONS) makegroupcommand.cpp -c $(INCLUDE) -o ./makegroupcommand.o - + +# Item # 202 -- chopseqscommand -- +./chopseqscommand.o : chopseqscommand.cpp + $(CC) $(CC_OPTIONS) chopseqscommand.cpp -c $(INCLUDE) -o ./chopseqscommand.o ##### END RUN #### diff --git a/makegroupcommand.cpp b/makegroupcommand.cpp index f567fbd..57028c9 100644 --- a/makegroupcommand.cpp +++ b/makegroupcommand.cpp @@ -127,12 +127,16 @@ int MakeGroupCommand::execute(){ for (int i = 0; i < fastaFileNames.size(); i++) { + if (m->control_pressed) { out.close(); remove(filename.c_str()); return 0; } + ifstream in; openInputFile(fastaFileNames[i], in); while (!in.eof()) { - Sequence seq(in); gobble(in); + Sequence seq(in, "no align"); gobble(in); + + if (m->control_pressed) { in.close(); out.close(); remove(filename.c_str()); return 0; } if (seq.getName() != "") { out << seq.getName() << '\t' << groupsNames[i] << endl; } } -- 2.39.2