Package Bio :: Package SearchIO :: Package BlastIO
[hide private]
[frames] | no frames]

Source Code for Package Bio.SearchIO.BlastIO

  1  # Copyright 2012 by Wibowo Arindrarto.  All rights reserved. 
  2  # This code is part of the Biopython distribution and governed by its 
  3  # license.  Please see the LICENSE file that should have been included 
  4  # as part of this package. 
  5   
  6  """Bio.SearchIO support for BLAST+ output formats. 
  7   
  8  This module adds support for parsing BLAST+ outputs. BLAST+ is a rewrite of 
  9  NCBI's legacy BLAST (Basic Local Alignment Search Tool), based on the NCBI 
 10  C++ toolkit. The BLAST+ suite is available as command line programs or on 
 11  NCBI's web page. 
 12   
 13  Bio.SearchIO.BlastIO was tested on the following BLAST+ flavors and versions: 
 14   
 15      - flavors: blastn, blastp, blastx, tblastn, tblastx 
 16      - versions: 2.2.22+, 2.2.26+ 
 17   
 18  You should also be able to parse outputs from a local BLAST+ search or from 
 19  NCBI's web interface. Although the module was not tested against all BLAST+, 
 20  it should still be able to parse these other versions' outputs. Please submit 
 21  a bug report if you stumble upon an unparseable file. 
 22   
 23  Some output formats from the BLAST legacy suite (BLAST+'s predecessor) may 
 24  still be parsed by this module. However, results are not guaranteed. You may 
 25  try to use the Bio.Blast module to parse them instead. 
 26   
 27  More information about BLAST are available through these links: 
 28    - Publication: http://www.biomedcentral.com/1471-2105/10/421 
 29    - Web interface: http://blast.ncbi.nlm.nih.gov/ 
 30    - User guide: http://www.ncbi.nlm.nih.gov/books/NBK1762/ 
 31   
 32   
 33  Supported Formats 
 34  ================= 
 35   
 36  Bio.SearchIO.BlastIO supports the following BLAST+ output formats: 
 37   
 38    - XML        - 'blast-xml'  - parsing, indexing, writing 
 39    - Tabular    - 'blast-tab'  - parsing, indexing, writing 
 40    - Plain text - 'blast-text' - parsing 
 41   
 42   
 43  blast-xml 
 44  ========= 
 45   
 46  The blast-xml parser follows the BLAST XML DTD written here: 
 47  http://www.ncbi.nlm.nih.gov/dtd/NCBI_BlastOutput.mod.dtd 
 48   
 49  It provides the following attributes for each SearchIO object: 
 50   
 51  +----------------+-------------------------+-----------------------------+ 
 52  | Object         | Attribute               | XML Element                 | 
 53  +================+=========================+=============================+ 
 54  | QueryResult    | target                  | BlastOutput_db              | 
 55  |                +-------------------------+-----------------------------+ 
 56  |                | program                 | BlastOutput_program         | 
 57  |                +-------------------------+-----------------------------+ 
 58  |                | reference               | BlastOutput_reference       | 
 59  |                +-------------------------+-----------------------------+ 
 60  |                | version                 | BlastOutput_version[1]      | 
 61  |                +-------------------------+-----------------------------+ 
 62  |                | description             | Iteration_query-def         | 
 63  |                +-------------------------+-----------------------------+ 
 64  |                | id                      | Iteration_query-ID          | 
 65  |                +-------------------------+-----------------------------+ 
 66  |                | seq_len                 | Iteration_query-len         | 
 67  |                +-------------------------+-----------------------------+ 
 68  |                | param_evalue_threshold  | Parameters_expect           | 
 69  |                +-------------------------+-----------------------------+ 
 70  |                | param_entrez_query      | Parameters_entrez-query     | 
 71  |                +-------------------------+-----------------------------+ 
 72  |                | param_filter            | Parameters_filter           | 
 73  |                +-------------------------+-----------------------------+ 
 74  |                | param_gap_extend        | Parameters_gap-extend       | 
 75  |                +-------------------------+-----------------------------+ 
 76  |                | param_gap_open          | Parameters_gap-open         | 
 77  |                +-------------------------+-----------------------------+ 
 78  |                | param_include           | Parameters_include          | 
 79  |                +-------------------------+-----------------------------+ 
 80  |                | param_matrix            | Parameters_matrix           | 
 81  |                +-------------------------+-----------------------------+ 
 82  |                | param_pattern           | Parameters_pattern          | 
 83  |                +-------------------------+-----------------------------+ 
 84  |                | param_score_match       | Parameters_sc-match         | 
 85  |                +-------------------------+-----------------------------+ 
 86  |                | param_score_mismatch    | Parameters_sc-mismatch      | 
 87  |                +-------------------------+-----------------------------+ 
 88  |                | stat_db_num             | Statistics_db-num           | 
 89  |                +-------------------------+-----------------------------+ 
 90  |                | stat_db_len             | Statistics_db-len           | 
 91  |                +-------------------------+-----------------------------+ 
 92  |                | stat_eff_space          | Statistics_eff-space        | 
 93  |                +-------------------------+-----------------------------+ 
 94  |                | stat_entropy            | Statistics_entropy          | 
 95  |                +-------------------------+-----------------------------+ 
 96  |                | stat_hsp_len            | Statistics_hsp-len          | 
 97  |                +-------------------------+-----------------------------+ 
 98  |                | stat_kappa              | Statistics_kappa            | 
 99  |                +-------------------------+-----------------------------+ 
100  |                | stat_lambda             | Statistics_lambda           | 
101  +----------------+-------------------------+-----------------------------+ 
102  | Hit            | accession               | Hit_accession               | 
103  |                +-------------------------+-----------------------------+ 
104  |                | description             | Hit_def                     | 
105  |                +-------------------------+-----------------------------+ 
106  |                | id                      | Hit_id                      | 
107  |                +-------------------------+-----------------------------+ 
108  |                | seq_len                 | Hit_len                     | 
109  +----------------+-------------------------+-----------------------------+ 
110  | HSP            | bitscore                | Hsp_bit-score               | 
111  |                +-------------------------+-----------------------------+ 
112  |                | density                 | Hsp_density                 | 
113  |                +-------------------------+-----------------------------+ 
114  |                | evalue                  | Hsp_evalue                  | 
115  |                +-------------------------+-----------------------------+ 
116  |                | gap_num                 | Hsp_gaps                    | 
117  |                +-------------------------+-----------------------------+ 
118  |                | ident_num               | Hsp_identity                | 
119  |                +-------------------------+-----------------------------+ 
120  |                | pos_num                 | Hsp_positive                | 
121  |                +-------------------------+-----------------------------+ 
122  |                | bitscore_raw            | Hsp_score                   | 
123  +----------------+-------------------------+-----------------------------+ 
124  | HSPFragment    | aln_span                | Hsp_align-len               | 
125  | (also via      +-------------------------+-----------------------------+ 
126  | HSP)           | hit_frame               | Hsp_hit-frame               | 
127  |                +-------------------------+-----------------------------+ 
128  |                | hit_start               | Hsp_hit-from                | 
129  |                +-------------------------+-----------------------------+ 
130  |                | hit_end                 | Hsp_hit-to                  | 
131  |                +-------------------------+-----------------------------+ 
132  |                | hit                     | Hsp_hseq                    | 
133  |                +-------------------------+-----------------------------+ 
134  |                | aln_annotation          | Hsp_midline                 | 
135  |                +-------------------------+-----------------------------+ 
136  |                | pattern_start           | Hsp_pattern-from            | 
137  |                +-------------------------+-----------------------------+ 
138  |                | pattern_end             | Hsp_pattern-to              | 
139  |                +-------------------------+-----------------------------+ 
140  |                | query_frame             | Hsp_query-frame             | 
141  |                +-------------------------+-----------------------------+ 
142  |                | query_start             | Hsp_query-from              | 
143  |                +-------------------------+-----------------------------+ 
144  |                | query_end               | Hsp_query-to                | 
145  |                +-------------------------+-----------------------------+ 
146  |                | query                   | Hsp_qseq                    | 
147  +----------------+-------------------------+-----------------------------+ 
148   
149  You may notice that in BLAST XML files, sometimes BLAST replaces your true 
150  sequence ID with its own generated ID. For example, the query IDs become 
151  'Query_1', 'Query_2', and so on. While the hit IDs sometimes become 
152  'gnl|BL_ORD_ID|1', 'gnl|BL_ORD_ID|2', and so on. In these cases, BLAST lumps the 
153  true sequence IDs together with their descriptions. 
154   
155  The blast-xml parser is aware of these modifications and will attempt to extract 
156  the true sequence IDs out of the descriptions. So when accessing QueryResult or 
157  Hit objects, you will use the non-BLAST-generated IDs. 
158   
159  Conversely, the blast-xml writer will try to concatenate the true sequence IDs 
160  with their descriptions and use the BLAST-generated IDs. This enables you to 
161  write BLAST XML files using SearchIO as if they were written by a real BLAST 
162  program. 
163   
164   
165  blast-tab 
166  ========= 
167   
168  The default format for blast-tab support is the variant without comments (-m 6 
169  flag). Commented BLAST tabular files may be parsed, indexed, or written using 
170  the keyword argument 'comments' set to True: 
171   
172      # blast-tab defaults to parsing uncommented files 
173      >>> from Bio import SearchIO 
174      >>> uncommented = 'Blast/tab_2226_tblastn_004.txt' 
175      >>> qresult = SearchIO.read(uncommented, 'blast-tab') 
176      >>> qresult 
177      QueryResult(id='gi|11464971:4-101', 5 hits) 
178   
179      # set the keyword argument to parse commented files 
180      >>> commented = 'Blast/tab_2226_tblastn_008.txt' 
181      >>> qresult = SearchIO.read(commented, 'blast-tab', comments=True) 
182      >>> qresult 
183      QueryResult(id='gi|11464971:4-101', 5 hits) 
184   
185  For uncommented files, the parser defaults to using BLAST's default column 
186  ordering: 'qseqid sseqid pident length mismatch gapopen qstart qend sstart send 
187  evalue bitscore'. 
188   
189  If you want to parse an uncommented file with a customized column order, you can 
190  use the 'fields' keyword argument to pass the custom column order. The names of 
191  the column follow BLAST's naming. For example, 'qseqid' is the column for the 
192  query sequence ID. These names may be passed either as a Python list or as a 
193  space-separated strings. 
194   
195      # pass the custom column names as a Python list 
196      >>> fname = 'Blast/tab_2226_tblastn_009.txt' 
197      >>> custom_fields = ['qseqid', 'sseqid'] 
198      >>> qresult = next(SearchIO.parse(fname, 'blast-tab', fields=custom_fields)) 
199      >>> qresult 
200      QueryResult(id='gi|16080617|ref|NP_391444.1|', 3 hits) 
201   
202      # pass the custom column names as a space-separated string 
203      >>> fname = 'Blast/tab_2226_tblastn_009.txt' 
204      >>> custom_fields = 'qseqid sseqid' 
205      >>> qresult = next(SearchIO.parse(fname, 'blast-tab', fields=custom_fields)) 
206      >>> qresult 
207      QueryResult(id='gi|16080617|ref|NP_391444.1|', 3 hits) 
208   
209  You may also use the 'std' field name as an alias to BLAST's default 12 columns, 
210  just like when you run a command line BLAST search. 
211   
212  Note that the 'fields' keyword argument will be ignored if the parsed file is 
213  commented. Commented files have their column ordering stated explicitly in the 
214  file, so there is no need to specify it again in SearchIO. 
215   
216  'comments' and 'fields' keyword arguments are both applicable for parsing, 
217  indexing, and writing. 
218   
219  blast-tab provides the following attributes for each SearchIO objects: 
220   
221  +-------------+-------------------+--------------+ 
222  | Object      | Attribute         | Column name  | 
223  +=============+===================+==============+ 
224  | QueryResult | accession         | qacc         | 
225  |             +-------------------+--------------+ 
226  |             | accession_version | qaccver      | 
227  |             +-------------------+--------------+ 
228  |             | gi                | qgi          | 
229  |             +-------------------+--------------+ 
230  |             | seq_len           | qlen         | 
231  |             +-------------------+--------------+ 
232  |             | id                | qseqid       | 
233  +-------------+-------------------+--------------+ 
234  | Hit         | accession         | sacc         | 
235  |             +-------------------+--------------+ 
236  |             | accession_version | sacc_ver     | 
237  |             +-------------------+--------------+ 
238  |             | gi                | sgi          | 
239  |             +-------------------+--------------+ 
240  |             | gi_all            | sallgi       | 
241  |             +-------------------+--------------+ 
242  |             | id_all            | sallseqid    | 
243  |             +-------------------+--------------+ 
244  |             | seq_len           | slen         | 
245  |             +-------------------+--------------+ 
246  |             | id                | sseqid       | 
247  +-------------+-------------------+--------------+ 
248  | HSP         | bitscore          | bitscore     | 
249  |             +-------------------+--------------+ 
250  |             | btop              | btop         | 
251  |             +-------------------+--------------+ 
252  |             | evalue            | evalue       | 
253  |             +-------------------+--------------+ 
254  |             | gapopen_num       | gapopen      | 
255  |             +-------------------+--------------+ 
256  |             | gap_num           | gaps         | 
257  |             +-------------------+--------------+ 
258  |             | ident_pct         | nident       | 
259  |             +-------------------+--------------+ 
260  |             | ident_num         | pident       | 
261  |             +-------------------+--------------+ 
262  |             | mismatch_num      | mismatch     | 
263  |             +-------------------+--------------+ 
264  |             | pos_pct           | ppos         | 
265  |             +-------------------+--------------+ 
266  |             | pos_num           | positive     | 
267  |             +-------------------+--------------+ 
268  |             | bitscore_raw      | score        | 
269  +-------------+-------------------+--------------+ 
270  | HSPFragment | frames            | frames[2]    | 
271  | (also via   +-------------------+--------------+ 
272  | HSP)        | aln_span          | length       | 
273  |             +-------------------+--------------+ 
274  |             | query_end         | qend         | 
275  |             +-------------------+--------------+ 
276  |             | query_frame       | qframe       | 
277  |             +-------------------+--------------+ 
278  |             | query             | qseq         | 
279  |             +-------------------+--------------+ 
280  |             | query_start       | qstart       | 
281  |             +-------------------+--------------+ 
282  |             | hit_end           | send         | 
283  |             +-------------------+--------------+ 
284  |             | hit_frame         | sframe       | 
285  |             +-------------------+--------------+ 
286  |             | hit               | sseq         | 
287  |             +-------------------+--------------+ 
288  |             | hit_start         | sstart       | 
289  +-------------+-------------------+--------------+ 
290   
291  If the parsed file is commented, the following attributes may be available as 
292  well: 
293   
294  +--------------+---------------+----------------------------+ 
295  | Object       | Attribute     | Value                      | 
296  +==============+===============+============================+ 
297  | QueryResult  | description   | query description          | 
298  |              +---------------+----------------------------+ 
299  |              | fields        | columns in the output file | 
300  |              +---------------+----------------------------+ 
301  |              | program       | BLAST flavor               | 
302  |              +---------------+----------------------------+ 
303  |              | rid           | remote search ID           | 
304  |              +---------------+----------------------------+ 
305  |              | target        | target database            | 
306  |              +---------------+----------------------------+ 
307  |              | version       | BLAST version              | 
308  +--------------+---------------+----------------------------+ 
309   
310   
311  blast-text 
312  ========== 
313  The BLAST plain text output format has been known to change considerably between 
314  BLAST versions. NCBI itself has recommended that users not rely on the plain 
315  text output for parsing-related work. 
316   
317  However, in some cases parsing the plain text output may still be useful. 
318  SearchIO provides parsing support for the plain text output, but guarantees only 
319  a minimum level of support. Writing a parser that fully supports plain text 
320  output for all BLAST versions is not a priority at the moment. 
321   
322  If you do have a BLAST plain text file that can not be parsed and would like to 
323  submit a patch, we are more than happy to accept it. 
324   
325  The blast-text parser provides the following object attributes: 
326   
327  +-----------------+-------------------------+----------------------------------+ 
328  | Object          | Attribute               | Value                            | 
329  +=================+=========================+==================================+ 
330  | QueryResult     | description             | query sequence description       | 
331  |                 +-------------------------+----------------------------------+ 
332  |                 | id                      | query sequence ID                | 
333  |                 +-------------------------+----------------------------------+ 
334  |                 | program                 | BLAST flavor                     | 
335  |                 +-------------------------+----------------------------------+ 
336  |                 | seq_len                 | full length of query sequence    | 
337  |                 +-------------------------+----------------------------------+ 
338  |                 | target                  | target database of the search    | 
339  |                 +-------------------------+----------------------------------+ 
340  |                 | version                 | BLAST version                    | 
341  +-----------------+-------------------------+----------------------------------+ 
342  | Hit             | evalue                  | hit-level evalue, from the hit   | 
343  |                 |                         | table                            | 
344  |                 +-------------------------+----------------------------------+ 
345  |                 | id                      | hit sequence ID                  | 
346  |                 +-------------------------+----------------------------------+ 
347  |                 | description             | hit sequence description         | 
348  |                 +-------------------------+----------------------------------+ 
349  |                 | score                   | hit-level score, from the hit    | 
350  |                 |                         | table                            | 
351  |                 +-------------------------+----------------------------------+ 
352  |                 | seq_len                 | full length of hit sequence      | 
353  +-----------------+-------------------------+----------------------------------+ 
354  | HSP             | evalue                  | hsp-level evalue                 | 
355  |                 +-------------------------+----------------------------------+ 
356  |                 | bitscore                | hsp-level bit score              | 
357  |                 +-------------------------+----------------------------------+ 
358  |                 | bitscore_raw            | hsp-level score                  | 
359  |                 +-------------------------+----------------------------------+ 
360  |                 | gap_num                 | number of gaps in alignment      | 
361  |                 +-------------------------+----------------------------------+ 
362  |                 | ident_num               | number of identical residues     | 
363  |                 |                         | in alignment                     | 
364  |                 +-------------------------+----------------------------------+ 
365  |                 | pos_num                 | number of positive matches in    | 
366  |                 |                         | alignment                        | 
367  +-----------------+-------------------------+----------------------------------+ 
368  | HSPFragment     | aln_annotation          | alignment similarity string      | 
369  | (also via       +-------------------------+----------------------------------+ 
370  | HSP)            | aln_span                | length of alignment fragment     | 
371  |                 +-------------------------+----------------------------------+ 
372  |                 | hit                     | hit sequence                     | 
373  |                 +-------------------------+----------------------------------+ 
374  |                 | hit_end                 | hit sequence end coordinate      | 
375  |                 +-------------------------+----------------------------------+ 
376  |                 | hit_frame               | hit sequence reading frame       | 
377  |                 +-------------------------+----------------------------------+ 
378  |                 | hit_start               | hit sequence start coordinate    | 
379  |                 +-------------------------+----------------------------------+ 
380  |                 | hit_strand              | hit sequence strand              | 
381  |                 +-------------------------+----------------------------------+ 
382  |                 | query                   | query sequence                   | 
383  |                 +-------------------------+----------------------------------+ 
384  |                 | query_end               | query sequence end coordinate    | 
385  |                 +-------------------------+----------------------------------+ 
386  |                 | query_frame             | query sequence reading frame     | 
387  |                 +-------------------------+----------------------------------+ 
388  |                 | query_start             | query sequence start coordinate  | 
389  |                 +-------------------------+----------------------------------+ 
390  |                 | query_strand            | query sequence strand            | 
391  +-----------------+-------------------------+----------------------------------+ 
392   
393   
394  .. [1] may be modified 
395   
396  .. [2] When 'frames' is present, both ``query_frame`` and ``hit_frame`` will be 
397     present as well. It is recommended that you use these instead of 'frames' directly. 
398   
399  """ 
400   
401  from .blast_tab import BlastTabParser, BlastTabIndexer, BlastTabWriter 
402  from .blast_xml import BlastXmlParser, BlastXmlIndexer, BlastXmlWriter 
403  from .blast_text import BlastTextParser 
404   
405  __docformat__ = "restructuredtext en" 
406   
407   
408  # if not used as a module, run the doctest 
409  if __name__ == "__main__": 
410      from Bio._utils import run_doctest 
411      run_doctest() 
412