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(string tag, float gO, float gE, float m, float mM) : Database(),
18 gapOpen(gO), gapExtend(gE), match(m), misMatch(mM) {
22 int randNumber = rand();
23 dbFileName = tag + toString(randNumber) + ".template.unaligned.fasta";
24 queryFileName = tag + toString(randNumber) + ".candidate.unaligned.fasta";
25 blastFileName = tag + 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.
81 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
83 blastCommand = path + "blast/bin/blastall -p blastn -d " + dbFileName + " -m 8 -W 28 -v " + toString(n) + " -b " + toString(n);;
84 blastCommand += (" -i " + (queryFileName+seq->getName()) + " -o " + blastFileName+seq->getName());
86 blastCommand = "\"" + path + "blast\\bin\\blastall\" -p blastn -d " + "\"" + dbFileName + "\"" + " -m 8 -W 28 -v " + toString(n) + " -b " + toString(n);
87 blastCommand += (" -i " + (queryFileName+seq->getName()) + " -o " + blastFileName+seq->getName());
88 //wrap entire string in ""
89 blastCommand = "\"" + blastCommand + "\"";
91 system(blastCommand.c_str());
93 ifstream m8FileHandle;
94 m->openInputFile(blastFileName+seq->getName(), m8FileHandle, "no error");
97 int templateAccession;
98 m->gobble(m8FileHandle);
100 while(!m8FileHandle.eof()){
101 m8FileHandle >> dummy >> templateAccession >> searchScore;
103 //get rest of junk in line
104 while (!m8FileHandle.eof()) { char c = m8FileHandle.get(); if (c == 10 || c == 13){ break; } }
106 m->gobble(m8FileHandle);
107 topMatches.push_back(templateAccession);
109 m8FileHandle.close();
110 remove((queryFileName+seq->getName()).c_str());
111 remove((blastFileName+seq->getName()).c_str());
115 catch(exception& e) {
116 m->errorOut(e, "BlastDB", "findClosestSequences");
121 /**************************************************************************************************/
122 //assumes you have added all the template sequences using the addSequence function and run generateDB.
123 vector<int> BlastDB::findClosestMegaBlast(Sequence* seq, int n, int minPerID) {
125 vector<int> topMatches;
126 float numBases, mismatch, gap, startQuery, endQuery, startRef, endRef, score;
131 m->openOutputFile((queryFileName+seq->getName()), queryFile);
132 queryFile << '>' << seq->getName() << endl;
133 queryFile << seq->getUnaligned() << endl;
135 // cout << seq->getUnaligned() << endl;
136 // the goal here is to quickly survey the database to find the closest match. To do this we are using the default
137 // wordsize used in megablast. I'm sure we're sacrificing accuracy for speed, but anyother way would take way too
138 // long. With this setting, it seems comparable in speed to the suffix tree approach.
139 //7000004128189528left 0 100 66 0 0 1 66 61 126 1e-31 131
141 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
142 blastCommand = path + "blast/bin/megablast -e 1e-10 -d " + dbFileName + " -m 8 -b " + toString(n) + " -v " + toString(n); //-W 28 -p blastn
143 blastCommand += (" -i " + (queryFileName+seq->getName()) + " -o " + blastFileName+seq->getName());
145 blastCommand = "\"" + path + "blast\\bin\\megablast\" -e 1e-10 -d " + "\"" + dbFileName + "\"" + " -m 8 -b " + toString(n) + " -v " + toString(n); //-W 28 -p blastn
146 blastCommand += (" -i " + (queryFileName+seq->getName()) + " -o " + blastFileName+seq->getName());
147 //wrap entire string in ""
148 blastCommand = "\"" + blastCommand + "\"";
152 system(blastCommand.c_str());
154 ifstream m8FileHandle;
155 m->openInputFile(blastFileName+seq->getName(), m8FileHandle, "no error");
157 string dummy, eScore;
158 int templateAccession;
159 m->gobble(m8FileHandle);
161 while(!m8FileHandle.eof()){
162 m8FileHandle >> dummy >> templateAccession >> searchScore >> numBases >> mismatch >> gap >> startQuery >> endQuery >> startRef >> endRef >> eScore >> score;
163 // cout << dummy << '\t' << templateAccession << '\t' << searchScore << '\t' << numBases << '\t' << mismatch << '\t' << gap << '\t' << startQuery << '\t' << endQuery << '\t' << startRef << '\t' << endRef << '\t' << eScore << '\t' << score << endl;
165 //get rest of junk in line
166 //while (!m8FileHandle.eof()) { char c = m8FileHandle.get(); if (c == 10 || c == 13){ break; }else{ cout << c; } } //
168 m->gobble(m8FileHandle);
169 if (searchScore >= minPerID) {
170 topMatches.push_back(templateAccession);
171 Scores.push_back(searchScore);
173 //cout << templateAccession << endl;
175 m8FileHandle.close();
176 remove((queryFileName+seq->getName()).c_str());
177 remove((blastFileName+seq->getName()).c_str());
181 catch(exception& e) {
182 m->errorOut(e, "BlastDB", "findClosestMegaBlast");
186 /**************************************************************************************************/
187 void BlastDB::addSequence(Sequence seq) {
190 ofstream unalignedFastaFile;
191 m->openOutputFileAppend(dbFileName, unalignedFastaFile);
193 // generating a fasta file with unaligned template
194 unalignedFastaFile << '>' << count << endl; // sequences, which will be input to formatdb
195 unalignedFastaFile << seq.getUnaligned() << endl;
196 unalignedFastaFile.close();
200 catch(exception& e) {
201 m->errorOut(e, "BlastDB", "addSequence");
205 /**************************************************************************************************/
206 void BlastDB::generateDB() {
209 //m->mothurOut("Generating the temporary BLAST database...\t"); cout.flush();
212 string tempPath = path;
213 for (int i = 0; i < path.length(); i++) { tempPath[i] = tolower(path[i]); }
214 path = path.substr(0, (tempPath.find_last_of('m')));
216 string formatdbCommand;
218 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
219 formatdbCommand = path + "blast/bin/formatdb -p F -o T -i " + dbFileName; // format the database, -o option gives us the ability
221 formatdbCommand = "\"" + path + "blast\\bin\\formatdb\" -p F -o T -i " + "\"" + dbFileName + "\"";
222 //wrap entire string in ""
223 formatdbCommand = "\"" + formatdbCommand + "\"";
225 system(formatdbCommand.c_str()); // to get the right sequence names, i think. -p F
226 // option tells formatdb that seqs are DNA, not prot
227 //m->mothurOut("DONE."); m->mothurOutEndLine(); m->mothurOutEndLine(); cout.flush();
229 catch(exception& e) {
230 m->errorOut(e, "BlastDB", "generateDB");
234 /**************************************************************************************************/
236 /**************************************************************************************************/