MDAnalysis的距离选择命令,如“box”和“sphzere”,从周期图像中选择原子(我使用的是矩形框)。
universe.select_atoms("name OW and around 4 (resid 20 and name O2)")然而,来自PBC盒的原子坐标位于盒子的另一边。换句话说,我必须手动翻译原子,以确保它们实际上是与4安格斯特龙距离。
是否有使用select_atoms函数实现这一目标的选择特性?
发布于 2017-01-17 18:36:00
如果我能理解的话,你会想让原子围绕一个给定的选择在图像中,是最接近该选择的。
universe.select_atoms不修改坐标,我也不知道有一个函数能满足您的需要。以下功能可以用于像您这样的正交盒:
def pack_around(atom_group, center):
"""
Translate atoms to their periodic image the closest to a given point.
The function assumes that the center is in the main periodic image.
"""
# Get the box for the current frame
box = atom_group.universe.dimensions
# The next steps assume that all the atoms are in the same
# periodic image, so let's make sure it is the case
atom_group.pack_into_box()
# AtomGroup.positions is a property rather than a simple attribute.
# It does not always propagate changes very well so let's work with
# a copy of the coordinates for now.
positions = atom_group.positions.copy()
# Identify the *coordinates* to translate.
sub = positions - center
culprits = numpy.where(numpy.sqrt(sub**2) > box[:3] / 2)
# Actually translate the coordinates.
positions[culprits] -= (u.dimensions[culprits[1]]
* numpy.sign(sub[culprits]))
# Propagate the new coordinates.
atom_group.positions = positions使用该函数,我在一个MDAnalysis测试文件上得到了预期的行为。需要安装MDAnalysisTests才能运行以下代码:
import numpy
import MDAnalysis as mda
from MDAnalysisTests.datafiles import PDB_sub_sol
u = mda.Universe(PDB_sub_sol)
selection = u.select_atoms('around 15 resid 32')
center = u.select_atoms('resid 32').center_of_mass()
# Save the initial file for latter comparison
u.atoms.write('original.pdb')
selection.write('selection_original.pdb')
# Translate the coordinates
pack_around(selection, center)
# Save the new coordinates
u.atoms.write('modified.pdb')
selection.write('selection_modified.pdb')https://stackoverflow.com/questions/41675253
复制相似问题