Search code examples
pythondistancepointbiopythonprotein-database

Biopython PDB: calculate distance between an atom and a point


Using a typical pdb file, I am able to calculate the distance between two atoms in a structure using a method similar to that presented in the Biopython documentation. Shown here:

from Bio import PDB
parser = PDB.PDBParser()

pdb1 ='./4twu.pdb' 
structure = parser.get_structure("4twu", pdb1) 
model = structure[0] 
chain = model['A'] 
residue1 = chain[1] 
residue2 = chain[2]
atom1 = residue1['CA'] 
atom2 = residue2['CA'] 

distance = atom1-atom2 

print(distance)

Is there a method within Biopython to calculate the distance from an atom to a fixed point with xyz coordinates? If not Biopython, what would be the best method to solve this problem? Thanks.


Solution

  • Your atom1 object stores the coordinates in the coord attribute (it's a NumPy array), e.g.

    >>> print(atom1.coord)
    [ 26.26600075  25.41300011   2.84200001]
    

    If you want to calculate the distance to another point in space you need to define it in a similar format.

    import numpy
    x = 1
    y = 1
    y = 1
    v = numpy.array((x, y, z), dtype=float)
    

    and you can then calculate the distance with NumPy.

    distance = numpy.linalg.norm(atom1.coord - v)