计算化学公社

 找回密码 Forget password
 注册 Register
Views: 3405|回复 Reply: 5

[VMD] 用matplotlib或gnuplot给VMD定义渐变色标尺

[复制链接 Copy URL]

1416

帖子

6

威望

3174

eV
积分
4710

Level 6 (一方通行)

傻傻的木瓜

发表于 Post on 2025-1-8 11:37:30 | 显示全部楼层 Show all |阅读模式 Reading model
本帖最后由 Uus/pMeC6H4-/キ 于 2025-1-11 17:10 编辑

用matplotlib或gnuplot给VMD定义渐变色标尺
Defining gradient color scale for VMD using matplotlib or gnuplot
Uus_pMeC6H4- | Init post 2025-01-08 / Last edit 2025-01-11


一、前言

If you're feeling lonely and you're still searching for your true friends, just look up in the sky. Who knows? Maybe you and your future best friends are all looking at the same rainbow.

1.9.3版VMD可绘制的颜色共有1057种,其中0-32号这33种为给定RGB的有名颜色,33-1056号这1024种为线性组合起中末三色RGB产生的渐变色,每种颜色的RGB值均由三个0到1的浮点数定义;颜色的定义可在VMD的Graphics - Colors窗口设置,或在VMD的命令行用colorinfo指令查询、color指令修改。目前仍在开发中的1.9.4版VMD扩展了颜色支持,可对比在此帖5楼和6楼展示的,分别对应于1.9.3版和1.9.4版颜色设置窗口的截图。

然而1.9.3版VMD因稳定性更佳仍广泛使用,此时内置的颜色略显不足,需要编写tcl脚本修改。比如,为使原子按元素着色,社长的在VMD中使用GaussView的元素着色的方法http://sobereva.com/652)提供了gview_color.tcl以修改101-211号颜色,而slxc920113的VMD使用Jmol风格的颜色策略提供了JmolColor.tcl以修改101-195号颜色。又比如,为使蛋白质结构按原子序号着色,ene在在VMD中以PyMOL rainbow着色风格绘制蛋白质结构提供了pymol_rainbow.tcl(更新为rainbow_v2.tcl)以修改渐变色。

VMD内置的渐变色方案种类偏少,起中末三色选择仅限于红绿蓝白灰黑的几个搭配。另一个可视化程序OVITO虽然内置也不多,但亦可加载外部图像定义渐变色来扩展可能性。而且VMD混色模式的算法亦不合理,正如liyuanhe211在用 PyMOL 渲染 HSV 混色模式的 RDG 填色等值面图所言,VMD和PyMOL的线性加权平均混色模式会导致部分颜色显得暗、“污”,但PyMOL可定义任意多颜色节点来产生渐变色来间接解决,而VMD自己只能定义起中末三个。同样是RDG分析,欧拉角在[Quantum ESPRESSO] 固体和表面的非共价相互作用分析一帖采用的是VESTA按内置的彩虹渐变色方案绘制填色等值面,以及matplotlib用jet渐变色方案绘制散点图;VESTA可在Properties - Sections的Sections and slices区设置渐变色,内置方案总体也比VMD的明亮些。

本帖将介绍如何用matplotlibgnuplot这两种常用科学绘图工具定义渐变色,载入VMD替换内置方案获得更好的绘制效果。相关脚本在下述文件包里已提供,后缀为.py的是python脚本,.gp的是gnuplot脚本。使用前放入VMD目录,这样方便在VMD的命令行找到执行脚本产生的文件。脚本是在Windows下写的,如果放到Linux系统下可能需要chmod添加权限和sed -i "s/\r$//"修改换行符等操作。
extcolor.zip (2.28 KB, 下载次数 Times of downloads: 52)

评分 Rate

参与人数
Participants 14
威望 +2 eV +56 收起 理由
Reason
SharkYYX2025 + 3 牛!
爱喝茶 + 5 牛!
RandomError + 5 GJ!
LittlePupil + 5 GJ!
南北多歧路 + 3 不明觉厉
叁拾叁 + 4 牛!
SiqiLee + 5 GJ!
hdhxx123 + 3 GJ!
丁越 + 5 赞!
zsu007 + 5 赞!
sobereva + 2 精品内容
Serious + 5 GJ!
imasen + 3
student0618 + 5 精品内容

查看全部评分 View all ratings

√546=23.36664289109

1416

帖子

6

威望

3174

eV
积分
4710

Level 6 (一方通行)

傻傻的木瓜

 楼主 Author| 发表于 Post on 2025-1-10 15:46:47 | 显示全部楼层 Show all
本帖最后由 Uus/pMeC6H4-/キ 于 2025-1-11 17:11 编辑

第一篇精品帖达成,谢谢大家!这两天稍微做了点修改,补充几个刚搜索到的同样涉及渐变色绘制的讨论帖链接,以及添加一段matplotlib用HSV混色模式定义渐变色的示例,脚本也有小变动,所以顶一顶。开头和脚本里的署名是有意修改的,去掉了我现在用户名里的非ASCII字符。

gnuplot生成的.vmd脚本似乎总要在每行开头加个空格,虽然不影响使用但尝试多次也没法移除。

2025-01-11编辑:修正了一处可能引起误解的脱漏,并添加一个讲解颜色知识的外部链接。现在想来这篇帖子发得太急了,应该早点开始多打磨下的。另外我也没意识到800*800的图对论坛页面而言这么大……
√546=23.36664289109

1416

帖子

6

威望

3174

eV
积分
4710

Level 6 (一方通行)

傻傻的木瓜

 楼主 Author| 发表于 Post on 2025-1-12 21:25:16 | 显示全部楼层 Show all
今天刚看到ggdh的这个帖子,钟老师指出VMD里有指令color scale colors <mode> <c1> <c2> <c3>可以修改内置渐变色方案的起中末所用颜色,其中mode是RWB、BGR等内置方案的名称,c1、c2、c3在0到1056号之间。这样前言里“起中末三色选择仅限于红绿蓝白灰黑的几个搭配”的描述并不完全准确,但行文不好改了,不过只有三个颜色节点来线性插值混色的结论不变。

注意这条指令和总结里列表倒数第三点是类似的,不兼容外部定义的渐变色。例如先color scale method BWR指定内置方案名为BWR,再source rainbow.vmd把颜色改成rainbow渐变色方案的,假如此时又执行color scale colors RGB 1 3 4,那么RGB方案被改成红(1)橙(3)黄(4)渐变时BWR方案也被退回内置的蓝白红渐变,rainbow.vmd失效。总之,给VMD加载外部渐变色后不能再动相关设置!

另外,Choosing Colormaps in Matplotlib介绍的matplotlib的内置渐变色方案可批量导出,以便无python情形使用。由于文件大小限制(以及懒得传网盘),按类别分成两个内容不同的独立压缩包传附件。

mpl_cm_pt1.7z (6.68 MB, 下载次数 Times of downloads: 10)
√546=23.36664289109

1416

帖子

6

威望

3174

eV
积分
4710

Level 6 (一方通行)

傻傻的木瓜

 楼主 Author| 发表于 Post on 2025-1-18 16:50:53 | 显示全部楼层 Show all
本帖最后由 Uus/pMeC6H4-/キ 于 2025-1-20 14:27 编辑

考古中又看到这帖问了VMD如何扩展渐变色方案种类的问题,社长回复了个博客链接。此站搭载于Google Site,如今网页已失效,不过Wayback Machine能找到一份存档。今将文字内容复制如下以便参考:
Matthew Perkett wrote in Fun with VMD:
Color a protein by RMSF using a custom color scale
It is often useful to calculate the Root Mean Squared Fluctuation (RMSF) for each residue in a protein (which can also be converted the crystal B-factors) since this gives a measure of how flexible each part of the protein is.  This is easily achieved using a helper program such as g_rmsf, which is included in the Gromacs MD package.  To visualize this on the protein, you can set the beta value for each residue to the RMSF (or B-factor) and then color the protein by Beta. VMD provides a number of built-in color scales, but if you would like to use a custom color scale, you need to do a little more work.  Here I will describe how to generate a custom color scale using the output from a g_rmsf calculation.

Run the code

  • Download ms2_cc.pdb, rmsf.xvg, and colorByRMSF.vmd into the same directory.
  • Open VMD and use the TkConsole to navigate to the download directory
  • The following commands in the TkConsole will load and color the ms2_cc.pdb file
    1. mol new ms2_cc.pdb type pdb
    2. source colorByRMSF.vmd
    3. colorByRMSF top rmsf.xvg 0.05 0.4
    复制代码

syntax:
colorByRMSF <molid> <rmsf_file> <lower_bound> <upper_bound>
<molid>: the molecule ID
<rmsf_file>: the rmsf.xvg output file generated by g_rmsf from which RMSF values will be read for each atom
<lower_bound>: the minimum value for the color scale (all lower values will be set to lower_bound)
<upper_bound>: the maximum value for the color scale (all higher values will be set to upper_bound)

Setting the upper and lower bounds allows you to explicitly control the start and end of the color scale between different loaded structures.  This is necessary for comparing the plots and has the advantage of only needing a single color legend.

When run properly, you will get a structure with a custom Teal -> Orange -> Magenta color scale that looks like Fig. 1.

How does it work

The majority of the procedure colorByRMSF is reading the RMSF values into beta for each atom.  Modifying the color scale is done by invoking the procedure tricolor.  Setting a representation to color by beta value scales the beta values to correspond to color IDs between between 32 and 1056.  This procedure modifies these color IDs to have custom RGB values, which are determined by a linear interpolation between three specified colors (in this case Cyan -> Orange -> Magenta).  See the procedure tricolor for more details.

Defining custom colors and generating a legend

To generate your own custom three-color scale, you need only modify three lines to give the three RGB colors you would like to use.
  1.     set firstColor { 0.0 0.88 1.0 }
  2.     set middleColor { 1.0 0.25 0.0 }
  3.     set lastColor { 0.9 0.0 0.9 }
复制代码

To do this, it is useful to use Mathematica to determine the desired RGB values and also generate a color scale.  The notebook colorSlider.nb (generated in Mathematica 10), will allow you to select different colors and determine their RGB values.  You can then copy these RGB values to the variables c1, c2, c3 to generate a custom color scale bar.  

It is simple to modify these procedures to work with other custom color functions, and you certainly are not restricted to coloring by RMSF.  For example, Fig. 2 was generated by setting beta values to the residue number.

博文相关的文件在这个压缩包里:
fun_with_vmd.7z (57.36 KB, 下载次数 Times of downloads: 7)
√546=23.36664289109

26

帖子

0

威望

1279

eV
积分
1305

Level 4 (黑子)

发表于 Post on 2025-1-18 22:36:25 | 显示全部楼层 Show all
叹为观止
我才是旅途的终点!

1416

帖子

6

威望

3174

eV
积分
4710

Level 6 (一方通行)

傻傻的木瓜

 楼主 Author| 发表于 Post on 2025-1-20 15:32:17 | 显示全部楼层 Show all
本帖最后由 Uus/pMeC6H4-/キ 于 2025-2-2 15:57 编辑

两天前的一段讨论值得用一条新回复解释清楚,感谢楼上的评论避免了我在连续盖楼的观感(笑)

注意到此帖的tricolor.tcl也有tricolor_scale,产生的“渐变色”方案只有三个不同的RGB值,用来给电子定域化函数(ELF)的等值面图按不同区域着色。社长曾有博文《在Chimera中用不同的颜色显示不同的等值面》(http://sobereva.com/168)和帖子[Multiwfn视频演示] 使用Multiwfn和ChimeraX绘制自定义着色的电子定域化函数(ELF)等值面图介绍了Chimera/ChimeraX里实现的方法,另外liyuanhe211也在[波函数分析交流] 分块自定义着色/隐藏 (填色) 等值面 (RDG/ELF) 区块的工具介绍了Pymol里实现的方法;但经过此帖的讨论后,Multiwfn已更新程序和辅助脚本来支持VMD实现了,详见手册4.17.10的解释。简单来说,Multiwfn在用主功能17对ELF做盆分析后,将盆导出为格点数据时用特殊选项b,同时导出ELF数据本身basinfunc.cub和monosynaptic/disynaptic/other分类的判定用basinsyn.cub;然后用examples/scripts文件夹下basinsyn.vmd脚本把两个cube文件同时载入VMD,以color scale method GWR设置渐变色方案为绿白红,按basinfunc.cub绘制等值面并按basinsyn.cub着色。

basinsyn.cub的判定作用类似于numpy数组操作经常涉及的mask,作为“掩码”或“遮罩”辅助确定主要数据的处理或展示方式,每个格点上的数据只有0,1,-1三个可能。正如本回复第一个链接里tricolor.tcl所示,按“遮罩”着色时颜色方案不需要连续渐变,少数几个离散的RGB值即足够使用;此类方案称为Qualitative colormaps,用matplotlib的ListedColormap可以创建,palettable或者pypalettes还有更多的选择。

这种思路也可以扩展到其他实空间函数:先照常用Multiwfn的主功能5计算格点数据并导出主要的cube文件,再按社长博文《使用Multiwfn做电子密度、ELF、静电势、密度差等函数的盆分析》(http://sobereva.com/179)的方法用主功能17做盆分析,用子功能-5的特殊选项a导出按盆编号给格点赋值的basin.cub(比只有0和正负1的选项b更灵活),然后根据实际着色需要适当合并编号(用文本编辑器的替换功能即可),定义相应的离散型颜色方案,最后把两个cube文件同时载入VMD并作图。当然等值面的isovalue选择就得多考虑一个问题,如果等值面太大而与盆边界相交,单个区域(在Chimera里叫blob)就会同时有两个盆对应的颜色。
√546=23.36664289109

本版积分规则 Credits rule

手机版 Mobile version|北京科音自然科学研究中心 Beijing Kein Research Center for Natural Sciences|京公网安备 11010502035419号|计算化学公社 — 北京科音旗下高水平计算化学交流论坛 ( 京ICP备14038949号-1 )|网站地图

GMT+8, 2026-8-9 00:09 , Processed in 0.240892 second(s), 25 queries , Gzip On.

快速回复 返回顶部 返回列表 Return to list