]> git.donarmstrong.com Git - mothur.git/commitdiff
added chop.seqs command
authorwestcott <westcott>
Mon, 10 May 2010 18:28:14 +0000 (18:28 +0000)
committerwestcott <westcott>
Mon, 10 May 2010 18:28:14 +0000 (18:28 +0000)
Mothur.xcodeproj/project.pbxproj
chopseqscommand.cpp [new file with mode: 0644]
chopseqscommand.h [new file with mode: 0644]
commandfactory.cpp
listseqscommand.cpp
makefile
makegroupcommand.cpp

index 29de1db916d04317adc1f744cc97f4e166697361..f9fd39e7ef1d4ba3baedb85737dbc1c8f6ce520c 100644 (file)
@@ -13,6 +13,8 @@
                A72B3A63118B37FD004B9F8D /* phylodiversitycommand.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = phylodiversitycommand.cpp; sourceTree = "<group>"; };
                A72B3A7B118B4D1B004B9F8D /* phylodiversity.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = phylodiversity.h; sourceTree = "<group>"; };
                A72B3A7C118B4D1B004B9F8D /* phylodiversity.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = phylodiversity.cpp; sourceTree = "<group>"; };
+               A73953DA11987ED100B0B160 /* chopseqscommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = chopseqscommand.h; sourceTree = "<group>"; };
+               A73953DB11987ED100B0B160 /* chopseqscommand.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = chopseqscommand.cpp; sourceTree = "<group>"; };
                A747E79B1163442A00FB9042 /* chimeracheckcommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = chimeracheckcommand.h; sourceTree = "<group>"; };
                A747E79C1163442A00FB9042 /* chimeracheckcommand.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = chimeracheckcommand.cpp; sourceTree = "<group>"; };
                A747E81C116365E000FB9042 /* chimeraslayercommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = chimeraslayercommand.h; sourceTree = "<group>"; };
                                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 (file)
index 0000000..adf0910
--- /dev/null
@@ -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<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
+                       
+                       OptionParser parser(option);
+                       map<string,string> parameters = parser.getParameters();
+                       
+                       ValidParameters validParameter;
+                       map<string,string>::iterator it;
+                       
+                       //check to make sure all parameters are valid for command
+                       for (map<string,string>::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 (file)
index 0000000..5d8d6c4
--- /dev/null
@@ -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
+
+
index e2d114a3ba25e41474af369a8d41752be7616ccd..2ac61e33e3252af5581b78b21168a5e2fbe760fb 100644 (file)
@@ -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;
index d23949f497e65b262e3067d74d566d4665124412..23c71ecdcf2ac06f522f034cbea813d4a1f2cd22 100644 (file)
@@ -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;  }
                }
index ea9f9ac8a62e05e350b93cc5ce7a26f628f6a895..ecdbcf039c52114204e40d2e11efae12518fe923 100644 (file)
--- a/makefile
+++ b/makefile
@@ -146,7 +146,8 @@ mothur : \
                ./globaldata.o\\r
                ./groupmap.o\\r
                ./helpcommand.o\
-               ./makegroupcommand.o\\r
+               ./makegroupcommand.o\
+               ./chopseqscommand.o\\r
                ./inputdata.o\\r
                ./jackknife.o\\r
                ./kmer.o\\r
@@ -348,7 +349,8 @@ mothur : \
                ./globaldata.o\\r
                ./groupmap.o\\r
                ./helpcommand.o\
-               ./makegroupcommand.o\\r
+               ./makegroupcommand.o\
+               ./chopseqscommand.o\\r
                ./inputdata.o\\r
                ./jackknife.o\\r
                ./kmer.o\\r
@@ -553,7 +555,8 @@ clean :
                ./globaldata.o\\r
                ./groupmap.o\\r
                ./helpcommand.o\
-               ./makegroupcommand.o\\r
+               ./makegroupcommand.o\
+               ./chopseqscommand.o\\r
                ./inputdata.o\\r
                ./jackknife.o\\r
                ./kmer.o\\r
@@ -1655,6 +1658,9 @@ install : mothur
 # Item # 201 -- makegroupcommand --\r
 ./makegroupcommand.o : makegroupcommand.cpp\r
        $(CC) $(CC_OPTIONS) makegroupcommand.cpp -c $(INCLUDE) -o ./makegroupcommand.o\r
-\r
+
+# Item # 202 -- chopseqscommand --\r
+./chopseqscommand.o : chopseqscommand.cpp\r
+       $(CC) $(CC_OPTIONS) chopseqscommand.cpp -c $(INCLUDE) -o ./chopseqscommand.o\r
 \r
 ##### END RUN ####\r
index f567fbda600cecf76338c9081e10d51a872ecbc3..57028c957397f8ead2e38cd326020391a61ef7a2 100644 (file)
@@ -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;         }
                        }