MolecularDiffusion.modules.models.syncogen.utils.rdkit

RDKit utility functions for molecule building and validation.

Functions

build_molecule(→ str)

Build a molecule from model outputs. Used in calculating pvalid.

build_molecules_from_graphs(...)

Build RDKit molecules from graphs with optional coordinates.

calc_energy(→ Optional[float])

Calculate MMFF94 or UFF energy for an RDKit molecule. Prefer MMFF if available, otherwise UFF. Return None if calculation fails.

get_lipinski_descriptors(→ list)

Get Lipinski descriptors for a molecule or batch of molecules (Rule of 5).

is_valid_action(→ bool)

Check if an action is valid for the current molecule fragment graph.

is_valid_smiles(smiles_or_mol)

Check if a SMILES string is valid and can be fully sanitized.

mol_to_pharm_cond(→ Tuple[torch.Tensor, torch.Tensor, ...)

Extract pharmacophore conditioning from a reference molecule.

save_as_sdf(mol, filepath[, properties])

Save molecule to SDF file with optional properties.

sdf_to_coordinates(→ torch.Tensor)

Load coordinates from SDF file.

set_mol_coordinates(→ rdkit.Chem.Mol)

Set 3D coordinates on an RDKit molecule.

Module Contents

MolecularDiffusion.modules.models.syncogen.utils.rdkit.build_molecule(nodes: torch.Tensor, decoded_edges: torch.Tensor, smiles: bool = False) str

Build a molecule from model outputs. Used in calculating pvalid.

Parameters:
  • nodes – Tensor of node indices [n_nodes]

  • decoded_edges – Tensor of decoded model outputs [n_edges, 5] containing (reaction_id, node1_order, node2_order, center1_idx, center2_idx). These are not the same as actions for adding to MFG and must be converted.

  • smiles – Whether to return a SMILES string or an RDKit molecule

Returns:

SMILES string of the molecule

MolecularDiffusion.modules.models.syncogen.utils.rdkit.build_molecules_from_graphs(graphs: MolecularDiffusion.modules.models.syncogen.api.graph.graph.BBRxnGraph, coords: torch.Tensor | None = None) List[rdkit.Chem.Mol | None]

Build RDKit molecules from graphs with optional coordinates.

Parameters:
  • graphs – BBRxnGraph object (batched or unbatched)

  • coords – Optional [B, N, 3] coordinate tensor

Returns:

List of RDKit molecules (None for failed reconstructions). Each successful mol has coordinates set if coords was provided.

MolecularDiffusion.modules.models.syncogen.utils.rdkit.calc_energy(mol: rdkit.Chem.Mol, per_atom: bool = False) float | None

Calculate MMFF94 or UFF energy for an RDKit molecule. Prefer MMFF if available, otherwise UFF. Return None if calculation fails.

MolecularDiffusion.modules.models.syncogen.utils.rdkit.get_lipinski_descriptors(mols) list

Get Lipinski descriptors for a molecule or batch of molecules (Rule of 5).

Parameters:

mols – RDKit molecule or list of RDKit molecules

Returns:

List of dicts with Lipinski Rule of 5 descriptors (MW, LogP, HBD, HBA)

Return type:

list

MolecularDiffusion.modules.models.syncogen.utils.rdkit.is_valid_action(mfg: MolecularDiffusion.modules.models.syncogen.api.rdkit.assembly.RDKitMoleculeAssembly, action: list) bool

Check if an action is valid for the current molecule fragment graph.

Parameters:
  • mfg – Current molecule fragment graph

  • action – List of [new_fragment_global_id, reaction_id, existing_frag_idx, center1_idx, center2_idx]

Returns:

Whether the action is valid

Return type:

bool

MolecularDiffusion.modules.models.syncogen.utils.rdkit.is_valid_smiles(smiles_or_mol)

Check if a SMILES string is valid and can be fully sanitized.

MolecularDiffusion.modules.models.syncogen.utils.rdkit.mol_to_pharm_cond(mol: rdkit.Chem.Mol, batch_size: int, n_subset: int, center: bool = True, normalize: bool = True) Tuple[torch.Tensor, torch.Tensor, torch.Tensor]

Extract pharmacophore conditioning from a reference molecule.

Parameters:
  • mol – RDKit molecule with conformer

  • batch_size – Number of copies to create (for batched conditioning)

  • n_subset – Max pharmacophores per sample (randomly subsampled per batch element)

  • center – Center molecule coordinates before extraction

  • normalize – Normalize positions by COORDS_STD

Returns:

[batch_size, n_subset, n_pharm_types] one-hot pharmacophore types pos: [batch_size, n_subset, 3] pharmacophore positions mask: [batch_size, n_subset] padding mask

Return type:

types

MolecularDiffusion.modules.models.syncogen.utils.rdkit.save_as_sdf(mol: rdkit.Chem.Mol, filepath: str, properties: dict | None = None)

Save molecule to SDF file with optional properties.

Parameters:
  • mol – RDKit molecule with conformer

  • filepath – Output file path

  • properties – Optional dict of properties to add as SDF data fields

MolecularDiffusion.modules.models.syncogen.utils.rdkit.sdf_to_coordinates(sdf_path: str) torch.Tensor

Load coordinates from SDF file.

Parameters:

sdf_path – Path to SDF file

Returns:

Tensor of shape [n_atoms, 3]

MolecularDiffusion.modules.models.syncogen.utils.rdkit.set_mol_coordinates(mol: rdkit.Chem.Mol, coords: torch.Tensor) rdkit.Chem.Mol

Set 3D coordinates on an RDKit molecule.

Parameters:
  • mol – RDKit molecule (will be modified in place)

  • coords – [n_atoms, 3] coordinate tensor

Returns:

Molecule with updated conformer