5 * Created by Pat Schloss on 12/22/08.
6 * Copyright 2008 Patrick D. Schloss. All rights reserved.
11 #include "database.hpp"
12 #include "sequence.hpp"
13 #include "blastdb.hpp"
15 /**************************************************************************************************/
17 BlastDB::BlastDB(float gO, float gE, float m, float mM) : Database(),
18 gapOpen(gO), gapExtend(gE), match(m), misMatch(mM) {
22 int randNumber = rand();
23 dbFileName = toString(randNumber) + ".template.unaligned.fasta";
24 queryFileName = toString(randNumber) + ".candidate.unaligned.fasta";
25 blastFileName = toString(randNumber) + ".blast";
28 /**************************************************************************************************/
30 BlastDB::BlastDB() : Database() {
34 int randNumber = rand();
35 dbFileName = toString(randNumber) + ".template.unaligned.fasta";
36 queryFileName = toString(randNumber) + ".candidate.unaligned.fasta";
37 blastFileName = toString(randNumber) + ".blast";
40 m->errorOut(e, "BlastDB", "BlastDB");
45 /**************************************************************************************************/
49 remove(queryFileName.c_str()); // let's clean stuff up and remove the temp files
50 remove(dbFileName.c_str()); // let's clean stuff up and remove the temp files
51 remove((dbFileName+".nsq").c_str()); // let's clean stuff up and remove the temp files
52 remove((dbFileName+".nsi").c_str()); // let's clean stuff up and remove the temp files
53 remove((dbFileName+".nsd").c_str()); // let's clean stuff up and remove the temp files
54 remove((dbFileName+".nin").c_str()); // let's clean stuff up and remove the temp files
55 remove((dbFileName+".nhr").c_str()); // let's clean stuff up and remove the temp files
56 remove(blastFileName.c_str()); // let's clean stuff up and remove the temp files
59 m->errorOut(e, "BlastDB", "~BlastDB");
63 /**************************************************************************************************/
64 //assumes you have added all the template sequences using the addSequence function and run generateDB.
65 vector<int> BlastDB::findClosestSequences(Sequence* seq, int n) {
67 vector<int> topMatches;
70 m->openOutputFile((queryFileName+seq->getName()), queryFile);
71 queryFile << '>' << seq->getName() << endl;
72 queryFile << seq->getUnaligned() << endl;
76 // the goal here is to quickly survey the database to find the closest match. To do this we are using the default
77 // wordsize used in megablast. I'm sure we're sacrificing accuracy for speed, but anyother way would take way too
78 // long. With this setting, it seems comparable in speed to the suffix tree approach.
80 string blastCommand = path + "blast/bin/blastall -p blastn -d " + dbFileName + " -m 8 -W 28 -v " + toString(n) + " -b " + toString(n);;
81 blastCommand += (" -i " + (queryFileName+seq->getName()) + " -o " + blastFileName+seq->getName());
82 system(blastCommand.c_str());
84 ifstream m8FileHandle;
85 m->openInputFile(blastFileName+seq->getName(), m8FileHandle, "no error");
88 int templateAccession;
89 m->gobble(m8FileHandle);
91 while(!m8FileHandle.eof()){
92 m8FileHandle >> dummy >> templateAccession >> searchScore;
94 //get rest of junk in line
95 while (!m8FileHandle.eof()) { char c = m8FileHandle.get(); if (c == 10 || c == 13){ break; } }
97 m->gobble(m8FileHandle);
98 topMatches.push_back(templateAccession);
100 m8FileHandle.close();
101 remove((queryFileName+seq->getName()).c_str());
102 remove((blastFileName+seq->getName()).c_str());
106 catch(exception& e) {
107 m->errorOut(e, "BlastDB", "findClosestSequences");
112 /**************************************************************************************************/
113 //assumes you have added all the template sequences using the addSequence function and run generateDB.
114 vector<int> BlastDB::findClosestMegaBlast(Sequence* seq, int n) {
116 vector<int> topMatches;
120 m->openOutputFile((queryFileName+seq->getName()), queryFile);
121 queryFile << '>' << seq->getName() << endl;
122 queryFile << seq->getUnaligned() << endl;
125 // the goal here is to quickly survey the database to find the closest match. To do this we are using the default
126 // wordsize used in megablast. I'm sure we're sacrificing accuracy for speed, but anyother way would take way too
127 // long. With this setting, it seems comparable in speed to the suffix tree approach.
129 string blastCommand = path + "blast/bin/megablast -e 1e-10 -d " + dbFileName + " -m 8 -b " + toString(n) + " -v " + toString(n); //-W 28 -p blastn
130 blastCommand += (" -i " + (queryFileName+seq->getName()) + " -o " + blastFileName+seq->getName());
131 system(blastCommand.c_str());
133 ifstream m8FileHandle;
134 m->openInputFile(blastFileName+seq->getName(), m8FileHandle, "no error");
137 int templateAccession;
138 m->gobble(m8FileHandle);
140 while(!m8FileHandle.eof()){
141 m8FileHandle >> dummy >> templateAccession >> searchScore;
142 //cout << templateAccession << '\t' << searchScore << endl;
144 //get rest of junk in line
145 while (!m8FileHandle.eof()) { char c = m8FileHandle.get(); if (c == 10 || c == 13){ break; } }
147 m->gobble(m8FileHandle);
148 topMatches.push_back(templateAccession);
149 megaScores.push_back(searchScore);
150 //cout << templateAccession << endl;
152 m8FileHandle.close();
153 remove((queryFileName+seq->getName()).c_str());
154 remove((blastFileName+seq->getName()).c_str());
158 catch(exception& e) {
159 m->errorOut(e, "BlastDB", "findClosestMegaBlast");
163 /**************************************************************************************************/
164 void BlastDB::addSequence(Sequence seq) {
167 ofstream unalignedFastaFile;
168 m->openOutputFileAppend(dbFileName, unalignedFastaFile);
170 // generating a fasta file with unaligned template
171 unalignedFastaFile << '>' << count << endl; // sequences, which will be input to formatdb
172 unalignedFastaFile << seq.getUnaligned() << endl;
173 unalignedFastaFile.close();
177 catch(exception& e) {
178 m->errorOut(e, "BlastDB", "addSequence");
182 /**************************************************************************************************/
183 void BlastDB::generateDB() {
186 //m->mothurOut("Generating the temporary BLAST database...\t"); cout.flush();
189 path = path.substr(0, (path.find_last_of('m')));
191 string formatdbCommand = path + "blast/bin/formatdb -p F -o T -i " + dbFileName; // format the database, -o option gives us the ability
192 system(formatdbCommand.c_str()); // to get the right sequence names, i think. -p F
193 // option tells formatdb that seqs are DNA, not prot
194 //m->mothurOut("DONE."); m->mothurOutEndLine(); m->mothurOutEndLine(); cout.flush();
196 catch(exception& e) {
197 m->errorOut(e, "BlastDB", "generateDB");
201 /**************************************************************************************************/
203 /**************************************************************************************************/