Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions PQAnalysis/io/moldescriptor_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ def read(self) -> Residues:
------
MoldescriptorReaderError
If the number of columns in the header of a mol type is not 3.
MoldescriptorReaderError
If a mol type declares more atoms than atom lines follow
before the end of the file.
"""
with open(self.filename, 'r', encoding='utf-8') as f:
lines = f.readlines()
Expand Down Expand Up @@ -103,6 +106,17 @@ def read(self) -> Residues:

n_atoms = int(splitted_line[1])

if counter + n_atoms + 1 > len(lines):
self.logger.error(
(
f"The mol type {splitted_line[0]} declares "
f"{n_atoms} atoms, but only "
f"{len(lines) - counter - 1} atom lines follow "
"before the end of the file.\n"
),
exception=MoldescriptorReaderError
)

mol_types.append(
self._read_mol_type(
lines[counter:counter + n_atoms + 1],
Expand Down
4 changes: 4 additions & 0 deletions tests/data/readMoldescriptor/moldescriptor_tooFewAtoms.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Molecule 1
H2O 3 0.0
O 0 -0.65966
H 1 0.32983
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
H2O 3 0.0
O 0 -0.65966
H 1 0.32983
AR 1 0.0
Ar 2 0.0
22 changes: 22 additions & 0 deletions tests/io/test_moldescriptorReader.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,25 @@ def test_read(self, test_with_data_dir):
assert str(
exception.value
) == "The number of columns in the header of a mol type must be 3.\nGot 2 columns instead in text: ' H2O 3'\n"

@pytest.mark.parametrize(
"example_dir",
["readMoldescriptor"],
indirect=False
)
def test_read_too_few_atom_lines(self, test_with_data_dir):
reader = MoldescriptorReader("moldescriptor_tooFewAtoms.dat")
with pytest.raises(MoldescriptorReaderError) as exception:
reader.read()
assert str(exception.value) == (
"The mol type H2O declares 3 atoms, but only 2 atom lines "
"follow before the end of the file.\n"
)

reader = MoldescriptorReader("moldescriptor_tooFewAtoms_midFile.dat")
with pytest.raises(MoldescriptorReaderError) as exception:
reader.read()
assert str(exception.value) == (
"The mol type Ar declares 2 atoms, but only 0 atom lines "
"follow before the end of the file.\n"
)
Loading