aizynthfinder.search.retrostar package

Submodules

aizynthfinder.search.retrostar.cost module

Module containing Retro* cost models

class aizynthfinder.search.retrostar.cost.MoleculeCost(config)

Bases: object

A class to compute the molecule cost.

The cost to be computed is taken from the input config. If no molecule_cost is set, assigns ZeroMoleculeCost as the cost by default. The molecule_cost can be set as a dictionary in config under search in the following format: ‘algorithm’: ‘retrostar’ ‘algorithm_config’: {

‘molecule_cost’: {

‘cost’: name of the search cost class or custom_package.custom_model.CustomClass, other settings or params

}

}

The cost can be computed by calling the instantiated class with a molecule.

calculator = MyCost(config)
cost = calculator.calculate(molecule)
Parameters:

config (Configuration) – the configuration of the tree search

class aizynthfinder.search.retrostar.cost.RetroStarCost(**kwargs)

Bases: object

Encapsulation of the original Retro* molecular cost model

Numpy implementation of original pytorch model

The predictions of the score is made on a Molecule object

mol = Molecule(smiles="CCC")
scorer = RetroStarCost()
score = scorer.calculate(mol)

The model provided when creating the scorer object should be a pickled tuple. The first item of the tuple should be a list of the model weights for each layer. The second item of the tuple should be a list of the model biases for each layer.

Parameters:
  • model_path – the filename of the model weights and biases

  • fingerprint_length – the number of bits in the fingerprint

  • fingerprint_radius – the radius of the fingerprint

  • dropout_rate – the dropout_rate

  • kwargs (Any)

calculate(mol)
Parameters:

mol (Molecule)

Return type:

float

class aizynthfinder.search.retrostar.cost.ZeroMoleculeCost

Bases: object

Encapsulation of a Zero cost model

calculate(_mol)
Parameters:

_mol (Molecule)

Return type:

float

aizynthfinder.search.retrostar.nodes module

Module containing a classes representation various tree nodes

class aizynthfinder.search.retrostar.nodes.MoleculeNode(mol, config, molecule_cost, parent=None)

Bases: TreeNodeMixin

An OR node representing a molecule

Variables:
  • cost – the cost of synthesizing the molecule

  • expandable – if True, this node is part of the frontier

  • mol – the molecule represented by the node

  • in_stock – if True the molecule is in stock and hence should not be expanded

  • parent – the parent of the node

  • solved – if True the molecule is in stock or at least one child node is solved

  • value – the current rn(m|T)

Parameters:
classmethod create_root(smiles, config, molecule_cost)

Create a root node for a tree using a SMILES.

Parameters:
  • smiles (str) – the SMILES representation of the root state

  • config (Configuration) – settings of the tree search algorithm

  • molecule_cost (MoleculeCost)

Returns:

the created node

Return type:

MoleculeNode

classmethod from_dict(dict_, config, molecules, molecule_cost, parent=None)

Create a new node from a dictionary, i.e. deserialization

Parameters:
Returns:

a deserialized node

Return type:

MoleculeNode

property children: List[ReactionNode]

Gives the reaction children nodes

property target_value: float

The V_t(m|T) value, the current cost of the tree containing this node

Returns:

the value

property prop: StrDict

Dictionary with publicly exposed properties

add_stub(cost, reaction)

Add a stub / sub-tree to this node

Parameters:
  • cost (float) – the cost of the reaction

  • reaction (RetroReaction) – the reaction creating the stub

Returns:

list of all newly added molecular nodes

Return type:

Sequence[MoleculeNode]

ancestors()

Return the ancestors of this node

Returns:

the ancestors

Return type:

set

close()

Updates the values of this node after expanding it.

Returns:

the delta V value

Return type:

float

serialize(molecule_store)

Serialize the node object to a dictionary

Parameters:

molecule_store (MoleculeSerializer) – the serialized molecules

Returns:

the serialized node

Return type:

StrDict

update(solved)

Update the node as part of the update algorithm, calling the update() method of its parent if available.

Parameters:

solved (bool) – if the child node was solved

Return type:

None

class aizynthfinder.search.retrostar.nodes.ReactionNode(cost, reaction, parent)

Bases: TreeNodeMixin

An AND node representing a reaction

Variables:
  • cost – the cost of the reaction

  • parent – the parent of the node

  • reaction – the reaction represented by the node

  • solved – if True all children nodes are solved

  • target_value – the V(m|T) for the children, the current cost

  • value – the current rn(r|T)

Parameters:
  • cost (float) – the cost of the reaction

  • reaction (RetroReaction) – the reaction to be represented by the node

  • parent (MoleculeNode) – the parent of the node

classmethod create_stub(cost, reaction, parent, config)

Create a ReactionNode and creates all the MoleculeNode objects that are the children of the node.

Parameters:
  • cost (float) – the cost of the reaction

  • reaction (RetroReaction) – the reaction to be represented by the node

  • parent (MoleculeNode) – the parent of the node

  • config (Configuration) – the configuration of the search tree

Return type:

ReactionNode

classmethod from_dict(dict_, config, molecules, molecule_cost, parent)

Create a new node from a dictionary, i.e. deserialization

Parameters:
Returns:

a deserialized node

Return type:

ReactionNode

property children: List[MoleculeNode]

Gives the molecule children nodes

property prop: StrDict

Dictionary with publicly exposed properties

serialize(molecule_store)

Serialize the node object to a dictionary

Parameters:

molecule_store (MoleculeSerializer) – the serialized molecules

Returns:

the serialized node

Return type:

StrDict

update(value, from_mol=None)

Update the node as part of the update algorithm, calling the update() method of its parent

Parameters:
  • value (float) – the delta V value

  • from_mol (Optional[TreeMolecule]) – the molecule being expanded, used for excluding propagation

Return type:

None

aizynthfinder.search.retrostar.search_tree module

Module containing a class that holds the tree search

class aizynthfinder.search.retrostar.search_tree.SearchTree(config, root_smiles=None)

Bases: AndOrSearchTreeBase

Encapsulation of the Retro* search tree (an AND/OR tree).

Variables:
  • config – settings of the tree search algorithm

  • root – the root node

Parameters:
  • config (Configuration) – settings of the tree search algorithm

  • root_smiles (Optional[str]) – the root will be set to a node representing this molecule, defaults to None

classmethod from_json(filename, config)

Create a new search tree by deserialization from a JSON file

Parameters:
  • filename (str) – the path to the JSON node

  • config (Configuration) – the configuration of the search tree

Returns:

a deserialized tree

Return type:

SearchTree

property mol_nodes: Sequence[MoleculeNode]

Return the molecule nodes of the tree

one_iteration()
Perform one iteration of
  1. Selection

  2. Expansion

  3. Update

Raises:

StopIteration – if the search should be pre-maturely terminated

Returns:

if a solution was found

Return type:

bool

routes()

Extracts and returns routes from the AND/OR tree

Returns:

the routes

Return type:

List[ReactionTree]

serialize(filename)

Seralize the search tree to a JSON file

Parameters:

filename (str) – the path to the JSON file

Return type:

None

Module contents