I/O
momapy.io
IO subpackage for reading and writing maps.
Provides functions and registries for reading and writing molecular maps in various formats (SBGN-ML, CellDesigner, SBML, etc.).
Examples:
from momapy.io import get_reader, get_writer
reader = get_reader("sbgnml")
writer = get_writer("sbgnml")
Modules:
| Name | Description |
|---|---|
core |
Base classes and functions for reading and writing maps. |
pickle |
Pickle-based reader and writer. |
Classes:
| Name | Description |
|---|---|
IOResult |
Base class for I/O results. |
Reader |
Abstract base class for map readers. |
ReaderResult |
Result from reading a map file. |
Writer |
Abstract base class for map writers. |
WriterResult |
Result from writing a map file. |
Functions:
| Name | Description |
|---|---|
get_reader |
Get a reader class by name. |
get_writer |
Get a writer class by name. |
list_readers |
List all available reader names. |
list_writers |
List all available writer names. |
read |
Read a map file. |
register_lazy_reader |
Register a reader for lazy loading. |
register_lazy_writer |
Register a writer for lazy loading. |
register_reader |
Register a reader class. |
register_writer |
Register a writer class. |
write |
Write an object to a file. |
Reader
Bases: ABC
Abstract base class for map readers.
Implementations must override read() and check_file() methods.
Examples:
class MyFormatReader(Reader):
@classmethod
def read(cls, file_path, **options):
# Implementation
pass
@classmethod
def check_file(cls, file_path):
return file_path.endswith(".myfmt")
Methods:
| Name | Description |
|---|---|
check_file |
Check if this reader supports the given file. |
read |
Read a file and return the result. |
check_file
abstractmethod
classmethod
Check if this reader supports the given file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str | PathLike
|
Path of the file to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the file is supported by this reader. |
Source code in src/momapy/io/core.py
read
abstractmethod
classmethod
read(file_path: str | PathLike, **options: Any) -> ReaderResult
Read a file and return the result.
Concrete readers accept the universal option set with these names,
types, and defaults, plus a trailing **options so unknown keywords
are accepted and ignored:
return_type: Literal["map", "model", "layout"] = "map"— the shape ofresult.obj."map"returns aMap(the uniform default for every format),"model"returns theModel,"layout"returns theLayout. A layout-less format (SBML) raisesNotImplementedErrorfor"layout"and returns aMapwithlayout=Nonefor"map".with_model: bool = True— whether to build the model.with_layout: bool = True— whether to build the layout (no effect on layout-less formats).with_annotations: bool = True— whether to read annotations.with_notes: bool = True— whether to read notes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str | PathLike
|
Path of the file to read. |
required |
options
|
Any
|
Reader-specific options (see the universal set above). |
{}
|
Returns:
| Type | Description |
|---|---|
ReaderResult
|
ReaderResult containing the read object. |
Source code in src/momapy/io/core.py
ReaderResult
dataclass
ReaderResult(*, obj: Any = None, element_to_annotations: frozendict | None = None, element_to_notes: frozendict | None = None, id_to_element: frozendict | None = None, source_id_to_model_element: FrozenIdentityMultiDict | None = None, source_id_to_layout_element: FrozenSurjectionDict | None = None, source_id_to_annotations: frozendict | None = None, source_id_to_notes: frozendict | None = None, file_path: str | PathLike | None = None)
Bases: IOResult
Result from reading a map file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
The read map object (MapElement or None). |
None
|
element_to_annotations
|
frozendict | None
|
Mapping from map elements to their annotations. |
None
|
element_to_notes
|
frozendict | None
|
Mapping from map elements to their notes. |
None
|
id_to_element
|
frozendict | None
|
Mapping from momapy element IDs to elements. Contains all model and layout elements regardless of source ID provenance. |
None
|
source_id_to_model_element
|
FrozenIdentityMultiDict | None
|
Mapping from source file IDs (e.g. XML attributes) to the set of model elements named by that ID. Only contains IDs that exist verbatim in the source file. One source ID may name several model elements — for example a CellDesigner |
None
|
source_id_to_layout_element
|
FrozenSurjectionDict | None
|
Mapping from source file IDs (e.g. XML attributes) to layout elements. Only contains IDs that exist verbatim in the source file. Unlike |
None
|
source_id_to_annotations
|
frozendict | None
|
Mapping from source file IDs to the RDF/MIRIAM annotations attached to that specific source element. Parallel to |
None
|
source_id_to_notes
|
frozendict | None
|
Mapping from source file IDs to the free text |
None
|
file_path
|
str | PathLike | None
|
Path of the file that was read. |
None
|
Writer
Bases: ABC
Abstract base class for map writers.
Implementations must override the write() method.
Examples:
class MyFormatWriter(Writer):
@classmethod
def write(cls, obj, file_path, **options):
# Implementation
pass
Methods:
| Name | Description |
|---|---|
write |
Write an object to a file. |
write
abstractmethod
classmethod
write(obj: Any, file_path: str | PathLike, **options: Any) -> WriterResult
Write an object to a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
Object to write. |
required |
file_path
|
str | PathLike
|
Path of the file to write to. |
required |
options
|
Any
|
Writer-specific options. |
{}
|
Returns:
| Type | Description |
|---|---|
WriterResult
|
WriterResult containing the written object. |
Source code in src/momapy/io/core.py
WriterResult
dataclass
Bases: IOResult
Result from writing a map file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
The written object. |
None
|
file_path
|
str | PathLike | None
|
Path of the file that was written. |
None
|
get_reader
get_reader(name: str) -> type[Reader]
Get a reader class by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Reader name (e.g., "sbgnml", "celldesigner"). |
required |
Returns:
| Type | Description |
|---|---|
type[Reader]
|
Reader class for the specified format. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no reader with that name exists. |
ImportError
|
If the reader is registered but its module cannot be imported (e.g. a missing optional dependency). |
Source code in src/momapy/io/core.py
get_writer
get_writer(name: str) -> type[Writer]
Get a writer class by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Writer name (e.g., "sbgnml", "pickle"). |
required |
Returns:
| Type | Description |
|---|---|
type[Writer]
|
Writer class for the specified format. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no writer with that name exists. |
ImportError
|
If the writer is registered but its module cannot be imported (e.g. a missing optional dependency). |
Source code in src/momapy/io/core.py
list_readers
List all available reader names.
Returns:
| Type | Description |
|---|---|
list[str]
|
Sorted list of available reader names. |
list_writers
List all available writer names.
Returns:
| Type | Description |
|---|---|
list[str]
|
Sorted list of available writer names. |
read
read(file_path: str | PathLike, reader: str | None = None, **options: Any) -> ReaderResult
Read a map file.
If reader is specified, uses that reader. Otherwise, checks registered readers in registration order and uses the first whose check_file method accepts the file. Registration order matters: a more specific reader must be registered before a more general one it specializes. For example a CellDesigner file is also a valid SBML file, so the CellDesigner reader is registered (and therefore checked) before the SBML reader.
Return contract: the shape of result.obj is governed by the universal
return_type option, uniformly across every format:
return_type="map"(the default) returns aMap. This is the uniform default for all formats; a layout-less format (SBML) returns aMapwithlayout=Noneandlayout_model_mapping=None.return_type="model"returns the bareModel.return_type="layout"returns theLayout; a layout-less format (SBML) raisesNotImplementedError.
Universal options accepted by every reader (with these defaults):
return_type="map", with_model=True, with_layout=True,
with_annotations=True, with_notes=True. Unknown keyword options are
accepted and ignored. Some formats accept extra options (for example the
SBGN-ML reader's xsep/ysep). A universal option may also be inert for a
given format: a layout-less format (SBML) ignores with_layout, since it
never produces a layout.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str | PathLike
|
Path of the file to read. |
required |
reader
|
str | None
|
Name of registered reader to use (e.g., "sbgnml"). If None, auto-detects based on file format. |
None
|
options
|
Any
|
Additional options passed to the reader (see the universal option set above, plus any format-specific options). |
{}
|
Returns:
| Type | Description |
|---|---|
ReaderResult
|
ReaderResult containing the read object and metadata. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If no file exists at |
ValueError
|
If no suitable reader is found. |
NotImplementedError
|
If |
Examples:
Source code in src/momapy/io/core.py
register_lazy_reader
Register a reader for lazy loading.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name to register the reader under. |
required |
import_path
|
str
|
Import path in format "module.path:ClassName". |
required |
Source code in src/momapy/io/core.py
register_lazy_writer
Register a writer for lazy loading.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name to register the writer under. |
required |
import_path
|
str
|
Import path in format "module.path:ClassName". |
required |
Source code in src/momapy/io/core.py
register_reader
register_reader(name: str, cls: type[Reader]) -> None
Register a reader class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name to register the reader under. |
required |
cls
|
type[Reader]
|
Reader class (must inherit from Reader). |
required |
register_writer
register_writer(name: str, cls: type[Writer]) -> None
Register a writer class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name to register the writer under. |
required |
cls
|
type[Writer]
|
Writer class (must inherit from Writer). |
required |
write
write(obj: Any, file_path: str | PathLike, writer: str | None = None, **options: Any) -> WriterResult
Write an object to a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
Object to write (typically a MapElement). |
required |
file_path
|
str | PathLike
|
Path of the file to write to. |
required |
writer
|
str | None
|
Name of registered writer to use (e.g., "sbgnml"). When
|
None
|
options
|
Any
|
Additional options passed to the writer. |
{}
|
Returns:
| Type | Description |
|---|---|
WriterResult
|
WriterResult containing the written object and metadata. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the writer is not found or cannot be inferred. |
Examples: