RDKit - read/write
Read a molecule
from rdkit import Chem
m = Chem.MolFromSmiles('Cc1ccccc1')
m = Chem.MolFromMolFile('data/input.mol')
mol_str=open('data/input.mol','r').read()
m = Chem.MolFromMolBlock(mol_str)
Read molecule will return <rdkit.Chem.rdchem.Mol object at 0x...>
or None
Read molecules
mols = Chem.SDMolSupplier('data/5ht3ligs.sdf')
f = open('data/5ht3ligs.sdf','rb')
mols = Chem.ForwardSDMolSupplier(f)
for mol in mols:
print(mol.GetNumAtoms())
Write molecule
m = Chem.MolFromMolFile('data/chiral.mol')
Chem.MolToSmiles(m) # 'C[C@H](O)c1ccccc1'
Chem.MolToSmiles(m,isomericSmiles=False) # 'CC(O)c1ccccc1'
Chem.MolToMolBlock(m2)
generic SMILES = no chiral or isotopic info
isomeric SMILES = with chiral & isotopic info
unique SMILES = canonicalized generic SMILES
absolute SMILES = unique isomeric SMILES
Output SMILES is canonical.
Molfile with coord
# 2D coordinates
from rdkit.Chem import AllChem
AllChem.Compute2DCoords(m)
# 4 4 0 0 0 0 0 0 0 0999 V2000
# 1.0607 -0.0000 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
# -0.0000 -1.0607 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
# -1.0607 0.0000 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
# 0.0000 1.0607 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
# 3D coordinates
m = Chem.AddHs(m) # To get good 3D conformations, it’s almost always a good idea to add hydrogens to the molecule first
AllChem.EmbedMolecule(m)
m = Chem.RemoveHs(m)
# 4 4 0 0 0 0 0 0 0 0999 V2000
# 0.3497 0.9755 -0.2202 C 0 0 0 0 0 0 0 0 0 0 0 0
# 0.9814 -0.3380 0.2534 C 0 0 0 0 0 0 0 0 0 0 0 0
# -0.3384 -1.0009 -0.1474 C 0 0 0 0 0 0 0 0 0 0 0 0
# -0.9992 0.3532 0.1458 C 0 0 0 0 0 0 0 0 0 0 0 0
Chem vs AllChem
speed startup and lower import times
rdkit.Chem
= basic func (reading/writing, substructure searching, cleanup, etc)
rdkit.Chem.AllChem
= advanced
Last updated
Was this helpful?