MDAnalysis从PBC框中选择原子但不移动坐标

时间:2017-01-16 11:23:09

标签: mdanalysis

MDAnalysis距离选择命令,如' around'和' sphzere'从周期图像中选择原子(我使用的是矩形框)。

universe.select_atoms("name OW and around 4 (resid 20 and name O2)")

然而,来自PBC盒子的原子坐标位于盒子的另一侧。换句话说,我必须手动平移原子以确保它们实际上具有4埃的距离。

是否有使用select_atoms功能实现此功能的选择功能?

1 个答案:

答案 0 :(得分:2)

如果我理解的话,你想让图像中给定选择的原子最接近那个选择。

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')