Support for PDB files with multiple frames?
jeevster opened this issue · 2 comments
I would like to use pdbfixer on pdb files which have trajectories of protein backbone conformations. Specifically, I would like to be able to add missing heavy atoms and hydrogens to all frames in the pdb, and write out the new, multi-frame pdb file. However, when I use pdbfixer as follows, it only loads/operates on the first frame.
fixer = PDBFixer(filename='sample-om_interpolate.pdb')
fixer.findMissingResidues()
# adding missing heavy atoms
fixer.findMissingAtoms()
fixer.addMissingAtoms()
# add missing hydrogens
fixer.addMissingHydrogens(7.0)
# write the fixed pdb file
PDBFile.writeFile(fixer.topology, fixer.positions, open('output.pdb', 'w'))
Here's a sample pdb file:
sample-om_interpolate.pdb.zip
Is there support for multli-frame PDB files?
PDBFixer only operates on a single frame of the PDB file. If you want to run it on multiple frames, your best bet is probably to run it separately on each frame, then combine them again. The code would be something like this. I'm just writing this in the browser so it may not be entirely correct, but it should give the idea.
from io import StringIO
input_pdb = PDBFile(...)
has_written_header = False
with open('output.pdb', 'w') as output_pdb:
for i in range(input_pdb.getNumFrames()):
# Create an in-memory PDB file containing just the one frame.
output = StringIO()
PDBFile.writeFile(input_pdb.topology, input_pdb.getPositions(frame=i), output)
# Process it with PDBFixer.
fixer = PDBFixer(pdbfile=StringIO(output.getvalue()))
...
# Write the result to the output file.
if not has_written_header:
PDBFile.writeHeader(fixer.topology, output_pdb)
has_written_header = True
PDBFile.writeModel(fixer.topology, fixer.positions, output_pdb, i+1)
PDBFile.writeFooter(fixer.topology, output_pdb)
Thanks!