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

标签 python save bioinformatics biopython protein-database

我有一个 PDB 文件列表。我想使用 BioPython 中的 Bio.PDB 模块提取所有文件的配体(因此,杂原子)并将每个单独保存到 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文件中单独保存一个一个)

最佳答案

你们很接近。

但是您必须提供一个Select 类作为io.save 的第二个参数。看看文档评论。它说这个参数应该提供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)

顺便说一句:我试图在这个过程中稍微提高你的代码的可读性......

关于python - 如何使用 Bio.PDB 分别保存 PDB 文件中的每个配体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61390035/

相关文章:

python - 按范围扩展 DataFrame

python - 如何在另一个字典中的 Python 字典中查找特定项目?

python - 如何从给定的二维张量中提取 n 个一维张量?

javascript - 使用 html2canvas 保存图像 - 纯 Javascript

java - Netbeans API : How to save a file, 或当前项目中的所有文件?

c# - 使用 SaveFileDialog 后 GDI+ 的 Bitmap.Save() 发生一般错误

python - 计算 Pandas 时间序列中的协方差

r - 是否有任何 R 函数可以从物种分类 ID/物种名称或属名中提取所有分类名称(门、类、目、科...)?

c# - 为 C# 保留局部性的哈希函数

python - 将相似的模式合并为单一的共识模式