【问题标题】:Reading an entire directory of .pdb files using BioPython使用 BioPython 读取 .pdb 文件的整个目录
【发布时间】:2017-06-21 07:21:22
【问题描述】:

我最近的任务是用 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 做。所以我很想听听任何人可能有的任何想法或示例代码。

【问题讨论】:

  • 您可能想查看os 模块,尤其是os.listdir
  • 顺便说一句,你也可以用if x.name in ('ZN', 'FE', 'CU', 'MG', 'CA', 'MN')替换你的or声明列表
  • @ason​​gtoruin 感谢您的建议,我会研究您提到的模块。
  • 另一个小提示:将变量 (list) 命名为类型会在某些时候引起麻烦。只需使用neighborList作为函数参数,那么你也可以跳过函数中的第一行。
  • @MaximilianPeters 是的,我想过,这只是一种习惯,主要是因为我觉得 Python 不允许你定义每个变量的类型有点不安全。

标签: python biopython pdb


【解决方案1】:

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

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

【讨论】:

  • 谢谢你,这不仅帮助了我的问题,也帮助了我过渡到 python。再次感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-25
  • 2011-01-03
  • 1970-01-01
  • 2021-09-24
  • 1970-01-01
  • 2016-03-24
相关资源
最近更新 更多