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+seq->getName()), queryFile);
58 queryFile << '>' << seq->getName() << endl;
59 queryFile << seq->getUnaligned() << endl;
63 // the goal here is to quickly survey the database to find the closest match. To do this we are using the default
64 // wordsize used in megablast. I'm sure we're sacrificing accuracy for speed, but anyother way would take way too
65 // long. With this setting, it seems comparable in speed to the suffix tree approach.
67 string blastCommand = path + "blast/bin/blastall -p blastn -d " + dbFileName + " -m 8 -W 28 -v " + toString(n) + " -b " + toString(n);;
68 blastCommand += (" -i " + (queryFileName+seq->getName()) + " -o " + blastFileName+seq->getName());
69 system(blastCommand.c_str());
71 ifstream m8FileHandle;
72 openInputFile(blastFileName+seq->getName(), m8FileHandle, "no error");
75 int templateAccession;
78 while(!m8FileHandle.eof()){
79 m8FileHandle >> dummy >> templateAccession >> searchScore;
81 //get rest of junk in line
82 while (!m8FileHandle.eof()) { char c = m8FileHandle.get(); if (c == 10 || c == 13){ break; } }
85 topMatches.push_back(templateAccession);
88 remove((queryFileName+seq->getName()).c_str());
89 remove((blastFileName+seq->getName()).c_str());
94 m->errorOut(e, "BlastDB", "findClosestSequences");
99 /**************************************************************************************************/
100 //assumes you have added all the template sequences using the addSequence function and run generateDB.
101 vector<int> BlastDB::findClosestMegaBlast(Sequence* seq, int n) {
103 vector<int> topMatches;
106 openOutputFile((queryFileName+seq->getName()), queryFile);
107 queryFile << '>' << seq->getName() << endl;
108 queryFile << seq->getUnaligned() << endl;
111 // the goal here is to quickly survey the database to find the closest match. To do this we are using the default
112 // wordsize used in megablast. I'm sure we're sacrificing accuracy for speed, but anyother way would take way too
113 // long. With this setting, it seems comparable in speed to the suffix tree approach.
115 string blastCommand = path + "blast/bin/megablast -e 1e-10 -d " + dbFileName + " -m 8 -b " + toString(n) + " -v " + toString(n); //-W 28 -p blastn
116 blastCommand += (" -i " + (queryFileName+seq->getName()) + " -o " + blastFileName+seq->getName());
117 system(blastCommand.c_str());
119 ifstream m8FileHandle;
120 openInputFile(blastFileName+seq->getName(), m8FileHandle, "no error");
123 int templateAccession;
124 gobble(m8FileHandle);
126 while(!m8FileHandle.eof()){
127 m8FileHandle >> dummy >> templateAccession >> searchScore;
129 //get rest of junk in line
130 while (!m8FileHandle.eof()) { char c = m8FileHandle.get(); if (c == 10 || c == 13){ break; } }
132 gobble(m8FileHandle);
133 topMatches.push_back(templateAccession);
134 //cout << templateAccession << endl;
136 m8FileHandle.close();
137 remove((queryFileName+seq->getName()).c_str());
138 remove((blastFileName+seq->getName()).c_str());
142 catch(exception& e) {
143 m->errorOut(e, "BlastDB", "findClosest");
147 /**************************************************************************************************/
148 void BlastDB::addSequence(Sequence seq) {
151 ofstream unalignedFastaFile;
152 openOutputFileAppend(dbFileName, unalignedFastaFile);
154 // generating a fasta file with unaligned template
155 unalignedFastaFile << '>' << count << endl; // sequences, which will be input to formatdb
156 unalignedFastaFile << seq.getUnaligned() << endl;
157 unalignedFastaFile.close();
161 catch(exception& e) {
162 m->errorOut(e, "BlastDB", "addSequence");
166 /**************************************************************************************************/
167 void BlastDB::generateDB() {
170 //m->mothurOut("Generating the temporary BLAST database...\t"); cout.flush();
172 path = globaldata->argv;
173 path = path.substr(0, (path.find_last_of('m')));
175 string formatdbCommand = path + "blast/bin/formatdb -p F -o T -i " + dbFileName; // format the database, -o option gives us the ability
176 system(formatdbCommand.c_str()); // to get the right sequence names, i think. -p F
177 // option tells formatdb that seqs are DNA, not prot
178 //m->mothurOut("DONE."); m->mothurOutEndLine(); m->mothurOutEndLine(); cout.flush();
180 catch(exception& e) {
181 m->errorOut(e, "BlastDB", "generateDB");
185 /**************************************************************************************************/
187 /**************************************************************************************************/