Skip to content

Commit

Permalink
Merge pull request #10 from Jhsmit/local_data
Browse files Browse the repository at this point in the history
Local data
  • Loading branch information
Jhsmit authored Apr 25, 2024
2 parents bda9a6e + 5ce362d commit 236f134
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 62 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,29 @@ view = PDBeMolstar(molecule_id='1qyn', theme='light', hide_water=True)
view
```

Loading local data, hiding the buttons:

```python
from pathlib import Path
fpth = Path().resolve() / 'assets' / '6vsb.bcif'
custom_data = {
'data': fpth.read_bytes(),
'format': 'cif',
'binary': True,
}
view = PDBeMolstar(
custom_data=custom_data,
hide_controls_icon=True,
hide_expand_icon=True,
hide_settings_icon=True,
hide_selection_icon=True,
hide_animation_icon=True,
hide_water=True,
hide_carbs=True,
)
view
```

See the example notebook for more advanced usage.
Solara example code can be found [here](https://github.com/Jhsmit/ploomber-solara-ipymolstar)

Expand Down
Binary file added assets/6vsb.bcif
Binary file not shown.
26 changes: 25 additions & 1 deletion example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"view = PDBeMolstar(\n",
Expand All @@ -25,6 +27,28 @@
"view"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# load local files via custom_data\n",
"from pathlib import Path \n",
"fpth = Path().resolve() / 'assets' / '6vsb.bcif'\n",
"custom_data = {\n",
" 'data': fpth.read_bytes(),\n",
" 'format': 'cif',\n",
" 'binary': True,\n",
" }\n",
"view = PDBeMolstar(\n",
" custom_data=custom_data, \n",
" hide_water=True,\n",
" hide_carbs=True,\n",
")\n",
"view"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down
15 changes: 13 additions & 2 deletions src/ipymolstar/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,22 @@ function getHideCanvasControls(model) {
return hideCanvasControls;
}

function getCustomData(model) {
var customData = model.get("custom_data");

if (customData && 'data' in customData) {
var url = URL.createObjectURL(new Blob([customData.data]));
customData.url = url;
delete customData.data;
}

return customData;
}

function getOptions(model) {
var options = {
moleculeId: model.get("molecule_id"),
customData: model.get("custom_data"),
customData: getCustomData(model),
assemblyId: model.get("assembly_id"),
defaultPreset: model.get("default_preset"),
ligandView: model.get("ligand_view"),
Expand Down Expand Up @@ -184,7 +196,6 @@ function render({ model, el }) {

viewerInstance.events.loadComplete.subscribe(() => {
// trigger callabacks which need to be called after loading
console.log("load complete");
Object.values(callbacksLoadComplete).forEach((callback) => callback());
});

Expand Down
89 changes: 30 additions & 59 deletions tests/test_notebook.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -29,25 +29,9 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "c716fd207a7642c3a8148d6429c0f456",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"VBox(children=(PDBeMolstar(bg_color='#F7F7F7', height='200px', molecule_id='1qyn'), PDBeMolstar(bg_color='#F7F…"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"\n",
"v1 = PDBeMolstar(molecule_id='1qyn', height=\"200px\")\n",
Expand All @@ -65,33 +49,17 @@
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "f20a9b627be1495a8680c6deb41aecfc",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"PDBeMolstar(bg_color='#F7F7F7', height='150px', molecule_id='1qyn')"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"v = PDBeMolstar(molecule_id='1qyn', height='150px')\n",
"v"
]
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -108,25 +76,9 @@
},
{
"cell_type": "code",
"execution_count": 34,
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "81d95973cf31465eac4767d2043a203f",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"PDBeMolstar(bg_color='#F7F7F7', custom_data={'url': 'https://www.ebi.ac.uk/pdbe/model-server/v1/1cbs/atoms?lab…"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"custom_data = dict(url='https://www.ebi.ac.uk/pdbe/model-server/v1/1cbs/atoms?label_entity_id=1&auth_asym_id=A&encoding=bcif', format='cif', binary=True)\n",
"v = PDBeMolstar(custom_data=custom_data, height='150px')\n",
Expand All @@ -135,20 +87,39 @@
},
{
"cell_type": "code",
"execution_count": 35,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"new_custom_data = dict(url='https://www.ebi.ac.uk/pdbe/model-server/v1/1qyn/atoms?label_entity_id=1&auth_asym_id=A&encoding=bcif', format='cif', binary=True)\n",
"v.cumstom_data = new_custom_data"
"v.custom_data = new_custom_data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
"source": [
"from pathlib import Path \n",
"fpth = Path().resolve().parent / 'assets' / '6vsb.bcif'\n",
"custom_data = {\n",
" 'data': fpth.read_bytes(),\n",
" 'format': 'cif',\n",
" 'binary': True,\n",
" }\n",
"view = PDBeMolstar(\n",
" custom_data=custom_data, \n",
" hide_controls_icon=True, \n",
" hide_expand_icon=True, \n",
" hide_settings_icon=True, \n",
" hide_selection_icon=True, \n",
" hide_animation_icon=True,\n",
" hide_water=True,\n",
" hide_carbs=True,\n",
")\n",
"view"
]
}
],
"metadata": {
Expand Down

0 comments on commit 236f134

Please sign in to comment.