修改.pdb文件中的值并另存为新文件

问题描述 投票:0回答:1

对于作业,我必须在 Python 中读取一个小的 .pdb 文件,更改一些值,然后将其另存为新的 .pdb 文件。
我的原始文件是这样的:

ATOM     19  HD2 TYR     1      26.910  61.717  39.871  1.00  0.00           H  
ATOM     20  C   TYR     1      29.796  62.354  41.246  1.00  0.00           C  
ATOM     23  H   SER     2      30.611  61.950  39.410  1.00  0.00           H  
ATOM     24  CA  SER     2      30.082  64.035  39.354  1.00  0.00           C 
END 

我尝试过 Pandas 但没有成功,因为如果它不保存索引,我就无法将其保存在所需的扩展中,这是我不想要的(我使用了

.to_csv('newfile.pdb')
)。
我找到了一个名为 BioPython 的模块,这是我对它的尝试:

from Bio.PDB import PDBParser, PDBIO

def translate_atoms(structure, resname, translation_vector):
    for model in structure:
        for chain in model:
            for residue in chain:
                if residue.resname == resname:
                    for atom in residue:
                        atom.coord += translation_vector

pdb_file = "pdb1.pdb"
# Read the PDB file
pdb_parser = PDBParser(QUIET=True)
structure = pdb_parser.get_structure("original_pdb1", pdb_file)

# Translation vector for x direction (0.55 nm, so 5.5Å)
translation_vector = [5.5, 0, 0]

# Translate all atoms of SER residue in x direction
translate_atoms(structure, "SER", translation_vector)

# Write the modified structure to a new PDB file
output_pdb = "modified_pdb1.pdb"
pdb_io = PDBIO()
pdb_io.set_structure(structure)
pdb_io.save(output_pdb)

就更改值而言,这符合我的要求,但是当我保存时,它会添加不需要的行,如下所示:

ATOM     19  HD2 TYR     1      26.910  61.717  39.871  1.00  0.00           H  
ATOM     20  C   TYR     1      29.796  62.354  41.246  1.00  0.00           C  
ATOM     23  H   SER     2      36.111  61.950  39.410  1.00  0.00           H  
ATOM     24  CA  SER     2      35.582  64.035  39.354  1.00  0.00           C  
TER      33      SER     2
END

如果没有最后一行,我该如何保存它?

谢谢您的帮助!

python bioinformatics biopython pdb pdb-files
1个回答
0
投票

我添加了一个新函数 save_without_ter_end 来保存修改后的结构,而无需 TER 和 END 行。 save_without_ter_end 函数手动构造 PDB 文件的每一行,确保只写入 ATOM 记录并在末尾附加 END 行。 我删除了 PDBIO 的使用,因为它没有提供排除 TER 和 END 行的所需结果。

from Bio.PDB import PDBParser, PDBIO

def translate_atoms(structure, resname, translation_vector):
    for model in structure:
        for chain in model:
            for residue in chain:
                if residue.resname == resname:
                    for atom in residue:
                        atom.coord += translation_vector

def save_without_ter_end(structure, filename):
    with open(filename, 'w') as pdb_file:
        for model in structure:
            for chain in model:
                for residue in chain:
                    for atom in residue:
                        atom_line = f"ATOM  {atom.serial_number:<5d} {atom.get_name():>4s} {residue.resname:>3s} {chain.id}{residue.id[1]:>4d}    {atom.coord[0]:8.3f}{atom.coord[1]:8.3f}{atom.coord[2]:8.3f}{atom.occupancy:6.2f}{atom.bfactor:6.2f}          {atom.element:>2s}\n"
                        pdb_file.write(atom_line)
        pdb_file.write("END\n")


pdb_file = "pdb1.pdb"
# Read the PDB file
pdb_parser = PDBParser(QUIET=True)
structure = pdb_parser.get_structure("original_pdb1", pdb_file)

# Translation vector for x direction (0.55 nm, so 5.5Å)
translation_vector = [5.5, 0, 0]

# Translate all atoms of SER residue in x direction
translate_atoms(structure, "SER", translation_vector)

# Write the modified structure to a new PDB file without TER and END lines
output_pdb = "modified_pdb1.pdb"
save_without_ter_end(structure, output_pdb)


ATOM  19     HD2 TYR     1      26.910  61.717  39.871  1.00  0.00           H
ATOM  20       C TYR     1      29.796  62.354  41.246  1.00  0.00           C
ATOM  23       H SER     2      36.111  61.950  39.410  1.00  0.00           H
ATOM  24      CA SER     2      35.582  64.035  39.354  1.00  0.00           C
END
© www.soinside.com 2019 - 2024. All rights reserved.