]> git.donarmstrong.com Git - mothur.git/blobdiff - aligncommand.cpp
1.12.0
[mothur.git] / aligncommand.cpp
index a4b3a79012aa5ac0d53c6ff78f541864984b42ec..ee27776c11912bd9fe346f300ffc867f5164223f 100644 (file)
@@ -107,9 +107,18 @@ AlignCommand::AlignCommand(string option)  {
                                                if (pid == 0) {
                                        #endif
 
-                                       ableToOpen = openInputFile(candidateFileNames[i], in);
-                                       in.close();
-                                       
+                                       ableToOpen = openInputFile(candidateFileNames[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() + getSimpleName(candidateFileNames[i]);
+                                                       m->mothurOut("Unable to open " + candidateFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
+                                                       ableToOpen = openInputFile(tryPath, in, "noerror");
+                                                       candidateFileNames[i] = tryPath;
+                                               }
+                                       }
+                                       in.close();                                     
                                        #ifdef USE_MPI  
                                                        for (int j = 1; j < processors; j++) {
                                                                MPI_Send(&ableToOpen, 1, MPI_INT, j, 2001, MPI_COMM_WORLD); 
@@ -122,7 +131,7 @@ AlignCommand::AlignCommand(string option)  {
                                        #endif
 
                                        if (ableToOpen == 1) { 
-                                               m->mothurOut(candidateFileNames[i] + " will be disregarded."); m->mothurOutEndLine(); 
+                                               m->mothurOut("Unable to open " + candidateFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine(); 
                                                //erase from file list
                                                candidateFileNames.erase(candidateFileNames.begin()+i);
                                                i--;
@@ -266,19 +275,19 @@ int AlignCommand::execute(){
                                
                                int outMode=MPI_MODE_CREATE|MPI_MODE_WRONLY; 
                                int inMode=MPI_MODE_RDONLY; 
-                                                               
-                               char outAlignFilename[alignFileName.length()];
+                               
+                               char outAlignFilename[1024];
                                strcpy(outAlignFilename, alignFileName.c_str());
                                
-                               char outReportFilename[reportFileName.length()];
+                               char outReportFilename[1024];
                                strcpy(outReportFilename, reportFileName.c_str());
                                
-                               char outAccnosFilename[accnosFileName.length()];
+                               char outAccnosFilename[1024];
                                strcpy(outAccnosFilename, accnosFileName.c_str());
                                
-                               char inFileName[candidateFileNames[s].length()];
+                               char inFileName[1024];
                                strcpy(inFileName, candidateFileNames[s].c_str());
-
+                               
                                MPI_File_open(MPI_COMM_WORLD, inFileName, inMode, MPI_INFO_NULL, &inMPI);  //comm, filename, mode, info, filepointer
                                MPI_File_open(MPI_COMM_WORLD, outAlignFilename, outMode, MPI_INFO_NULL, &outMPIAlign);
                                MPI_File_open(MPI_COMM_WORLD, outReportFilename, outMode, MPI_INFO_NULL, &outMPIReport);
@@ -291,13 +300,16 @@ int AlignCommand::execute(){
                                        MPIPos = setFilePosFasta(candidateFileNames[s], numFastaSeqs); //fills MPIPos, returns numSeqs
                                        
                                        //send file positions to all processes
-                                       MPI_Bcast(&numFastaSeqs, 1, MPI_INT, 0, MPI_COMM_WORLD);  //send numSeqs
-                                       MPI_Bcast(&MPIPos[0], (numFastaSeqs+1), MPI_LONG, 0, MPI_COMM_WORLD); //send file pos   
+                                       for(int i = 1; i < processors; i++) { 
+                                               MPI_Send(&numFastaSeqs, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
+                                               MPI_Send(&MPIPos[0], (numFastaSeqs+1), MPI_LONG, i, tag, MPI_COMM_WORLD);
+                                       }
                                        
                                        //figure out how many sequences you have to align
                                        numSeqsPerProcessor = numFastaSeqs / processors;
-                                       if(pid == (processors - 1)){    numSeqsPerProcessor = numFastaSeqs - pid * numSeqsPerProcessor;         }
                                        int startIndex =  pid * numSeqsPerProcessor;
+                                       if(pid == (processors - 1)){    numSeqsPerProcessor = numFastaSeqs - pid * numSeqsPerProcessor;         }
+                                       
                                
                                        //align your part
                                        driverMPI(startIndex, numSeqsPerProcessor, inMPI, outMPIAlign, outMPIReport, outMPIAccnos, MPIPos);
@@ -310,14 +322,16 @@ int AlignCommand::execute(){
                                                if (tempResult != 0) { MPIWroteAccnos = true; }
                                        }
                                }else{ //you are a child process
-                                       MPI_Bcast(&numFastaSeqs, 1, MPI_INT, 0, MPI_COMM_WORLD); //get numSeqs
+                                       MPI_Recv(&numFastaSeqs, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
                                        MPIPos.resize(numFastaSeqs+1);
-                                       MPI_Bcast(&MPIPos[0], (numFastaSeqs+1), MPI_LONG, 0, MPI_COMM_WORLD); //get file positions
+                                       MPI_Recv(&MPIPos[0], (numFastaSeqs+1), MPI_LONG, 0, tag, MPI_COMM_WORLD, &status);
+
                                        
                                        //figure out how many sequences you have to align
                                        numSeqsPerProcessor = numFastaSeqs / processors;
-                                       if(pid == (processors - 1)){    numSeqsPerProcessor = numFastaSeqs - pid * numSeqsPerProcessor;         }
                                        int startIndex =  pid * numSeqsPerProcessor;
+                                       if(pid == (processors - 1)){    numSeqsPerProcessor = numFastaSeqs - pid * numSeqsPerProcessor;         }
+                                       
                                        
                                        //align your part
                                        driverMPI(startIndex, numSeqsPerProcessor, inMPI, outMPIAlign, outMPIReport, outMPIAccnos, MPIPos);
@@ -356,7 +370,7 @@ int AlignCommand::execute(){
                        if(processors == 1){
                                ifstream inFASTA;
                                openInputFile(candidateFileNames[s], inFASTA);
-                               numFastaSeqs=count(istreambuf_iterator<char>(inFASTA),istreambuf_iterator<char>(), '>');
+                               getNumSeqs(inFASTA, numFastaSeqs);
                                inFASTA.close();
                                
                                lines.push_back(new linePair(0, numFastaSeqs));
@@ -381,7 +395,7 @@ int AlignCommand::execute(){
                                }
                        }
                        else{
-                               vector<int> positions;
+                               vector<unsigned long int> positions;
                                processIDS.resize(0);
                                
                                ifstream inFASTA;
@@ -391,7 +405,7 @@ int AlignCommand::execute(){
                                while(!inFASTA.eof()){
                                        input = getline(inFASTA);
                                        if (input.length() != 0) {
-                                               if(input[0] == '>'){    long int pos = inFASTA.tellg(); positions.push_back(pos - input.length() - 1);  }
+                                               if(input[0] == '>'){    unsigned long int pos = inFASTA.tellg(); positions.push_back(pos - input.length() - 1); }
                                        }
                                }
                                inFASTA.close();
@@ -401,7 +415,7 @@ int AlignCommand::execute(){
                                int numSeqsPerProcessor = numFastaSeqs / processors;
                                
                                for (int i = 0; i < processors; i++) {
-                                       long int startPos = positions[ i * numSeqsPerProcessor ];
+                                       unsigned long int startPos = positions[ i * numSeqsPerProcessor ];
                                        if(i == processors - 1){
                                                numSeqsPerProcessor = numFastaSeqs - i * numSeqsPerProcessor;
                                        }
@@ -455,7 +469,7 @@ int AlignCommand::execute(){
        #else
                        ifstream inFASTA;
                        openInputFile(candidateFileNames[s], inFASTA);
-                       numFastaSeqs=count(istreambuf_iterator<char>(inFASTA),istreambuf_iterator<char>(), '>');
+                       getNumSeqs(inFASTA, numFastaSeqs);
                        inFASTA.close();
                        
                        lines.push_back(new linePair(0, numFastaSeqs));
@@ -647,10 +661,13 @@ int AlignCommand::driverMPI(int start, int num, MPI_File& inMPI, MPI_File& align
                if (pid == 0) {
                        outputString = report.getHeaders();
                        int length = outputString.length();
-                       char buf[length];
-                       strcpy(buf, outputString.c_str()); 
+            
+                       char* buf = new char[length];
+                       memcpy(buf, outputString.c_str(), length);
                
                        MPI_File_write_shared(reportFile, buf, length, MPI_CHAR, &statusReport);
+
+            delete buf;
                }
                
                for(int i=0;i<num;i++){
@@ -659,10 +676,16 @@ int AlignCommand::driverMPI(int start, int num, MPI_File& inMPI, MPI_File& align
 
                        //read next sequence
                        int length = MPIPos[start+i+1] - MPIPos[start+i];
-                       char buf4[length];
+
+                       char* buf4 = new char[length];
+                       memcpy(buf4, outputString.c_str(), length);
+
                        MPI_File_read_at(inMPI, MPIPos[start+i], buf4, length, MPI_CHAR, &status);
                        
                        string tempBuf = buf4;
+
+                       delete buf4;
+
                        if (tempBuf.length() > length) { tempBuf = tempBuf.substr(0, length);  }
                        istringstream iss (tempBuf,istringstream::in);
        
@@ -727,10 +750,12 @@ int AlignCommand::driverMPI(int start, int num, MPI_File& inMPI, MPI_File& align
                                        
                                        //send results to parent
                                        int length = outputString.length();
-                                       char buf[length];
-                                       strcpy(buf, outputString.c_str()); 
+
+                                       char* buf = new char[length];
+                                       memcpy(buf, outputString.c_str(), length);
                                
                                        MPI_File_write_shared(accnosFile, buf, length, MPI_CHAR, &statusAccnos);
+                                       delete buf;
                                        MPIWroteAccnos = true;
                                }
                                
@@ -744,20 +769,23 @@ int AlignCommand::driverMPI(int start, int num, MPI_File& inMPI, MPI_File& align
                                
                                //send results to parent
                                int length = outputString.length();
-                               char buf2[length];
-                               strcpy(buf2, outputString.c_str()); 
+                               char* buf2 = new char[length];
+                               memcpy(buf2, outputString.c_str(), length);
                                
                                MPI_File_write_shared(alignFile, buf2, length, MPI_CHAR, &statusAlign);
                                
+                               delete buf2;
+
                                outputString = report.getReport();
                                
                                //send results to parent
                                length = outputString.length();
-                               char buf3[length];
-                               strcpy(buf3, outputString.c_str()); 
+                               char* buf3 = new char[length];
+                               memcpy(buf3, outputString.c_str(), length);
                                
                                MPI_File_write_shared(reportFile, buf3, length, MPI_CHAR, &statusReport);
-
+                               
+                               delete buf3;
                                delete nast;
                                if (needToDeleteCopy) {   delete copy;   }
                        }