MindSponge分子动力学模拟——使用MDAnalysis工具进行后分析(2024.02)

技术背景

分子动力学模拟(Molecule Dynamics Simulation,MD),本质上是一门采样技术。通过配置力场参数、拓扑结构和积分器,对一个给定的体系不断的采样,最终得到一系列的轨迹。那么得到分子动力学模拟的轨迹之后,如何使用后分析工具进行轨迹分析,也是一项很重要的工作。目前来说,基于Python的开源工具MDAnalysis(简称mda)是一个比较常用的MD后分析工具。本文主要介绍基于MindSponge分子动力学模拟框架生成了相应的轨迹之后,如何使用MDAnalysis工具进行分析。

环境配置

需要说明的是,MindSponge当前主要有两个版本,一个是华为MindSpore下的官方仓库MindScience,这里面包含了多个工具的正式发布版本,其中也有MindSponge,相对而言功能比较稳定,但是需要编译构建和安装使用。另外一个仓库是MindSponge,是MindSponge开发团队维护的一个develop版本,这个仓库只要git clone下来就可以测试和使用。本文章中的相关代码是基于后者来实现的,暂时没上正式版仓库。关于MindSponge的安装和基本使用方法,可以参考下之前的文章,所有的内容都是开源免费的。

然后MDAnalysis可以用pip直接安装(这里我们使用的是pip清华源):

$ python3 -m pip install mdanalysis --upgrade
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Requirement already satisfied: mdanalysis in /home/dechin/anaconda3/envs/mindsponge/lib/python3.9/site-packages (2.7.0)
Requirement already satisfied: numpy<2.0,>=1.22.3 in /home/dechin/anaconda3/envs/mindsponge/lib/python3.9/site-packages (from mdanalysis) (1.24.0)
Requirement already satisfied: GridDataFormats>=0.4.0 in /home/dechin/anaconda3/envs/mindsponge/lib/python3.9/site-packages (from mdanalysis) (1.0.2)
Requirement already satisfied: mmtf-python>=1.0.0 in /home/dechin/anaconda3/envs/mindsponge/lib/python3.9/site-packages (from mdanalysis) (1.1.3)
Requirement already satisfied: joblib>=0.12 in /home/dechin/.local/lib/python3.9/site-packages (from mdanalysis) (1.2.0)
Requirement already satisfied: scipy>=1.5.0 in /home/dechin/anaconda3/envs/mindsponge/lib/python3.9/site-packages (from mdanalysis) (1.10.1)
Requirement already satisfied: matplotlib>=1.5.1 in /home/dechin/anaconda3/envs/mindsponge/lib/python3.9/site-packages (from mdanalysis) (3.7.1)
Requirement already satisfied: tqdm>=4.43.0 in /home/dechin/anaconda3/envs/mindsponge/lib/python3.9/site-packages (from mdanalysis) (4.65.0)
Requirement already satisfied: threadpoolctl in /home/dechin/.local/lib/python3.9/site-packages (from mdanalysis) (3.1.0)
Requirement already satisfied: packaging in /home/dechin/anaconda3/envs/mindsponge/lib/python3.9/site-packages (from mdanalysis) (23.0)
Requirement already satisfied: fasteners in /home/dechin/anaconda3/envs/mindsponge/lib/python3.9/site-packages (from mdanalysis) (0.19)
Requirement already satisfied: mda-xdrlib in /home/dechin/anaconda3/envs/mindsponge/lib/python3.9/site-packages (from mdanalysis) (0.2.0)
Requirement already satisfied: mrcfile in /home/dechin/anaconda3/envs/mindsponge/lib/python3.9/site-packages (from GridDataFormats>=0.4.0->mdanalysis) (1.5.0)
Requirement already satisfied: contourpy>=1.0.1 in /home/dechin/anaconda3/envs/mindsponge/lib/python3.9/site-packages (from matplotlib>=1.5.1->mdanalysis) (1.0.7)
Requirement already satisfied: cycler>=0.10 in /home/dechin/anaconda3/envs/mindsponge/lib/python3.9/site-packages (from matplotlib>=1.5.1->mdanalysis) (0.11.0)
Requirement already satisfied: fonttools>=4.22.0 in /home/dechin/anaconda3/envs/mindsponge/lib/python3.9/site-packages (from matplotlib>=1.5.1->mdanalysis) (4.38.0)
Requirement already satisfied: kiwisolver>=1.0.1 in /home/dechin/anaconda3/envs/mindsponge/lib/python3.9/site-packages (from matplotlib>=1.5.1->mdanalysis) (1.4.4)
Requirement already satisfied: pillow>=6.2.0 in /home/dechin/anaconda3/envs/mindsponge/lib/python3.9/site-packages (from matplotlib>=1.5.1->mdanalysis) (9.4.0)
Requirement already satisfied: pyparsing>=2.3.1 in /home/dechin/anaconda3/envs/mindsponge/lib/python3.9/site-packages (from matplotlib>=1.5.1->mdanalysis) (3.0.9)
Requirement already satisfied: python-dateutil>=2.7 in /home/dechin/anaconda3/envs/mindsponge/lib/python3.9/site-packages (from matplotlib>=1.5.1->mdanalysis) (2.8.2)
Requirement already satisfied: importlib-resources>=3.2.0 in /home/dechin/anaconda3/envs/mindsponge/lib/python3.9/site-packages (from matplotlib>=1.5.1->mdanalysis) (5.12.0)
Requirement already satisfied: msgpack>=1.0.0 in /home/dechin/anaconda3/envs/mindsponge/lib/python3.9/site-packages (from mmtf-python>=1.0.0->mdanalysis) (1.0.5)
Requirement already satisfied: zipp>=3.1.0 in /home/dechin/anaconda3/envs/mindsponge/lib/python3.9/site-packages (from importlib-resources>=3.2.0->matplotlib>=1.5.1->mdanalysis) (3.11.0)
Requirement already satisfied: six>=1.5 in /home/dechin/anaconda3/envs/mindsponge/lib/python3.9/site-packages (from python-dateutil>=2.7->matplotlib>=1.5.1->mdanalysis) (1.16.0)

安装完成后,就可以先用MindSponge生成一个用于后分析的轨迹,再调用MDAnalysis进行分析。

生成轨迹

这里我们使用的案例轨迹,还是前一篇文章中所用到的能量极小化的一个案例。模拟的分子是这个样子的:

分子动力学模拟的相关代码如下:

from mindspore import nn, context
import numpy as np
import sys
# 添加sponge所在的路径,这样就不需要安装即可直接使用
sys.path.insert(0, '../..')
from sponge import ForceField, Sponge, set_global_units, Protein
from sponge.callback import RunInfo, WriteH5MD, SaveLastPdb
from sponge.colvar import Distance, Angle, Torsion

# 配置MindSpore的执行环境
context.set_context(mode=context.GRAPH_MODE, device_target='GPU', device_id=1)
# 配置全局单位
set_global_units('A', 'kcal/mol')

# 定义一个基于case1.pdb的分子系统
system = Protein('../pdb/case1.pdb', template=['protein0.yaml'], rebuild_hydrogen=True)
# 定义一个amber.ff99sb的力场
energy = ForceField(system, parameters=['AMBER.FF99SB'])
# 定义一个学习率为1e-03的Adam优化器
min_opt = nn.Adam(system.trainable_params(), 1e-03)

cv_bond = Distance([0, 1])
cv_angle = Angle([0, 1, 2])
cv_dihedral = Torsion([0, 1, 2, 3])
# 定义一个用于执行分子模拟的Sponge实例
md = Sponge(system, potential=energy, optimizer=min_opt, metrics={'bond': cv_bond, 'angle': cv_angle,
                                                                  'dihedral': cv_dihedral})

# RunInfo这个回调函数可以在屏幕上根据指定频次输出能量参数
run_info = RunInfo(20)
# WriteH5MD回调函数,可以将轨迹、能量、力和速度等参数保留到一个hdf5文件中,文件后缀为h5md
cb_h5md = WriteH5MD(system, 'test.h5md', save_freq=10, write_image=False, save_last_pdb='last_pdb.pdb')
# 保存PDB文件
bonds = np.array([[0, 56], [6, 10]], np.int32)
# 开始执行分子动力学模拟,运行2000次迭代
md.run(200, callbacks=[run_info, cb_h5md])

运行结束后,会在当前路径下生成一个名为last_pdb.pdb的构象文件和一个test.h5md的轨迹文件。关于h5md格式的轨迹文件,可以用silx这个工具来进行直观的可视化:

这是体系能量极小化过程中的能量变化曲线:

并且保存了轨迹数据:

MDAnalysis分析

使用MDAnalysis进行分析的主要流程,就是用拓扑结构文件和轨迹文件构建两个MDAnalysis.Universe对象。这里拓扑结构文件可以使用pdb文件,但要求pdb文件中包含有CONECT成键相互关系,否则跟成键相互作用相关的内容使用mda无法分析,MindSponge所生成的pdb文件中是包含了成键关系信息的。再者就是h5md也是mda所支持的轨迹文件扩展名,使用MindSponge生成的轨迹可以直接用mda加载:

import MDAnalysis as mda
u = mda.Universe('last_pdb.pdb', 'test.h5md')

加载完之后,我们可以打印其中的一些关键信息,比如原子类型和残基类型等:

print('Atom Types List:\n', u.atoms)
# Atom Types List:
# <AtomGroup [<Atom 1: N of type N of resname ALA, resid 1 and segid A and altLoc >, <Atom 2: CA of type C of resname ALA, resid 1 and segid A and altLoc >, <Atom 3: CB of type C of resname ALA, resid 1 and segid A and altLoc >, ..., <Atom 55: HB1 of type H of resname ALA, resid 4 and segid A and altLoc >, <Atom 56: HB2 of type H of resname ALA, resid 4 and segid A and altLoc >, <Atom 57: HB3 of type H of resname ALA, resid 4 and segid A and altLoc >]>
print('Residue Types List:\n', u.residues)
# Residue Types List:
# <ResidueGroup [<Residue ALA, 1>, <Residue ARG, 2>, <Residue ALA, 3>, <Residue ALA, 4>]>
print('Step 0 Coordinates Shape:\n', np.array(u.coord).shape)
# Step 0 Coordinates Shape:
# (57, 3)
print('C Atoms:\n', u.select_atoms('name C'))
# C Atoms:
# <AtomGroup [<Atom 4: C of type C of resname ALA, resid 1 and segid A and altLoc >, <Atom 22: C of type C of resname ARG, resid 2 and segid A and altLoc >, <Atom 40: C of type C of resname ALA, resid 3 and segid A and altLoc >, <Atom 50: C of type C of resname ALA, resid 4 and segid A and altLoc >]>
print('Contact Map:\n', contact_matrix(np.array(u.coord)))
# Contact Map:
# [[ True  True  True ...  True  True  True]
# [ True  True  True ...  True  True  True]
# [ True  True  True ...  True  True  True]
# ...
# [ True  True  True ...  True  True  True]
# [ True  True  True ...  True  True  True]
# [ True  True  True ...  True  True  True]]
print(u.bonds)
# <TopologyGroup containing 56 bonds>

然后是一些跟轨迹相关的条目:

print('Number of Frames in Trajectory:\n', u.trajectory.n_frames)
# Number of Frames in Trajectory:
# 20
print('Number of Atoms:\n', u.trajectory.n_atoms)
# Number of Atoms:
# 57
print(u.trajectory.has_positions)
# True
print(u.trajectory.ts.positions.shape)
# (57, 3)
print(u.trajectory.ts.has_velocities)
# False
print(u.trajectory.ts.has_forces)
# False
print(u.trajectory[0].data)
# {'trajectory': 10, 'time': 0.010000000707805157, 'step': 10}
print(u.trajectory[1].positions[0])
# [ -0.11355944 -11.455442    -0.79421705]

因为我们在定义CallBack的时候没有在轨迹中保存速度参量和力参量,因此这里has_velocitieshas_forces两个的值都是False,但实际上我们是可以支持在中间轨迹把这两个参量写入到h5md文件中的。由于轨迹有很多帧,在mda里面我们可以直接对u.trajectory使用索引,来定位到特定的某一帧,再导出自己所需要的参量。除了单点分析,我们还可以定义一个reference trajectory来计算RMSD等参数:

ref = mda.Universe('last_pdb.pdb', 'test.h5md')
R = RMSD(u, ref, select="backbone", groupselections=['backbone and resid 1-4'])
R.run()
rmsd = R.results.rmsd.T
print (rmsd[2])

更多的MDAnalysis工具的使用方法和函数接口,可以参考MDAnalysis官方文档或者是这个中文翻译版文档

总结概要

这篇文章我们主要介绍了MindSponge分子动力学模拟软件如何跟后分析工具MDAnalysis相配合的方法,其主要操作流程就是调用MindSponge自带的CallBack来输出拓扑文件和轨迹文件给MDAnalysis,然后就可以调用MDAnalysis的相关分析函数接口,十分的方便。

版权声明

本文首发链接为:https://www.cnblogs.com/dechinphy/p/mda-mds.html

作者ID:DechinPhy

更多原著文章:https://www.cnblogs.com/dechinphy/

请博主喝咖啡:https://www.cnblogs.com/dechinphy/gallery/image/379634.html

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MDAnalysis 分析分子动力学轨迹 MDAnalysis-0.7.2.tar.gz MDAnalysis is an object-oriented python toolkit to analyze molecular dynamics trajectories generated by CHARMM, Gromacs, NAMD, LAMMPS, or Amber. It allows one to read molecular dynamics trajectories and access the atomic coordinates through numpy arrays. This provides an extremely flexible and relatively fast framework for complex analysis tasks. In addition, CHARMM-style atom selection commands are implemented. Trajectories can also be manipulated (for instance, fit to a reference structure) and written out. A typical usage pattern is to iterate through a trajectory and analyze coordinates for every frame. In the following example the end-to-end distance of a protein and the radius of gyration of the backbone atoms are calculated: import MDAnalysis from MDAnalysis.tests.datafiles import PSF,DCD # test trajectory import numpy.linalg u = MDAnalysis.Universe(PSF,DCD) # always start with a Universe nterm = u.s4AKE.N[0] # can access structure via segid (s4AKE) and atom name cterm = u.s4AKE.C[-1] # ... takes the last atom named 'C' bb = u.selectAtoms('protein and backbone') # a selection (a AtomGroup) for ts in u.trajectory: # iterate through all frames r = cterm.pos - nterm.pos # end-to-end vector from atom positions d = numpy.linalg.norm(r) # end-to-end distance rgyr = bb.radiusOfGyration() # method of a AtomGroup; updates with each frame print "frame = %d: d = %f Angstroem, Rgyr = %f Angstroem" % (ts.frame, d, rgyr)
管理层讨论与分析是一种组织决策过程中非常重要的环节。在这个过程中,管理层会就某个特定问题或挑战进行深入的讨论和分析,以制定最佳的行动计划。 首先,管理层讨论是为了加强沟通和协商。在一个组织中,不同的管理层成员可能拥有不同的观点和意见,通过讨论和分析这些观点和意见,可以提高彼此之间的理解和信任,进而找到共识,为组织制定合适的战略和决策。 其次,管理层讨论和分析还可以帮助识别问题和挑战。通过深入的讨论和分析,管理层可以更好地了解组织内部和外部的情况,发现可能存在的问题和挑战。这样,他们就可以及时采取行动来解决这些问题和挑战,避免对组织造成严重的负面影响。 另外,管理层讨论和分析是为了优化资源的分配。在一个组织中,资源是有限的,需要合理地进行分配。通过讨论和分析,管理层可以评估不同的用途和方案,确定最佳的资源分配策略。这可以帮助组织提高效率和效益,使资源得到最大的利用和回报。 最后,管理层讨论和分析还有助于提高组织的决策质量。通过深入的讨论和分析,管理层可以全面地评估各种可能性和风险,从而作出更明智和可持续的决策。这将有助于组织在竞争激烈的市场环境中保持竞争优势,并实现长期的可持续发展。 总之,管理层讨论与分析是组织决策过程中不可或缺的环节。通过这个过程,管理层可以加强沟通和协商,识别问题和挑战,优化资源分配,提高决策质量,从而为组织的成功和发展提供有力的支持。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值