]> git.donarmstrong.com Git - mothur.git/blobdiff - filterseqscommand.cpp
added sequence name to error string in fastq.info. Changed np_shannon to npshannon.
[mothur.git] / filterseqscommand.cpp
index a677eef9729ef5f1035db03f0a5daff9f7280e36..82c73f3d439c1d3cc154c8dac67398075134a1e8 100644 (file)
@@ -78,6 +78,7 @@ FilterSeqsCommand::FilterSeqsCommand(string option)  {
                
                //allow user to run help
                if(option == "help") { help(); abort = true; calledHelp = true; }
+               else if(option == "citation") { citation(); abort = true; calledHelp = true;}
                
                else {
                        vector<string> myArray = setParameters();
@@ -132,51 +133,67 @@ FilterSeqsCommand::FilterSeqsCommand(string option)  {
                                
                                //go through files and make sure they are good, if not, then disregard them
                                for (int i = 0; i < fastafileNames.size(); i++) {
-                                       if (inputDir != "") {
-                                               string path = m->hasPath(fastafileNames[i]);
-                                               //if the user has not given a path then, add inputdir. else leave path alone.
-                                               if (path == "") {       fastafileNames[i] = inputDir + fastafileNames[i];               }
-                                       }
-
-                                       ifstream in;
-                                       int ableToOpen = m->openInputFile(fastafileNames[i], in, "noerror");
-                               
-                                       //if you can't open it, try default location
-                                       if (ableToOpen == 1) {
-                                               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();
-                                                       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;
+                                       bool ignore = false;
+                                       if (fastafileNames[i] == "current") { 
+                                               fastafileNames[i] = m->getFastaFile(); 
+                                               if (fastafileNames[i] != "") {  m->mothurOut("Using " + fastafileNames[i] + " as input file for the fasta parameter where you had given current."); m->mothurOutEndLine(); }
+                                               else {  
+                                                       m->mothurOut("You have no current fastafile, ignoring current."); m->mothurOutEndLine(); ignore=true; 
+                                                       //erase from file list
+                                                       fastafileNames.erase(fastafileNames.begin()+i);
+                                                       i--;
                                                }
                                        }
                                        
-                                       in.close();
+                                       if (!ignore) {
+                                               if (inputDir != "") {
+                                                       string path = m->hasPath(fastafileNames[i]);
+                                                       //if the user has not given a path then, add inputdir. else leave path alone.
+                                                       if (path == "") {       fastafileNames[i] = inputDir + fastafileNames[i];               }
+                                               }
+
+                                               ifstream in;
+                                               int ableToOpen = m->openInputFile(fastafileNames[i], in, "noerror");
                                        
-                                       if (ableToOpen == 1) { 
-                                               m->mothurOut("Unable to open " + fastafileNames[i] + ". It will be disregarded."); m->mothurOutEndLine();
-                                               //erase from file list
-                                               fastafileNames.erase(fastafileNames.begin()+i);
-                                               i--;
-                                       }else{  
-                                               string simpleName = m->getSimpleName(fastafileNames[i]);
-                                               filterFileName += simpleName.substr(0, simpleName.find_first_of('.'));
+                                               //if you can't open it, try default location
+                                               if (ableToOpen == 1) {
+                                                       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();
+                                                               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) { 
+                                                       m->mothurOut("Unable to open " + fastafileNames[i] + ". It will be disregarded."); m->mothurOutEndLine();
+                                                       //erase from file list
+                                                       fastafileNames.erase(fastafileNames.begin()+i);
+                                                       i--;
+                                               }else{  
+                                                       string simpleName = m->getSimpleName(fastafileNames[i]);
+                                                       filterFileName += simpleName.substr(0, simpleName.find_first_of('.'));
+                                                       m->setFastaFile(fastafileNames[i]);
+                                               }
+                                               in.close();
                                        }
-                                       in.close();
                                }
                                
                                //make sure there is at least one valid file left
@@ -205,7 +222,7 @@ FilterSeqsCommand::FilterSeqsCommand(string option)  {
                        
                        temp = validParameter.validFile(parameters, "processors", false);       if (temp == "not found"){       temp = m->getProcessors();      }
                        m->setProcessors(temp);
-                       convert(temp, processors); 
+                       m->mothurConvert(temp, processors); 
                        
                        vertical = validParameter.validFile(parameters, "vertical", false);             
                        if (vertical == "not found") { 
@@ -281,7 +298,7 @@ int FilterSeqsCommand::execute() {
                        if(filter[i] == '1'){   filteredLength++;       }
                }
                
-               if (m->control_pressed) {  outputTypes.clear(); 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++) { m->mothurRemove(outputNames[i]); }  return 0; }
 
                
                m->mothurOutEndLine();
@@ -324,7 +341,7 @@ int FilterSeqsCommand::filterSequences() {
 #ifdef USE_MPI 
                                int pid, numSeqsPerProcessor, num; 
                                int tag = 2001;
-                               vector<unsigned long int>MPIPos;
+                               vector<unsigned long long>MPIPos;
                                                
                                MPI_Status status; 
                                MPI_Comm_size(MPI_COMM_WORLD, &processors); //set processors to the number of mpi processes running
@@ -403,12 +420,14 @@ int FilterSeqsCommand::filterSequences() {
                                MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
                                
 #else
-                       vector<unsigned long int> positions = m->divideFile(fastafileNames[s], processors);
-                               
+                       
+               #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
+                       vector<unsigned long long> positions = m->divideFile(fastafileNames[s], processors);
+                       
                        for (int i = 0; i < (positions.size()-1); i++) {
                                lines.push_back(new linePair(positions[i], positions[(i+1)]));
                        }       
-               #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
+                       
                                if(processors == 1){
                                        int numFastaSeqs = driverRunFilter(filter, filteredFasta, fastafileNames[s], lines[0]);
                                        numSeqs += numFastaSeqs;
@@ -421,12 +440,13 @@ int FilterSeqsCommand::filterSequences() {
                                        //append fasta files
                                        for(int i=1;i<processors;i++){
                                                m->appendFiles((fastafileNames[s] + toString(processIDS[i]) + ".temp"), filteredFasta);
-                                               remove((fastafileNames[s] + toString(processIDS[i]) + ".temp").c_str());
+                                               m->mothurRemove((fastafileNames[s] + toString(processIDS[i]) + ".temp"));
                                        }
                                }
                                
                                if (m->control_pressed) {  return 1; }
                #else
+                               lines.push_back(new linePair(0, 1000));
                                int numFastaSeqs = driverRunFilter(filter, filteredFasta, fastafileNames[s], lines[0]);
                                numSeqs += numFastaSeqs;
 
@@ -445,7 +465,7 @@ int FilterSeqsCommand::filterSequences() {
 }
 #ifdef USE_MPI
 /**************************************************************************************/
-int FilterSeqsCommand::driverMPIRun(int start, int num, MPI_File& inMPI, MPI_File& outMPI, vector<unsigned long int>& MPIPos) {        
+int FilterSeqsCommand::driverMPIRun(int start, int num, MPI_File& inMPI, MPI_File& outMPI, vector<unsigned long long>& MPIPos) {       
        try {
                string outputString = "";
                int count = 0;
@@ -551,7 +571,7 @@ int FilterSeqsCommand::driverRunFilter(string F, string outputFilename, string i
                        }
                        
                        #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
-                               unsigned long int pos = in.tellg();
+                               unsigned long long pos = in.tellg();
                                if ((pos == -1) || (pos >= filePos->end)) { break; }
                        #else
                                if (in.eof()) { break; }
@@ -620,7 +640,7 @@ int FilterSeqsCommand::createProcessesRunFilter(string F, string filename) {
                        string tempFile =  filename + toString(processIDS[i]) + ".num.temp";
                        m->openInputFile(tempFile, in);
                        if (!in.eof()) { int tempNum = 0; in >> tempNum; num += tempNum; }
-                       in.close(); remove(tempFile.c_str());
+                       in.close(); m->mothurRemove(tempFile);
                }
 
                
@@ -659,7 +679,7 @@ string FilterSeqsCommand::createFilter() {
 #ifdef USE_MPI 
                                int pid, numSeqsPerProcessor, num; 
                                int tag = 2001;
-                               vector<unsigned long int> MPIPos;
+                               vector<unsigned long long> MPIPos;
                                
                                MPI_Status status; 
                                MPI_File inMPI; 
@@ -719,12 +739,14 @@ string FilterSeqsCommand::createFilter() {
                                MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
                                
 #else
-               vector<unsigned long int> positions = m->divideFile(fastafileNames[s], processors);
                                
-               for (int i = 0; i < (positions.size()-1); i++) {
-                       lines.push_back(new linePair(positions[i], positions[(i+1)]));
-               }       
+               
                #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
+                               vector<unsigned long long> positions = m->divideFile(fastafileNames[s], processors);
+                               for (int i = 0; i < (positions.size()-1); i++) {
+                                       lines.push_back(new linePair(positions[i], positions[(i+1)]));
+                               }       
+                               
                                if(processors == 1){
                                        int numFastaSeqs = driverCreateFilter(F, fastafileNames[s], lines[0]);
                                        numSeqs += numFastaSeqs;
@@ -735,6 +757,7 @@ string FilterSeqsCommand::createFilter() {
                                
                                if (m->control_pressed) {  return filterString; }
                #else
+                               lines.push_back(new linePair(0, 1000));
                                int numFastaSeqs = driverCreateFilter(F, fastafileNames[s], lines[0]);
                                numSeqs += numFastaSeqs;
                                if (m->control_pressed) {  return filterString; }
@@ -860,7 +883,7 @@ int FilterSeqsCommand::driverCreateFilter(Filters& F, string filename, linePair*
                        }
                        
                        #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
-                               unsigned long int pos = in.tellg();
+                               unsigned long long pos = in.tellg();
                                if ((pos == -1) || (pos >= filePos->end)) { break; }
                        #else
                                if (in.eof()) { break; }
@@ -882,7 +905,7 @@ int FilterSeqsCommand::driverCreateFilter(Filters& F, string filename, linePair*
 }
 #ifdef USE_MPI
 /**************************************************************************************/
-int FilterSeqsCommand::MPICreateFilter(int start, int num, Filters& F, MPI_File& inMPI, vector<unsigned long int>& MPIPos) {   
+int FilterSeqsCommand::MPICreateFilter(int start, int num, Filters& F, MPI_File& inMPI, vector<unsigned long long>& MPIPos) {  
        try {
                
                MPI_Status status; 
@@ -1007,7 +1030,7 @@ int FilterSeqsCommand::createProcessesCreateFilter(Filters& F, string filename)
                        for (int k = 0; k < alignmentLength; k++) {             in >> temp; F.gap[k] += temp; } m->gobble(in);
                                
                        in.close();
-                       remove(tempFilename.c_str());
+                       m->mothurRemove(tempFilename);
                }
                
                return num;