Skip to content

Commit

Permalink
Merge pull request #228 from isaacsas/graph-viz
Browse files Browse the repository at this point in the history
Generation of graphviz graphs
  • Loading branch information
ChrisRackauckas committed Jul 21, 2020
2 parents 9e6cc07 + a187b22 commit 196eb9e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ uuid = "479239e8-5488-4da2-87a7-35f2df7eef83"
version = "5.0.1"

[deps]
Catlab = "134e5e36-593f-5add-ad60-77f754baafbe"
Latexify = "23fbe1c1-3f47-55db-b15f-69d7ec21a316"
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78"
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"

[compat]
Catlab = "0.7.2"
Latexify = "0.13.5"
MacroTools = "0.5"
ModelingToolkit = "3.14"
Expand Down
7 changes: 7 additions & 0 deletions src/Catalyst.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,11 @@ export dependants, dependents
# for Latex printing of ReactionSystems
include("latexify_recipes.jl")

# for making and saving graphs
import Base.Iterators: flatten
using Catlab.Graphics.Graphviz
import Catlab.Graphics.Graphviz: Graph, Edge
include("graphs.jl")
export savegraph

end # module
45 changes: 45 additions & 0 deletions src/graphs.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# adapted from Petri.jl
# https://github.com/mehalter/Petri.jl

graph_attrs = Attributes(:rankdir=>"LR")
node_attrs = Attributes(:shape=>"plain", :style=>"filled", :color=>"white")
edge_attrs = Attributes(:splines=>"splines")

function edgify(δ, i, reverse::Bool)
attr = Attributes()
return map(δ) do p
val = String(p[1].op.name)
weight = "$(p[2])"
attr = Attributes(:label=>weight, :labelfontsize=>"6")
return Edge(reverse ? ["rx_$i", "$val"] :
["$val", "rx_$i"], attr)
end
end

"""
Graph(model::Model)
convert a Model into a GraphViz Graph. Transition are green boxes and states are blue circles. Arrows go from the input states to the output states for each transition.
"""
function Graph(model::ReactionSystem)
rxs = reactions(model)
statenodes = [Node(string(s.name), Attributes(:shape=>"circle", :color=>"#6C9AC3")) for s in species(model)]
transnodes = [Node(string("rx_$i"), Attributes(:shape=>"point", :color=>"#E28F41", :width=>".1")) for (i,r) in enumerate(rxs)]

stmts = vcat(statenodes, transnodes)
edges = map(enumerate(rxs)) do (i,r)
vcat(edgify(zip(r.substrates,r.substoich), i, false),
edgify(zip(r.products,r.prodstoich), i, true))
end |> flatten |> collect
stmts = vcat(stmts, edges)
g = Graphviz.Graph("G", true, stmts, graph_attrs, node_attrs,edge_attrs)
return g
end


function savegraph(g::Graph, fname, fmt="png")
open(fname, "w") do io
run_graphviz(io, g, format=fmt)
end
nothing
end

0 comments on commit 196eb9e

Please sign in to comment.