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) {
20 globaldata = GlobalData::getInstance();
23 int randNumber = rand();
24 dbFileName = toString(randNumber) + ".template.unaligned.fasta";
25 queryFileName = toString(randNumber) + ".candidate.unaligned.fasta";
26 blastFileName = toString(randNumber) + ".blast";
29 /**************************************************************************************************/
31 BlastDB::BlastDB() : Database() {
33 globaldata = GlobalData::getInstance();
36 int randNumber = rand();
37 dbFileName = toString(randNumber) + ".template.unaligned.fasta";
38 queryFileName = toString(randNumber) + ".candidate.unaligned.fasta";
39 blastFileName = toString(randNumber) + ".blast";
43 /**************************************************************************************************/
46 remove(queryFileName.c_str()); // let's clean stuff up and remove the temp files
47 remove(dbFileName.c_str()); // let's clean stuff up and remove the temp files
48 remove(blastFileName.c_str()); // let's clean stuff up and remove the temp files
50 /**************************************************************************************************/
51 //assumes you have added all the template sequences using the addSequence function and run generateDB.
52 vector<int> BlastDB::findClosestSequences(Sequence* seq, int n) {
54 vector<int> topMatches;
57 openOutputFile(queryFileName, queryFile);
58 queryFile << '>' << seq->getName() << endl;
59 queryFile << seq->getUnaligned() << endl;
62 // the goal here is to quickly survey the database to find the closest match. To do this we are using the default
63 // wordsize used in megablast. I'm sure we're sacrificing accuracy for speed, but anyother way would take way too
64 // long. With this setting, it seems comparable in speed to the suffix tree approach.
66 string blastCommand = path + "blast/bin/blastall -p blastn -d " + dbFileName + " -m 8 -W 28 -v " + toString(n) + " -b " + toString(n);;
67 blastCommand += (" -i " + queryFileName + " -o " + blastFileName);
68 system(blastCommand.c_str());
70 ifstream m8FileHandle;
71 openInputFile(blastFileName, m8FileHandle);
74 int templateAccession;
77 while(!m8FileHandle.eof()){
78 m8FileHandle >> dummy >> templateAccession >> searchScore;
80 //get rest of junk in line
81 while (!m8FileHandle.eof()) { char c = m8FileHandle.get(); if (c == 10 || c == 13){ break; } }
84 topMatches.push_back(templateAccession);
91 m->errorOut(e, "BlastDB", "findClosestSequences");
96 /**************************************************************************************************/
97 //assumes you have added all the template sequences using the addSequence function and run generateDB.
98 vector<int> BlastDB::findClosestMegaBlast(Sequence* seq, int n) {
100 vector<int> topMatches;
103 openOutputFile(queryFileName, queryFile);
104 queryFile << '>' << seq->getName() << endl;
105 queryFile << seq->getUnaligned() << endl;
108 // the goal here is to quickly survey the database to find the closest match. To do this we are using the default
109 // wordsize used in megablast. I'm sure we're sacrificing accuracy for speed, but anyother way would take way too
110 // long. With this setting, it seems comparable in speed to the suffix tree approach.
112 string blastCommand = path + "blast/bin/megablast -e 1e-10 -d " + dbFileName + " -m 8 -b " + toString(n) + " -v " + toString(n); //-W 28 -p blastn
113 blastCommand += (" -i " + queryFileName + " -o " + blastFileName);
114 system(blastCommand.c_str());
116 ifstream m8FileHandle;
117 openInputFile(blastFileName, m8FileHandle, "no error");
120 int templateAccession;
121 gobble(m8FileHandle);
123 while(!m8FileHandle.eof()){
124 m8FileHandle >> dummy >> templateAccession >> searchScore;
126 //get rest of junk in line
127 while (!m8FileHandle.eof()) { char c = m8FileHandle.get(); if (c == 10 || c == 13){ break; } }
129 gobble(m8FileHandle);
130 topMatches.push_back(templateAccession);
131 //cout << templateAccession << endl;
133 m8FileHandle.close();
137 catch(exception& e) {
138 m->errorOut(e, "BlastDB", "findClosest");
142 /**************************************************************************************************/
143 void BlastDB::addSequence(Sequence seq) {
146 ofstream unalignedFastaFile;
147 openOutputFileAppend(dbFileName, unalignedFastaFile);
149 // generating a fasta file with unaligned template
150 unalignedFastaFile << '>' << count << endl; // sequences, which will be input to formatdb
151 unalignedFastaFile << seq.getUnaligned() << endl;
152 unalignedFastaFile.close();
156 catch(exception& e) {
157 m->errorOut(e, "BlastDB", "addSequence");
161 /**************************************************************************************************/
162 void BlastDB::generateDB() {
165 //m->mothurOut("Generating the temporary BLAST database...\t"); cout.flush();
167 path = globaldata->argv;
168 path = path.substr(0, (path.find_last_of('m')));
170 string formatdbCommand = path + "blast/bin/formatdb -p F -o T -i " + dbFileName; // format the database, -o option gives us the ability
171 system(formatdbCommand.c_str()); // to get the right sequence names, i think. -p F
172 // option tells formatdb that seqs are DNA, not prot
173 //m->mothurOut("DONE."); m->mothurOutEndLine(); m->mothurOutEndLine(); cout.flush();
175 catch(exception& e) {
176 m->errorOut(e, "BlastDB", "generateDB");
180 /**************************************************************************************************/
182 /**************************************************************************************************/