Hi everyone (mostly who are interested in structural bioinformatics). I face the problem that I cannot plot the electron density of a specific part of my protein. For the whole protein it is not a problem:

from Bio.PDB import *
import nglview as nv
import ipywidgets

pdbl = PDBList()
path = pdbl.retrieve_pdb_file('1FAT')
parser = MMCIFParser()
struct = parser.get_structure("1fat", path)
view = nv.show_biopython(struct)

But when I tried to pick up residues from 10 till 109 (res_list = Selection.unfold_entities(struct, "R")[10:109]) and plot I can't do that, because for nglview I need to have a special Object. As I'm a newbie, I'd like to address this question to specialists.

At the moment I'm trying to visualise model (structure), not electron density. But the next step is trying to find a package to visualize electron density also. I've just started becoming familiar with some open packages.

Thank You in Advance!

  • $\begingroup$ Hi @KitsuneBreeze its a good question and looks to be resolved. Please remember to "accept" and "upvote" $\endgroup$
    – M__
    Commented Feb 13, 2022 at 15:12

1 Answer 1


NGLview runs off the JavaScript libray NGL, which is documented as a series of very helpful examples: in the gallery. The commands are not one-to-one (slightly frustratingly), but it is helpful as you can figure it out easily with a few dir and help commands. In this case whatever example of density being loaded it will reveals that it's a different component —this is the same everywhere as density files do not contain atomic coordinates. Unfortunately, there's not a direct way to generate a new ComponentViewer from different sources to an NGLWidget and you cannot merge two of them: the various .show_* functions return an NGLWidget but aren't part of it.

First, I am not sure why you are going through a convoluted route for loading a PDB code:

pdb_code = '1fat'

import nglview as nv

view: nv.NGLWidget = nv.show_pdbid(pdb_code) 

Now, on loading the component a representation is added to it. These can hide the representation you then add and things will not make sense, until this detail is recalled! I fall in that pit nearly every time. But we can change it with update command:

# I don't like wonky sheets
view.update_cartoon(smoothSheets=True, color='salmon', component=0)

Now let's add the density map the circuitous way (no .show_url).

import requests, io
response: requests.Response = requests.get('https://www.ebi.ac.uk/pdbe/coordinates/files/1fat.ccp4')
fh = io.BytesIO(response.content)
comp: nv.component.ComponentViewer = view.add_component(fh, ext='ccp4') 

Now we can add a surface representation, but first let's clear the default. For the arguments I had a gander at the example:


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.