Package obitools :: Package blast
[hide private]
[frames] | no frames]

Source Code for Package obitools.blast

  1  from os import popen2 
  2  from itertools import imap,count 
  3   
  4  from obitools.table import iTableIterator,TableRow,Table,SelectionIterator 
  5  from obitools.utils import ColumnFile 
  6  from obitools.location import SimpleLocation 
  7  from obitools.fasta import formatFasta 
  8  import sys 
  9   
10 -class Blast(object):
11 ''' 12 Run blast 13 ''' 14
15 - def __init__(self,mode,db,program='blastall',**options):
16 self._mode = mode 17 self._db = db 18 self._program = program 19 self._options = options
20
21 - def getMode(self):
22 return self._mode
23 24
25 - def getDb(self):
26 return self._db
27 28
29 - def getProgram(self):
30 return self._program
31
32 - def _blastcmd(self):
33 tmp = """%(program)s \\ 34 -p %(mode)s \\ 35 -d %(db)s \\ 36 -m 8 \\ 37 %(options)s \\ 38 """ 39 options = ' '.join(['-%s %s' % (x[0],str(x[1])) 40 for x in self._options.iteritems()]) 41 data = { 42 'program' : self.program, 43 'db' : self.db, 44 'mode' : self.mode, 45 'options' : options 46 } 47 48 return tmp % data
49
50 - def __call__(self,sequence):
51 ''' 52 Run blast with one sequence object 53 @param sequence: 54 @type sequence: 55 ''' 56 cmd = self._blastcmd() 57 58 (blast_in,blast_out) = popen2(cmd) 59 60 print >>blast_in,formatFasta(sequence) 61 blast_in.close() 62 63 blast = BlastResultIterator(blast_out) 64 65 return blast
66 67 mode = property(getMode, None, None, "Mode's Docstring") 68 69 db = property(getDb, None, None, "Db's Docstring") 70 71 program = property(getProgram, None, None, "Program's Docstring")
72 73
74 -class NetBlast(Blast):
75 ''' 76 Run blast on ncbi servers 77 ''' 78
79 - def __init__(self,mode,db,**options):
80 ''' 81 82 @param mode: 83 @param db: 84 ''' 85 Blast.__init__(self, mode, db, 'blastcl3',**options)
86 87
88 -class BlastResultIterator(iTableIterator):
89
90 - def __init__(self,blastoutput,query=None):
91 ''' 92 93 @param blastoutput: 94 @type blastoutput: 95 ''' 96 self._blast = ColumnFile(blastoutput, 97 strip=True, 98 skip="#", 99 sep="\t", 100 types=self.types 101 ) 102 self._query = query 103 self._hindex = dict((k,i) for i,k in imap(None,count(),self._getHeaders()))
104
105 - def _getHeaders(self):
106 return ('Query id','Subject id', 107 '% identity','alignment length', 108 'mismatches', 'gap openings', 109 'q. start', 'q. end', 110 's. start', 's. end', 111 'e-value', 'bit score')
112
113 - def _getTypes(self):
114 return (str,str, 115 float,int, 116 int,int, 117 int,int, 118 int,int, 119 float,float)
120
121 - def _getRowFactory(self):
122 return BlastMatch
123
124 - def _getSubrowFactory(self):
125 return TableRow
126
127 - def _getQuery(self):
128 return self._query
129 130 131 headers = property(_getHeaders,None,None) 132 types = property(_getTypes,None,None) 133 rowFactory = property(_getRowFactory,None,None) 134 subrowFactory = property(_getSubrowFactory,None,None) 135 query = property(_getQuery,None,None) 136
137 - def next(self):
138 ''' 139 140 ''' 141 value = self._blast.next() 142 return self.rowFactory(self,value)
143 144 145
146 -class BlastResult(Table):
147 ''' 148 Results of a blast run 149 '''
150
151 -class BlastMatch(TableRow):
152 ''' 153 Blast high scoring pair between two sequences 154 ''' 155
156 - def getQueryLocation(self):
157 l = SimpleLocation(self[6], self[7]) 158 return l
159
160 - def getSubjectLocation(self):
161 l = SimpleLocation(self[8], self[9]) 162 return l
163
164 - def getSubjectSequence(self,database):
165 return database[self[1]]
166
167 - def queryCov(self,query=None):
168 ''' 169 Compute coverage of match on query sequence. 170 171 @param query: the query sequence. Default is None. 172 In this case the query sequence associated 173 to this blast result is used. 174 @type query: L{obitools.BioSequence} 175 176 @return: coverage fraction 177 @rtype: float 178 ''' 179 if query is None: 180 query = self.table.query 181 assert query is not None 182 return float(self[7]-self[6]+1)/float(len(query))
183
184 - def __getitem__(self,key):
185 if key=='query coverage' and self.table.query is not None: 186 return self.queryCov() 187 else: 188 return TableRow.__getitem__(self,key)
189
190 -class BlastCovMinFilter(SelectionIterator):
191
192 - def __init__(self,blastiterator,covmin,query=None,**conditions):
193 if query is None: 194 query = blastiterator.table.query 195 assert query is not None 196 SelectionIterator.__init__(self,blastiterator,**conditions) 197 self._query = query 198 self._covmin=covmin
199
200 - def _covMinPredicat(self,row):
201 return row.queryCov(self._query)>=self._covmin
202
203 - def _checkCondition(self,row):
204 return self._covMinPredicat(row) and SelectionIterator._checkCondition(self, row)
205