snapatac2.tl.prune_network#

snapatac2.tl.prune_network(network, node_filter=None, edge_filter=None, remove_isolates=True)[source]#

Filter nodes and edges from a network.

Use this function to build a retained subgraph from node and edge predicates, optionally dropping isolated nodes after filtering.

Anti-Patterns#

  • Do NOT mutate node or edge attributes inside filter callbacks; predicates should only decide whether to retain items.

  • Do NOT assume node indices are preserved; the returned graph has new rustworkx node indices.

param network:

Input graph.

type network:

PyDiGraph

param node_filter:

Predicate returning True for nodes to retain. If None, retain all nodes.

type node_filter:

Optional[Callable[[NodeData], bool]]

param edge_filter:

Predicate returning True for edges to retain. Receives original source index, target index, and edge data. If None, retain all eligible edges.

type edge_filter:

Optional[Callable[[int, int, LinkData], bool]]

param remove_isolates:

If True, remove nodes with no incoming or outgoing edges after filtering.

type remove_isolates:

bool

returns:

Filtered graph.

rtype:

PyDiGraph

Examples

>>> import snapatac2 as snap
>>> network = snap.tl.init_network_from_annotation(["chr1:10000-10500"], snap.genome.hg38)
>>> pruned = snap.tl.prune_network(network, node_filter=lambda node: node.type != "motif")
>>> pruned.num_nodes() >= 0
True