-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjoin_datasets.r
383 lines (280 loc) · 12.4 KB
/
join_datasets.r
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
################################################################################
## join_datasets.r
## Francisco Martínez Picó - francisco9896@gmail.com
################################################################################
# KOOT INCLUDED:
Sys.info()[c('nodename', 'user')]
rm(list = ls())
R.version.string # 'R version 4.0.3 (2020-10-10)'
# LOAD PACKAGES -----------------------------------------------------------
library(limma)
library(venn)
# library(gsrmUtils)
functions_path = '/Users/francisco/Desktop/TFM/functions'
join_path = '/Users/francisco/Desktop/TFM/datasets/join_datasets'
results_path = '/Users/francisco/Desktop/TFM/datasets/results/results_sva_2'
getwd()
setwd(join_path)
source(paste0(functions_path, '/function_plot_PCAscores.r'))
source(paste0(functions_path, '/function_diffExprAnalysis.r'))
# LOAD DATASETS (DAT & ED) ------------------------------------------------
files_koot = dir(results_path, full.names = T)
## DAT OBJECTS (expression data):
dats2_koot = lapply(files_koot, function(f){
cat(f, '\n')
load(f,verbose = T)
return(dat)
})
names(dats2_koot) = gsub(pattern = '.rda', replacement = '',
x = basename(files_koot))
str(dats2_koot)
# How many genes in common between datasets?
common_genes_koot = Reduce('intersect', lapply(dats2_koot, rownames))
length(common_genes_koot) # [1] 12156
dats2_koot = do.call('cbind', lapply(dats2_koot, function(x){
x[common_genes_koot, ]
}))
dim(dats2_koot) # [1] 12156 195
# 12.156 genes (in common) in all datasets and 195 samples.
## ED OBJECTS (experimental dessigns):
eds2_koot = do.call('rbind', lapply(files_koot, function(f){
load(f)
ed = data.frame(sample = rownames(ed),
condition = ed$condition,
experiment = gsub(pattern = '.rda',replacement = '',
x = basename(f)),
stringsAsFactors = F)
return(ed)
}))
rownames(eds2_koot) = eds2_koot$sample
str(eds2_koot)
# SAMPLES AMONG DATASETS --------------------------------------------------
# We expect samples to be clustered in datasets. We should remove samples that
# are not clustered in datasets.
plot_PCAscores(dat = dats2_koot, ed = eds2_koot, condition1 = 'experiment',
title = 'PCA: muestras agrupadas por datasets')
# REMOVE EXPERIMENT EFFECT ------------------------------------------------
design_koot = model.matrix(~condition, data = eds2_koot)
dats3_koot = removeBatchEffect(dats2_koot, batch = eds2_koot$experiment)
plot_PCAscores(dat = dats3_koot, ed = eds2_koot, condition1 = 'experiment',
title = 'PCA: eliminado el efecto del experimento')
# CHECK DEGs AMONG CONTROLS -----------------------------------------------
control_dat = dats3_koot[, eds2_koot$condition == 'control']
control_ed = eds2_koot[eds2_koot$condition == 'control', ]
DEGs_control = diffExprAnalysis(dat = control_dat, ed = control_ed,
condition = 'experiment')
sigs = lapply(DEGs_control, function(deg){
rownames(deg)[deg$adj.P.Val <= 0.05]
})
str(sigs)
to_remove_control = unique(unlist(sigs))
# Significantly differentially expressed genes among controls:
length(to_remove_control) # [1] 2628
# CHECK DEGs AMONG RIF ----------------------------------------------------
rif_dat = dats3_koot[, eds2_koot$condition == 'RIF']
rif_ed = eds2_koot[eds2_koot$condition == 'RIF', ]
DEGs_rif = diffExprAnalysis(dat = rif_dat, ed = rif_ed,
condition = 'experiment')
sigs = lapply(DEGs_rif, function(deg){
rownames(deg)[deg$adj.P.Val <= 0.05]
})
str(sigs)
to_remove_rif = unique(unlist(sigs))
# Significantly differentially expressed genes among RIF:
length(to_remove_rif) # [1] 2154
# VENN DIAGRAM (DEGs CONTROL & DEGs RIF) ----------------------------------
venn(list(Control = to_remove_control, RIF = to_remove_rif))
# 1632 in common
# REMOVE DEGs IN CONTROLS & RIF -------------------------------------------
to_remove = unique(c(to_remove_control, to_remove_rif))
length(to_remove) # [1] 3150
dats3_koot = dats3_koot[!rownames(dats3_koot)%in%to_remove, ]
dim(dats3_koot) # 9006 genes & 195 samples
plot_PCAscores(dat = dats3_koot, ed = eds2_koot, condition1 = 'condition',
components = c(1,2),
title = 'PCA: control vs RIF')
# DEGs BETWEEN CONDITIONS -------------------------------------------------
DEGs_network_koot = diffExprAnalysis(dat = dats3_koot, ed = eds2_koot,
condition = 'condition')
head(DEGs_network_koot$`control-RIF`)
sigs_DEGs_network_koot = lapply(DEGs_network_koot, function(deg){
rownames(deg)[deg$adj.P.Val <= 0.05]
})
length(sigs_DEGs_network_koot$`control-RIF`) # 436 DEGs
# CHECK GENES AMONG EXPERIMENTS -------------------------------------------
DEGs = diffExprAnalysis(dat = dats3_koot, ed = eds2_koot,
condition = 'experiment')
str(DEGs)
sigs = lapply(DEGs, function(deg){
rownames(deg)[deg$adj.P.Val <= 0.05]
})
str(sigs)
to_remove = unique(unlist(sigs))
length(to_remove) # 0 --> No genes to be removed
################################################################################
################################################################################
# KOOT NOT INCLUDED:
Sys.info()[c('nodename', 'user')]
rm(list = ls())
R.version.string # 'R version 4.0.3 (2020-10-10)'
# LOAD PACKAGES -----------------------------------------------------------
library(limma)
library(venn)
library(ggplot2)
# library(gsrmUtils)
functions_path = '/Users/francisco/Desktop/TFM/functions'
join_path = '/Users/francisco/Desktop/TFM/datasets/join_datasets'
results_path = '/Users/francisco/Desktop/TFM/datasets/results/results_sva_2'
getwd()
setwd(join_path)
source(paste0(functions_path, '/function_plot_PCAscores.r'))
source(paste0(functions_path, '/function_diffExprAnalysis.r'))
# LOAD DATASETS (DAT & ED) ------------------------------------------------
files = dir(results_path, full.names = T)
files = files[-4] # Remove koot since it provides much noise
## DAT OBJECTS (expression data):
dats2 = lapply(files, function(f){
cat(f, '\n')
load(f,verbose = T)
return(dat)
})
names(dats2) = gsub(pattern = '.rda', replacement = '', x = basename(files))
str(dats2)
# How many genes in common between datasets?
common_genes = Reduce('intersect', lapply(dats2, rownames))
length(common_genes) # [1] 12358
dats2 = do.call('cbind', lapply(dats2, function(x){
x[common_genes, ]
}))
dim(dats2) # [1] 12358 80
# 12.358 genes (in common) in all datasets and 80 samples.
## ED OBJECTS (experimental dessigns):
eds2 = do.call('rbind', lapply(files, function(f){
load(f)
ed = data.frame(sample = rownames(ed),
condition = ed$condition,
experiment = gsub(pattern = '.rda',replacement = '',
x = basename(f)),
stringsAsFactors = F)
return(ed)
}))
rownames(eds2) = eds2$sample
str(eds2)
table(eds2$condition)
table(eds2$experiment)
# SAMPLES AMONG DATASETS --------------------------------------------------
# We expect samples to be clustered in datasets. We should remove samples that
# are not clustered in datasets.
plot_PCAscores(dat = dats2, ed = eds2, condition1 = 'experiment',
title = 'PCA: muestras agrupadas por datasets (sin koot)')
#############
pca = prcomp(t(dats2))
var_norm = round(summary(pca)$importance[2, c(1,2)] * 100, 1)
toplot_norm = data.frame(pca$x[, c(1,2)], stringsAsFactors = F)
lim_norm = max(abs(c(min(toplot_norm), max(toplot_norm))))
axis_limits_norm = c(-lim_norm, lim_norm)
toplot_norm$color = eds2$experiment
# To fit the title:
wrapper = function(x, ...)
{
paste(strwrap(x, ...), collapse = '\n')
}
my_title = 'PCA: muestras agrupadas por datasets (sin koot)'
# + ggtitle(wrapper(my_title, width = 20))
ggplot(data = toplot_norm,
aes_string(x = colnames(toplot_norm)[1], y = colnames(toplot_norm)[2])) +
geom_point(aes(color = color), size = 3) +
scale_color_manual(name = 'RIF', values = c('#E69F00', '#56B4E9',
'#009E73', '#F0E442',
'#0072B2')) +
# geom_text_repel(label = ed$sample, size = 3) +
xlab(paste0('PC1', ': ', var_norm[1], '%')) +
ylab(paste0('PC2', ': ', var_norm[2], '%')) +
ggtitle(wrapper(my_title, width = 35)) +
xlim(axis_limits_norm) + ylim(axis_limits_norm) +
theme_light() +
theme(legend.position = 'bottom',
axis.title = element_text(size = 18),
axis.text = element_text(size = 15),
plot.title = element_text(size = 22, hjust = 0.5),
legend.title = element_blank(),
legend.text = element_text(size = 13))
# REMOVE EXPERIMENT EFFECT ------------------------------------------------
design = model.matrix(~condition, data = eds2)
dats3 = removeBatchEffect(dats2, batch = eds2$experiment)
plot_PCAscores(dat = dats3, ed = eds2, condition1 = 'experiment',
title = 'PCA: eliminado el efecto del experimento')
# CHECK DEGs AMONG CONTROLS -----------------------------------------------
control_dat = dats3[, eds2$condition == 'control']
control_ed = eds2[eds2$condition == 'control', ]
DEGs_control = diffExprAnalysis(dat = control_dat, ed = control_ed,
condition = 'experiment')
sigs = lapply(DEGs_control, function(deg){
rownames(deg)[deg$adj.P.Val <= 0.05]
})
str(sigs)
to_remove_control = unique(unlist(sigs))
# Significantly differentially expressed genes among controls:
length(to_remove_control) # [1] 88
# CHECK DEGs AMONG RIF ----------------------------------------------------
rif_dat = dats3[, eds2$condition == 'RIF']
rif_ed = eds2[eds2$condition == 'RIF', ]
DEGs_rif = diffExprAnalysis(dat = rif_dat, ed = rif_ed,
condition = 'experiment')
sigs = lapply(DEGs_rif, function(deg){
rownames(deg)[deg$adj.P.Val <= 0.05]
})
str(sigs)
to_remove_rif = unique(unlist(sigs))
# Significantly differentially expressed genes among RIF:
length(to_remove_rif) # [1] 129
# VENN DIAGRAM (DEGs CONTROL & DEGs RIF) ----------------------------------
venn(list(Control = to_remove_control, RIF = to_remove_rif))
# 36 in common
# REMOVE DEGs IN CONTROLS & RIF -------------------------------------------
to_remove = unique(c(to_remove_control, to_remove_rif))
length(to_remove) # [1] 181
dats3 = dats3[!rownames(dats3)%in%to_remove, ]
dim(dats3) # 12177 genes & 80 samples
plot_PCAscores(dat = dats3, ed = eds2, condition1 = 'condition',
components = c(1,2),
title = 'PCA: control vs RIF')
# DEGs BETWEEN CONDITIONS -------------------------------------------------
DEGs_network = diffExprAnalysis(dat = dats3, ed = eds2, condition = 'condition')
head(DEGs_network$`control-RIF`)
sigs_DEGs_network = lapply(DEGs_network, function(deg){
rownames(deg)[deg$adj.P.Val <= 0.05]
})
length(sigs_DEGs_network$`control-RIF`) # 2152 DEGs
# Differential analysis
DEGs = diffExprAnalysis(dat = dats3, ed = eds2, condition = 'experiment')
str(DEGs)
sigs = lapply(DEGs, function(deg){
rownames(deg)[deg$adj.P.Val <= 0.05]
})
str(sigs)
to_remove = unique(unlist(sigs))
length(to_remove) # 0 --> No genes to be removed
################################################################################
# sigs_DEGs_network_koot includes sig DEGs when Koot included
network_koot_v2 = sigs_DEGs_network_koot$`control-RIF`
length(network_koot_v2) # 436 DEGs when Koot included
# sigs_DEGs_network includes sig DEGS when koot NOT included
network_v2 = sigs_DEGs_network$`control-RIF`
length(network_v2) # 2152 DEGs when koot NOT included
network_koot_v2%in%network_v2 # returns TRUE/FALSE if genes in network koot are in network
core_DEGs_v2 = network_koot_v2[network_koot_v2%in%network_v2]
core_DEGs_v2
length(core_DEGs_v2) # 368
venn(list(Koot_included = network_koot_v2, Koot_NOT_included = network_v2))
save(network_koot_v2, network_v2, core_DEGs_v2,
file = '/Users/francisco/Desktop/network_genes_v2.rda', version = 2)
load('/Users/francisco/Desktop/network_genes_v2.rda', verbose = T)
venn(list(core_genes_v1 = core_DEGs_v1, core_genes_v2 = core_DEGs_v2))
save(DEGs_network, )
# SAVING FOR FUNCTIONAL ANALYSIS ------------------------------------------
save(DEGs_network, sigs_DEGs_network,
file = '/Users/francisco/Desktop/DEGs_network_v2.rda', version = 2)
# DEGs_network es el resultado de la expresión diferencial (tanto aquellos que
# la tienen como los que no), y sigs_DEGs_network contiene sólo aquellos genes
#con expresión diferencial significativa.