Skip to content

fix: plot array species better #1287

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ext/CatalystGraphMakieExtension.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
38 changes: 26 additions & 12 deletions ext/CatalystGraphMakieExtension/rn_graph_plot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -164,17 +164,18 @@ 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)),
["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])
@show ilabels

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)
Expand All @@ -184,7 +185,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
Expand Down Expand Up @@ -236,7 +237,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)
Expand Down Expand Up @@ -281,22 +282,35 @@ function complexelem_tostr(e::Catalyst.ReactionComplexElement, specstrs)
end
end

"""
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

# Get the strings corresponding to the reaction complexes
function complexlabels(rn::ReactionSystem)
labels = String[]
labels = LaTeXString[]

specstrs = map(s -> String(tosymbol(s, escape=false)), 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
Expand Down
4 changes: 2 additions & 2 deletions test/extensions/graphmakie.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading