Import the standard Python modules used by the example code.
Import the additional modules and classes needed.
The ColorOption class facilitates interoperation between Chimera
colors and color wells (which use rgba colors).
ColorWellDialog is a "nameless" dialog. See the
Extension-Specific User Interface example for a more detailed
explanation of Chimera dialogs.
Set the title bar of the dialog to display
Create a ColorOption instance. The ColorOption will contain
a descriptive label and a color well. The arguments to the
ColorOption constructor are:
Call
Define
Loop through all atoms in all molecules, looking for protein
backbone atoms. When one is found, its color is compared
against the last color seen,
Set the color of the well to match
The
Handle case 2. Set the color well to the proper color
Handle case 1. Set the color well to show that no color
is present
Define
Call the
Define the module variable
Define
Declare that the name
Check whether the dialog has already been created. If so,
the dialog window is displayed by calling it's
Otherwise, create the dialog.
Create the Chimera toolbar button that invokes the
import os
import Tkinter
import chimera
from chimera.baseDialog import ModelessDialog
from chimera.tkoptions import ColorOption
import ColorWellUI
class ColorWellDialog(ModelessDialog):
Set Backbone Color.
title = 'Set Backbone Color'
def fillInUI(self, master):
griding the ColorOption into the
master widget. The default column is 0. The tkoptions
module contains other options besides ColorOption (e.g.
StringOption), which are generally intended to be put in
vertical lists, and therefore a row number is specified in
the constructor. In this example we are only using one
option however.
coloropt = ColorOption(master, 0, 'Backbone Color', None, self._setBackboneColor, balloon='Protein backbone color')
_updateBackboneColor to make the color displayed
in the color well reflect the current color of protein
backbone atoms. While not strictly necessary, keeping the
color in the well consistent with the color in the molecules
enhances the extension usability.
self._updateBackboneColor(coloropt)
_updateBackboneColor, which is used to make the color
of a well reflect the color of protein backbone atoms.
def _updateBackboneColor(self, coloroption):
theColor. The first time this
comparison is made, theColor does not exist yet and a
NameError exception is raised; this exception is caught,
and theColor is assigned the color of the atom. All
subsequent times, the comparison between atom color and
theColor should work as expected. If the two colors are
different, the color well is set to show that multiple colors
are present and execution returns to the caller. If the two
colors are the same, the next atom is examined. If only one
color is found among all protein backbone atoms (or zero if
no molecules are present), then execution continues.
for m in chimera.openModels.list(modelTypes=[chimera.Molecule]):
for a in m.atoms:
if ColorWellUI.MAINCHAIN.match(a.name):
try:
if a.color != theColor:
coloroption.setMultiple()
return
except NameError:
theColor = a.color
theColor. There are
two possible cases:
theColor is not set (because there are no molecules),theColor is None or a color object.set function will not result in a callback to
_setBackboneColor.
try:
coloroption.set(theColor)
except NameError:
coloroption.set(None)
_setBackboneColor, which is invoked each time the
color in the well changes. When called by the ColorOption,
_setBackboneColor receives a single argument coloropt,
which is the ColorOption instance.
def _setBackboneColor(self, coloroption):
mainchain function in the main package, with
the color object corresponding to the color well color
as its argument (which will be None if No Color is
the current selection in the well), to set the color of
backbone atoms.
ColorWellUI.mainchain(coloroption.get())
dialog, which keeps track of the
dialog window containing the color well. It is initialized to
None, and is assigned a usable value when the dialog is created.
dialog = None
showColorWellUI, which is invoked when the Chimera
toolbar button is pressed.
def showColorWellUI():
dialog refers to the global variable
defined above.
global dialog
enter()
function, and then the rest of the function is skipped by returning.
if dialog is not None:
dialog.enter()
return
dialog = ColorWellDialog()
showColorWellUI
dir, file = os.path.split(__file__)
icon = os.path.join(dir, 'ColorWellUI.tiff')
chimera.tkgui.app.toolbar.add(icon, showColorWellUI, 'Set Main Chain Color', None)