Skip to content

Commit

Permalink
List external files/databases in report
Browse files Browse the repository at this point in the history
  • Loading branch information
benmwebb committed Aug 25, 2023
1 parent 344ce6e commit ad465f4
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
11 changes: 11 additions & 0 deletions ihm/location.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ def __init__(self, db_code, version=None, details=None):
self.access_code = db_code
self.version = version

def __str__(self):
return "<%s.%s(%s)>" % (self.__module__, self.__class__.__name__,
repr(self.access_code))


class EMDBLocation(DatabaseLocation):
"""Something stored in the EMDB database.
Expand Down Expand Up @@ -207,6 +211,10 @@ def __init__(self, path, repo=None, details=None):
# Store absolute path in case the working directory changes later
self.path = os.path.abspath(path)

def __str__(self):
return "<%s.%s(%s)>" % (self.__module__, self.__class__.__name__,
repr(self.path))


class InputFileLocation(FileLocation):
"""An externally stored file used as input.
Expand Down Expand Up @@ -294,6 +302,9 @@ def __eq__(self, other):
def __hash__(self):
return hash((self.doi, self.url))

def __str__(self):
return "<ihm.location.Repository(%r)>" % self.doi

def __init__(self, doi, root=None, url=None, top_directory=None,
details=None):
# todo: DOI should be optional (could also use URL, local path)
Expand Down
29 changes: 29 additions & 0 deletions ihm/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ class MissingDataWarning(UserWarning):
pass


class LocalFilesWarning(UserWarning):
pass


class _SectionReporter(object):
def __init__(self, title, fh):
self.fh = fh
Expand All @@ -29,6 +33,8 @@ def report(self):
print("Title: %s" % self.system.title, file=self.fh)
self.report_entities()
self.report_asyms()
self.report_databases()
self.report_files()
self.report_citations()
self.report_software()

Expand Down Expand Up @@ -69,3 +75,26 @@ def report_software(self):
if not s.citation:
warnings.warn(
"No citation provided for %s" % s, MissingDataWarning)

def report_databases(self):
r = self._section("External databases referenced")
for loc in ihm._remove_identical(self.system._all_locations()):
if isinstance(loc, ihm.location.DatabaseLocation):
r.report(" - %s accession %s"
% (loc.db_name, loc.access_code))

def report_files(self):
r = self._section("Additional files referenced")
locs_by_repo = collections.defaultdict(list)
for loc in ihm._remove_identical(self.system._all_locations()):
if not isinstance(loc, ihm.location.DatabaseLocation):
locs_by_repo[loc.repo].append(loc)
for repo, locs in locs_by_repo.items():
r.report("- %s" % ("DOI: " + repo.doi if repo else "Local files"))
for loc in locs:
r.report(" - %r, %s" % (loc.path, loc.details))
if None in locs_by_repo:
warnings.warn(
"The following local files are referenced (they will need to "
"be deposited in a database or with a DOI): %s"
% [loc.path for loc in locs_by_repo[None]], LocalFilesWarning)
36 changes: 36 additions & 0 deletions test/test_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import ihm
import ihm.report
import ihm.reference
import ihm.location


class Tests(unittest.TestCase):
Expand Down Expand Up @@ -73,6 +74,41 @@ def test_software(self):
soft.citation = c
r.report_software()

def test_databases(self):
"""Test report_databases"""
sio = StringIO()
s = ihm.System(title='test system')
s.locations.append(
ihm.location.BMRBLocation('27600', version='foo', details='bar'))
s.locations.append(
ihm.location.FileLocation(repo='mydoi', path='a'))
r = ihm.report.Reporter(s, sio)
r.report_databases()

def test_files(self):
"""Test report_files"""
sio = StringIO()
s = ihm.System(title='test system')
repo = ihm.location.Repository(doi='1.2.3.4')
s.locations.append(
ihm.location.BMRBLocation('27600', version='foo', details='bar'))
s.locations.append(
ihm.location.FileLocation(repo=repo, path='a'))
r = ihm.report.Reporter(s, sio)
r.report_files()

def test_files_local(self):
"""Test report_files with local files"""
sio = StringIO()
s = ihm.System(title='test system')
s.locations.append(
ihm.location.BMRBLocation('27600', version='foo', details='bar'))
s.locations.append(
ihm.location.FileLocation(repo=None, path='.'))
r = ihm.report.Reporter(s, sio)
# Should warn about local files
self.assertWarns(ihm.report.LocalFilesWarning, r.report_files)


if __name__ == '__main__':
unittest.main()

0 comments on commit ad465f4

Please sign in to comment.