Skip to content

Commit

Permalink
Reimplemented RDConverter tagging with register_abstract_class_attr()
Browse files Browse the repository at this point in the history
  • Loading branch information
timbernat committed Dec 4, 2024
1 parent 31f79e3 commit 6ecbf9a
Showing 1 changed file with 10 additions and 19 deletions.
29 changes: 10 additions & 19 deletions polymerist/rdutils/rdconvert.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,20 @@
__author__ = 'Timotej Bernat'
__email__ = '[email protected]'

from abc import ABC, abstractmethod, abstractproperty
from abc import ABC, abstractmethod
from rdkit import Chem
from rdkit.Chem.rdchem import Mol

from ..genutils.decorators.classmod import register_subclasses
from ..genutils.decorators.classmod import register_subclasses, register_abstract_class_attrs

from .labeling.bijection import bijective_atom_id_iter
from .rdprops import copy_rd_props


@register_subclasses(key_attr='TAG')
@register_abstract_class_attrs('TAG')
class RDConverter(ABC): # TODO : add some optional sanitization measures to ensure valid output and bijection
'''For converting an existing RDKit Molecule to and from a particular format to gain new properties'''
@abstractproperty
@classmethod
def TAG(cls):
pass

@abstractmethod
def _convert(self, rdmol : Mol) -> Mol:
'''Implement conversion mechanism here'''
Expand All @@ -41,34 +37,29 @@ def convert(self, rdmol : Mol, sanitize : bool=True) -> Mol:

return newmol

class SMARTSConverter(RDConverter):
TAG = 'SMARTS'
class SMARTSConverter(RDConverter, TAG='SMARTS'):
def _convert(self, rdmol : Mol) -> Mol:
return Chem.MolFromSmarts(Chem.MolToSmarts(rdmol))

class SMILESConverter(RDConverter):
TAG = 'SMILES'
class SMILESConverter(RDConverter, TAG='SMILES'):
def _convert(self, rdmol : Mol) -> Mol:
return Chem.MolFromSmiles(Chem.MolToSmiles(rdmol), sanitize=False)

class CXSMARTSConverter(RDConverter):
class CXSMARTSConverter(RDConverter, TAG='CXSMARTS'):
'''Similar to SMARTSConverter but preserves the 3D structure'''
TAG = 'CXSMARTS'
def _convert(self, rdmol : Mol) -> Mol:
return Chem.MolFromSmarts(Chem.MolToCXSmarts(rdmol))

class CXSMILESConverter(RDConverter):
class CXSMILESConverter(RDConverter, TAG='CXSMILES'):
'''Similar to SMILESConverter but preserves the 3D structure'''
TAG = 'CXSMILES'
def _convert(self, rdmol : Mol) -> Mol:
return Chem.MolFromSmiles(Chem.MolToCXSmiles(rdmol), sanitize=False)

class InChIConverter(RDConverter): # TOSELF : this does not preserve atom map num ordering (how to incorporate AuxInfo?)
TAG = 'InChI'
class InChIConverter(RDConverter, TAG='InChI'):
# TOSELF : this does not preserve atom map num ordering (how to incorporate AuxInfo?)
def _convert(self, rdmol : Mol) -> Mol:
return Chem.AddHs(Chem.MolFromInchi(Chem.MolToInchi(rdmol), removeHs=False, sanitize=False))

class JSONConverter(RDConverter):
TAG = 'JSON'
class JSONConverter(RDConverter, TAG='JSON'):
def _convert(self, rdmol : Mol) -> Mol:
return Chem.rdMolInterchange.JSONToMols(Chem.MolToJSON(rdmol))[0]

0 comments on commit 6ecbf9a

Please sign in to comment.