使用 BioPython 读取整个目录的 .pdb 文件

Reading an entire directory of .pdb files using BioPython

我最近的任务是在 python 中编写一个程序,以从 .pdb(蛋白质数据库)中的蛋白质中找到距离每种金属 2 埃以内的原子。这是我为它写的脚本。

from Bio.PDB import *
parser = PDBParser(PERMISSIVE=True)

def print_coordinates(list):
    neighborList = list
    for y in neighborList:
        print "     ", y.get_coord()

structure_id = '5m6n'
fileName = '5m6n.pdb'
structure = parser.get_structure(structure_id, fileName)

atomList = Selection.unfold_entities(structure, 'A')

ns = NeighborSearch(atomList)

for x in structure.get_atoms():
    if x.name == 'ZN' or x.name == 'FE' or x.name == 'CU' or x.name == 'MG' or x.name == 'CA' or x.name == 'MN':
        center = x.get_coord()
        neighbors = ns.search(center,2.0)
        neighborList = Selection.unfold_entities(neighbors, 'A')

        print x.get_id(), ': ', neighborList
        print_coordinates(neighborList)
    else:
        continue

但这仅适用于单个 .pdb 文件,我希望能够读取它们的整个目录。由于我直到现在才使用 Java,所以我不完全确定如何在 Python 2.7 中执行此操作。我有一个想法是,我会把脚本放在一个 try catch 语句中,在其中,一个 while 循环,然后在它结束时抛出一个异常,但这就是我在 Java 中所做的,而不是确定我会如何在 Python 中做到这一点。所以我很想听听任何人可能有的任何想法或示例代码。

您的代码中有一些冗余,例如,这也是一样的:

from Bio.PDB import *
parser = PDBParser(PERMISSIVE=True)

def print_coordinates(neighborList):
    for y in neighborList:
        print "     ", y.get_coord()

structure_id = '5m6n'
fileName = '5m6n.pdb'
structure = parser.get_structure(structure_id, fileName)
metals = ['ZN', 'FE', 'CU', 'MG', 'CA', 'MN']

atomList = [atom for atom in structure.get_atoms() if atom.name in metals]
ns = NeighborSearch(Selection.unfold_entities(structure, 'A'))

for atom in atomList:
    neighbors = ns.search(atom.coord, 2)
    print("{0}: {1}").format(atom.name, neighbors)
    print_coordinates(neighborList)

要回答您的问题,您可以使用 glob 模块获取所有 pdb 文件的列表,并将您的代码嵌套在 for 循环中迭代所有文件。假设您的 pdb 文件位于 /home/pdb_files/:

from Bio.PDB import *
from glob import glob
parser = PDBParser(PERMISSIVE=True)
pdb_files = glob('/home/pdb_files/*')

def print_coordinates(neighborList):
    for y in neighborList:
        print "     ", y.get_coord()

for fileName in pdb_files:
     structure_id = fileName.rsplit('/', 1)[1][:-4]
     structure = parser.get_structure(structure_id, fileName)
     # The rest of your code