omex¶
COMBINE archive (OMEX) support.
A COMBINE archive is a single file which bundles everything belonging to a
modeling project: models (SBML, CellML), simulation experiments (SED-ML), data,
figures and documentation. It is a ZIP container with a manifest.xml at its
root listing every file together with its format, given as an identifiers.org
URI rather than guessed from the file suffix.
This module provides three classes:
Omex: the archive; reading, writing and access to its filesManifest: the entries of the archive, i.e., themanifest.xmlManifestEntry: a single file withlocation,formatandmaster
and two errors:
PasswordError: aValueErrorfor a wrong or missing passwordConcurrentUseError: aRuntimeErrorfor one archive used by two threads
Example
Read an existing archive and list the SBML models it contains:
from pathlib import Path
from pymetadata.omex import Omex
omex = Omex.from_omex(Path("archive.omex"))
for entry in omex.entries_by_format("sbml"):
print(entry.location, omex.get_path(entry.location))
Create an archive from single files:
Encrypted archives can be read by passing a password; writing encrypted archives is not supported. Manipulation of OMEX metadata is not supported.
References
Bergmann FT, Adams R, Moodie S, Cooper J, Glont M, Golebiewski M, Hucka M, Laibe C, Miller AK, Nickerson DP, Olivier BG, Rodriguez N, Sauro HM, Scharm M, Soiland-Reyes S, Waltemath D, Yvon F, Le Novere N. COMBINE archive and OMEX format: one file to share all information to reproduce a modeling project. BMC Bioinformatics. 2014;15(1):369. https://doi.org/10.1186/s12859-014-0369-z
Bergmann FT, Rodriguez N, Le Novere N. COMBINE Archive Specification Version 1. J Integr Bioinform. 2015;12(2):261. https://doi.org/10.2390/biecoll-jib-2015-261
ConcurrentUseError
¶
Bases: RuntimeError
One archive used by two threads at the same time.
The core gives access to one archive to one thread at a time and refuses a
second one instead of waiting. Omex serializes the calls of its own
methods, so one Omex can be used from several threads; this error is the
backstop for an archive which is shared in another way, e.g., for two
Omex instances built around the same Archive object.
A RuntimeError, not a ValueError: nothing is wrong with the values the
caller passed, the call came at the wrong moment and can be repeated.
It cannot be raised by open_archive, which builds a new archive no other
thread can hold yet.
PasswordError
¶
Bases: ValueError
A member of an archive which the given password does not open.
A subclass of ValueError, so code which catches the ValueError of the
bridge keeps working. It is raised for a password which is wrong and for a
password which is missing, and it proves that the member exists. A caller
can tell it from a container which has no manifest at all, but
Omex.is_omex does not need to: it asks about the structure of the zip
and never reads a member.
EntryFormat
¶
Bases: str, Enum
Format URIs used in the manifest.xml.
COMBINE specifications (SBML, SED-ML, CellML, SBGN, BioPAX, OMEX metadata,
FROG results) are identified by http://identifiers.org/combine.specifications/*
URIs, all other files by their media type via https://purl.org/NET/mediatypes/*.
Where a specification is versioned, both the generic and the level and
version specific term exist, e.g., SBML and SBML_L3V2.
ManifestEntry
¶
Bases: BaseModel
A single file of the archive, as listed in the manifest.xml.
Attributes:
| Name | Type | Description |
|---|---|---|
location |
str
|
location of the file in the archive, relative and starting
with |
format |
str
|
format URI of the file, see |
master |
bool
|
marks the entry a tool should open first, e.g., the SED-ML file of a simulation study |
is_format
staticmethod
¶
Check if a format URI matches a format key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
format_key
|
str
|
|
required |
format
|
str
|
format URI to check |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the format matches the key. |
Manifest
¶
Bases: BaseModel
Content of the manifest.xml, i.e., the entries of an archive.
The manifest behaves like a mapping keyed by location and always contains
the two entries required by the specification: the archive itself (.) and
the manifest (./manifest.xml).
Attributes:
| Name | Type | Description |
|---|---|---|
entries |
list[ManifestEntry]
|
the manifest entries |
Example
Initialize Manifest.
from_manifest
classmethod
¶
Read a manifest from a manifest.xml file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
manifest_path
|
Path
|
path of the |
required |
Returns:
| Type | Description |
|---|---|
Manifest
|
Manifest with the entries listed in the file. |
to_manifest_xml
¶
Serialize the manifest to manifest.xml content.
Returns:
| Type | Description |
|---|---|
str
|
The XML of the manifest as a string. |
to_manifest
¶
Write the manifest to a manifest.xml file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
manifest_path
|
Path
|
path of the file to write |
required |
add_entry
¶
Add an entry to the manifest.
Duplicated locations are not checked, use Omex.add_entry to add
a file together with its entry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entry
|
ManifestEntry
|
entry to add |
required |
Omex
¶
COMBINE archive (OMEX), version 1.
The content of the archive is kept in a temporary directory, manifest
holds the corresponding entries. An archive read with Omex.from_omex or
Omex.from_url is read lazily: its manifest is complete at once, while
the file of an entry appears in the temporary directory when a caller asks
for it, see Omex.get_path. Writing it back with Omex.to_omex copies
every entry which was neither replaced nor asked for as the compressed
blob it is, so reading an archive and changing one entry does not inflate
and deflate the rest.
Use the from_* constructors to read an archive and the to_* methods to
write one:
| read | write |
|---|---|
Omex.from_omex from an omex file |
Omex.to_omex to an omex file |
Omex.from_url from a url |
Omex.to_directory to a directory |
Omex.from_directory from a directory |
An empty archive is filled with Omex.add_entry.
One Omex can be used from several threads: the calls which reach the
archive it was read from, i.e., Omex.get_path, Omex.to_omex and
Omex.to_directory, are serialized with a lock of the instance, because
the core gives access to one archive to one thread at a time. They are
serialized, not parallel, so threads do not make reading an archive faster;
they only keep working. Changing the manifest of one Omex from several
threads at the same time is not supported.
Attributes:
| Name | Type | Description |
|---|---|---|
manifest |
Manifest
|
entries of the archive, i.e., the content of the |
The temporary directory is removed by Omex.close, when the archive is
used as a context manager and the block is left, or at the latest when the
archive is garbage collected.
Example
Using the archive as a context manager removes the temporary directory when the block is left:
Create an empty COMBINE archive.
close
¶
Remove the temporary directory with the archive content.
The content which was read from a file is released as well, so that
nothing is extracted into the removed directory afterwards. The archive
cannot be used afterwards. Calling close more than once has no effect.
get_path
¶
Get the path of an entry in the extracted archive.
The file of an entry of a read archive is extracted into the temporary
directory here, i.e., when it is asked for the first time. Several
threads can ask one Omex for a path: the extractions are serialized
with a lock of the instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
location
|
str
|
location of the entry, e.g., |
required |
Returns:
| Type | Description |
|---|---|
Path
|
Path of the file in the temporary directory of the archive, which |
Path
|
can be passed on to a reader such as libsbml. |
Raises:
| Type | Description |
|---|---|
KeyError
|
if no entry exists for the location |
ValueError
|
if the content of the entry cannot be read, e.g., for a member compressed with a method which is not supported |
ConcurrentUseError
|
if the same archive is used by another thread
outside of this |
is_omex
staticmethod
¶
Check if the path is a COMBINE archive.
The file must be a zip archive containing a manifest.xml. This is a
question about the structure of the file: it is answered from the
central directory of the zip, so it does not depend on the size of the
file and does not read any member. An encrypted archive is a COMBINE
archive as well, its manifest is only not readable without the password.
The names of the central directory are normalized before they are
compared, as Omex.from_omex normalizes them, so a container whose
manifest is stored as ./manifest.xml, or with a backslash as the
separator, is an archive for both. A name which cannot be normalized,
because it is absolute or points outside of the archive, is no
manifest.xml; such a container is still structurally an archive if
another member is one, although Omex.from_omex refuses it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
omex_path
|
Path
|
path to check |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the path is a COMBINE archive. |
Raises:
| Type | Description |
|---|---|
ValueError
|
if the path does not exist or is not a file |
OSError
|
if the file exists and cannot be read |
from_omex
staticmethod
¶
Read a COMBINE archive from a path.
The entries are taken from the manifest.xml of the archive, followed
by the members the manifest does not list, sorted by their location; an
entry of the manifest which no member provides is dropped. The content
of an entry is extracted into the temporary directory of the archive
when it is asked for, see Omex.get_path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
omex_path
|
Path
|
path of the omex file |
required |
password
|
bytes | None
|
password of an encrypted archive |
None
|
Returns:
| Type | Description |
|---|---|
Omex
|
Omex with the content of the archive. |
Raises:
| Type | Description |
|---|---|
ValueError
|
if the path does not exist, is not a file or is not a
COMBINE archive, i.e., a zip archive with a |
from_url
staticmethod
¶
Read a COMBINE archive from a url.
The archive is downloaded to a temporary file and read from there.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
omex_url
|
str
|
url of the omex file |
required |
password
|
bytes | None
|
password of an encrypted archive |
None
|
Returns:
| Type | Description |
|---|---|
Omex
|
Omex with the content of the archive. |
Raises:
| Type | Description |
|---|---|
ValueError
|
if the url is not an http or https url |
RequestException
|
if the archive could not be downloaded |
from_directory
classmethod
¶
Create a COMBINE archive from a directory.
If the directory contains a manifest.xml, the entries listed there are
reused. The format of every other file is inferred with
Omex.guess_format; SED-ML files added this way get master=True,
as they are the entry point of a simulation study.
The entries are in the order of the manifest, followed by the files which the manifest does not list, sorted by their location. The order does not depend on the file system.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
directory
|
Path
|
directory with the content of the archive |
required |
Returns:
| Type | Description |
|---|---|
Omex
|
Omex with one entry per file in the directory. |
Raises:
| Type | Description |
|---|---|
ValueError
|
if the directory does not exist or is not a directory |
add_entry
¶
Add a file to the archive.
The file is copied into the archive, i.e., later changes to the source file do not affect the content of the archive. Adding a second entry for an existing location replaces the first one and logs a warning.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entry_path
|
Path
|
path of the file to add |
required |
entry
|
ManifestEntry
|
manifest entry describing location, format and master flag |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
if |
remove_entry_for_location
¶
Remove an entry and the corresponding file from the archive.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
location
|
str
|
location of the entry, e.g., |
required |
Returns:
| Type | Description |
|---|---|
ManifestEntry | None
|
The removed entry, or None if no entry exists for the location. |
to_omex
¶
Write the archive to an omex file.
The manifest.xml is generated from the entries of the archive. By
definition OMEX files are zip deflated. The archive is written to a
temporary file first, so a failing write does not leave a broken file
or destroy an existing one.
An entry of an archive which was read from a file and whose content was
neither replaced nor extracted is copied as the compressed blob it is:
it is neither inflated nor deflated again and keeps the compression of
the archive it came from, which is what makes writing an archive back
cheap. A compresslevel other than the default and a compression
other than ZIP_DEFLATED deflate every entry again instead.
A write applies the removals of the manifest to the archive this one was
read from, so writing is not free of side effects: an entry which was
removed from manifest loses its content here when the archive is
written. Declaring that location again afterwards and writing a second
time fails with FileNotFoundError, because neither a file in the
temporary directory nor content in the archive is left for it. A write
which repeats the same entries writes the same bytes again, and
removing an entry with Omex.remove_entry_for_location before the first
write is unaffected.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
omex_path
|
Path
|
path of the omex file to write |
required |
password
|
str | None
|
unused, encrypted archives cannot be written yet |
None
|
compression
|
int
|
zipfile compression algorithm. A COMBINE archive is
deflated by definition and nothing else is written: a request
for another algorithm is logged and the entries are deflated,
|
ZIP_DEFLATED
|
compresslevel
|
int
|
level of compression, 0-9 (see zlib). Larger values
compress better. Only a level other than the default deflates
the entries again. |
9
|
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
if an entry of the manifest has no content, i.e., neither a file in the temporary directory nor content in the archive this one was read from, which includes an entry whose file a caller added or extracted and then removed |
ValueError
|
if the content of an entry cannot be read, the core
refuses an entry or |
TypeError
|
if |
ConcurrentUseError
|
if the same archive is used by another thread
outside of this |
to_directory
¶
Extract the archive to a directory.
The manifest.xml is written next to the files, so the result can be
read back with Omex.from_directory. Every entry is written once: the
content of an entry which was not extracted before goes from the
archive to the directory, not through the temporary directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output_dir
|
Path
|
directory to write to, created if it does not exist |
required |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
if an entry of the manifest has no content, i.e.,
neither a file in the temporary directory nor content in the
archive this one was read from, which includes an entry whose
file a caller added or extracted and then removed. It names the
missing file and sets |
ValueError
|
if the content of an entry cannot be read |
ConcurrentUseError
|
if the same archive is used by another thread
outside of this |
entries_by_format
¶
Get all entries of a given format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
format_key
|
str
|
|
required |
Returns:
| Type | Description |
|---|---|
list[ManifestEntry]
|
List of matching entries, empty if the archive contains none. |
lookup_format
staticmethod
¶
Look up the format URI for a format key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
format_key
|
str
|
name of an |
required |
Returns:
| Type | Description |
|---|---|
str
|
The format URI, or the URI for an unknown media type if the key |
str
|
cannot be resolved. |
guess_format
staticmethod
¶
Guess the format URI of a file.
The start of .xml files is inspected to tell SBML, SED-ML, CellML and
COPASI apart; for every other file the suffix decides.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
path of the file |
required |
Returns:
| Type | Description |
|---|---|
str
|
The format URI, or the URI for an unknown media type if the format |
str
|
cannot be determined. |
normalize_location
¶
Normalize the location of an entry in a COMBINE archive.
Locations are relative POSIX paths starting with ./, e.g.,
./models/model.xml; the archive itself has the location .. A missing
./ prefix is added, backslashes are converted to slashes and empty or .
path segments are removed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
location
|
str
|
location to normalize |
required |
Returns:
| Type | Description |
|---|---|
str
|
The normalized location. |
Raises:
| Type | Description |
|---|---|
ValueError
|
if the location is empty, absolute or points outside of the
archive, e.g., |