Join two sub-structures.
import rdkit
from rdkit import Chem
from rdkit.Chem.Draw import IPythonConsole
from rdkit.Chem import Draw
import numpy as np
rdkit.__version__
'2018.03.4'
def to_mol(rdmol):
return Chem.MolFromSmiles(Chem.MolToSmiles(Chem.Mol(rdmol)))
def add_fragment(origin,ix_o,fragment,ix_f):
editable = Chem.RWMol(origin)
n_atoms = len(editable.GetAtoms())
for a in fragment.GetAtoms():
# import atoms
editable.AddAtom(Chem.Atom(a.GetAtomicNum()))
bonds = list(zip(*np.where(Chem.GetAdjacencyMatrix(fragment))))
for i,j in bonds:
# import bonds
i=int(i);j=int(j)
if i<j:
bond = fragment.GetBondBetweenAtoms(i,j)
editable.AddBond(i+n_atoms,j+n_atoms,
bond.GetBondType())
editable.AddBond(ix_o,n_atoms+ix_f,Chem.BondType.SINGLE)
return to_mol(editable)
from rdkit.Chem.Draw.MolDrawing import DrawingOptions
def molview(mole):
"""Function to check atom indices in a molecule"""
DrawingOptions.includeAtomNumbers =True
view = Chem.Draw.MolToImage(mole)
DrawingOptions.includeAtomNumbers =False
return view
toluene = Chem.MolFromSmiles('Cc1ccccc1')
molview(toluene)
benzene = Chem.MolFromSmiles('c1ccccc1')
add_fragment(toluene,4,benzene, 0)
add_fragment(toluene,3,benzene,0)
add_fragment(toluene,2,benzene,0)