BioPandas/biopandas

Saving new pdb from dataframe

SkwisgaarSkwigelf opened this issue · 2 comments

I have a very basic question. I often load pdb files as ppdb objects and then convert to pandas df to manipulate them:

ppdb = PandasPdb().read_pdb(input_pdb)
chainA_df = ppdb.df['ATOM'][ppdb.df['ATOM']['chain_id'] == 'A']
...(more code to alter the chainA_df)...

For instance, I want to renumber residues in only chain A using a conversion dictionary, so I find it easier to work with the individual chain as a pandas df, like this:

chainA_df = chainA_df.replace({'residue_number': conversion_dict})

However, I can't find a function to now to take the altered pandas df and save a new PDB. Is there any way to go backwards (i.e. create a ppdb object from the altered df and then save)? Any advice would be helpful.

a-r-j commented

You can set it on a new pandas pdb object.

Something like:

from biopandas import PandasPdb

new_ppdb = PandasPdb()
new_ppdb.df["ATOM"] = my_new_df
new_ppdb.df["HETATM"] = my_new_hetatm_df

new_ppdb.to_pdb("my_transformed_pdb.pdb")

This is exactly what I was missing, thank you!