Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added NGL protein viewer example #1

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Added NGL protein viewer example
  • Loading branch information
Jhsmit committed Sep 24, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit e15012cdb32ec45b9401e90cbe822c65c9de1ac7
106 changes: 106 additions & 0 deletions examples/guide/ngl_protein_viewer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
"""
This is an example of a Protein viewer app, using [NGL Viewer](https://github.com/nglviewer/ngl), implemented as
panel HTML pane.

Source: Discussion on [Discourse 583]\
(https://discourse.holoviz.org/t/how-to-use-ngl-webgl-protein-viewer-in-panel/583)
"""

import panel as pn
import param


class NGLViewer(pn.pane.HTML):
pdb_string = param.String()
rcsb_id = param.String()
representation = param.Selector(default='ribbon',
objects=['ball+stick', 'backbone', 'ball+stick', 'cartoon', 'hyperball', 'licorice',
'ribbon', 'rope', 'spacefill', 'surface'])
color_scheme = param.Selector(default='chainid', objects=['chainid', 'residueindex', 'chainname'])
spin = param.Boolean(default=False)
priority = 0
_rename = dict(pn.pane.HTML._rename, pdb_string=None, rcsb_id=None, representation=None, spin=None, color_scheme=None)

def __init__(self, **params):
super().__init__(**params)
self.load_string = \
f"""
stage = new NGL.Stage("viewport");
stage.loadFile()"""
self._update_object_from_parameters()

@param.depends('representation', 'spin', 'color_scheme', watch=True)
def _update_object_from_parameters(self):
html =\
f"""
<div id="viewport" style="width:100%; height:100%;"></div>
<script>
{self.load_string}.then(function(o){{
o.addRepresentation("{self.representation}", {{colorScheme: "{self.color_scheme}"}});
o.autoView();
}}
);
stage.setSpin({'true' if self.spin else 'false'});
</script>
"""
self.object = html

@param.depends('pdb_string', watch=True)
def _update_object_from_pdb_string(self):
self.load_string = \
f"""
var PDBString = `{self.pdb_string}`;
stage = new NGL.Stage("viewport");
stage.loadFile( new Blob([PDBString], {{type: 'text/plain'}}), {{ ext:'pdb'}} )"""
self._update_object_from_parameters()

@param.depends('rcsb_id', watch=True)
def _update_object_from_rcsb_id(self):
self.load_string = \
f"""
stage = new NGL.Stage("viewport");
stage.loadFile("rcsb://{self.rcsb_id}")"""
self._update_object_from_parameters()


class ProteinViewer(param.Parameterized):

input_option = param.Selector(objects=['Upload File', 'RCSB PDB'])
rcsb_id = param.String()
load_structure = param.Action(lambda self: self._load_structure())

def __init__(self, **param):
super(ProteinViewer, self).__init__(**param)
self.file_widget = pn.widgets.FileInput(accept='.pdb')
self.ngl_html = NGLViewer(height=500, width=500)

def _load_structure(self):
if self.input_option == 'Upload File':
if self.file_widget.value:
string = self.file_widget.value.decode()
self.ngl_html.pdb_string = string
else:
pass

elif self.input_option == 'RCSB PDB':
self.ngl_html.rcsb_id = self.rcsb_id

def view(self):
col = pn.Column(*pn.Param(self.param))
col.insert(2, self.file_widget)
col.append(self.ngl_html.param.representation)
col.append(self.ngl_html.param.color_scheme)
col.append(self.ngl_html.param.spin)
app = pn.Row(
col,
self.ngl_html
)

return app


pn.config.js_files["ngl"]="https://unpkg.com/[email protected]/dist/ngl.js"
pn.extension()

pv = ProteinViewer()
pv.view().servable()