永远相信美好的事情终将发生

MDTraj原子选择语句

  MDTraj是一款重要的MD轨迹分析工具,本文将介绍MDTraj中的原子选择语句。内容主要来自于MDtraj官网。MDTraj基于分子的topology进行原子选择,选择语句的返回值为被选中原子的atom index(0-based)。

top.atoms

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import mdtraj as md

traj = md.load('trajectory.xtc', top='top.pdb')
print(traj)

top=traj.topology
print(top)

print('Fifth atom: %s' % top.atom(4))
print('All atoms: %s' % [atom for atom in top.atoms])

print('Second residue: %s' % top.residue(1))
print('All residues: %s' % [residue for residue in top.residues])

atom = top.atom(10)
print('''Hi! I am the %sth atom, and my name is %s.
I am a %s atom with %s bonds.
I am part of an %s residue.''' % ( atom.index, atom.name, atom.element.name, atom.n_bonds, atom.residue.name))

res = top.residue(10)
atom.is_sidechain
res.is_protein 等

print([atom.index for atom in top.atoms if atom.element.symbol is 'C' and atom.is_sidechain])
print([residue for residue in top.chain(0).residues if residue.index % 2 == 0])

top.select

1
2
3
4
5
6
7
8
9
10
11
top.select("water") 
top.select("resSeq 35")
top.select("water and name O")
top.select("mass 5.5 to 20")
top.select("resname =~ 'C.*'")
top.select("protein and (backbone or resname ALA)")
top.select('protein and not symbol H') # 重原子

top.select("symbol == O")
top.select("symbol == 'O'")
top.select('symbol == "O"')

通过select_expression函数查看选择内容

1
2
selection = top.select_expression('name CA and resid 1 to 2')
print(selection)

top.select的关键字

关键字 等价关键字 类型 描述
all everything bool Matches everything
none nothing bool Matches nothing
backbone is_backbone bool Whether atom is in the backbone of a protein residue
sidechain is_sidechain bool Whether atom is in the sidechain of a protein residue
protein is_protein bool Whether atom is part of a protein residue
water is_water, waters bool Whether atom is part of a water residue
name - str Atom name
index - int Atom index (0-based)
n_bonds - int Number of bonds this atom participates in
type element, symbol str 1 or 2-letter chemical symbols from the periodic table
mass - float Element atomic mass (daltons)
residue resSeq int Residue Sequence record (generally 1-based, but depends on topology)
resid resi int Residue index (0-based)
resname resn str Residue name
rescode code, resc str 1-letter residue code
chainid - int Chain index (0-based)
segment_id segname int Segment index (0-based)

运算符

  • 布尔运算:andornot以及C语言风格的&&||
  • 逻辑运算:<<===!=>=>以及FORTRAN风格的ltleeqnegegt
  • 支持正则表达式匹配符=~,比如top.select("name =~ 'C[1-4]'")可匹配'C1''C2''C3''C4'。此处仅支持Python原生的正则表达式语句。
    1
    2
    3
    top.select("resid 35")      # 0-based 
    top.select("resid == 35")
    top.select("residue 35") # 等于pdb文件中的残基编号

选择范围

1
2
3
top.select("resid 10 to 30")                    # 包括两端 
top.select("(10 <= resid) and (resid <= 30)")
top.select("residue 10 to 30") # 包括两端,等于pdb文件中的残基编号

坐标

1
traj.xyz[frame_idx, atom_idx,:]

Ref: