diff --git a/ihm/dumper.py b/ihm/dumper.py index ce4a858..a25edaf 100644 --- a/ihm/dumper.py +++ b/ihm/dumper.py @@ -5,6 +5,7 @@ import collections import operator import itertools +import warnings import ihm.format import ihm.format_bcif import ihm.model @@ -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 diff --git a/test/test_dumper.py b/test/test_dumper.py index db9131d..d2c957c 100644 --- a/test/test_dumper.py +++ b/test/test_dumper.py @@ -1,6 +1,7 @@ import utils import os import unittest +import warnings import sys if sys.version_info[0] >= 3: from io import StringIO @@ -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()