如何使用Bio.PDB从PDB文件中分别保存每个配体?

3
我有一组PDB文件,希望利用Bio.PDB模块从BioPython库中提取这些文件中的配体(即杂原子),并将每个配体保存为单独的PDB文件。我尝试了一些解决方案,例如Remove heteroatoms from PDB,但我只能得到将所有配体保存在同一个文件中的结果。我还尝试过类似以下的方法:
def accept_residue(residue):
    """ Recognition of heteroatoms - Remove water molecules """ 
    res = residue.id[0]
    if res != " ": # Heteroatoms have some flags, that's why we keep only residue with id != " "
        if res != "W": # Don't take in consideration the water molecules
            return True


def extract_ligands(path):
    """ Extraction of the heteroatoms of .pdb files """
    for element in os.listdir(path+'/data/pdb'):
        i=1
        if element.endswith('.pdb'):
            if not element.startswith("lig_"):
                pdb = PDBParser().get_structure(element[:-4], path+'/data/pdb/'+element)
                io = PDBIO()
                io.set_structure(pdb)
                for model in pdb:
                    for chain in model:
                        for residue in chain:
                            if accept_residue(residue):
                                io.save("lig_"+element[:-4]+"_"+str(i)+".pdb", accept_residue(residue))
                                i += 1 # Counter for the result filename

            

# Main
path = mypath

extract_ligands(path)

显然,它引发了一个错误:

AttributeError: 'bool' object has no attribute 'accept_model'

我知道这是由于在我的“io.save”中的“accept_residue()”所致,但我没有找到任何逻辑解决方案来实现我想要的...
最后,我尝试了一个类似于这样的解决方案,使用了“chain.detach_child()”。
                    ...
                    for chain in model:
                        for residue in chain:
                            res = residue.id[0]
                            if res == " " or res == "W": 
                                chain.detach_child(residue.id)
                        if len(chain) == 0:
                            model.detach_child(chain.id)
                     ...

在我看来,它会"分离"所有不是杂原子的残基(res.id[0] == " ")和所有水分子(res.id[0] == "W")。但最终,所有的残基和水分子仍然存在且有缺陷。

那么,我需要的操作是否可行呢?(从所有的文件中提取出所有配体,并将它们分别保存在PDB文件中)

1个回答

2

你很接近了。

但是你必须在io.save的第二个参数中提供一个Select类。看一下文档注释。它说这个参数应该提供accept_modelaccept_chainaccept_residueaccept_atom

我创建了一个从Bio.PDB.PDBIO.Select继承的ResidueSelect类。这样我只需要重写我们需要的方法。在我们的情况下,是链和残基。

因为我们只想保存当前链中的当前残基,所以我为构造函数提供了两个相应的参数。

import os

from Bio.PDB import PDBParser, PDBIO, Select


def is_het(residue):
    res = residue.id[0]
    return res != " " and res != "W"


class ResidueSelect(Select):
    def __init__(self, chain, residue):
        self.chain = chain
        self.residue = residue

    def accept_chain(self, chain):
        return chain.id == self.chain.id

    def accept_residue(self, residue):
        """ Recognition of heteroatoms - Remove water molecules """
        return residue == self.residue and is_het(residue)


def extract_ligands(path):
    """ Extraction of the heteroatoms of .pdb files """

    for pfb_file in os.listdir(path + '/data/pdb'):
        i = 1
        if pfb_file.endswith('.pdb') and not pfb_file.startswith("lig_"):
            pdb_code = pfb_file[:-4]
            pdb = PDBParser().get_structure(pdb_code, path + '/data/pdb/' + pfb_file)
            io = PDBIO()
            io.set_structure(pdb)
            for model in pdb:
                for chain in model:
                    for residue in chain:
                        if not is_het(residue):
                            continue
                        print(f"saving {chain} {residue}")
                        io.save(f"lig_{pdb_code}_{i}.pdb", ResidueSelect(chain, residue))
                        i += 1


# Main
path = mypath

extract_ligands(path)

顺便说一下:在这个过程中,我试图改善您的代码的可读性...


好的,一切都运行得非常顺利!我现在很好地理解了构造函数的作用。 因此,非常感谢你! - MathB

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接