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 mm, float mM) : Database(),
18 gapOpen(gO), gapExtend(gE), match(mm), misMatch(mM) {
22 int randNumber = rand();
23 //int randNumber = 12345;
24 dbFileName = tag + toString(getpid()) + toString(randNumber) + ".template.unaligned.fasta";
25 queryFileName = tag + toString(getpid()) + toString(randNumber) + ".candidate.unaligned.fasta";
26 blastFileName = tag + toString(getpid()) + toString(randNumber) + ".blast";
28 //make sure blast exists in the write place
30 string tempPath = path;
31 for (int i = 0; i < path.length(); i++) { tempPath[i] = tolower(path[i]); }
32 path = path.substr(0, (tempPath.find_last_of('m')));
34 string formatdbCommand;
35 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
36 formatdbCommand = path + "blast/bin/formatdb"; // format the database, -o option gives us the ability
38 formatdbCommand = path + "blast\\bin\\formatdb.exe";
39 //wrap entire string in ""
40 //formatdbCommand = "\"" + formatdbCommand + "\"";
43 //test to make sure formatdb exists
45 formatdbCommand = m->getFullPathName(formatdbCommand);
46 int ableToOpen = m->openInputFile(formatdbCommand, in, "no error"); in.close();
47 if(ableToOpen == 1) { m->mothurOut("[ERROR]: " + formatdbCommand + " file does not exist. mothur requires formatdb.exe to be in the ./blast/bin folder relative to the mothur.exe location."); m->mothurOutEndLine(); m->control_pressed = true; }
50 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
51 blastCommand = path + "blast/bin/blastall"; // format the database, -o option gives us the ability
53 blastCommand = path + "blast\\bin\\blastall.exe";
54 //wrap entire string in ""
55 //blastCommand = "\"" + blastCommand + "\"";
58 //test to make sure formatdb exists
60 blastCommand = m->getFullPathName(blastCommand);
61 ableToOpen = m->openInputFile(blastCommand, in2, "no error"); in2.close();
62 if(ableToOpen == 1) { m->mothurOut("[ERROR]: " + blastCommand + " file does not exist. mothur requires blastall.exe to be in the ./blast/bin folder relative to the mothur.exe location."); m->mothurOutEndLine(); m->control_pressed = true; }
65 string megablastCommand;
66 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
67 megablastCommand = path + "blast/bin/megablast"; // format the database, -o option gives us the ability
69 megablastCommand = path + "blast\\bin\\megablast.exe";
70 //wrap entire string in ""
71 //megablastCommand = "\"" + megablastCommand + "\"";
74 //test to make sure formatdb exists
76 megablastCommand = m->getFullPathName(megablastCommand);
77 ableToOpen = m->openInputFile(megablastCommand, in3, "no error"); in3.close();
78 if(ableToOpen == 1) { m->mothurOut("[ERROR]: " + megablastCommand + " file does not exist. mothur requires megablast.exe to be in the ./blast/bin folder relative to the mothur.exe location."); m->mothurOutEndLine(); m->control_pressed = true; }
82 m->errorOut(e, "BlastDB", "BlastDB");
86 /**************************************************************************************************/
88 BlastDB::BlastDB() : Database() {
92 int randNumber = rand();
93 //int randNumber = 12345;
94 dbFileName = toString(randNumber) + toString(getpid()) + ".template.unaligned.fasta";
95 queryFileName = toString(randNumber) + toString(getpid()) + ".candidate.unaligned.fasta";
96 blastFileName = toString(randNumber) + toString(getpid()) + ".blast";
98 //make sure blast exists in the write place
100 string tempPath = path;
101 for (int i = 0; i < path.length(); i++) { tempPath[i] = tolower(path[i]); }
102 path = path.substr(0, (tempPath.find_last_of('m')));
104 string formatdbCommand;
105 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
106 formatdbCommand = path + "blast/bin/formatdb"; // format the database, -o option gives us the ability
108 formatdbCommand = path + "blast\\bin\\formatdb.exe";
109 //wrap entire string in ""
110 //formatdbCommand = "\"" + formatdbCommand + "\"";
113 //test to make sure formatdb exists
115 formatdbCommand = m->getFullPathName(formatdbCommand);
116 int ableToOpen = m->openInputFile(formatdbCommand, in, "no error"); in.close();
117 if(ableToOpen == 1) { m->mothurOut("[ERROR]: " + formatdbCommand + " file does not exist. mothur requires formatdb.exe to be in the ./blast/bin folder relative to the mothur.exe location."); m->mothurOutEndLine(); m->control_pressed = true; }
120 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
121 blastCommand = path + "blast/bin/blastall"; // format the database, -o option gives us the ability
123 blastCommand = path + "blast\\bin\\blastall.exe";
124 //wrap entire string in ""
125 //blastCommand = "\"" + blastCommand + "\"";
128 //test to make sure formatdb exists
130 blastCommand = m->getFullPathName(blastCommand);
131 ableToOpen = m->openInputFile(blastCommand, in2, "no error"); in2.close();
132 if(ableToOpen == 1) { m->mothurOut("[ERROR]: " + blastCommand + " file does not exist. mothur requires blastall.exe to be in the ./blast/bin folder relative to the mothur.exe location."); m->mothurOutEndLine(); m->control_pressed = true; }
135 string megablastCommand;
136 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
137 megablastCommand = path + "blast/bin/megablast"; // format the database, -o option gives us the ability
139 megablastCommand = path + "blast\\bin\\megablast.exe";
140 //wrap entire string in ""
141 //megablastCommand = "\"" + megablastCommand + "\"";
144 //test to make sure formatdb exists
146 megablastCommand = m->getFullPathName(megablastCommand);
147 ableToOpen = m->openInputFile(megablastCommand, in3, "no error"); in3.close();
148 if(ableToOpen == 1) { m->mothurOut("[ERROR]: " + megablastCommand + " file does not exist. mothur requires megablast.exe to be in the ./blast/bin folder relative to the mothur.exe location."); m->mothurOutEndLine(); m->control_pressed = true; }
152 catch(exception& e) {
153 m->errorOut(e, "BlastDB", "BlastDB");
158 /**************************************************************************************************/
162 remove(queryFileName.c_str()); // let's clean stuff up and remove the temp files
163 remove(dbFileName.c_str()); // let's clean stuff up and remove the temp files
164 remove((dbFileName+".nsq").c_str()); // let's clean stuff up and remove the temp files
165 remove((dbFileName+".nsi").c_str()); // let's clean stuff up and remove the temp files
166 remove((dbFileName+".nsd").c_str()); // let's clean stuff up and remove the temp files
167 remove((dbFileName+".nin").c_str()); // let's clean stuff up and remove the temp files
168 remove((dbFileName+".nhr").c_str()); // let's clean stuff up and remove the temp files
169 remove(blastFileName.c_str()); // let's clean stuff up and remove the temp files
171 catch(exception& e) {
172 m->errorOut(e, "BlastDB", "~BlastDB");
176 /**************************************************************************************************/
177 //assumes you have added all the template sequences using the addSequence function and run generateDB.
178 vector<int> BlastDB::findClosestSequences(Sequence* seq, int n) {
180 vector<int> topMatches;
183 int randNumber = rand();
184 m->openOutputFile((queryFileName+toString(getpid())+toString(randNumber)), queryFile);
185 queryFile << '>' << seq->getName() << endl;
186 queryFile << seq->getUnaligned() << endl;
190 // the goal here is to quickly survey the database to find the closest match. To do this we are using the default
191 // wordsize used in megablast. I'm sure we're sacrificing accuracy for speed, but anyother way would take way too
192 // long. With this setting, it seems comparable in speed to the suffix tree approach.
195 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
197 blastCommand = path + "blast/bin/blastall -p blastn -d " + dbFileName + " -m 8 -W 28 -v " + toString(n) + " -b " + toString(n);
198 blastCommand += (" -i " + (queryFileName+toString(getpid())+toString(randNumber)) + " -o " + blastFileName+toString(getpid())+toString(randNumber));
200 blastCommand = "\"" + path + "blast\\bin\\blastall\" -p blastn -d " + "\"" + dbFileName + "\"" + " -m 8 -W 28 -v " + toString(n) + " -b " + toString(n);
201 blastCommand += (" -i " + (queryFileName+toString(getpid())+toString(randNumber)) + " -o " + blastFileName+toString(getpid())+toString(randNumber));
202 //wrap entire string in ""
203 blastCommand = "\"" + blastCommand + "\"";
206 system(blastCommand.c_str());
208 ifstream m8FileHandle;
209 m->openInputFile(blastFileName+toString(getpid())+toString(randNumber), m8FileHandle, "no error");
212 int templateAccession;
213 m->gobble(m8FileHandle);
215 while(!m8FileHandle.eof()){
216 m8FileHandle >> dummy >> templateAccession >> searchScore;
218 //get rest of junk in line
219 while (!m8FileHandle.eof()) { char c = m8FileHandle.get(); if (c == 10 || c == 13){ break; } }
221 m->gobble(m8FileHandle);
222 topMatches.push_back(templateAccession);
224 m8FileHandle.close();
225 remove((queryFileName+toString(getpid())+toString(randNumber)).c_str());
226 remove((blastFileName+toString(getpid())+toString(randNumber)).c_str());
230 catch(exception& e) {
231 m->errorOut(e, "BlastDB", "findClosestSequences");
236 /**************************************************************************************************/
237 //assumes you have added all the template sequences using the addSequence function and run generateDB.
238 vector<int> BlastDB::findClosestMegaBlast(Sequence* seq, int n, int minPerID) {
240 vector<int> topMatches;
241 float numBases, mismatch, gap, startQuery, endQuery, startRef, endRef, score;
245 int randNumber = rand();
246 //int randNumber = 12345;
247 m->openOutputFile((queryFileName+toString(getpid())+toString(randNumber)), queryFile);
248 queryFile << '>' << seq->getName() << endl;
249 queryFile << seq->getUnaligned() << endl;
251 // cout << seq->getUnaligned() << endl;
252 // the goal here is to quickly survey the database to find the closest match. To do this we are using the default
253 // wordsize used in megablast. I'm sure we're sacrificing accuracy for speed, but anyother way would take way too
254 // long. With this setting, it seems comparable in speed to the suffix tree approach.
255 //7000004128189528left 0 100 66 0 0 1 66 61 126 1e-31 131
257 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
258 blastCommand = path + "blast/bin/megablast -e 1e-10 -d " + dbFileName + " -m 8 -b " + toString(n) + " -v " + toString(n); //-W 28 -p blastn
259 blastCommand += (" -i " + (queryFileName+toString(getpid())+toString(randNumber)) + " -o " + blastFileName+toString(getpid())+toString(randNumber));
261 //blastCommand = path + "blast\\bin\\megablast -e 1e-10 -d " + dbFileName + " -m 8 -b " + toString(n) + " -v " + toString(n); //-W 28 -p blastn
262 //blastCommand += (" -i " + (queryFileName+toString(randNumber)) + " -o " + blastFileName+toString(randNumber));
264 blastCommand = "\"" + path + "blast\\bin\\megablast\" -e 1e-10 -d " + "\"" + dbFileName + "\"" + " -m 8 -b " + toString(n) + " -v " + toString(n); //-W 28 -p blastn
265 blastCommand += (" -i " + (queryFileName+toString(getpid())+toString(randNumber)) + " -o " + blastFileName+toString(getpid())+toString(randNumber));
266 //wrap entire string in ""
267 blastCommand = "\"" + blastCommand + "\"";
270 system(blastCommand.c_str());
272 ifstream m8FileHandle;
273 m->openInputFile(blastFileName+toString(getpid())+toString(randNumber), m8FileHandle, "no error");
275 string dummy, eScore;
276 int templateAccession;
277 m->gobble(m8FileHandle);
279 while(!m8FileHandle.eof()){
280 m8FileHandle >> dummy >> templateAccession >> searchScore >> numBases >> mismatch >> gap >> startQuery >> endQuery >> startRef >> endRef >> eScore >> score;
281 // 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;
283 //get rest of junk in line
284 //while (!m8FileHandle.eof()) { char c = m8FileHandle.get(); if (c == 10 || c == 13){ break; }else{ cout << c; } } //
286 m->gobble(m8FileHandle);
287 if (searchScore >= minPerID) {
288 topMatches.push_back(templateAccession);
289 Scores.push_back(searchScore);
291 //cout << templateAccession << endl;
293 m8FileHandle.close();
294 remove((queryFileName+toString(getpid())+toString(randNumber)).c_str());
295 remove((blastFileName+toString(getpid())+toString(randNumber)).c_str());
299 catch(exception& e) {
300 m->errorOut(e, "BlastDB", "findClosestMegaBlast");
304 /**************************************************************************************************/
305 void BlastDB::addSequence(Sequence seq) {
308 ofstream unalignedFastaFile;
309 m->openOutputFileAppend(dbFileName, unalignedFastaFile);
311 // generating a fasta file with unaligned template
312 unalignedFastaFile << '>' << count << endl; // sequences, which will be input to formatdb
313 unalignedFastaFile << seq.getUnaligned() << endl;
314 unalignedFastaFile.close();
318 catch(exception& e) {
319 m->errorOut(e, "BlastDB", "addSequence");
323 /**************************************************************************************************/
324 void BlastDB::generateDB() {
327 //m->mothurOut("Generating the temporary BLAST database...\t"); cout.flush();
330 string tempPath = path;
331 for (int i = 0; i < path.length(); i++) { tempPath[i] = tolower(path[i]); }
332 path = path.substr(0, (tempPath.find_last_of('m')));
334 string formatdbCommand;
336 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
337 formatdbCommand = path + "blast/bin/formatdb -p F -o T -i " + dbFileName; // format the database, -o option gives us the ability
339 //formatdbCommand = path + "blast\\bin\\formatdb -p F -o T -i " + dbFileName; // format the database, -o option gives us the ability
341 formatdbCommand = "\"" + path + "blast\\bin\\formatdb\" -p F -o T -i " + "\"" + dbFileName + "\"";
342 //wrap entire string in ""
343 formatdbCommand = "\"" + formatdbCommand + "\"";
345 //cout << formatdbCommand << endl;
346 system(formatdbCommand.c_str()); // to get the right sequence names, i think. -p F
347 // option tells formatdb that seqs are DNA, not prot
348 //m->mothurOut("DONE."); m->mothurOutEndLine(); m->mothurOutEndLine(); cout.flush();
350 catch(exception& e) {
351 m->errorOut(e, "BlastDB", "generateDB");
355 /**************************************************************************************************/
357 /**************************************************************************************************/