-
Notifications
You must be signed in to change notification settings - Fork 0
/
mae_project.Rmd
420 lines (345 loc) · 12.3 KB
/
mae_project.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
---
title: "mae_demo"
output: pdf_document
date: '2023-02-10'
---
```{r echo=TRUE}
library(MultiAssayExperiment)
library(genefilter)
library(gplots)
library(mogsa)
library(tidyverse)
library(patchwork)
library(stats)
library(knitr)
# MOGSA Analysis
library(mogsa)
# plotting
library(UpSetR)
library(igraph)
library(ggplot2)
library(gplots)
# survival analysis
library(survival)
library(survminer)
library(SingleCellExperiment)
```
Explore two major analysis techniques, one is mogsa (multi omics data integrative clustering and gene set analysis), the other one is mofa (multi omics factor analysis)
```{r}
data(miniACC)
data <- as.data.frame(colData(miniACC))
# each row is an observation (patient)
bio_info <- mergeReplicates(as.data.frame(wideFormat(miniACC)))
bio_info <- drop_na(bio_info) # 43 patients remaining
```
# Check Baseline Characteristics
```{r}
dead <- data[data$vital_status==1, ]
survived <- data[data$vital_status==0, ]
Mean.IQR.by.trt <- function(y,trt,decp=1){
groups <- sort(unique(trt))
all <- quantile(y)
g1 <- quantile(y[trt==groups[1]]) # indicates hormone == 1
g2 <- quantile(y[trt==groups[2]]) # indicates hormone == 2
result <- matrix(NA,1,3)
colnames(result) <- c(groups,"Overall")
# g1[2]: 25% quantile, g1[3]: median (50% quantile), g1[4]: 75% quantile
result[1,1] <- paste0(round(g1[3],decp)," (",round(g1[2],decp),", ",round(g1[4],decp),")")
result[1,2] <- paste0(round(g2[3],decp)," (",round(g2[2],decp),", ",round(g2[4],decp),")")
result[1,3] <- paste0(round(all[3],decp)," (",round(all[2],decp),", ",round(all[4],decp),")")
return(result)
}
N.prct.by.trt <- function(x,trt,decp=1){
groups <- sort(unique(trt))
x.levels <- sort(unique(x))
p <- length(x.levels)
n <- length(x)
n1 <- length(x[trt==groups[1]]) # total number of patients have no hormone
n2 <- length(x[trt==groups[2]]) # total number of patients have hormone
result <- matrix(NA,p,3)
colnames(result) <- c(groups,"Overall")
rownames(result) <- x.levels
for (i in 1:p){
# calculate the total number of patients with non-hormone on each level
n1i <- sum(x[trt==groups[1]]==x.levels[i])
# calculate the total number of patients with hormone on each level
n2i <- sum(x[trt==groups[2]]==x.levels[i])
ni <- sum(x==x.levels[i])
result[i,1] <- paste0(n1i," (",round(n1i/n1*100,decp),"%)")
result[i,2] <- paste0(n2i," (",round(n2i/n2*100,decp),"%)")
result[i,3] <- paste0(ni," (",round(ni/n*100,decp),"%)")
}
return(result)
}
table1 <- rbind(
Mean.IQR.by.trt(data$years_to_birth, data$vital_status), # age
N.prct.by.trt(data$gender, data$vital_status), #gender
N.prct.by.trt(data$pathologic_stage[complete.cases(data$pathologic_stage)], data$vital_status[complete.cases(data$pathologic_stage)]),
Mean.IQR.by.trt(data$date_of_initial_pathologic_diagnosis, data$vital_status)
)
row.names(table1)<-c('Age', 'Female', 'Male', 'Stage I', 'Stage II', 'Stage III', 'Stage IV', 'Date of Initial Pathologic Diagnosis')
colnames(table1) <- c('Survived', 'Dead', 'Overall')
knitr::kable(table1,caption="Adrenocortical Carcinoma Baseline Characteristics")
```
# Survival Analysis
```{r}
data$y <- Surv(data$days_to_death, data$vital_status)
# Kaplan - Meier Curve
KMfit <- survfit(y ~ pathologic_stage, data = data)
ggsurvplot(
KMfit,
cex.axis=1,
cex.lab=1,
cex.main=1,
lwd=2,
risk.table = F,
data = data,
# x = 'Days to Death',
# title = 'Kaplan Meier Curves Based on Pathologic Stages'
) +
labs(
x = 'Days to Death',
title = 'Kaplan Meier Curves Based on Pathologic Stages'
)
```
# Estimating correlation between RNA & Copy Number
```{r}
# For the gene with highest correlation to copy number, make a box plot of log2 expression against copy number.
# correlation between RNASeq2GeneNorm and copy number
subacc <- miniACC[, , c('RNASeq2GeneNorm', 'gistict')]
subacc.list <- assays(subacc)
# log transformation of the RNA-seq assay
# +1 avoids taking log of zeroes
subacc.list[[1]] <- log2(subacc.list[[1]] + 1)
corres <- cor(subacc.list[[1]], subacc.list[[2]])
hist(corres)
cat(which.max(diag(corres)))
# represent copy numer CHEK2
```
```{r}
df <- wideFormat(subacc["CHEK2", , ])
head(df)
boxplot(RNASeq2GeneNorm_CHEK2 ~ gistict_CHEK2,
data=df, varwidth=TRUE,
xlab="GISTIC Relative Copy Number Call",
ylab="RNA-seq counts",
main='CHEK2')
```
# Prepare for Mogsa
```{r}
# return those biological units (here are patients) that have measurements across all experiments
mae <- intersectColumns(miniACC[, , c('RNASeq2GeneNorm', 'gistict', 'Mutations')])
```
# Mogsa Analysis
```{r}
# Create support data
set.seed(123)
gene_names <- sample(rownames(mae[[1]]))
# every 10 genes belong to one cluster hypothetically, the last group contains only 8 genes
cluster.names <- c()
for (i in 1:20) {
n <- paste0('gene.cluster.', i)
cluster.names <- append(cluster.names, n)
}
# Create clustered gene sets, a list containing 10 vectors
gene_set <- list()
for (i in 1:20) {
vec <- c()
if (length(gene_names) >= 10){
vec <- list(gene_names[1:10])
} else {
vec <- list(gene_names[1:length(gene_names)])
}
gene_names <- gene_names[11:length(gene_names)]
gene_set <- append(gene_set, vec)
}
# main data for mogsa
gene <- list()
gene$rna <- assay(mae[[1]])
gene$gistict <- assay(mae[[2]])
gene$mutation <- assay(mae[[3]])
# create a supp matrix, row is gene names, column is the gene set
# 1 means this gene belongs to this gene set
# Create 3 different matrix, where the number of rows of each matrix corresponds to the number of rows of each assay info in mae
container <- list()
for (i in 1:3) {
mat <- matrix(0, nrow=nrow(gene[[i]]), ncol=length(cluster.names))
colnames(mat) <- cluster.names
rownames(mat) <- rownames(gene[[i]])
for (r in 1:nrow(mat)) {
for (c in 1:ncol(mat)) {
if (rownames(mat)[r] %in% gene_set[[c]]) {
mat[r, c] <- 1
}
}
}
container <- append(container, list(mat))
}
supp <- list()
supp$rna <- container[[1]]
supp$gistict <- container[[2]]
supp$mutation <- container[[3]]
mgsa1 <- mogsa(x=gene, sup=supp, nf=10, proc.row = "center_ssq1", w.data = "inertia", statis = TRUE)
```
```{r}
# The variance of each principal components (PC)
eigs <- getmgsa(mgsa1, "partial.eig") # get partial "eigenvalue" for separate data
barplot(as.matrix(eigs), legend.text = rownames(eigs))
# not sure why there are 75 principal components
```
```{r}
scores <- getmgsa(mgsa1, "score") # get the gene set sample pathway scores
heatmap.2(scores, trace = "none", scale='r', Rowv=NA, Colv=T,margins=c(12,12))
# find the cluster with the maxmimal score
max.score <- names(which(rowSums(scores)==max(rowSums(scores))))
max.score
# cluster 4 has the highest gene set pathway scores
```
(Remember to Revise)It is also interesting to look into more detailed information for a specific gene set. For example, which dataset(s) contribute most to the high or low gene set score of a gene set?
And which genes are most important in defining the gene set score for a gene set? The former
question could be answered by the gene set score decomposition; the later question could
be solve by the gene influential score.
```{r}
### Part 1 Find most significant gene set(cluster) by estimating the #of significant p-values ###
p.mat <- getmgsa(mgsa1, "p.val") # get p value matrix
# select gene sets with most signficant GSS scores.
# by summing up the number of each gene's p value < 0.01 across all patients
top.gs <- sort(rowSums(p.mat < 0.01), decreasing = TRUE) # total length is 20 bc of there are only 20 clusters
top.gs.name <- names(top.gs)
# gene.cluster.16 is the most significant one
# gene_set[[16]] "PXN" "MAPK1" "XBP1" "RAB11B" "EGFR" "PGR" "BCL2" "SMAD4" "MAPK14" "TYMS"
gs1 <- top.gs.name[1] # select the most significant gene set
gs1
### Part 2 Find the genes are positive correlated with gene set scores ###
# gistict (copy number) & rna dataset has slightly higher influential scores
# The expression of genes with high positive GIS more likely to have a good positive correlation with the gene set score.
gis1 <- GIS(mgsa1, gs1)
head(gis1) # first 6 genes highly positively correlated with gene set scores
```
# Multi-Omics Factor Analysis
```{r}
library(MOFA2)
library(basilisk)
# Use both mogsa and mofa2 to analysis mae
mofa_obj <- create_mofa(mae)
# Data Inspection
# plot_data_overview(mofa_obj)
print(mofa_obj) # rownames as feature (colnames are patient id's), 92 samples per group
```
```{r echo=TRUE}
# Define Data option
data_opts <- get_default_data_options(mofa_obj)
# Normalization works worse when presenting heatmap though it reduces variations among factors
data_opts$center_groups <- FALSE
data_opts$scale_groups <- FALSE
# Define Model option, dont change the default option unless familiar with the underlying mathmatical models
model_opts <- get_default_model_options(mofa_obj)
# head(model_opts)
# Define Training option
train_opts <- get_default_training_options(mofa_obj)
# head(train_opts)
# Prepare mofa object
mofa_obj <- prepare_mofa(
object = mofa_obj,
data_options = data_opts,
model_options = model_opts,
training_options = train_opts
)
# training
outfile = file.path(tempdir(),"model.hdf5")
mofa_obj.trained <- run_mofa(mofa_obj, outfile, use_basilisk = TRUE)
plot_variance_explained(mofa_obj.trained)
```
Observation, factor 1 is the most variational factor among gistict assay
```{r}
v <- calculate_variance_explained(mofa_obj.trained)
v_gistict_max <- max(as.data.frame(v$r2_per_factor)[, 2])
max.percent <- v_gistict_max/(as.data.frame(v$r2_total)[2,])
paste0(round(max.percent * 100, 2), '%') # factor 1 accounts for 19.02 of total R^2 among all gistict factors
```
```{r}
# Coefficient of Determination (R^2)
v$r2_total
# by checking the coefficient of determination, we observe that gistict dataset fit the MOFA model the best
```
```{r}
# Violin plot of first three factors stratified by gender
# thinner part represents lower probability
plot_factor(mofa_obj.trained,
factor = 1:3,
color_by = 'gender',
add_violin = T,
violin_alpha = 0.25,
dodge = T
)
```
```{r}
# Distribution and scatter plots of first three factors
plot_factors(
mofa_obj.trained,
factors = 1:3,
color_by = 'SCNA.cluster'
)
```
From previous variance plot, we would like to investigate the factor1 in gistict, by checking the weights of each feature and their correlations.
Top 5 weights
```{r}
plot_top_weights(mofa_obj.trained,
view = "gistict",
factor = 1,
nfeatures = 5
)
```
Heatmap, we seek to present the heat map for which factor has highest R^2
RNASeq2GeneNorm
```{r}
# RNASeq2GeneNorm
v_factor <- as.data.frame(v$r2_per_factor)
which(v_factor[, 1] == max(v_factor[, 1]))
# columns are patient id
# also report some deep orange grids
plot_data_heatmap(mofa_obj.trained,
view = "RNASeq2GeneNorm",
factor = which(v_factor[, 1] == max(v_factor[, 1])),
features = 10,
# extra arguments that are passed to the `pheatmap` function
cluster_rows = TRUE,
cluster_cols = TRUE,
show_rownames = TRUE,
show_colnames = TRUE
)
```
Gistict, copy number
```{r}
# gistict
v_factor <- as.data.frame(v$r2_per_factor)
which(v_factor[, 2] == max(v_factor[, 2]))
# columns are patient id
# also report some deep orange grids
plot_data_heatmap(mofa_obj.trained,
view = "gistict",
factor = which(v_factor[, 2] == max(v_factor[, 2])),
features = 10,
# extra arguments that are passed to the `pheatmap` function
cluster_rows = TRUE,
cluster_cols = TRUE,
show_rownames = TRUE,
show_colnames = TRUE
)
```
Same way to check the mutation, find the factor with highest R^2 first
Mutations
```{r}
# Inspecting the highly correlated genes in Mutations
which(v_factor[, 3] == max(v_factor[, 3]))
plot_data_heatmap(mofa_obj.trained,
view = "Mutations",
factor = which(v_factor[, 3] == max(v_factor[, 3])),
features = 10,
# extra arguments that are passed to the `pheatmap` function
cluster_rows = TRUE,
cluster_cols = TRUE,
show_rownames = TRUE,
show_colnames = TRUE
)
```