Specify the color of each residue in a protein (PDB)?

I’m importing a *.pdb file containing a single protein. Mathematica automatically produces a plot of the protein.

I want to specify the color of each residue independently, in this plot. Is this possible?

Additionally, I would like to change the type of plot to “cartoon”. How can I do this?




Not sure what you mean by “cartoon”, and, sadly, the documentation doesn’t seem to give much information on the type of ColorFunction that can be used
– JasonB
Apr 28 at 13:54



@JasonB “Cartoon” is the name given in Pymol and VMD (other protein visualization software) to the ribbon diagram, with the alpha-helices and so on.
– becko
Apr 28 at 13:59



It doesn’t appear to be have many options other than Import[“ExampleData/1PPT.pdb”, “Rendering” -> #] & /@ {“BallAndStick”, “Structure”, “Wireframe”, “Spacefilling”}
– JasonB
Apr 28 at 14:04


2 Answers


Okay, so this is what it looks like with standard coloring of the residues:

Import[“ExampleData/1PPT.pdb”, ColorFunction -> “Residue”]

But now, if we want to change the residue colors, we need to change the value of a certain internal color list called Graphics`MoleculePlotDump`residueColorRules

residuelist = {“Gly”, “Pro”, “Ser”, “Gln”, “Pro”, “Thr”, “Tyr”, “Pro”,
“Gly”, “Asp”, “Asp”, “Ala”, “Pro”, “Val”, “Glu”, “Asp”, “Leu”,
“Ile”, “Arg”, “Phe”, “Tyr”, “Asp”, “Asn”, “Leu”, “Gln”, “Gln”,
“Tyr”, “Leu”, “Asn”, “Val”, “Val”, “Thr”, “Arg”, “His”, “Arg”,
Graphics`MoleculePlotDump`residueColorRules =
Thread[residuelist -> (ColorData[97] /@ Range[36])]

Now when we import the PDB, it uses the new color list,

The definition of Graphics`MoleculePlotDump`residueColorRules will be reset when you restart the kernel.



That’s useful, but I don’t want to color by residue type. I want to color by residue number. Each residue with a particular color
– becko
Apr 28 at 14:39



Good luck, there is like zero documentation on the allowable color functions
– JasonB
Apr 28 at 14:52

Here is a very hack-ish way of coloring each residue sequentially. The trick is in constructing a Blend[] function where the color corresponding to each residue appears twice in the first argument. A slight shift is apparently needed to match up the colors, even if only approximately.

n = StringLength[First[Import[“ExampleData/1PPT.pdb”, “Sequence”]]];
clist = PadRight[{}, n, Values[Association[ColorData[97, “ColorRules”]]]];

ColorFunction -> (Directive[GrayLevel[1/10], Specularity[1/5, 10],
Glow[Blend[Riffle[clist, clist], # – 1/(2 n)]]] &),
Lighting -> “Classic”]