]> git.donarmstrong.com Git - mothur.git/blob - blastdb.cpp
760824dbb23f9b2a9d680ace769ccc1c6ffab96c
[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(float gO, float gE, float m, float mM) : Database(), 
18 gapOpen(gO), gapExtend(gE), match(m), misMatch(mM) {
19         
20         globaldata = GlobalData::getInstance();
21         count = 0;
22
23         int randNumber = rand();
24         dbFileName = toString(randNumber) + ".template.unaligned.fasta";
25         queryFileName = toString(randNumber) + ".candidate.unaligned.fasta";
26         blastFileName = toString(randNumber) + ".blast";
27
28 }
29 /**************************************************************************************************/
30
31 BlastDB::BlastDB() : Database() {
32         
33         globaldata = GlobalData::getInstance();
34         count = 0;
35
36         int randNumber = rand();
37         dbFileName = toString(randNumber) + ".template.unaligned.fasta";
38         queryFileName = toString(randNumber) + ".candidate.unaligned.fasta";
39         blastFileName = toString(randNumber) + ".blast";
40
41 }
42
43 /**************************************************************************************************/
44
45 BlastDB::~BlastDB(){
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
49 }
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) {
53         try{
54                 vector<int> topMatches;
55                 
56                 ofstream queryFile;
57                 openOutputFile((queryFileName+seq->getName()), queryFile);
58                 queryFile << '>' << seq->getName() << endl;
59                 queryFile << seq->getUnaligned() << endl;
60                 queryFile.close();
61
62                                 
63                 //      the goal here is to quickly survey the database to find the closest match.  To do this we are using the default
64                 //      wordsize used in megablast.  I'm sure we're sacrificing accuracy for speed, but anyother way would take way too
65                 //      long.  With this setting, it seems comparable in speed to the suffix tree approach.
66                 
67                 string blastCommand = path + "blast/bin/blastall -p blastn -d " + dbFileName + " -m 8 -W 28 -v " + toString(n) + " -b " + toString(n);;
68                 blastCommand += (" -i " + (queryFileName+seq->getName()) + " -o " + blastFileName+seq->getName());
69                 system(blastCommand.c_str());
70                 
71                 ifstream m8FileHandle;
72                 openInputFile(blastFileName+seq->getName(), m8FileHandle, "no error");
73                 
74                 string dummy;
75                 int templateAccession;
76                 gobble(m8FileHandle);
77                 
78                 while(!m8FileHandle.eof()){
79                         m8FileHandle >> dummy >> templateAccession >> searchScore;
80                         
81                         //get rest of junk in line
82                         while (!m8FileHandle.eof())     {       char c = m8FileHandle.get(); if (c == 10 || c == 13){   break;  }       } 
83                         
84                         gobble(m8FileHandle);
85                         topMatches.push_back(templateAccession);
86                 }
87                 m8FileHandle.close();
88                 remove((queryFileName+seq->getName()).c_str());
89                 remove((blastFileName+seq->getName()).c_str());
90
91                 return topMatches;
92         }
93         catch(exception& e) {
94                 m->errorOut(e, "BlastDB", "findClosestSequences");
95                 exit(1);
96         }
97
98 }
99 /**************************************************************************************************/
100 //assumes you have added all the template sequences using the addSequence function and run generateDB.
101 vector<int> BlastDB::findClosestMegaBlast(Sequence* seq, int n) {
102         try{
103                 vector<int> topMatches;
104                 
105                 ofstream queryFile;
106                 openOutputFile((queryFileName+seq->getName()), queryFile);
107                 queryFile << '>' << seq->getName() << endl;
108                 queryFile << seq->getUnaligned() << endl;
109                 queryFile.close();
110                                 
111                 //      the goal here is to quickly survey the database to find the closest match.  To do this we are using the default
112                 //      wordsize used in megablast.  I'm sure we're sacrificing accuracy for speed, but anyother way would take way too
113                 //      long.  With this setting, it seems comparable in speed to the suffix tree approach.
114         
115                 string blastCommand = path + "blast/bin/megablast -e 1e-10 -d " + dbFileName + " -m 8 -b " + toString(n) + " -v " + toString(n); //-W 28 -p blastn
116                 blastCommand += (" -i " + (queryFileName+seq->getName()) + " -o " + blastFileName+seq->getName());
117                 system(blastCommand.c_str());
118
119                 ifstream m8FileHandle;
120                 openInputFile(blastFileName+seq->getName(), m8FileHandle, "no error");
121         
122                 string dummy;
123                 int templateAccession;
124                 gobble(m8FileHandle);
125                 
126                 while(!m8FileHandle.eof()){
127                         m8FileHandle >> dummy >> templateAccession >> searchScore;
128                         
129                         //get rest of junk in line
130                         while (!m8FileHandle.eof())     {       char c = m8FileHandle.get(); if (c == 10 || c == 13){   break;  }       } 
131                         
132                         gobble(m8FileHandle);
133                         topMatches.push_back(templateAccession);
134 //cout << templateAccession << endl;
135                 }
136                 m8FileHandle.close();
137                 remove((queryFileName+seq->getName()).c_str());
138                 remove((blastFileName+seq->getName()).c_str());
139 //cout << "\n\n" ;              
140                 return topMatches;
141         }
142         catch(exception& e) {
143                 m->errorOut(e, "BlastDB", "findClosest");
144                 exit(1);
145         }
146 }
147 /**************************************************************************************************/
148 void BlastDB::addSequence(Sequence seq) {
149         try {
150         
151                 ofstream unalignedFastaFile;
152                 openOutputFileAppend(dbFileName, unalignedFastaFile);                           
153         
154                 //      generating a fasta file with unaligned template
155                 unalignedFastaFile << '>' << count << endl;                                     //      sequences, which will be input to formatdb
156                 unalignedFastaFile << seq.getUnaligned() << endl;
157                 unalignedFastaFile.close();
158         
159                 count++;
160         }
161         catch(exception& e) {
162                 m->errorOut(e, "BlastDB", "addSequence");
163                 exit(1);
164         }
165 }
166 /**************************************************************************************************/
167 void BlastDB::generateDB() {
168         try {
169         
170                 //m->mothurOut("Generating the temporary BLAST database...\t"); cout.flush();
171                 
172                 path = globaldata->argv;
173                 path = path.substr(0, (path.find_last_of('m')));
174         
175                 string formatdbCommand = path + "blast/bin/formatdb -p F -o T -i " + dbFileName;        //      format the database, -o option gives us the ability
176                 system(formatdbCommand.c_str());                                                                //      to get the right sequence names, i think. -p F
177                                                                                                                                         //      option tells formatdb that seqs are DNA, not prot
178                 //m->mothurOut("DONE."); m->mothurOutEndLine(); m->mothurOutEndLine(); cout.flush();
179         }
180         catch(exception& e) {
181                 m->errorOut(e, "BlastDB", "generateDB");
182                 exit(1);
183         }
184 }
185 /**************************************************************************************************/
186
187 /**************************************************************************************************/
188