GitHub Copilot RDkit


In [1]:
from rdkit import Chem
from rdkit.Chem import AllChem as Chem
In [2]:
# SMILES of aspirin
aspirin = 'CC(=O)Oc1ccccc1C(=O)O'
# aspirin to mol
mol = Chem.MolFromSmiles(aspirin)
mol
Out[2]:
In [3]:
# Calculate the molecular weight of aspirin
Chem.CalcExactMolWt(mol)
Out[3]:
180.042258736
In [4]:
# Print atomic symbol and indices of atoms in molecule mol
for atom in mol.GetAtoms():
    print(atom.GetSymbol(), atom.GetIdx())
C 0
C 1
O 2
O 3
C 4
C 5
C 6
C 7
C 8
C 9
C 10
O 11
O 12
In [11]:
# Create a new molecule and replace the atom at index 0 with a new atom
new_mol = Chem.RWMol(mol)
new_mol.ReplaceAtom(3, Chem.Atom('S'))
new_mol
Out[11]:
In [12]:
def create3Dmol(mol):
    """Create a 3D molecule from a 2D molecule"""
    # Add Explicit Hydrogens
    mol = Chem.AddHs(mol)
    # Create a 3D molecule from a 2D molecule
    Chem.EmbedMolecule(mol)
    # Optimize the geometry of the molecule
    Chem.MMFFOptimizeMolecule(mol)
    return mol
In [13]:
create3Dmol(new_mol)
[15:25:20] 

****
Pre-condition Violation
getNumImplicitHs() called without preceding call to calcImplicitValence()
Violation occurred on line 299 in file /Users/runner/miniforge3/conda-bld/rdkit_1657061307449/work/Code/GraphMol/Atom.cpp
Failed Expression: d_implicitValence > -1
****

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
/Users/mhelvath/Desktop/5MinOfFame-2.ipynb Zelle 7 in <cell line: 1>()
----> <a href='vscode-notebook-cell:/Users/mhelvath/Desktop/5MinOfFame-2.ipynb#W6sZmlsZQ%3D%3D?line=0'>1</a> create3Dmol(new_mol)

/Users/mhelvath/Desktop/5MinOfFame-2.ipynb Zelle 7 in create3Dmol(mol)
      <a href='vscode-notebook-cell:/Users/mhelvath/Desktop/5MinOfFame-2.ipynb#W6sZmlsZQ%3D%3D?line=1'>2</a> """Create a 3D molecule from a 2D molecule"""
      <a href='vscode-notebook-cell:/Users/mhelvath/Desktop/5MinOfFame-2.ipynb#W6sZmlsZQ%3D%3D?line=2'>3</a> # Add Explicit Hydrogens
----> <a href='vscode-notebook-cell:/Users/mhelvath/Desktop/5MinOfFame-2.ipynb#W6sZmlsZQ%3D%3D?line=3'>4</a> mol = Chem.AddHs(mol)
      <a href='vscode-notebook-cell:/Users/mhelvath/Desktop/5MinOfFame-2.ipynb#W6sZmlsZQ%3D%3D?line=4'>5</a> # Create a 3D molecule from a 2D molecule
      <a href='vscode-notebook-cell:/Users/mhelvath/Desktop/5MinOfFame-2.ipynb#W6sZmlsZQ%3D%3D?line=5'>6</a> Chem.EmbedMolecule(mol)

RuntimeError: Pre-condition Violation
	getNumImplicitHs() called without preceding call to calcImplicitValence()
	Violation occurred on line 299 in file Code/GraphMol/Atom.cpp
	Failed Expression: d_implicitValence > -1
	RDKIT: 2022.03.4
	BOOST: 1_74

Obviously, this is not an adequate way to create new_mols; quick fix and sanitize the mol

In [18]:
# Sanitize new_mol
Chem.SanitizeMol(new_mol)
create3Dmol(new_mol)
Out[18]: