schrodinger.analysis.reaction module¶
A module to support chemical reactions, retrosynthetic analysis, and reaction enumeration.
Examples:
# run one reaction
rxn_smarts = "[C:8][N:7][C:2]([C:1])=[O:4]>>[C:1][C:2](O)=[O:4].[N:7][C:8]"
rxn = Reaction("Amide coupling", rxn_smarts)
amide = Chem.MolFromSmiles('CC(=O)NC')
for acid, amine in rxn.apply((amide,)):
print Chem.MolToSmiles(acid)
print Chem.MolToSmiles(amine)
reverse_rxn = rxn.inverse()
# retrosynthetic analysis
reactions = read_reactions_file('reactions.json')
retrosynthesis = retrosynthesize(amide, reactions)
print retrosynthesis.asString()
for route in retrosynthesis.getRoutes():
print route.asString()
print "Starting materials:"
for sm in route.getStartingNodes():
print "-", sm
route.write('route.json')
# combinatorial enumeration from a route file
route = read_route('route.json')
synthesizer = Synthesizer(route)
for product in synthesizer.synthesizeCombinations([acids, amines]):
print Chem.MolToSmiles(product)
-
class
schrodinger.analysis.reaction.
Node
(mol=None, reagent_class=None)¶ Bases:
object
Base class for a node in a synthetic route or a retrosynthetic tree. A node is associated with a molecule and with a reagent class (both of which may be None). It is also associated with zero or more reaction instances, but the base class does not implement an API to add reaction instances because each subclass has a different policy concerning the number of reaction instances allowed.
-
treeAsString
(products=True, starting_materials=True, indexes=True)¶ Return a recursive string representation of the tree (unlike __str__, which is a short representation of the current node). The reaction names are always shown; starting materials and products are optional.
Parameters: - products (bool) – include reaction product SMILES
- starting_materials (bool) – include starting material SMILES
Return type: str
-
-
class
schrodinger.analysis.reaction.
RetroSynthesisNode
(mol=None, reagent_class=None)¶ Bases:
schrodinger.analysis.reaction.Node
A node in a retrosynthetic analysis tree. A node may have one or more reaction instances, which represent the ways of synthesizing the node in a single step.
-
addReactionInstance
(reaction, precursors)¶ Add a reaction instance to the current node. This represents a one-step synthesis of the node from a list of precursor nodes.
-
getRoutes
(include_zero=False)¶ Enumerate all the possible routes to synthesize the target.
Parameters: include_zero (bool) – if True, the first route on the list will be the “zero-step route” where the target is the starting material. Return type: list of RouteNode
-
-
class
schrodinger.analysis.reaction.
RouteNode
(mol=None, reagent_class=None)¶ Bases:
schrodinger.analysis.reaction.Node
A node in a synthetic route. Similar to RetroSynthesisNode, except that it can have one reaction instance at most. Also, RouteNode provides additional methods that only make sense for a synthetic route.
-
precursors
¶ The list of precursors of this node (may be empty).
-
reaction_instance
¶ The reaction instance associated with this node. If the node has no reaction instance, raises KeyError.
-
reaction
¶ The reaction associated with this node. If the node has no reaction instance, raises KeyError.
-
isStartingMaterial
()¶ Return True if the node represents a starting material (i.e., has no reaction instance).
-
getStartingNodes
()¶ Search recursively and return the Node objects for all the starting materials in the route.
Return type: list of Node-derived objects
-
setReactionInstance
(reaction, precursors)¶ Set the reaction instance to the current node. This represents a one-step synthesis of the node from a list of precursor nodes.
-
steps
()¶ Return the total number of steps in the route. For example, the following synthesis has 3 steps but depth 2.
Target => A + B A => AA B => BBReturn type: int
-
depth
(_depth=0)¶ Return the maximum depth of the route. See example under steps().
Return type: int
-
write
(filename)¶ Write a route file.
-
getTreeData
()¶ Return a simple data structure, suitable for dumping to JSON, representing the route. See write() for more details.
-
getReactionSet
()¶ Return the set of reactions used by the route.
-
checkReactions
(reqs)¶ Check that the route meets all requirements. Every element of ‘reqs’ must match a tag or reaction name for at least one of the reactions used by the route. Tag matching is exact; name matching uses shell-like globbing (*, ?, []).
Returns: True if route meets requirements. Return type: bool
-
-
class
schrodinger.analysis.reaction.
ReagentNode
(mol=None, reagent_class=None, filename=None, smiles=None, smiles_list=None)¶ Bases:
schrodinger.analysis.reaction.RouteNode
A node representing a starting material in a synthetic route. Unlike RouteNode, it cannot have any reaction instances. Reagent nodes are identified by a reagent class.
Reagents may optionally have a filename or a smiles or a list of smiles as a source of reagent molecules. If none of these is provided, the object can try to find a reagent file based on the reagent class alone.
-
findReagentFile
(libpath=None)¶ First, look for structure files matching <reagent_class>.* in the CWD. If one is found, return it. If multiple matches are found, an exception is raised. If none are found, look for <reagent_class>.csv in the mmshare data directory and return it if it exists, or None otherwise.
Parameters: libpath (list of str) – list of directories to prepend to the standard reagent library search path Returns: path to reagent file, or None if not found Return type: str Raises: ValueError if multiple matches are found in the CWD.
-
-
class
schrodinger.analysis.reaction.
ReagentClass
(name, reactive=False, description=None, smarts=None)¶ Bases:
object
Struct-like class to hold metadata for a reagent class. Fields include name, description, and ‘reactive’. A reactive reagent is one which shouldn’t be analyzed retrosynthetically. For example, acyl chlorides.
-
asDict
()¶ Return a dict representation of the reaction suitable for JSON serialization.
Returns: dict Return type: dict
-
findReagentFile
(libpath=None)¶ Look for <reagent_class>.* (where the extension corresponds to a recognized structure file format) in each of the directories in the library search path. Return the first match, but if multiple matches are found in the same directory, raise an exception.
Parameters: libpath (list of str) – list of directories to prepend to the default reagent library search path Returns: path to reagent file, or None if not found Return type: str Raises: ValueError if multiple matches are found in the CWD.
-
size
(libpath=None)¶ Return the number of structures in the reagent file for this class.
Parameters: libpath (list of str) – list of directories to prepend to the standard reagent library search path Returns: number of structures, or zero when no file is found. Return type: int
-
-
class
schrodinger.analysis.reaction.
Synthesizer
(route, id_property='s_m_title', allow_multiple_products=False)¶ Bases:
object
A Synthesizer is a “machine” that given a RouteNode, knows how to apply all the reactions in the RouteNode in the right order to a list of starting materials to give the final product.
-
run
(starting_materials)¶ Return the final product of the RouteNode given a list of starting materials.
Return type: Mol, or None if the synthesis failed
-
synthesizeCombinations
(reagent_sources, start=0, stop=None)¶ A generator of all combinations (or a slice thereof) of products that can be synthesized by the route using the given sets of starting materials.
Parameters: - reagent_sources (iterable of ReagentSource or iterable of list of Mol.) – List of reagent sources.
- start (int) – Start yielding from this reagent combination index (counting from zero).
- stop (int or NoneType) – Stop yielding when this product reagent combination index is reached. None means unlimited.
Returns: Generator of synthesis products.
Return type: generator of Mol
-
synthesizeRandomSample
(reagent_sources, size, max_tries=None, seed=None)¶ Generate a random sample of unique products. This function works by repeatedly choosing a random reagent from each reagent source and keeping track of which products have been seen so far. Therefore, it is most efficient when only a fraction of the possible space of reagent combinations is sampled, because otherwise there may be many unproductive duplicates. Also, since it keeps the set of products seen so far, it uses memory proportional to ‘size’, so it is best if that number is not too large (thousands is fine).
Parameters: - reagent_sources (iterable of ReagentSource or iterable of list of Mol.) – List of reagent sources
- max_tries (int) – maximum number of random attempts to make before giving up, even if not enough products have been synthesized. The default is a function of ‘size’ and the number of reactant combinations.
- seed (int) – random seed. If None, a random random seed will be generated and logged as an info message.
Param: size: desired sample size (number of products).
-
-
class
schrodinger.analysis.reaction.
RouteRenderer
(plus_size=50, arrow_length=250, arrowhead_size=25, label_padding=25, label_font=('helvetica', 30), plus_font=('helvetica', 48), max_scale=0.3, mol_padding=50, scale=0.375)¶ Bases:
object
A class for rendering a Route as an image.
Note: this class only supports “specific routes”, for which all nodes have a ‘mol’ attribute. Generic routes, in which nodes might have only a reagent class, are not supported yet.
-
renderToFile
(route, filename)¶ Render a Route and save the image to a file.
-
-
schrodinger.analysis.reaction.
mol_to_qpicture
(mol)¶ Generate a QPicture from an RDKit Mol.
Parameters: mol – molecule to render Return type: QPicture
-
class
schrodinger.analysis.reaction.
Reaction
(name, smarts=None, lhs_classes=None, rhs_classes=None, inverse_smarts=None, tags=None, tier=None, allow_multiple_products=False, rxnfile=None, inverse_rxnfile=None, description=None, reagents_dict=None, long_name=None, ld_data=None)¶ Bases:
object
A Reaction object represents a generic reaction, such as “amide coupling”. An actual instance of a reaction between specific reagents is a ReactionInstance (see below).
A Reaction may optionally be associated with “reagent classes” for the molecules on the left-hand-side and right-hand-side of the reaction. We avoid the terms “reactants” and “products” because they depend on the context; Reaction objects may be used for actual reactions, but also for retrosynthetic transforms (where “target” and “precursors” would be more appropriate), or for even for alchemical transformations.
A reagent class is just a name representing the type of compound involved in the reaction; the naming scheme is up to the caller. For example, for amide coupling, the reagent classes on one side might be “amine” and “acid”, and on the other “amide”.
-
asDict
()¶ Return a dict representation of the reaction suitable for JSON serialization.
Returns: dict Return type: dict
-
apply
(reactants)¶ Apply the reaction to the given reactants, returning a list of lists of products. The products are already sanitized.
Return type: list of list of Mol
-
inverse
()¶ Return a new Reaction object for the inverse of the current reaction.
-
suggestReagentClasses
(r_classes, libpath=None, max_search=10)¶ Search through r_classes for reagent classes matching each of the reactants in the reaction.
Parameters: - r_classes (dict of ReagentClass) – dictionary of reagent classes by name
- libpath (list of str) – list of directories to prepend to the standard reagent library search path
- max_search (int) – maximum number of structures to search from each reagent file before deciding the file doesn’t match.
Returns: list of lists of reagent classes. Each item in the outer list corresponds to one of the molecules on the right-hand side of the reaction. Items in the inner list (which may be empty) are the suggested classes for that molecule.
Return type: list of list of ReagentClass
-
-
class
schrodinger.analysis.reaction.
ReactionInstance
(reaction, precursors)¶ Bases:
object
A ReactionInstance is the application of a Reaction to a list of reagents/precursors. For example, “Amide syntesis” is a Reaction; but “Amide synthesis from acetic acid and ethylamine” is a ReactionInstance.
-
name
¶ The name of the reaction.
-
-
schrodinger.analysis.reaction.
read_route_file
(filename, reactions_dict)¶ Read a route file in JSON format, returning a RouteNode object.
Parameters: reactions_dict (dict of {str: Reaction}) – dictionary of Reaction objects by name.
-
schrodinger.analysis.reaction.
parse_route_data
(json_data, reactions_dict)¶ Generate a Route from the raw dict/list-based data structure usually obtained from a route JSON file.
Parameters: reactions_dict (dict of {str: Reaction}) – dictionary of Reaction objects by name.
-
schrodinger.analysis.reaction.
read_reactions_file
(filename=None, reagents_dict=None, merge_with_defaults=False)¶ Read a reactions file in JSON format and return a dictionary of reactions by name. If filename is None, read the standard reactions file from the mmshare data directory and add the ‘default’ tag to each reaction.
Parameters: - filename (str) – file to read
- reagents_dict (dict {str: ReagentClass}) – dictionary of reagent classes (normally from read_reagent_classes_file).
- merge_with_defaults (bool) – if True, read both the default file and the specified file and merge them.
Returns: dictionary of reactions by name
Return type: dict {str: Reaction}
-
schrodinger.analysis.reaction.
read_reagent_classes_file
(filename=None, merge_with_defaults=False)¶ Read a reagent classes file in JSON format and return a dictionary of reagent classes by name. If filename is None, read the standard reagent classes file from the mmshare data directory.
Parameters: - filename (str) – file to read
- merge_with_defaults (bool) – if True, read both the default file and the specified file and merge them.
Returns: dictionary of reagent classes by name
Return type: dict {str: ReagentClass}
-
schrodinger.analysis.reaction.
get_custom_reagent_classes
(filename)¶ Reads a reagent classes file in JSON format and returns a dictionary of reagent classes by name. If custom reagent classes file is not found it will be created.
Parameters: filename – file to read Returns: dictionary of reagent classes Return type: dict {str: ReagentClass}
-
schrodinger.analysis.reaction.
get_default_reactions_filename
()¶ Return the path to the default reactions file.
Returns: path Return type: str
-
schrodinger.analysis.reaction.
get_default_reagent_classes_filename
()¶ Return the path to the default reagent classes file.
Returns: path Return type: str
-
schrodinger.analysis.reaction.
parse_reaction_data
(raw_dict, reagents_dict=None)¶ Convert a “raw dict” (usually from a JSON file) into a dictionary of Reaction objects by name.
Return type: dict {str: Reaction}
-
schrodinger.analysis.reaction.
parse_reagent_classes_data
(raw_dict)¶ Convert a “raw dict” (usually from a JSON file) into a dictionary of ReagentClass objects by name.
Return type: dict {str: Reaction}
-
schrodinger.analysis.reaction.
write_reactions_file
(reactions, filename)¶ Write a reactions file. :reactions: list or dict of reactions :type reactions: list or dict of Reaction
-
schrodinger.analysis.reaction.
write_reagent_classes_file
(reagent_classes, filename)¶ Write a reagent classes file.
Parameters: - reagent_classes – dict of reagent classes by name
- filename (str) – file to write
-
schrodinger.analysis.reaction.
invert_reaction_smarts
(smarts)¶ Given a reaction SMARTS, return the reaction SMARTS for the reverse reaction.
Return type: str
-
schrodinger.analysis.reaction.
reaction_is_excluded
(reaction, exclude)¶ Check if a reaction meets any of the exclusion criteria.
Parameters: exclude (set of str) – Set of tags or reaction names to exclude. Tags are matched exactly; names are matched using shell globbing (*, ?, []). Returns: True if reaction is meets any of the exclusion criteria Return type: bool
-
schrodinger.analysis.reaction.
filter_reactions
(reactions, exclude)¶ Return a shallow copy of a list of reactions, filtering out those matching any of the exclusion criteria.
Parameters: exclude (set of str) – Set of tags or reaction names to exclude. Tags are matched exactly; names are matched using shell globbing (*, ?, []). Return type: list of reaction
-
schrodinger.analysis.reaction.
retrosynthesize
(target_mol, reactions_dict, max_depth=1, exclude=None)¶ Generate a retrosynthetic tree to the desired depth based on the target molecule.
Parameters: - reactions_dict (dict {str: Reaction}) – Reaction dictionary by name.
- exclude (set of str) – Set of tags or reaction names to exclude. Tags are matched exactly; names are matched using shell globbing (*, ?, []).
Return type: RetrosynthesisNode
-
schrodinger.analysis.reaction.
debug_mols
(message, mols, separator=' + ')¶ Print a debug message (if the logger level indicates it), appending a list of SMILES representation of the molecules.
-
class
schrodinger.analysis.reaction.
ReagentSource
(mols, size, filename, reagent_class)¶ Bases:
tuple
A reagent source is just a tuple which associates the following: - mols: a generator or list of Mol objects; - size: (int) the number of molecules in mols; - filename: (str) file where the mols came from (or as a special case, “SMILES”
if they came from the SMILES list hard-coded into the route);- reagent_class: (str) name of the reagent class.
-
filename
¶ Alias for field number 2
-
mols
¶ Alias for field number 0
-
reagent_class
¶ Alias for field number 3
-
size
¶ Alias for field number 1
-
schrodinger.analysis.reaction.
get_reagent_sources
(route, r_dict=None, random_access=False, libpath=None)¶ Return a list of reagent sources given a route and a dictionary of reagent filenames (each key is either a reagent index or reagent class name; each value is a filename).
Parameters: - random_access (bool) – should the reagent sources sources support random access in addition to iteration, possibly costing memory or speed?
- libpath (list of str) – list of directories to prepend to the standard reagent library search path
Returns: reagent sources
Return type: list of ReagentSource
-
schrodinger.analysis.reaction.
get_mol_reader
(filename, random_access=False, skip_bad=True)¶ Return a Mol generator given a filename. For SMILES, use the RDKit SmilesMolSupplier; for other formats, use StructureReader but convert Structure to Mol before yielding each molecule.
Parameters: - random_access (bool) – should the reagent sources sources support random access in addition to iteration, possibly costing memory or speed?
- skip_bad (bool) – if True, bad structures are skipped implicitly, instead of being yielded as None (only applies to SMILES and CSV formats.)
Return type: generator of Mol
-
schrodinger.analysis.reaction.
adapt_st_reader
(reader)¶ Generate RDKit Mol objects given a StructureReader-like object. Structures which cause conversion errors are skipped but a warning is logged.
Parameters: reader (iterable of Structure) – source of structures to convert Returns: converted RDKit molecule objects Return type: generator of Mol
-
schrodinger.analysis.reaction.
create_reagent_library
(name, input_file, smarts, id_property='s_m_title')¶ Create a reagent library file for one reagent class by searching through an input file using the provided SMARTS pattern. Writes a file called <name>.csv.
Parameters: - name (str) – name of the reagent class.
- input_file (str) – name of structure file to process
- smarts (str) – SMARTS pattern to search
- id_property (str) – name of structure property to store as the title of each reagent.
Returns: output filename
Return type: str
-
schrodinger.analysis.reaction.
get_title
(mol, prop='s_m_title')¶ Return the title of a molecule. Looks for the property with the name specified by ‘prop’ but falls back to known aliases such as “NAME”.
Parameters: - mol (Chem.Mol) – input molecule
- prop (str) – title property name
Returns: molecule title, or None if unspecified
Return type: str or NoneType
-
schrodinger.analysis.reaction.
is_reagent_file
(filename)¶ Check whether a given file is usable as a reagent file. A reagent file must be a structure file and the structures need to have a title. In the case of a csv file, we look for columns named ‘s_m_title’ or ‘NAME’.
Parameters: filename (str) – filename Returns: True if the file is a reagent file Return type: bool
-
schrodinger.analysis.reaction.
get_one_step_route
(rxn)¶ Create a route for a one-step synthesis given a Reaction.
Parameters: rxn (Reaction) – reaction object Returns: new route object Return type: RouteNode
-
schrodinger.analysis.reaction.
get_libpath
()¶ Return the reagent library search path. It consists of the directories listed in the SCHRODINGER_REAGENT_LIB environment variable, if it exists, followed by ~/.schrodinger/reagents or its Windows equivalent, followed by the mmshare data/reagents directory.
Return type: list of str