aizynthfinder package

Subpackages

Submodules

aizynthfinder.aizynthfinder module

Module containing a class that is the main interface the retrosynthesis tool.

class aizynthfinder.aizynthfinder.AiZynthFinder(configfile=None, configdict=None)

Bases: object

Public API to the aizynthfinder tool

If instantiated with the path to a yaml file or dictionary of settings the stocks and policy networks are loaded directly. Otherwise, the user is responsible for loading them prior to executing the tree search.

Variables:
  • config – the configuration of the search

  • expansion_policy – the expansion policy model

  • filter_policy – the filter policy model

  • stock – the stock

  • scorers – the loaded scores

  • tree – the search tree

  • analysis – the tree analysis

  • routes – the top-ranked routes

  • search_stats – statistics of the latest search

Parameters:
  • configfile (Optional[str]) – the path to yaml file with configuration (has priority over configdict), defaults to None

  • configdict (Optional[StrDict]) – the config as a dictionary source, defaults to None

property target_smiles: str

The SMILES representation of the molecule to predict routes on.

property target_mol: Molecule | None

The molecule to predict routes on

build_routes(selection=None, scorer=None)

Build reaction routes

This is necessary to call after the tree search has completed in order to extract results from the tree search.

Parameters:
  • selection (Optional[RouteSelectionArguments]) – the selection criteria for the routes

  • scorer (Optional[Union[str, List[str]]]) – a reference to the object used to score the nodes, can be a list

Raises:

ValueError – if the search tree not initialized

Return type:

None

extract_statistics()

Extracts tree statistics as a dictionary

Return type:

StrDict

prepare_tree()

Setup the tree for searching

Raises:

ValueError – if the target molecule was not set

Return type:

None

stock_info()

Return the stock availability for all leaf nodes in all collected reaction trees

The key of the return dictionary will be the SMILES string of the leaves, and the value will be the stock availability

Returns:

the collected stock information.

Return type:

StrDict

Perform the actual tree search

Parameters:

show_progress (bool) – if True, shows a progress bar

Returns:

the time past in seconds

Return type:

float

class aizynthfinder.aizynthfinder.AiZynthExpander(configfile=None, configdict=None)

Bases: object

Public API to the AiZynthFinder expansion and filter policies

If instantiated with the path to a yaml file or dictionary of settings the stocks and policy networks are loaded directly. Otherwise, the user is responsible for loading them prior to executing the tree search.

Variables:
  • config – the configuration of the search

  • expansion_policy – the expansion policy model

  • filter_policy – the filter policy model

Parameters:
  • configfile (Optional[str]) – the path to yaml file with configuration (has priority over configdict), defaults to None

  • configdict (Optional[StrDict]) – the config as a dictionary source, defaults to None

do_expansion(smiles, return_n=5, filter_func=None)

Do the expansion of the given molecule returning a list of reaction tuples. Each tuple in the list contains reactions producing the same reactants. Hence, nested structure of the return value is way to group reactions.

If filter policy is setup, the probability of the reactions are added as metadata to the reaction.

The additional filter functions makes it possible to do customized filtering. The callable should take as only argument a RetroReaction object and return True if the reaction can be kept or False if it should be removed.

Parameters:
  • smiles (str) – the SMILES string of the target molecule

  • return_n (int) – the length of the return list

  • filter_func (Optional[Callable[[RetroReaction], bool]]) – an additional filter function

Returns:

the grouped reactions

Return type:

List[Tuple[FixedRetroReaction, …]]

aizynthfinder.reactiontree module

Module containing the implementation of a reaction tree or route and factory classes to make such trees

class aizynthfinder.reactiontree.ReactionTree

Bases: object

Encapsulation of a bipartite reaction tree of a single route. The nodes consists of either FixedRetroReaction or UniqueMolecule objects.

The reaction tree is initialized at instantiation and is not supposed to be updated.

Variables:
  • graph – the bipartite graph

  • is_solved – if all of the leaf nodes are in stock

  • root – the root of the tree

  • created_at_iteration – iteration the reaction tree was created

classmethod from_dict(tree_dict)

Create a new ReactionTree by parsing a dictionary.

This is supposed to be the opposite of to_dict, but because that format loses information, the returned object is not a full copy as the stock will only contain the list of molecules marked as in_stock in the dictionary.

The returned object should be sufficient to e.g. generate an image of the route.

Parameters:

tree_dict (StrDict) – the dictionary representation

Returns:

the reaction tree

Return type:

ReactionTree

property metadata: StrDict

Return a dicitionary with route metadata

depth(node)

Return the depth of a node in the route

Parameters:

node (Union[UniqueMolecule, FixedRetroReaction]) – the query node

Returns:

the depth

Return type:

int

distance_to(other, content='both')

Calculate the distance to another reaction tree

This is a tree edit distance, with unit cost to insert and deleted nodes, and the Jaccard distance for substituting nodes

Parameters:
  • other (ReactionTree) – the reaction tree to compare to

  • content (str) – determine what part of the tree to include in the calculation

Returns:

the distance between the routes

Return type:

float

hash_key()

Calculates a hash code for the tree using the sha224 hash function recursively

Returns:

the hash key

Return type:

str

in_stock(node)

Return if a node in the route is in stock

Note that is a property set on creation and as such is not updated.

Parameters:

node (Union[UniqueMolecule, FixedRetroReaction]) – the query node

Returns:

if the molecule is in stock

Return type:

bool

is_branched()

Returns if the route is branched

i.e. checks if the maximum depth is not equal to the number of reactions.

Return type:

bool

leafs()

Generates the molecules nodes of the reaction tree that has no predecessors, i.e. molecules that has not been broken down

Yield:

the next leaf molecule in the tree

Return type:

Iterable[UniqueMolecule]

molecules()

Generates the molecule nodes of the reaction tree

Yield:

the next molecule in the tree

Return type:

Iterable[UniqueMolecule]

parent_molecule(mol)

Returns the parent molecule within the reaction tree. :param mol: the query node (molecule) :return: the parent molecule

Parameters:

mol (UniqueMolecule)

Return type:

UniqueMolecule

reactions()

Generates the reaction nodes of the reaction tree

Yield:

the next reaction in the tree

Return type:

Iterable[FixedRetroReaction]

subtrees()

Generates the subtrees of this reaction tree a subtree is a reaction treee starting at a molecule node that has children.

Yield:

the next subtree

Return type:

Iterable[ReactionTree]

to_dict(include_metadata=False)

Returns the reaction tree as a dictionary in a pre-defined format. :param include_metadata: if True include metadata :return: the reaction tree

Return type:

StrDict

to_image(in_stock_colors=None, show_all=True)

Return a pictorial representation of the route

Parameters:
  • in_stock_colors (Optional[FrameColors]) – the colors around molecules, defaults to {True: “green”, False: “orange”}

  • show_all (bool) – if True, also show nodes that are marked as hidden

Returns:

the image of the route

Return type:

PilImage

to_json(include_metadata=False)

Returns the reaction tree as a JSON string in a pre-defined format.

Returns:

the reaction tree

Return type:

str

class aizynthfinder.reactiontree.ReactionTreeLoader(*args, **kwargs)

Bases: ABC

Base class for classes that creates a reaction tree object

This class makes sure that node attributes are set after the graph is generated, and provides utility methods.

Parameters:
  • args (Any)

  • kwargs (Any)

class aizynthfinder.reactiontree.ReactionTreeFromDict(*args, **kwargs)

Bases: ReactionTreeLoader

Creates a reaction tree object from a dictionary

Parameters:
  • args (Any)

  • kwargs (Any)

class aizynthfinder.reactiontree.ReactionTreeFromExpansion(*args, **kwargs)

Bases: ReactionTreeLoader

Create a ReactionTree from a single reaction

This is mainly intended as a convenience function for the expander interface

Parameters:
  • args (Any)

  • kwargs (Any)

Module contents