]> git.donarmstrong.com Git - mothur.git/blob - blastdb.cpp
changed blastdb.cpp to no include sequence names in blast input filenames. modified...
[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                 int randNumber = rand();
71                 m->openOutputFile((queryFileName+toString(randNumber)), queryFile);
72                 queryFile << '>' << seq->getName() << endl;
73                 queryFile << seq->getUnaligned() << endl;
74                 queryFile.close();
75
76                                 
77                 //      the goal here is to quickly survey the database to find the closest match.  To do this we are using the default
78                 //      wordsize used in megablast.  I'm sure we're sacrificing accuracy for speed, but anyother way would take way too
79                 //      long.  With this setting, it seems comparable in speed to the suffix tree approach.
80                 
81                 string blastCommand;
82                 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
83                 
84                         blastCommand = path + "blast/bin/blastall -p blastn -d " + dbFileName + " -m 8 -W 28 -v " + toString(n) + " -b " + toString(n);
85                         blastCommand += (" -i " + (queryFileName+toString(randNumber)) + " -o " + blastFileName+toString(randNumber));
86                 #else
87                         blastCommand =  "\"" + path + "blast\\bin\\blastall\" -p blastn -d " + "\"" + dbFileName + "\"" + " -m 8 -W 28 -v " + toString(n) + " -b " + toString(n);
88                         blastCommand += (" -i " + (queryFileName+toString(randNumber)) + " -o " + blastFileName+toString(randNumber));
89                         //wrap entire string in ""
90                         blastCommand = "\"" + blastCommand + "\"";
91                 #endif
92                 system(blastCommand.c_str());
93                 
94                 ifstream m8FileHandle;
95                 m->openInputFile(blastFileName+toString(randNumber), m8FileHandle, "no error");
96                 
97                 string dummy;
98                 int templateAccession;
99                 m->gobble(m8FileHandle);
100                 
101                 while(!m8FileHandle.eof()){
102                         m8FileHandle >> dummy >> templateAccession >> searchScore;
103                         
104                         //get rest of junk in line
105                         while (!m8FileHandle.eof())     {       char c = m8FileHandle.get(); if (c == 10 || c == 13){   break;  }       } 
106                         
107                         m->gobble(m8FileHandle);
108                         topMatches.push_back(templateAccession);
109                 }
110                 m8FileHandle.close();
111                 remove((queryFileName+toString(randNumber)).c_str());
112                 remove((blastFileName+toString(randNumber)).c_str());
113
114                 return topMatches;
115         }
116         catch(exception& e) {
117                 m->errorOut(e, "BlastDB", "findClosestSequences");
118                 exit(1);
119         }
120
121 }
122 /**************************************************************************************************/
123 //assumes you have added all the template sequences using the addSequence function and run generateDB.
124 vector<int> BlastDB::findClosestMegaBlast(Sequence* seq, int n, int minPerID) {
125         try{
126                 vector<int> topMatches;
127                 float numBases, mismatch, gap, startQuery, endQuery, startRef, endRef, score;
128                 Scores.clear();
129                 
130                 ofstream queryFile;
131                 int randNumber = rand();
132                 m->openOutputFile((queryFileName+toString(randNumber)), queryFile);
133                 queryFile << '>' << seq->getName() << endl;
134                 queryFile << seq->getUnaligned() << endl;
135                 queryFile.close();
136 //              cout << seq->getUnaligned() << endl;
137                 //      the goal here is to quickly survey the database to find the closest match.  To do this we are using the default
138                 //      wordsize used in megablast.  I'm sure we're sacrificing accuracy for speed, but anyother way would take way too
139                 //      long.  With this setting, it seems comparable in speed to the suffix tree approach.
140 //7000004128189528left  0       100             66      0       0       1       66      61      126     1e-31    131    
141                 string blastCommand;
142                 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
143                         blastCommand = path + "blast/bin/megablast -e 1e-10 -d " + dbFileName + " -m 8 -b " + toString(n) + " -v " + toString(n); //-W 28 -p blastn
144                         blastCommand += (" -i " + (queryFileName+toString(randNumber)) + " -o " + blastFileName+toString(randNumber));
145                 #else
146                         blastCommand =  "\"" + path + "blast\\bin\\megablast\" -e 1e-10 -d " + "\"" + dbFileName + "\"" + " -m 8 -b " + toString(n) + " -v " + toString(n); //-W 28 -p blastn
147                         blastCommand += (" -i " + (queryFileName+toString(randNumber)) + " -o " + blastFileName+toString(randNumber));
148                         //wrap entire string in ""
149                         blastCommand = "\"" + blastCommand + "\"";
150
151                 #endif
152                 
153                 system(blastCommand.c_str());
154
155                 ifstream m8FileHandle;
156                 m->openInputFile(blastFileName+toString(randNumber), m8FileHandle, "no error");
157         
158                 string dummy, eScore;
159                 int templateAccession;
160                 m->gobble(m8FileHandle);
161                 
162                 while(!m8FileHandle.eof()){
163                         m8FileHandle >> dummy >> templateAccession >> searchScore >> numBases >> mismatch >> gap >> startQuery >> endQuery >> startRef >> endRef >> eScore >> score;
164 //                      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                         
166                         //get rest of junk in line
167                         //while (!m8FileHandle.eof())   {       char c = m8FileHandle.get(); if (c == 10 || c == 13){   break;  }else{ cout << c; }     } //
168                                 //cout << endl;
169                         m->gobble(m8FileHandle);
170                         if (searchScore >= minPerID) { 
171                                 topMatches.push_back(templateAccession);
172                                 Scores.push_back(searchScore);
173                         }
174 //cout << templateAccession << endl;
175                 }
176                 m8FileHandle.close();
177                 remove((queryFileName+toString(randNumber)).c_str());
178                 remove((blastFileName+toString(randNumber)).c_str());
179 //cout << "\n" ;                
180                 return topMatches;
181         }
182         catch(exception& e) {
183                 m->errorOut(e, "BlastDB", "findClosestMegaBlast");
184                 exit(1);
185         }
186 }
187 /**************************************************************************************************/
188 void BlastDB::addSequence(Sequence seq) {
189         try {
190         
191                 ofstream unalignedFastaFile;
192                 m->openOutputFileAppend(dbFileName, unalignedFastaFile);                                
193         
194                 //      generating a fasta file with unaligned template
195                 unalignedFastaFile << '>' << count << endl;                                     //      sequences, which will be input to formatdb
196                 unalignedFastaFile << seq.getUnaligned() << endl;
197                 unalignedFastaFile.close();
198         
199                 count++;
200         }
201         catch(exception& e) {
202                 m->errorOut(e, "BlastDB", "addSequence");
203                 exit(1);
204         }
205 }
206 /**************************************************************************************************/
207 void BlastDB::generateDB() {
208         try {
209         
210                 //m->mothurOut("Generating the temporary BLAST database...\t"); cout.flush();
211                 
212                 path = m->argv;
213                 string tempPath = path;
214                 for (int i = 0; i < path.length(); i++) { tempPath[i] = tolower(path[i]); }
215                 path = path.substr(0, (tempPath.find_last_of('m')));
216         
217                 string formatdbCommand;
218                 
219                 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
220                         formatdbCommand = path + "blast/bin/formatdb -p F -o T -i " + dbFileName;       //      format the database, -o option gives us the ability
221                 #else
222                         formatdbCommand = "\"" + path + "blast\\bin\\formatdb\" -p F -o T -i " + "\"" +  dbFileName + "\"";
223                         //wrap entire string in ""
224                         formatdbCommand = "\"" + formatdbCommand + "\"";
225                 #endif
226                 system(formatdbCommand.c_str());                                                                //      to get the right sequence names, i think. -p F
227                                                                                                                                         //      option tells formatdb that seqs are DNA, not prot
228                 //m->mothurOut("DONE."); m->mothurOutEndLine(); m->mothurOutEndLine(); cout.flush();
229         }
230         catch(exception& e) {
231                 m->errorOut(e, "BlastDB", "generateDB");
232                 exit(1);
233         }
234 }
235 /**************************************************************************************************/
236
237 /**************************************************************************************************/
238