Skip to content

Commit

Permalink
Only warn about empty entities
Browse files Browse the repository at this point in the history
Warn about any entities that have no
sequence, but don't raise an exception about
duplicates if there are multiple empty
entities. Closes #128.
  • Loading branch information
benmwebb committed Dec 8, 2023
1 parent c1e79c2 commit ad5aca3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
12 changes: 10 additions & 2 deletions ihm/dumper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import collections
import operator
import itertools
import warnings
import ihm.format
import ihm.format_bcif
import ihm.model
Expand Down Expand Up @@ -261,13 +262,20 @@ def dump(self, system, writer):

class _EntityDumper(Dumper):
def finalize(self, system):
# Assign IDs and check for duplicates
# Assign IDs and check for duplicates or empty entities
seen = {}
empty = []
for num, entity in enumerate(system.entities):
if entity in seen:
if entity in seen and len(entity.sequence) > 0:
raise ValueError("Duplicate entity %s found" % entity)
if len(entity.sequence) == 0:
empty.append(entity)
entity._id = num + 1
seen[entity] = None
if empty:
warnings.warn(
"At least one empty Entity (with no sequence) was found: %s"
% empty)

def dump(self, system, writer):
# Count all molecules (if any) for each entity
Expand Down
27 changes: 27 additions & 0 deletions test/test_dumper.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import utils
import os
import unittest
import warnings
import sys
if sys.version_info[0] >= 3:
from io import StringIO
Expand Down Expand Up @@ -445,6 +446,32 @@ def test_entity_duplicate_branched(self):
#
""")

def test_entity_empty(self):
"""Test EntityDumper with empty entity"""
system = ihm.System()
system.entities.append(ihm.Entity(''))
dumper = ihm.dumper._EntityDumper()

with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
dumper.finalize(system) # Assign IDs
_ = _get_dumper_output(dumper, system)
self.assertEqual(len(w), 1)
self.assertIn('At least one empty Entity', str(w[0].message))

def test_entity_duplicate_empty(self):
"""Test EntityDumper with duplicate empty entities"""
system = ihm.System()
system.entities.append(ihm.Entity(''))
system.entities.append(ihm.Entity(''))
dumper = ihm.dumper._EntityDumper()

with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
dumper.finalize(system) # Assign IDs
_ = _get_dumper_output(dumper, system)
self.assertIn('At least one empty Entity', str(w[0].message))

def test_entity_src_nat_dumper(self):
"""Test EntitySrcNatDumper"""
system = ihm.System()
Expand Down

0 comments on commit ad5aca3

Please sign in to comment.