Add lightweight PDB (Protein Data Bank) file support#7926
Add lightweight PDB (Protein Data Bank) file support#7926behroozazarkhalili wants to merge 5 commits into
Conversation
|
Hi ! same comment as in #7925: is there a lib in python that parses the text of the files ? Maybe it would be more useful to return a parsed object rather than just the data as string |
- Add zero-dependency pure Python parser for PDB format - Support ATOM and HETATM record types with configurable filtering - Handle fixed-width column parsing per official PDB specification - Support gzip, bzip2, and xz compression via magic bytes detection - Support .pdb and .ent file extensions - Add comprehensive test suite with 24 tests - Add documentation to loading.mdx Columns include: atom_serial, atom_name, residue_name, chain_id, residue_seq, x, y, z, occupancy, temp_factor, element, and more. Part of the bioinformatics file format support series.
Based on reviewer feedback from @lhoestq, refactor the PDB loader to follow the ImageFolder pattern where each row contains one complete protein structure file, rather than one row per atom. Changes: - Add ProteinStructure feature type for lazy loading structure files - Refactor PdbFolder to use FolderBasedBuilder with folder/metadata support - Support automatic label inference from directory names - Support metadata files (CSV/JSONL) for additional annotations - Simplify from ~400 lines atom-parser to ~55 lines folder-based builder This approach matches how researchers typically work with protein structures: - One structure = one data point - Supports downstream tools (BioPython, PyMOL, MDAnalysis) - Consistent with ImageFolder/AudioFolder/VideoFolder patterns Test: 43 tests passing (15 feature + 28 builder tests)
- Sort imports alphabetically in features.py - Fix line length in protein_structure.py error messages - Format function call in test_pdb.py
…e_files API The embed_storage API gained local_files and remote_files parameters (v4.8.5) to gate which paths are embedded into the Arrow array. ProteinStructure was written before this change and raised TypeError (unexpected keyword argument 'local_files') whenever the embed path ran, e.g. on save_to_disk/push_to_hub. Align its signature and gating logic with the other binary features (Pdf/Image), embedding local paths only when local_files is set and remote URLs only when remote_files is set.
564fb37 to
4cbaa08
Compare
|
Hi @lhoestq, thanks for the review! Same plan as #7925 (cross-linking so the discussion stays in one place): On a parsing library: mature options exist — gemmi, biotite, and Biopython all parse PDB/mmCIF — but they're all compiled/heavyweight dependencies (Biopython ~150 MB installed; gemmi and biotite ship C/C++ extensions), which is a lot to add to On returning a parsed object instead of a string: agreed, and it's a regression I introduced. The original version parsed ATOM/HETATM records into typed columns ( Proposal — combine both so each row is one structure and a parsed object, with no new dependency:
That keeps it queryable and ML-friendly while staying dependency-free. Does that direction work for you? |
Decoding a ProteinStructure now returns a struct-of-arrays of atom-level
columns keyed by PDBx/mmCIF dictionary names (type_symbol, label_atom_id,
label_comp_id, label_asym_id, label_seq_id, Cartn_x/y/z, occupancy,
B_iso_or_equiv) instead of the raw file text, while keeping one row per
structure. PDB fixed-width records are mapped onto the same mmCIF-native
names so PDB- and mmCIF-derived datasets share one column vocabulary.
The shared parser lives in features/protein_parsing.py (single canonical
column/dtype table reused by both formats). ProteinStructure parses lazily
on decode (mirroring the Image/Audio/Mesh features); decode=False still
returns the raw {path, bytes} mapping for a custom parser. New feature/config
options include_hetatm and columns are threaded through a _base_feature()
factory on FolderBasedBuilder so folder loaders can forward config-derived
arguments to their base feature. Docs updated to match the new API.
|
Hi @lhoestq — same update as #7925, applied here so the two PRs stay consistent. The >>> ds = load_dataset("pdb", data_dir="structures")
>>> ds[0]["structure"]
{'type_symbol': ['N', 'C', ...], 'label_atom_id': ['N', 'CA', ...],
'label_comp_id': ['ALA', 'ALA', ...], 'Cartn_x': [0.0, 1.458, ...], ...}
Existing tests pass; added coverage for the parsed schema, HETATM filtering, and column selection, and |
|
Hi ! same comment as in the mmCIF PR:
lmk what you think |
Summary
This PR adds support for loading PDB (Protein Data Bank) files with
load_dataset(), following the ImageFolder pattern where one row = one structure.Based on feedback from @lhoestq in #7930, this approach makes datasets more practical for ML workflows:
Architecture
Uses
FolderBasedBuilderpattern (likeImageFolder,AudioFolder):New
ProteinStructureFeature TypeSupported Extensions
.pdb,.entUsage
Test Results
All 28 PDB tests + 15 ProteinStructure feature tests pass.
Related PRs
cc @lhoestq @georgia-hf