Skip to content

Biopython BLAST Parse Fix

DodaTech Updated 2026-06-26 3 min read

You will learn how to extract and summarize key information from BLAST result XML files.

The Problem

The bioinfo blast parse pattern is frequently misapplied by data scientists and Python developers, leading to runtime errors, incorrect results, or inefficient code. This quick-fix guide shows the correct implementation and common pitfalls to avoid when working with BIOINFO in Python.

The Wrong Way

The most common mistake is using the wrong method signature, incorrect parameters, or misunderstanding the underlying data structure. Here is what typically goes wrong:

from Bio.Blast import NCBIXML
result = NCBIXML.read(open('blast.xml'))
for ali in result.alignments:
    hsp = ali.hsps[0]
    if hsp.expect < 1e-10:
        print(ali.title[:60], hsp.expect)

What happens:

gi|12345|ref|NM_001| Homo sapiens gene 1e-150 gi|67890|ref|NM_002| ... 1e-50

This approach fails because the API contract is violated -- parameters are passed in the wrong order, the input shape doesn't match expectations, or the method is called on an incompatible object type.

The Right Way

The correct approach uses the proper API with the right parameters. Here is the fixed version:

print(f'Query: {result.query}
	Length: {result.query_length}
	Hits: {len(result.alignments)}
	Best E-value: {result.alignments[0].hsps[0].expect}')

Expected output:

Query: ATCGATCGATCG
	Length: 150
	Hits: 45
	Best E-value: 3e-120

Step-by-Step Fix

1. Understand the data types and shapes

Before applying any operation, verify the data types and shapes of your inputs. In Python Data Science, most errors come from type or shape mismatches.

# Always inspect your data first
print(type(data))
print(data.shape if hasattr(data, 'shape') else 'No shape')
print(data.dtype if hasattr(data, 'dtype') else 'No dtype')

2. Apply the correct method with proper arguments

Use the corrected code shown above. Pay special attention to keyword arguments that control behavior like axis, inplace, or how.

3. Verify the result

Always validate that the output matches expectations before proceeding:

# Verification pattern
result = perform_operation(data)
assert some_condition(result), "Operation failed unexpectedly"
print(f"Success: {result.shape if hasattr(result, 'shape') else result}")

Prevention Tips

  • Access query metadata: result.query, result.query_length: Access query metadata: result.query, result.query_length
  • Iterate over result.alignments for each hit: Iterate over result.alignments for each hit
  • Each alignment has result.descriptions with additional metadata: Each alignment has result.descriptions with additional metadata
  • Access multiple HSPs via alignment.hsps for split alignments: Access multiple HSPs via alignment.hsps for split alignments
  • Filter by e-value threshold for significant results: Filter by e-value threshold for significant results

Common Mistakes

  1. Not filtering by e-value and getting many insignificant hits - Not filtering by e-value and getting many insignificant hits
  2. Only looking at first HSP when query/subject have multiple high-scoring segments - Only looking at first HSP when query/subject have multiple high-scoring segments

These mistakes appear frequently in real-world bioinfo code. DodaTech's contributors have identified these patterns through analysis of open-source projects, production systems, and community forums like Stack Overflow.

Practice Exercise

Parse a BLAST result, filter for e-value < 1e-5, extract sequence identity percentage for each significant hit.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions. This hands-on approach ensures you retain the knowledge and can apply it independently.

FAQ

### What is the difference between alignments and descriptions?

descriptions has summary info; alignments has full sequence alignment data.

How do I get percent identity?

hsp.identities / hsp.align_length * 100.

What does bit score mean?

Normalized score independent of database size and scoring matrix. Higher = better.

Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro. DodaTech tools integrate seamlessly with Python Data Science workflows for enhanced productivity and security.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro