picogl.examples.utils.pdb_loader

PDB File Loader for MolViewSpec Integration

This module provides functionality to: 1. Load and parse PDB files 2. Convert PDB data to MolViewSpec format 3. Integrate with PicoGL’s molecular visualization system

Classes

Atom

Represents a single atom from a PDB file

BondType

Python 3.10 compatible StrEnum.

Bond

Represents a bond between two atoms

Residue

Represents a residue/amino acid

PDBStructure

Complete PDB structure data

PDBLoaderResults

Raw PDB parse output before bond inference and residue finalization.

PDBLoader

Loads and parses PDB files

Functions

_pdb_line(raw_line) → str

Normalize a PDB text line for fixed-width field parsing.

_optional_float(value, default) → float

_parse_atom_line(line) → Atom

Parse an ATOM or HETATM line from PDB format.

_bond_key(i, j) → tuple[int, int]

_atom_distance(a, b) → float

_parse_conect_line(line, serial_to_idx) → List[Bond]

Parse a CONECT line into bonds (central atom → up to four partners).

_find_named_atom(residue, name) → Optional[Atom]

_generate_bonds(atoms, residues) → List[Bond]

Infer covalent bonds from geometry (protein/ligand sticks).

_generate_peptide_bonds(atom_index, bonds, residues)

_generate_intra_residue_bonds(atom_index, bonds, residues)

_merge_bonds(*bond_lists) → List[Bond]

Union bond lists, dropping duplicate undirected pairs.

save_molviewspec(molviewspec, output_path)

Save MolViewSpec data to a JSON file

Module Contents

class picogl.examples.utils.pdb_loader.Atom[source]

Represents a single atom from a PDB file

serial: int[source]
name: str[source]
res_name: str[source]
chain_id: str[source]
res_seq: int[source]
x: float[source]
y: float[source]
z: float[source]
element: str[source]
charge: str = ''[source]
occupancy: float = 1.0[source]
b_factor: float = 0.0[source]
class picogl.examples.utils.pdb_loader.BondType[source]

Bases: picogl.utils.strenum.StrEnum

Python 3.10 compatible StrEnum.

Behaves like Python 3.11 enum.StrEnum.

SINGLE: str = 'single'[source]
DOUBLE: str = 'double'[source]
TRIPLE: str = 'triple'[source]
AROMATIC: str = 'aromatic'[source]
class picogl.examples.utils.pdb_loader.Bond[source]

Represents a bond between two atoms

atom1_idx: int[source]
atom2_idx: int[source]
bond_type: BondType[source]
class picogl.examples.utils.pdb_loader.Residue[source]

Represents a residue/amino acid

name: str[source]
chain_id: str[source]
seq_num: int[source]
atoms: List[Atom][source]
start_idx: int[source]
class picogl.examples.utils.pdb_loader.PDBStructure[source]

Complete PDB structure data

title: str[source]
atoms: List[Atom][source]
bonds: List[Bond][source]
residues: List[Residue][source]
chains: List[str][source]
newline: str = Multiline-String[source]
Show Value
"""
"""
property calpha_atoms: list[source]

calpha_atoms

property calpha_bonds[source]

Generate bonds between consecutive C-alpha atoms in the same chain.

get_atom_positions() → numpy.ndarray[source]

Get all atom positions as a numpy array

get_atom_elements() → List[str][source]

Get all atom element symbols

get_residue_atoms(residue_idx: int) → List[Atom][source]

Get atoms for a specific residue

property report: str[source]
picogl.examples.utils.pdb_loader._pdb_line(raw_line: str) → str[source]

Normalize a PDB text line for fixed-width field parsing.

picogl.examples.utils.pdb_loader._optional_float(value, default: float) → float[source]
picogl.examples.utils.pdb_loader._parse_atom_line(line: str) → Atom[source]

Parse an ATOM or HETATM line from PDB format.

picogl.examples.utils.pdb_loader._bond_key(i: int, j: int) → tuple[int, int][source]
picogl.examples.utils.pdb_loader._atom_distance(a: Atom, b: Atom) → float[source]
picogl.examples.utils.pdb_loader._parse_conect_line(line: str, serial_to_idx: Dict[int, int]) → List[Bond][source]

Parse a CONECT line into bonds (central atom → up to four partners).

picogl.examples.utils.pdb_loader._find_named_atom(residue: Residue, name: str) → Atom | None[source]
picogl.examples.utils.pdb_loader._generate_bonds(atoms: List[Atom], residues: List[Residue]) → List[Bond][source]

Infer covalent bonds from geometry (protein/ligand sticks).

PDB CONECT records usually cover ligands only. Protein connectivity must be rebuilt from residue geometry.

picogl.examples.utils.pdb_loader._generate_peptide_bonds(atom_index: dict[int, int], bonds: list[Bond], residues: list[Residue])[source]
picogl.examples.utils.pdb_loader._generate_intra_residue_bonds(atom_index: dict[int, int], bonds: list[Bond], residues: list[Residue])[source]
picogl.examples.utils.pdb_loader._merge_bonds(*bond_lists: List[Bond]) → List[Bond][source]

Union bond lists, dropping duplicate undirected pairs.

class picogl.examples.utils.pdb_loader.PDBLoaderResults[source]

Raw PDB parse output before bond inference and residue finalization.

atoms: List[Atom][source]
conect_bonds: List[Bond][source]
chains: set[str][source]
current_residue: Residue | None[source]
current_residue_atoms: List[Atom][source]
residues: List[Residue][source]
title: str = 'Unknown Structure'[source]
to_structure() → PDBStructure[source]

Append the last residue, infer bonds, and return the public structure.

class picogl.examples.utils.pdb_loader.PDBLoader(path: str | pathlib.Path)[source]

Loads and parses PDB files

path[source]
structure: PDBStructure | None = None[source]
_load_pdb() → None[source]

Load and parse the PDB file.

open_file() → PDBLoaderResults[source]
to_molviewspec() → Dict[source]

Convert PDB structure to MolViewSpec format

to_picogl_data() → Dict[source]

Convert PDB structure to PicoGL-compatible data format

picogl.examples.utils.pdb_loader.save_molviewspec(molviewspec: Dict, output_path: str)[source]

Save MolViewSpec data to a JSON file