From 6b0652bca86b64eb5ac6dbbdfca5190ad33a88d6 Mon Sep 17 00:00:00 2001 From: Oleg Sobolev Date: Mon, 6 Nov 2023 11:25:22 -0800 Subject: [PATCH] Do not produce flawed PDB files (cases where model does not fit into PDB format). Adjust tests. --- iotbx/pdb/hierarchy.py | 2 ++ .../tst_hierarchy_long_chain_ids_1.py | 9 +++++---- .../regression/tst_hierarchy_long_resname_1.py | 17 +++++++++-------- .../regression/tst_hierarchy_long_resname_2.py | 6 +----- .../regression/tst_hierarchy_long_resname_3.py | 7 +------ .../regression/tst_hierarchy_long_resname_4.py | 6 +----- iotbx/regression/tst_hierarchy_pdb_v3.py | 2 +- mmtbx/model/model.py | 2 +- 8 files changed, 21 insertions(+), 30 deletions(-) diff --git a/iotbx/pdb/hierarchy.py b/iotbx/pdb/hierarchy.py index 0f3e06935b..ba577daee9 100644 --- a/iotbx/pdb/hierarchy.py +++ b/iotbx/pdb/hierarchy.py @@ -658,6 +658,8 @@ def as_pdb_string(self, :param siguij: write SIGUIJ records if applicable :returns: Python str """ + if not self.fits_in_pdb_format(): + return "" if (cstringio is None): cstringio = StringIO() if (return_cstringio is Auto): diff --git a/iotbx/regression/tst_hierarchy_long_chain_ids_1.py b/iotbx/regression/tst_hierarchy_long_chain_ids_1.py index d420b6caa8..f877d5ffc4 100644 --- a/iotbx/regression/tst_hierarchy_long_chain_ids_1.py +++ b/iotbx/regression/tst_hierarchy_long_chain_ids_1.py @@ -261,10 +261,11 @@ def test1(): # Note here incorrect/trimmed chain id # There's no way to correctly output chain ids longer than 2 char in PDB format print(o_pdb_str) - assert_lines_in_text(o_pdb_str, """\ -ATOM 61 C SERA-2 2 72.898 71.361 62.393 1.00 67.20 C -ATOM 62 O SERA-2 2 73.055 70.333 61.737 1.00 65.10 O - """) + assert o_pdb_str=="" +# assert_lines_in_text(o_pdb_str, """\ +# ATOM 61 C SERA-2 2 72.898 71.361 62.393 1.00 67.20 C +# ATOM 62 O SERA-2 2 73.055 70.333 61.737 1.00 65.10 O +# """) o_cif_str = "%s" % h.as_cif_block() assert_lines_in_text(o_cif_str, """\ diff --git a/iotbx/regression/tst_hierarchy_long_resname_1.py b/iotbx/regression/tst_hierarchy_long_resname_1.py index 12b6a35f85..89ce153182 100644 --- a/iotbx/regression/tst_hierarchy_long_resname_1.py +++ b/iotbx/regression/tst_hierarchy_long_resname_1.py @@ -98,14 +98,15 @@ def test1(): # Note here incorrect/trimmed residue names # There's no way to correctly output resnames longer than 3 char in PDB format # print(o_pdb_str) - assert_lines_in_text(o_pdb_str, """\ -ATOM 8 CE LYS A 279 -2.923 -13.799 42.993 1.00 52.86 C -ATOM 9 NZ LYS A 279 -3.209 -12.856 44.100 1.00 54.19 N -TER -HETATM 10 CA CA A 301 -17.362 -22.385 28.047 1.00 15.20 CA -HETATM 11 C10 7ZTVU A 302 -7.646 -6.965 5.796 1.00 22.62 C -HETATM 12 C2 7ZTVU A 302 -8.462 -5.534 9.265 1.00 16.68 C - """) + assert o_pdb_str == "" +# assert_lines_in_text(o_pdb_str, """\ +# ATOM 8 CE LYS A 279 -2.923 -13.799 42.993 1.00 52.86 C +# ATOM 9 NZ LYS A 279 -3.209 -12.856 44.100 1.00 54.19 N +# TER +# HETATM 10 CA CA A 301 -17.362 -22.385 28.047 1.00 15.20 CA +# HETATM 11 C10 7ZTVU A 302 -7.646 -6.965 5.796 1.00 22.62 C +# HETATM 12 C2 7ZTVU A 302 -8.462 -5.534 9.265 1.00 16.68 C +# """) o_cif_str = "%s" % h.as_cif_block() # print(o_cif_str) diff --git a/iotbx/regression/tst_hierarchy_long_resname_2.py b/iotbx/regression/tst_hierarchy_long_resname_2.py index 4cb1d449b6..dd421cbd0b 100644 --- a/iotbx/regression/tst_hierarchy_long_resname_2.py +++ b/iotbx/regression/tst_hierarchy_long_resname_2.py @@ -129,11 +129,7 @@ def test1(): ' HETATM 10 O . longHOH A 401 ? -3.20900 -12.85600 46.10000 1.000 30.11000 O ? B ? . 1']: assert_lines_in_text(model_cif, l) model_pdb = model.model_as_pdb() - # print(model_pdb) - for l in [ - 'LINK NZ LYS A 279 O longHOH A 401 ', - 'HETATM 10 O longHOH A 401 -3.209 -12.856 46.100 1.00 30.11 O']: - assert_lines_in_text(model_pdb, l) + assert not model.can_be_outputted_as_pdb() if (__name__ == "__main__"): t0 = time.time() diff --git a/iotbx/regression/tst_hierarchy_long_resname_3.py b/iotbx/regression/tst_hierarchy_long_resname_3.py index df28561e75..4db6df0667 100644 --- a/iotbx/regression/tst_hierarchy_long_resname_3.py +++ b/iotbx/regression/tst_hierarchy_long_resname_3.py @@ -212,12 +212,7 @@ def test1(): 'data_comp_7ZTVU', ]: assert_lines_in_text(model_cif, l) - model_pdb = model.model_as_pdb() - print(model_pdb) - for l in [ - 'HETATM 10 C10 7ZTVU A 302 -7.646 -6.965 5.796 1.00 22.62 C', - ]: - assert_lines_in_text(model_pdb, l) + assert not model.can_be_outputted_as_pdb() def test2(): """ diff --git a/iotbx/regression/tst_hierarchy_long_resname_4.py b/iotbx/regression/tst_hierarchy_long_resname_4.py index 80c67e54bb..a8e693b249 100644 --- a/iotbx/regression/tst_hierarchy_long_resname_4.py +++ b/iotbx/regression/tst_hierarchy_long_resname_4.py @@ -209,11 +209,7 @@ def test1(): assert_lines_in_text(model_cif, l) model_pdb = model.model_as_pdb() # print(model_pdb) - for l in [ - 'ATOM 20 ND2 ASN A 3 -1.271 0.715 7.306 1.00 13.48 N', - 'ATOM 21 N longGLY A 4 -1.005 2.228 3.598 1.00 10.29 N', - ]: - assert_lines_in_text(model_pdb, l) + assert not model.can_be_outputted_as_pdb() def test2(): """ diff --git a/iotbx/regression/tst_hierarchy_pdb_v3.py b/iotbx/regression/tst_hierarchy_pdb_v3.py index cc59975348..86aa8bef90 100644 --- a/iotbx/regression/tst_hierarchy_pdb_v3.py +++ b/iotbx/regression/tst_hierarchy_pdb_v3.py @@ -277,7 +277,7 @@ def test6(): for atom_group in residue_group.atom_groups(): for atom in atom_group.atoms(): atom.set_segid('UNK') - assert ph.as_pdb_string().find("UNK")>-1 + # assert ph.as_pdb_string().find("UNK")>-1 assert ph.as_mmcif_string().find("UNK") == -1 assert ph.as_mmcif_string(segid_as_auth_segid=True).find("UNK") > -1 diff --git a/mmtbx/model/model.py b/mmtbx/model/model.py index 5ec3d1786f..2a94004313 100644 --- a/mmtbx/model/model.py +++ b/mmtbx/model/model.py @@ -1555,7 +1555,7 @@ def _figure_out_hierarchy_to_output(self, do_not_shift_back): return hierarchy_to_output def can_be_outputted_as_pdb(self): - return True + return self.get_hierarchy().fits_in_pdb_format() def model_as_pdb(self, output_cs = True,