2

使用 biopython 库,我想删除列表中列出的残基,如下所示。这个线程(http://pelican.rsvs.ulaval.ca/mediawiki/index.php/Manipulating_PDB_files_using_BioPython)提供了一个去除残留物的例子。我有以下代码来去除残留物

 residue_ids_to_remove = [105, 5, 8, 10, 25, 48]
 structure = pdbparser.get_structure("3chy", "./3chy.pdb")
 first_model = structure[0]
 for chain in first_model:
     for residue in chain:
         id = residue.id
         if id[1] in residue_ids_to_remove:
             chain.detach_child(id[1])
 modified_first_model = first_model 

但是这段代码不起作用并引发了错误

def detach_child(self, id):
    "Remove a child."
    child=self.child_dict[id]
    KeyError: '105'

这段代码有什么问题?

或者,我可以使用 accept_residue() 并将其写入 PDB。我不想这样做,因为我想在内存中进行进一步处理。

4

1 回答 1

3

Biopython 无法在链的内部字典中找到密钥,因为您提供的是随机密钥。字典看起来像这样:

child_dict = {(' ', 5, ' '): <Residue HOH het=W resseq=5 icode= >,
              (' ', 6, ' '): <Residue HOH het=W resseq=6 icode= >,
              (' ', 7, ' '): <Residue HOH het=W resseq=7 icode= >}

即:使用元组作为字典键。你可以看到 dict 正在做print chain.child_dict.

一旦你知道了这一点,错误/解决方案就很清楚了。将有效密钥传递给detach_child,即删除[1]

   if id[1] in residue_ids_to_remove:
       chain.detach_child(id)

正确的方式

将子级与链级分离,不要直接循环残差:

for chain in first model:
    for id in residue_ids_to_remove:
        chain.detach_child((' ', id, ' '))

或者使用列表理解:

for chain in first_model:
    [chain.detach_child((' ', id, ' ')) for id in residue_ids_to_remove]
于 2014-09-17T11:42:41.273 回答