Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
zhu0619 committed Sep 8, 2022
1 parent e631bae commit f12ed35
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
2 changes: 1 addition & 1 deletion datamol/reactions/_attachments.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def add_brackets_to_attachment_points(smi: str):
)


def convert_attach_to_isotope(mol_or_smi: Union[Chem.Mol, str], same_isotope:bool=True):
def convert_attach_to_isotope(mol_or_smi: Union[Chem.Mol, str], same_isotope:bool=False):
"""Convert attachment to isotope mapping"""
mol = dm.to_mol(mol_or_smi)
smiles = dm.to_smiles(mol)
Expand Down
29 changes: 19 additions & 10 deletions datamol/reactions/_reactions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from functools import singledispatch

from typing import Union
import numpy as np
from rdkit.Chem import rdChemReactions
from rdkit.Chem import AllChem
Expand All @@ -15,20 +15,22 @@ def from_smarts(rxn_smarts: str):
return rdChemReactions.ReactionFromSmarts(rxn_smarts)


def is_reaction_ok(rxn):
def is_reaction_ok(rxn: Union[str, rdChemReactions.ChemicalReaction]):
"""Check is the reaction is synthetically valid"""
if isinstance(rxn, str):
rxn = from_smarts(rxn_smarts=rxn)
return rdChemReactions.SanitizeRxn(rxn) == rdChemReactions.SanitizeFlags.SANITIZE_NONE


def compute_reaction_product(
out, single_output=True, rm_attach: bool = False, as_smiles: bool = False
out, single_output=True, rm_attach: bool = False, as_smiles: bool = False, sanitize: bool = True
):
"""Compute the products of a reaction"""
out = [dm.fix_mol(x[0], n_iter=0) for x in out]
if not single_output:
out = [dm.sanitize_mol(x) for x in out]
else:
out = [dm.sanitize_first(np.random.permutation(out))]
out = [x[0] for x in out]
if single_output:
out = np.random.choice(out, 1)
if sanitize:
out = [dm.sanitize_mol(m) for m in out]
if rm_attach:
out = [dm.remove_dummies(x) for x in out]
if as_smiles:
Expand All @@ -40,7 +42,6 @@ def compute_reaction_product(
# Might be a important to make a tradeoff decision in selecting products for greater speed.
# product = sorted(out, key=lambda x: MoleculeEnv.compute_reward_from_mol(x, True))[-1]
# sampling from list of products is an alternative
return out


def apply_reaction(
Expand All @@ -49,13 +50,21 @@ def apply_reaction(
single_output: bool = False,
as_smiles: bool = False,
rm_attach: bool = False,
disable_log: bool = True,
sanitize: bool = True,
):
"""Apply a chemical reaction on a molecule"""
if disable_log:
dm.disable_rdkit_log()
if not rxn.IsInitialized():
rxn.Initialize()
out = rxn.RunReactants(reactants)
return compute_reaction_product(
out=out, single_output=single_output, as_smiles=as_smiles, rm_attach=rm_attach
out=out,
single_output=single_output,
as_smiles=as_smiles,
rm_attach=rm_attach,
sanitize=sanitize,
)


Expand Down

0 comments on commit f12ed35

Please sign in to comment.