5 * Created by Pat Schloss on 12/22/08.
6 * Copyright 2008 Patrick D. Schloss. All rights reserved.
13 #include "database.hpp"
14 #include "sequence.hpp"
15 #include "blastdb.hpp"
17 /**************************************************************************************************/
19 BlastDB::BlastDB(string fastaFileName, float gO, float gE, float m, float mM) : Database(fastaFileName),
20 gapOpen(gO), gapExtend(gE), match(m), misMatch(mM) {
22 globaldata = GlobalData::getInstance();
24 cout << "Generating the temporary BLAST database...\t"; cout.flush();
26 int randNumber = rand();
27 dbFileName = toString(randNumber) + ".template.unaligned.fasta";
28 queryFileName = toString(randNumber) + ".candidate.unaligned.fasta";
29 blastFileName = toString(randNumber) + ".blast";
32 ofstream unalignedFastaFile;
33 openOutputFile(dbFileName, unalignedFastaFile);
35 for(int i=0;i<numSeqs;i++){ // generating a fasta file with unaligned template
36 unalignedFastaFile << '>' << i << endl; // sequences, which will be input to formatdb
37 unalignedFastaFile << templateSequences[i]->getUnaligned() << endl;
39 unalignedFastaFile.close();
41 path = globaldata->argv;
42 path = path.substr(0, (path.find_last_of('m')));
44 string formatdbCommand = path + "blast/bin/formatdb -p F -o T -i " + dbFileName; // format the database, -o option gives us the ability
45 system(formatdbCommand.c_str()); // to get the right sequence names, i think. -p F
46 // option tells formatdb that seqs are DNA, not prot
47 cout << "DONE." << endl << endl; cout.flush();
48 emptySequence = new Sequence();
49 emptySequence->setName("no_match");
50 emptySequence->setUnaligned("XXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
51 emptySequence->setAligned("XXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
56 /**************************************************************************************************/
59 remove(queryFileName.c_str()); // let's clean stuff up and remove the temp files
60 remove(dbFileName.c_str()); // let's clean stuff up and remove the temp files
61 remove(blastFileName.c_str()); // let's clean stuff up and remove the temp files
64 /**************************************************************************************************/
66 Sequence* BlastDB::findClosestSequence(Sequence* candidate){
69 openOutputFile(queryFileName, queryFile);
70 queryFile << '>' << candidate->getName() << endl;
71 queryFile << candidate->getUnaligned() << endl;
75 // the goal here is to quickly survey the database to find the closest match. To do this we are using the default
76 // wordsize used in megablast. I'm sure we're sacrificing accuracy for speed, but anyother way would take way too
77 // long. With this setting, it seems comparable in speed to the suffix tree approach.
79 string blastCommand = path + "blast/bin/blastall -p blastn -d " + dbFileName + " -b 1 -m 8 -W 28";
80 blastCommand += (" -i " + queryFileName + " -o " + blastFileName);
81 system(blastCommand.c_str());
83 ifstream m8FileHandle;
84 openInputFile(blastFileName, m8FileHandle);
87 int templateAccession;
89 if(!m8FileHandle.eof()){
90 m8FileHandle >> dummy >> templateAccession >> searchScore;
92 return templateSequences[templateAccession];
100 /**************************************************************************************************/