我正在使用biopython来查找两个残基的C α原子之间的距离,但我不断收到错误。这是我的代码和错误:
'''
>>> from Bio.PDB.mmtf import MMTFParser
>>> structure = MMTFParser.get_structure_from_url('4mne')
/Library/Python/2.7/site-packages/Bio/PDB/StructureBuilder.py:89: PDBConstructionWarning: WARNING: Chain A is discontinuous at line 0.
PDBConstructionWarning)
/Library/Python/2.7/site-packages/Bio/PDB/StructureBuilder.py:89: PDBConstructionWarning: WARNING: Chain D is discontinuous at line 0.
PDBConstructionWarning)
/Library/Python/2.7/site-packages/Bio/PDB/StructureBuilder.py:89: PDBConstructionWarning: WARNING: Chain E is discontinuous at line 0.
PDBConstructionWarning)
/Library/Python/2.7/site-packages/Bio/PDB/StructureBuilder.py:89: PDBConstructionWarning: WARNING: Chain F is discontinuous at line 0.
PDBConstructionWarning)
/Library/Python/2.7/site-packages/Bio/PDB/StructureBuilder.py:89: PDBConstructionWarning: WARNING: Chain H is discontinuous at line 0.
PDBConstructionWarning)
/Library/Python/2.7/site-packages/Bio/PDB/StructureBuilder.py:89: PDBConstructionWarning: WARNING: Chain B is discontinuous at line 0.
PDBConstructionWarning)
/Library/Python/2.7/site-packages/Bio/PDB/StructureBuilder.py:89: PDBConstructionWarning: WARNING: Chain C is discontinuous at line 0.
PDBConstructionWarning)
/Library/Python/2.7/site-packages/Bio/PDB/StructureBuilder.py:89: PDBConstructionWarning: WARNING: Chain G is discontinuous at line 0.
PDBConstructionWarning)
>>> for c in structure.get_chains():
... if c.get_id() == 'B':
... chain = c
...
>>> chain
<Chain id=B>
>>> CAatoms = [a for a in chain.get_atoms() if a.id == 'CA']
>>> for a in CAatoms:
... for b in CAatoms:
... distance = a-b
...
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
File "/Library/Python/2.7/site-packages/Bio/PDB/Atom.py", line 124, in __sub__
diff = self.coord - other.coord
TypeError: unsupported operand type(s) for -: 'list' and 'list'
>>>
'''
这与MMTFParser的"get_structure_from_url"方法有关吗?我已经用PDBParser((尝试过这个.get_structure它工作得很好。
Biopython的atom类有一个自定义的减法方法。
从源代码:
def __sub__(self, other):
"""Calculate distance between two atoms.
:param other: the other atom
:type other: L{Atom}
Examples
--------
>>> distance = atom1 - atom2
"""
diff = self.coord - other.coord
return numpy.sqrt(numpy.dot(diff, diff))
对于 MMTFParser,此功能似乎缺失,但您可以自己轻松完成。
MMTFParser将坐标读取为列表(init_atom(str(atom_name), [x, y, z] ...
,第53行(,不像PDBParser将坐标读取为Numpy数组(coord = numpy.array((x, y, z), "f")
,第187行(。
为了获得距离,您可以将坐标列表转换为Numpy数组,然后计算距离。
import numpy as np
distance = np.linalg.norm(np.array(a.coord) - np.array(b.coord))