synplan.chem package#

Subpackages#

Submodules#

synplan.chem.precursor module#

Module containing a class Precursor that represents a precursor (extend molecule object) in the search tree.

class synplan.chem.precursor.Precursor(molecule: MoleculeContainer, canonicalize: bool = True)#

Bases: object

Precursor class is used to extend the molecule behavior needed for interaction with a tree in MCTS.

is_building_block(bb_stock: set[str], min_mol_size: int = 6) bool#

Checks if a Precursor is a building block.

Parameters:
  • bb_stock – The list of building blocks. Each building block is represented by a canonical SMILES.

  • min_mol_size – If the size of the Precursor is equal or smaller than min_mol_size it is automatically classified as building block.

Returns:

True is Precursor is a building block.

synplan.chem.precursor.compose_precursors(precursors: list | None = None, exclude_small: bool = True, min_mol_size: int = 6) MoleculeContainer#

Takes a list of precursors, excludes small precursors if specified, and composes them into a single molecule. The composed molecule then is used for the prediction of synthesisability of the characterizing the possible success of the route including the nodes with the given precursor.

Parameters:
  • precursors – The list of precursor to be composed.

  • exclude_small – The parameter that determines whether small precursor should be excluded from the composition process. If exclude_small is set to True, only precursor with a length greater than min_mol_size will be composed.

  • min_mol_size – The parameter used with exclude_small.

Returns:

A composed precursor as a MoleculeContainer object.

synplan.chem.reaction module#

Module containing classes and functions for manipulating reactions and reaction rules.

class synplan.chem.reaction.Reaction(*args, **kwargs)#

Bases: ReactionContainer

Reaction class used for a general representation of reaction.

synplan.chem.reaction.add_small_mols(big_mol: MoleculeContainer, small_molecules: Any | None = None) list[MoleculeContainer]#

Takes a molecule and returns a list of modified molecules where each small molecule has been added to the big molecule.

Parameters:
  • big_mol – A molecule.

  • small_molecules – A list of small molecules that need to be added to the molecule.

Returns:

Returns a list of molecules.

synplan.chem.reaction.apply_reaction_rule(molecule: MoleculeContainer, reaction_rule: Reactor, sort_reactions: bool = False, top_reactions_num: int = 3, validate_products: bool = True, rebuild_with_cgr: bool = False) Iterator[list[MoleculeContainer]]#

Applies a reaction rule to a given molecule.

Parameters:
  • molecule – A molecule to which reaction rule will be applied.

  • reaction_rule – A reaction rule to be applied.

  • sort_reactions

  • top_reactions_num – The maximum amount of reactions after the application of reaction rule.

  • validate_products – If True, validates the final products.

  • rebuild_with_cgr – If True, the products are extracted from CGR decomposition.

Returns:

An iterator yielding the products of reaction rule application.

synplan.chem.rdkit_compat module#

RDKit compatibility layer for SynPlanner.

Provides conversion functions so RDKit users can pass RDKit Mol objects as input (target molecules, building blocks) and receive RDKit Mol objects as output (route molecules) without working with chython directly.

Requires rdkit to be installed.

synplan.chem.rdkit_compat.building_blocks_from_rdkit(rdkit_mols: Iterable) frozenset[str]#

Convert RDKit Mol objects to a building block set of canonical SMILES.

The returned frozenset is compatible with Tree(building_blocks=...).

Parameters:

rdkit_mols – Iterable of RDKit Mol objects.

Returns:

Frozenset of canonical SMILES strings.

synplan.chem.rdkit_compat.extract_routes_rdkit(tree, keep_mapping: bool = True) list[dict]#

Extract all winning routes as nested dicts with RDKit Mol objects.

Mirrors extract_routes() but each molecule node also carries an "mol" key with an RDKit Mol object.

Structure:

{
    "type": "mol",
    "smiles": str,
    "mol": rdkit.Chem.Mol,
    "in_stock": bool,
    "children": [{
        "type": "reaction",
        "children": [<mol nodes>, ...]
    }]
}
Parameters:
  • tree – A solved Tree object.

  • keep_mapping – Preserve atom map numbers on RDKit atoms.

Returns:

List of route tree dicts, one per winning node.

synplan.chem.rdkit_compat.route_to_rdkit(tree, node_id: int, keep_mapping: bool = True) list[dict]#

Convert a synthesis route to a flat list of retrosynthetic steps with RDKit Mol objects.

Each step is a dict:

{
    "target": rdkit.Chem.Mol,       # molecule being disconnected
    "precursors": [Mol, ...],        # resulting fragments
    "in_stock": [bool, ...],         # building block status per precursor
    "rule_id": int | None,           # reaction rule applied
    "depth": int,                    # step depth in the tree
}
Parameters:
  • tree – A solved Tree object.

  • node_id – Winning node ID (must be in tree.winning_nodes).

  • keep_mapping – Preserve atom map numbers on RDKit atoms. Required to build atom-mapped RDKit ChemicalReaction objects from the molecules.

Returns:

List of step dicts ordered from target to building blocks.

synplan.chem.rdkit_compat.target_from_rdkit(rdkit_mol, standardize: bool = True, clean_stereo: bool = True, clean2d: bool = True) MoleculeContainer#

Convert an RDKit Mol to a chython MoleculeContainer for use as a search target.

Applies the same standardization as mol_from_smiles(): canonicalization, stereo cleaning, and 2D coordinate generation.

Parameters:
  • rdkit_mol – An RDKit Mol or RWMol object.

  • standardize – Whether to canonicalize the molecule.

  • clean_stereo – Whether to remove stereo marks.

  • clean2d – Whether to generate clean 2D coordinates.

Returns:

A standardized MoleculeContainer ready for Tree search.

synplan.chem.utils module#

Module containing additional functions needed in different reaction data processing protocols.

synplan.chem.utils.cgr_from_reaction_rule(reaction_rule: ReactionContainer) CGRContainer#

Creates a CGR from the given reaction rule.

Parameters:

reaction_rule – The reaction rule to be converted.

Returns:

The resulting CGR.

synplan.chem.utils.hash_from_reaction_rule(reaction_rule: ReactionContainer) int#

Generates hash for the given reaction rule.

Parameters:

reaction_rule – The reaction rule to be converted.

Returns:

The resulting hash.

synplan.chem.utils.mol_from_smiles(smiles: str, standardize: bool = True, clean_stereo: bool = True, clean2d: bool = True) MoleculeContainer#

Converts a SMILES string to a MoleculeContainer object and optionally standardizes, cleans stereochemistry, and cleans 2D coordinates.

Parameters:
  • smiles – The SMILES string representing the molecule.

  • standardize – Whether to standardize the molecule (default is True).

  • clean_stereo – Whether to remove the stereo marks on atoms of the molecule (default is True).

  • clean2d – Whether to clean the 2D coordinates of the molecule (default is True).

Returns:

The processed molecule object.

Raises:

ValueError – If the SMILES string could not be processed by chython.

synplan.chem.utils.query_to_mol(query: QueryContainer) MoleculeContainer#

Converts a QueryContainer object into a MoleculeContainer object.

Parameters:

query – A QueryContainer object representing the query structure.

Returns:

A MoleculeContainer object that replicates the structure of the query.

synplan.chem.utils.reaction_query_to_reaction(reaction_rule: ReactionContainer) ReactionContainer#

Converts a ReactionContainer object with query structures into a ReactionContainer with molecular structures.

Parameters:

reaction_rule – A ReactionContainer object where reactants and products are QueryContainer objects.

Returns:

A new ReactionContainer object where reactants and products are MoleculeContainer objects.

synplan.chem.utils.reverse_reaction(reaction: ReactionContainer) ReactionContainer#

Reverses the given reaction.

Parameters:

reaction – The reaction to be reversed.

Returns:

The reversed reaction.

synplan.chem.utils.safe_canonicalization(molecule: MoleculeContainer) MoleculeContainer#

Attempts to canonicalize a molecule, handling any exceptions. If the canonicalization process fails due to an InvalidAromaticRing exception, it safely returns the original molecule.

Parameters:

molecule – The given molecule to be canonicalized.

Returns:

The canonicalized molecule if successful, otherwise the original molecule.

synplan.chem.utils.standardize_building_blocks(input_file: str, output_file: str) str#

Standardizes custom building blocks.

Parameters:
  • input_file – The path to the file that stores the original building blocks.

  • output_file – The path to the file that will store the standardized building blocks.

Returns:

The path to the file with standardized building blocks.

synplan.chem.utils.unite_molecules(molecules: Iterable[MoleculeContainer]) MoleculeContainer#

Unites a list of MoleculeContainer objects into a single MoleculeContainer. This function takes multiple molecules and combines them into one larger molecule. The first molecule in the list is taken as the base, and subsequent molecules are united with it sequentially.

Parameters:

molecules – A list of MoleculeContainer objects to be united.

Returns:

A single MoleculeContainer object representing the union of all input molecules.

Module contents#