Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop unexpected columns when writing PDB dataframes #124 #125

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
15 changes: 2 additions & 13 deletions biopandas/pdb/pandas_pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ def to_pdb(self, path, records=None, gz=False, append_newline=True):
for idx in range(dfs[r][c].values.shape[0]):
if len(dfs[r][c].values[idx]) > 8:
dfs[r][c].values[idx] = str(dfs[r][c].values[idx]).strip()
if c in {"line_idx", "OUT"}:
if c in {"line_idx", "OUT", "model_id"}:
pass
elif r in {"ATOM", "HETATM"} and c not in pdb_df_columns:
warn(
Expand All @@ -712,19 +712,8 @@ def to_pdb(self, path, records=None, gz=False, append_newline=True):
else:
dfs[r]["OUT"] = dfs[r]["OUT"] + dfs[r][c]

if pd_version < LooseVersion("0.17.0"):
warn(
"You are using an old pandas version (< 0.17)"
" that relies on the old sorting syntax."
" Please consider updating your pandas"
" installation to a more recent version.",
DeprecationWarning,
)
dfs.sort(columns="line_idx", inplace=True)

elif pd_version < LooseVersion("0.23.0"):
if pd_version < LooseVersion("0.23.0"):
df = pd.concat(dfs)

else:
df = pd.concat(dfs, sort=False)

Expand Down
13 changes: 13 additions & 0 deletions biopandas/pdb/tests/test_write_pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import warnings

import pandas as pd

from biopandas.pdb import PandasPdb

TESTDATA_FILENAME = os.path.join(os.path.dirname(__file__), "data", "3eiy.pdb")
Expand Down Expand Up @@ -77,6 +78,18 @@ def test_anisou():
os.remove(OUTFILE)
assert f1 == four_eiy


def test_write_with_model_id():
"""Test writing a dataframe with a model ID column added."""
ppdb = PandasPdb()
ppdb.read_pdb(TESTDATA_FILENAME)
df.label_models()
ppdb.to_pdb(path=OUTFILE, records=None)
with open(OUTFILE, "r") as f:
f1 = f.read()
os.remove(OUTFILE)
assert f1 == f2


def test_add_remark():
"""Test adding a REMARK entry."""
Expand Down