add pymol change_chain_identifier

This commit is contained in:
root
2024-01-25 10:31:33 +08:00
parent d73dfe93f4
commit 324fda852c

View File

@@ -25,11 +25,63 @@ from Bio.Align import PairwiseAligner
import requests
from copy import deepcopy
from pymol import cmd
from pymol import CmdException
import pymol
import os
# 使用 BioPython 导入氨基酸缩写
AMINO_ACIDS = set(IUPACData.protein_letters)
class PyMOLInstance:
"""
Class to handle a separate PyMOL instance.
"""
def __init__(self):
self.cmd = pymol.cmd
self.cmd.reinitialize()
@classmethod
def change_chain_identifier(cls, pdb_file: Path, old_chain_id: str, new_chain_id: str, output_file: Path):
"""
Change the identifier of a specific chain in a PDB file using PyMOL.
Args:
pdb_file (Path): Path of the PDB file.
old_chain_id (str): Original identifier of the chain.
new_chain_id (str): New identifier to be assigned to the chain.
output_file (Path): Path to save the modified PDB file.
"""
cmd = pymol.cmd
cmd.reinitialize()
try:
cmd.load(pdb_file.as_posix(), "temp_structure")
cmd.alter(f'temp_structure and chain {old_chain_id}', f'chain="{new_chain_id}"')
cmd.save(output_file.as_posix(), "temp_structure")
except CmdException as e:
print(f"Error in PyMOL command: {e}")
finally:
cmd.delete("temp_structure")
def save(self, output_file: Path, format: str = 'pdb'):
"""
Save the current state of the PyMOL instance to a file.
Args:
output_file (Path): Path to save the modified PDB file.
"""
try:
self.cmd.save(filename=output_file.as_posix(), selection="temp_structure", format=format)
finally:
self.cmd.delete("temp_structure")
# 使用示例
def PyMOLInstance_change_chain_identifier(pdb_file_path: Path, old_chain_id: str, new_chain_id: str, output_file_path: Path):
# pdb_file_path = Path("path/to/your/pdb_file.pdb") # 更改为实际的文件路径
# old_chain_id = "A"
# new_chain_id = "D"
# output_file_path = Path("path/to/your/output_file.pdb") # 更改为希望保存的输出路径
a = PyMOLInstance.change_chain_identifier(pdb_file_path, old_chain_id, new_chain_id, output_file_path)
a.save(output_file_path)
@dataclass
class PDBAnalyzer:
"""