Source code for schrodinger.livedesign.convert
import enum
from rdkit import Chem
from schrodinger.thirdparty import rdkit_adapter
[docs]def convert(data: str, input_format: Format, output_format: Format) -> str:
"""
:param data: input text string
:param input_format: expected format for input string
:param output_format: desired format for output string
:return: converted text string
"""
str_to_mol = {
Format.SDF: sdf_to_rdkit,
Format.SMILES: Chem.MolFromSmiles,
Format.CXSMILES: Chem.MolFromSmiles,
}
mol_to_str = {
Format.SDF: rdkit_to_sdf,
Format.SMILES: Chem.MolToSmiles,
Format.CXSMILES: rdkit_to_cxsmiles,
}
with rdkit_adapter.convert_log_to_exception():
mol = str_to_mol[input_format](data)
return mol_to_str[output_format](mol)
[docs]def get_sd_reader(molblock: str) -> Chem.SDMolSupplier:
"""
:param molblock: given SDF molblock
:return: iterator through each mol in the given SDF data
"""
reader = Chem.SDMolSupplier()
reader.SetData(molblock,
sanitize=False,
removeHs=False,
strictParsing=False)
return reader
[docs]def sdf_to_rdkit(molblock: str) -> Chem.rdchem.Mol:
"""
:param molblock: given SDF molblock
:return: corresponding RDKit mol
"""
reader = get_sd_reader(molblock)
molblock_count = len(reader)
if molblock_count != 1:
raise RuntimeError(
f"Single molblock required; {molblock_count} present")
with rdkit_adapter.convert_log_to_exception():
return next(reader)
[docs]def rdkit_to_sdf(mol: Chem.rdchem.Mol) -> str:
"""
:param mol: given RDKit mol
:return: corresponding SDF molblock
"""
return Chem.SDWriter.GetText(mol, force_v3000=True)
[docs]def rdkit_to_cxsmiles(mol: Chem.rdchem.Mol) -> str:
"""
:param mol: given RDKit mol
:return: corresponding CXSMILES stripped of coordinates
"""
mol.RemoveAllConformers() # remove coordinates from CXSMILES
return Chem.MolToCXSmiles(mol)