/
03_mae.Rmd
195 lines (143 loc) · 4.44 KB
/
03_mae.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
---
title: "Bleomycin cross-sectional data formatting"
author: Sergio Picart-Armada
date: "20th May, 2019"
output:
html_document:
toc: TRUE
toc_float: true
code_folding: hide
df_print: paged
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, message = FALSE, error = FALSE, warning = FALSE)
```
## Loading the data
```{r cars}
library(plyr)
library(dplyr)
library(tidyr)
library(magrittr)
library(SummarizedExperiment)
library(MultiAssayExperiment)
library(ggplot2)
library(pcaMethods)
library(data.table)
library(UpSetR)
config <- new.env()
source("config.R", local = config)
# load all the omics data
env.se <- new.env()
local({
Metabolites <- readRDS(config$file.metab.se)
Lipids <- readRDS(config$file.lipid.se)
}, env.se)
theme_set(theme_bw())
```
```{r}
# Load the phenotypes
```
## Defining a `MultiAssayExperiment` object
From their vignette, the `MultiAssayExperiment` constructor function can take three arguments:
1. experiments - An ExperimentList or list of data
2. colData - A DataFrame describing the patients (or cell lines, or other biological units)
3. sampleMap - A DataFrame of assay, primary, and colname identifiers
### Experiments
They are already available from the original `rds` files.
### sampleMap: add the same `AnimalID` column to every `colData`
```{r}
with(env.se, {
# browser()
colnames(Metabolites) <- colData(Metabolites)$AnimalID <- paste0(config$id.prefix,
colData(Metabolites)$ANIMAL_ID)
# rownames(Metabolites) <- rowData(Metabolites)$BIOCHEMICAL
colnames(Lipids) <- colData(Lipids)$AnimalID <- paste0(config$id.prefix,
colData(Lipids)$ANIMAL_ID)
# rownames(Lipids) <- rowData(Lipids)$BIOCHEMICAL
})
df.map <- lapply(env.se, function(se) {
cd <- colData(se)
data.frame(primary = cd$AnimalID,
colname = rownames(cd),
stringsAsFactors = FALSE)
}) %>% listToMap
```
Seems like `MultiAssayExperiment` will only take the first assay in every `SummarizedExperiment` object.
Therefore, we must ensure we are picking the preprocessed ones.
```{r}
sapply(env.se, function(se) length(assays(se)))
assays(env.se$Metabolites)
```
In the metabolomics assays, we will get rid of the `raw` data.
```{r}
with(env.se, {
assays(Metabolites)$metab.lung.raw <- NULL
})
```
### colData: clean and add the phenotypes
### Build the `MultiAssayExperiment` object
Check sample mappings
```{r}
all(colnames(env.se$Lipids) %in% df.map$colname)
all(colnames(env.se$Metabolites) %in% df.map$colname)
all(colData(env.se$Lipids)$AnimalID %in% df.map$primary)
all(colData(env.se$Metabolites)$AnimalID %in% df.map$primary)
all(rownames(df.pheno) %in% df.map$primary)
```
```{r}
setdiff(rownames(df.pheno), df.map$primary)
```
```{r}
# mae <- MultiAssayExperiment(experiments = ExperimentList(as.list(env.se)))
# sampleMap(mae)$primary <- df.map$primary
# # harmonizing input:
# # removing 56 sampleMap rows with 'primary' not in colData
# # removing 56 colData rownames not in sampleMap 'primary'
#
#
# mae <- MultiAssayExperiment(experiments = ExperimentList(as.list(env.se)), colData = df.pheno)
```
Would the `BIOCHEMICAL` column give proper rownames?
`make.names()` will add periods instead of whitespaces, not ideal...
```{r}
setdiff(make.names(rowData(env.se$Lipids)$BIOCHEMICAL), rowData(env.se$Lipids)$BIOCHEMICAL) %>% head
```
```{r}
# had painful issues with the samples mapping correctly
# specifically, intersectColumns(mae) giving 0 columns
# and the mae having 0 columns in both SEs
#
# Solution: convert df.pheno to data.frame again and set rownames (it was a DFrame)
mae <- MultiAssayExperiment(experiments = ExperimentList(as.list(env.se)),
colData = df.pheno,
sampleMap = df.map)
mae
```
## Basic inspection of the `MultiAssayExperiment` object
As expected, metabolomics has one less sample (28) than transcriptomics and proteomics (29).
Indeed, the 28 samples are common across the datasets.
```{r}
upsetSamples(mae)
```
```{r}
assays(mae)
```
What does the `colData` look like in each omic study?
```{r}
lapply(experiments(mae), function(se) dim(colData(se)))
```
And the `rowData`?
```{r}
lapply(experiments(mae), function(se) dim(rowData(se)))
```
```{r}
if (!dir.exists(config$dir.mae)) dir.create(config$dir.mae)
saveRDS(mae, file = config$out.mae, compress = "xz")
```
## Reproducibility
```{r}
date()
```
```{r}
sessionInfo()
```