'Obtain information about bonded neighbors to all atoms in a molecule in python

I am coding in python a script and I need to obtain from a PDB file information about what atoms are bonded with each other in an automatized way.

For example, if we had ethane obtain a dictionary as such

{'C1':['H1','H2','H3','C2'],'C2':['C1','H4','H5','H6'], 'H1':[C1], 'H2':[C1], 'H3':[C1], 'H4':[C2], 'H5':[C2], 'H6':[C2]}

I have been searching libraries like RDkit and others but I have not been able to find a solution. Any help would be appreciated. Thanks!



Solution 1:[1]

I came up with an idea that solves the problem (maybe not in the most efficient way):

atoms = []

with open(path_ligand) as filein:
  for line in filein:
    if residue_name in line:
      atom = line.split()[2]                    
      atoms.append(atom)

m = Chem.rdmolfiles.MolFromPDBFile(path_ligand)
heavy_atoms = [k for k in atoms if 'H' not in k]

neighbours_dict = {}

for atom in range(len(heavy_atoms)):
  neighbours_dict[heavy_atoms[atom]] = [atoms[x.GetIdx()] for x in m.GetAtomWithIdx(atom).GetNeighbors()]

First I create a list of all the heavy atoms to then iterate over them in the "for" loop. With rdkit.Chem functions of GetIdx(), GetAtomWithIdx() and GetNeighbors() I have been able to create the result I wanted.

In the case of PDB 1T3R:

{'N1': ['C1'], 'C1': ['N1', 'C2', 'C6'], 'C2': ['C1', 'C3'], 'C3': ['C2', 'C4'], 'C4': ['C3', 'C5', 'S1'], 'C5': ['C4', 'C6'], 'C6': ['C1', 'C5'], 'S1': ['C4', 'O1', 'O2', 'N2'], 'O1': ['S1'], 'O2': ['S1'], 'N2': ['S1', 'C7', 'C11'], 'C7': ['N2', 'C8'], 'C8': ['C7', 'C9', 'C10'], 'C9': ['C8'], 'C10': ['C8'], 'C11': ['N2', 'C12'], 'C12': ['C11', 'O3', 'C13'], 'O3': ['C12'], 'C13': ['C12', 'N3', 'C21'], 'N3': ['C13', 'C14'], 'C14': ['N3', 'O4', 'O5'], 'O4': ['C14'], 'O5': ['C14', 'C15'], 'C15': ['O5', 'C16', 'C20'], 'C16': ['C15', 'O6'], 'O6': ['C16', 'C17'], 'C17': ['O6', 'O7', 'C20'], 'O7': ['C17', 'C18'], 'C18': ['O7', 'C19'], 'C19': ['C18', 'C20'], 'C20': ['C17', 'C19', 'C15'], 'C21': ['C13', 'C27'], 'C22': ['C23', 'C27'], 'C23': ['C22', 'C24'], 'C24': ['C23', 'C25'], 'C25': ['C24', 'C26'], 'C26': ['C25', 'C27'], 'C27': ['C21', 'C26', 'C22']}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ignasi