|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import molviewspec as mvs |
| 4 | + |
| 5 | + |
| 6 | +def gen_html_ligandfit(pdb_file, map_file, outdir, acr, smiles, cc): |
| 7 | + # make a story from snapshots |
| 8 | + builder = mvs.create_builder() |
| 9 | + structure = builder.download(url=pdb_file).parse(format="pdb").model_structure() |
| 10 | + structure.component(selector="polymer").representation( |
| 11 | + type="surface", size_factor=0.7 |
| 12 | + ).opacity(opacity=0.2).color(color="#AABDF1") |
| 13 | + structure.component(selector="polymer").representation().opacity( |
| 14 | + opacity=0.25 |
| 15 | + ).color(custom={"molstar_color_theme_name": "chain_id"}) |
| 16 | + structure.component(selector="ligand").representation(type="ball_and_stick").color( |
| 17 | + custom={"molstar_color_theme_name": "element-symbol"} |
| 18 | + ) |
| 19 | + structure.component(selector="ligand").representation(type="surface").opacity( |
| 20 | + opacity=0.1 |
| 21 | + ).color(custom={"molstar_color_theme_name": "element-symbol"}) |
| 22 | + |
| 23 | + ccp4 = builder.download(url=map_file).parse(format="map") |
| 24 | + ccp4.volume().representation( |
| 25 | + type="isosurface", |
| 26 | + relative_isovalue=1.5, |
| 27 | + show_wireframe=True, |
| 28 | + show_faces=False, |
| 29 | + ).color(color="blue").opacity(opacity=0.25) |
| 30 | + |
| 31 | + snapshot1 = builder.get_snapshot( |
| 32 | + title="Main View", |
| 33 | + description=f"## Ligand_Fit Results: \n ### {acr} with ligand & electron density map \n - SMILES: {smiles} \n - 2FO-FC at 1.5σ, blue \n - Fitting CC = {cc}", |
| 34 | + transition_duration_ms=2000, |
| 35 | + linger_duration_ms=5000, |
| 36 | + ) |
| 37 | + |
| 38 | + # SNAPSHOT2 |
| 39 | + builder = mvs.create_builder() |
| 40 | + structure = builder.download(url=pdb_file).parse(format="pdb").model_structure() |
| 41 | + structure.component(selector="polymer").representation( |
| 42 | + type="surface", size_factor=0.7 |
| 43 | + ).opacity(opacity=0.5).color(color="#D8BFD8") |
| 44 | + structure.component(selector="polymer").representation().opacity(opacity=0.6).color( |
| 45 | + color="grey" |
| 46 | + ) |
| 47 | + structure.component(selector="ligand").focus().representation( |
| 48 | + type="ball_and_stick" |
| 49 | + ).color(custom={"molstar_color_theme_name": "element-symbol"}) |
| 50 | + |
| 51 | + ccp4 = builder.download(url=map_file).parse(format="map") |
| 52 | + ccp4.volume().representation( |
| 53 | + type="isosurface", |
| 54 | + relative_isovalue=1.5, |
| 55 | + show_wireframe=True, |
| 56 | + show_faces=False, |
| 57 | + ).color(color="blue").opacity(opacity=0.25) |
| 58 | + |
| 59 | + # add a label |
| 60 | + # info = get_chain_and_residue_numbers(pdb_file, "LIG") |
| 61 | + # resid = info[0][1] |
| 62 | + residue = mvs.ComponentExpression(label_seq_id=202) |
| 63 | + ( |
| 64 | + structure.component( |
| 65 | + selector=residue, |
| 66 | + custom={ |
| 67 | + "molstar_show_non_covalent_interactions": True, |
| 68 | + "molstar_non_covalent_interactions_radius_ang": 5.0, |
| 69 | + }, |
| 70 | + ).label(text=f"CC = {cc}") |
| 71 | + ) |
| 72 | + |
| 73 | + snapshot2 = builder.get_snapshot( |
| 74 | + title="Focus View", |
| 75 | + description=f"## Ligand_Fit Results: \n ### {acr} with ligand & electron density map \n - SMILES: {smiles} \n - 2FO-FC at 1.5σ, blue \n - Fitting CC = {cc}", |
| 76 | + transition_duration_ms=2000, |
| 77 | + linger_duration_ms=5000, |
| 78 | + ) |
| 79 | + |
| 80 | + states = mvs.States( |
| 81 | + snapshots=[snapshot1, snapshot2], |
| 82 | + metadata=mvs.GlobalMetadata(description="Ligand_fit Results"), |
| 83 | + ) |
| 84 | + |
| 85 | + with open(pdb_file) as f: |
| 86 | + pdb_data = f.read() |
| 87 | + |
| 88 | + with open(map_file, mode="rb") as f: |
| 89 | + map_data = f.read() |
| 90 | + |
| 91 | + html = mvs.molstar_html( |
| 92 | + states, |
| 93 | + data={pdb_file: pdb_data, map_file: map_data}, |
| 94 | + ui="stories", |
| 95 | + ) |
| 96 | + |
| 97 | + with open(f"{outdir}/ligand_fit.html", "w") as f: |
| 98 | + f.write(html) |
0 commit comments