MD轨迹文件的基本I/O
mdio的Python项目详细描述
MDIO
用于读取、写入和操作分子动力学(MD)轨迹文件的Python库。
mdio
旨在提供基本的md文件i/o功能。它不应该取代像mdtraj和mdanalysis这样的优秀软件包,但是当您只需要基本的md轨迹文件i/o而不需要更多东西时,它是一个轻量级的替代品。
例如,下面的脚本将在gromacs .xtc
格式的轨迹文件中读取,去掉水分子,修正由周期性边界条件伪影分割的分子坐标,最小二乘法将每个快照拟合到第一个快照,然后以琥珀色netcdf(.nc
)格式写出新的坐标:
importmdiotopfile='../test/examples/test.gro'trajfile='../test/examples/test.xtc'outfile='protein.nc'traj=mdio.load(trajfile,top=topfile,selection='not water')traj=traj.make_whole()traj=traj.fitted_to(traj[0])traj.save(outfile)
安装
最容易通过pip
。您需要预先安装numpy
和cython
:
% pip install numpy cython
% pip install mdio
用户指南
加载数据
mdio
可以从多种文件格式加载数据:gromacs(.xtc
和.gro
)、namd/charmm(.dcd
)、amber(.nc
、.mdcrd
、.rst
)和pdb(.pdb
)。mdio
自动检测文件格式,因此尽管使用标准文件扩展名可能很有用,但您不必-mdio
很乐意加载扩展名为.traj
(甚至是.xtc
)的琥珀色netcdf文件。.
t=mdio.load('../test/examples/test.nc')print(t)
mdio.Trajectory with 10 frames, 892 atoms and box info.
支持其他读取文件的方式:
f=mdio.open('../test/examples/test.nc',top='../test/examples/test.pdb')t2=f.read()f.close()print(t2)
mdio.Trajectory with 10 frames, 892 atoms and box info.
或者使用上下文管理器,以逐帧方式:
withmdio.open('../test/examples/test.dcd')asf:frames=[]frame=f.read_frame()whileframeisnotNone:frames.append(frame)frame=f.read_frame()t3=mdio.Trajectory(frames)print(t3)
mdio.Trajectory with 10 frames, 892 atoms and box info.
您可以从形状合适的numpy数组创建一个mdio
轨迹对象。mdio
假设阵列中的数字以纳米为单位。
importnumpyasnpxyz=np.random.random((120,55,3))t4=mdio.Trajectory(xyz)print(t4)
mdio.Trajectory with 120 frames, and 55 atoms.
保存数据
轨迹文件也可以用多种方式编写。所需的格式是从文件扩展名推断出来的,因此与文件读取不同,这必须是适当的。
# a) Using the save() method of a trajectory object:t.save('test.nc')# b) Using mopen():withmdio.open('test2.dcd',"w")asf:f.write(t)# c) Frame-by-frame:f=mdio.open('test3.xtc',"w")forframeint.frames():f.write_frame(frame)f.close()
操纵轨迹
a)帧:
轨迹可以切片和连接/附加(如果它们兼容):
t5=t[2:9:3]+t2t5+=t3print(t5)
mdio.Trajectory with 23 frames, 892 atoms and box info.
b)原子级:
可以创建具有选定原子子集的轨迹。有两种方法可以做到这一点。一种是指定原子索引列表:
t6=t.select([0,1,3,5,23,34])print(t6)
mdio.Trajectory with 10 frames, 6 atoms and box info.
另一种方法是指定一个选择字符串,它使用的语法与mdtraj
使用的语法非常相似,请参见here:
t7=t2.select('name CA')print(t7)
mdio.Trajectory with 10 frames, 58 atoms and box info.
c)坐标方向:
提供丰富多样的轨迹分析工具并不是mdio
的目的,而是实现一些常见的功能。首先,坐标可以是最小二乘法拟合到参考结构,拟合可以加权:
# a) Simple fitt8=t.fitted_to(t[0])# b) Mass-weighted fit of t2 to the 6th frame in trajectory t3:weights=[atom.element.massforatomint2.topology.atoms]t9=t2.fitted_to(t3[5],weights=weights)# c) Use just residue 1 for the fit:weights=np.zeros(t2.n_atoms)weights[t2.topology.select('residue 1')]=1.0t10=t2.fitted_to(t3[5],weights=weights)
其次,支持一些与pbc相关的转换。
packed_around()
变换坐标,使它们位于单位胞内,其中心将是所选原子的几何中心。make_whole()
修正了pbc成像所分裂的分子;这使用了从拓扑文件分析中生成的键信息,因此这需要具有“良好”的几何结构。
t11=t2.packed_around('residue 3')t12=t11.make_whole()
最后可以计算rmsds:
r2=t2.rmsd_from(t3[6])print(r2)
[0.09093863981724826, 0.07614511939046421, 0.07949020859896917, 0.07497944478603998, 0.06590847615210413, 0.051843638106744715, 5.960464477539063e-08, 0.07091305468398602, 0.07106069413686344, 0.08153553252567688]
nglviewer兼容性
mdio.Trajectory
对象与mdtraj生成的对象非常相似,可以在jupyter笔记本中使用nglview函数查看它们。
作者:
查理·劳顿charles.laughton@nottingham.ac.uk
许可证:
BSD 3条款