From adcf9f47d45c64edf1742c9153979e16181ccafb Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 20 Jun 2025 16:53:22 -0400 Subject: [PATCH 1/4] fix: plot array species names better --- ext/CatalystGraphMakieExtension/rn_graph_plot.jl | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/ext/CatalystGraphMakieExtension/rn_graph_plot.jl b/ext/CatalystGraphMakieExtension/rn_graph_plot.jl index be83f6f8e6..26cfe25467 100644 --- a/ext/CatalystGraphMakieExtension/rn_graph_plot.jl +++ b/ext/CatalystGraphMakieExtension/rn_graph_plot.jl @@ -164,7 +164,7 @@ function Catalyst.plot_network(rn::ReactionSystem; kwargs...) ns = length(species(rn)) nodecolors = vcat([:skyblue3 for i in 1:ns], [:green for i in ns+1:nv(srg)]) - ilabels = vcat(map(s -> String(tosymbol(s, escape=false)), species(rn)), + ilabels = vcat(map(spectostr, species(rn)), ["R$i" for i in 1:nv(srg)-ns]) ssm = substoichmat(rn) @@ -273,6 +273,17 @@ function Catalyst.plot_complexes(rn::ReactionSystem; show_rate_labels = false, k f end +function spectostr(spec) + spec = Symbolics.value(spec) + if iscall(spec) && operation(spec) == getindex + (spec, idxs...) = arguments(spec) + idxs = join(idxs, ", ") + String(tosymbol(spec, escape = false))*"["*idxs*"]" + else + String(tosymbol(spec, escape = false)) + end +end + function complexelem_tostr(e::Catalyst.ReactionComplexElement, specstrs) if e.speciesstoich == 1 return "$(specstrs[e.speciesid])" @@ -285,7 +296,7 @@ end function complexlabels(rn::ReactionSystem) labels = String[] - specstrs = map(s -> String(tosymbol(s, escape=false)), species(rn)) + specstrs = map(spectostr, species(rn)) complexes, B = reactioncomplexes(rn) for complex in complexes From 4cf9fa432ab8887702c378a611eb7db626cf4060 Mon Sep 17 00:00:00 2001 From: vyudu Date: Thu, 26 Jun 2025 17:36:24 -0400 Subject: [PATCH 2/4] fix: use LatexStrings for species display --- ext/CatalystGraphMakieExtension.jl | 1 + .../rn_graph_plot.jl | 34 +++++++++---------- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/ext/CatalystGraphMakieExtension.jl b/ext/CatalystGraphMakieExtension.jl index 0a63fa1415..75d40839db 100644 --- a/ext/CatalystGraphMakieExtension.jl +++ b/ext/CatalystGraphMakieExtension.jl @@ -3,6 +3,7 @@ module CatalystGraphMakieExtension # Fetch packages. using Catalyst, GraphMakie, Graphs, Symbolics, SparseArrays, NetworkLayout, Makie using Symbolics: get_variables! +using Latexify, LaTeXStrings import Catalyst: species_reaction_graph, incidencematgraph, lattice_plot, lattice_animation # Creates and exports graph plotting functions. diff --git a/ext/CatalystGraphMakieExtension/rn_graph_plot.jl b/ext/CatalystGraphMakieExtension/rn_graph_plot.jl index 26cfe25467..e3531740aa 100644 --- a/ext/CatalystGraphMakieExtension/rn_graph_plot.jl +++ b/ext/CatalystGraphMakieExtension/rn_graph_plot.jl @@ -164,8 +164,8 @@ function Catalyst.plot_network(rn::ReactionSystem; kwargs...) ns = length(species(rn)) nodecolors = vcat([:skyblue3 for i in 1:ns], [:green for i in ns+1:nv(srg)]) - ilabels = vcat(map(spectostr, species(rn)), - ["R$i" for i in 1:nv(srg)-ns]) + ilabels = vcat(map(latexify_no_t, species(rn)), + [latexify("R_$i", env = :inline) for i in 1:nv(srg)-ns]) ssm = substoichmat(rn) psm = prodstoichmat(rn) @@ -236,7 +236,7 @@ function Catalyst.plot_complexes(rn::ReactionSystem; show_rate_labels = false, k rxs = reactions(rn) specs = species(rn) edgecolors = [:black for i in 1:length(rxs)] - edgelabels = [repr(rx.rate) for rx in rxs] + edgelabels = [latexify(rx.rate, env=:inline) for rx in rxs] deps = Set() for (i, rx) in enumerate(rxs) @@ -273,17 +273,6 @@ function Catalyst.plot_complexes(rn::ReactionSystem; show_rate_labels = false, k f end -function spectostr(spec) - spec = Symbolics.value(spec) - if iscall(spec) && operation(spec) == getindex - (spec, idxs...) = arguments(spec) - idxs = join(idxs, ", ") - String(tosymbol(spec, escape = false))*"["*idxs*"]" - else - String(tosymbol(spec, escape = false)) - end -end - function complexelem_tostr(e::Catalyst.ReactionComplexElement, specstrs) if e.speciesstoich == 1 return "$(specstrs[e.speciesid])" @@ -292,22 +281,31 @@ function complexelem_tostr(e::Catalyst.ReactionComplexElement, specstrs) end end +""" +Helper to remove the (t) from the Latex string. +""" +function latexify_no_t(s) + str = latexify(s, env = :raw).s + sub = split(str, "\\left(")[1] + return LaTeXString(sub) +end + # Get the strings corresponding to the reaction complexes function complexlabels(rn::ReactionSystem) - labels = String[] + labels = LaTeXString[] - specstrs = map(spectostr, species(rn)) + specstrs = map(latexify_no_t, species(rn)) complexes, B = reactioncomplexes(rn) for complex in complexes if isempty(complex) - push!(labels, "∅") + push!(labels, L"∅") elseif length(complex) == 1 push!(labels, complexelem_tostr(complex[1], specstrs)) else elems = map(c -> complexelem_tostr(c, specstrs), complex) str = reduce((e1, e2) -> *(e1, " + ", e2), @view elems[2:end]; init = elems[1]) - push!(labels, str) + push!(labels, LaTeXString(str)) end end labels From 0c17ae16ad3518adc17a48362896f6b5f7da8b36 Mon Sep 17 00:00:00 2001 From: vyudu Date: Thu, 26 Jun 2025 17:47:18 -0400 Subject: [PATCH 3/4] fix: use regular strings for empty edge labels --- ext/CatalystGraphMakieExtension/rn_graph_plot.jl | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/ext/CatalystGraphMakieExtension/rn_graph_plot.jl b/ext/CatalystGraphMakieExtension/rn_graph_plot.jl index e3531740aa..8de56fc99a 100644 --- a/ext/CatalystGraphMakieExtension/rn_graph_plot.jl +++ b/ext/CatalystGraphMakieExtension/rn_graph_plot.jl @@ -170,11 +170,11 @@ function Catalyst.plot_network(rn::ReactionSystem; kwargs...) ssm = substoichmat(rn) psm = prodstoichmat(rn) # Get stoichiometry of reaction - edgelabels = map(Graphs.edges(srg.g)) do e - string(src(e) > ns ? + edgelabels = Vector{Any}(map(Graphs.edges(srg.g)) do e + LaTeXString(string(src(e) > ns ? psm[dst(e), src(e)-ns] : - ssm[src(e), dst(e)-ns]) - end + ssm[src(e), dst(e)-ns])) + end) edgecolors = [:black for i in 1:ne(srg)] num_e = ne(srg.g) @@ -184,7 +184,7 @@ function Catalyst.plot_network(rn::ReactionSystem; kwargs...) if srg.edgeorder[i] > num_e edgecolors[i] = :red insert!(edgelabels, i, "") - elseif edgelabels[i] == "0" + elseif edgelabels[i].s == "0" edgecolors[i] = :red edgelabels[i] = "" end From 10bff9ce6623196c8a7221bd1ff5db3242a43ae2 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 7 Jul 2025 22:54:38 -0400 Subject: [PATCH 4/4] fix; fix handling for multi character symbol names --- ext/CatalystGraphMakieExtension/rn_graph_plot.jl | 7 ++++++- test/extensions/graphmakie.jl | 4 ++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/ext/CatalystGraphMakieExtension/rn_graph_plot.jl b/ext/CatalystGraphMakieExtension/rn_graph_plot.jl index 8de56fc99a..239cf91af1 100644 --- a/ext/CatalystGraphMakieExtension/rn_graph_plot.jl +++ b/ext/CatalystGraphMakieExtension/rn_graph_plot.jl @@ -166,6 +166,7 @@ function Catalyst.plot_network(rn::ReactionSystem; kwargs...) [:green for i in ns+1:nv(srg)]) ilabels = vcat(map(latexify_no_t, species(rn)), [latexify("R_$i", env = :inline) for i in 1:nv(srg)-ns]) + @show ilabels ssm = substoichmat(rn) psm = prodstoichmat(rn) @@ -282,10 +283,14 @@ function complexelem_tostr(e::Catalyst.ReactionComplexElement, specstrs) end """ -Helper to remove the (t) from the Latex string. +Helper to remove the (t) and mathtt environment from the Latex string. """ function latexify_no_t(s) str = latexify(s, env = :raw).s + @show str + str = replace(str, r"\\mathrm\{(.*?)\}" => s"\1") + str = replace(str, r"\\mathtt\{(.*?)\}" => s"\1") + @show str sub = split(str, "\\left(")[1] return LaTeXString(sub) end diff --git a/test/extensions/graphmakie.jl b/test/extensions/graphmakie.jl index 0c1cc29d5e..4112efe33b 100644 --- a/test/extensions/graphmakie.jl +++ b/test/extensions/graphmakie.jl @@ -26,8 +26,8 @@ let k₃, KKKE1 --> KKK_ + E1 (k₄, k₅), KKK_ + E2 <--> KKKE2 k₆, KKKE2 --> KKK + E2 - (k₇, k₈), KK + KKK_ <--> KK_KKK_ - k₉, KK_KKK_ --> KKP + KKK_ + (k₇, k₈), KK + KKK_ <--> KK_KKK + k₉, KK_KKK --> KKP + KKK_ (k₁₀, k₁₁), KKP + KKK_ <--> KKPKKK_ k₁₂, KKPKKK_ --> KKPP + KKK_ (k₁₃, k₁₄), KKP + KKPase <--> KKPKKPase