<h2>不要在空白处拆分</h2>
<p>这里给出的其他答案提出了一个错误的假设——坐标将以空格分隔。根据<a href="http://deposit.rcsb.org/adit/docs/pdb_atom_format.html#ATOM" rel="noreferrer">PDB specification of ^{<cd1>}</a>,这是<em>而不是</em>必要的情况:PDB记录值是由列索引指定的,并且可以相互流动。例如,第一个<code>ATOM</code>记录如下:</p>
<pre><code>ATOM 920 CA GLN A 203 39.292 -13.354 17.416 1.00 55.76 C
</code></pre>
<p>但这也是完全正确的:</p>
<pre><code>ATOM 920 CA GLN A 203 39.292-13.3540 17.416 1.00 55.76 C
</code></pre>
<h2>更好的方法</h2>
<p>由于列指定的索引以及PDB文件中可能出现的其他问题的数量,您不应该编写自己的解析器。PDB格式很混乱,需要处理很多特殊情况和格式错误的文件。相反,使用已经为您编写的解析器。</p>
<p>我喜欢<a href="http://biopython.org" rel="noreferrer">Biopython</a>的<code>PDB.PDBParser</code>。它将把结构解析为Python对象,并提供方便的特性。如果您更喜欢Perl,请查看<a href="http://www.bioperl.org" rel="noreferrer">BioPerl</a>。</p>
<p><code>PDB.Residue</code>对象允许按名称对原子进行键控访问,并且<code>PDB.Atom</code>对象重载<code>-</code>运算符以返回两个原子之间的距离。我们可以使用它来编写简洁明了的代码:</p>
<h2>代码</h2>
<pre><code>from Bio import PDB
parser = PDB.PDBParser()
# Parse the structure into a PDB.Structure object
pdb_code = "1exm"
pdb_path = "pdb1exm.ent"
struct = parser.get_structure(pdb_code, pdb_path)
# Grab the first two residues from the structure
residues = struct.get_residues()
res_one = residues.next()
res_two = residues.next()
try:
alpha_dist = res_one['CA'] - res_two['CA']
except KeyError:
print "Alpha carbon missing, computing distance impossible!"
</code></pre>