Skip to content

Commit

Permalink
Integrate PyMOL rendering into examples
Browse files Browse the repository at this point in the history
  • Loading branch information
padix-key committed Feb 8, 2025
1 parent 1bde72b commit 5fbff29
Show file tree
Hide file tree
Showing 25 changed files with 238 additions and 405 deletions.
2 changes: 1 addition & 1 deletion doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@
"within_subsection_order": FileNameSortKey,
# Do not run example scripts with a trailing '_noexec'
"filename_pattern": "^((?!_noexec).)*$",
"ignore_pattern": r"(.*ignore\.py)|(.*pymol\.py)",
"ignore_pattern": r"(.*ignore\.py)",
"download_all_examples": False,
# Never report run time
"min_reported_time": sys.maxsize,
Expand Down
Binary file not shown.
17 changes: 15 additions & 2 deletions doc/examples/scripts/structure/alphabet/structure_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import numpy as np
import biotite
import biotite.database.rcsb as rcsb
import biotite.interface.pymol as pymol_interface
import biotite.sequence as seq
import biotite.sequence.align as align
import biotite.sequence.graphics as graphics
Expand Down Expand Up @@ -374,8 +375,20 @@ def filter_undefined_spans(sequence, min_length):
_, transform = struc.superimpose(query_anchors, target_anchors)
# Apply this transformation to full structure
target_chain = transform.apply(target_chain)
# Visualization with PyMOL...
# sphinx_gallery_ammolite_script = "structure_search_pymol.py"

# Visualization with PyMOL
pymol_interface.cmd.set("cartoon_rect_length", 1.0)
pymol_interface.cmd.set("depth_cue", 0)
pymol_interface.cmd.set("cartoon_cylindrical_helices", 1)
pymol_interface.cmd.set("cartoon_helix_radius", 1.5)
pymol_query = pymol_interface.PyMOLObject.from_structure(query_chain)
pymol_target = pymol_interface.PyMOLObject.from_structure(target_chain)
pymol_query.show_as("cartoon")
pymol_target.show_as("cartoon")
pymol_query.color("biotite_lightgreen")
pymol_target.color("biotite_lightorange")
pymol_query.orient()
pymol_interface.show((1500, 1000))
# sphinx_gallery_thumbnail_number = 3

########################################################################################
Expand Down
26 changes: 0 additions & 26 deletions doc/examples/scripts/structure/alphabet/structure_search_pymol.py

This file was deleted.

Binary file not shown.
44 changes: 41 additions & 3 deletions doc/examples/scripts/structure/contacts/contact_sites.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import numpy as np
import biotite.database.rcsb as rcsb
import biotite.interface.pymol as pymol_interface
import biotite.structure as struc
import biotite.structure.io.pdbx as pdbx

Expand All @@ -25,7 +26,8 @@

# Fetch and load structure
pdbx_file = pdbx.BinaryCIFFile.read(rcsb.fetch("2or1", "bcif"))
structure = pdbx.get_structure(pdbx_file, model=1)
structure = pdbx.get_structure(pdbx_file, model=1, include_bonds=True)
structure = structure[~struc.filter_solvent(structure)]


# Separate structure into the DNA and the two identical protein chains
Expand Down Expand Up @@ -70,5 +72,41 @@
res_name = protein_l.res_name[protein_l.res_id == res_id][0]
print(res_name.capitalize() + str(res_id))

# Visualization with PyMOL...
# sphinx_gallery_ammolite_script = "contact_sites_pymol.py"

# Visualization with PyMOL
pymol_obj = pymol_interface.PyMOLObject.from_structure(structure)
pymol_obj.color("gray", np.isin(structure.chain_id, ["A", "B"]))
pymol_obj.color("biotite_brightorange", structure.chain_id == "L")
pymol_obj.color("biotite_lightgreen", structure.chain_id == "R")
# Set view
pymol_interface.cmd.set_view(
(
-0.044524662,
0.767611504,
0.639355302,
0.998693943,
0.018437184,
0.047413416,
0.024606399,
0.640637815,
-0.767439663,
0.000000000,
0.000000000,
-115.614288330,
56.031833649,
23.317802429,
3.761308193,
73.517341614,
157.711288452,
-20.000000000,
)
)
# Highlight contacts
residue_mask = np.isin(structure.res_id, common_ids)
pymol_obj.show("sticks", np.isin(structure.chain_id, ["L", "R"]) & residue_mask)
for chain, color in zip(("L", "R"), ("biotite_dimorange", "biotite_darkgreen")):
pymol_obj.color(
color,
(structure.chain_id == chain) & (structure.atom_name != "CA") & residue_mask,
)
pymol_interface.show((1500, 800))
64 changes: 0 additions & 64 deletions doc/examples/scripts/structure/contacts/contact_sites_pymol.py

This file was deleted.

Binary file removed doc/examples/scripts/structure/contacts/leaflet.png
Binary file not shown.
32 changes: 29 additions & 3 deletions doc/examples/scripts/structure/contacts/leaflet.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from tempfile import NamedTemporaryFile
import networkx as nx
import numpy as np
import biotite.interface.pymol as pymol_interface
import biotite.structure as struc
import biotite.structure.io as strucio

Expand Down Expand Up @@ -111,7 +112,32 @@ def find_leaflets(structure, head_atom_mask, cutoff_distance=15.0, periodic=Fals
# Save marked lipids to structure file
temp = NamedTemporaryFile(suffix=".pdb")
strucio.save_structure(temp.name, structure)
# Visualization with PyMOL...
# sphinx_gallery_ammolite_script = "leaflet_pymol.py"

temp.close()


# Visualization with PyMOL
pymol_interface.cmd.set("sphere_scale", 1.5)
# Remove hydrogen and water
structure = structure[(structure.element != "H") & (structure.res_name != "TIP")]
structure.bonds = struc.connect_via_distances(structure)
pymol_obj = pymol_interface.PyMOLObject.from_structure(structure)
# Configure lipid tails
pymol_obj.color("biotite_lightgreen", structure.chain_id == "A")
pymol_obj.color("biotite_brightorange", structure.chain_id == "B")
pymol_obj.show("sticks", np.isin(structure.chain_id, ("A", "B")))
# Configure lipid heads
pymol_obj.color(
"biotite_darkgreen", (structure.chain_id == "A") & (structure.atom_name == "P")
)
pymol_obj.color(
"biotite_dimorange", (structure.chain_id == "B") & (structure.atom_name == "P")
)
pymol_obj.show(
"spheres", np.isin(structure.chain_id, ("A", "B")) & (structure.atom_name == "P")
)
# Adjust camera
pymol_obj.orient()
pymol_interface.cmd.turn("x", 90)
pymol_obj.zoom(buffer=-10)
# Display
pymol_interface.show((1500, 1000))
46 changes: 0 additions & 46 deletions doc/examples/scripts/structure/contacts/leaflet_pymol.py

This file was deleted.

Binary file not shown.
54 changes: 37 additions & 17 deletions doc/examples/scripts/structure/misc/biological_assembly.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,23 @@
`this page <https://pdb101.rcsb.org/learn/guide-to-understanding-pdb-data/biological-assemblies>`_.
In this example, we will create the complete biological assembly of the
capsid from the *Sulfolobus turreted icosahedral virus*
- a hetero 1080-mer!
capsid from the lambda phage.
At first we will check, which assemblies are available to us.
"""

# Code source: Patrick Kunzmann
# License: BSD 3 clause

from tempfile import NamedTemporaryFile
import biotite.database.rcsb as rcsb
import biotite.interface.pymol as pymol_interface
import biotite.structure as struc
import biotite.structure.io as strucio
import biotite.structure.io.pdbx as pdbx

pdbx_file = pdbx.BinaryCIFFile.read(rcsb.fetch("3J31", "bcif"))
PDB_ID = "7VII"


pdbx_file = pdbx.BinaryCIFFile.read(rcsb.fetch(PDB_ID, "bcif"))

assemblies = pdbx.list_assemblies(pdbx_file)
print("ID name")
Expand All @@ -61,19 +62,38 @@
# It returns the chosen assembly as :class:`AtomArray`.
# Note that the assembly ID is a string, not an integer.

biological_unit = pdbx.get_assembly(pdbx_file, assembly_id="1", model=1)
print("Number of protein chains:", struc.get_chain_count(biological_unit))
assembly = pdbx.get_assembly(
pdbx_file,
assembly_id="1",
model=1,
# To identify later which atoms belong to which protein type
extra_fields=["label_entity_id"],
)

print("Number of protein chains:", struc.get_chain_count(assembly))

########################################################################
# Now we could do some analysis on the biological unit.
# But for this example we will simply save the entire assembly as *PDB*
# file for later visualization.
# The assembly consists of two different protein types, so called entities.
# Each entity may be represented by multiple chain IDs.

# For brevity, save only CA atoms to file for visualization
biological_unit = biological_unit[biological_unit.atom_name == "CA"]
temp = NamedTemporaryFile(suffix=".cif")
strucio.save_structure(temp.name, biological_unit)
# Visualization with PyMOL...
# sphinx_gallery_ammolite_script = "biological_assembly_pymol.py"
entity_info = pdbx_file.block["entity"]
print("ID description")
print()
for entity_id, description in zip(
entity_info["id"].as_array(), entity_info["pdbx_description"].as_array()
):
print(f"{entity_id:2} {description}")

########################################################################
# Now we could do some analysis on the biological unit.
# But for this example we will visualize the entire assembly.

temp.close()
# Show capsid structure as CA spheres to increase rendering speed
assembly = assembly[assembly.atom_name == "CA"]
pymol_object = pymol_interface.PyMOLObject.from_structure(assembly)
pymol_object.color("biotite_dimgreen", assembly.label_entity_id == "1")
pymol_object.color("biotite_lightorange", assembly.label_entity_id == "2")
pymol_object.set("sphere_scale", 2.0)
pymol_object.show_as("spheres")
pymol_object.zoom(buffer=25)
pymol_interface.show((1500, 1500))
22 changes: 0 additions & 22 deletions doc/examples/scripts/structure/misc/biological_assembly_pymol.py

This file was deleted.

Binary file not shown.
Loading

0 comments on commit 5fbff29

Please sign in to comment.