Skip to content

Biopython Neighbor Search Fix

DodaTech Updated 2026-06-26 3 min read

You will learn how to find nearby atoms and residues within a distance threshold.

The Problem

The bioinfo pdb neighbor search 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.PDB import PDBParser, NeighborSearch
parser = PDBParser()
structure = parser.get_structure('1ubq', '1ubq.pdb')
atoms = list(structure.get_atoms())
ns = NeighborSearch(atoms)
center = structure[0]['A'][50]['CA'].get_vector()
nearby = ns.search(center, 5.0)
print(len(nearby))

What happens: 25 # 25 atoms within 5 Angstroms of CA of residue 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:

residues = set(a.get_parent() for a in nearby)
print(sorted((r.get_id()[1], r.get_resname()) for r in residues)[:10])

Expected output:

[(48, 'LEU'), (49, 'LYS'), (50, 'ILE'), (51, 'GLY'), (52, 'THR')]

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

  • Use NeighborSearch(atom_list) for spatial indexing of atoms: Use NeighborSearch(atom_list) for spatial indexing of atoms
  • Use .search(center, radius) for all atoms within radius: Use .search(center, radius) for all atoms within radius
  • Use .search(center, radius, level='R') for residue-level search: Use .search(center, radius, level='R') for residue-level search
  • Use KD-tree implementation for efficient spatial queries: Use KD-tree implementation for efficient spatial queries
  • NeighborSearch is built on a KD-tree for O(log N) queries: NeighborSearch is built on a KD-tree for O(log N) queries

Common Mistakes

  1. Not using level parameter and getting atom-level results when residue-level is needed - Not using level parameter and getting atom-level results when residue-level is needed
  2. Creating NeighborSearch on every query (reuse the same object for many searches) - Creating NeighborSearch on every query (reuse the same object for many searches)

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

Find all residues within 6 Angstroms of a bound ligand in a protein-ligand complex structure.

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 a KD-tree?

A spatial data structure for efficient nearest-neighbor and radius queries in 3D.

Can NeighborSearch find contacts between specific chains?

Search within the full atom list, then filter results by chain.

What is the search complexity?

O(log N) per query after O(N log N) construction.

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