-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathCDSE2019.Rmd
443 lines (323 loc) · 9.04 KB
/
CDSE2019.Rmd
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
---
title: "Bioconductor for Everyone: Exploring, Analyzing, and Visualizing Large Data Sets with R"
author:
- name: Martin Morgan
affiliation: Roswell Park Comprehensive Cancer Center
output:
BiocStyle::html_document
abstract: |
We'll take a fast-paced tour through R and the software project I
work on, Bioconductor (https://bioconductor.org), learning how to
explore and visualize large cancer-related data sets. We'll work
through two particular analyses. In the process, we'll learn some
pretty significant new R skills. For instance, we will learn about
formal classes for representing complex data, strategies for
iteration and parallel processing, and accessing 'remote' resources
accessible through web-based interfaces. This workshop should be
interesting to people who know a bit of R, and want to learn more!
vignette: |
%\VignetteIndexEntry{Bioconductor for Everyone}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r style, echo = FALSE, results = 'asis'}
knitr::opts_chunk$set(
eval=as.logical(Sys.getenv("KNITR_EVAL", "TRUE")),
cache=as.logical(Sys.getenv("KNITR_CACHE", "TRUE"))
)
```
# Biology
Gene expression
RNA-seq
Single-cell technologies
# Large data
## What and how
What is it?
- Doesn't fit in memory
- Computationally expensive to process
Common use cases
- Query -- in this haystack, where is my needle
- Processing -- e.g., reduction to summary statistics
## Strategies
Queries
- e.g., of data bases
Processing
- Iteration & chunk-wise
- Distributed / parallel
# Real-world: objects
## Objects
We'll set up some data to use (pay no attention to this!)
```{r setup-airway, message=FALSE}
dir <- tempdir()
if (!dir.exists(dir))
dir.create(dir)
library(airway)
data(airway)
write.table(colData(airway), file.path(dir, "samples.csv"))
write.table(assay(airway), file.path(dir, "expression.csv"))
```
Related data elements
```{r}
samples <- read.table(file.path(dir, "samples.csv"))
samples
counts <- read.table(file.path(dir, "expression.csv"))
head(counts)
counts <- as.matrix(counts)
```
Coordinate data management
- separately 'managing' `samples` and `counts` is error-prone, e.g. subset
`samples` but not `counts`, and the association of sample rows and count
columns is distrupted.
```{r, message=FALSE}
library(SummarizedExperiment)
SummarizedExperiment()
```
An 'S4' object for coordinating 'assay' matrices with row and column annotations
```{r}
se <- SummarizedExperiment(
assays = list(counts = counts),
colData = samples
)
se
```
Classes allow the developer to make data access easy
- e.g., matrix-like 'interface'
- Accessors, so internal representation can be chosen for efficiency while
user interface remains easy to use
```{r}
se$dex
se[, se$dex == "trt"]
```
Data manipulation, e.g., non-zero rows
```{r}
idx <- rowSums(assay(se)) > 0
se[idx,]
```
Simple visualize
```{r}
dotchart(colSums(assay(se)), xlab = "Library size")
```
```{r}
expr <- rowSums(assay(se))
plot(density(log(expr[expr > 0])), ylab = "log expression")
```
# Tidy data and the tidyverse
Not 'better', but different
Challenges in base R and formal objects
- Many different types -- vector, data.frame, matrix, SummarizedExperiment --
all with different operations
- Each function is somehow different, e.g., `[` applied to a matrix usually (!)
returns a matrix, whereas `rowSums()` returns a vector
'tidy' analysis
- Consistent representation of data -- data.frame
- Consistent methods
- First argument is always 'the data',
- Tidy functions are always 'endomorphisms' -- the class of the input data is
the same as the class of the result
- Only a few standard 'verbs' -- `filter()`, `select()`, `group_by()`,
`count()`, `summarize()`, `mutate()`, ...
```{r, message = FALSE}
library(dplyr)
library(tibble)
```
The pipe, `%>%`
- Base R: reasoning 'inside out'
```{r}
x <- runif(10, 1, 5)
log(ceiling(x))
```
- Procedural R
```
x1 <- ceiling(x)
log(x1)
```
- Tidy R -- use `%>%` so that operations read left-to-right
```
x %>% ceiling() %>% log()
```
Data representation
- A `tibble` is like a user-friendly `data.frame`
```{r}
as_tibble(mtcars)
```
- Row names are just another column
```{r}
sample <- tibble::rownames_to_column(
as.data.frame(colData(se)),
var="Accession"
) %>% as_tibble()
```
- Data is represented in 'long-form'
```{r}
count <- reshape::melt(assay(se), c("Feature", "Accession")) %>%
as_tibble()
colnames(count)[3] = "Count"
count
```
Endomorphism: tibble in, tibble out
```{r}
cars <- rownames_to_column(mtcars, "make") %>%
as_tibble()
cars %>% filter(cyl >= 6)
cars %>% select(make, mpg, cyl, disp)
```
Generally, playing well with other packages in the 'tidyverse'
```{r setup-ggplot2, message=FALSE}
library(ggplot2)
```
```{r}
cars %>% ggplot(aes(x = factor(cyl), y = mpg)) + geom_boxplot()
```
# Data base
## Data base representation
Create a sqlite data base
```{r setup-RSQLite, message = FALSE}
library(RSQLite)
airway_db <- file.path(dir, "airway.sqlite")
con <- dbConnect(SQLite(), airway_db)
```
Add a sample table
```{r}
sample <- tibble::rownames_to_column(
as.data.frame(colData(se)),
var="Accession"
)
dbWriteTable(con, "sample", sample)
```
Add a count table, in 'tidy' form
```{r}
count <- reshape::melt(assay(se), c("Feature", "Accession"))
colnames(count)[3] = "Count"
dbWriteTable(con, "count", count)
```
Extract data using SQL statements
```{r}
dbListTables(con)
dbGetQuery(con, "SELECT * FROM Sample;")
dbGetQuery(con, "SELECT Accession, cell, dex FROM Sample;")
dbGetQuery(con, "SELECT * FROM Count LIMIT 3;")
dbDisconnect(con)
```
## dbplyr
Open data base
```{r, message = FALSE}
library(dplyr)
library(dbplyr)
src <- src_sqlite(airway_db)
src
```
Manipulations on "Sample" table -- standard verbs, plus `collect()`
```{r}
tbl(src, "Sample")
tbl(src, "Sample") %>% select(Accession, cell, dex)
tbl(src, "Sample") %>% filter(dex == "trt") %>% collect()
```
Manipulations on "Count" table
```{r}
tbl(src, "Count")
tbl(src, "Count") %>%
group_by(Accession) %>%
summarize(library_size = SUM(Count)) %>%
collect()
```
Relations between tables
```{r}
left_join(tbl(src, "Count"), tbl(src, "Sample"))
left_join(
tbl(src, "Count"),
tbl(src, "Sample") %>% select(Accession, cell, dex)
)
```
Library size
- Find column (Accession) counts
```{r}
tbl(src, "Count") %>%
group_by(Accession) %>%
summarize(library_size = SUM(Count))
```
Filter rows with non-zero counts
- Rows with non-zero counts
```{r}
keep <- tbl(src, "Count") %>%
group_by(Feature) %>%
summarize(row_sum = SUM(Count)) %>%
filter(row_sum > 0) %>%
select(Feature)
```
- `left_join()` to keep only these rows
```{r}
left_join(keep, tbl(src, "Count"))
```
## Aside: SRAdb
```{r setup-SRAdb, message = FALSE, eval = FALSE}
library(BiocFileCache)
if (nrow(bfcquery(query="SRAdb", field = "rname")) == 0L) {
fl <- SRAdb::getSRAdbFile(tempdir())
bfcadd(rname = "SRAdb", fpath = fl, action = "move")
}
```
```{r, eval = FALSE}
fl <- BiocFileCache::bfcrpath(rnames = "SRAdb")
src <- src_sqlite(fl)
tbl(src, "study")
tbl(src, "study") %>%
filter(study_title %like% "%ovarian%")
```
# Other on-disk or remote representations
Data bases are appropriate for 'relational' data.
The 'big' part of scientific data is often not relational
- E.g., an expression _matrix_
Access patterns for databases adn scientific data often differ.
- database: query
- scientific data: process all data
Strategy for processing data: iterate through the file
- In python, other languages: iterate one record (e.g., sample) at a time.
- In R: iterate in chunks to allow vector processing.
## hdf5
```{r setup-rhdf5, message = FALSE}
library(rhdf5)
```
Fast 'block-wise' access
## TENxBrainData
```{r setup-TENxBrainData, message = FALSE}
library(TENxBrainData)
tenx <- TENxBrainData()
```
Illusions...
```{r}
log(1 + assay(tenx))
```
Subset
```{r}
tenx_subset <- tenx[, sample(ncol(tenx), 200)]
count <- as.matrix(assay(tenx_subset))
dotchart(
unname(colSums(count)),
xlab = "Library size"
)
hist(log(1 + rowSums(count)))
```
Actually, though, chunk-wise data processing is transparent
```{r}
dotchart(
unname(colSums(assay(tenx_subset))),
xlab = "Library size"
)
```
## restfulSE
# End matter
## Acknowledgements
A portion of this work is supported by the Chan Zuckerberg Initiative
DAF, an advised fund of Silicon Valley Community Foundation.
Research reported in this presentation was supported by the NHGRI and
NCI of the National Institutes of Health under award numbers
U41HG004059, U24CA180996, and U24CA232979. The content is solely the
responsibility of the authors and does not necessarily represent the
official views of the National Institutes of Health.
This work was performed on behalf of the SOUND Consortium and funded
under the EU H2020 Personalizing Health and Care Program, Action
contract number 633974.
## Session info {.unnumbered}
```{r sessionInfo, echo=FALSE}
sessionInfo()
```