Compartment
The Compartment class represents a spatial or logical container within the SBMLNetwork model. It groups related biological species and reactions within a defined boundary—just as compartments do in SBML itself.
After loading a model you can:
Get just the IDs (strings) with
get_compartment_ids().Retrieve one compartment by ID with
get_compartment().Get the full list of compartment objects via
get_compartments_list().
Example:
import sbmlnetwork as sn
net = sn.load("my_model.xml")
# Get compartment ids
compartment_ids = net.get_compartment_ids()
# Single compartment
compartment = net.get_compartment(compartment_ids[0])
print(compartment.get_compartment_id())
# All compartments
for comp in net.get_compartments_list():
print(comp.get_compartment_id())
In the visualization context, each Compartment includes its own Shapes and Labels, which define how the compartment is drawn and annotated in the rendered network diagram. The diagram below shows how a Compartment connects to its Shape and Label elements:
![digraph compartment_hierarchy {
rankdir=TB;
node [shape=record, style=rounded, fontname=Helvetica];
Compartment [
label=< <font color="#3366cc">Compartment</font> >,
URL="compartment.html",
tooltip="Click for more about Compartment"
];
Shapes [
label=<
<table border="0" cellborder="0" cellpadding="5">
<tr><td><font color="#3366cc">Shapes</font></td></tr>
</table>
>,
shape=record,
style=rounded,
URL="shape.html",
tooltip="Click for more about Shapes"
];
Labels [
label=<
<table border="0" cellborder="0" cellpadding="5">
<tr><td><font color="#3366cc">Labels</font></td></tr>
</table>
>,
shape=record,
style=rounded,
URL="label.html",
tooltip="Click for more about Labels"
];
Compartment -> Shapes;
Compartment -> Labels;
}](../../_images/graphviz-aff6cc09446141f4095a676ab4649240c007b9f7.png)
Core Methods
Identification
- get_id()
Return the unique identifier of the drawing glyph for this compartment (the CompartmentGlyph element), as defined in the SBML Layout package.
- Return type:
str
- get_compartment_id()
Return the SBML object identifier of the underlying compartment in the model.
- Return type:
str
Contents
- get_species_list()
Return a list‑like wrapper over all species that reside in this compartment.
- Return type:
SpeciesList
- get_species(id)
Fetch a single species (of type
Species) by its SBML identifier.- Parameters:
id (str) – The SBML id of the desired species
- Returns:
The matching Species object
- get_reactions_list()
Return a list‑like wrapper over all reactions whose products or reactants appear in this compartment.
- Return type:
ReactionList
- get_reactions(id_list=None)
Fetch one or many reactions scoped to the compartment.
- Parameters:
id_list (str | Iterable[str] | None) –
Nonefor all reactions, a single id, or an iterable of ids- Returns:
A single Reaction object or a sub‑list depending on id_list
Geometry & Positioning
- get_position()
Get the current (
x, y) position—upper‑left corner by convention—of the compartment on the canvas.- Returns:
(x, y)tuple in canvas units (typically pixels)
- set_position(x, y)
Move the compartment to a new position on the canvas.
- Parameters:
x (float) – X‑coordinate of the upper‑left corner
y (float) – Y‑coordinate of the upper‑left corner
- get_size()
Get the current width and height of the compartment in canvas units.
- Returns:
(width, height)
- set_size(size)
Resize the compartment.
- Parameters:
size (tuple) – (width, height)
Appearance
- add_shape(shape_type='rectangle', **options)
Add a new geometric shape that defines the compartment boundary.
- Parameters:
shape_type (str) –
'rectangle','ellipse', etc.options – Shape‑specific keyword arguments (border thickness, corner radius, …)
- get_shape()
Return the primary
Shapeobject that outlines the compartment.
- set_shape(shape)
Replace the existing shape with shape.
- get_border_color()
Get the current border color (hex string
'#rrggbb').
- set_border_color(color)
Set the border color.
- Parameters:
color (str) – Hex or named color string
- get_fill_color()
Get the interior fill color.
- set_fill_color(color)
Change the fill color.
Labels
- add_label(text, **options)
Attach a textual label to the compartment.
- Parameters:
text (str) – Label text
options – Keyword arguments forwarded to the underlying
Labelconstructor
- get_labels_list()
Return a list of all labels attached to the compartment.
- Return type:
LabelList
Visibility
- show()
Make the compartment (and all nested shapes & labels) visible on the canvas.
- hide()
Hide the compartment without deleting it from the model.
Information
- property info
Human-readable summary of glyph state for debugging.
- Return type:
str
- get_info()
Generate the info string.
Example workflow
import sbmlnetwork as sn
# Load the network from an SBML file
net = sn.load('my_model.xml')
# Get the default compartment
compartment = net.get_compartment()
# Move and resize
compartment.set_position((50, 80))
compartment.set_size((800, 600))
# Change shape to ellipse
compartment.set_shape('ellipse')
# Style tweaks
compartment.set_border_color('#3366cc')
compartment.set_fill_color('#f0f4ff')
# Add a label
compartment.add_label('Cytosol', (0, -20))
# Inspect species
for s in compartment.get_species_list():
print(s.get_id(), s.get_position())
# Finally, draw the full network
net.draw('my_network.png')