Exporting networks to DOT files
Many software packages designed to analyse and display networks can read files in the DOT format; two examples that
are widely used in bioninformatics are Cytoscape (link) and Gephi
(link). bnlearn can export network structures (that is, bn
objects) as DOT files, making it possible to use advanced layout and display capabilities that are not available from
any R package.
The relevant function for exporting is write.dot()
.
> library(bnlearn) > > dag = random.graph(LETTERS[1:10]) > write.dot(dag, file = "dag.dot")
The DOT files contains only basic information about the network structure: its nodes and its arc set.
> cat(readLines("dag.dot"), sep = "\n")
digraph { "A" ; "B" ; "C" ; "D" ; "E" ; "F" ; "G" ; "H" ; "I" ; "J" ; edge [dir=forward] "A" -> "I" ; edge [dir=forward] "C" -> "D" ; edge [dir=forward] "C" -> "H" ; edge [dir=forward] "D" -> "E" ; edge [dir=forward] "E" -> "J" ; edge [dir=forward] "F" -> "H" ; edge [dir=forward] "F" -> "I" ; edge [dir=forward] "H" -> "I" ; }
If all arcs in the network are undirected, the network is exported as an undirected graph (note that the keyword
is graph
instead of digraph
).
> write.dot(skeleton(dag), file = "ug.dot") > cat(readLines("ug.dot"), sep = "\n")
graph { "A" ; "B" ; "C" ; "D" ; "E" ; "F" ; "G" ; "H" ; "I" ; "J" ; edge [dir=none] "A" -- "I" ; edge [dir=none] "C" -- "D" ; edge [dir=none] "C" -- "H" ; edge [dir=none] "D" -- "E" ; edge [dir=none] "E" -- "J" ; edge [dir=none] "F" -- "H" ; edge [dir=none] "F" -- "I" ; edge [dir=none] "H" -- "I" ; }
Since the DOT format has no specific keyword to denote partially directed graphs, such as those returned by
cpdag()
and by all constraint-based structure learning algorithms, write.dot()
exports
them using the digraph
keyword and removing the direction from the undirected arcs.
> write.dot(set.edge(dag, from = "A", to = "G"), file = "pdag.dot") > cat(readLines("pdag.dot"), sep = "\n")
digraph { "A" ; "B" ; "C" ; "D" ; "E" ; "F" ; "G" ; "H" ; "I" ; "J" ; edge [dir=none] "A" -> "G" ; edge [dir=forward] "A" -> "I" ; edge [dir=forward] "C" -> "D" ; edge [dir=forward] "C" -> "H" ; edge [dir=forward] "D" -> "E" ; edge [dir=forward] "E" -> "J" ; edge [dir=forward] "F" -> "H" ; edge [dir=forward] "F" -> "I" ; edge [dir=forward] "H" -> "I" ; }
Mon Aug 5 02:41:51 2024
with bnlearn
5.0
and R version 4.4.1 (2024-06-14)
.