]> git.donarmstrong.com Git - mothur.git/blobdiff - chimerapintailcommand.cpp
changed how we break up the files on parallelized commands to avoid scanning file.
[mothur.git] / chimerapintailcommand.cpp
index fd8724dc2f2908d3eebebb995e5ef22e697cfd5e..d9368cce2afee08e8d22c638a0df30b2ca6e2e4a 100644 (file)
@@ -337,57 +337,24 @@ int ChimeraPintailCommand::execute(){
                                MPI_File_close(&outMPIAccnos);
                                MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
                #else
-               
+                       vector<unsigned long int> positions = divideFile(fastaFileNames[s], processors);
+                               
+                       for (int i = 0; i < (positions.size()-1); i++) {
+                               lines.push_back(new linePair(positions[i], positions[(i+1)]));
+                       }       
+                       
                        //break up file
                        #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
                                if(processors == 1){
-                                       ifstream inFASTA;
-                                       openInputFile(fastaFileNames[s], inFASTA);
-                                       getNumSeqs(inFASTA, numSeqs);
-                                       inFASTA.close();
-                                       
-                                       lines.push_back(new linePair(0, numSeqs));
-                                       
-                                       driver(lines[0], outputFileName, fastaFileNames[s], accnosFileName);
+               
+                                       numSeqs = driver(lines[0], outputFileName, fastaFileNames[s], accnosFileName);
                                        
-                                       if (m->control_pressed) { 
-                                               remove(outputFileName.c_str()); 
-                                               remove(accnosFileName.c_str());
-                                               for (int j = 0; j < outputNames.size(); j++) {  remove(outputNames[j].c_str()); } 
-                                               for (int i = 0; i < lines.size(); i++) {  delete lines[i];  }  lines.clear();
-                                               delete chimera;
-                                               return 0;
-                                       }
+                                       if (m->control_pressed) { remove(outputFileName.c_str()); remove(accnosFileName.c_str()); for (int j = 0; j < outputNames.size(); j++) {        remove(outputNames[j].c_str()); } for (int i = 0; i < lines.size(); i++) {  delete lines[i];  }  lines.clear(); delete chimera; return 0; }
                                        
                                }else{
-                                       vector<unsigned long int> positions;
                                        processIDS.resize(0);
                                        
-                                       ifstream inFASTA;
-                                       openInputFile(fastaFileNames[s], inFASTA);
-                                       
-                                       string input;
-                                       while(!inFASTA.eof()){
-                                               input = getline(inFASTA);
-                                               if (input.length() != 0) {
-                                                       if(input[0] == '>'){    unsigned long int pos = inFASTA.tellg(); positions.push_back(pos - input.length() - 1); }
-                                               }
-                                       }
-                                       inFASTA.close();
-                                       
-                                       numSeqs = positions.size();
-                                       
-                                       int numSeqsPerProcessor = numSeqs / processors;
-                                       
-                                       for (int i = 0; i < processors; i++) {
-                                               unsigned long int startPos = positions[ i * numSeqsPerProcessor ];
-                                               if(i == processors - 1){
-                                                       numSeqsPerProcessor = numSeqs - i * numSeqsPerProcessor;
-                                               }
-                                               lines.push_back(new linePair(startPos, numSeqsPerProcessor));
-                                       }
-                                       
-                                       createProcesses(outputFileName, fastaFileNames[s], accnosFileName); 
+                                       numSeqs = createProcesses(outputFileName, fastaFileNames[s], accnosFileName); 
                                
                                        rename((outputFileName + toString(processIDS[0]) + ".temp").c_str(), outputFileName.c_str());
                                        rename((accnosFileName + toString(processIDS[0]) + ".temp").c_str(), accnosFileName.c_str());
@@ -415,22 +382,9 @@ int ChimeraPintailCommand::execute(){
                                }
 
                        #else
-                               ifstream inFASTA;
-                               openInputFile(fastaFileNames[s], inFASTA);
-                               getNumSeqs(inFASTA, numSeqs);
-                               inFASTA.close();
-                               lines.push_back(new linePair(0, numSeqs));
-                               
-                               driver(lines[0], outputFileName, fastaFileNames[s], accnosFileName);
+                               numSeqs = driver(lines[0], outputFileName, fastaFileNames[s], accnosFileName);
                                
-                               if (m->control_pressed) { 
-                                               remove(outputFileName.c_str()); 
-                                               remove(accnosFileName.c_str());
-                                               for (int j = 0; j < outputNames.size(); j++) {  remove(outputNames[j].c_str()); } 
-                                               for (int i = 0; i < lines.size(); i++) {  delete lines[i];  }  lines.clear();
-                                               delete chimera;
-                                               return 0;
-                               }
+                               if (m->control_pressed) { remove(outputFileName.c_str()); remove(accnosFileName.c_str()); for (int j = 0; j < outputNames.size(); j++) {        remove(outputNames[j].c_str()); } for (int i = 0; i < lines.size(); i++) {  delete lines[i];  }  lines.clear(); delete chimera; return 0; }
                        #endif
                        
                #endif  
@@ -460,7 +414,7 @@ int ChimeraPintailCommand::execute(){
 }
 //**********************************************************************************************************************
 
-int ChimeraPintailCommand::driver(linePair* line, string outputFName, string filename, string accnos){
+int ChimeraPintailCommand::driver(linePair* filePos, string outputFName, string filename, string accnos){
        try {
                ofstream out;
                openOutputFile(outputFName, out);
@@ -471,10 +425,13 @@ int ChimeraPintailCommand::driver(linePair* line, string outputFName, string fil
                ifstream inFASTA;
                openInputFile(filename, inFASTA);
 
-               inFASTA.seekg(line->start);
-               
-               for(int i=0;i<line->numSeqs;i++){
-               
+               inFASTA.seekg(filePos->start);
+
+               bool done = false;
+               int count = 0;
+       
+               while (!done) {
+                               
                        if (m->control_pressed) {       return 1;       }
                
                        Sequence* candidateSeq = new Sequence(inFASTA);  gobble(inFASTA);
@@ -492,20 +449,24 @@ int ChimeraPintailCommand::driver(linePair* line, string outputFName, string fil
                                        //print results
                                        chimera->print(out, out2);
                                }
+                               count++;
                        }
                        delete candidateSeq;
                        
+                       unsigned long int pos = inFASTA.tellg();
+                       if ((pos == -1) || (pos >= filePos->end)) { break; }
+                       
                        //report progress
-                       if((i+1) % 100 == 0){   m->mothurOut("Processing sequence: " + toString(i+1)); m->mothurOutEndLine();           }
+                       if((count) % 100 == 0){ m->mothurOut("Processing sequence: " + toString(count)); m->mothurOutEndLine();         }
                }
                //report progress
-               if((line->numSeqs) % 100 != 0){ m->mothurOut("Processing sequence: " + toString(line->numSeqs)); m->mothurOutEndLine();         }
+               if((count) % 100 != 0){ m->mothurOut("Processing sequence: " + toString(count)); m->mothurOutEndLine();         }
                
                out.close();
                out2.close();
                inFASTA.close();
                                
-               return 0;
+               return count;
        }
        catch(exception& e) {
                m->errorOut(e, "ChimeraPintailCommand", "driver");
@@ -576,7 +537,7 @@ int ChimeraPintailCommand::createProcesses(string outputFileName, string filenam
        try {
 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
                int process = 0;
-               //              processIDS.resize(0);
+               int num = 0;
                
                //loop through and create all the processes you want
                while (process != processors) {
@@ -586,7 +547,15 @@ int ChimeraPintailCommand::createProcesses(string outputFileName, string filenam
                                processIDS.push_back(pid);  //create map from line number to pid so you can append files in correct order later
                                process++;
                        }else if (pid == 0){
-                               driver(lines[process], outputFileName + toString(getpid()) + ".temp", filename, accnos + toString(getpid()) + ".temp");
+                               num = driver(lines[process], outputFileName + toString(getpid()) + ".temp", filename, accnos + toString(getpid()) + ".temp");
+                               
+                               //pass numSeqs to parent
+                               ofstream out;
+                               string tempFile = toString(getpid()) + ".temp";
+                               openOutputFile(tempFile, out);
+                               out << num << endl;
+                               out.close();
+
                                exit(0);
                        }else { m->mothurOut("unable to spawn the necessary processes."); m->mothurOutEndLine(); exit(0); }
                }
@@ -597,7 +566,15 @@ int ChimeraPintailCommand::createProcesses(string outputFileName, string filenam
                        wait(&temp);
                }
                
-               return 0;
+               for (int i = 0; i < processIDS.size(); i++) {
+                       ifstream in;
+                       string tempFile =  toString(processIDS[i]) + ".temp";
+                       openInputFile(tempFile, in);
+                       if (!in.eof()) { int tempNum = 0; in >> tempNum; num += tempNum; }
+                       in.close(); remove(tempFile.c_str());
+               }
+               
+               return num;
 #endif         
        }
        catch(exception& e) {