From cfebf3d77b7584b542f7cdfba5b0cc9c64a3a9be Mon Sep 17 00:00:00 2001 From: Iago Bonnici Date: Thu, 1 Aug 2024 14:51:18 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20Expose=20`adjacency=5Fmatrix()`?= =?UTF-8?q?=20and=20variants.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/basic_topology_queries.jl | 46 +++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/basic_topology_queries.jl b/src/basic_topology_queries.jl index e32da046a..cdb104814 100644 --- a/src/basic_topology_queries.jl +++ b/src/basic_topology_queries.jl @@ -312,6 +312,52 @@ function live_tops(g::Topology, tops_indices) end export live_tops +# ========================================================================================== +# Adjacency matrices. + +""" + adjacency_matrix(g::Topology, source, edge, target; transpose = false; prune = true) + +Produce a boolean sparse matrix representing the connections of the given edge type, +from the given source node compartment (lines) \ +to the given target node compartment (colums). +Flip dimensions if `transpose` is set. +Lower `prune` to keep lines and columns for the nodes marked as removed. +See [`topology`](@ref). +""" +function adjacency_matrix( + g::Topology, + source::Symbol, + edge::Symbol, + target::Symbol; + transpose = false, + prune = true, +) + # Same, but with stricter input signature. + Topologies.adjacency_matrix(g, source, edge, target; transpose, prune) +end +export adjacency_matrix + +""" + species_adjacency_matrix(g::Topology, edge::Symbol; kwargs...) + +Restriction of [`adjacency_matrix`](@ref) to only `:species` compartments. +""" +function species_adjacency_matrix(g::Topology, edge::Symbol; kwargs...) + adjacency_matrix(g, :species, edge, :species; kwargs...) +end +export species_adjacency_matrix + +""" + foodweb_matrix(g::Topology; kwargs...) + +Restriction of [`species_adjacency_matrix`](@ref) +to only `:species` compartment and `:trophic` links. +""" +foodweb_matrix(g::Topology; kwargs...) = species_adjacency_matrix(g, :trophic; kwargs...) +export foodweb_matrix + + # ========================================================================================== # Common checks to raise useful error messages.