]> git.donarmstrong.com Git - mothur.git/blobdiff - filterseqscommand.cpp
added set.current and get.current commands and modified existing commands to update...
[mothur.git] / filterseqscommand.cpp
index 798ae64bfa88620fb6403f68842c50cee381ca25..cb0b131f58e039f5eea76b784334715e1e7481e3 100644 (file)
 #include "filterseqscommand.h"
 #include "sequence.hpp"
 
+//**********************************************************************************************************************
+vector<string> FilterSeqsCommand::getValidParameters(){        
+       try {
+               string Array[] =  {"fasta", "trump", "soft", "hard", "vertical", "outputdir","inputdir", "processors"};
+               vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
+               return myArray;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "FilterSeqsCommand", "getValidParameters");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+FilterSeqsCommand::FilterSeqsCommand(){        
+       try {
+               abort = true; calledHelp = true; 
+               vector<string> tempOutNames;
+               outputTypes["fasta"] = tempOutNames;
+               outputTypes["filter"] = tempOutNames;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "FilterSeqsCommand", "FilterSeqsCommand");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+vector<string> FilterSeqsCommand::getRequiredParameters(){     
+       try {
+               string Array[] =  {"fasta"};
+               vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
+               return myArray;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "FilterSeqsCommand", "getRequiredParameters");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+vector<string> FilterSeqsCommand::getRequiredFiles(){  
+       try {
+               vector<string> myArray;
+               return myArray;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "FilterSeqsCommand", "getRequiredFiles");
+               exit(1);
+       }
+}
 /**************************************************************************************/
-
 FilterSeqsCommand::FilterSeqsCommand(string option)  {
        try {
-               abort = false;
+               abort = false; calledHelp = false;   
                filterFileName = "";
                
                //allow user to run help
-               if(option == "help") { help(); abort = true; }
+               if(option == "help") { help(); abort = true; calledHelp = true; }
                
                else {
                        //valid paramters for this command
@@ -36,6 +83,11 @@ FilterSeqsCommand::FilterSeqsCommand(string option)  {
                                if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
                        }
                        
+                       //initialize outputTypes
+                       vector<string> tempOutNames;
+                       outputTypes["fasta"] = tempOutNames;
+                       outputTypes["filter"] = tempOutNames;
+               
                        //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 = "";          }
@@ -80,10 +132,25 @@ FilterSeqsCommand::FilterSeqsCommand(string option)  {
                                                if (m->getDefaultPath() != "") { //default path is set
                                                        string tryPath = m->getDefaultPath() + m->getSimpleName(fastafileNames[i]);
                                                        m->mothurOut("Unable to open " + fastafileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
-                                                       ableToOpen = m->openInputFile(tryPath, in, "noerror");
+                                                       ifstream in2;
+                                                       ableToOpen = m->openInputFile(tryPath, in2, "noerror");
+                                                       in2.close();
+                                                       fastafileNames[i] = tryPath;
+                                               }
+                                       }
+                                       
+                                       //if you can't open it, try default location
+                                       if (ableToOpen == 1) {
+                                               if (m->getOutputDir() != "") { //default path is set
+                                                       string tryPath = m->getOutputDir() + m->getSimpleName(fastafileNames[i]);
+                                                       m->mothurOut("Unable to open " + fastafileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
+                                                       ifstream in2;
+                                                       ableToOpen = m->openInputFile(tryPath, in2, "noerror");
+                                                       in2.close();
                                                        fastafileNames[i] = tryPath;
                                                }
                                        }
+                                       
                                        in.close();
                                        
                                        if (ableToOpen == 1) { 
@@ -147,13 +214,14 @@ void FilterSeqsCommand::help(){
        try {
                                
                m->mothurOut("The filter.seqs command reads a file containing sequences and creates a .filter and .filter.fasta file.\n");
-               m->mothurOut("The filter.seqs command parameters are fasta, trump, soft, hard and vertical. \n");
+               m->mothurOut("The filter.seqs command parameters are fasta, trump, soft, hard, processors and vertical. \n");
                m->mothurOut("The fasta parameter is required. You may enter several fasta files to build the filter from and filter, by separating their names with -'s.\n");
                m->mothurOut("For example: fasta=abrecovery.fasta-amazon.fasta \n");
                m->mothurOut("The trump parameter .... The default is ...\n");
                m->mothurOut("The soft parameter .... The default is ....\n");
                m->mothurOut("The hard parameter allows you to enter a file containing the filter you want to use.\n");
                m->mothurOut("The vertical parameter removes columns where all sequences contain a gap character. The default is T.\n");
+               m->mothurOut("The processors parameter allows you to specify the number of processors to use. The default is 1.\n");
                m->mothurOut("The filter.seqs command should be in the following format: \n");
                m->mothurOut("filter.seqs(fasta=yourFastaFile, trump=yourTrump) \n");
                m->mothurOut("Example filter.seqs(fasta=abrecovery.fasta, trump=.).\n");
@@ -171,7 +239,7 @@ void FilterSeqsCommand::help(){
 int FilterSeqsCommand::execute() {     
        try {
        
-               if (abort == true) { return 0; }
+               if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
                
                ifstream inFASTA;
                m->openInputFile(fastafileNames[0], inFASTA);
@@ -187,7 +255,7 @@ int FilterSeqsCommand::execute() {
                
                m->mothurOutEndLine();  m->mothurOutEndLine();
                
-               if (m->control_pressed) { return 0; }
+               if (m->control_pressed) { outputTypes.clear(); return 0; }
                
                #ifdef USE_MPI
                        int pid;
@@ -206,7 +274,7 @@ int FilterSeqsCommand::execute() {
                m->openOutputFile(filterFile, outFilter);
                outFilter << filter << endl;
                outFilter.close();
-               outputNames.push_back(filterFile);
+               outputNames.push_back(filterFile); outputTypes["filter"].push_back(filterFile);
                
                #ifdef USE_MPI
                        }
@@ -225,7 +293,7 @@ int FilterSeqsCommand::execute() {
                        if(filter[i] == '1'){   filteredLength++;       }
                }
                
-               if (m->control_pressed) {  for(int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }  return 0; }
+               if (m->control_pressed) {  outputTypes.clear(); for(int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }  return 0; }
 
                
                m->mothurOutEndLine();
@@ -234,6 +302,12 @@ int FilterSeqsCommand::execute() {
                m->mothurOut("Length of the original alignment: " + toString(alignmentLength)); m->mothurOutEndLine();
                m->mothurOut("Number of sequences used to construct filter: " + toString(numSeqs)); m->mothurOutEndLine();
                
+               //set fasta file as new current fastafile
+               string current = "";
+               itTypes = outputTypes.find("fasta");
+               if (itTypes != outputTypes.end()) {
+                       if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
+               }
                
                m->mothurOutEndLine();
                m->mothurOut("Output File Names: "); m->mothurOutEndLine();
@@ -260,7 +334,7 @@ int FilterSeqsCommand::filterSequences() {
                                
                                string filteredFasta = outputDir + m->getRootName(m->getSimpleName(fastafileNames[s])) + "filter.fasta";
 #ifdef USE_MPI 
-                               int pid, start, end, numSeqsPerProcessor, num; 
+                               int pid, numSeqsPerProcessor, num; 
                                int tag = 2001;
                                vector<unsigned long int>MPIPos;
                                                
@@ -269,7 +343,6 @@ int FilterSeqsCommand::filterSequences() {
                                MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
                                
                                MPI_File outMPI;
-                               MPI_File tempMPI;
                                MPI_File inMPI;
                                int outMode=MPI_MODE_CREATE|MPI_MODE_WRONLY; 
                                int inMode=MPI_MODE_RDONLY; 
@@ -309,8 +382,8 @@ int FilterSeqsCommand::filterSequences() {
                                        
                                        //wait on chidren
                                        for(int i = 1; i < processors; i++) { 
-                                               char buf[4];
-                                               MPI_Recv(buf, 4, MPI_CHAR, i, tag, MPI_COMM_WORLD, &status); 
+                                               char buf[5];
+                                               MPI_Recv(buf, 5, MPI_CHAR, i, tag, MPI_COMM_WORLD, &status); 
                                        }
                                        
                                }else { //you are a child process
@@ -330,11 +403,11 @@ int FilterSeqsCommand::filterSequences() {
                                        
                                        if (m->control_pressed) {  MPI_File_close(&inMPI);  MPI_File_close(&outMPI);  return 0;  }
                                        
-                                       char buf[4];
+                                       char buf[5];
                                        strcpy(buf, "done"); 
                                        
                                        //tell parent you are done.
-                                       MPI_Send(buf, 4, MPI_CHAR, 0, tag, MPI_COMM_WORLD);
+                                       MPI_Send(buf, 5, MPI_CHAR, 0, tag, MPI_COMM_WORLD);
                                }
                                
                                MPI_File_close(&outMPI);
@@ -372,7 +445,7 @@ int FilterSeqsCommand::filterSequences() {
                                if (m->control_pressed) {  return 1; }
                #endif
 #endif
-                       outputNames.push_back(filteredFasta);
+                       outputNames.push_back(filteredFasta); outputTypes["fasta"].push_back(filteredFasta);
                }
 
                return 0;
@@ -541,7 +614,11 @@ int FilterSeqsCommand::createProcessesRunFilter(string F, string filename) {
                                out.close();
                                
                                exit(0);
-                       }else { m->mothurOut("unable to spawn the necessary processes."); m->mothurOutEndLine(); exit(0); }
+                       }else { 
+                               m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine(); 
+                               for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
+                               exit(0);
+                       }
                }
                
                //force parent to wait until all the processes are done
@@ -905,7 +982,11 @@ int FilterSeqsCommand::createProcessesCreateFilter(Filters& F, string filename)
                                out.close();
                                
                                exit(0);
-                       }else { m->mothurOut("unable to spawn the necessary processes."); m->mothurOutEndLine(); exit(0); }
+                       }else { 
+                               m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine(); 
+                               for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
+                               exit(0);
+                       }
                }
                
                //parent do your part