Skip to content

Layout

pd2af.building.layout

Layout helpers shared by both language builders: styling, arcs and dot.

Functions:

Name Description
dim_layout_elements

Wash out layout_element_builders in place: the overlay background.

get_layout_elements_sorted_by_id

Return the glyphs that draw model_element, sorted by id_.

get_nearest_layout_element

Return the layout element whose position is nearest position.

harmonize_root_layout

Return layout with a white fill, fitted around its children.

influence_layout_pairs

Return the (source_layout, target_layout) pairs an arc is drawn between.

make_arc_segments_from_source_and_target

Segments for a modulation / influence arc connecting two node layouts.

make_auto_layout

Reposition an already-built layout with graphviz (dot).

nearest_layout_pair

Return the (source_layout, target_layout) pair closest in Euclidean distance.

dim_layout_elements

dim_layout_elements(layout_element_builders: Any) -> None

Wash out layout_element_builders in place: the overlay background.

Source code in src/pd2af/building/layout.py
def dim_layout_elements(layout_element_builders: typing.Any) -> None:
    """Wash out ``layout_element_builders`` in place: the overlay background."""
    layout_element_selector = momapy.styling.ClassSelector("LayoutElement")
    active_border_selector = _ClassNameSuffixSelector("ActiveLayout")
    text_layout_selector = momapy.styling.TypeSelector("TextLayout")
    production_layout_selector = momapy.styling.TypeSelector("ProductionLayout")
    compartment_layout_selector = momapy.styling.TypeSelector(
        "RectangleCompartmentLayout"
    )
    reaction_layout_selector = momapy.styling.OrSelector(
        tuple(
            [
                momapy.styling.TypeSelector(class_name)
                for class_name in [
                    "StateTransitionLayout",
                    "HeterodimerAssociationLayout",
                    "KnownTransitionOmittedLayout",
                    "UnknownTransitionLayout",
                    "TransportLayout",
                    "TranslationLayout",
                    "TranscriptionLayout",
                ]
            ]
        )
    )
    style_sheet = momapy.styling.StyleSheet(
        {
            layout_element_selector: momapy.styling.StyleCollection(
                {
                    "stroke": None,
                    "fill": momapy.coloring.white,
                    "path_stroke": None,
                    "end_arrowhead_stroke": None,
                    "start_arrowhead_stroke": None,
                    "arrowhead_stroke": None,
                    "reaction_node_stroke": None,
                    "active_stroke": None,
                    "inner_stroke": None,
                    "group_stroke": momapy.coloring.lightgray,
                }
            ),
            # Active-border sublayouts (`*ActiveLayout`) are transparent by
            # default and drawn larger, on top of the species body. The blanket
            # rule above sets `fill: white`, which makes the border opaque and
            # paints over the body; restore transparency so the dimmed body
            # shows through. Must follow `layout_element_selector`: matching
            # selectors apply in dict order with no specificity, last wins.
            active_border_selector: momapy.styling.StyleCollection(
                {
                    "fill": momapy.drawing.NoneValue,
                }
            ),
            text_layout_selector: momapy.styling.StyleCollection(
                {
                    "stroke": momapy.drawing.NoneValue,
                    "fill": momapy.coloring.lightgray,
                }
            ),
            production_layout_selector: momapy.styling.StyleCollection(
                {
                    "arrowhead_stroke": momapy.coloring.lightgray,
                    "arrowhead_fill": momapy.coloring.lightgray,
                }
            ),
            compartment_layout_selector: momapy.styling.StyleCollection(
                {
                    "fill": momapy.coloring.lightgray,
                }
            ),
            reaction_layout_selector: momapy.styling.StyleCollection(
                {
                    "end_arrowhead_stroke": momapy.coloring.lightgray,
                    "end_arrowhead_fill": momapy.coloring.lightgray,
                    "start_arrowhead_stroke": momapy.coloring.lightgray,
                    "start_arrowhead_fill": momapy.coloring.lightgray,
                    "_font_fill": momapy.coloring.lightgray,
                }
            ),
        }
    )
    for layout_element_builder in layout_element_builders:
        momapy.styling.apply_style_sheet(
            layout_element_builder, style_sheet, strict=False
        )

get_layout_elements_sorted_by_id

get_layout_elements_sorted_by_id(layout_model_mapping: Any, model_element: Any) -> list

Return the glyphs that draw model_element, sorted by id_.

Source code in src/pd2af/building/layout.py
def get_layout_elements_sorted_by_id(
    layout_model_mapping: typing.Any, model_element: typing.Any
) -> list:
    """Return the glyphs that draw ``model_element``, sorted by ``id_``."""
    return sorted(
        layout_model_mapping.get_representative_layout_elements(model_element),
        key=lambda layout_element: layout_element.id_,
    )

get_nearest_layout_element

get_nearest_layout_element(layout_elements: Any, position: Any) -> Any

Return the layout element whose position is nearest position.

Source code in src/pd2af/building/layout.py
def get_nearest_layout_element(
    layout_elements: typing.Any, position: typing.Any
) -> typing.Any:
    """Return the layout element whose position is nearest ``position``."""
    return min(
        layout_elements,
        key=lambda layout_element: math.dist(
            layout_element.position.to_tuple(), position.to_tuple()
        ),
    )

harmonize_root_layout

harmonize_root_layout(layout: Any) -> Any

Return layout with a white fill, fitted around its children.

Applied uniformly across all layout modes so the rendered canvas has consistent background and padding.

Source code in src/pd2af/building/layout.py
def harmonize_root_layout(layout: typing.Any) -> typing.Any:
    """Return ``layout`` with a white fill, fitted around its children.

    Applied uniformly across all layout modes so the rendered canvas has
    consistent background and padding.
    """
    if not layout.layout_elements:
        # An empty output map (e.g. every activity suppressed via
        # `--set-all-inactive`) has nothing to fit around; `fit` raises
        # on an empty element list. Give the root a minimal, valid canvas so
        # the map still has numeric width/height/position for the writer.
        side = 2 * _ROOT_LAYOUT_SEP
        return dataclasses.replace(
            layout,
            fill=momapy.coloring.white,
            width=side,
            height=side,
            position=momapy.geometry.Point(_ROOT_LAYOUT_SEP, _ROOT_LAYOUT_SEP),
        )
    bounding_box = momapy.positioning.fit(
        layout.layout_elements, xsep=_ROOT_LAYOUT_SEP, ysep=_ROOT_LAYOUT_SEP
    )
    return dataclasses.replace(
        layout,
        fill=momapy.coloring.white,
        width=bounding_box.width,
        height=bounding_box.height,
        position=bounding_box.position,
    )

influence_layout_pairs

influence_layout_pairs(source_layouts: Any, target_layouts: Any, prefer_nearest: bool) -> list[tuple]

Return the (source_layout, target_layout) pairs an arc is drawn between.

By default the full source x target cross product. prefer_nearest (-p nearest) collapses the fan-out to the single closest pair, but only where there is fan-out (more than one layout on a side) and every layout has a real position -- in dot mode positions are throwaway placeholders that graphviz overwrites, so the cross product is kept.

Source code in src/pd2af/building/layout.py
def influence_layout_pairs(
    source_layouts: typing.Any, target_layouts: typing.Any, prefer_nearest: bool
) -> list[tuple]:
    """Return the (source_layout, target_layout) pairs an arc is drawn between.

    By default the full ``source x target`` cross product. ``prefer_nearest``
    (``-p nearest``) collapses the fan-out to the single closest pair, but only
    where there *is* fan-out (more than one layout on a side) and every layout
    has a real ``position`` -- in ``dot`` mode positions are throwaway
    placeholders that graphviz overwrites, so the cross product is kept.
    """
    use_nearest = (
        prefer_nearest
        and (len(source_layouts) > 1 or len(target_layouts) > 1)
        and all(
            layout.position is not None
            for layout in itertools.chain(source_layouts, target_layouts)
        )
    )
    if use_nearest:
        return [nearest_layout_pair(source_layouts, target_layouts)]
    return list(itertools.product(source_layouts, target_layouts))

make_arc_segments_from_source_and_target

make_arc_segments_from_source_and_target(source_layout: Any, target_layout: Any) -> list

Segments for a modulation / influence arc connecting two node layouts.

A self-loop (source is target) bows out into a visible loop via :func:_make_self_loop_segments; otherwise it is drawn straight by :func:_make_straight_segments. Returns a list of segments.

Source code in src/pd2af/building/layout.py
def make_arc_segments_from_source_and_target(
    source_layout: typing.Any, target_layout: typing.Any
) -> list:
    """Segments for a modulation / influence arc connecting two node layouts.

    A self-loop (source is target) bows out into a visible loop via
    :func:`_make_self_loop_segments`; otherwise it is drawn straight by
    :func:`_make_straight_segments`. Returns a list of segments.
    """
    if source_layout is target_layout:
        start_angle, end_angle = _make_self_loop_angles(0, 1)
        return _make_self_loop_segments(source_layout, start_angle, end_angle)
    return _make_straight_segments(source_layout, target_layout)

make_auto_layout

make_auto_layout(cd_map: Any, compartment_layout_classes: Any = (RectangleCompartmentLayout, OvalCompartmentLayout), reversed_arc_classes: Any = (), operator_arc_resolver: Any = None) -> Any

Reposition an already-built layout with graphviz (dot).

Generic over the map language except for the compartment-layout classes, which differ per language: pass compartment_layout_classes to identify the compartment containers (so they become dot clusters rather than nodes). Defaults to the CellDesigner compartment-layout classes.

reversed_arc_classes flips the dot-edge direction of the given arc classes for ranking only (see :func:_build_dot_graph); operator_arc_resolver overrides the rebuilt segments of selected arcs (see :func:_rebuild_arc_geometry). Both default to inert, so callers that pass neither (e.g. CellDesigner) are unaffected.

The root layout of the returned map is fitted around the repositioned elements (see :func:harmonize_root_layout).

Source code in src/pd2af/building/layout.py
def make_auto_layout(
    cd_map: typing.Any,
    compartment_layout_classes: typing.Any = (
        momapy.celldesigner.RectangleCompartmentLayout,
        momapy.celldesigner.OvalCompartmentLayout,
    ),
    reversed_arc_classes: typing.Any = (),
    operator_arc_resolver: typing.Any = None,
) -> typing.Any:
    """Reposition an already-built layout with graphviz (dot).

    Generic over the map language except for the compartment-layout classes,
    which differ per language: pass ``compartment_layout_classes`` to identify
    the compartment containers (so they become dot clusters rather than nodes).
    Defaults to the CellDesigner compartment-layout classes.

    ``reversed_arc_classes`` flips the dot-edge direction of the given arc
    classes for ranking only (see :func:`_build_dot_graph`);
    ``operator_arc_resolver`` overrides the rebuilt segments of selected arcs
    (see :func:`_rebuild_arc_geometry`). Both default to inert, so callers
    that pass neither (e.g. CellDesigner) are unaffected.

    The root layout of the returned map is fitted around the repositioned
    elements (see :func:`harmonize_root_layout`).
    """
    new_map_builder = momapy.builder.builder_from_object(cd_map)
    new_layout_builder = new_map_builder.layout
    (
        dot_graph,
        id_to_layout_element,
        descendant_id_to_top_level_id,
        dot_cluster_name_to_compartment_layout_element,
    ) = _build_dot_graph(
        new_map_builder,
        new_layout_builder,
        compartment_layout_classes,
        reversed_arc_classes=reversed_arc_classes,
    )
    id_to_new_layout_element_builder = _reposition_from_dot(
        dot_graph,
        id_to_layout_element,
        dot_cluster_name_to_compartment_layout_element,
    )
    _rebuild_arc_geometry(
        new_layout_builder,
        id_to_new_layout_element_builder,
        descendant_id_to_top_level_id,
        operator_arc_resolver=operator_arc_resolver,
    )
    new_map = momapy.builder.object_from_builder(new_map_builder)
    return dataclasses.replace(new_map, layout=harmonize_root_layout(new_map.layout))

nearest_layout_pair

nearest_layout_pair(source_layouts: Any, target_layouts: Any) -> tuple

Return the (source_layout, target_layout) pair closest in Euclidean distance.

min over itertools.product is deterministic: on a distance tie it keeps the first pair in product order (source-major), so the choice mirrors the order the cross product would have emitted.

Source code in src/pd2af/building/layout.py
def nearest_layout_pair(
    source_layouts: typing.Any, target_layouts: typing.Any
) -> tuple:
    """Return the (source_layout, target_layout) pair closest in Euclidean distance.

    ``min`` over ``itertools.product`` is deterministic: on a distance tie it
    keeps the first pair in product order (source-major), so the choice mirrors
    the order the cross product would have emitted.
    """
    return min(
        itertools.product(source_layouts, target_layouts),
        key=lambda pair: math.dist(
            pair[0].position.to_tuple(), pair[1].position.to_tuple()
        ),
    )