]> git.donarmstrong.com Git - mothur.git/blob - blastdb.cpp
d227bc6a88cc8553181065680c7dc56fd1707a21
[mothur.git] / blastdb.cpp
1 /*
2  *  blastdb.cpp
3  *  
4  *
5  *  Created by Pat Schloss on 12/22/08.
6  *  Copyright 2008 Patrick D. Schloss. All rights reserved.
7  *
8  */
9
10
11 #include "database.hpp"
12 #include "sequence.hpp"
13 #include "blastdb.hpp"
14
15 /**************************************************************************************************/
16
17 BlastDB::BlastDB(string tag, float gO, float gE, float m, float mM) : Database(), 
18 gapOpen(gO), gapExtend(gE), match(m), misMatch(mM) {
19         
20         count = 0;
21
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";
26
27 }
28 /**************************************************************************************************/
29
30 BlastDB::BlastDB() : Database() {
31         try {
32                 count = 0;
33
34                 int randNumber = rand();
35                 dbFileName = toString(randNumber) + ".template.unaligned.fasta";
36                 queryFileName = toString(randNumber) + ".candidate.unaligned.fasta";
37                 blastFileName = toString(randNumber) + ".blast";
38         }
39         catch(exception& e) {
40                 m->errorOut(e, "BlastDB", "BlastDB");
41                 exit(1);
42         }
43 }
44
45 /**************************************************************************************************/
46
47 BlastDB::~BlastDB(){
48         try{
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
57         }
58         catch(exception& e) {
59                 m->errorOut(e, "BlastDB", "~BlastDB");
60                 exit(1);
61         }
62 }
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) {
66         try{
67                 vector<int> topMatches;
68                 
69                 ofstream queryFile;
70                 m->openOutputFile((queryFileName+seq->getName()), queryFile);
71                 queryFile << '>' << seq->getName() << endl;
72                 queryFile << seq->getUnaligned() << endl;
73                 queryFile.close();
74
75                                 
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.
79                 
80                 string blastCommand = path + "blast/bin/blastall -p blastn -d " + dbFileName + " -m 8 -W 28 -v " + toString(n) + " -b " + toString(n);;
81                 blastCommand += (" -i " + (queryFileName+seq->getName()) + " -o " + blastFileName+seq->getName());
82                 system(blastCommand.c_str());
83                 
84                 ifstream m8FileHandle;
85                 m->openInputFile(blastFileName+seq->getName(), m8FileHandle, "no error");
86                 
87                 string dummy;
88                 int templateAccession;
89                 m->gobble(m8FileHandle);
90                 
91                 while(!m8FileHandle.eof()){
92                         m8FileHandle >> dummy >> templateAccession >> searchScore;
93                         
94                         //get rest of junk in line
95                         while (!m8FileHandle.eof())     {       char c = m8FileHandle.get(); if (c == 10 || c == 13){   break;  }       } 
96                         
97                         m->gobble(m8FileHandle);
98                         topMatches.push_back(templateAccession);
99                 }
100                 m8FileHandle.close();
101                 remove((queryFileName+seq->getName()).c_str());
102                 remove((blastFileName+seq->getName()).c_str());
103
104                 return topMatches;
105         }
106         catch(exception& e) {
107                 m->errorOut(e, "BlastDB", "findClosestSequences");
108                 exit(1);
109         }
110
111 }
112 /**************************************************************************************************/
113 //assumes you have added all the template sequences using the addSequence function and run generateDB.
114 vector<int> BlastDB::findClosestMegaBlast(Sequence* seq, int n, int minPerID) {
115         try{
116                 vector<int> topMatches;
117                 float numBases, mismatch, gap, startQuery, endQuery, startRef, endRef, score;
118                 Scores.clear();
119                 
120                 ofstream queryFile;
121
122                 m->openOutputFile((queryFileName+seq->getName()), queryFile);
123                 queryFile << '>' << seq->getName() << endl;
124                 queryFile << seq->getUnaligned() << endl;
125                 queryFile.close();
126 //              cout << seq->getUnaligned() << endl;
127                 //      the goal here is to quickly survey the database to find the closest match.  To do this we are using the default
128                 //      wordsize used in megablast.  I'm sure we're sacrificing accuracy for speed, but anyother way would take way too
129                 //      long.  With this setting, it seems comparable in speed to the suffix tree approach.
130 //7000004128189528left  0       100             66      0       0       1       66      61      126     1e-31    131    
131                 
132                 string blastCommand = path + "blast/bin/megablast -e 1e-10 -d " + dbFileName + " -m 8 -b " + toString(n) + " -v " + toString(n); //-W 28 -p blastn
133                 blastCommand += (" -i " + (queryFileName+seq->getName()) + " -o " + blastFileName+seq->getName());
134                 system(blastCommand.c_str());
135
136                 ifstream m8FileHandle;
137                 m->openInputFile(blastFileName+seq->getName(), m8FileHandle, "no error");
138         
139                 string dummy, eScore;
140                 int templateAccession;
141                 m->gobble(m8FileHandle);
142                 
143                 while(!m8FileHandle.eof()){
144                         m8FileHandle >> dummy >> templateAccession >> searchScore >> numBases >> mismatch >> gap >> startQuery >> endQuery >> startRef >> endRef >> eScore >> score;
145 //                      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; 
146                         
147                         //get rest of junk in line
148                         //while (!m8FileHandle.eof())   {       char c = m8FileHandle.get(); if (c == 10 || c == 13){   break;  }else{ cout << c; }     } //
149                                 //cout << endl;
150                         m->gobble(m8FileHandle);
151                         if (searchScore >= minPerID) { 
152                                 topMatches.push_back(templateAccession);
153                                 Scores.push_back(searchScore);
154                         }
155 //cout << templateAccession << endl;
156                 }
157                 m8FileHandle.close();
158                 remove((queryFileName+seq->getName()).c_str());
159                 remove((blastFileName+seq->getName()).c_str());
160 //cout << "\n" ;                
161                 return topMatches;
162         }
163         catch(exception& e) {
164                 m->errorOut(e, "BlastDB", "findClosestMegaBlast");
165                 exit(1);
166         }
167 }
168 /**************************************************************************************************/
169 void BlastDB::addSequence(Sequence seq) {
170         try {
171         
172                 ofstream unalignedFastaFile;
173                 m->openOutputFileAppend(dbFileName, unalignedFastaFile);                                
174         
175                 //      generating a fasta file with unaligned template
176                 unalignedFastaFile << '>' << count << endl;                                     //      sequences, which will be input to formatdb
177                 unalignedFastaFile << seq.getUnaligned() << endl;
178                 unalignedFastaFile.close();
179         
180                 count++;
181         }
182         catch(exception& e) {
183                 m->errorOut(e, "BlastDB", "addSequence");
184                 exit(1);
185         }
186 }
187 /**************************************************************************************************/
188 void BlastDB::generateDB() {
189         try {
190         
191                 //m->mothurOut("Generating the temporary BLAST database...\t"); cout.flush();
192                 
193                 path = m->argv;
194                 string tempPath = path;
195                 for (int i = 0; i < path.length(); i++) { tempPath[i] = tolower(path[i]); }
196                 path = path.substr(0, (tempPath.find_last_of('m')));
197         
198                 string formatdbCommand = path + "blast/bin/formatdb -p F -o T -i " + dbFileName;        //      format the database, -o option gives us the ability
199                 system(formatdbCommand.c_str());                                                                //      to get the right sequence names, i think. -p F
200                                                                                                                                         //      option tells formatdb that seqs are DNA, not prot
201                 //m->mothurOut("DONE."); m->mothurOutEndLine(); m->mothurOutEndLine(); cout.flush();
202         }
203         catch(exception& e) {
204                 m->errorOut(e, "BlastDB", "generateDB");
205                 exit(1);
206         }
207 }
208 /**************************************************************************************************/
209
210 /**************************************************************************************************/
211