|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +import gemmi |
| 6 | +import molviewspec as mvs |
| 7 | + |
| 8 | +from dlstbx.util.mvs.helpers import find_residue_by_name |
| 9 | + |
| 10 | + |
| 11 | +def gen_html_pandda(pdb_file, event_map, z_map, resname, outdir, dtag, smiles, score): |
| 12 | + # make an mvs story from snapshots |
| 13 | + st = gemmi.read_structure(pdb_file) |
| 14 | + chain, res = find_residue_by_name(st, resname) |
| 15 | + residue = mvs.ComponentExpression(label_seq_id=res.seqid.num) |
| 16 | + |
| 17 | + builder = mvs.create_builder() |
| 18 | + structure = builder.download(url=pdb_file).parse(format="pdb").model_structure() |
| 19 | + structure.component(selector="polymer").representation( |
| 20 | + type="surface", size_factor=0.7 |
| 21 | + ).opacity(opacity=0.2).color(color="#AABDF1") |
| 22 | + structure.component(selector="polymer").representation().opacity( |
| 23 | + opacity=0.25 |
| 24 | + ).color(custom={"molstar_color_theme_name": "chain_id"}) |
| 25 | + structure.component(selector="ligand").representation( |
| 26 | + type="surface", size_factor=0.7 |
| 27 | + ).opacity(opacity=0.1).color(custom={"molstar_color_theme_name": "element-symbol"}) |
| 28 | + |
| 29 | + structure.component(selector=residue).focus().representation( |
| 30 | + type="ball_and_stick" |
| 31 | + ).color(custom={"molstar_color_theme_name": "element-symbol"}) |
| 32 | + |
| 33 | + ccp4 = builder.download(url=event_map).parse(format="map") |
| 34 | + ccp4.volume().representation( |
| 35 | + type="isosurface", |
| 36 | + relative_isovalue=3, |
| 37 | + show_wireframe=True, |
| 38 | + show_faces=False, |
| 39 | + ).color(color="blue").opacity(opacity=0.25) |
| 40 | + |
| 41 | + structure.component( |
| 42 | + selector=residue, |
| 43 | + custom={ |
| 44 | + "molstar_show_non_covalent_interactions": True, |
| 45 | + "molstar_non_covalent_interactions_radius_ang": 5, |
| 46 | + }, |
| 47 | + ) |
| 48 | + |
| 49 | + snapshot1 = builder.get_snapshot( |
| 50 | + title="Event_map", |
| 51 | + description=f"## PanDDA2 Results: \n ### {dtag} \n - Ligand score: {score} \n - SMILES: {smiles} \n - Event map at 3σ, blue", |
| 52 | + transition_duration_ms=700, |
| 53 | + linger_duration_ms=4000, |
| 54 | + ) |
| 55 | + |
| 56 | + # SNAPSHOT2 |
| 57 | + builder = mvs.create_builder() |
| 58 | + structure = builder.download(url=pdb_file).parse(format="pdb").model_structure() |
| 59 | + structure.component(selector="polymer").representation( |
| 60 | + type="surface", size_factor=0.7 |
| 61 | + ).opacity(opacity=0.2).color(color="#AABDF1") |
| 62 | + structure.component(selector="polymer").representation().opacity( |
| 63 | + opacity=0.25 |
| 64 | + ).color(custom={"molstar_color_theme_name": "chain_id"}) |
| 65 | + structure.component(selector="ligand").representation( |
| 66 | + type="surface", size_factor=0.7 |
| 67 | + ).opacity(opacity=0.1).color(custom={"molstar_color_theme_name": "element-symbol"}) |
| 68 | + |
| 69 | + structure.component(selector=residue).focus().representation( |
| 70 | + type="ball_and_stick" |
| 71 | + ).color(custom={"molstar_color_theme_name": "element-symbol"}) |
| 72 | + |
| 73 | + ccp4 = builder.download(url=z_map).parse(format="map") |
| 74 | + ccp4.volume().representation( |
| 75 | + type="isosurface", |
| 76 | + relative_isovalue=3, |
| 77 | + show_wireframe=True, |
| 78 | + show_faces=False, |
| 79 | + ).color(color="green").opacity(opacity=0.25) |
| 80 | + |
| 81 | + structure.component( |
| 82 | + selector=residue, |
| 83 | + custom={ |
| 84 | + "molstar_show_non_covalent_interactions": True, |
| 85 | + "molstar_non_covalent_interactions_radius_ang": 5, |
| 86 | + }, |
| 87 | + ) |
| 88 | + |
| 89 | + snapshot2 = builder.get_snapshot( |
| 90 | + title="Z_map", |
| 91 | + description=f"## PanDDA2 Results: \n ### {dtag} \n - Ligand score: {score} \n - SMILES: {smiles} \n - Z_map at 3σ, green", |
| 92 | + transition_duration_ms=700, |
| 93 | + linger_duration_ms=4000, |
| 94 | + ) |
| 95 | + |
| 96 | + states = mvs.States( |
| 97 | + snapshots=[snapshot1, snapshot2], # [snapshot1, snapshot2] |
| 98 | + metadata=mvs.GlobalMetadata(description="PanDDA2 Results"), |
| 99 | + ) |
| 100 | + |
| 101 | + with open(pdb_file) as f: |
| 102 | + pdb_data = f.read() |
| 103 | + |
| 104 | + # with open(event_map, mode="rb") as f: |
| 105 | + # map_data1 = f.read() |
| 106 | + |
| 107 | + with open(z_map, mode="rb") as f: |
| 108 | + map_data2 = f.read() |
| 109 | + |
| 110 | + html = mvs.molstar_html( |
| 111 | + states, |
| 112 | + data={pdb_file: pdb_data, z_map: map_data2}, # event_map: map_data1, |
| 113 | + ui="stories", |
| 114 | + ) |
| 115 | + |
| 116 | + out_file = Path(f"{outdir}/pandda2_mvs.html") |
| 117 | + with open(out_file, "w") as f: |
| 118 | + f.write(html) |
| 119 | + |
| 120 | + return out_file |
0 commit comments