for an assignment I have to read a small .pdb file in Python, change some values, and save it as a new .pdb file.
My original file is like this:

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 

I have tried with Pandas but with no success, as I cannot save it in the desired extension without it also saving an index, which I don't want (I used .to_csv('newfile.pdb')).
I have found a module called BioPython, and this is my attempt with it:

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

没有最后一行,我怎么能保存它呢?

谢谢你的帮助!

推荐答案

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

from Bio.PDB imp或t PDBParser, PDBIO

def translate_atoms(structure, resname, translation_vect或):
    f或 model in structure:
        f或 chain in model:
            f或 residue in chain:
                if residue.resname == resname:
                    f或 atom in residue:
                        atom.co或d += translation_vect或

def save_without_ter_end(structure, filename):
    with open(filename, 'w') as pdb_file:
        f或 model in structure:
            f或 chain in model:
                f或 residue in chain:
                    f或 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.co或d[0]:8.3f}{atom.co或d[1]:8.3f}{atom.co或d[2]:8.3f}{atom.occupancy:6.2f}{atom.bfact或: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("或iginal_pdb1", pdb_file)

# Translation vect或 f或 x direction (0.55 nm, so 5.5Å)
translation_vect或 = [5.5, 0, 0]

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

# 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

或者干脆把它添加到你的命令中.这段代码遍历 struct 中的每个原子,并手动写入PDB文件,而不包括TER和END行.

# Manually write the PDB file
with open(output_pdb, 'w') as pdb_file:
    f或 model in structure:
        f或 chain in model:
            f或 residue in chain:
                f或 atom in residue:
                    atom_line = f"{atom.get_id():6s}{atom.get_name():5s}{residue.resname:>4s} {chain.id:4s}{residue.id[1]:>4d}    {atom.co或d[0]:8.3f}{atom.co或d[1]:8.3f}{atom.co或d[2]:8.3f}{atom.occupancy:6.2f}{atom.bfact或:6.2f}          {atom.element:>2s}\n"
                    pdb_file.write(atom_line)

或者你可以通过以下方式获得相同的结果

imp或t pandas as pd
from Bio.PDB imp或t PDBParser, PDBIO
# Read the 或iginal PDB file into a DataFrame
pdb_file = "pdb1.pdb"
columns = ["rec或d_name", "atom_number", "atom_name", "residue_name", "chain_id", "residue_number", "x", "y", "z", "occupancy", "b_fact或", "element_symbol", "charge"]
df = pd.read_csv(pdb_file, delim_whitespace=True, names=columns)

def translate_atoms(structure, resname, translation_vect或):
    f或 model in structure:
        f或 chain in model:
            f或 residue in chain:
                if residue.resname == resname:
                    f或 atom in residue:
                        atom.co或d += translation_vect或

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

# Define translation vect或
translation_vect或 = [5.5, 0, 0]

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

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


with open(output_pdb, 'w') as pdb_file:
    f或 model in structure:
        f或 chain in model:
            f或 residue in chain:
                f或 atom in residue:
                    atom_line = f"ATOM  {atom.serial_number:>4d} {atom.element:>2s} {residue.resname:>4s} {chain.id:4s}{residue.id[1]:>4d}    {atom.co或d[0]:8.3f}{atom.co或d[1]:8.3f}{atom.co或d[2]:8.3f}{atom.occupancy:6.2f}{atom.bfact或:6.2f}          {atom.element:>2s}\n"
                    pdb_file.write(atom_line)
    pdb_file.write("END\n")

print("Modified PDB file saved successfully.")

from Bio.PDB imp或t PDBParser, PDBIO
imp或t pandas as pd

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

# Create an empty DataFrame to st或e the atom inf或mation
atom_data = []

# Iterate over the structure and populate the DataFrame with atom inf或mation
f或 model in structure:
    f或 chain in model:
        f或 residue in chain:
            f或 atom in residue:
                atom_data.append([
                    atom.serial_number,
                    atom.element,
                    residue.resname,
                    chain.id,
                    residue.id[1],
                    atom.co或d[0],
                    atom.co或d[1],
                    atom.co或d[2],
                    atom.occupancy,
                    atom.bfact或,
                    atom.element
                ])

# Create a DataFrame from the collected atom data
columns = ["atom_number", "atom_name", "residue_name", "chain_id", "residue_number", "x", "y", "z", "occupancy", "b_fact或", "charge"]
df = pd.DataFrame(atom_data, columns=columns)
# Define translation vect或
translation_vect或 = [5.5, 0, 0]

# Translate all atoms of SER residue in x direction
ser_mask = df["residue_name"] == "SER"
df.loc[ser_mask, ["x", "y", "z"]] += translation_vect或

# Save the modified DataFrame to a new PDB file
output_pdb = "modified_pdb1.pdb"
output_pdb = "modified_pdb1.pdb"
# Save the DataFrame to a new PDB file
output_pdb = "modified_pdb1.pdb"
with open(output_pdb, 'w') as pdb_file:
    f或 index, row in df.iterrows():
        atom_line = f"ATOM  {row['atom_number']:>4d} {row['atom_name']:>2s} {row['residue_name']:>4s} {row['chain_id']:4s}{row['residue_number']:>4d}    {row['x']:8.3f}{row['y']:8.3f}{row['z']:8.3f}{row['occupancy']:6.2f}{row['b_fact或']:6.2f}          {row['charge']:>2s}\n"
        pdb_file.write(atom_line)
    pdb_file.write("END\n")

print("Modified PDB file saved successfully.")

Python相关问答推荐

pandas DataFrame GroupBy.diff函数的意外输出

为什么tkinter框架没有被隐藏?

抓取rotowire MLB球员新闻并使用Python形成表格

对整个 pyramid 进行分组与对 pyramid 列子集进行分组

在线条上绘制表面

Python解析整数格式说明符的规则?

在np数组上实现无重叠的二维滑动窗口

Python列表不会在条件while循环中正确随机化'

在www.example.com中使用`package_data`包含不包含__init__. py的非Python文件

在Admin中显示从ManyToMany通过模型的筛选结果

Python—为什么我的代码返回一个TypeError

如何从pandas DataFrame中获取. groupby()和. agg()之后的子列?

比Pandas 更好的 Select

如何在Python 3.9.6和MacOS Sonoma 14.3.1下安装Pyregion

如何在Python中将超链接添加到PDF中每个页面的顶部?

解决Geopandas和Altair中的正图和投影问题

如何在信号的FFT中获得正确的频率幅值

Python如何导入类的实例

如何为需要初始化的具体类实现依赖反转和接口分离?

Pandas 删除只有一种类型的值的行,重复或不重复