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]:
In [4]:
# Print atomic symbol and indices of atoms in molecule mol
for atom in mol.GetAtoms():
print(atom.GetSymbol(), atom.GetIdx())
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)
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]: