torchref.io.data_router module
Data Router - Automatic file type detection and reader selection.
This module provides a smart router that automatically detects the file type and returns the appropriate reader (CIF or legacy format).
Supported file types: - Structure factors: .mtz, .cif (with reflection data) - Structure models: .pdb, .cif (with atom_site data) - Restraints: .cif (with restraint dictionaries)
- Usage:
from torchref.io import DataRouter
# Automatic detection router = DataRouter(“data.mtz”) reader = router.get_reader() data_type = router.data_type # ‘reflections’, ‘structure’, or ‘restraints’
# Or use the factory method reader, data_type = DataRouter.route(“data.cif”)
- exception torchref.io.data_router.DataRouterError[source]
Bases:
ExceptionException raised when file type cannot be determined or is unsupported.
- class torchref.io.data_router.DataRouter(filepath, verbose=1)[source]
Bases:
objectAutomatic file type detection and reader selection.
This class examines a file and automatically selects the appropriate reader based on file extension and content.
- Parameters:
- filepath
Path to the file to read.
- Type:
Path
- data_type
Type of data detected (‘reflections’, ‘structure’, ‘restraints’, ‘ihm_ensemble’, or None).
- Type:
str or None
Examples
router = DataRouter("structure.cif") reader = router.get_reader() print(router.data_type) # 'structure'
- MTZ_EXTENSIONS = {'.mtz'}
- PDB_EXTENSIONS = {'.ent', '.pdb'}
- CIF_EXTENSIONS = {'.cif', '.mmcif'}
- get_reader()[source]
Get the appropriate reader for this file.
- Returns:
Reader instance (ReflectionCIFReader, ModelCIFReader, RestraintCIFReader, MTZ, or PDB depending on file type).
- Return type:
- Raises:
DataRouterError – If file type is not supported or cannot be determined.
- get_data()[source]
Get the data from the file using the appropriate reader.
This is a convenience method that calls get_reader() and then invokes the reader to get the data.
- Returns:
For reflections: (data_dict, cell, spacegroup) For structure: (dataframe, residues, spacegroup) For restraints: Restraint data (format depends on reader)
- Return type:
- classmethod route(filepath, verbose=1)[source]
Factory method to quickly route a file to the appropriate reader.
- Parameters:
- Returns:
Tuple of (reader, data_type) where: - reader: The appropriate reader instance - data_type: String indicating the type (‘reflections’, ‘structure’, ‘restraints’)
- Return type:
Examples
reader, data_type = DataRouter.route("7JI4-sf.cif") if data_type == 'reflections': data_dict, cell, spacegroup = reader()