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);
88 string root = dbFileName;
89 string temp = dbFileName + ".nsq";
91 temp = dbFileName + ".nsi";
94 temp = dbFileName + ".nsd";
97 temp = dbFileName + ".nin";
100 temp = dbFileName + ".nhr";
101 remove(temp.c_str());
107 catch(exception& e) {
108 m->errorOut(e, "BlastDB", "findClosestSequences");
113 /**************************************************************************************************/
114 //assumes you have added all the template sequences using the addSequence function and run generateDB.
115 vector<int> BlastDB::findClosestMegaBlast(Sequence* seq, int n) {
117 vector<int> topMatches;
120 openOutputFile(queryFileName, queryFile);
121 queryFile << '>' << seq->getName() << endl;
122 queryFile << seq->getUnaligned() << endl;
125 // the goal here is to quickly survey the database to find the closest match. To do this we are using the default
126 // wordsize used in megablast. I'm sure we're sacrificing accuracy for speed, but anyother way would take way too
127 // long. With this setting, it seems comparable in speed to the suffix tree approach.
129 string blastCommand = path + "blast/bin/megablast -e 1e-10 -d " + dbFileName + " -m 8 -b " + toString(n) + " -v " + toString(n); //-W 28 -p blastn
130 blastCommand += (" -i " + queryFileName + " -o " + blastFileName);
131 system(blastCommand.c_str());
133 ifstream m8FileHandle;
134 openInputFile(blastFileName, m8FileHandle, "no error");
137 int templateAccession;
138 gobble(m8FileHandle);
140 while(!m8FileHandle.eof()){
141 m8FileHandle >> dummy >> templateAccession >> searchScore;
143 //get rest of junk in line
144 while (!m8FileHandle.eof()) { char c = m8FileHandle.get(); if (c == 10 || c == 13){ break; } }
146 gobble(m8FileHandle);
147 topMatches.push_back(templateAccession);
148 //cout << templateAccession << endl;
150 m8FileHandle.close();
154 catch(exception& e) {
155 m->errorOut(e, "BlastDB", "findClosest");
159 /**************************************************************************************************/
160 void BlastDB::addSequence(Sequence seq) {
163 ofstream unalignedFastaFile;
164 openOutputFileAppend(dbFileName, unalignedFastaFile);
166 // generating a fasta file with unaligned template
167 unalignedFastaFile << '>' << count << endl; // sequences, which will be input to formatdb
168 unalignedFastaFile << seq.getUnaligned() << endl;
169 unalignedFastaFile.close();
173 catch(exception& e) {
174 m->errorOut(e, "BlastDB", "addSequence");
178 /**************************************************************************************************/
179 void BlastDB::generateDB() {
182 //m->mothurOut("Generating the temporary BLAST database...\t"); cout.flush();
184 path = globaldata->argv;
185 path = path.substr(0, (path.find_last_of('m')));
187 string formatdbCommand = path + "blast/bin/formatdb -p F -o T -i " + dbFileName; // format the database, -o option gives us the ability
188 system(formatdbCommand.c_str()); // to get the right sequence names, i think. -p F
189 // option tells formatdb that seqs are DNA, not prot
190 //m->mothurOut("DONE."); m->mothurOutEndLine(); m->mothurOutEndLine(); cout.flush();
192 catch(exception& e) {
193 m->errorOut(e, "BlastDB", "generateDB");
198 /**************************************************************************************************/
199 int BlastDB::MPISend(int receiver) {
202 //send gapOpen - float
203 MPI_Send(&gapOpen, 1, MPI_FLOAT, receiver, 2001, MPI_COMM_WORLD);
205 //send gapExtend - float
206 MPI_Send(&gapExtend, 1, MPI_FLOAT, receiver, 2001, MPI_COMM_WORLD);
209 MPI_Send(&match, 1, MPI_FLOAT, receiver, 2001, MPI_COMM_WORLD);
211 //send mismatch - float
212 MPI_Send(&misMatch, 1, MPI_FLOAT, receiver, 2001, MPI_COMM_WORLD);
216 catch(exception& e) {
217 m->errorOut(e, "BlastDB", "MPISend");
221 /**************************************************************************************************/
222 int BlastDB::MPIRecv(int sender) {
226 //receive gapOpen - float
227 MPI_Recv(&gapOpen, 1, MPI_FLOAT, sender, 2001, MPI_COMM_WORLD, &status);
229 //receive gapExtend - float
230 MPI_Recv(&gapExtend, 1, MPI_FLOAT, sender, 2001, MPI_COMM_WORLD, &status);
232 //receive match - float
233 MPI_Recv(&match, 1, MPI_FLOAT, sender, 2001, MPI_COMM_WORLD, &status);
235 //receive mismatch - float
236 MPI_Recv(&misMatch, 1, MPI_FLOAT, sender, 2001, MPI_COMM_WORLD, &status);
240 catch(exception& e) {
241 m->errorOut(e, "BlastDB", "MPIRecv");
246 /**************************************************************************************************/
248 /**************************************************************************************************/