| name | miscellaneous |
| description | Collection of useful PyMOL patterns and commands that don't fit into specific workflow skills. Use as reference for edge cases and less common operations. |
| version | 0.1.0 |
Miscellaneous PyMOL Patterns
Useful commands and patterns that don't fit neatly into other skills.
Send all cmd.* code via: ~/.pymol-agent-bridge/bin/pymol-agent-bridge exec "..." (or heredoc for multi-line). See @pymol-fundamentals for details.
Object and State Information
List Objects
cmd.get_names()
cmd.get_names("objects")
cmd.get_names("selections")
Count Atoms
cmd.count_atoms("all")
cmd.count_atoms("polymer.protein")
cmd.count_atoms("organic")
Count States (for NMR/MD)
n = cmd.count_states("object_name")
Get Object Properties
resnames = []
cmd.iterate("all", "resnames.append(resn)", space={"resnames": resnames})
unique_resnames = list(set(resnames))
Session Management
Save Session
cmd.save("session.pse")
Load Session
cmd.load("session.pse")
Clear Everything
cmd.delete("all")
cmd.reset()
Object Manipulation
Copy Object
cmd.copy("new_name", "source_object")
Translate Object
cmd.translate([10, 0, 0], "object_name")
Rotate Object
cmd.rotate("y", 45, "object_name")
Get Object List
objects = cmd.get_object_list()
print("Objects: " + str(objects))
Enable/Disable Objects
cmd.disable("object_name")
cmd.enable("object_name")
cmd.toggle("object_name")
Group Objects
cmd.group("group_name", "obj1 obj2 obj3")
cmd.disable("group_name")
cmd.enable("group_name")
cmd.ungroup("group_name")
Show As (Hide Others)
cmd.show_as("sticks", "selection")
Working with Chains
Get Chain IDs
chains = []
cmd.iterate("all", "chains.append(chain)", space={"chains": chains})
unique_chains = list(set(chains))
Split by Chain
cmd.split_chains("object_name")
Rename Chain
cmd.alter("chain A", "chain='X'")
Modifying Structures
Remove Water
cmd.remove("solvent")
Remove Hydrogens
cmd.remove("hydro")
Add Hydrogens
cmd.h_add("all")
Keep Only Protein
cmd.remove("not polymer.protein")
Selection Tricks
Expand Selection
cmd.select("expanded", "all within 5 of ligand")
cmd.select("expanded", "byres (all within 5 of ligand)")
cmd.select("expanded", "bychain (all within 5 of ligand)")
Invert Selection
cmd.select("inverse", "not current_selection")
Backbone and Sidechain
cmd.select("backbone", "name N+CA+C+O")
cmd.select("sidechain", "not (name N+CA+C+O+H)")
Non-Water Heteroatoms
cmd.select("hetero", "hetatm and not solvent")
Get Residue Range
residues = []
cmd.iterate("protein and name CA", "residues.append(int(resi))",
space={"residues": residues})
print("Range: " + str(min(residues)) + "-" + str(max(residues)))
Select by Sequence Motif
cmd.select("motif", "pepseq KTL")
cmd.select("leucines", "pepseq L")
Select Bonded Neighbors
cmd.select("neighbors", "neighbor resi 30 and name CA")
cmd.select("bonded", "bound_to resi 30 and name CA")
Around vs Within
cmd.select("s1", "all within 5 of ligand")
cmd.select("s2", "all around 5 of ligand")
Special Representations
Ball and Stick
cmd.show("sticks", "selection")
cmd.set("stick_ball", 1)
cmd.set("stick_ball_ratio", 1.5)
Putty (B-factor Tube)
cmd.show("cartoon", "selection")
cmd.cartoon("putty", "selection")
Dots for Surface Points
cmd.show("dots", "selection")
cmd.set("dot_density", 3)
Label Formatting
Custom Labels
cmd.label("name CA", "resn")
cmd.label("name CA", "resi")
cmd.label("name CA", "resn+resi")
cmd.label("name CA", "'%s%s'%(resn,resi)")
Label Styling
cmd.set("label_size", 14)
cmd.set("label_font_id", 7)
cmd.set("label_color", "black", "all")
cmd.set("label_position", (0, 0, 2))
Clear Labels
cmd.label("all", "")
Symmetry Operations
Show Unit Cell
cmd.show("cell")
Generate Symmetry Mates
cmd.symexp("sym", "object", "object", 10)
File Formats
Export Formats
cmd.save("output.pdb", "selection")
cmd.save("output.cif", "selection")
cmd.save("output.mol2", "selection")
cmd.save("output.sdf", "selection")
Image Formats
cmd.ray(width, height)
cmd.png("image.png")
cmd.save("image.pse")
Performance Tips
Disable Updates During Batch Operations
cmd.set("defer_updates", 1)
cmd.set("defer_updates", 0)
cmd.rebuild()
Reduce Quality for Large Systems
cmd.set("cartoon_sampling", 5)
cmd.set("surface_quality", 0)
cmd.set("hash_max", 200)
Debugging
Print Command Output
output = cmd.get("ray_trace_mode")
print("Current setting: " + str(output))
Check Selection Syntax
count = cmd.count_atoms("your_selection_here")
print("Matched atoms: " + str(count))
Metal and Cofactor Visualization
Select Metals
cmd.select("metals", "elem Cu+Fe+Zn+Mg+Mn+Ca")
cmd.show("spheres", "metals")
cmd.color("orange", "metals")
Select Heme Groups
cmd.select("heme", "resn HEM+HEC+HEA")
cmd.show("sticks", "heme")
Coordination Sphere
cmd.select("coord_sphere", "byres (polymer.protein within 3 of metals)")
cmd.show("sticks", "coord_sphere")
cmd.color("yellow", "coord_sphere and elem C")
Residue Property Selection
By Charge
cmd.select("basic", "resn ARG+LYS+HIS")
cmd.select("acidic", "resn ASP+GLU")
cmd.select("charged", "resn ARG+LYS+HIS+ASP+GLU")
By Hydrophobicity
cmd.select("hydrophobic", "resn ALA+VAL+ILE+LEU+MET+PHE+TRP+PRO")
cmd.select("polar", "resn SER+THR+CYS+TYR+ASN+GLN")
Color by Property
cmd.color("blue", "resn ARG+LYS+HIS")
cmd.color("red", "resn ASP+GLU")
cmd.color("yellow", "hydrophobic")
cmd.color("cyan", "polar")
NMR Ensemble Handling
Check Number of States
states = cmd.count_states("object_name")
print("States: " + str(states))
Show All States Overlaid
cmd.set("all_states", 1)
cmd.set("all_states", 0)
Create Movie Through States
n_states = cmd.count_states("object_name")
cmd.mset("1 -" + str(n_states))
cmd.mplay()
Style for Ensemble Visualization
cmd.set("all_states", 1)
cmd.show("lines", "all")
cmd.set("line_width", 1)
cmd.color("gray70", "all")
cmd.set("state", 1)
cmd.color("red", "all")
Programmatic Measurements
Get Distance (Returns Value)
d = cmd.get_distance("sel1", "sel2")
print("Distance: " + str(round(d, 2)) + " A")
Get Angle
a = cmd.get_angle("atom1", "atom2", "atom3")
print("Angle: " + str(round(a, 1)) + " degrees")
Get Dihedral
d = cmd.get_dihedral(
"resi 10 and name N",
"resi 10 and name CA",
"resi 10 and name C",
"resi 11 and name N"
)
print("Psi: " + str(round(d, 1)) + " degrees")
Pair Fit (Align Specific Atoms)
rmsd = cmd.pair_fit(
"mobile and resi 1-30 and name CA",
"target and resi 1-30 and name CA"
)
print("Pair fit RMSD: " + str(round(rmsd, 2)) + " A")
Finding Contacts
Select Contacting Residues
cmd.select("contact_A", "byres (chain A within 4 of chain B)")
cmd.select("contact_B", "byres (chain B within 4 of chain A)")
Iterate to Get Contact List
contacts = []
cmd.iterate(
"chain A within 4 of chain B",
"contacts.append((resi, resn))",
space={"contacts": contacts}
)
unique = list(set(contacts))
print("Contact residues: " + str(len(unique)))
Visualize Contacts
cmd.select("interface", "byres (chain A within 4 of chain B) or byres (chain B within 4 of chain A)")
cmd.show("sticks", "interface")
cmd.color("orange", "interface and chain A")
cmd.color("purple", "interface and chain B")
Known Limitations
- cmd.morph() may not work reliably through socket connection
- Electrostatic surfaces require APBS or similar external tools
- Some wizard operations may need interactive PyMOL
Solvent Accessible Surface Area
Calculate Total SASA
total_area = cmd.get_area("selection")
print("SASA: " + str(round(total_area, 1)) + " sq A")
Color by SASA
cmd.get_area("selection", load_b=1)
cmd.spectrum("b", "blue_white_red", "selection")
Find Buried Residues
cmd.get_area("protein", load_b=1)
buried = []
cmd.iterate(
"protein and name CA",
"buried.append((resi, resn)) if b < 10 else None",
space={"buried": buried}
)
print("Buried residues: " + str(len(buried)))
Select by Exposure
cmd.get_area("protein", load_b=1)
cmd.select("exposed", "protein and name CA and b > 50")
cmd.select("buried", "protein and name CA and b < 10")
Salt Bridges
Select Salt Bridge Atoms
cmd.select("basic_atoms",
"(resn ARG and name NH1+NH2+NE) or "
"(resn LYS and name NZ) or "
"(resn HIS and name ND1+NE2)")
cmd.select("acidic_atoms",
"(resn ASP and name OD1+OD2) or "
"(resn GLU and name OE1+OE2)")
Find and Display Salt Bridges
cmd.distance("salt_bridges", "basic_atoms", "acidic_atoms",
cutoff=4.0, mode=0)
cmd.set("dash_color", "yellow", "salt_bridges")
Aromatic Interactions
Select Aromatic Residues
cmd.select("aromatic", "resn PHE+TYR+TRP")
cmd.select("aromatic_HIS", "resn HIS")
Visualize Aromatic Rings
cmd.show("sticks", "resn PHE+TYR+TRP")
cmd.color("purple", "resn PHE+TYR+TRP and elem C")
Data Extraction
Get FASTA Sequence
seq = cmd.get_fastastr("object_name")
print(seq)
Get Residue List
residues = []
cmd.iterate(
"object and name CA",
"residues.append((resi, resn))",
space={"residues": residues}
)
print("Residues: " + str(len(residues)))
Get Coordinates
coords = cmd.get_coords("selection")
print("First atom: " + str(coords[0]))
Center of Mass
com = cmd.centerofmass("selection")
print("Center: " + str([round(c, 2) for c in com]))
Get Model Object (Full Atom Data)
model = cmd.get_model("selection")
for atom in model.atom:
print(f"{atom.resn} {atom.resi} {atom.name}")
Iterate with Atom Properties
Available properties: name, resn, resi, chain, b (B-factor), q (occupancy), ss (secondary structure), elem, x, y, z
helices = []
cmd.iterate("name CA", "helices.append(resi) if ss=='H' else None",
space={"helices": helices})
bfactors = []
cmd.iterate("name CA", "bfactors.append(b)", space={"bfactors": bfactors})
avg_b = sum(bfactors) / len(bfactors)
low_occ = []
cmd.iterate("all", "low_occ.append((resi, name)) if q < 1.0 else None",
space={"low_occ": low_occ})
Pseudoatoms and Markers
Create Marker at Center of Mass
com = cmd.centerofmass("selection")
cmd.pseudoatom("marker", pos=com)
cmd.show("spheres", "marker")
cmd.color("red", "marker")
cmd.set("sphere_scale", 1.0, "marker")
Create Origin Marker
cmd.pseudoatom("origin", pos=[0, 0, 0])
cmd.show("spheres", "origin")
cmd.color("blue", "origin")
Bounding Box
Get Extent (Bounding Box)
extent = cmd.get_extent("selection")
print("Min: " + str(extent[0]))
print("Max: " + str(extent[1]))
Calculate Box Size
extent = cmd.get_extent("selection")
size = [extent[1][i] - extent[0][i] for i in range(3)]
print("Box dimensions: " + str([round(s, 1) for s in size]))
Calculate Box Center
extent = cmd.get_extent("selection")
center = [(extent[0][i] + extent[1][i]) / 2 for i in range(3)]
print("Box center: " + str([round(c, 1) for c in center]))
Custom Colors
Define Custom Color
cmd.set_color("my_color", [0.5, 0.8, 0.3])
cmd.color("my_color", "selection")
Common Custom Colors
cmd.set_color("light_gray", [0.8, 0.8, 0.8])
cmd.set_color("dark_gray", [0.4, 0.4, 0.4])
cmd.set_color("light_blue", [0.6, 0.8, 1.0])
cmd.set_color("coral", [1.0, 0.5, 0.31])
Representation-Specific Colors
cmd.set("stick_color", "gray", "selection")
cmd.set("cartoon_color", "green", "selection")
cmd.set("sphere_color", "red", "selection")