File: graphAttributes.Rmd

package info (click to toggle)
r-bioc-graph 1.84.1-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,820 kB
  • sloc: ansic: 842; makefile: 12
file content (185 lines) | stat: -rw-r--r-- 5,541 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
---
title: "Attributes for Graph Objects" 
author: 
- name: "Seth Falcon"
- name: "Paul Villafuerte"
  affiliation: "Vignette translation from Sweave to Rmarkdown / HTML"
date: "`r format(Sys.time(), '%B %d, %Y')`" 
vignette: > 
  %\VignetteIndexEntry{Attributes for Graph Objects}
  %\VignetteDepends{graph}
  %\VignetteKeywords{Graph}
  %\VignettePackage{graph}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
output:
  BiocStyle::html_document:
    number_sections: true
    toc: yes
    toc_depth: 4
---

# Introduction

The `r Biocpkg('graph')` package provides representations of graphs (nodes and edges)
as S4 classes. This vignette demonstrates how to add arbitrary node and
edge attributes to graph objects.

First, we create a graph to use as an example. We will work with a
*graphAM-class* instance, however, any subclass of *graph-class* would
work. See FigureĀ [1](#foo){reference-type="ref" reference="foo"}.

```{r exampleGraph1, message=FALSE, results='hide'}
library("graph")
mat <- matrix(c(0, 0, 1, 1, 
                0, 0, 1, 1, 
                1, 1, 0, 1, 
                1, 1, 1, 0),
              byrow=TRUE, ncol=4)
rownames(mat) <- letters[1:4]
colnames(mat) <- letters[1:4]
```

```{r exampleGraph2}
(g1 <- graphAM(adjMat=mat))
```

```{r foo, fig.cap="The graph `g1`.", fig.height=6, fig.small=TRUE, fig.width=6, echo=FALSE, message=FALSE, out.extra='id="foo"'}
if (require("Rgraphviz")) {
   gn = as(g1, "graphNEL")
   plot(gn, nodeAttrs=makeNodeAttrs(gn, shape="circle", fillcolor="orange"))
} else {
 plot(1, 1, main="Rgraphviz required for this plot")
}
```

# Edge Attributes

## Default edge attributes

All edges in a graph support the same set of attributes. The set of
supported attributes can be defined and accessed using the
`edgeDataDefaults` method. A new graph instance will not have any edge
attributes defined.

```{r edgeDataDefaults1}
edgeDataDefaults(g1)
```

When a new edge attribute is defined, a default value must be specified.
Here we will define two edge attributes: `weight` and `code` and specify
a default value for each one.

```{r edgeDataDefaults2}
edgeDataDefaults(g1, "weight") <- 1
edgeDataDefaults(g1, "code") <- "plain"
edgeDataDefaults(g1)
```

The default value for a particular attribute can be obtained by
specifying the attribute name in the call to `edgeDataDefaults`.

```{r edgeDataDefaults3}
edgeDataDefaults(g1, "weight")
```

## Getting edge attributes

Edge attributes are set and accessed using the `edgeData` method. Only
attributes defined using `edgeDataDefaults` can be accessed using
`edgeData`. If an attribute has not be set using `edgeData` for a given
edge, then the default value is used.

```{r edgeData1}
edgeData(g1, from="a", to="d", attr="weight")
edgeData(g1, from="a", attr="weight")
edgeData(g1, to="a", attr="weight")
allAttrsAllEdges <- edgeData(g1)
weightAttrAllEdges <- edgeData(g1, attr="weight")
```

## Setting edge attributes

Attributes are set using the replacement form of `edgeData`.
This method allows the user to update the attribute for single edge,
set the attributes for a collection of edges to a single value, and to
set the attributes for a collection of edges to different values
specified by a vector of values.

```{r edgeData2}
edgeData(g1, from="a", to="d", attr="weight") <- 2
edgeData(g1, from="a", attr="code") <- "fancy"
edgeData(g1, from="a", attr="weight")
edgeData(g1, from="a", attr="code")
```

We can set the attributes for multiple edges to a single value.

```{r edgeData3}
f <- c("a", "b")
t <- c("c", "c")
edgeData(g1, from=f, to=t, attr="weight") <- 10
edgeData(g1, from=f, to=t, attr="weight")
```

It is also possible to set multiple attributes to different values in
a single call to `edgeData`.

```{r edgeData4}
edgeData(g1, from=f, to=t, attr="weight") <- c(11, 22)
edgeData(g1, from=f, to=t, attr="weight")
```

Finally, we can set the an attribute to a vector of values by packing
it into a list:

```{r edgeData5}
edgeData(g1, from="a", to="d", attr="code") <- list(1:10)
edgeData(g1, from=f, to=t, attr="weight") <- mapply(c, f, t, "e", SIMPLIFY=FALSE) 
edgeData(g1, from="a", to="d", attr="code")
edgeData(g1, from=f, to=t, attr="weight")
```

# Node Attributes

## Default node attributes

Like edge attributes, all nodes in a graph support the same set of
attributes. The supported set of attributes and their default values is
accessed using the `nodeDataDefaults` method. The interface is similar
to `edgeDataDefaults`.

```{r defaultNodeData1}
nodeDataDefaults(g1)
nodeDataDefaults(g1, attr="weight") <- 1
nodeDataDefaults(g1, attr="type") <- "vital"
nodeDataDefaults(g1)
nodeDataDefaults(g1, "weight")
```

As with edge attributes, default values are required for each node
attribute. The default value is used as the node attribute for all nodes
in the graph that have not had their attribute value explicitly set.
Attribute values can be any R object.

## Getting and setting node attributes

Once a node attribute has been defined and given a default value using
`nodeDataDefaults`, individual node attributes can be accessed using
`nodeData`.

```{r nodeData1}
nodeData(g1, n="a")
nodeData(g1, n="a", attr="weight") <- 100
nodeData(g1, n=c("a", "b"), attr="weight")
nodeData(g1, n=c("a", "b"), attr="weight") <- 500
nodeData(g1, n=c("a", "b"), attr="weight")
nodeData(g1, n=c("a", "b"), attr="weight") <- c(11, 22)
nodeData(g1, n=c("a", "b"), attr="weight")
```

```{r other, echo=FALSE}
## We need to reconcile this
#g2 <- as(g1, "graphNEL")
#edgeWeights(g2)
```