-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reimplemented RDConverter tagging with register_abstract_class_attr()
- Loading branch information
Showing
1 changed file
with
10 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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''' | ||
|
@@ -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] |