Coverage for src/python/ensembl/vep/render/graph/model.py: 100%
49 statements
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-18 14:41 +0000
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-18 14:41 +0000
1# See the NOTICE file distributed with this work for additional information
2# regarding copyright ownership.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14"""A directed graph carrying semantics, not styling.
16A builder describes what a node *is* -- its kind, which layer it belongs to, what state it ended
17in -- and never what colour it should be. Renderers map those semantics onto an appearance, so
18every styling decision stays in one package and a builder needs no knowledge of the theme.
19"""
21from __future__ import annotations
23from attrs import define, field
25__all__ = ["Edge", "Graph", "Node"]
28@define(frozen=True, kw_only=True, auto_attribs=True)
29class Node:
30 """One vertex.
32 Attributes:
33 identifier (str): Unique within a graph, and what edges refer to.
34 label (str): The text a renderer draws.
35 kind (str): What the node represents, e.g. "fact", "rule", "term".
36 layer (int): Which column or row the node belongs to. Layered layouts read this directly;
37 it comes from something meaningful in the domain, such as a fact's cost tier or a
38 rule's rank.
39 state (str): How the node ended up, e.g. "computed", "forced_false", "fired".
40 hits (int): How many times the node was reached, for a corpus-reach overlay.
41 detail (str): Optional secondary text, e.g. a value or a reason.
42 """
44 identifier: str
45 label: str
46 kind: str = field(default="")
47 layer: int = field(default=0)
48 state: str = field(default="")
49 hits: int = field(default=0)
50 detail: str = field(default="")
53@define(frozen=True, kw_only=True, auto_attribs=True)
54class Edge:
55 """One directed connection between two nodes.
57 Attributes:
58 source (str): The identifier the edge leaves.
59 target (str): The identifier the edge enters.
60 kind (str): What the connection means, e.g. "depends", "requires", "subsumes".
61 label (str): Optional text drawn along the edge.
62 """
64 source: str
65 target: str
66 kind: str = field(default="")
67 label: str = field(default="")
70@define(frozen=True, kw_only=True, auto_attribs=True)
71class Graph:
72 """A set of nodes and the edges between them.
74 Attributes:
75 nodes (tuple[Node, ...]): The vertices, in the order a builder produced them.
76 edges (tuple[Edge, ...]): The connections.
77 title (str): What the graph shows.
78 """
80 nodes: tuple[Node, ...] = field(default=())
81 edges: tuple[Edge, ...] = field(default=())
82 title: str = field(default="")
84 def node(self, identifier: str) -> Node:
85 """Return one node by its identifier.
87 Raises:
88 KeyError: If no node has that identifier.
89 """
90 for node in self.nodes:
91 if node.identifier == identifier:
92 return node
93 raise KeyError(f"Unknown node '{identifier}'")
95 def layers(self) -> tuple[int, ...]:
96 """Return the distinct layer numbers present, in ascending order."""
97 present = {node.layer for node in self.nodes}
98 return tuple(sorted(present))
100 def nodes_in_layer(self, layer: int) -> tuple[Node, ...]:
101 """Return the nodes of one layer, in declaration order."""
102 selected = []
103 for node in self.nodes:
104 if node.layer == layer:
105 selected.append(node)
106 return tuple(selected)
108 def edges_from(self, identifier: str) -> tuple[Edge, ...]:
109 """Return the edges leaving one node."""
110 leaving = []
111 for edge in self.edges:
112 if edge.source == identifier:
113 leaving.append(edge)
114 return tuple(leaving)
116 def with_hits(self, hits_by_identifier: dict[str, int]) -> Graph:
117 """Return a copy whose nodes carry the given hit counts.
119 How a corpus-reach overlay is applied: the shape of the graph is unchanged, only what each
120 node reports about how often it was reached.
121 """
122 updated = []
123 for node in self.nodes:
124 hits = hits_by_identifier.get(node.identifier, 0)
125 updated.append(
126 Node(
127 identifier=node.identifier,
128 label=node.label,
129 kind=node.kind,
130 layer=node.layer,
131 state=node.state,
132 hits=hits,
133 detail=node.detail,
134 )
135 )
136 return Graph(nodes=tuple(updated), edges=self.edges, title=self.title)