Skip to content

Instantly share code, notes, and snippets.

@jhjensen2
Last active September 3, 2017 12:26
Show Gist options
  • Save jhjensen2/17a7c521352c6924eec6a2f35fd30860 to your computer and use it in GitHub Desktop.
Save jhjensen2/17a7c521352c6924eec6a2f35fd30860 to your computer and use it in GitHub Desktop.
create molecules using RDKit
from rdkit import Chem
from rdkit.Chem import AllChem
parent_smiles = 'c1ccccc1'
rxn_smarts_list = ['[cX3;H1:1]>>[*:1]F','[cX3;H1:1]>>[*:1]O']
molecule_substitutions = {}
molecules = []
mol = Chem.MolFromSmiles(parent_smiles)
molecule_substitutions[0] = [mol]
molecules.append(mol)
substitutions = 6
for i in range(1,substitutions+1):
molecule_substitutions[i] = []
smiles_list = []
for mol in molecule_substitutions[i-1]:
for rxn_smarts in rxn_smarts_list:
rxn = AllChem.ReactionFromSmarts(rxn_smarts)
new_mols = rxn.RunReactants((mol,))
for new_mol in new_mols:
new_smiles = Chem.MolToSmiles(new_mol[0],isomericSmiles=True)
if new_smiles not in smiles_list:
smiles_list.append(new_smiles)
molecule_substitutions[i].append(Chem.MolFromSmiles(new_smiles))
molecules += molecule_substitutions[i]
# Write 3D coordinates in sdf format
folder = "/Users/jan/Desktop/"
for i, mol in enumerate(molecules):
mol = Chem.AddHs(mol)
AllChem.EmbedMolecule(mol)
file = folder+"comp"+str(i)+".sdf"
writer = Chem.SDWriter(file)
writer.write(mol)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment