Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save fomightez/23aa329632f62b2f735338e08191f8a5 to your computer and use it in GitHub Desktop.

Select an option

Save fomightez/23aa329632f62b2f735338e08191f8a5 to your computer and use it in GitHub Desktop.
Cartoon representation option for the core code sampling various combinations of applying colors from a specific palette to the chains of a large macromolecular complex
# Compromises quality for speed/efficiency
# REPLACE CONTENT OF THE CELL WITH THIS CODE, or use a varation of it if you just prefer cartoon represenation for your use:
text_2_save_templ = '''#!/usr/bin/python
import sys, os
# pymol environment
moddir='/opt/pymol-svn/modules'
sys.path.insert(0, moddir)
os.environ['PYMOL_PATH'] = os.path.join(moddir, 'pymol/pymol_path')
import pymol
cmd = pymol.cmd
# end of set-up
# get the structure & load it into PyMol
cmd.fetch('the_STRUCTURE_PLACEHOLDER')
cmd.zoom()
# set my preferred starting point for representation
cmd.set ("ray_opaque_background", 0)
cmd.set ("cartoon_fancy_helices", 1)
cmd.bg_color ("white")
cmd.set ("cartoon_side_chain_helper", "on")
#cmd.hide ("everything", "all")
#set desired orientation
def set_my_view():
cmd.set_view("the_ORIENTATION_PLACEHOLDER")
set_my_view()
# Generate an image for a few color combinations ( NOT ALL)
number_shuffles_to_run = the_SHUFFLE_NUMBER_PLACEHOLDER
# define any custom colors
def hex_to_rgb(value):
#based on https://stackoverflow.com/a/214657/8508004
value = value.lstrip('#')
lv = len(value)
return tuple(int(value[i:i + lv // 3], 16) for i in range(0, lv, lv // 3))
cmd.set_color ("my_color", [*hex_to_rgb("#7D80B0")])
# specify the palette of colors to use
color_list = the_COLOR_LIST_PLACEHOLDER
# determine ids of loaded chains, see `iterating over residue secondary structure and color.md`
names_of_structures_loaded = cmd.get_names()
the_chains = cmd.get_chains(names_of_structures_loaded[0])
print ("There are {} chains.".format(len(the_chains)))
stored.elems = []
cmd.iterate ("metals", "stored.elems.append(elem)")
the_metals = list(set(stored.elems))
if the_metals:
print ("There are {} metal ions.".format(len(the_metals)))
the_chains_n_metals = the_chains + the_metals
the_chains_as_selections = ["chain {}".format(x) for x in the_chains]
the_metals_as_selections = ["resn {}".format(x) for x in the_metals]
items_to_color = the_chains_as_selections + the_metals_as_selections
#base display styling
#cmd.show ("surface", "polymer.protein")
#show metals as speheres
for sm in the_metals_as_selections:
cmd.show ("spheres", sm)
cmd.set ("fog_start", 0.70) #move fog back farther so not hitting main part as much
import random
from itertools import cycle
f=open('color_lists.txt','w')
cmd.util.performance(100)
for x in range(number_shuffles_to_run):
random.seed(x)
random.shuffle(color_list)
pymol_colors = cycle(color_list)
applied = []
for i in items_to_color:
clr_4_now = next(pymol_colors)
cmd.color(clr_4_now, i) # Colors spheres and other representations
cmd.set("cartoon_color", clr_4_now, i) # Colors cartoons specifically
applied.append((i,clr_4_now))
cmd.png("img_{}.png".format(x), width=500, height=500, dpi=72, ray=1, quiet=0)
f.write("seed {}: {}\\n".format(x,applied)) #note double
# backslash to escape necessary or get `SyntaxError: EOL while scanning string
# literal` when hit that line
f.flush();os.fsync(f.fileno()) #added so even if task gets killed, I get
# color list for what processed thus far; based on
#https://stackoverflow.com/a/19756479/8508004
f.close()
'''
text_2_save = text_2_save_templ.replace("the_STRUCTURE_PLACEHOLDER",PDB_id_code)
text_2_save = text_2_save.replace("the_COLOR_LIST_PLACEHOLDER",str(my_colors_as_strings))
text_2_save = text_2_save.replace("the_ORIENTATION_PLACEHOLDER",orientation_string)
text_2_save = text_2_save.replace("the_SHUFFLE_NUMBER_PLACEHOLDER",str(shuffles_to_do))
%store text_2_save >script.py
!pymol -cq script.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment