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