]> git.donarmstrong.com Git - mothur.git/blobdiff - alignmentdb.cpp
added save parameter to align.seqs, chimera commands, classify.seqs, and seq.error...
[mothur.git] / alignmentdb.cpp
index 6909f47b1235c72b67ae409907af97346dfa8007..0fd2ac4dca4ff4fcfc770488ab96f40dc21c38d7 100644 (file)
@@ -11,6 +11,7 @@
 #include "kmerdb.hpp"
 #include "suffixdb.hpp"
 #include "blastdb.hpp"
+#include "referencedb.h"
 
 
 /**************************************************************************************************/
@@ -20,91 +21,118 @@ AlignmentDB::AlignmentDB(string fastaFileName, string s, int kmerSize, float gap
                longest = 0;
                method = s;
                bool needToGenerate = true;
+               ReferenceDB* rdb = ReferenceDB::getInstance();
                
-               m->mothurOutEndLine();
-               m->mothurOut("Reading in the " + fastaFileName + " template sequences...\t");   cout.flush();
+               if (fastaFileName == "saved") {
+                       int start = time(NULL);
+                       m->mothurOutEndLine();  m->mothurOut("Using sequences from " + rdb->getSavedReference() + " that are saved in memory.");        m->mothurOutEndLine();
+
+                       for (int i = 0; i < rdb->referenceSeqs.size(); i++) {
+                               templateSequences.push_back(rdb->referenceSeqs[i]);
+                               //save longest base
+                               if (rdb->referenceSeqs[i].getUnaligned().length() >= longest)  { longest = (rdb->referenceSeqs[i].getUnaligned().length()+1); }
+                       }
+                       fastaFileName = rdb->getSavedReference();
+                       
+                       numSeqs = templateSequences.size();
+                       m->mothurOut("It took " + toString(time(NULL) - start) + " to load " + toString(rdb->referenceSeqs.size()) + " sequences.");m->mothurOutEndLine();  
+
+               }else {
+                       int start = time(NULL);
+                       m->mothurOutEndLine();
+                       m->mothurOut("Reading in the " + fastaFileName + " template sequences...\t");   cout.flush();
+                       
+                       #ifdef USE_MPI  
+                               int pid, processors;
+                               vector<unsigned long int> positions;
+                       
+                               MPI_Status status; 
+                               MPI_File inMPI;
+                               MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
+                               MPI_Comm_size(MPI_COMM_WORLD, &processors);
+                               int tag = 2001;
                
-               #ifdef USE_MPI  
-                       int pid, processors;
-                       vector<unsigned long int> positions;
+                               char inFileName[1024];
+                               strcpy(inFileName, fastaFileName.c_str());
                
-                       MPI_Status status; 
-                       MPI_File inMPI;
-                       MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
-                       MPI_Comm_size(MPI_COMM_WORLD, &processors);
-                       int tag = 2001;
-       
-                       char inFileName[1024];
-                       strcpy(inFileName, fastaFileName.c_str());
-       
-                       MPI_File_open(MPI_COMM_WORLD, inFileName, MPI_MODE_RDONLY, MPI_INFO_NULL, &inMPI);  //comm, filename, mode, info, filepointer
+                               MPI_File_open(MPI_COMM_WORLD, inFileName, MPI_MODE_RDONLY, MPI_INFO_NULL, &inMPI);  //comm, filename, mode, info, filepointer
+                               
+                               if (pid == 0) {
+                                       positions = m->setFilePosFasta(fastaFileName, numSeqs); //fills MPIPos, returns numSeqs
+
+                                       //send file positions to all processes
+                                       for(int i = 1; i < processors; i++) { 
+                                               MPI_Send(&numSeqs, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
+                                               MPI_Send(&positions[0], (numSeqs+1), MPI_LONG, i, tag, MPI_COMM_WORLD);
+                                       }
+                               }else{
+                                       MPI_Recv(&numSeqs, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
+                                       positions.resize(numSeqs+1);
+                                       MPI_Recv(&positions[0], (numSeqs+1), MPI_LONG, 0, tag, MPI_COMM_WORLD, &status);
+                               }
+                       
+                               //read file 
+                               for(int i=0;i<numSeqs;i++){
+                                       
+                                       if (m->control_pressed) {  templateSequences.clear(); break;  }
+                                       
+                                       //read next sequence
+                                       int length = positions[i+1] - positions[i];
+                                       char* buf4 = new char[length];
+                               
+                                       MPI_File_read_at(inMPI, positions[i], buf4, length, MPI_CHAR, &status);
                        
-                       if (pid == 0) {
-                               positions = m->setFilePosFasta(fastaFileName, numSeqs); //fills MPIPos, returns numSeqs
+                                       string tempBuf = buf4;
+                                       if (tempBuf.length() > length) { tempBuf = tempBuf.substr(0, length); }
+                                       delete buf4;
 
-                               //send file positions to all processes
-                               for(int i = 1; i < processors; i++) { 
-                                       MPI_Send(&numSeqs, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
-                                       MPI_Send(&positions[0], (numSeqs+1), MPI_LONG, i, tag, MPI_COMM_WORLD);
+                                       istringstream iss (tempBuf,istringstream::in);
+                       
+                                       Sequence temp(iss);  
+                                       if (temp.getName() != "") {
+                                               templateSequences.push_back(temp);
+                                               
+                                               if (rdb->save) { rdb->referenceSeqs.push_back(temp); }
+                                               
+                                               //save longest base
+                                               if (temp.getUnaligned().length() >= longest)  { longest = temp.getUnaligned().length()+1; }
+                                       }
                                }
-                       }else{
-                               MPI_Recv(&numSeqs, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
-                               positions.resize(numSeqs+1);
-                               MPI_Recv(&positions[0], (numSeqs+1), MPI_LONG, 0, tag, MPI_COMM_WORLD, &status);
-                       }
-               
-                       //read file 
-                       for(int i=0;i<numSeqs;i++){
                                
-                               if (m->control_pressed) {  templateSequences.clear(); break;  }
+                               MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
                                
-                               //read next sequence
-                               int length = positions[i+1] - positions[i];
-                               char* buf4 = new char[length];
+                               MPI_File_close(&inMPI);
                        
-                               MPI_File_read_at(inMPI, positions[i], buf4, length, MPI_CHAR, &status);
-               
-                               string tempBuf = buf4;
-                               if (tempBuf.length() > length) { tempBuf = tempBuf.substr(0, length); }
-                               delete buf4;
+               #else
+                       ifstream fastaFile;
+                       m->openInputFile(fastaFileName, fastaFile);
 
-                               istringstream iss (tempBuf,istringstream::in);
-               
-                               Sequence temp(iss);  
+                       while (!fastaFile.eof()) {
+                               Sequence temp(fastaFile);  m->gobble(fastaFile);
+                               
+                               if (m->control_pressed) {  templateSequences.clear(); break;  }
+                               
                                if (temp.getName() != "") {
                                        templateSequences.push_back(temp);
+                                       
+                                       if (rdb->save) { rdb->referenceSeqs.push_back(temp); }
+                                       
                                        //save longest base
-                                       if (temp.getUnaligned().length() >= longest)  { longest = temp.getUnaligned().length()+1; }
+                                       if (temp.getUnaligned().length() >= longest)  { longest = (temp.getUnaligned().length()+1); }
                                }
                        }
-                       
-                       MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
-                       
-                       MPI_File_close(&inMPI);
+                       fastaFile.close();
+               #endif
                
-       #else
-               ifstream fastaFile;
-               m->openInputFile(fastaFileName, fastaFile);
-
-               while (!fastaFile.eof()) {
-                       Sequence temp(fastaFile);  m->gobble(fastaFile);
-                       
-                       if (m->control_pressed) {  templateSequences.clear(); break;  }
+                       numSeqs = templateSequences.size();
+                       //all of this is elsewhere already!
                        
-                       if (temp.getName() != "") {
-                               templateSequences.push_back(temp);
-                               //save longest base
-                               if (temp.getUnaligned().length() >= longest)  { longest = (temp.getUnaligned().length()+1); }
-                       }
+                       m->mothurOut("DONE.");
+                       m->mothurOutEndLine();  cout.flush();
+                       m->mothurOut("It took " + toString(time(NULL) - start) + " to read  " + toString(rdb->referenceSeqs.size()) + " sequences."); m->mothurOutEndLine();  
+
                }
-               fastaFile.close();
-       #endif
-       
-               numSeqs = templateSequences.size();
-               //all of this is elsewhere already!
                
-               m->mothurOut("DONE.");
-               m->mothurOutEndLine();  cout.flush();
                
                //in case you delete the seqs and then ask for them
                emptySequence = Sequence();
@@ -156,6 +184,7 @@ AlignmentDB::AlignmentDB(string fastaFileName, string s, int kmerSize, float gap
                
                        search->setNumSeqs(numSeqs);
                }
+               
        }
        catch(exception& e) {
                m->errorOut(e, "AlignmentDB", "AlignmentDB");
@@ -192,7 +221,7 @@ Sequence AlignmentDB::findClosestSequence(Sequence* seq) {
        try{
        
                vector<int> spot = search->findClosestSequences(seq, 1);
-
+       
                if (spot.size() != 0)   {               return templateSequences[spot[0]];      }
                else                                    {               return emptySequence;                           }