-
-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #228 from isaacsas/graph-viz
Generation of graphviz graphs
- Loading branch information
Showing
3 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |