<p>If you need to process multiple files, you could use <a href="http://biopython.org/wiki/The_Biopython_Structural_Bioinformatics_FAQ" rel="noreferrer">Biopython</a> to parse a PDB structure.</p>



<pre class="lang-python prettyprint-override"><code>from Bio.PDB import PDBParser

# create parser
parser = PDBParser()

# read structure from file
structure = parser.get_structure('PHA-L', '1fat.pdb')

model = structure[0]
chain = model['A']

# this example uses only the first residue of a single chain.
# it is easy to extend this to multiple chains and residues.
for residue1 in chain:
    for residue2 in chain:
        if residue1 != residue2:
            # compute distance between CA atoms
            try:
                distance = residue1['CA'] - residue2['CA']
            except KeyError:
                ## no CA atom, e.g. for H_NAG
                continue
            if distance &lt; 6:
                print(residue1, residue2, distance)
        # stop after first residue
        break
</code></pre>

<p>If you need to look at one structure, using a viewer perhaps would be easier. You could try <a href="https://www.pymol.org/" rel="noreferrer">PyMOL</a>: (<a href="https://pymolwiki.org/index.php/Distance" rel="noreferrer">how to measure distance</a>). There are other PDB viewers, some of which can work even through a browser.</p>

CC BY-SA 3.0
2017-06-16T16:27:32.490Z
Dear lakov, I installed Anaconda and Biopython packages and run the above code in PyCharm 5.0 and got the following error:
Sara
CC BY-SA 3.0
2017-06-16T16:29:10.143Z
Traceback (most recent call last):
  File "C:/Users/VN7-592/PycharmProjects/untitled/sara.py", line 7, in <module>
    structure = parser.get_structure('PHA-L', '1FAT.pdb')
Sara
CC BY-SA 3.0
2017-06-16T16:29:18.203Z
File "C:\Users\VN7-592\Desktop\Anaconda\lib\site-packages\Bio\PDB\PDBParser.py", line 81, in get_structure
    with as_handle(file, mode='rU') as handle:
  File "C:\Users\VN7-592\Desktop\Anaconda\lib\contextlib.py", line 17, in __enter__
    return self.gen.next()
  File "C:\Users\VN7-592\Desktop\Anaconda\lib\site-packages\Bio\File.py", line 88, in as_handle
    with open(handleish, mode, **kwargs) as fp:
IOError: [Errno 2] No such file or directory: '1FAT.pdb'
Sara
CC BY-SA 3.0
2017-06-16T20:01:34.277Z
You don't have the 1FAT.pdb file in the working directory, or your operating system is case sensitive.
Ajasja
CC BY-SA 3.0
2017-06-17T19:36:42.770Z
But this gives only intermolecular distances? BioPython ignores crystal symmetry? In general, to check for contacts between residues X and Y one would need to calculate distance between X and the nearest symmetrical image of Y.
marcin
CC BY-SA 3.0
2017-06-16T14:22:17.763Z
2017-06-16T14:22:17.763Z
Iakov Davydov
<p>Could you use CCP4's NCONT program? There's a GUI and a command line interface, whatever suits. You can specify which chains you want to target and interact with and set a cut off for distance. The bonus here is once you're in you have a nice suite of other structural tools to use.</p>

<p>If you're just doing it once, the GUI is friendly enough to work things out, if you're doing a batch then you can run it across several files via the command line.</p>

<p><a href="http://www.ccp4.ac.uk/download/" rel="nofollow noreferrer">Download</a></p>

CC BY-SA 3.0
2017-06-17T02:03:14.933Z
I have downloaded the program but I haven't been able to work with it yet. I'm trying to run it
Sara
CC BY-SA 3.0
2017-06-17T18:24:33.043Z
@Sara what is it you're having a problem with?
TW93
CC BY-SA 3.0
2017-06-18T08:03:35.537Z
It doesn't run with an error. The program says choose a project name and even when I have chosen a project name, again says choose a project name and stops running
Sara
CC BY-SA 3.0
2017-06-16T14:48:10.087Z
2017-06-16T14:57:04.067Z
2017-06-16T14:57:04.067Z
TW93
<p>As part of a project me and some teammates did a <a href="https://github.com/llrs/PYT-SBI" rel="nofollow noreferrer">script</a> that outputs visual maps of distances between residues. It uses Biopython. </p>

<p>The module contact_map.py does what you are looking for. As an example, if you want to find the residues whose CA are below 5 you can run the following command:</p>

<pre><code>python3 contact_map.py pdb1cd8.ent -a CA -CA 5 
</code></pre>

<p>This will produce three files:</p>

<pre><code>distance_map_pdb1cd8_CA.png # Heatmap of the distance between the residues
contact_map_pdb1cd8_CA.png # Black/White heatmap: If it is at that min distance
contact_map.log  # The actions taken
</code></pre>

<p>If you don't have downloaded already the pdb structue you can use the main module:</p>

<pre><code>python3 cozmic.py real 1cd8 -a CA -CA 5 
</code></pre>

CC BY-SA 3.0
2017-06-20T16:28:41.097Z
How it calculates distances? Inside the asymmetric unit only, or as the shortest distance between the two residues in the crystal (or in biological assembly)?
marcin
CC BY-SA 3.0
2017-06-21T07:29:33.990Z
@marcin It calculates the distance between the C alpha (or beta if you chose so) between each residue in the file.
llrs
CC BY-SA 3.0
2017-06-21T08:12:31.383Z
Usually, the file == asymmetric unit. If you'd like to find all contacts in [biological assembly](https://pdb101.rcsb.org/learn/guide-to-understanding-pdb-data/biological-assemblies) or in crystal you'd need to take the symmetry into account.
marcin
CC BY-SA 3.0
2017-06-21T08:53:48.977Z
Thanks for the tips, maybe you want to contribute to the modules.
llrs
CC BY-SA 3.0
2017-06-21T18:52:45.117Z
I'm working now on a [project](https://github.com/project-gemmi/gemmi/) that in the future may be competitive to Bio.PDB, but probably not this year yet :-)
marcin
CC BY-SA 3.0
2017-06-22T07:26:11.230Z
I think Bio.PDB is being actively modified, but good luck with your work
llrs
CC BY-SA 3.0
2017-06-19T11:18:01.990Z
2017-06-19T11:18:01.990Z
llrs
<p>Distance between C-alphas can also be found using the <a href="https://github.com/BioJulia/BioStructures.jl" rel="nofollow noreferrer">BioStructures.jl</a> package in Julia:</p>

<pre><code>using BioStructures
struc = read("1AKE.pdb", PDB)
calphas = collectatoms(struc['A'], calphaselector)
dm = DistanceMap(calphas)
</code></pre>

<p>Then <code>dm[5, 10]</code> gives <code>12.19677604943208</code> etc.</p>

<p>Other variations also work, see the <a href="https://biojulia.net/BioStructures.jl/stable/documentation.html#Spatial-calculations-1" rel="nofollow noreferrer">docs</a>.</p>

CC BY-SA 4.0
2019-09-10T21:28:12.907Z
2019-09-10T21:28:12.907Z
jgreener
<p>You can use <a href="http://mdtraj.org/1.8.0/" rel="nofollow noreferrer">MDtraj</a>. The package is easy to install using Anaconda.</p>

<p>You can get the interacting residues using the following snippet (taken from <a href="http://mdtraj.org/1.6.2/examples/native-contact.html" rel="nofollow noreferrer">http://mdtraj.org/1.6.2/examples/native-contact.html</a>)</p>

<pre><code>heavy_pairs = np.array(
    [(i,j) for (i,j) in combinations(heavy, 2)
        if abs(native.topology.atom(i).residue.index - \
               native.topology.atom(j).residue.index) &gt; 3])

# compute the distances between these pairs in the native state
heavy_pairs_distances = md.compute_distances(native[0], heavy_pairs)[0]
# and get the pairs s.t. the distance is less than NATIVE_CUTOFF
native_contacts = heavy_pairs[heavy_pairs_distances &lt; NATIVE_CUTOFF]
print("Number of native contacts", len(native_contacts))
</code></pre>

CC BY-SA 3.0
2017-06-16T20:09:38.157Z
2017-06-16T20:09:38.157Z
Ajasja
<p>If we have a PDB structrure, how can we find residues physically interacting with each other in space? I know that we must find the distance between residues and if the distance is less than 5-6 Angstrom, we say that residues are physically interacting. But how can we find the distance between all residues and how can we finally determine the distances between all residues? Is there a software or webserver for that?</p>

CC BY-SA 3.0
2017-06-16T14:47:36.790Z
These interaction may be already annotated in the PDB, see Connectivity Section and Connectivity Annotation Section in the [PDB spec](http://www.wwpdb.org/documentation/file-format-content/format33/v3.3.html).
marcin
CC BY-SA 3.0
2017-06-16T21:18:09.167Z
One thing you're going to have to define for yourself is what you mean by "distance between residues". Do you mean closest atom pair? Closest heavyatom pair? Distance between C-alphas? The exact mechanism is probably going to be similar, regardless, but the details and the results will differ based on the definition.
R.M.
CC BY-SA 3.0
2017-06-16T21:33:21.063Z
I mean the distance between C-alphas
Sara
CC BY-SA 3.0
2017-06-17T10:56:57.827Z
Is the Ring 2.0 webserver (http://protein.bio.unipd.it/ring/) good enough for this purpose?
Sara
CC BY-SA 4.0
2019-09-10T21:29:38.420Z
If anyone is interested in speed benchmarks of distance calculations using various software, there are some at https://github.com/jgreener64/pdb-benchmarks. There are also example scripts for each software in that repository.
jgreener
<protein-structure><pdb>
How can we find the distance between all residues in a PDB file?
# How can we find the distance between all residues in a PDB file?

(Asked by: username_0 on 2017-06-16)

If we have a PDB structrure, how can we find residues physically interacting with each other in space? I know that we must find the distance between residues and if the distance is less than 5-6 Angstrom, we say that residues are physically interacting. But how can we find the distance between all residues and how can we finally determine the distances between all residues? Is there a software or webserver for that?

**Tags:** protein-structure, pdb
> **Comments:**
> **username_1**: These interaction may be already annotated in the PDB, see Connectivity Section and Connectivity Annotation Section in the [PDB spec](http://www.wwpdb.org/documentation/file-format-content/format33/v3.3.html).  
> **username_2**: One thing you're going to have to define for yourself is what you mean by "distance between residues". Do you mean closest atom pair? Closest heavyatom pair? Distance between C-alphas? The exact mechanism is probably going to be similar, regardless, but the details and the results will differ based on the definition.  
> **username_0**: I mean the distance between C-alphas  
> **username_0**: Is the Ring 2.0 webserver (http://protein.bio.unipd.it/ring/) good enough for this purpose?  
> **username_3**: If anyone is interested in speed benchmarks of distance calculations using various software, there are some at https://github.com/jgreener64/pdb-benchmarks. There are also example scripts for each software in that repository.  

---

## 5 answers

### Answer by username_4 on 2017-06-16. Score: 10

If you need to process multiple files, you could use Biopython to parse a PDB structure.
from Bio.PDB import PDBParser

# create parser
parser = PDBParser()

# read structure from file
structure = parser.get_structure('PHA-L', '1fat.pdb')

model = structure[0]
chain = model['A']

# this example uses only the first residue of a single chain.
# it is easy to extend this to multiple chains and residues.
for residue1 in chain:
    for residue2 in chain:
        if residue1 != residue2:
            # compute distance between CA atoms
            try:
                distance = residue1['CA'] - residue2['CA']
            except KeyError:
                ## no CA atom, e.g. for H_NAG
                continue
            if distance < 6:
                print(residue1, residue2, distance)
        # stop after first residue
        break

If you need to look at one structure, using a viewer perhaps would be easier. You could try PyMOL: (how to measure distance). There are other PDB viewers, some of which can work even through a browser.

> **Comments:**
> **username_0**: Dear lakov, I installed Anaconda and Biopython packages and run the above code in PyCharm 5.0 and got the following error:  
> **username_0**: Traceback (most recent call last):   File "C:/Users/VN7-592/PycharmProjects/untitled/sara.py", line 7, in <module>     structure = parser.get_structure('PHA-L', '1FAT.pdb')  
> **username_0**: File "C:\Users\VN7-592\Desktop\Anaconda\lib\site-packages\Bio\PDB\PDBParser.py", line 81, in get_structure     with as_handle(file, mode='rU') as handle:   File "C:\Users\VN7-592\Desktop\Anaconda\lib\contextlib.py", line 17, in __enter__     return self.gen.next()   File "C:\Users\VN7-592\Desktop\Anaconda\lib\site-packages\Bio\File.py", line 88, in as_handle     with open(handleish, mode, **kwargs) as fp: IOError: [Errno 2] No such file or directory: '1FAT.pdb'  
> **username_5**: You don't have the 1FAT.pdb file in the working directory, or your operating system is case sensitive.  
> **username_1**: But this gives only intermolecular distances? BioPython ignores crystal symmetry? In general, to check for contacts between residues X and Y one would need to calculate distance between X and the nearest symmetrical image of Y.  

---

### Answer by username_6 on 2017-06-16. Score: 4

Could you use CCP4's NCONT program? There's a GUI and a command line interface, whatever suits. You can specify which chains you want to target and interact with and set a cut off for distance. The bonus here is once you're in you have a nice suite of other structural tools to use.

If you're just doing it once, the GUI is friendly enough to work things out, if you're doing a batch then you can run it across several files via the command line.

[Download](http://www.ccp4.ac.uk/download/)

> **Comments:**
> **username_0**: I have downloaded the program but I haven't been able to work with it yet. I'm trying to run it  
> **username_6**: @Sara what is it you're having a problem with?  
> **username_0**: It doesn't run with an error. The program says choose a project name and even when I have chosen a project name, again says choose a project name and stops running  

---

### Answer by username_7 on 2017-06-19. Score: 2

As part of a project me and some teammates did a script that outputs visual maps of distances between residues. It uses Biopython. 
The module contact_map.py does what you are looking for. As an example, if you want to find the residues whose CA are below 5 you can run the following command:
python3 contact_map.py pdb1cd8.ent -a CA -CA 5 

This will produce three files:
distance_map_pdb1cd8_CA.png # Heatmap of the distance between the residues
contact_map_pdb1cd8_CA.png # Black/White heatmap: If it is at that min distance
contact_map.log  # The actions taken

If you don't have downloaded already the pdb structue you can use the main module:
python3 cozmic.py real 1cd8 -a CA -CA 5

> **Comments:**
> **username_1**: How it calculates distances? Inside the asymmetric unit only, or as the shortest distance between the two residues in the crystal (or in biological assembly)?  
> **username_7**: @marcin It calculates the distance between the C alpha (or beta if you chose so) between each residue in the file.  
> **username_1**: Usually, the file == asymmetric unit. If you'd like to find all contacts in [biological assembly](https://pdb101.rcsb.org/learn/guide-to-understanding-pdb-data/biological-assemblies) or in crystal you'd need to take the symmetry into account.  
> **username_7**: Thanks for the tips, maybe you want to contribute to the modules.  
> **username_1**: I'm working now on a [project](https://github.com/project-gemmi/gemmi/) that in the future may be competitive to Bio.PDB, but probably not this year yet :-)  
> **username_7**: I think Bio.PDB is being actively modified, but good luck with your work  

---

### Answer by username_3 on 2019-09-10. Score: 0

Distance between C-alphas can also be found using the BioStructures.jl package in Julia:
using BioStructures
struc = read("1AKE.pdb", PDB)
calphas = collectatoms(struc['A'], calphaselector)
dm = DistanceMap(calphas)

Then dm[5, 10] gives 12.19677604943208 etc.
Other variations also work, see the docs.


---

### Answer by username_5 on 2017-06-16. Score: 0

You can use MDtraj. The package is easy to install using Anaconda.
You can get the interacting residues using the following snippet (taken from http://mdtraj.org/1.6.2/examples/native-contact.html)
heavy_pairs = np.array(
    [(i,j) for (i,j) in combinations(heavy, 2)
        if abs(native.topology.atom(i).residue.index - \
               native.topology.atom(j).residue.index) > 3])

# compute the distances between these pairs in the native state
heavy_pairs_distances = md.compute_distances(native[0], heavy_pairs)[0]
# and get the pairs s.t. the distance is less than NATIVE_CUTOFF
native_contacts = heavy_pairs[heavy_pairs_distances < NATIVE_CUTOFF]
print("Number of native contacts", len(native_contacts))


---

