aizynthfinder.chem package

Submodules

aizynthfinder.chem.mol module

Module containing classes to deal with Molecules - mostly wrappers around rdkit routines.

class aizynthfinder.chem.mol.Molecule(rd_mol=None, smiles=None, sanitize=False)

Bases: object

A base class for molecules. Encapsulate an RDKit mol object and functions that can be applied to such a molecule.

The objects of this class is hashable by the inchi key and hence comparable with the equality operator.

Variables:
  • rd_mol – the RDkit mol object that is encapsulated

  • smiles – the SMILES representation of the molecule

Parameters:
  • rd_mol (Optional[RdMol]) – a RDKit mol object to encapsulate, defaults to None

  • smiles (Optional[str]) – a SMILES to convert to a molecule object, defaults to None

  • sanitize (bool) – if True, the molecule will be immediately sanitized, defaults to False

Raises:

MoleculeException – if neither rd_mol or smiles is given, or if the molecule could not be sanitized

property inchi: str

The inchi representation of the molecule Created by lazy evaluation. Will cause the molecule to be sanitized.

Returns:

the inchi

property inchi_key: str

The inchi key representation of the molecule Created by lazy evaluation. Will cause the molecule to be sanitized.

Returns:

the inchi key

property index_to_mapping: Dict[int, int]

Return a dictionary mapping to atom indices to atom mappings

property mapping_to_index: Dict[int, int]

Return a dictionary mapping to atom mappings to atom indices

property weight: float

Return the exact molecular weight of the molecule

basic_compare(other)

Compare this molecule to another but only to the basic part of the inchi key, thereby ignoring stereochemistry etc

Parameters:

other (Molecule) – the molecule to compare to

Returns:

True if chemical formula and connectivity is the same

Return type:

bool

fingerprint(radius, nbits=2048, chiral=False)

Returns the Morgan fingerprint of the molecule

Parameters:
  • radius (int) – the radius of the fingerprint

  • nbits (int) – the length of the fingerprint

  • chiral (bool) – if True, include chirality information

Returns:

the fingerprint

Return type:

ndarray

has_atom_mapping()

Determines if a the molecule has atom mappings

Returns:

True if at least one atom has a mapping

Return type:

bool

make_unique()

Returns an instance of the UniqueMolecule class that is representing the same molecule but is not hashable or comparable.

Returns:

the unique molecule

Return type:

UniqueMolecule

remove_atom_mapping(exceptions=None)

Remove all mappings of the atoms and update the smiles

Parameters:

exceptions (Optional[Sequence[int]]) – keep the listed atom mappings

Return type:

None

sanitize(raise_exception=True)

Sanitizes the molecule if it has not been done before.

Parameters:

raise_exception (bool) – if True will raise exception on failed sanitation

Raises:

MoleculeException – if the molecule could not be sanitized

Return type:

None

class aizynthfinder.chem.mol.TreeMolecule(parent, transform=None, rd_mol=None, smiles=None, sanitize=False, mapping_update_callback=None)

Bases: Molecule

A special molecule that keeps a reference to a parent molecule.

If the class is instantiated without specifying the transform argument, it is computed by increasing the value of the parent.transform variable.

If no parent is provided the atoms with atom mapping number are tracked and inherited to children.

Variables:
  • mapped_mol – the tracked molecule with atom mappings

  • mapped_smiles – the SMILES of the tracked molecule with atom mappings

  • original_smiles – the SMILES as passed when instantiating the class

  • parent – parent molecule

  • transform – a numerical number corresponding to the depth in the tree

Parameters:
  • parent (Optional['TreeMolecule']) – a TreeMolecule object that is the parent

  • transform (Optional[int]) – the transform value, defaults to None

  • rd_mol (Optional[RdMol]) – a RDKit mol object to encapsulate, defaults to None

  • smiles (Optional[str]) – a SMILES to convert to a molecule object, defaults to None

  • sanitize (bool) – if True, the molecule will be immediately sanitized, defaults to False

  • mapping_update_callback (Optional[Callable[['TreeMolecule'], None]]) – if given will call this method before setting up the mapped_smiles

Raises:

MoleculeException – if neither rd_mol or smiles is given, or if the molecule could not be sanitized

property mapping_to_index: Dict[int, int]

Return a dictionary mapping to atom mappings to atom indices

property mapped_atom_bonds: List[Tuple[int, int]]

Return a list of atom bonds as tuples on the mapped atom indices

get_bonds_in_molecule(query_bonds)

Get bonds (from a list of bonds) that are present in the molecule. :param bonds: List of bond (atom pairs) :return: A list of bonds

Parameters:

query_bonds (Sequence[Sequence[int]])

Return type:

Sequence[Sequence[int]]

has_all_focussed_bonds(bonds)

Checks that the focussed bonds exist in the target molecule’s atom bonds.

Parameters:
  • bonds (Sequence[Sequence[int]]) – Focussed bonds.

  • target_mol – The target molecule.

Returns:

A boolean indicating if the input bonds exist in the target molecule.

Return type:

bool

class aizynthfinder.chem.mol.UniqueMolecule(rd_mol=None, smiles=None, sanitize=False)

Bases: Molecule

A special molecule with the hash set to the id of the object. Therefore no two instances of this class will be comparable.

Parameters:
  • rd_mol (Optional[RdMol]) – a RDKit mol object to encapsulate, defaults to None

  • smiles (Optional[str]) – a SMILES to convert to a molecule object, defaults to None

  • sanitize (bool) – if True, the molecule will be immediately sanitized, defaults to False

Raises:

MoleculeException – if neither rd_mol or smiles is given, or if the molecule could not be sanitized

aizynthfinder.chem.mol.none_molecule()

Return an empty molecule

Return type:

UniqueMolecule

aizynthfinder.chem.reaction module

Module containing classes to deal with Reactions.

class aizynthfinder.chem.reaction.RetroReaction(mol, index=0, metadata=None, **kwargs)

Bases: ABC, _ReactionInterfaceMixin

A retrosynthesis reaction. Only a single molecule is the reactant.

This is an abstract class and child classes needs to implement the _apply and _make_smiles functions that should create the reactants molecule objects and the reaction SMILES representation, respectively.

Variables:
  • mol – the TreeMolecule object that this reaction is applied to

  • index – a unique index of this reaction, to count for the fact that a reaction can produce more than one outcome

  • metadata – meta data associated with the reaction

Parameters:
  • mol (TreeMolecule) – the molecule

  • index (int) – the index, defaults to 0

  • metadata (Optional[StrDict]) – some meta data

  • kwargs (Any)

Params kwargs:

any extra parameters for child classes

classmethod from_serialization(init_args, reactants)

Create an object from a serialization. It does 1) instantiate an object using the init_args and 2) set the reactants to a tuple-form of `reactants

Parameters:
  • init_args (StrDict) – the arguments passed to the __init__ method

  • reactants (List[List[TreeMolecule]]) – the reactants

Returns:

the deserialized object

Return type:

RetroReaction

property reactants: Tuple[Tuple[TreeMolecule, ...], ...]

Returns the reactant molecules. Apply the reaction if necessary.

Returns:

the products of the reaction

property smiles: str

The reaction as a SMILES

Returns:

the SMILES

property unqueried: bool

Return True if the reactants has never been retrieved

copy(index=None)

Shallow copy of this instance.

Parameters:

index (Optional[int]) – new index, defaults to None

Returns:

the copy

Return type:

RetroReaction

mapped_reaction_smiles()

Get the mapped reaction SMILES if it exists :return: the SMILES

Return type:

str

to_dict()

Return the retro reaction as dictionary This dictionary is not suitable for serialization, but is used by other serialization routines The elements of the dictionary can be used to instantiate a new reaction object

Return type:

StrDict

class aizynthfinder.chem.reaction.TemplatedRetroReaction(mol, index=0, metadata=None, **kwargs)

Bases: RetroReaction

A retrosynthesis reaction that uses a reaction SMARTS and RDChiral to produce reactant molecules. The SMILES representation of the reaction is the SMARTS (modified by RDKit)

Parameters:
  • mol (TreeMolecule) – the molecule

  • index (int) – the index, defaults to 0

  • metadata (Optional[StrDict]) – some meta data

  • smarts – a string representing the template

  • kwargs (Any)

property rd_reaction: RdReaction

Return the RDKit reaction created from the SMART

to_dict()

Return the retro reaction as dictionary This dictionary is not suitable for serialization, but is used by other serialization routines The elements of the dictionary can be used to instantiate a new reaction object

Return type:

StrDict

class aizynthfinder.chem.reaction.SmilesBasedRetroReaction(mol, index=0, metadata=None, **kwargs)

Bases: RetroReaction

A retrosynthesis reaction where the SMILES of the reactants are given on initiation

The SMILES representation of the reaction is the reaction SMILES

Parameters:
  • mol (TreeMolecule) – the molecule

  • index (int) – the index, defaults to 0

  • metadata (Optional[StrDict]) – some meta data

  • reactants_str – a dot-separated string of reactant SMILES strings

  • kwargs (Any)

to_dict()

Return the retro reaction as dictionary This dictionary is not suitable for serialization, but is used by other serialization routines The elements of the dictionary can be used to instantiate a new reaction object

Return type:

StrDict

class aizynthfinder.chem.reaction.FixedRetroReaction(mol, smiles='', metadata=None)

Bases: _ReactionInterfaceMixin

A retrosynthesis reaction that has the same interface as RetroReaction but it is fixed so it does not support SMARTS application or any creation of reactants.

The reactants are set by using the reactants property.

Variables:
  • mol – the UniqueMolecule object that this reaction is applied to

  • smiles – the SMILES representation of the RDKit reaction

  • metadata – meta data associated with the reaction

  • reactants – the reactants of this reaction

Parameters:
  • mol (UniqueMolecule) – the molecule

  • smiles (str) – the SMILES of the reaction

  • metadata (Optional[StrDict]) – some meta data

copy()

Shallow copy of this instance.

Returns:

the copy

Return type:

FixedRetroReaction

to_smiles_based_retroreaction()

Convert a FixedRetroReaction to a SmilesBasedRetroReaction.

Returns:

the SmilesBasedRetroReaction.

Return type:

SmilesBasedRetroReaction

aizynthfinder.chem.reaction.hash_reactions(reactions, sort=True)

Creates a hash for a list of reactions

Parameters:
  • reactions (Union[Iterable[RetroReaction], Iterable[FixedRetroReaction]]) – the reactions to hash

  • sort (bool) – if True will sort all molecules, defaults to True

Returns:

the hash string

Return type:

str

aizynthfinder.chem.serialization module

Module containing helper classes and routines for serialization.

class aizynthfinder.chem.serialization.MoleculeSerializer

Bases: object

Utility class for serializing molecules

The id of the molecule to be serialized can be obtained with:

serializer = MoleculeSerializer()
mol = Molecule(smiles="CCCO")
idx = serializer[mol]

which will take care of the serialization of the molecule.

property store: Dict[int, Any]

Return all serialized molecules as a dictionary

class aizynthfinder.chem.serialization.MoleculeDeserializer(store)

Bases: object

Utility class for deserializing molecules. The serialized molecules are created upon instantiation of the class.

The deserialized molecules can be obtained with:

deserializer = MoleculeDeserializer()
mol = deserializer[idx]
Parameters:

store (Dict[int, Any])

get_tree_molecules(ids)

Return multiple deserialized tree molecules

Parameters:

ids (Sequence[int]) – the list of IDs to deserialize

Returns:

the molecule objects

Return type:

Sequence[aizynthfinder.chem.TreeMolecule]

aizynthfinder.chem.serialization.serialize_action(action, molecule_store)

Serialize a retrosynthesis action

Parameters:
Returns:

the action as a dictionary

Return type:

StrDict

aizynthfinder.chem.serialization.deserialize_action(dict_, molecule_store)

Deserialize a retrosynthesis action

Parameters:
  • dict – the (re)action as a dictionary

  • molecule_store (MoleculeDeserializer) – the molecule deserialization object

  • dict_ (StrDict)

Returns:

the created action object

Return type:

RetroReaction

Module contents

Sub-package containing chemistry routines