Title: | graph: A package to handle graph data structures |
---|---|
Description: | A package that implements some simple graph handling capabilities. |
Authors: | R Gentleman [aut], Elizabeth Whalen [aut], W Huber [aut], S Falcon [aut], Jeff Gentry [aut], Paul Shannon [aut], Halimat C. Atanda [ctb] (Converted 'MultiGraphClass' and 'GraphClass' vignettes from Sweave to RMarkdown / HTML.), Paul Villafuerte [ctb] (Converted vignettes from Sweave to RMarkdown / HTML.), Aliyu Atiku Mustapha [ctb] (Converted 'Graph' vignette from Sweave to RMarkdown / HTML.), Bioconductor Package Maintainer [cre] |
Maintainer: | Bioconductor Package Maintainer <[email protected]> |
License: | Artistic-2.0 |
Version: | 1.85.0 |
Built: | 2024-11-29 06:25:11 UTC |
Source: | https://github.com/bioc/graph |
This generic function takes an object that inherits from the graph
class and a node in that graph and returns a vector containing information
about all other nodes that are accessible from the given node. The
methods are vectorized so that index
can be a vector.
## S4 method for signature 'graph,character' acc(object, index) ## S4 method for signature 'clusterGraph,character' acc(object, index)
## S4 method for signature 'graph,character' acc(object, index) ## S4 method for signature 'clusterGraph,character' acc(object, index)
object |
An instance of the appropriate graph class. |
index |
A character vector specifying the nodes for which accessibilty information is wanted. |
The methods should return a named list of integer vectors. The
names
of the list correspond to the names of the supplied
nodes. For each element of the list the returned vector is named. The
names of the vector elements correspond to the nodes that are
accessible from the given node. The values in the vector indicate how
many edges are between the given node and the node in the return vector.
An object of class graph.
An instance of the clusterGraph
class.
A character
vector of indices corresponding to nodes in the
graph.
set.seed(123) gR3 <- randomGraph(LETTERS[1:10], M<-1:2, p=.5) acc(gR3, "A") acc(gR3, c("B", "D"))
set.seed(123) gR3 <- randomGraph(LETTERS[1:10], M<-1:2, p=.5) acc(gR3, "A") acc(gR3, c("B", "D"))
A function to add an edge to a graph.
addEdge(from, to, graph, weights)
addEdge(from, to, graph, weights)
from |
The node the edge starts at |
to |
The node the edge goes to. |
graph |
The graph that the edge is being added to. |
weights |
A vector of weights, one for each edge. |
Both from
and to
can be vectors. They need not be the
same length (if not the standard rules for replicating the shorter one
are used). Edges are added to the graph between the supplied nodes.
The weights
are given for each edge.
The implementation is a bit too oriented towards the graphNEL
class and will likely change in the next release to accomodate more
general graph classes.
If the graph is undirected then the edge is bidirectional (and only needs to be added once). For directed graphs the edge is directional.
A new instance of a graph object with the same class as graph
but with the indicated edges added.
R. Gentleman
addNode
,removeEdge
,
removeNode
V <- LETTERS[1:4] edL2 <- vector("list", length=4) names(edL2) <- V for(i in 1:4) edL2[[i]] <- list(edges=c(2,1,2,1)[i], weights=sqrt(i)) gR2 <- graphNEL(nodes=V, edgeL=edL2, edgemode="directed") gX <- addEdge("A", "C", gR2, 1) gR3 <- randomEGraph(letters[10:14], .4) gY <- addEdge("n", "l", gR3, 1)
V <- LETTERS[1:4] edL2 <- vector("list", length=4) names(edL2) <- V for(i in 1:4) edL2[[i]] <- list(edges=c(2,1,2,1)[i], weights=sqrt(i)) gR2 <- graphNEL(nodes=V, edgeL=edL2, edgemode="directed") gX <- addEdge("A", "C", gR2, 1) gR3 <- randomEGraph(letters[10:14], .4) gY <- addEdge("n", "l", gR3, 1)
Add one or more nodes to a graph.
addNode(node, object, edges)
addNode(node, object, edges)
node |
A character vector of node names. |
object |
A |
edges |
A named list of edges. |
The supplied node
s are added to the set of nodes of the
object
.
If edges
are provided then their must be the
same number as there are node
s and the must be in the same
order. The elements of the edges
list are vectors. They can be
character vectors of node labels for nodes in object
and if so
then they are added with unit weights. If the vector is numeric then
it must be named (with labels corresponding to nodes in the
object
) and the values are taken to be the edge weights.
When the object
is a distGraph
then the edges
must
be supplied and they must contain appropriate distances for all nodes
both those in object
and those supplied.
A new graph of the same class as object
with the supplied node
added to the set of nodes.
R. Gentleman
removeNode
, removeEdge
,
addEdge
V <- LETTERS[1:4] edL1 <- vector("list", length=4) names(edL1) <- V for(i in 1:4) edL1[[i]] <- list(edges=c(2,1,4,3)[i], weights=sqrt(i)) gR <- graphNEL(nodes=V, edgeL=edL1) gX <- addNode("X", gR) set.seed(123) g1 <- randomGraph(letters[1:10], 1:4, p=.3) g2 <- addNode("z", g1, edges=list(c("a", "h", "g")))
V <- LETTERS[1:4] edL1 <- vector("list", length=4) names(edL1) <- V for(i in 1:4) edL1[[i]] <- list(edges=c(2,1,4,3)[i], weights=sqrt(i)) gR <- graphNEL(nodes=V, edgeL=edL1) gX <- addNode("X", gR) set.seed(123) g1 <- randomGraph(letters[1:10], 1:4, p=.3) g2 <- addNode("z", g1, edges=list(c("a", "h", "g")))
This generic function takes an object that inherits from the graph
class and a node in that graph and returns a vector containing information
about all other nodes that are adjacent to the given node.
This means that they are joined to the given node by an edge.
The accessibility list, acc
is the list of all nodes that can
be reached from a specified node.
The methods return vector of nodes that are adjacent to the specified node.
An object that inherits from glass graph
An index (could be multiple) which can be either the integer offset for the node(s) or their labels.
set.seed(123) gR3 <- randomGraph(LETTERS[1:4], M<-1:2, p=.5) adj(gR3, "A") adj(gR3, c(2,3))
set.seed(123) gR3 <- randomGraph(LETTERS[1:4], M<-1:2, p=.5) adj(gR3, "A") adj(gR3, c(2,3))
Though unwieldy for large matrices, a full adjacency matrix can be useful for debugging and export.
If the graph is “undirected” then recicprocal edges are explicit in the matrix.
adjacencyMatrix(object)
adjacencyMatrix(object)
object |
A |
Thus far only implemented for graphBAM
objects.
adjacencyMatrix
returns an n x n matrix, where n is
the number of nodes in the graph, ordered in the same manner as
seen in the nodes
method. All cells in the matrix are 0
except where edges are found.
P. Shannon
from <- c("a", "a", "a", "x", "x", "c") to <- c("b", "c", "x", "y", "c", "a") weight <- c(3.4, 2.6, 1.7, 5.3, 1.6, 7.9) df <- data.frame(from, to, weight, stringsAsFactors = TRUE) g1 <- graphBAM(df, edgemode = "directed") adjacencyMatrix(g1)
from <- c("a", "a", "a", "x", "x", "c") to <- c("b", "c", "x", "y", "c", "a") weight <- c(3.4, 2.6, 1.7, 5.3, 1.6, 7.9) df <- data.frame(from, to, weight, stringsAsFactors = TRUE) g1 <- graphBAM(df, edgemode = "directed") adjacencyMatrix(g1)
A graph representing the apoptosis pathway from
KEGG, as well as a data.frame of attributes for use in plotting the
graph with Rgraphviz
and a list to compare the nodes with their
respective LocusLink IDs.
data(apopGraph)
data(apopGraph)
The apopGraph
data set contains three objects:
The first is apopGraph
, which is an object of class
graph-NEL
and represents the hsa04210 graph from KEGG
.
The second is apopAttrs
, which is a data.frame with two columns,
and a row for every node in apopGraph
. The first column lists
what color the node is represented with on the KEGG
site. The
second column lists the type of the node - either genesym
or
text
. Most nodes are of type genesym
as they represent
genes, but some of the nodes in the KEGG
graph were not genes
and thus those nodes are of type text
.
The third, apopLocusLink
is a named list where the names
correspond to the node names in apopGraph
. The values of the
list are the LocusLink IDs that correspond to that node in the KEGG graph.
http://www.genome.ad.jp/kegg/pathway/hsa/hsa04210.html
data(apopGraph) if (require("Rgraphviz") & interactive()) plot(apopGraph)
data(apopGraph) if (require("Rgraphviz") & interactive()) plot(apopGraph)
A container class to manage generic attributes. Supports named attributes with default values with methods for vectorized access.
Objects can be created by calls of the form new("attrData", defaults)
.
The defaults
argument should be a named list containing the
initial attribute names and default values.
data
:Where custom attribute data is stored
defaults
:A named list of known attribute names and defualt values.
signature(self = "attrData", x = "character", attr = "character")
: ...
signature(self = "attrData", x = "character", attr = "missing")
: ...
signature(self = "attrData", x = "character", attr = "character")
: ...
signature(self = "attrData", attr = "character", value = "ANY")
: ...
signature(self = "attrData", attr = "missing", value = "list")
: ...
signature(self = "attrData", attr = "missing")
: ...
signature(self = "attrData", attr = "character")
: ...
signature(.Object = "attrData")
: ...
return the names of the stored attributes
set the names of the stored attributes
signature(self="attrData",
x="character", value="NULL")
: Remove the data associated with
the key specified by x
.
Seth Falcon
defaultProps <- list(weight=1, color="blue", friends=c("Bob", "Alice")) adat <- new("attrData", defaults=defaultProps) ## Get all defaults attrDefaults(adat) ## Or get only a specific attribute attrDefaults(adat, attr="color") ## Update default weight attrDefaults(adat, attr="weight") <- 500 ## Add new attribute attrDefaults(adat, attr="length") <- 0 ## Asking for the attributes of an element you haven't customized ## returns the defaults attrDataItem(adat, x=c("n1", "n2"), attr="length") ## You can customize values attrDataItem(adat, x=c("n1", "n2"), attr="length") <- 5 ## What keys have been customized? names(adat)
defaultProps <- list(weight=1, color="blue", friends=c("Bob", "Alice")) adat <- new("attrData", defaults=defaultProps) ## Get all defaults attrDefaults(adat) ## Or get only a specific attribute attrDefaults(adat, attr="color") ## Update default weight attrDefaults(adat, attr="weight") <- 500 ## Add new attribute attrDefaults(adat, attr="length") <- 0 ## Asking for the attributes of an element you haven't customized ## returns the defaults attrDataItem(adat, x=c("n1", "n2"), attr="length") ## You can customize values attrDataItem(adat, x=c("n1", "n2"), attr="length") <- 5 ## What keys have been customized? names(adat)
The attrDataItem
method provides get/set access to items stored
in a attrData-class
object.
attrDataItem(self, x, attr) attrDataItem(self, x, attr) <- value
attrDataItem(self, x, attr) attrDataItem(self, x, attr) <- value
self |
A |
x |
A |
attr |
A |
value |
An R object to set as the attribute value for the
specified items. If the object has length one or does not have a
length method defined, it will be assigned to all items in |
The attrDefaults
method provides access to a
attrData-class
object's default attribute list. The
default attribute list of a attrData-class
object defines what
attributes can be customized for individual data elements by
defining attribute names and default values.
attrDefaults(self, attr) attrDefaults(self, attr) <- value
attrDefaults(self, attr) attrDefaults(self, attr) <- value
self |
A |
attr |
A |
value |
An R object that will be used as the default value of the
specified attribute, or a named list of attribute name/default value
pairs if |
aveNumEdges divides the number of edges in the graph by the number of nodes to give the average number of edges.
aveNumEdges(objgraph)
aveNumEdges(objgraph)
objgraph |
the graph object |
A double representing the average number of edges will be returned.
Elizabeth Whalen
numEdges
, mostEdges
,
numNoEdges
set.seed(124) g1 <- randomGraph(1:10, letters[7:12], p=.6) aveNumEdges(g1)
set.seed(124) g1 <- randomGraph(1:10, letters[7:12], p=.6) aveNumEdges(g1)
This graph is a rendition of the Bioconductor package repository and represents the dependency graph of that repository. An edge between two package denotes a dependency on the 'to' package by the 'from' package.
data(biocRepos)
data(biocRepos)
data(biocRepos) ## An example of usage will be here soon
data(biocRepos) ## An example of usage will be here soon
The boundary of a subgraph is the set of nodes in the original graph
that have edges to nodes in the subgraph. The function boundary
computes the boundary and returns it as a list whose length is the same
length as the number of nodes in the subgraph.
boundary(subgraph, graph)
boundary(subgraph, graph)
graph |
the original graph from which the boundary will be created |
subgraph |
can either be the vector of the node labels or the subgraph itself. |
The boundary of a subgraph is the set of nodes in the graph which have an edge that connects them to the specified subgraph but which are themselves not elements of the subgraph.
For convenience users can specify the subgraph as either a graph or a vector of node labels.
This function returns a named list of length equal to the number of
nodes in subgraph
. The elements of the list
correspond to the nodes in the subgraph
. The elements are lists
of the nodes in graph
which share an edge with the
respective node in subgraph
.
Elizabeth Whalen and R. Gentleman
set.seed(123) g1 <- randomGraph(letters[1:10], 1:4, p=.3) ##both should be "a" boundary(c("g", "i"), g1)
set.seed(123) g1 <- randomGraph(letters[1:10], 1:4, p=.3) ##both should be "a" boundary(c("g", "i"), g1)
calcProb
calculates the probability of having the number of edges
found in the subgraph given that it was made from origgraph
.
The hypergeometric distribution is used to calculate the
probability (using the pdf).
calcProb(subgraph, origgraph)
calcProb(subgraph, origgraph)
subgraph |
subgraph made from the original graph |
origgraph |
original graph object from which the subgraph was made |
The probability of the subgraph's number of edges is returned.
Elizabeth Whalen
#none right now
#none right now
For any graph a set of nodes can be used to obtain an induced subgraph
(see subGraph
). An interesting question is whether that
subgraph has an unusually large number of edges. This function
computes the probability that a random subgraph with the same
number of nodes has more edges than the number observed in the
presented subgraph. The appropriate probability distribution is
the hypergeometric.
calcSumProb(sg, g)
calcSumProb(sg, g)
sg |
subgraph made from the original graph |
g |
original graph object from which the subgraph was made |
The computation is based on the following argument. In the original
graph there are nodes and hence
edges in the
complete graph. If we consider these
nodes to be of two types,
corresponding to those that are either in our graph,
g
, or not in
it. Then we think of the subgraph which has say nodes and
possible edges as representing
draws from an
urn containing
balls of which some are white (those in
g
)
and some are black. We count the number of edges in the subgraph and use
a Hypergeomtric distribution to ask whether our subgraph is particularly
dense.
The probability of having greater than or equal to the subgraph's number of edges is returned.
Elizabeth Whalen
set.seed(123) V <- letters[14:22] g1 <- randomEGraph(V, .2) sg1 <- subGraph(letters[c(15,17,20,21,22)], g1) calcSumProb(sg1, g1)
set.seed(123) V <- letters[14:22] g1 <- randomEGraph(V, .2) sg1 <- subGraph(letters[c(15,17,20,21,22)], g1) calcSumProb(sg1, g1)
This function removes all edges to or from the specified node in the graph.
clearNode(node, object)
clearNode(node, object)
node |
a node |
object |
a |
All edges to and from node
are removed. node
can be a
vector.
A new instance of the graph with all edges to and from the specified node(s) removed.
R. Gentleman
V <- LETTERS[1:4] edL3 <- vector("list", length=4) for(i in 1:4) edL3[[i]] <- list(edges=(i%%4)+1, weights=i) names(edL3) <- V gR3 <- graphNEL(nodes=V, edgeL=edL3, "directed") g4 <- clearNode("A", gR3)
V <- LETTERS[1:4] edL3 <- vector("list", length=4) for(i in 1:4) edL3[[i]] <- list(edges=(i%%4)+1, weights=i) names(edL3) <- V gR3 <- graphNEL(nodes=V, edgeL=edL3, "directed") g4 <- clearNode("A", gR3)
A cluster graph is a special sort of graph for clustered data. Each cluster forms a completely connected subgraph. Three are no edges between clusters.
Objects can be created by calls of the form new("clusterGraph", ...)
.
clusters
:Object of class "list"
a list of the
labels of the elements, one element of the list for each cluster.
Class "graph"
, directly.
signature(object = "clusterGraph")
: find the
connected components; simply the clusters in this case.
signature(object = "clusterGraph")
: find the
accessible nodes from the supplied node.
signature(object = "clusterGraph")
: find the
adjacent nodes to the supplied node.
signature(object = "clusterGraph")
: return the
nodes.
signature(object="clusterGraph", value="character")
:
replace the node names with the new labels given in value
.
signature(object = "clusterGraph")
: return
the number of nodes.
Return a list of edge weights in a list format
similar to the edges
method.
signature(graph = "clusterGraph")
: A method for
obtaining the edge list.
signature(from = "clusterGraph", to =
"matrix")
: Convert the clusterGraph
to an adjacency
matrix. Currently, weights are ignored. The conversion assumes
no self-loops.
R. Gentleman
cG1 <- new("clusterGraph", clusters=list(a=c(1,2,3), b=c(4,5,6))) cG1 acc(cG1, c("1", "2"))
cG1 <- new("clusterGraph", clusters=list(a=c(1,2,3), b=c(4,5,6))) cG1 acc(cG1, c("1", "2"))
This generic function takes an object that inherits from the graph
class. The graph needs to have edgemode=="undirected"
. If it has
edgemode=="directed"
, the function will return NULL.
## S4 method for signature 'graph' clusteringCoefficient(object, selfLoops=FALSE)
## S4 method for signature 'graph' clusteringCoefficient(object, selfLoops=FALSE)
object |
An instance of the appropriate graph class. |
selfLoops |
Logical. If true, the calculation takes self loops into account. |
For a node with n adjacent nodes, if selfLoops
is
FALSE
, the clustering coefficent is
N/(n*(n-1)), where N is the number of edges between these nodes.
The graph may not have self loops.
If selfLoops
is TRUE
, the clustering coefficent is
N/(n*n), where N is the number of edges between these nodes,
including self loops.
A named numeric vector with the clustering coefficients for each node. For nodes with 2 or more edges, the values are between 0 and 1. For nodes that have no edges, the function returns the value NA. For nodes that have exactly one edge, the function returns NaN.
Wolfgang Huber http://www.dkfz.de/mga/whuber
set.seed(123) g1 <- randomGraph(letters[1:10], 1:4, p=.3) clusteringCoefficient(g1) clusteringCoefficient(g1, selfLoops=TRUE)
set.seed(123) g1 <- randomGraph(letters[1:10], 1:4, p=.3) clusteringCoefficient(g1) clusteringCoefficient(g1, selfLoops=TRUE)
A collection of functions and methods to convert various forms of matrices into graph objects.
aM2bpG(aM) ftM2adjM(ft, W=NULL, V=NULL, edgemode="directed") ftM2graphNEL(ft, W=NULL, V=NULL, edgemode="directed") ## S4 method for signature 'graphNEL,matrix' coerce(from,to="matrix",strict=TRUE) ## S4 method for signature 'matrix,graphNEL' coerce(from,to="graphNEL",strict=TRUE)
aM2bpG(aM) ftM2adjM(ft, W=NULL, V=NULL, edgemode="directed") ftM2graphNEL(ft, W=NULL, V=NULL, edgemode="directed") ## S4 method for signature 'graphNEL,matrix' coerce(from,to="matrix",strict=TRUE) ## S4 method for signature 'matrix,graphNEL' coerce(from,to="graphNEL",strict=TRUE)
ft |
An nx2 matrix containing the |
W |
An optional vector of edge weights. |
V |
An optional vector of node names. |
aM |
An affiliation matrix for a bipartite graph. |
edgemode |
Character. Specifies if the resulting graph is to be directed or undirected. |
from |
Object to coerce from, either of type |
to |
Character giving class to coerce to. Either "matrix" or "graphNEL". |
strict |
Strict object checking. |
In the functions ftM2adjM
and ftM2graphNEL
, a
from/to
matrix ft
is converted into an adjacency
matrix or a graphNEL
object respectively. In ft
,
the first column represents the from
nodes and the
second column the to
nodes.
To have unconnected nodes, use the V
argument (see below). The
edgemode
parameter can be used to specify if the desired output
is a directed or undirected graph.
The same edge must not occur twice in the from/to
matrix.
If edgemode
is undirected
, the edge (u,v)
and
(v,u)
must only be specified once.
W
is an optional vector of edge weights. The order of the edge
weights in the vector should correspond to the order of the edges
recorded in ft
. If it is not specified, edge weights of 1 are
assigned by default.
V
is an optional vector of node names. All elements of ft
must be contained in V
, but not all names in V
need to be
contained in ft
. If V
is not specified, it is set to all
nodes represented in ft
. Specifying V
is most useful for
creating a graph that includes nodes with degree 0.
aM
is an affiliation matrix as frequently used in social networks
analysis. The rows of aM
represent actors, and the columns
represent events. An entry of "1" in the ith row and jth column
represents affiliation of the ith actor with the jth event. Weighted
entries may also be used. aM2bpG
returns a graphNEL
object with
nodes consisting of the set of actors and events, and directed (possibly
weighted) edges from the actors to their corresponding events. If
plotted using Rgraphviz
and the dot
layout, the bipartite structure of
the graph returned by aM2bpG
should be evident.
An adjacency
matrix can be coerced into a graphNEL
using
the as
method. If the matrix is a symmetric matrix, then the
resulting graph will be undirected
, otherwise it will be
directed
.
For ftM2graphNEL
and aM2bpG
, an object of class
graphNEL
.
For ftM2adjM
, a matrix (the adjacency matrix representation).
Denise Scholtens, Wolfgang Huber
## From-To matrix From <- c("A","A","C","C") To <- c("B","C","B","D") L <- cbind(From,To) W <- 1:4 M1 <- ftM2adjM(L, W, edgemode="directed") M2 <- ftM2adjM(L, W, edgemode="undirected") stopifnot(all(M1+t(M1)==M2)) G1 <- ftM2graphNEL(L, W, edgemode="directed") G2 <- ftM2graphNEL(L, W, edgemode="undirected") ## Adjacency matrix From <- matrix(runif(100), nrow=10, ncol=10) From <- (From+t(From)) > pi/4 rownames(From) <- colnames(From) <- LETTERS[1:10] To <- as(From,"graphNEL") Back <- as(To,"matrix") stopifnot(all(From == Back))
## From-To matrix From <- c("A","A","C","C") To <- c("B","C","B","D") L <- cbind(From,To) W <- 1:4 M1 <- ftM2adjM(L, W, edgemode="directed") M2 <- ftM2adjM(L, W, edgemode="undirected") stopifnot(all(M1+t(M1)==M2)) G1 <- ftM2graphNEL(L, W, edgemode="directed") G2 <- ftM2graphNEL(L, W, edgemode="undirected") ## Adjacency matrix From <- matrix(runif(100), nrow=10, ncol=10) From <- (From+t(From)) > pi/4 rownames(From) <- colnames(From) <- LETTERS[1:10] To <- as(From,"graphNEL") Back <- as(To,"matrix") stopifnot(all(From == Back))
A function to combine, or collapse, a specified set of nodes in a graph.
combineNodes(nodes, graph, newName, ...) ## S4 method for signature 'character,graphNEL,character' combineNodes(nodes, graph, newName, collapseFunction=sum)
combineNodes(nodes, graph, newName, ...) ## S4 method for signature 'character,graphNEL,character' combineNodes(nodes, graph, newName, collapseFunction=sum)
nodes |
A set of nodes that are to be collapsed. |
graph |
The graph containing the nodes |
newName |
The name for the new, collapsed node. |
collapseFunction |
Function or character giving the name of a function used to collapse the edge weights after combining nodes. The default is to sum up the weights, but mean would be a useful alternative. |
... |
Additional arguments for the generic |
The nodes specified are reduced to a single new node with label given
by newName
. The in and out edges of the set of nodes are all
made into in and out edges for the new node.
An new instance of a graph of the same class as graph
is
returned. This new graph has the specified nodes reduced to a single
node.
R. Gentleman
V <- LETTERS[1:4] edL1 <- vector("list", length=4) names(edL1) <- V for(i in 1:4) edL1[[i]] <- list(edges=c(2,1,4,3)[i], weights=sqrt(i)) gR <- graphNEL(nodes=V, edgeL=edL1, edgemode="directed") gR <- addNode("M", gR) gR <- addEdge("M", "A", gR, 1) gR <- addEdge("B", "D", gR, 1) gX <- combineNodes(c("B","D"), gR, "X") gR <- addNode("K", gR) gR <- addEdge(c("K","K"), c("D", "B"), gR, c(5,3)) edgeWeights(combineNodes(c("B","D"), gR, "X"))$K edgeWeights(combineNodes(c("B","D"), gR, "X", mean))$K
V <- LETTERS[1:4] edL1 <- vector("list", length=4) names(edL1) <- V for(i in 1:4) edL1[[i]] <- list(edges=c(2,1,4,3)[i], weights=sqrt(i)) gR <- graphNEL(nodes=V, edgeL=edL1, edgemode="directed") gR <- addNode("M", gR) gR <- addEdge("M", "A", gR, 1) gR <- addEdge("B", "D", gR, 1) gX <- combineNodes(c("B","D"), gR, "X") gR <- addNode("K", gR) gR <- addEdge(c("K","K"), c("D", "B"), gR, c(5,3)) edgeWeights(combineNodes(c("B","D"), gR, "X"))$K edgeWeights(combineNodes(c("B","D"), gR, "X", mean))$K
This function implements algorithm 4.2.1 of Gross and Yellen. The
input is a graph
and a node
to start from. It returns a
standard vertex labeling of graph
. This is a vector with
elements corresponding to the nodes of graph
and with values
that correspond to point in the depth first search the node is
visited.
DFS(object, node, checkConn=TRUE)
DFS(object, node, checkConn=TRUE)
object |
An instance of the |
node |
A |
checkConn |
A |
This function implements algorithm 4.2.1 of Gross and Yellen. Specific details are given there.
It requires that the graph be connected. By default, this is checked, but since the checking can be expensive it is optional.
A faster and mostly likely better implementation of depth first
searching is given by dfs
in the RBGL
package.
A vector with names given by the nodes of graph
whose values
are 0
to one less than the number of nodes. These indices
indicate the point at which the node will be visited.
R. Gentleman
Graph Theory and its Applications, J. Gross and J. Yellen.
RNGkind("Mersenne-Twister") set.seed(123) g1 <- randomGraph(letters[1:10], 1:4, p=.3) RNGkind() DFS(g1, "a")
RNGkind("Mersenne-Twister") set.seed(123) g1 <- randomGraph(letters[1:10], 1:4, p=.3) RNGkind() DFS(g1, "a")
A class definition for graphs that are based on distances.
Objects can be created by calls of the form new("distGraph", ...)
.
Dist
:Object of class "dist"
that forms the
basis for the edge weights used in the distGraph
.
Class "graph"
, directly.
signature(object = "distGraph")
: a print method
signature(object = "distGraph")
: return the dist
object.
signature(object = "distGraph")
: find the nodes
adjacent to the supplied node.
signature(object = "distGraph")
: return the
nodes in the graph.
signature(object = "distGraph")
: return the
number of nodes.
signature(object = "distGraph", k, value)
: set all
distances that are larger than the supplied threshold, k
, to the
supplied value. The default is value is zero (and so is appropriate for
similarities, rather than distances).
signature(object = "distGraph")
:
initialize a distGraph
instance.
Return a list of edge weights in a list format
similar to the edges
method.
signature(graph = "distGraph")
: A method for
obtaining the edge list.
R. Gentleman
Shamir's paper and Butte et al
graph-class
, clusterGraph-class
set.seed(123) x <- rnorm(26) names(x) <- letters library(stats) d1 <- dist(x) g1 <- new("distGraph", Dist=d1)
set.seed(123) x <- rnorm(26) names(x) <- letters library(stats) d1 <- dist(x) g1 <- new("distGraph", Dist=d1)
A multigraph is a graph where edges between nodes can be represented
several times. For some algorithms this causes
problems. duplicatedEdges
tests an instance of the
graphNEL
class to see if it has duplicated edges and returns
TRUE
if it does and FALSE
otherwise.
duplicatedEdges(graph)
duplicatedEdges(graph)
graph |
An instance of the class |
It would be nice to handle other types of graphs.
A logical, either TRUE
if the graph has duplicated edges or
FALSE
it not.
R. Gentleman
##---- Should be DIRECTLY executable !! ---- ##-- ==> Define data, use random,
##---- Should be DIRECTLY executable !! ---- ##-- ==> Define data, use random,
Attributes of the edges of a graph can be accessed using
edgeData
. The attributes must be defined using
edgeDataDefaults
. You can ommit the from
or
to
argument to retrieve attribute values for all edges to
(respectively, from) a given node.
edgeData(self, from, to, attr) edgeData(self, from, to, attr) <- value
edgeData(self, from, to, attr) edgeData(self, from, to, attr) <- value
self |
A |
from |
A |
to |
A |
attr |
A |
value |
An R object to store as the attribute value |
Set default values for attributes associated with the edges of a graph.
edgeDataDefaults(self, attr) edgeDataDefaults(self, attr) <- value
edgeDataDefaults(self, attr) edgeDataDefaults(self, attr) <- value
self |
A |
attr |
A |
value |
An R class to use as the default value for the specified attribute |
For our purposes an edge matrix is a matrix with two rows and as many columns as there are edges. The entries in the first row are the index of the node the edge is from, those in the second row indicate the node the edge is to.
If the graph is “undirected” then the duplicates
option
can be used to indicate whether reciprocal edges are wanted. The
default is to leave them out. In this case the notions of from
and to are not relevant.
edgeMatrix(object, duplicates=FALSE) eWV(g, eM, sep = ifelse(edgemode(g) == "directed", "->", "--"), useNNames=FALSE) pathWeights(g, p, eM=NULL)
edgeMatrix(object, duplicates=FALSE) eWV(g, eM, sep = ifelse(edgemode(g) == "directed", "->", "--"), useNNames=FALSE) pathWeights(g, p, eM=NULL)
object |
An object that inherits from |
g |
An object that inherits from |
duplicates |
Whether or not duplicate edges should be produced for “undirected” graphs. |
eM |
An edge matrix |
sep |
a character string to concatenate node labels in the edge label |
useNNames |
a logical; if TRUE, node names are used in the edge label; if FALSE, node indices are used |
p |
a vector of node names constituting a path in graph |
Implementations for graphNEL
, clusterGraph
and
distGraph
are available.
edgeMatrix
returns a matrix with two rows, from and to, and as many columns
as there are edges. Entries indicate the index in the node vector that
corresponds to the appropriate end of the edge.
eWV
uses the edge matrix to create an annotated vector of
edge weights.
pathWeights
returns an annotated vector of edge weights
for a specified path in a graph.
A path through an undirected graph may have several representations as a named vector of edges. Thus in the example, when the weights for path b-a-i are requested, the result is the pair of weights for edges a–b and a–i, as these are the edge labels computed for graph g1.
R. Gentleman
set.seed(123) g1 <- randomGraph(letters[1:10], 1:4, p=.3) edgeMatrix(g1) g2 <- new("clusterGraph", clusters=list(a=c(1,2,3), b=c(4,5,6))) em2 <- edgeMatrix(g2) eWV(g1, edgeMatrix(g1)) eWV(g1, edgeMatrix(g1), useNNames=TRUE) pathWeights(g1, c("b", "a", "i"))
set.seed(123) g1 <- randomGraph(letters[1:10], 1:4, p=.3) edgeMatrix(g1) g2 <- new("clusterGraph", clusters=list(a=c(1,2,3), b=c(4,5,6))) em2 <- edgeMatrix(g2) eWV(g1, edgeMatrix(g1)) eWV(g1, edgeMatrix(g1), useNNames=TRUE) pathWeights(g1, c("b", "a", "i"))
C57BL/6J and C3H/HeJ mouse strains exhibit different cardiovascular and
metabolic phenotypes on the hyperlipidemic apolipoprotein E (Apoe) null background.
The interaction data for the genes from adipose, brain, liver and muscle tissue
samples from male and female mice are included as a list of data.frame
s.
Each data.frame
contains information for the from-gene
, to-gene
and the strength of interaction (weight
) for each of the tissues studied.
data(esetsFemale) data(esetsMale)
data(esetsFemale) data(esetsMale)
Sage Commons Repository http://sagebase.org/commons/dataset1.php#UCLA1
data(esetsFemale) data(esetsMale)
data(esetsFemale) data(esetsMale)
A generic function that returns the edge weights of a graph. If
index
is specified, only the weights for the edges from the
specified nodes are returned. The user can control which edge
attribute is interpreted as the weight, see the Details section.
edgeWeights(object, index, ..., attr = "weight", default = 1, type.checker = is.numeric)
edgeWeights(object, index, ..., attr = "weight", default = 1, type.checker = is.numeric)
object |
A graph, any object that inherits from the |
index |
If supplied, a character or numeric vector of node names or indices. |
... |
Unused. |
attr |
The name of the edge attribute to use as a weight. You
can view the list of defined edge attributes and their default values
using |
default |
The value to use if |
type.checker |
A function that will be used to check that the
edge weights are of the correct type. This function should return
TRUE if the input vector is of the right type and FALSE otherwise.
The default is to check for numeric edge weights using
|
If index
is suppled, then edge weights from these nodes to all
adjacent nodes are returned. If index
is not supplied, then the
edge weights for all nodes are returned. The value for nodes without
any outgoing edges will be a zero-length vector of the appropriate
mode.
The edgeWeights
method is a convenience wrapper around
edgeData
, the general-purpose way to access edge attribute
information for a graph
instance. In general, edge attributes
can be arbitary R objects. However, for edgeWeights
to make
sense, the values must be vectors of length not more than one.
By default, edgeWeights
looks for an edge attribute with name
"weight"
and, if found, uses these values to construct the edge
weight list. You can make use of attributes stored under a different
name by providing a value for the attr
argument. For example,
if object
is a graph instance with an edge attribute named
"WTS"
, then the call edgeWeights(object, attr="WTS")
will attempt to use those values.
The function specified by type.checker
will be given a vector
of edge weights; if the return value is not TRUE
, then an error
will be signaled indicating that the edge weights in the graph are not
of the expected type. Type checking is skipped if type.checker
is NULL
.
If the graph instance does not have an edge attribute with name given
by the value of the attr
argument, default
will be used
as the weight for all edges. Note that if there is an attribute named
by attr
, then its default value will be used for edges not
specifically customized. See edgeData
and
edgeDataDefaults
for more information.
Because of their position after the ...
, no partial matching is
performed for the arguments attr
, default
, and
type.checker
.
A named list of named edge weight vectors. The names on the list are
the names of the nodes specified by index
, or all nodes if
index
was not provided. The names on the weight vectors are
node names to identify the edge to which the weight belongs.
R. Gentleman and S. Falcon
nodes
edges
edgeData
edgeDataDefaults
is.numeric
is.integer
is.character
V <- LETTERS[1:4] edL2 <- vector("list", length=4) names(edL2) <- V for(i in 1:4) edL2[[i]] <- list(edges=c(2,1,2,1)[i], weights=sqrt(i)) gR2 <- graphNEL(nodes=V, edgeL=edL2, edgemode="directed") edgeWeights(gR2, "C") edgeWeights(gR2) edgeWeights(gR2, attr="foo", default=5) edgeData(gR2, attr="weight") edgeData(gR2, from="C", attr="weight")
V <- LETTERS[1:4] edL2 <- vector("list", length=4) names(edL2) <- V for(i in 1:4) edL2[[i]] <- list(edges=c(2,1,2,1)[i], weights=sqrt(i)) gR2 <- graphNEL(nodes=V, edgeL=edL2, edgemode="directed") edgeWeights(gR2, "C") edgeWeights(gR2) edgeWeights(gR2, attr="foo", default=5) edgeData(gR2, attr="weight") edgeData(gR2, from="C", attr="weight")
GXL http://www.gupro.de/GXL is "an XML sublanguage designed to be a standard exchange format for graphs". This document describes tools in the graph package for importing GXL data to R and for writing graph data out as GXL.
fromGXL |
currently returns a graphNEL when possible. This
function is based on |
toGXL |
for an input of class "graphNEL", returns an object of class c("XMLInternalDOM", "XMLOutputStream"); see the example for how to convert this to a text stream encoding XML |
dumpGXL |
returns an R list with all the node, edge, and named attribute information specified in the GXL stream |
validateGXL |
returns silently (invisibly returns the parsed tree) for a DTD-compliant stream, or is otherwise very noisy |
con = connection: returns a graphNEL based on a parsing of the GXL stream on the connection
con = connection: returns an R list based on a parsing of the GXL stream on the connection
con = connection: checks the GXL stream against its DTD
object = graphNEL: creates an XMLInternalDOM representing the graph in GXL
At present, toGXL does not return a validating GXL stream
because XML package does not properly handle the dtd and namespaces
arguments to xmlTree. This is being repaired. To fix
the stream, add
<!DOCTYPE gxl SYSTEM "http://www.gupro.de/GXL/gxl-1.0.1.dtd">
as second record in the output.
Some structures in a graphNEL and some tags in GXL may not be handled at this time.
Vince Carey <[email protected]>
sf <- file(system.file("GXL/simpleExample.gxl", package="graph")) show(fromGXL(sf)) print(dumpGXL(sf)) close(sf) #validateGXL(sf) # bad <- file(system.file("GXL/c2.gxl", package="graph")) # here's how you can check if the GXL is well-formed, if # you have a libxml2-based version of R XML package # # try( validateGXL(bad) ) # gR <- graphNEL(nodes=letters[1:4], edgeL=list( a=list(edges=4), b=list(edges=3), c=list(edges=c(2,1)), d=list(edges=1)), edgemode="directed") # # following requires that you are using XML bound with recent libxml2 # #an <- as.numeric #if (an(libxmlVersion()$major)>=2 && an(libxmlVersion()$minor)>=4) ## since toGXL returns an XML object, we need to attach the XML ## package. library("XML") cat(saveXML(toGXL(gR)$value())) wtd <- file(system.file("GXL/kmstEx.gxl", package="graph")) wtdg <- fromGXL(wtd) close(wtd) print(edgeWeights(wtdg))
sf <- file(system.file("GXL/simpleExample.gxl", package="graph")) show(fromGXL(sf)) print(dumpGXL(sf)) close(sf) #validateGXL(sf) # bad <- file(system.file("GXL/c2.gxl", package="graph")) # here's how you can check if the GXL is well-formed, if # you have a libxml2-based version of R XML package # # try( validateGXL(bad) ) # gR <- graphNEL(nodes=letters[1:4], edgeL=list( a=list(edges=4), b=list(edges=3), c=list(edges=c(2,1)), d=list(edges=1)), edgemode="directed") # # following requires that you are using XML bound with recent libxml2 # #an <- as.numeric #if (an(libxmlVersion()$major)>=2 && an(libxmlVersion()$minor)>=4) ## since toGXL returns an XML object, we need to attach the XML ## package. library("XML") cat(saveXML(toGXL(gR)$value())) wtd <- file(system.file("GXL/kmstEx.gxl", package="graph")) wtdg <- fromGXL(wtd) close(wtd) print(edgeWeights(wtdg))
A virtual class that all graph classes should extend.
degree
returns either a named vector (names correspond to the
nodes in the graph) containing the degree for undirected graphs or a
list with two components, inDegree
and outDegree
for
directed graphs.
connComp
returns a list of the connected components. Each
element of this list contains the labels of all nodes in that
component.
For a directed graph or digraph the underlying
graph is the graph that results from removing all direction from
the edges. This can be achieved using the function ugraph
.
A weakly connected component of a digraph is one that is
a connected component of the underlying graph. This is the default for
connComp
. A digraph is strongly connected if
every two vertices are mutually reachable. A strongly connected
component of a digraph, D, is a maximal strongly
connected subdigraph of D. See the RBGL package for an
implementation of Trajan's algorithm to find strongly
connected components (strongComp
).
In the graph implementation of connComp
weak
connectivity is used. If the argument to connComp
is a
directed graph then ugraph
is called to create the
underlying undirected graph and that is used to compute connected
components. Users who want different behavior are encouraged to use
RBGL.
A virtual Class: No objects may be created from it.
edgeData
:An attrData
instance for edge attributes.
nodeData
:An attrData
instance for node attributes.
graphData
:A list
for graph-level
attributes. Only mandatory list item is edgemode
which
indicates whether edges are "directed"
or
"undirected"
renderInfo
:A list
of graph rendering information.
return a character vector containing the names of the nodes of the graph
rename the nodes of the graph
signature(object = "graph")
:A print method for
the graph.
signature(object = "graph")
: find all nodes
accessible from the specified node.
signature(x = "graph")
: compute the
complement of the supplied graph. The complement is defined with
respect to the complete graph on the nodes in x
.
Currently this returns an object of class graphNEL
.
signature(object = "graph")
: find the
connected components of a graph.
signature(object = "graph")
:
find the degree
of a node (number of coincident edges).
signature(object = "MultiGraph")
:
find the degree
of a node (number of coincident edges).
signature(object = "graph")
: execute a depth first
search on a graph starting with the specified node.
signature(object="graph", which="character")
:
return the edges indicated by which
. which
can be
missing in which case all edges are returned or it can be a
character vector with the node labels indicating the nodes whose
edge lists are wanted.
Get and set default attributes for the edges in the graph.
Get and set attributes for edges in the graph
signature(object="graph")
: return the
edgemode
for the graph. Currently this can be either
directed
or undirected
.
signature(object="graph",
value="character")
: set the edgemode
for the graph. Currently this can be either
directed
or undirected
.
Return a list of edge weights in a list format
similar to the edges
method.
signature(x = "graph", y = "graph")
: compute the
intersection of the two supplied graphs. They must have identical
nodes. Currently this returns an object of class
graphNEL
. With edge weights of 1 for any matching edge.
signature(from="character",
to="character")
: Determine if edges exists between nodes.
signature(object = "graph")
: A boolean
that details if a graph is fully connected or not.
Return TRUE
if the graph object has
directed edges and FALSE
otherwise.
signature(x = "graph", y = "graph")
: returns the
joining of two graphs. Nodes which are shared by both graphs will
have their edges merged. Note that edgeWeights for the resulting
graph are all set to 1. Users wishing to preserve weights in
a join operation must
perform addEdge operations on the resulting graph to restore weights.
A generic function that allows different
implementations of the graph
class to reset the node
labels
Get/set default attributes for nodes in the graph.
Get/set attributes for nodes in the graph.
signature(object = "graph")
: compute the
number of edges in a graph.
signature(object = "graph")
: compute the
number of nodes in a graph.
Please see the help page for the
plot,graph-method
method in the Rgraphviz
package
signature(x = "graph", y = "graph")
: compute the
union of the two supplied graphs. They must have identical
nodes. Currently this returns an object of class graphNEL
.
signature(object = "graph")
: Returns a
vector of the edge names for this graph, using the format
tail\~head
, where head
is the name of the tail node
and head
is the name of the head node.
signature(object = "graph")
: Updates old
instances of graph objects.
R. Gentleman and E. Whalen.
Graph Theory and its Applications, J. Gross and J. Yellen.
graphNEL-class
, graphAM-class
,
distGraph-class
.
set.seed(123) g1 <- randomGraph(letters[1:10], 1:4, p= 0.3) numEdges(g1) edgeNames(g1) edges(g1) edges(g1, c("a","d")) # those incident to 'a' or 'd'
set.seed(123) g1 <- randomGraph(letters[1:10], 1:4, p= 0.3) numEdges(g1) edgeNames(g1) edges(g1) edges(g1, c("a","d")) # those incident to 'a' or 'd'
The functions or variables listed here are no longer part of the graph package.
buildRepDepGraph() pkgInstOrder() ugraphOld()
buildRepDepGraph() pkgInstOrder() ugraphOld()
Functions providing an interface to persistent graphical parameters and other settings used in the package.
graph.par(...) graph.par.get(name)
graph.par(...) graph.par.get(name)
... |
either character strings naming parameters whose values are to be retrieved, or named arguments giving values that are to be set. |
name |
character string, giving a valid parameter name. |
graph.par
works sort of like par
, but the details
are yet to be decided.
graph.par.get(name)
is equivalent to graph.par(name)[[1]]
In query mode, when no parameters are being set, graph.par
returns a list containing the current values of the requested
parameters. When called with no arguments, it returns a list with all
parameters. When a parameter is set, the return value is a list
containing previous values of these parameters.
Deepayan Sarkar, [email protected]
These functions provide coercions between objects that inherit from
the graph
class to sparse matrices from the SparseM
package.
graph2SparseM(g, useweights=FALSE) sparseM2Graph(sM, nodeNames, edgemode=c("directed", "undirected"))
graph2SparseM(g, useweights=FALSE) sparseM2Graph(sM, nodeNames, edgemode=c("directed", "undirected"))
g |
An instance of the |
useweights |
A logical value indicating whether to use the edge weights in the graph as values in the sparse matrix. |
sM |
A sparse matrix. |
nodeNames |
A |
edgemode |
Specifies whether the graph to be created should have
directed (default) or undirected edges. If undirected, the input
matrix |
A very simple coercion from one representation to another.
Currently it is presumed that the matrix is square. For other graph formats, such as bipartite graphs, some improvements will be needed; patches are welcome.
graph2SparseM
takes as input an instance of a subclass of the
graph
class and returns a sparse matrix.
sparseM2Graph
takes a sparse matrix as input and returns an
instance of the graphNEL
class. By default, the
graphNEL
returned will have directed edges.
R. Gentleman
graph-class
,
graphNEL-class
, and for
other conversions, aM2bpG
and ftM2adjM
set.seed(123) g1 <- randomGraph(letters[1:10], 1:4, p=.3) s1 <- graph2SparseM(g1, useweights=TRUE) g2 <- sparseM2Graph(s1, letters[1:10], edgemode="undirected") ## consistency check stopifnot(all.equal(g1, g2))
set.seed(123) g1 <- randomGraph(letters[1:10], 1:4, p=.3) s1 <- graph2SparseM(g1, useweights=TRUE) g2 <- sparseM2Graph(s1, letters[1:10], edgemode="undirected") ## consistency check stopifnot(all.equal(g1, g2))
A graph class where node and edge information is represented as an
adjacency matrix. The adjacency matrix is square and element
adjMat[i, j]
is one if there is an edge from node i to
node j and zero otherwise.
The non-zero matrix values can be used to initialize an edge
attribute. If this is desired, use the values
argument in the
call to new
and provide a list with a single named element.
The name determines the attributes and the value provides the default
value for that attribute.
Objects can be created by calls of the form graphAM(adjMat,
edgemode, values)
.
adjMat
:An adjacency "matrix"
describing the
graph structure. The colnames
of the matrix will be used as
node names for the graph if present.
edgeData
:Storage for edge attributes.
nodeData
:Storage for node attributes.
Class "graph"
, directly.
graphAM(adjMat=matrix(integer(), 0, 0), edgemode='undirected', values=NA)
creates a graphAM instance.
An integer
matrix specifying which nodes have
edges between them.
Either "directed" or "undirected".
A named list of length 1, used (rather obscurely) to specify that non-zero adjMat values initialize an edge attribute. The name of the single element in that list becomes the name of that attribute, with the specified default value. This default value is, however, never used: the specified edge attribute always has the value contained in the adjacency matrix, which is traditionally 1, but can be any positive number.
signature(from = "character", to = "character", graph = "graphAM", weights = "missing")
: ...
signature(object = "graphAM", nodes = "character")
: ...
signature(node = "character", object = "graphAM")
: ...
signature(from = "graphAM", to = "graphNEL")
: ...
signature(from = "graphAM", to = "graphBAM")
: ...
signature(from = "graphAM", to = "matrix")
: In
converting to a matrix
, if an edge attribute named
"weight"
is defined, the non-zero elements of the matrix
will contain the corresponding attribute value. For more flexible
matrix conversion, see toMatrix
.
signature(from = "matrix", to = "graphAM")
:
This coerce method exists for symmetry. In most cases, creating a
new graphAM
instance using new
gives one more
control over the resulting graph.
signature(object = "graphAM")
: ...
signature(.Object = "graphAM")
: ...
signature(node = "character", object =
"graphNEL")
: Return the incoming edges for the specified
nodes. See inEdges
.
signature(object = "graphAM", from = "character", to = "character")
: ...
signature(object = "graphAM", value = "character")
: ...
signature(object = "graphAM")
: ...
signature(graph = "graphAM")
: ...
signature(object = "graphAM")
: ...
signature(from = "character", to = "character", graph = "graphAM")
: ...
signature(node = "character", object = "graphAM")
: ...
Seth Falcon
mat <- rbind(c(0, 0, 1, 1), c(0, 0, 1, 1), c(1, 1, 0, 1), c(1, 1, 1, 0)) rownames(mat) <- colnames(mat) <- letters[1:4] g1 <- graphAM(adjMat=mat) stopifnot(identical(mat, as(g1, "matrix")), validObject(g1)) ## now with weights: mat[1,3] <- mat[3,1] <- 10 gw <- graphAM(adjMat=mat, values=list(weight=1)) ## consistency check: stopifnot(identical(mat, as(gw, "matrix")), validObject(gw), identical(gw, as(as(gw, "graphNEL"), "graphAM")))
mat <- rbind(c(0, 0, 1, 1), c(0, 0, 1, 1), c(1, 1, 0, 1), c(1, 1, 1, 0)) rownames(mat) <- colnames(mat) <- letters[1:4] g1 <- graphAM(adjMat=mat) stopifnot(identical(mat, as(g1, "matrix")), validObject(g1)) ## now with weights: mat[1,3] <- mat[3,1] <- 10 gw <- graphAM(adjMat=mat, values=list(weight=1)) ## consistency check: stopifnot(identical(mat, as(gw, "matrix")), validObject(gw), identical(gw, as(as(gw, "graphNEL"), "graphAM")))
The graphBAM class represents a graph as an adjacency matrix. The
adjacency matrix is stored as a bit array using a raw
vector to
reduce the memory footprint and speed operations like
graphIntersection
. This class is EXPERIMENTAL and its API is
subject to change.
graphBAM(df, nodes=NULL, edgemode="undirected", ignore_dup_edges = FALSE)
graphBAM(df, nodes=NULL, edgemode="undirected", ignore_dup_edges = FALSE)
df |
A |
nodes |
A character vector of node labels. Use this to add degree zero
nodes to the graph. If |
edgemode |
A string, one of "directed" or "undirected". |
ignore_dup_edges |
If |
The GraphBAM
function is used to create new graphBAM
instances. Edges are specified in a data.frame
. For
undirected graphs, reciprical edges should not be includes unless
ignoe_dup_edges
is TRUE
.
Class "graph"
, directly.
addEdge(from, to, graph, weights)
Return a new graphBAM
object with the specified edge(s)
added. The from
and to
arguments must either be the
same length or one of them must be of length one. Each time an
edge is added, the entire graph is copied. For the purpose of
building a graph it will often be more efficient to build up the
list of edges and call GraphBAM
.
addNode(node, object)
Return a new graphBAM
object with the specified node(s)
added.
clearNode(node, object)
This operation is not currently supported.
edges(object, which)
Returns an adjacency list representation of the graph. The list
will have an entry for each node with a vector of adjacent node
labels or character(0)
. For undirected graphs,
edges
returns the reciprocal edges. The optional
argument which
can be a character vector of node labels.
When present, only entries for the specified nodes will be
returned.
inEdges(node, object)
(Not yet supported)
Similar to the edges
function, but the adjacency list maps
nodes that have an edge to the given node instead of from the
given node.
isAdjacent(object, from, to)
Returns a logical vector indicating whether there is an edge
corresponding to the elements in from
and to
. These
vectors must have the same length, unless one has length one.
nodes(object)
Return the node labels for the graph
numEdges(object)
Returns the number of edges in the graph.
numNodes(object)
Returns the number of nodes in the graph
removeEdge(from, to, graph)
Return a new graphBAM
object with the specified edges
removed. The from
and to
arguments must be
the same length unless one of them has length one.
removeNode(node, object)
Returns a new graphBAM
object with the specified node removed.
Node and edge attributes corresponding to that node are also removed.
edgeData(self, from, to, attr)
Access edge attributes. See help for edgeData
.
edgeDataDefaults(self, attr)
Access edge data default attributes .
nodeDataDefaults(self, attr)
Access node data default attributes .
edgeWeights(object, index)
Return the edge weights for the graph in adjacency list format.
The optional argument index
specified a character vector of
nodes. In this case, only the weights for the specified nodes
will be returned.
extractFromTo(g)
Returns a data frame with column names "from", "to", and "weight" corresponding to the connected nodes in the graphBAM object.
graphIntersect(x, y, nodeFun, edgeFun)
When given two graphBAM
objects, graphIntersect
returns a new graphBAM
containing the nodes and edges in
common between the two graphs. Both x and y should either be
directed or undirected. The intersection is computed by
first finding the intersection of the node sets, obtaining the
resulting subgraphs, and finding the intersection of the resulting
edge sets. Node/Edge attributes that are equal are carried over to
the result. Non equal edge/node attributes will result in the
corresponding attribute being set to NA. The user has the option
of providing a named list of functions correspoding to the names of
the edge attributes for resolving conflicting edge attributes.
For resolving any of the conflicting node attributes
the user has the option of providing a named list
of functions
corresponding to the node attribute names.
graphUnion(x, y, nodeFun, edgeFun)
When given two graphBAM
objects, graphUnion
returns a new graphBAM
containing the union of nodes and
edges between the two graphs. The union is compted by first finding
the union of the nodesets. Both x and y should be either directed or
undirected. Node/Edge attributes that are equal are carried over to
the result. Non equal edge/node attributes will result in the
corresponding attribute being set to NA. The user has the option
of providing a named list of functions correspoding to the names of
the edge attributes for resolving conflicting edge attributes.
For resolving any of the conflicting node attributes
the user has the option of providing a named list
of functions
corresponding to the node attribute names.
edgemode(object) <- value
Set the edgemode for the graph ("directed" or "undirected"). If
the specified edgemode is the same, the object is returned without
changes. Otherwise, a directed graph is converted to an
undirected graph via ugraph
and an undirected graph is
returned such that each edge is interpreted as two edges, one in
each direction.
ugraph(graph)
Return an undirected version of the current graph. Conceptually, the arrows of a graph's directed edges are removed.
nodes(object) <- value
Replacement of a graphBAM
object's node labels is currently
not supported. An error is raised if this method is called.
graphBAM
objects can be coerced to graphAM
,
graphNEL
, and matrix
instances via as(g, CLASS)
.
N. Gopalakrishnan, S. Falcon
f <- c("a", "a", "b", "c", "d") t <- c("b", "c", "c", "d", "a") weight <- c(2.3, 2.3, 4.3, 1.0, 3.0) df <- data.frame(from=f, to=t, weight= weight, stringsAsFactors = TRUE) g <- graphBAM(df) nd <- nodes(g) nodeDataDefaults(g, attr ="color") <- "green" nodeData(g,n=c("b", "c"), attr ="color") <- "red" w1 <- edgeWeights(g) w2 <- edgeWeights(g,"a") w3 <- edgeWeights(g,1) d1 <- edges(g) d2 <- edges(g,c("a", "b")) e1 <- edgeData(g) e2 <- edgeData(g, "a", "c",attr="weight") em <- edgeMatrix(g) id <- isDirected(g) sg <- subGraph(c("a","c","d"), g) ft <- extractFromTo(g) am <- as(g,"graphAM") nl <- as(g,"graphNEL") mt <- as(g,"matrix") k <- graphIntersect(g,g) k <- graphUnion(g,g) e <- removeEdgesByWeight(g,lessThan= 3.0) f <- removeNode("a", g) g
f <- c("a", "a", "b", "c", "d") t <- c("b", "c", "c", "d", "a") weight <- c(2.3, 2.3, 4.3, 1.0, 3.0) df <- data.frame(from=f, to=t, weight= weight, stringsAsFactors = TRUE) g <- graphBAM(df) nd <- nodes(g) nodeDataDefaults(g, attr ="color") <- "green" nodeData(g,n=c("b", "c"), attr ="color") <- "red" w1 <- edgeWeights(g) w2 <- edgeWeights(g,"a") w3 <- edgeWeights(g,1) d1 <- edges(g) d2 <- edges(g,c("a", "b")) e1 <- edgeData(g) e2 <- edgeData(g, "a", "c",attr="weight") em <- edgeMatrix(g) id <- isDirected(g) sg <- subGraph(c("a","c","d"), g) ft <- extractFromTo(g) am <- as(g,"graphAM") nl <- as(g,"graphNEL") mt <- as(g,"matrix") k <- graphIntersect(g,g) k <- graphUnion(g,g) e <- removeEdgesByWeight(g,lessThan= 3.0) f <- removeNode("a", g) g
This data set contains a list of example graphNEL
objects, which can then
be used for plotting.
data(graphExamples)
data(graphExamples)
Various sources, primarily from randomGraph
and
randomEGraph
data(graphExamples) a <- graphExamples[[1]] a
data(graphExamples) a <- graphExamples[[1]] a
This is a class of graphs that are represented in terms of nodes and an edge list. This is a suitable representation for a graph with a large number of nodes and relatively few edges.
The graphNEL
class provides a very general structure for
representing graphs. It will be reasonably efficient for lists with
relatively more nodes than edges. Although this representation can
support multi-edges, such support is not implemented and instances
of graphNEL
are assumed to be simple graphs with at most one
edge between any pair of nodes.
The edgeL
is a named list
of the same length as the
node vector. The names are the names of the nodes. Each element of
edgeL
is itself a list. Each element of this (sub)list is a
vector (all must be the same length) and each element represents an
edge to another node. The sublist named edges
holds index
values into the node vector. And each such entry represents an edge
from the node which has the same name as the component of
edgeL
to the node with index provided. Another component that
is often used is named weights
. It represents edge weights.
The user can specify any other edge attributes (such as types
etc). They are responsible for any special handling that
these might require.
For an undirected
instance all edges are reciprocated (there
is an edge from A to B and from B to A).
Note that the reason for using indices to represent the to
end
of a node is so that we can easily support permutation of the node
labels as a way to generate randomizations of the graph.
nodes
:Object of class "vector"
.
edgeL
:Object of class "list"
. The edgeL
must be the same length as nodes
. The elements of this
vector correspond to the same element in nodes
. The
elements are themselves lists. If the node has any edges then this
list will have an element named edges
. This will
eventually change. Since edge weights are now stored in the
edge attributes construct, we do not need the extra level of
list.
Class "graph"
, directly.
graphNEL(nodes=character(), edgeL=list(), edgemode='undirected')
creates a graphNEL instance.
A character vector of node labels.
A named list either in the format returned by the
edges
method or a list of lists where each inner list has
an element named edges
and optionally an element named
weights
. If weights
is present, it must be the same
length as the edges
element.
Either "directed" or "undirected".
signature(object = "graphNEL")
: A method for
finding nodes adjacent to the suplied node.
signature(graph = "graphNEL")
: A method for
obtaining the edge list.
signature(object = "graphNEL")
: A method
for obtaining the edge weights.
signature(object = "graphNEL")
: A method for
obtaining the edges.
signature(node = "character", object =
"graphNEL")
: Return the incoming edges for the specified
nodes. See inEdges
.
signature(object = "graphNEL")
: A method for
obtaining the nodes.
signature(object = "graphNEL")
:A method for
determining how many nodes are in the graph.
signature(snodes="character", graph =
"graphNEL")
:A method for
obtaining the induced subgraph based on the set of supplied nodes
and the supplied graph.
Please see the help page for plot.graphNEL
in the
Rgraphviz
package
signature(object = "graphNEL")
: A method
that will convert a graphNEL
object into a matrix suitable
for interaction with Rgraphviz
. Not intended to be called
directly. This function will insure that no NA's (or other
undesired values) are in the graph, or created by coersion.
signature(object="graphNEL",
value="character")
: A method for replacing the nodes in a graph
object. It checks to be sure the values are the right length and
unique.
signature(from = "graphNEL", to = "graphAM")
:
Called via as
, the method converts to an adjacency matrix
representation. See graphAM-class
.
signature(from = "graphNEL", to = "graphBAM")
:
Called via as
, the method converts to an bit array
representation. See graphBAM-class
.
R. Gentleman
graphAM-class
, distGraph-class
,
clusterGraph-class
set.seed(123) V <- LETTERS[1:4] edL <- vector("list", length=4) names(edL) <- V for(i in 1:4) edL[[i]] <- list(edges=5-i, weights=runif(1)) gR <- graphNEL(nodes=V, edgeL=edL) edges(gR) edgeWeights(gR)
set.seed(123) V <- LETTERS[1:4] edL <- vector("list", length=4) names(edL) <- V for(i in 1:4) edL[[i]] <- list(edges=5-i, weights=runif(1)) gR <- graphNEL(nodes=V, edgeL=edL) edges(gR) edgeWeights(gR)
Returns a list of all incoming edges for the specified nodes.
inEdges(node, object)
inEdges(node, object)
node |
character vector of node names |
object |
a |
If no node
argument is specified, inEdges
returns the
incoming edges for all nodes in the graph.
For an undirected graph, inEdges
returns all edges for the
specified nodes.
A list with length matching the length of node
. If node
was missing, a list containing an element for each node in the graph.
Each list element contains a character vector of node names giving the nodes that have outgoing edges to the node given by the name of the list element.
R. Gentleman
V <- LETTERS[1:4] edL3 <- vector("list", length=4) for(i in 1:4) edL3[[i]] <- list(edges=(i%%4)+1, weights=i) names(edL3) <- V gR3 <- graphNEL(nodes=V, edgeL=edL3, "directed") inEdges(c("A", "B"), gR3)
V <- LETTERS[1:4] edL3 <- vector("list", length=4) for(i in 1:4) edL3[[i]] <- list(edges=(i%%4)+1, weights=i) names(edL3) <- V gR3 <- graphNEL(nodes=V, edgeL=edL3, "directed") inEdges(c("A", "B"), gR3)
A graph representing the integrin-mediated cell adhesion pathway from
KEGG, as well as a list of attributes for use in plotting the
graph with Rgraphviz
.
data(integrinMediatedCellAdhesion)
data(integrinMediatedCellAdhesion)
The integrinMediatedCellAdhesion
data set contains two objects:
The first is IMCAGraph
, which is an object of class
graph-NEL
and represents the hsa04510 graph from KEGG
.
The second is IMCAAttrs
, which is a list of four elements. The
first element, defAttrs
corresponds to the attrs
arguments of agopen
and
plot.graph
. The
second element is nodeAttrs
which corresponds to the
nodeAttrs
argument in the same two functions from
Rgraphviz
. The third element, subGList
corresponds to
the subGList
argument in those functions. Lastly, the fourth
element, LocusLink
provides a named list where the names are
the nodes and the values are vectors of LocusLink ID values which
correspond to those nodes.
The values from defAttrs
, nodeAttrs
and subGList
in the IMCAAttrs
list are part of an ongoing attempt by
Bioconductor to provide the set of options to most accurately recreate
the actual visual image of the pathway from the KEGG site using
Rgraphviz
. Users may try out their own combination of
attributes and settings for their own needs, but these represent our
own efforts at as closely recreating the image as possible.
http://www.genome.ad.jp/kegg/pathway/hsa/hsa04510.html
data(integrinMediatedCellAdhesion) if (require("Rgraphviz") & interactive()) plot(IMCAGraph, attrs=IMCAAttrs$defAttrs, nodeAttrs=IMCAAttrs$nodeAttrs, subGList=IMCAAttrs$subGList)
data(integrinMediatedCellAdhesion) if (require("Rgraphviz") & interactive()) plot(IMCAGraph, attrs=IMCAAttrs$defAttrs, nodeAttrs=IMCAAttrs$nodeAttrs, subGList=IMCAAttrs$subGList)
For a given subclass of graph-class
, returns TRUE
if the
graph contains an edge from node specified by from
to the node
specified by to
.
The appropriate logical vector will be returned as long as from
and to
have the same length and contain nodes in the graph
object specified by object
.
isAdjacent(object, from, to, ...)
isAdjacent(object, from, to, ...)
object |
An instance of a subclass of |
from |
A |
to |
A |
... |
May be used by methods called on subclasses of |
The edges of a graph-class
object are either directed or
undirected. This function returns TRUE
if the edges are
directed and FALSE
otherwise.
isDirected(object)
isDirected(object)
object |
A |
A leaf of an undirected graph is a node with degree equal to one. A leaf of a directed graph is defined with respect to in-degree or out-degree. The leaves of a directed graph with respect to in-degree (out-degree) are those nodes with in-degree (out-degree) equal to zero.
leaves(object, degree.dir)
leaves(object, degree.dir)
object |
A |
degree.dir |
One of |
A character vector giving the node labels of the leaves.
Seth Falcon
data(graphExamples) graphExamples[[1]] leaves(graphExamples[[1]]) data(apopGraph) leaves(apopGraph, "in") leaves(apopGraph, "out")
data(graphExamples) graphExamples[[1]] leaves(graphExamples[[1]]) data(apopGraph) leaves(apopGraph, "in") leaves(apopGraph, "out")
A list where each element contains all edges between two nodes, regardless of orientation. The list has names which are node pairs, in lexicographic order, and elements all edges between those nodes.
listEdges(object, dropNULL=TRUE)
listEdges(object, dropNULL=TRUE)
object |
An instance of the |
dropNULL |
Should those node pairs with no edges be dropped from the returned list. |
The function is currently only implemented for graphs of the
graphNEL-class
. The edges in the returned list are
instances of the simpleEdge-class
.
A named list of simpleEdge-class
objects.
R. Gentleman
set.seed(123) V <- LETTERS[1:4] edL <- vector("list", length=4) names(edL) <- V toE <- LETTERS[4:1] for(i in 1:4) edL[[i]] <- list(edges=5-i, weights=runif(1)) gR <- graphNEL(nodes=V, edgeL=edL) listEdges(gR)
set.seed(123) V <- LETTERS[1:4] edL <- vector("list", length=4) names(edL) <- V toE <- LETTERS[4:1] for(i in 1:4) edL[[i]] <- list(edges=5-i, weights=runif(1)) gR <- graphNEL(nodes=V, edgeL=edL) listEdges(gR)
A graph encoding parts of the MAPK signaling pathway
data(MAPKsig)
data(MAPKsig)
The format is: Formal class 'graphNEL' [package "graph"] with edgemode "directed".
The KEGG pancreatic cancer pathway was visually inspected on 17 Sept 2007, and the subgraph associated with MAPK signaling was isolated. The HUGO names for each symbol in the KEGG visualization were obtained and checked for existance on hgu95av2. Some abbreviated terms for processes are also included as nodes.
data(MAPKsig) if (require(Rgraphviz)) { nat = rep(FALSE, length(nodes(MAPKsig))) names(nat) = nodes(MAPKsig) plot(MAPKsig, nodeAttrs=list(fixedsize=nat)) }
data(MAPKsig) if (require(Rgraphviz)) { nat = rep(FALSE, length(nodes(MAPKsig))) names(nat) = nodes(MAPKsig) plot(MAPKsig, nodeAttrs=list(fixedsize=nat)) }
mostEdges
finds the node that has the most edges in the graph.
This is the node with the highest degree.
mostEdges(objGraph)
mostEdges(objGraph)
objGraph |
the graph object |
index |
the index of the node with the most edges |
id |
the node value with the most edges; may be affy id, locus link id, or genename depending on the node type |
maxLen |
the number of edges for that node |
Elizabeth Whalen
numEdges
, aveNumEdges
,
numNoEdges
set.seed(123) g1 <- randomGraph(11:30, letters[20:26], p=.4) mostEdges(g1)
set.seed(123) g1 <- randomGraph(11:30, letters[20:26], p=.4) mostEdges(g1)
A collection of classes to model multigraphs. These include the multiGraph class as well as classes to contain edge sets.
Objects can be created from the multiGraph
class, the
edgeSet
class is virtual, and particular variants should be used.
These slots are for the multiGraph class.
The names of the nodes.
A list of edge lists.
An instance of the attrData
class.
A list.
These slots are for the edgeSet
class, or one of its
sublcasses.
An instance of the attrData
class.
A character vector, one of directed, or undirected.
A list of the edges (graphNEL)
An adjacency matrix (graphAM)
Print a multigraph.
A vector indicating which of the edgeSets is directed.
Retrieve the node names
Return the number of nodes
Return either all edges, or a subset of them, depending on the arguments supplied.
Return a vector with the number of edges, for each edge set.
The MultiGraph class represents a single node set and a set of edge sets. Each edge set is either directed or undirected. We can think of an edge in a MultiGraph as a 4-tuple (from-node, to-node, edge-type, weight), where the edge-type field in the tuple identifies the edge set, the weight is a numeric value, and the order of the nodes only matters in the case of a directed edge set. Unlike some of the graph representations, self-loops are allowed (from-node == to-node).
There is support for arbitrary edge attributes which is primarily useful for rendering plots of MultiGraphs. These attributes are stored separately from the edge weights to facilitate efficient edge weight computation.
MultiGraph(edgeSets, nodes = NULL, directed = TRUE, ignore_dup_edges = FALSE) eweights(object, names.sep = NULL) edgeSetIntersect0(g, edgeFun = NULL) edgeSetIntersect0(g, edgeFun = NULL) extractGraphAM(g, edgeSets) extractGraphBAM(g, edgeSets)
MultiGraph(edgeSets, nodes = NULL, directed = TRUE, ignore_dup_edges = FALSE) eweights(object, names.sep = NULL) edgeSetIntersect0(g, edgeFun = NULL) edgeSetIntersect0(g, edgeFun = NULL) extractGraphAM(g, edgeSets) extractGraphBAM(g, edgeSets)
edgeSets |
A named list of |
nodes |
A character vector of node labels. Nodes with zero degree can be
included in a graph by specifying the node labels in |
directed |
A logical vector indicating whether the edge sets specified in
|
object |
A |
g |
A |
names.sep |
The string to use as a separator between from and to node labels.
If |
ignore_dup_edges |
If |
edgeFun |
A user specified named list of functions to resolve edge attributes in a union or intersection operation |
MultiGraph
Return the nodes of the multigraph.
Return an integer vector named by edge set containing edge counts for each edge set.
Return the number of nodes in the multigraph.
Return a list named by edge set; each element is a numeric vector of edge weights for the corresponding edge set.
Return a logical vector named by the edge sets in
object
with a TRUE
indicating a directed edge set
and FALSE
for undirected.
Returns a list named by edge set; for the edges in the MultiGraph
Returns a list named by the edge set; for the names of the edges in the MultiGraph
Return a list named by the edge sets; each element is a data frame with column names from, to and weight corresponding to the connected nodes in the edge set.
Return a new MultiGraph
object
representing the subset of edge sets from the original
MultiGraph
.
Return a named list
of graphAM
objects corresponding to the edge sets from the original
MultiGraph
.
Return a named list
of graphBAM
objects corresponding to the edge sets from the original
MultiGraph
.
Return a new MultiGraph
object in which all
edge sets have been converted to undirected edge sets. This
operation sets all edge weights to one and drops other edge
attributes.
Return a new MultiGraph
object
representing the intersection of edges across all edge sets within
g
. The return value will have a single edge set if the edge
sets in g
are disjoint. Otherwise, there will be a single
edge set containing the shared edges. The node set is preserved.
Edge weights and edge attributes are transferred over to the output if they
have the same value, else user has the option of providing a function
to resolve the conflict.
Return a new MultiGraph
object
representing the union of edges across all edge sets within
g
. The node set is preserved. Edge weights and edge attributes are
transferred over to the output if they have the same value, else user has
the option of providing a function to resolve the conflict.
graphIntersect(x, y, nodeFun, edgeFun)
When given two MultiGraph
objects, graphIntersect
returns a new MultiGraph
containing the nodes and edges in
common between the two graphs. The intersection is computed by
first finding the intersection of the node sets, obtaining the
induced subgraphs, and finding the intersection of the resulting
edge sets. The corresponding named edgeSets in x
and y
should
both be either directed or undirected.Node/Edge attributes that are equal
are carried over to the result. Non equal edge/node attributes will result
in the corresponding attribute being set to NA. The user has the option
of providing a named list(names of edgeSets) of list (names of edge attributes)
of edge functions correspoding to the names of the edge attributes for
resolving conflicting edge attributes (edgeFun
). For resolving any
of the conflicting node attributes, the user has the option of providing a
named list
of functions corresponding to the node attribute names (nodeFun
).
graphUnion(x, y, nodeFun, edgeFun)
When given two MultiGraph
objects, graphUnion
returns a new MultiGraph
containing the union of nodes and
edges between the two graphs. The corresponding pairs of named edgeSets
in x
and y
should both be either directed or undirected.
Non equal edge/node attributes will result in the corresponding attribute
being set to NA. The user has the option of providing a named list(names of
edgeSets) of list (names of edge attributes) of edge functions correspoding
to the names of the edge attributes for resolving conflicting edge
attributes (edgeFun
). For resolving any of the conflicting node attributes, the user
has the option of providing a named list
of functions corresponding
to the node attribute names (nodeFun
).
edgeSets(object, ...)
Returns the names of the edgeSets in the MultiGraph
object
as a character vector.
Prints a short summary of a MultiGraph object
S. Falcon, Gopalakrishnan N
ft1 <- data.frame(from=c("a", "a", "a", "b", "b"), to=c("b", "c", "d", "a", "d"), weight=c(1, 3.1, 5.4, 1, 2.2), stringsAsFactors = TRUE) ft2 <- data.frame(from=c("a", "a", "a", "x", "x", "c"), to=c("b", "c", "x", "y", "c", "a"), weight=c(3.4, 2.6, 1, 1, 1, 7.9), stringsAsFactors = TRUE) esets <- list(es1=ft1, es2=ft2) g <- MultiGraph(esets) nodes(g) numEdges(g) eweights(g) eweights(g, names.sep = "=>") isDirected(g) edges(g, edgeSet ="es1") edges(g, "a", "es1") edgeNames(g, "es2") edgeSets(g) ug <- ugraph(g) isDirected(ug) numEdges(ug) edgeSetIntersect0(g) subsetEdgeSets(g, "es1") extractFromTo(g) extractGraphAM(g) extractGraphAM(g, "es1") extractGraphBAM(g, "es1") graphIntersect(g, g) graphUnion(g,g) mgEdgeDataDefaults(g, "es1", attr = "color" ) <- "white" mgEdgeData(g, "es1", from = "a", to = c("b", "c"), attr = "color") <- "red" mgEdgeData(g, "es1", from = "a", to = c("b", "c"), attr = "color") nodeDataDefaults(g, attr ="shape") <- "circle" nodeData(g, n = c("a", "b", "c"), attr = "shape") <- "triangle" nodeData(g, n = c("a", "b", "x", "y"), attr = "shape")
ft1 <- data.frame(from=c("a", "a", "a", "b", "b"), to=c("b", "c", "d", "a", "d"), weight=c(1, 3.1, 5.4, 1, 2.2), stringsAsFactors = TRUE) ft2 <- data.frame(from=c("a", "a", "a", "x", "x", "c"), to=c("b", "c", "x", "y", "c", "a"), weight=c(3.4, 2.6, 1, 1, 1, 7.9), stringsAsFactors = TRUE) esets <- list(es1=ft1, es2=ft2) g <- MultiGraph(esets) nodes(g) numEdges(g) eweights(g) eweights(g, names.sep = "=>") isDirected(g) edges(g, edgeSet ="es1") edges(g, "a", "es1") edgeNames(g, "es2") edgeSets(g) ug <- ugraph(g) isDirected(ug) numEdges(ug) edgeSetIntersect0(g) subsetEdgeSets(g, "es1") extractFromTo(g) extractGraphAM(g) extractGraphAM(g, "es1") extractGraphBAM(g, "es1") graphIntersect(g, g) graphUnion(g,g) mgEdgeDataDefaults(g, "es1", attr = "color" ) <- "white" mgEdgeData(g, "es1", from = "a", to = c("b", "c"), attr = "color") <- "red" mgEdgeData(g, "es1", from = "a", to = c("b", "c"), attr = "color") nodeDataDefaults(g, attr ="shape") <- "circle" nodeData(g, n = c("a", "b", "c"), attr = "shape") <- "triangle" nodeData(g, n = c("a", "b", "x", "y"), attr = "shape")
Attributes of the nodes of a graph can be accessed using
nodeData
. The attributes must be defined using
nodeDataDefaults
. You can ommit the n
argument
to retrieve attributes for all nodes in the graph. You can ommit the
attr
argument to retrieve all attributes.
nodeData(self, n, attr) nodeData(self, n, attr) <- value
nodeData(self, n, attr) nodeData(self, n, attr) <- value
self |
A |
n |
A |
attr |
A |
value |
An R object to store as the attribute value |
You can associate arbitrary attributes with the nodes of a graph. Use
nodeDataDefaults
to specify the set of attributes that describe
nodes. Each attribute must have a default value. You can set the
attribute for a particular node or set of nodes using
nodeData
.
nodeDataDefaults(self, attr) nodeDataDefaults(self, attr) <- value
nodeDataDefaults(self, attr) nodeDataDefaults(self, attr) <- value
self |
A |
attr |
A |
value |
An R object to set as the default value for the given attribute |
numNoEdges
calculates the number of nodes that have an edge list
of NULL (i.e. no edges).
numNoEdges(objGraph)
numNoEdges(objGraph)
objGraph |
the graph object |
An integer representing the number of NULL edge lists in the graph.
Elizabeth Whalen
numEdges
, aveNumEdges
,
mostEdges
set.seed(999) g1 <- randomEGraph(letters, .01) numNoEdges(g1)
set.seed(999) g1 <- randomEGraph(letters, .01) numNoEdges(g1)
A graph encoding parts of the pancreatic cancer initiation pathway
data(pancrCaIni)
data(pancrCaIni)
The format is: Formal class 'graphNEL' [package "graph"] with edgemode "directed".
The KEGG pancreatic cancer pathway was visually inspected on 17 Sept 2007 and a subgraph was isolated. The HUGO names for each symbol in the KEGG visualization were obtained and checked for existance on hgu95av2. Some abbreviated terms for processes are also included as nodes.
data(pancrCaIni) if (require(Rgraphviz)) { nat = rep(FALSE, length(nodes(pancrCaIni))) names(nat) = nodes(pancrCaIni) plot(pancrCaIni, nodeAttrs=list(fixedsize=nat)) }
data(pancrCaIni) if (require(Rgraphviz)) { nat = rep(FALSE, length(nodes(pancrCaIni))) names(nat) = nodes(pancrCaIni) plot(pancrCaIni, nodeAttrs=list(fixedsize=nat)) }
A function to create random graphs according to a random edge model.
The user supplies the set of nodes for the graph as V
and
either a probability, p
, that is used for each edge or the
number of edges, edges
they want to have in the resulting graph.
randomEGraph(V, p, edges)
randomEGraph(V, p, edges)
V |
The nodes for the graph. |
p |
The probability of an edge being selected. |
edges |
The number of edges wanted. |
The user must specify the set of nodes and either a probability for
edge selection or the number of edges wanted, but not both.
Let nV
denote the
number of nodes. There are choose(nV, 2)
edges in the complete
graph. If p
is specified then a biased coin (probability of
heads being p
) is tossed for each edge and if it is heads that
edge is selected. If edges
is specified then that many edges
are sampled without replacement from the set of possible edges.
An object of class graphNEL-class
that contains the nodes and
edges.
R. Gentleman
set.seed(123) V <- letters[14:22] g1 <- randomEGraph(V, .2) g2 <- randomEGraph(V, edges=30)
set.seed(123) V <- letters[14:22] g1 <- randomEGraph(V, .2) g2 <- randomEGraph(V, edges=30)
This function generates a random graph according to a model that
involves a latent variable. The construction is to randomly assign
members of the set M
to the nodes, V
. An edge is assigned
between two elements of V
when they both have the same element
of M
assigned to them. An object of class graphNEL
is
returned.
randomGraph(V, M, p, weights=TRUE)
randomGraph(V, M, p, weights=TRUE)
V |
The nodes of the graph. |
M |
A set of values used to generate the graph. |
p |
A value between 0 and 1 that indicates the probability of
selecting an element of |
weights |
A logical indicating whether to use the number of
shared elements of |
The model is quite simple. To generate a graph, G
, the user
supplies the list of nodes, V
and a set of values M
which will be used to create the graph. For each node in V
a
logical vector with length equal to the length of M
is
generated. The probability of a TRUE
at any position is
determined by p
. Once valus from M
have been assigned to
each node in V
the result is processed into a graph. This is
done by creating an edge between any two elements of V
that
share an element of M
(as chosen by the selection process).
The sizes of V
and M
and the values of p
determine how dense the graph will be.
An object of class graphNEL-class
is returned.
R. Gentleman
set.seed(123) V <- letters[1:10] M <- 1:4 g1 <- randomGraph(V, M, 0.2) numEdges(g1) # 16, in this case edgeNames(g1)# "<from> ~ <to>" since undirected
set.seed(123) V <- letters[1:10] M <- 1:4 g1 <- randomGraph(V, M, 0.2) numEdges(g1) # 16, in this case edgeNames(g1)# "<from> ~ <to>" since undirected
randomNodeGraph
generates a random graph with the specified
degree distribution. Self-loops are allowed. The resultant graph is
directed (but can always be coerced to be undirected).
randomNodeGraph(nodeDegree)
randomNodeGraph(nodeDegree)
nodeDegree |
A named integer vector specifying the node degrees. |
The input vector must be named, the names are taken to be the names of the nodes. The sum must be even (there is a theorem that says we require that to construct a graph). Self-loops are allowed, although patches to the code that make this a switchable parameter would be welcome.
An instance of the graphNEL
class. The graph is directed.
R. Gentleman
Random Graphs as Models of Networks, M. E. J. Newman.
set.seed(123) c1 <- c(a = 1, b = 1, c = 2, d = 4) (g1 <- randomNodeGraph(c1)) stopifnot(validObject(g1))
set.seed(123) c1 <- c(a = 1, b = 1, c = 2, d = 4) (g1 <- randomNodeGraph(c1)) stopifnot(validObject(g1))
A function to remove the specified edges from a graph.
removeEdge(from, to, graph)
removeEdge(from, to, graph)
from |
from edge labels |
to |
to edge labels |
graph |
a |
A new graph instance is returned with the edges specified by
corresponding elements of the from
and to
vectors
removed. If from
and to
are not the same length, one of
them should have length one. All edges to be removed must exist in
graph
.
A new instance of a graph with the same class as graph
is
returned with the specified edges removed.
R. Gentleman
V <- LETTERS[1:4] edL1 <- vector("list", length=4) names(edL1) <- V for(i in 1:4) edL1[[i]] <- list(edges=c(2,1,4,3)[i], weights=sqrt(i)) gR <- graphNEL(nodes=V, edgeL=edL1) gX <- removeEdge("A", "B", gR) set.seed(123) g <- randomEGraph(V=letters[1:5],edges=5) g2 <- removeEdge(from=c("a","b"), to=c("c","e"), g)
V <- LETTERS[1:4] edL1 <- vector("list", length=4) names(edL1) <- V for(i in 1:4) edL1[[i]] <- list(edges=c(2,1,4,3)[i], weights=sqrt(i)) gR <- graphNEL(nodes=V, edgeL=edL1) gX <- removeEdge("A", "B", gR) set.seed(123) g <- randomEGraph(V=letters[1:5],edges=5) g2 <- removeEdge(from=c("a","b"), to=c("c","e"), g)
A function to remove a node from a graph. All edges to and from the node are also removed.
removeNode(node, object)
removeNode(node, object)
node |
The label of the node to be removed. |
object |
The graph to remove the node from. |
The specified node is removed from the graph as are all edges to and
from that node. A new instance of the same class as object
with
the specified node(s) is returned.
Note, node can be a vector of labels, in which case all nodes are removed.
This is similar to subGraph
.
A new instance of a graph of the same class as object
but with
all specified nodes removed.
R. Gentleman
removeEdge
, addEdge
,
addNode
,subGraph
V <- LETTERS[1:4] edL2 <- vector("list", length=4) names(edL2) <- V for(i in 1:4) edL2[[i]] <- list(edges=c(2,1,2,1)[i], weights=sqrt(i)) gR2 <- graphNEL(nodes=V, edgeL=edL2, edgemode="directed") gX <- removeNode("C", gR2)
V <- LETTERS[1:4] edL2 <- vector("list", length=4) names(edL2) <- V for(i in 1:4) edL2[[i]] <- list(edges=c(2,1,2,1)[i], weights=sqrt(i)) gR2 <- graphNEL(nodes=V, edgeL=edL2, edgemode="directed") gX <- removeNode("C", gR2)
A container class to manage graph rendering attributes.
Objects can be created by calls of the form new("renderInfo")
or by using the initializer .renderInfoPrototype
.
pars
:List of default rendering attributes with two
items nodes
and edges
. When not set further down the
parameter hierarchy, these defaults will be used for all
nodes/edges in the graph.
nodes
:Named list of attributes specific to nodes.
edges
:Named list of attributes specific to edges.
graph
:Named list of graph-wide attributes.
Each item of nodes
and edges
can take arbitrary
vectors, the only restriction is that they have to be of either
length 1 or length equal to the number of nodes or edges,
respectively.
pars
and graph
can take arbitrary skalars, the latter
for both edges and nodes.
The following are functions rather than methods and build the API to
control the graphical output of a graph when it is plotted using
renderGraph
.
getter and setter for
items of slot pars
getter and setter for
items of slot nodes
getter and setter for
items of slot edges
getter and setter for
items of slot graph
The getters all take two arguments: g
is a graph object and
name
is a character giving the name of one of the item in the
respective slot. When name
is missing this will give you the
whole list.
The setters are a bit more complex: nodeRenderInfo<-
and
edgeRenderInfo<-
can take
where the names have to match the node
or edge names. Items in the vector that don't match a valid edge or
node name will be silently ignored. For undirected edges the order
of head nodes and tail nodes in edge names is ignored,
i.e. a~b
is equivalent to codeb~a
which will set all the attribute for all edges or nodes in the graph
parRenderInfo<-
will only take a list with items
nodes
, edges
and graph
. The content of these
list items can be arbitrary named vectors.
parRenderInfo<-
takes an arbitrary list
Available rendering parameters for nodes are:
the color of the line drawn as node border. Defaults to
black
.
the type of the line drawn as node border. Defaults to
solid
. Valid values are the same as for the R's base
graphic parameter lty
.
the width of the line drawn as node border. Defaults to
1
. Note that the underlying low level plotting functions do
not support vectorized lwd
values. Instead, only the first
item of the vector will be used.
the color used to fill a node. Defaults to
transparent
.
the font color used for the node labels. Defaults
to black
.
the font size for the node labels in
points. Defaults to 14
. Note that the fontsize will be
automatically adjusted to make sure that all labels fit their
respective nodes. You may want to increase the node size by
supplying the appropriate layout parameters to Graphviz
in order to allow for larger fontsizes.
Expansion factor to further control the fontsize. As default, this parameter is not set, in which case the fontsize will be clipped to the node size. This mainly exists to for consistency with the base graphic parameters and to override the clipping of fontsize to nodesize.
Available rendering parameters for edges are:
the color of the edge line. Defaults to black
.
the type of the edge line. Defaults to
solid
. Valid values are the same as for the R's base
graphic parameter lty
.
the width of the edge line. Defaults to 1
.
the font color used for the edge labels. Defaults
to black
.
the font size for the edge labels in
points. Defaults to 14
.
Expansion factor to further control the fontsize. This mainly exists to be consistent with the base graphic parameters.
Deepayan Sarkar, Florian Hahne
g <- randomGraph(letters[1:4], 1:3, p=0.8) nodeRenderInfo(g) <- list(fill=c("a"="red", "b"="green")) edgeRenderInfo(g) <- list(lwd=3) edgeRenderInfo(g) <- list(lty=3, col="red") parRenderInfo(g) <- list(edges=list(lwd=2, lty="dashed"), nodes=list(col="gray", fill="gray")) nodeRenderInfo(g) edgeRenderInfo(g, "lwd") edgeRenderInfo(g, c("lwd", "col")) parRenderInfo(g)
g <- randomGraph(letters[1:4], 1:3, p=0.8) nodeRenderInfo(g) <- list(fill=c("a"="red", "b"="green")) edgeRenderInfo(g) <- list(lwd=3) edgeRenderInfo(g) <- list(lty=3, col="red") parRenderInfo(g) <- list(edges=list(lwd=2, lty="dashed"), nodes=list(col="gray", fill="gray")) nodeRenderInfo(g) edgeRenderInfo(g, "lwd") edgeRenderInfo(g, c("lwd", "col")) parRenderInfo(g)
Return a new directed graph instance with each edge oriented in the opposite direction relative to the corresponding edge in the input graph.
reverseEdgeDirections(g)
reverseEdgeDirections(g)
g |
A |
WARNING: this doesn't handle edge attributes properly. It is a preliminary implementation and subject to change.
A graphNEL
instance
S. Falcon
g <- graphNEL(nodes=c("a", "b", "c"), edgeL=list(a=c("b", "c"), b=character(0), c=character(0)), edgemode="directed") stopifnot(isAdjacent(g, "a", "b")) stopifnot(!isAdjacent(g, "b", "a")) grev <- reverseEdgeDirections(g) stopifnot(!isAdjacent(grev, "a", "b")) stopifnot(isAdjacent(grev, "b", "a"))
g <- graphNEL(nodes=c("a", "b", "c"), edgeL=list(a=c("b", "c"), b=character(0), c=character(0)), edgemode="directed") stopifnot(isAdjacent(g, "a", "b")) stopifnot(!isAdjacent(g, "b", "a")) grev <- reverseEdgeDirections(g) stopifnot(!isAdjacent(grev, "a", "b")) stopifnot(isAdjacent(grev, "b", "a"))
A simple class for representing edges in graphs.
Objects can be created by calls of the form new("simpleEdge", ...)
.
edgeType
:Object of class "character"
; the type
of the edge.
weight
:Object of class "numeric"
; the edge weight.
directed
:Object of class "logical"
; is the
edge directed.
bNode
:Object of class "character"
; the
beginning node.
eNode
:Object of class "character"
; the ending node.
No methods defined with class "simpleEdge" in the signature.
All slots are length one vectors (this is not currently checked for). If the edge is not directed there is no real meaning to the concepts of beginning node or ending node and these should not be interpreted as such.
R. Gentleman
new("simpleEdge", bNode="A", eNode="D")
new("simpleEdge", bNode="A", eNode="D")
Functions to convert between from-to representation and standard labeling of the edges for undirected graphs with no self-loops.
ftM2int(ft) int2ftM(i)
ftM2int(ft) int2ftM(i)
i |
Numeric vector. |
ft |
Numeric nx2 or 2xn matrix. |
A standard 1-based node labeling of a graph G=(V,E) is a one-to-one mapping between the integers from 1 to |V| and the nodes in V. A standard 1-based edge labeling of an undirected graph G=(V,E) with no self-loops is the one-to-one mapping between the integers from 1 to |V| choose 2 = |V|*(|V|-1)/2 such that the edge labeled 1 is between nodes 2 and 1, the edge labeled 2 is between nodes 3 and 1, the edge labeled 3 is between nodes 3 and 2, and so on.
For ftM2int
, a numeric vector of length n.
For int2ftM
, a length(i) x 2
matrix.
Wolfgang Huber
nNodes <- 200 nEdges <- choose(nNodes, 2) i <- 1:nEdges ft <- int2ftM(i) ft[1:6,] stopifnot(all(ft[,1]>ft[,2])) ## always from higher to lower stopifnot(!any(duplicated(paste(ft[,1], ft[,2])))) stopifnot(ft[nEdges, 1]==nNodes, ft[nEdges, 2]==nNodes-1) j <- ftM2int(ft) stopifnot(all(i==j))
nNodes <- 200 nEdges <- choose(nNodes, 2) i <- 1:nEdges ft <- int2ftM(i) ft[1:6,] stopifnot(all(ft[,1]>ft[,2])) ## always from higher to lower stopifnot(!any(duplicated(paste(ft[,1], ft[,2])))) stopifnot(ft[nEdges, 1]==nNodes, ft[nEdges, 2]==nNodes-1) j <- ftM2int(ft) stopifnot(all(i==j))
Given a set of nodes and a graph this function creates and returns subgraph with only the supplied nodes and any edges between them.
subGraph(snodes, graph)
subGraph(snodes, graph)
snodes |
A |
graph |
A graph object, it must inherit from the |
The returned subgraph is a copy of the graph. Implementations for
Implementations for graphNEL
, distGraph
and
clusterGraph
.
A graph of the same class as the graph
argument but with only
the supplied nodes.
R. Gentleman
set.seed(123) x <- rnorm(26) names(x) <- letters library(stats) d1 <- dist(x) g1 <- new("distGraph", Dist=d1) subGraph(letters[1:5], g1)
set.seed(123) x <- rnorm(26) names(x) <- letters library(stats) d1 <- dist(x) g1 <- new("distGraph", Dist=d1) subGraph(letters[1:5], g1)
There are two basic methods of generating
dot (http://www.graphviz.org) language serializations
of R graph-class
structures. First,
using the toDot
methods of the
Rgraphviz package, the native graphviz agraph-associated methods can be
employed to create the dot serialization.
Second, with the methods described here, R functions can
be used to perform the serialization directly from
the graph data structure, without Rgraphviz.
create dot language descriptionof graph
create dot language descriptionof graph
create dot language descriptionof graph
create dot language descriptionof graph
create dot language descriptionof graph
create dot language descriptionof graph
create dot language descriptionof graph
create dot language descriptionof graph
create dot language descriptionof graph
create dot language descriptionof graph
create dot language descriptionof graph
example(randomGraph) tmp <- tempfile() toDotR( g1, tmp ) readLines(tmp) unlink(tmp)
example(randomGraph) tmp <- tempfile() toDotR( g1, tmp ) readLines(tmp) unlink(tmp)
The function takes a graph object and translates it into the dot format. All rendering information is written verbatim into the dot graph as well
toDotWithRI(graph, graph_name = NULL, subGraphList = list(), isStrict = TRUE)
toDotWithRI(graph, graph_name = NULL, subGraphList = list(), isStrict = TRUE)
graph |
An object of graph |
graph_name |
The name of the graph |
subGraphList |
A list of objects of class |
isStrict |
Should the graph be strict |
Given a graph object, it is translated into the dot
language so
that it can be rendered using the graphviz
software. In
addition to plotting the graph itself, all the rendering information
is being used as well.
graphRenderInfo
attributes are written as an attribute list
after the graph
statement in dot.
nodeRendenInfo
attributes are written as attribute lists after
each node. If an attribute is constant across all node, a global node
attribute is written instead of many individual ones.##' Newlines ##'
in attributes do not lead to newlines in labels. In label
,
headlabel
and taillabel
, in order to get a newline,
right justification or left justification, the two character sequences
\n
, \r
and \l
have to be written (i.e. in
order to create this in R, the backslash has to be escaped in a
string, that is has to be written as a double-backslash).
edgeRenderInfo
attributes as written as attribute lists after
each edge, unless an attribute is constant, then it is written as a
global edge attribute.
In general, all attribute values are being wrapped in double-quotes,
unless the attibute value start with a <
and ends with a
>
. In this case it is taken as html content and not wrapped in
double quotes (nor are contained newlines escaped).
The resulting graph in dot format is returned as a character vector.
A character vector with the graph in dot format
Holger Hoefling [email protected]
For a directed graph the underlying graph is the graph that is
constructed where all edge orientation is ignored. This function
carries out such a transformation on graphNEL
instances.
ugraph(graph)
ugraph(graph)
graph |
a |
If graph
is already undirected then it is simply
returned.
If graph
is a multi-graph (has multiple edges) an error is
thrown as it is unclear how to compute the underlying graph in that
context.
The method will work for any graph
subclass for which an
edgeMatrix
method exists.
An instance of graphNEL
with the same nodes as the input but
which is undirected
.
R. Gentleman
Graph Theory and its Applications, J. Gross and J. Yellen.
V <- letters[1:4] edL2 <- vector("list", length=4) names(edL2) <- V for(i in 1:4) edL2[[i]] <- list(edges=c(2,1,2,1)[i], weights=sqrt(i)) gR2 <- graphNEL(nodes=V, edgeL=edL2, edgemode="directed") ugraph(gR2)
V <- letters[1:4] edL2 <- vector("list", length=4) names(edL2) <- V for(i in 1:4) edL2[[i]] <- list(edges=c(2,1,2,1)[i], weights=sqrt(i)) gR2 <- graphNEL(nodes=V, edgeL=edL2, edgemode="directed") ugraph(gR2)
validGraph is a validating function for a graph object.
validGraph(object, quietly=FALSE)
validGraph(object, quietly=FALSE)
object |
a graph object to be tested |
quietly |
|
If the graph object is valid, TRUE
is returned otherwise
FALSE
is returned. If object
is not a valid graph and
quietly
is set to FALSE
then descriptions of the problems
are printed.
Elizabeth Whalen
testGraph<-graphNEL() testGraph@nodes<-c("node1","node2","node3") validGraph(testGraph)
testGraph<-graphNEL() testGraph@nodes<-c("node1","node2","node3") validGraph(testGraph)
Write a graph object in a file in the Tulip format.
write.tlp(graph, filename)
write.tlp(graph, filename)
graph |
a |
filename |
Name of the output file |
The Tulip format is used by the program Tulip.
Laurent Gautier <[email protected]>
http://www.tulip-software.org/