pythonpython-3.xanacondamdanalysis

How to display a Protein Data Bank PDB in a Jupyter notebook?


I am trying to follow this post, but it fails to display a PDB inside a Jupyter notebook:

import MDAnalysis as mda
import nglview as nv
from nglview.datafiles import PDB, XTC

u = mda.Universe(PDB, XTC)

protein = u.select_atoms('protein')

When I try to do:

w = nv.show_mdanalysis(protein)
w

I get:

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-2-499e28f0ffd3> in <module>()
----> 1 w = nv.show_mdanalysis(protein)
      2 w

~/anaconda3/lib/python3.6/site-packages/nglview-0.4-py3.5.egg/nglview/__init__.py in show_mdanalysis(atomgroup, **kwargs)
    118     '''
    119     structure_trajectory = MDAnalysisTrajectory(atomgroup)
--> 120     return NGLWidget(structure_trajectory, **kwargs)
    121 
    122 

~/anaconda3/lib/python3.6/site-packages/nglview-0.4-py3.5.egg/nglview/__init__.py in __init__(self, structure, trajectory, representations, parameters, **kwargs)
    347         if parameters:
    348             self.parameters = parameters
--> 349         self.set_structure(structure)
    350         if trajectory:
    351             self.trajectory = trajectory

~/anaconda3/lib/python3.6/site-packages/nglview-0.4-py3.5.egg/nglview/__init__.py in set_structure(self, structure)
    372     def set_structure(self, structure):
    373         self.structure = {
--> 374             "data": structure.get_structure_string(),
    375             "ext": structure.ext,
    376             "params": structure.params

~/anaconda3/lib/python3.6/site-packages/nglview-0.4-py3.5.egg/nglview/__init__.py in get_structure_string(self)
    313                 "'MDAnalysisTrajectory' requires the 'MDAnalysis' package"
    314             )
--> 315         import cStringIO
    316         u = self.atomgroup.universe
    317         u.trajectory[0]

ModuleNotFoundError: No module named 'cStringIO'

Edit Feb. 2020: I believe this is not an issue anymore if the libraries you are using updated their code to python3 properly: python 3.x ImportError: No module named 'cStringIO', and it should be ok by now as python2 is deprecated.


Solution

  • I would attempt to get a clean nglview installation via conda (and the Anaconda distribution):

    Try uninstalling nglview with

    conda remove nglview
    

    just in case there's an old version. If this conda-remove fails then this package might have been pip or easy_installed. Try pip remove nglview. If this still not helping, you have to look for the installed nglview package and manually remove it with rm but I do not want to put copy&paste instructions here as this requires careful looking at files.

    Once all traces of nglview have been removed, install the latest version of nglview from the conda-forge channel with conda:

    conda install -c conda-forge nglview