From dea1acdbcb43fcba4e7689151ba669dc7a32f033 Mon Sep 17 00:00:00 2001 From: Co Quach Date: Sun, 18 Aug 2024 23:37:41 -0500 Subject: [PATCH] add exception test for pdb writer --- .../with_errors/CT_CT_C_3_OH_with_errors.pdb | 26 +++++++++---------- .../tests/test_file_read_write.py | 8 ++++++ .../utils/file_read_and_write.py | 9 ++++--- 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/mosdef_dihedral_fit/tests/files/with_errors/CT_CT_C_3_OH_with_errors.pdb b/mosdef_dihedral_fit/tests/files/with_errors/CT_CT_C_3_OH_with_errors.pdb index 402cbc9..515644b 100644 --- a/mosdef_dihedral_fit/tests/files/with_errors/CT_CT_C_3_OH_with_errors.pdb +++ b/mosdef_dihedral_fit/tests/files/with_errors/CT_CT_C_3_OH_with_errors.pdb @@ -1,13 +1,13 @@ -CRYST1 9.879 8.003 6.628 90.00 90.00 90.00 P 1 -HETATM 1 C1 RES 1 0.000 0.000 0.000 0.00 0.00 C -HETATM 2 C2 RES 1 -1.500 0.000 0.000 0.00 0.00 C -HETATM 3 C3 RES 1 -2.200 1.212 0.000 0.00 0.00 C -HETATM 4 O1 RES 1 0.700 -1.212 -0.000 0.00 0.00 O -HETATM 5 O2 RES 1 0.771 1.169 0.000 0.00 0.00 O -HETATM 6 H1 RES 1 -1.813 1.790 0.713 0.00 0.00 H -HETATM 7 H2 RES 1 -1.967 1.754 -0.813 0.00 0.00 H -HETATM 8 H3 RES 1 -1.820 -0.485 -0.814 0.00 0.00 H -HETATM 9 H4 RES 1 -1.820 -0.485 0.814 0.00 0.00 H -HETATM 10 H5 RES 1 -3.197 1.252 0.096 0.00 0.00 H -HETATM 11 H6 RES 1 1.682 -1.022 -0.000 0.00 0.00 H -END +CRYST1 9.879 8.003 6.628 90.00 90.00 90.00 P 1 +HETATM 1 C1 RES 1 0.000 0.000 0.000 0.00 0.00 +HETATM 2 C2 RES 1 -1.500 0.000 0.000 0.00 0.00 +HETATM 3 C3 RES 1 -2.200 1.212 0.000 0.00 0.00 +HETATM 4 O1 RES 1 0.700 -1.212 -0.000 0.00 0.00 +HETATM 5 O2 RES 1 0.771 1.169 0.000 0.00 0.00 +HETATM 6 H1 RES 1 -1.813 1.790 0.713 0.00 0.00 +HETATM 7 H2 RES 1 -1.967 1.754 -0.813 0.00 0.00 +HETATM 8 H3 RES 1 -1.820 -0.485 -0.814 0.00 0.00 +HETATM 9 H4 RES 1 -1.820 -0.485 0.814 0.00 0.00 +HETATM 10 H5 RES 1 -3.197 1.252 0.096 0.00 0.00 +HETATM 11 H6 RES 1 1.682 -1.022 -0.000 0.00 0.00 +END diff --git a/mosdef_dihedral_fit/tests/test_file_read_write.py b/mosdef_dihedral_fit/tests/test_file_read_write.py index 9395827..7687b38 100644 --- a/mosdef_dihedral_fit/tests/test_file_read_write.py +++ b/mosdef_dihedral_fit/tests/test_file_read_write.py @@ -107,6 +107,14 @@ def test_get_from_pdb(self): "H", "H", ] + error_fn = "with_errors/CT_CT_C_3_OH_with_errors.pdb" + full_error_fn = self.get_fn(error_fn) + error_msg = ( + "ERROR: The provided pdb format is not the required PDB format " + "or the pdb file has zero \(0\) atoms or beads in it." + ) + with pytest.raises(TypeError, match=error_msg): + get_atom_names_and_elements_from_pdb(full_error_fn) def test_write_xyz_file_from_gaussian(self): atom_namesList = [ diff --git a/mosdef_dihedral_fit/utils/file_read_and_write.py b/mosdef_dihedral_fit/utils/file_read_and_write.py index b94d441..f1db4e4 100755 --- a/mosdef_dihedral_fit/utils/file_read_and_write.py +++ b/mosdef_dihedral_fit/utils/file_read_and_write.py @@ -109,13 +109,16 @@ def get_atom_names_and_elements_from_pdb(pdb_directory_and_filename): if element_name_str_space_iter != " ": element_name_j += element_name_str_space_iter - atom_name_list.append(atom_name_j) - element_name_list.append(element_name_j) + if atom_name_j != "": + atom_name_list.append(atom_name_j) + if element_name_j != "": + element_name_list.append(element_name_j) if len(split_line_m) > 0 and str(split_line_m[0]) in ["CRYST1"]: get_atom_type_bool = True - if len(atom_name_list) == 0: + print(element_name_list) + if len(element_name_list) == 0: raise TypeError( "ERROR: The provided pdb format is not the required PDB format " "or the pdb file has zero (0) atoms or beads in it."