Python Progam to read SDF (chemistry) file

Question:

I want to read sdf file (containing many molecules) and return the weighted adjacency matrix of the molecule. Atoms should be treated as vertices and bond as edges. If i and j vertex are connected by single, double, or triple bond then corresponding entries in the adjacency matrix should be 1,2, and 3 respectively. I need to further obtain a distance vector for each vertex which list the number of vertices at different distance.

Are there any python package available to do this?

Asked By: DurgaDatta

||

Answers:

See

http://code.google.com/p/cinfony/

However for your exact problem you will need to consult the documentation.

Answered By: beginner_

I would recommend Pybel for reading and manipulating SDF files in Python. To get the bonding information, you will probably need to also use the more full-featured but less pythonic openbabel module, which can be used in concert with Pybel (as pybel.ob).

To start with, you would write something like this:

import pybel

for mol in pybel.readfile('sdf', 'many_molecules.sdf'):
  for atom in mol:
    coords = atom.coords
    for neighbor in pybel.ob.OBAtomAtomIter(atom.OBAtom):
      neighbor_coords = pybel.atom(neighbor).coords
Answered By: vamin

Why not use the most commonly used package rdkit to process the SDF chemical file? It is really fast and convenient.

suppl = Chem.SDMolSupplier('xxx.sdf') # name the path of the SDF file 
for mol in supply:
    print(mol.GetNumAtoms()) # give the number of atoms in each molecule
    ......                   # do whatever kind of operations you want 
Answered By: Fang WU
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.