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 //int randNumber = 12345;
24 dbFileName = tag + toString(randNumber) + ".template.unaligned.fasta";
25 queryFileName = tag + toString(randNumber) + ".candidate.unaligned.fasta";
26 blastFileName = tag + toString(randNumber) + ".blast";
29 /**************************************************************************************************/
31 BlastDB::BlastDB() : Database() {
35 int randNumber = rand();
36 //int randNumber = 12345;
37 dbFileName = toString(randNumber) + ".template.unaligned.fasta";
38 queryFileName = toString(randNumber) + ".candidate.unaligned.fasta";
39 blastFileName = toString(randNumber) + ".blast";
42 m->errorOut(e, "BlastDB", "BlastDB");
47 /**************************************************************************************************/
51 remove(queryFileName.c_str()); // let's clean stuff up and remove the temp files
52 remove(dbFileName.c_str()); // let's clean stuff up and remove the temp files
53 remove((dbFileName+".nsq").c_str()); // let's clean stuff up and remove the temp files
54 remove((dbFileName+".nsi").c_str()); // let's clean stuff up and remove the temp files
55 remove((dbFileName+".nsd").c_str()); // let's clean stuff up and remove the temp files
56 remove((dbFileName+".nin").c_str()); // let's clean stuff up and remove the temp files
57 remove((dbFileName+".nhr").c_str()); // let's clean stuff up and remove the temp files
58 remove(blastFileName.c_str()); // let's clean stuff up and remove the temp files
61 m->errorOut(e, "BlastDB", "~BlastDB");
65 /**************************************************************************************************/
66 //assumes you have added all the template sequences using the addSequence function and run generateDB.
67 vector<int> BlastDB::findClosestSequences(Sequence* seq, int n) {
69 vector<int> topMatches;
72 int randNumber = rand();
73 m->openOutputFile((queryFileName+toString(randNumber)), queryFile);
74 queryFile << '>' << seq->getName() << endl;
75 queryFile << seq->getUnaligned() << endl;
79 // the goal here is to quickly survey the database to find the closest match. To do this we are using the default
80 // wordsize used in megablast. I'm sure we're sacrificing accuracy for speed, but anyother way would take way too
81 // long. With this setting, it seems comparable in speed to the suffix tree approach.
84 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
86 blastCommand = path + "blast/bin/blastall -p blastn -d " + dbFileName + " -m 8 -W 28 -v " + toString(n) + " -b " + toString(n);
87 blastCommand += (" -i " + (queryFileName+toString(randNumber)) + " -o " + blastFileName+toString(randNumber));
89 blastCommand = "\"" + path + "blast\\bin\\blastall\" -p blastn -d " + "\"" + dbFileName + "\"" + " -m 8 -W 28 -v " + toString(n) + " -b " + toString(n);
90 blastCommand += (" -i " + (queryFileName+toString(randNumber)) + " -o " + blastFileName+toString(randNumber));
91 //wrap entire string in ""
92 blastCommand = "\"" + blastCommand + "\"";
95 system(blastCommand.c_str());
97 ifstream m8FileHandle;
98 m->openInputFile(blastFileName+toString(randNumber), m8FileHandle, "no error");
101 int templateAccession;
102 m->gobble(m8FileHandle);
104 while(!m8FileHandle.eof()){
105 m8FileHandle >> dummy >> templateAccession >> searchScore;
107 //get rest of junk in line
108 while (!m8FileHandle.eof()) { char c = m8FileHandle.get(); if (c == 10 || c == 13){ break; } }
110 m->gobble(m8FileHandle);
111 topMatches.push_back(templateAccession);
113 m8FileHandle.close();
114 remove((queryFileName+toString(randNumber)).c_str());
115 remove((blastFileName+toString(randNumber)).c_str());
119 catch(exception& e) {
120 m->errorOut(e, "BlastDB", "findClosestSequences");
125 /**************************************************************************************************/
126 //assumes you have added all the template sequences using the addSequence function and run generateDB.
127 vector<int> BlastDB::findClosestMegaBlast(Sequence* seq, int n, int minPerID) {
129 vector<int> topMatches;
130 float numBases, mismatch, gap, startQuery, endQuery, startRef, endRef, score;
134 int randNumber = rand();
135 //int randNumber = 12345;
136 m->openOutputFile((queryFileName+toString(randNumber)), queryFile);
137 queryFile << '>' << seq->getName() << endl;
138 queryFile << seq->getUnaligned() << endl;
140 // cout << seq->getUnaligned() << endl;
141 // the goal here is to quickly survey the database to find the closest match. To do this we are using the default
142 // wordsize used in megablast. I'm sure we're sacrificing accuracy for speed, but anyother way would take way too
143 // long. With this setting, it seems comparable in speed to the suffix tree approach.
144 //7000004128189528left 0 100 66 0 0 1 66 61 126 1e-31 131
146 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
147 blastCommand = path + "blast/bin/megablast -e 1e-10 -d " + dbFileName + " -m 8 -b " + toString(n) + " -v " + toString(n); //-W 28 -p blastn
148 blastCommand += (" -i " + (queryFileName+toString(randNumber)) + " -o " + blastFileName+toString(randNumber));
150 //blastCommand = path + "blast\\bin\\megablast -e 1e-10 -d " + dbFileName + " -m 8 -b " + toString(n) + " -v " + toString(n); //-W 28 -p blastn
151 //blastCommand += (" -i " + (queryFileName+toString(randNumber)) + " -o " + blastFileName+toString(randNumber));
153 blastCommand = "\"" + path + "blast\\bin\\megablast\" -e 1e-10 -d " + "\"" + dbFileName + "\"" + " -m 8 -b " + toString(n) + " -v " + toString(n); //-W 28 -p blastn
154 blastCommand += (" -i " + (queryFileName+toString(randNumber)) + " -o " + blastFileName+toString(randNumber));
155 //wrap entire string in ""
156 blastCommand = "\"" + blastCommand + "\"";
159 //cout << blastCommand << endl;
160 system(blastCommand.c_str());
162 ifstream m8FileHandle;
163 m->openInputFile(blastFileName+toString(randNumber), m8FileHandle, "no error");
165 string dummy, eScore;
166 int templateAccession;
167 m->gobble(m8FileHandle);
169 while(!m8FileHandle.eof()){
170 m8FileHandle >> dummy >> templateAccession >> searchScore >> numBases >> mismatch >> gap >> startQuery >> endQuery >> startRef >> endRef >> eScore >> score;
171 // 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;
173 //get rest of junk in line
174 //while (!m8FileHandle.eof()) { char c = m8FileHandle.get(); if (c == 10 || c == 13){ break; }else{ cout << c; } } //
176 m->gobble(m8FileHandle);
177 if (searchScore >= minPerID) {
178 topMatches.push_back(templateAccession);
179 Scores.push_back(searchScore);
181 //cout << templateAccession << endl;
183 m8FileHandle.close();
184 remove((queryFileName+toString(randNumber)).c_str());
185 remove((blastFileName+toString(randNumber)).c_str());
189 catch(exception& e) {
190 m->errorOut(e, "BlastDB", "findClosestMegaBlast");
194 /**************************************************************************************************/
195 void BlastDB::addSequence(Sequence seq) {
198 ofstream unalignedFastaFile;
199 m->openOutputFileAppend(dbFileName, unalignedFastaFile);
201 // generating a fasta file with unaligned template
202 unalignedFastaFile << '>' << count << endl; // sequences, which will be input to formatdb
203 unalignedFastaFile << seq.getUnaligned() << endl;
204 unalignedFastaFile.close();
208 catch(exception& e) {
209 m->errorOut(e, "BlastDB", "addSequence");
213 /**************************************************************************************************/
214 void BlastDB::generateDB() {
217 //m->mothurOut("Generating the temporary BLAST database...\t"); cout.flush();
220 string tempPath = path;
221 for (int i = 0; i < path.length(); i++) { tempPath[i] = tolower(path[i]); }
222 path = path.substr(0, (tempPath.find_last_of('m')));
224 string formatdbCommand;
226 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
227 formatdbCommand = path + "blast/bin/formatdb -p F -o T -i " + dbFileName; // format the database, -o option gives us the ability
229 //formatdbCommand = path + "blast\\bin\\formatdb -p F -o T -i " + dbFileName; // format the database, -o option gives us the ability
231 formatdbCommand = "\"" + path + "blast\\bin\\formatdb\" -p F -o T -i " + "\"" + dbFileName + "\"";
232 //wrap entire string in ""
233 formatdbCommand = "\"" + formatdbCommand + "\"";
235 //cout << formatdbCommand << endl;
236 system(formatdbCommand.c_str()); // to get the right sequence names, i think. -p F
237 // option tells formatdb that seqs are DNA, not prot
238 //m->mothurOut("DONE."); m->mothurOutEndLine(); m->mothurOutEndLine(); cout.flush();
240 catch(exception& e) {
241 m->errorOut(e, "BlastDB", "generateDB");
245 /**************************************************************************************************/
247 /**************************************************************************************************/