2
$\begingroup$

How to find the matrix of Bond lengths between atoms in an hydrogen depleted molecule given the smiles with explicit hydrogen=true. Also how to find the geometric distance between atoms in an hydrogen depleted molecule given the smiles with explicit hydrogen=true.

# -*- coding: utf-8 -*-
"""
Created on Wed Nov 16 09:37:44 2022

@author: Sriram
"""
import matplotlib.pyplot as plt
from pysmiles import read_smiles
from rdkit.Chem import AllChem as Chem
from rdkit.Chem import rdMolTransforms
from rdkit.Chem.Draw import rdMolDraw2D, rdDepictor, IPythonConsole
rdDepictor.SetPreferCoordGen(True)
from IPython.display import Image
import networkx as nx
import numpy as np
from scipy import sparse
smiles = 'CCC(C)CC(C)C'
import sys
import numpy
numpy.set_printoptions(threshold=sys.maxsize)
# 3D for lengths
mol3d = Chem.MolFromSmiles(smiles)
Chem.EmbedMolecule(mol3d, randomSeed=42)
Chem.MMFFOptimizeMolecule(mol3d)

# 2D for depiction
mol2d = Chem.MolFromSmiles(smiles)
rdDepictor. Compute2DCoords(mol2d)

bonds = [(x.GetBeginAtomIdx(), x.GetEndAtomIdx()) for x in mol3d.GetBonds()]
mol = read_smiles(smiles,explicit_hydrogen=False)
#WNM=nx.to_numpy_matrix(mol)
#print(WNM)
m=np.array(nx.to_numpy_matrix(mol))
#G=nx.from_numpy_array(m)
#G.edges(data=True)
#print(nx.number_of_edges(G))
#A=sparse.csc_matrix(nx.to_numpy_matrix(mol))
#print(A)
#G=nx.Graph(A)

#print(nx.adjacency_matrix(G))

conf = mol3d.GetConformer()

b_lengths = []

for b in bonds:
    bl = rdMolTransforms.GetBondLength(conf,b[0],b[1])
    b_lengths.append(str(round(bl,2)))
    m[b[0]][b[1]]=round(bl,2)
    m[b[1]][b[0]]=round(bl,2)
G = nx.from_numpy_matrix(np.matrix(np.asmatrix(m)))
layout = nx.spring_layout(G)
nx.draw(G, layout, node_size=1000, with_labels=True, font_weight='bold',    font_size=15)
labels = nx.get_edge_attributes(G,'weight')
nx.draw_networkx_edge_labels(G,pos=layout,edge_labels=labels)
plt.show()
d = rdMolDraw2D.MolDraw2DCairo(600, 400)
for n in range(len(b_lengths)):
    mol2d.GetBondWithIdx(n).SetProp('bondNote', b_lengths[n])
A=np.asmatrix(m)
#K=np.asmatrix(m)
#DimK=K.shape
#print(f"{'['}")
#for i in range(DimK[0]+1):
 #   print(f"{'['}")
  #  for j in range(DimK[0]+1):
   #     namek=K[i][j]
    #    print(f"{namek},\n")
    #print(f"{']'}")
for i in range(len(A)):
        flag=0
        if (i==0):
            print("[[", end ="")
        else:
            print("[", end ="")
        for j in range(len(A)):
            if (flag==0):
                print("%.2f,"%(A[i,j]), end ="")
                flag=1
            else:
                print("%.2f,"%(A[i,j]), end ="")
        if (i==len(A)-1):
            print("]]")
        else:
            print("]")

I don't know if my code is correct for my bond length problem Similarly how to to do for geometric distance kind help. Here i want to do with explicit hydrogen=true I believe my code might result in wrong answer can some help validate and help please kind kind help as I am new to rdkit package.

$\endgroup$
2
  • 1
    $\begingroup$ This seems like two separate questions. I think you want some sort of list of bond lengths? I think you also want to generate the $N \times N$ interatomic distance matrix? Is that correct? $\endgroup$ Dec 30, 2022 at 0:52
  • $\begingroup$ Can you clarify what is your question? Also, can you clean up/comment your code? $\endgroup$
    – Greg
    Dec 31, 2022 at 19:14

0

Browse other questions tagged .