From 8eeac5dca98901d48a3686d1f1f2ee49669a9db2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Poisot?= Date: Thu, 21 Sep 2023 14:06:45 -0400 Subject: [PATCH 1/7] deps: Graphs.jl --- Project.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/Project.toml b/Project.toml index 669b455..c1956e3 100644 --- a/Project.toml +++ b/Project.toml @@ -9,6 +9,7 @@ Combinatorics = "861a8166-3701-5b0c-9a16-15d98fcdc6aa" DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8" DelimitedFiles = "8bb1440f-4735-579b-a4ab-409b98df4dab" Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f" +Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" Mangal = "b8b640a6-63d9-51e6-b784-5033db27bef2" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" From d338861a97f6853060b81f19f293743f3253d8ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Poisot?= Date: Thu, 21 Sep 2023 14:07:00 -0400 Subject: [PATCH 2/7] feat: Graphs.jl interface start --- src/SpeciesInteractionNetworks.jl | 2 ++ src/interfaces/graphs.jl | 0 2 files changed, 2 insertions(+) create mode 100644 src/interfaces/graphs.jl diff --git a/src/SpeciesInteractionNetworks.jl b/src/SpeciesInteractionNetworks.jl index 97f3f49..b4494bd 100644 --- a/src/SpeciesInteractionNetworks.jl +++ b/src/SpeciesInteractionNetworks.jl @@ -14,6 +14,7 @@ using StatsBase using TestItems import Tables import Mangal +import Graphs # Various utilities for probabilities # include(joinpath(".", "misc", "probabilities.jl")) @@ -45,6 +46,7 @@ include("interfaces/iteration.jl") include("interfaces/table.jl") include("interfaces/broadcast.jl") include("interfaces/linearalgebra.jl") +include("interfaces/graphs.jl") export svd, rank, diag export complexity, tsvd, rdpg diff --git a/src/interfaces/graphs.jl b/src/interfaces/graphs.jl new file mode 100644 index 0000000..e69de29 From 8bae623fca511ba5f4a03801bc2d732e4c884b52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Poisot?= Date: Thu, 21 Sep 2023 14:18:50 -0400 Subject: [PATCH 3/7] feat: reverse/reverse! --- src/interfaces/graphs.jl | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/interfaces/graphs.jl b/src/interfaces/graphs.jl index e69de29..23c197e 100644 --- a/src/interfaces/graphs.jl +++ b/src/interfaces/graphs.jl @@ -0,0 +1,34 @@ +""" + Base.reverse(N::SpeciesInteractionNetwork{<:Partiteness, <:Interactions}) + +Returns a copy of the network, in which the interactions have been flipped. In +other words, an interaction A → B is now B → A. This maintains the nature of the +interaction, and works for loops, self-edges etc. +""" +function Base.reverse(N::SpeciesInteractionNetwork{<:Partiteness, <:Interactions}) + M = copy(N) + reverse!(M) +end + +""" + Base.reverse!(N::SpeciesInteractionNetwork{<:Partiteness, <:Interactions}) + +Modifies the network given as its argument so that the interactions are flipped. +See [`reverese`](@ref) for more information. +""" +function Base.reverse!(N::SpeciesInteractionNetwork{<:Partiteness, <:Interactions}) + for interaction in interactions(N) + N[interaction[2], interaction[1]], N[interaction[1], interaction[2]] = N[interaction[1], interaction[2]], N[interaction[2], interaction[1]] + end + return N +end + +@testitem "We can reverse a network" begin + edges = Binary(Bool[0 1 0 0; 0 0 1 0; 1 0 0 0; 0 1 1 1]) + nodes = Unipartite(edges) + N = SpeciesInteractionNetwork(nodes, edges) + R = reverse(N) + for interaction in interactions(R) + @test N[interaction[2], interaction[1]] == interaction[3] + end +end \ No newline at end of file From ed11af4750dabddedc314f09e83785731f02f639 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Poisot?= Date: Thu, 21 Sep 2023 14:19:16 -0400 Subject: [PATCH 4/7] typo: reverse! --- src/interfaces/graphs.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/interfaces/graphs.jl b/src/interfaces/graphs.jl index 23c197e..50ec126 100644 --- a/src/interfaces/graphs.jl +++ b/src/interfaces/graphs.jl @@ -14,7 +14,7 @@ end Base.reverse!(N::SpeciesInteractionNetwork{<:Partiteness, <:Interactions}) Modifies the network given as its argument so that the interactions are flipped. -See [`reverese`](@ref) for more information. +See [`reverse`](@ref) for more information. """ function Base.reverse!(N::SpeciesInteractionNetwork{<:Partiteness, <:Interactions}) for interaction in interactions(N) From 46436cb9d7f3b61669d629cda761ce6ac12aaaeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Poisot?= Date: Thu, 21 Sep 2023 14:51:09 -0400 Subject: [PATCH 5/7] feat: has_edge/has_vertex --- src/interfaces/graphs.jl | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/interfaces/graphs.jl b/src/interfaces/graphs.jl index 50ec126..65d6b80 100644 --- a/src/interfaces/graphs.jl +++ b/src/interfaces/graphs.jl @@ -31,4 +31,35 @@ end for interaction in interactions(R) @test N[interaction[2], interaction[1]] == interaction[3] end +end + +Graphs.has_vertex(N::T, v) where {T <: SpeciesInteractionNetwork} = v in species(N) + +@testitem "We can check the existence of a vertex" begin + import SpeciesInteractionNetworks.Graphs + edges = Binary(Bool[0 1 0 0; 0 0 1 0; 1 0 0 0; 0 1 1 1]) + nodes = Unipartite(edges) + N = SpeciesInteractionNetwork(nodes, edges) + @test Graphs.has_vertex(N, :node_1) + @test !Graphs.has_vertex(N, :node_1000) +end + +function Graphs.has_edge(N::T, s, d) where {T <: SpeciesInteractionNetwork} + if !(Graphs.has_vertex(N, s) && Graphs.has_vertex(N, d)) + return false + else + return !iszero(N[s,d]) + end +end + +@testitem "We can check the existence of an edge" begin + import SpeciesInteractionNetworks.Graphs + edges = Binary(Bool[0 1 0 0; 0 0 1 0; 1 0 0 0; 0 1 1 1]) + nodes = Unipartite(edges) + N = SpeciesInteractionNetwork(nodes, edges) + @test Graphs.has_edge(N, :node_1, :node_2) + @test !Graphs.has_edge(N, :node_2, :node_1) + @test !Graphs.has_edge(N, :node_2000, :node_1) + @test !Graphs.has_edge(N, :node_2, :node_1000) + @test !Graphs.has_edge(N, :node_20000, :node_1000) end \ No newline at end of file From a5e854adf67f9dd0498e4364518a70cb35fbefa6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Poisot?= Date: Thu, 21 Sep 2023 14:53:27 -0400 Subject: [PATCH 6/7] feat: in/out neighbors --- src/interfaces/graphs.jl | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/interfaces/graphs.jl b/src/interfaces/graphs.jl index 65d6b80..57e8214 100644 --- a/src/interfaces/graphs.jl +++ b/src/interfaces/graphs.jl @@ -62,4 +62,7 @@ end @test !Graphs.has_edge(N, :node_2000, :node_1) @test !Graphs.has_edge(N, :node_2, :node_1000) @test !Graphs.has_edge(N, :node_20000, :node_1000) -end \ No newline at end of file +end + +Graphs.inneighbors(N::T, v) where {T <: SpeciesInteractionNetwork} = predecessors(N, v) +Graphs.outneighbors(N::T, v) where {T <: SpeciesInteractionNetwork} = successors(N, v) \ No newline at end of file From 27b3d2ba68979ec2115c9f958820ef1e1a2b53e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Poisot?= Date: Thu, 21 Sep 2023 14:55:15 -0400 Subject: [PATCH 7/7] feat: edgetype --- src/interfaces/graphs.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/interfaces/graphs.jl b/src/interfaces/graphs.jl index 57e8214..02e0906 100644 --- a/src/interfaces/graphs.jl +++ b/src/interfaces/graphs.jl @@ -65,4 +65,6 @@ end end Graphs.inneighbors(N::T, v) where {T <: SpeciesInteractionNetwork} = predecessors(N, v) -Graphs.outneighbors(N::T, v) where {T <: SpeciesInteractionNetwork} = successors(N, v) \ No newline at end of file +Graphs.outneighbors(N::T, v) where {T <: SpeciesInteractionNetwork} = successors(N, v) + +Graphs.edgetype(N::T) where {T <: SpeciesInteractionNetwork} = eltype(N) \ No newline at end of file