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)
This does what I want as far as changing the values but when I save it adds an unwanted line, like so:
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
How can I save it without that last line?
Thank you for your help!
I have dded a new function save_without_ter_end to save the modified structure without TER and END lines. The save_without_ter_end function constructs each line of the PDB file manually, ensuring that only ATOM records are written and appending the END line at the end. I removed the usage of PDBIO since it didn't provide the desired result of excluding TER and END lines.
or simply add this in your command. This code iterates over each atom in the structure and manually writes the PDB file without including the TER and END line.
or u can acheive same by following
or