将残基 ID 添加到由水坐标时间序列数据组成的 numpy 数组

Adding residue IDs to a numpy array consisting of time series data of water coordinates

我得到了这个用于生成水分子时间序列数据的脚本,我想在生成的矩阵中再添加一个 header 行,其中包含水分子的残基 ID。有人可以帮助修改这个脚本吗?谢谢!

import numpy as np
import MDAnalysis as mda

u = mda.Universe(PSF, DCD)
water_oxygens = u.select_atoms("name OW")

# pre-allocate the array for the data
data = np.zeros((u.trajectory.n_frames, water_oxygens.n_atoms + 1))

for i, ts in enumerate(u.trajectory):
   data[i, 0] = ts.time                          # store current time
   data[i, 1:] = water_oxygens.positions[:, 2]   # extract all z-coordinates

这是一个调整后的代码示例。您可能需要将软件包 MDAnalysisTests 安装到 运行 它:

import numpy as np
import MDAnalysis as mda
from MDAnalysisTests.datafiles import waterPSF, waterDCD

u = mda.Universe(waterPSF, waterDCD)
water_oxygens = u.select_atoms("name OH2")

# pre-allocate the array for the data
# one extra row for the header water residue IDs
data = np.zeros((u.trajectory.n_frames + 1, water_oxygens.n_atoms + 1))

# initialise the water residue IDs
data[0, 0] = np.NaN # the time column
data[0, 1:] = water_oxygens.atoms.resids

for i, ts in enumerate(u.trajectory, start=1):
   data[i, 0] = ts.time                          # store current time
   data[i, 1:] = water_oxygens.positions[:, 2]   # extract all z-coordinates