7
$\begingroup$

Given the SMILES of my molecule, how can I get a weighted adjacency matrix where the weights are the bond lengths, using the Rdkit package and Python?

$\endgroup$

1 Answer 1

9
$\begingroup$

Yes, this can be done using two different functions in RDkit, one for the adjacency matrix (A) and one for distance matrix (D). In this case, A*D is what you want.

from rdkit.Chem import MolFromSmiles, rdmolops
from rdkit.Chem import AllChem, AddHs

# SMILES for methanol
smi='CO'

# SMILES to mol
mol=MolFromSmiles(smi)

# Add hydrogens
mol=AddHs(mol)

# Print atom ordering
print(f'Ordering={[atom.GetSymbol() for atom in mol.GetAtoms()]}')

# Print the adjacency matrix
A=rdmolops.GetAdjacencyMatrix(mol)
print(f'A={A}')

# Make the distance matrix
AllChem.EmbedMolecule(mol) 
D=AllChem.Get3DDistanceMatrix(mol)

# The weighed adjacency matrix can be formed via
print(f'D={A*D}')

The output should look like this:

Ordering=['C', 'O', 'H', 'H', 'H', 'H']
A=[[0 1 1 1 1 0]
 [1 0 0 0 0 1]
 [1 0 0 0 0 0]
 [1 0 0 0 0 0]
 [1 0 0 0 0 0]
 [0 1 0 0 0 0]]
D=[[0.         1.40928192 1.10391551 1.10763937 1.12250832 0.        ]
 [1.40928192 0.         0.         0.         0.         0.97916277]
 [1.10391551 0.         0.         0.         0.         0.        ]
 [1.10763937 0.         0.         0.         0.         0.        ]
 [1.12250832 0.         0.         0.         0.         0.        ]
 [0.         0.97916277 0.         0.         0.         0.        ]]
$\endgroup$
1
  • 2
    $\begingroup$ Welcome! This is a great answer with code. Could you add an example of doing this on some molecule for the community to see what the output should look like? $\endgroup$ Feb 1, 2023 at 1:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .