torchref.io.ihm module
IHM mmCIF reader and writer for kinetic ensemble data.
Provides IHMReader and IHMWriter for reading and writing IHM
(Integrative/Hybrid Methods) mmCIF files that describe multi-state
kinetic ensembles.
Requires the optional python-ihm dependency:
pip install torchref[ihm]
Detection of IHM files (via is_ihm_file) uses only gemmi and does
not require python-ihm.
- class torchref.io.ihm.IHMReader(filepath, verbose=0)[source]
Bases:
objectRead IHM mmCIF files into torchref ModelCollection + IHMEnsembleMapping.
Uses
python-ihmto parse IHM-specific categories (_ihm_multi_state_modeling,_ihm_model_group, etc.) and gemmi /ModelCIFReaderto parse standard_atom_sitedata.- Parameters:
Examples
reader = IHMReader("ensemble.cif") model_collection, mapping = reader(max_res=1.5) # Or step by step: mapping = reader.read_mapping() mapping.atom_data_per_state = reader.read_atom_data(mapping) model_collection = reader.build_model_collection(mapping)
- static is_ihm_file(filepath)[source]
Quick check whether a CIF file contains IHM categories.
Uses gemmi to avoid requiring
python-ihmfor detection. Looks for_ihm_model_listor_ihm_multi_state_modelingloops.
- read_mapping()[source]
Parse IHM categories and build an
IHMEnsembleMapping.Reads the following IHM categories: -
_ihm_multi_state_modeling-> states -_ihm_model_list-> model enumeration -_ihm_model_group+_ihm_model_group_link-> groups -_ihm_multi_state_model_group_link-> state-group fractionsAlso extracts cell and spacegroup from standard mmCIF categories.
- Return type:
- read_atom_data(mapping)[source]
Extract per-state atom DataFrames using
pdbx_PDB_model_num.Reuses
ModelCIFReaderfor robust_atom_siteparsing, then splits by model number and maps to state IDs.- Parameters:
mapping (IHMEnsembleMapping) – Mapping with state info (specifically
model_numper state).- Returns:
Mapping of
state_id-> atom DataFrame.- Return type:
dict of int -> pandas.DataFrame
- build_model_collection(mapping, max_res=1.5, radius_angstrom=4.0, device=None)[source]
Build a
ModelCollectionfrom parsed IHM data.For each state, creates a
ModelFTand loads atom data. Then assembles aModelCollectionwith one timepoint per model group.- Parameters:
mapping (IHMEnsembleMapping) – Must have
atom_data_per_statepopulated.max_res (float) – Maximum resolution for FFT grid setup.
radius_angstrom (float) – Radius for electron density calculation.
device (torch.device, optional) – Device for model tensors.
- Return type:
- __call__(max_res=1.5, radius_angstrom=4.0, device=None)[source]
Read IHM file and return
(ModelCollection, IHMEnsembleMapping).- Parameters:
max_res (float) – Maximum resolution for FFT grid.
radius_angstrom (float) – Radius for electron density calculation.
device (torch.device, optional) – Device for tensors.
- Return type:
- class torchref.io.ihm.IHMWriter(model_collection, mapping=None, datasets=None, verbose=0)[source]
Bases:
objectWrite a torchref
ModelCollectionto IHM mmCIF format.Uses
python-ihmto build a complete IHM System object and write it as a compliant mmCIF file.- Parameters:
model_collection (ModelCollection) – The collection to write.
mapping (IHMEnsembleMapping, optional) – Original mapping for round-tripping metadata. If
None, creates a minimal mapping from the collection structure.verbose (int) – Verbosity level.
Examples
writer = IHMWriter(model_collection, mapping=mapping) writer.write("refined_ensemble.cif") # Or without a pre-existing mapping: writer = IHMWriter(model_collection) writer.write("refined_ensemble.cif")
- write(filepath)[source]
Write IHM mmCIF file.
Builds a
python-ihmSystem object with: - Entity and assembly from base model atom data - Multi-state definitions from mapping states - Model groups from mapping groups with population fractions - Atom coordinates per state viapdbx_PDB_model_num- Parameters:
filepath (str) – Output file path.
- static mapping_from_kinetic_refinement(model_collection, state_names=None, time_delays=None)[source]
Build an
IHMEnsembleMappingfrom aModelCollectionwith optional kinetic metadata.- Parameters:
model_collection (ModelCollection) – The refined model collection.
state_names (list of str, optional) – Names for each base model / state. Default: state_1, state_2, …
time_delays (dict, optional) – Mapping of timepoint name -> time delay in seconds.
- Return type: