MDAnalysis 从 PBC 盒中选择原子但不移动坐标
MDAnalysis selects atoms from the PBC box but does not shift the coordinates
MDAnalysis 距离选择命令,如 'around' 和 'sphzere' 从周期图像中选择原子(我使用的是矩形框)。
universe.select_atoms("name OW and around 4 (resid 20 and name O2)")
然而,来自 PBC 盒子的原子坐标位于盒子的另一侧。换句话说,我必须手动平移原子以确保它们实际上在 4 埃距离内。
是否有使用 select_atoms 函数实现此目的的选择功能?
如果我很清楚,您想在最接近该选择的图像中获取给定选择周围的原子。
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')
MDAnalysis 距离选择命令,如 'around' 和 'sphzere' 从周期图像中选择原子(我使用的是矩形框)。
universe.select_atoms("name OW and around 4 (resid 20 and name O2)")
然而,来自 PBC 盒子的原子坐标位于盒子的另一侧。换句话说,我必须手动平移原子以确保它们实际上在 4 埃距离内。
是否有使用 select_atoms 函数实现此目的的选择功能?
如果我很清楚,您想在最接近该选择的图像中获取给定选择周围的原子。
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')