我有一组PDB文件,希望利用Bio.PDB模块从BioPython库中提取这些文件中的配体(即杂原子),并将每个配体保存为单独的PDB文件。我尝试了一些解决方案,例如Remove heteroatoms from PDB,但我只能得到将所有配体保存在同一个文件中的结果。我还尝试过类似以下的方法:
我知道这是由于在我的“io.save”中的“accept_residue()”所致,但我没有找到任何逻辑解决方案来实现我想要的...
最后,我尝试了一个类似于这样的解决方案,使用了“chain.detach_child()”。
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文件中)