forked from MelinaKlostermann/Molitor-et-al-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
4_Compare_PURA_iCLIPs.Rmd
676 lines (506 loc) · 30.2 KB
/
4_Compare_PURA_iCLIPs.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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
---
title: "4 Comparison of the crosslink patterns in three different PURA iCLIP experiments from HeLa cells and NPC PURA iCLIP"
author: "Melina"
date: "`r format(Sys.time(), '%d %B, %Y')`"
output: pdf_document
---
```{r setup, include=FALSE}
require("knitr")
knitr::opts_chunk$set(warning=FALSE, message=FALSE, cache=TRUE) #, fig.pos = "!H", out.extra = ""
```
```{r libraries, include=FALSE}
library(GenomicRanges)
library(rtracklayer)
library(knitr)
library(GenomicFeatures)
library(dplyr)
library(ggpubr)
library(Gviz)
library(purrr)
library(wesanderson)
library(ComplexHeatmap)
library(BSgenome.Hsapiens.UCSC.hg38)
report_color <- (pals::ocean.solar(15))
outpath <- "/Users/melinaklostermann/Documents/projects/PURA/02_R_new_pip/01-BS_def/02-BS_def_endo/Report59-output/"
source("/Users/melinaklostermann/Documents/projects/PURA/02_R_new_pip/XX-helpful-chunks/theme_paper.R")
```
```{r}
################################################################################
### CODE REVIEW SUMMARY ###
################################################################################
###
### Review by: Mirko Brüggemann
### Comment shortcut: MB
### Date: 04.10.2022
### Time spent: ~2h
# Main objective:
# Comparison of different PURA iCLIP datasets, mainly binding in NPC cells
#
# Input:
# - PURA crosslinks from the preprocessing as bw
# - gene annotation as of gencode v31 as rds
# - Binding sites from script 1
#
# Minor tasks the script performs are:
# -
#
# Major/ issues:
# - No major issues, code works as expected :)
#
# Minor/ suggestions:
# - More comments in the code this time which is nice :)
# - Still the code is often hard to read
# -> multiple reassignments of values between variables within the same code junk
# -> input data is loaded in partly redundant lists
# -> a lot of hard coded values
# - GVIZ functions are slow because iCLIP signal is loaded as GRanges instead of RLE
# - calculation of iCLIP coverage over a GRanges object can be optimized by using BindingSiteFinder
#
# Nice job!
# Feel free to review the review :)
#
```
# What is done here?
- Metaprofile of PURA binding in NPC cells (iCLIP experiment with low signal)
- Some GVIZ plots of PUAR crosslinking patterns in HeLa snd NPC cells
- Min max normalised spline smoothed heatmaps of crosslinks from endogenous PURA in Hela, and FLAG-PURA overexpression pulled down with two different antibodies (antiFALG & antiPURA)
# Input
```{r}
# load BS with overlap annotation
BS <- readRDS("/Users/melinaklostermann/Documents/projects/PURA/Molitor-et-al-2022/binding_sites_characterized.rds") %>% makeGRangesFromDataFrame(keep.extra.columns = T)
# crosslinks from NPC cells
npc_cl_p_rle <- import.bw("/Users/melinaklostermann/Documents/projects/PURA/01_raw_data/PURA_NPC_and_FLAG/imb_koenig_2020_17_koenig_iCLIP_PURA/merged/bw/imb_koenig_2020_17_NPC.v2uniqMD.duprm.plus.bw", as = "Rle")
npc_cl_m_rle <- import.bw("/Users/melinaklostermann/Documents/projects/PURA/01_raw_data/PURA_NPC_and_FLAG/imb_koenig_2020_17_koenig_iCLIP_PURA/merged/bw/imb_koenig_2020_17_NPC.v2uniqMD.duprm.minus.bw", as = "Rle")
npc_cl_p_gr <- import.bw("/Users/melinaklostermann/Documents/projects/PURA/01_raw_data/PURA_NPC_and_FLAG/imb_koenig_2020_17_koenig_iCLIP_PURA/merged/bw/imb_koenig_2020_17_NPC.v2uniqMD.duprm.plus.bw")
npc_cl_m_gr <- import.bw("/Users/melinaklostermann/Documents/projects/PURA/01_raw_data/PURA_NPC_and_FLAG/imb_koenig_2020_17_koenig_iCLIP_PURA/merged/bw/imb_koenig_2020_17_NPC.v2uniqMD.duprm.minus.bw")
# crosslinks from all three HeLa iCLIPs
hela_cl_rle <- list(endo_p = import.bw("/Users/melinaklostermann/Documents/projects/PURA/01_raw_data/PURA_endo/imb_koenig_2020_07_koenig_iCLIP_PURA_endogene/merged/bw/imb_koenig_2020_07_PURAendo.v2uniqMD.duprm.plus.bw", as = "Rle"),
endo_m = import.bw("/Users/melinaklostermann/Documents/projects/PURA/01_raw_data/PURA_endo/imb_koenig_2020_07_koenig_iCLIP_PURA_endogene/merged/bw/imb_koenig_2020_07_PURAendo.v2uniqMD.duprm.minus.bw", as = "Rle"),
oe_pura_p = import.bw("/Users/melinaklostermann/Documents/projects/PURA/01_raw_data/oe-imb_koenig_2019_11_koenig_iCLIP_PURA___v2uniqMD/merged/bw/imb_koenig_2019_11_allsamples.v2uniqMD.duprm.plus.bw", as = "Rle"),
oe_pura_m = import.bw("/Users/melinaklostermann/Documents/projects/PURA/01_raw_data/oe-imb_koenig_2019_11_koenig_iCLIP_PURA___v2uniqMD/merged/bw/imb_koenig_2019_11_allsamples.v2uniqMD.duprm.minus.bw", as = "Rle"),
oe_flag_p = import.bw("/Users/melinaklostermann/Documents/projects/PURA/01_raw_data/PURA_NPC_and_FLAG/imb_koenig_2020_17_koenig_iCLIP_PURA/merged/bw/imb_koenig_2020_17_flag.v2uniqMD.duprm.plus.bw", as = "Rle"),
oe_flag_m = import.bw("/Users/melinaklostermann/Documents/projects/PURA/01_raw_data/PURA_NPC_and_FLAG/imb_koenig_2020_17_koenig_iCLIP_PURA/merged/bw/imb_koenig_2020_17_flag.v2uniqMD.duprm.minus.bw", as = "Rle"))
# crosslinks from all HeLa iCLIP as granges
hela_cl <- list(endo_p = import.bw("/Users/melinaklostermann/Documents/projects/PURA/01_raw_data/PURA_endo/imb_koenig_2020_07_koenig_iCLIP_PURA_endogene/merged/bw/imb_koenig_2020_07_PURAendo.v2uniqMD.duprm.plus.bw"), endo_m = import.bw("/Users/melinaklostermann/Documents/projects/PURA/01_raw_data/PURA_endo/imb_koenig_2020_07_koenig_iCLIP_PURA_endogene/merged/bw/imb_koenig_2020_07_PURAendo.v2uniqMD.duprm.minus.bw"),
oe_pura_p = import.bw("/Users/melinaklostermann/Documents/projects/PURA/01_raw_data/oe-imb_koenig_2019_11_koenig_iCLIP_PURA___v2uniqMD/merged/bw/imb_koenig_2019_11_allsamples.v2uniqMD.duprm.plus.bw"),
oe_pura_m = import.bw("/Users/melinaklostermann/Documents/projects/PURA/01_raw_data/oe-imb_koenig_2019_11_koenig_iCLIP_PURA___v2uniqMD/merged/bw/imb_koenig_2019_11_allsamples.v2uniqMD.duprm.minus.bw"),
oe_flag_p = import.bw("/Users/melinaklostermann/Documents/projects/PURA/01_raw_data/PURA_NPC_and_FLAG/imb_koenig_2020_17_koenig_iCLIP_PURA/merged/bw/imb_koenig_2020_17_flag.v2uniqMD.duprm.plus.bw"),
oe_flag_m = import.bw("/Users/melinaklostermann/Documents/projects/PURA/01_raw_data/PURA_NPC_and_FLAG/imb_koenig_2020_17_koenig_iCLIP_PURA/merged/bw/imb_koenig_2020_17_flag.v2uniqMD.duprm.minus.bw"))
# filtered annotation
anno_txdb <- loadDb("/Users/melinaklostermann/Documents/projects/PURA/Molitor-et-al-2022/annotation_txdb.db")
annotation <- readRDS("/Users/melinaklostermann/Documents/projects/PURA/Molitor-et-al-2022/annotation.rds")
# setting for gviz plots
gen <- "hg38"
cex <- 0.7
cex.a <- 0.5
```
# Meta Profile NPC
```{r meta_profile_npc, fig.height=5, fig.width=5}
#############################
# metaprofile of npc crosslinks in 15nt window on PURA BS
###########################
# enlarge BS
BS_15nt <- as.data.frame(BS+10 )%>%
arrange(desc(score)) %>%
.[1:floor(NROW(BS)),] %>%
makeGRangesFromDataFrame(keep.extra.columns = T)
npc_cl_p <- as.matrix(npc_cl_p_rle[BS_15nt[strand(BS_15nt) == "+"]])
npc_cl_m <- as.matrix(npc_cl_m_rle[BS_15nt[strand(BS_15nt) == "-"]])
npc_cl <- rbind(npc_cl_p, npc_cl_m)
# exclude windows with 0 NPC crosslinks
npc_cl_without_0 <- npc_cl[rowSums(npc_cl)!=0 ,] %>%
as.data.frame()%>%
arrange(desc(rowSums(.))) %>%
as.matrix()
# make heatmap
col_fun <- circlize::colorRamp2(c(0,10), c("white", "black"))
line <- HeatmapAnnotation(crosslinks = anno_lines(colSums(npc_cl), height = unit(2, "cm"), ylim = c(0, 136000)))
ht_opt$heatmap_column_names_gp = gpar(fontsize = 5)
# label top genes
npc_cl_top_genes <- BS_15nt$gene_name
npc_cl_top_genes[duplicated(npc_cl_top_genes)] <- ""
npc_cl_top_genes[21:length(npc_cl_top_genes)] <- ""
Heatmap(npc_cl_without_0[1:1000,], col =col_fun, cluster_columns = F, cluster_rows = F, top_annotation = line, use_raster = T, raster_device = "png", raster_quality = 10, right_annotation = rowAnnotation(foo = anno_mark(at = 1:20, labels = npc_cl_top_genes)))
```
# GVIZ examples Hela endogenous PURA and NPC endogenous PURA crosslinking
```{r}
############################
# function for GVIZ plots
############################
plotGviz <- function(GR, i, w, cl_plus, cl_minus, name, anno, BS, color){
# get chromosome amd range, make data frame
chr<- as.character(seqnames(GR[i]))
df <- GR[i,] %>% as.data.frame
range <- GRanges(seqnames=chr, strand=strand(GR[i]), ranges=IRanges(start= start(GR[i])-w[1], end = end(GR[i])+w[2]))
# make track of crosslinks depending on which strand
# cl_2
if(df$strand=="+"){
cl_at_region <- subsetByOverlaps(cl_plus, range) %>% keepStandardChromosomes()
dTrack <-
DataTrack(
cl_at_region,
genome = gen,
name = "cl endo",
type = "histogram", cex.title=cex, cex.axis= cex.a, background.title = "white", col.axis= "black", fontcolor = "black")
}else{
cl_at_region <- subsetByOverlaps(cl_minus, range) %>% keepStandardChromosomes()
dTrack <-
DataTrack(
cl_at_region,
genome = gen,
name = "cl_endo",
type = "histogram", cex.title=cex, cex.axis= cex.a, background.title = "white", col.axis= "black", fontcolor = "black")
}
if(df$strand=="+"){
seqTrack <- SequenceTrack(Hsapiens, chromosome=chr, complement = F)
}else{
seqTrack <-SequenceTrack(Hsapiens, chromosome=chr, complement = T)}
#pureclip track
BS_range <- BS[strand(BS)==strand(GR[i])] %>% subsetByOverlaps(.,GR[i]+w[1])
BS_new_track <- GeneRegionTrack(BS_range, genome = gen, name="BS endo",cex.axis= cex.a,
col.histogram=report_color[13], fill=report_color[5], stacking="hide", rotation.title=0, cex.title=cex, background.title = "white", fontcolor = "black")
# track showing loaction
gtrack <- GenomeAxisTrack(cex.title= 2, add53 = T)
# annotation track
anno_track <- GeneRegionTrack(anno, genome = gen, chromosome = chr,
name = "anno", fill = color, cex.title=cex, cex.axis= cex.a, stacking = "squish", background.title = "transparent", fontcolor = "black")
plotTracks(list(dTrack, seqTrack, BS_new_track, anno_track, gtrack),
from = start(range), to = end(range),
chromosome=chr, sizes =c(5,1,1,1.5, 1),
main= paste(GR[i]$gene_name, "-", chr, ":", start(range),"-", end(range)), cex.main = 1)
}
##########################
# make GVIZ plots
########################
#=================================
# Highest crosslinked regions in STARD7, SEC61A1, YWHAE zoom-in endo. PURA HeLa
#==================================
regions_for_gviz_1 <- BS %>%
as.data.frame(.) %>%
group_by(gene_id) %>%
arrange(desc(score), .by_group = T) %>%
dplyr::slice(1) %>%
ungroup %>%
filter(gene_name %in% c("STARD7", "SEC61A1", "YWHAE")) %>%
makeGRangesFromDataFrame(keep.extra.columns = T)
# SEC61A1
plotGviz(GR = regions_for_gviz_1[1] , i = 1, w = c(150,150), cl_plus =hela_[[1]], cl_minus = hela_cl[[2]], name = regions_for_gviz_1[1]$gene_name , anno = anno_txdb, BS = BS, color = "darkred")
plotGviz(GR = regions_for_gviz_1[1] , i = 1, w = c(150,150), cl_plus = npc_cl_p_gr, cl_minus = npc_cl_m_gr, name = regions_for_gviz_1[1]$gene_name , anno = anno_txdb, BS = BS, color = "darkred")
# STARD7
plotGviz(GR = regions_for_gviz_1[2] , i = 1, w = c(150,170), cl_plus =hela_cl[[1]], cl_minus = hela_cl[[2]], name = regions_for_gviz_1[2]$gene_name , anno = anno_txdb, BS = BS, color = "darkred")
plotGviz(GR = regions_for_gviz_1[2] , i = 1, w = c(150,170), cl_plus = npc_cl_p_gr, cl_minus = npc_cl_m_gr, name = regions_for_gviz_1[1]$gene_name , anno = anno_txdb, BS = BS, color = "darkred")
# STARD7 whole 3'UTR
plotGviz(GR = regions_for_gviz_1[2] , i = 1, w = c(300,2000), cl_plus =hela_cl[[1]], cl_minus = hela_cl[[2]], name = regions_for_gviz_1[2]$gene_name , anno = anno_txdb, BS = BS, color = "darkred")
# YWHAE
plotGviz(GR = regions_for_gviz_1[3] , i = 1, w = c(150,170), cl_plus =hela_cl[[1]], cl_minus = hela_cl[[2]], name = regions_for_gviz_1[3]$gene_name , anno = anno_txdb, BS = BS, color = "darkred")
plotGviz(GR = regions_for_gviz_1[3] , i = 1, w = c(150,170), cl_plus = npc_cl_p_gr, cl_minus = npc_cl_m_gr, name = regions_for_gviz_1[3]$gene_name , anno = anno_txdb, BS = BS, color = "darkred")
#=================================
# non-coding examples RN7SL1, MALAT1, NEAT1
#==================================
regions_for_gviz_2 <- BS %>%
as.data.frame(.) %>%
group_by(gene_id) %>%
arrange(desc(score), .by_group = T) %>%
dplyr::slice(1) %>%
ungroup %>%
filter(gene_id %in% c("ENSG00000251562", "ENSG00000245532", "ENSG00000276168" )) %>% # MALAT1
makeGRangesFromDataFrame(keep.extra.columns = T)
# MALAT1
plotGviz(GR = regions_for_gviz_2[2] , i = 1, w = c(400,400), cl_plus =hela_cl[[1]], cl_minus = hela_cl[[2]], name = regions_for_gviz_1[2]$gene_name , anno = anno_txdb, BS = BS, color = "darkred")
# NEAT1
plotGviz(GR = regions_for_gviz_2[1] , i = 1, w = c(400,400), cl_plus =hela_cl[[1]], cl_minus = hela_cl[[2]], name = regions_for_gviz_1[1]$gene_name , anno = anno_txdb, BS = BS, color = "darkred")
# RN7SL1
plotGviz(GR = regions_for_gviz_2[3] , i = 1, w = c(200,100), cl_plus =hela_cl[[1]], cl_minus = hela_cl[[2]], name = regions_for_gviz_1[3]$gene_name , anno = anno_txdb, BS = BS, color = "darkred")
#=================================
# RNA of p-body essential proteins: LSM14A, DDX6 (whole gene)
#==================================
regions_for_gviz_3 <- annotation[annotation$type == "gene"] %>%
as.data.frame(.) %>%
filter(gene_name %in% c("LSM14A", "DDX6") ) %>%
makeGRangesFromDataFrame(keep.extra.columns = T)
# LSM14A
plotGviz(GR = regions_for_gviz_3[2] , i = 1, w = c(0,0), cl_plus =hela_cl[[1]], cl_minus = hela_cl[[2]], name = regions_for_gviz_1[2]$gene_name , anno = anno_txdb, BS = BS, color = "darkred")
# DDX6
plotGviz(GR = regions_for_gviz_3[1] , i = 1, w = c(0,0), cl_plus =hela_cl[[1]], cl_minus = hela_cl[[2]], name = regions_for_gviz_1[1]$gene_name , anno = anno_txdb, BS = BS, color = "darkred")
#=================================
# Bound regions tested in EMSAs
#==================================
# regions of seqeunces used for emsa
regions_for_gviz_4 <- GRanges(c("chr2:96185058-96185112:-", "chr11:65441947-65441998:+", "chr5:86619374-86619444:+", "chr19:34221504-34221561:+") , gene_name = c("STARD7", "NEAT1", "COX7C", "LSM14A" ))
# STARD7
plotGviz(GR = regions_for_gviz_4[1] , i = 1, w = c(100,100), cl_plus =hela_cl[[1]], cl_minus = hela_cl[[2]], name = regions_for_gviz_1[1]$gene_name , anno = anno_txdb, BS = BS, color = "darkred")
# NEAT1
plotGviz(GR = regions_for_gviz_4[2] , i = 1, w = c(100,100), cl_plus =hela_cl[[1]], cl_minus = hela_cl[[2]], name = regions_for_gviz_4[2]$gene_name , anno = anno_txdb, BS = BS, color = "darkred")
# COX7C
plotGviz(GR = regions_for_gviz_4[3] , i = 1, w = c(100,100), cl_plus =hela_cl[[1]], cl_minus = hela_cl[[2]], name = regions_for_gviz_4[3]$gene_name , anno = anno_txdb, BS = BS, color = "darkred")
# LSM14A
plotGviz(GR = regions_for_gviz_4[4] , i = 1, w = c(100,100), cl_plus =hela_cl[[1]], cl_minus = hela_cl[[2]], name = regions_for_gviz_4[4]$gene_name , anno = anno_txdb, BS = BS, color = "darkred")
```
# Compare three PURA HeLa CLIPs with spline smoothed maps
```{r }
### ===============================================
### Normalisation and spline smoothing of crosslink patterns
### ===============================================
# Note: this approach was adapted from Heyl & Backofen 2021 - "StoatyDive: Evaluation and classification of peak profiles for sequencing data"
# min max normalisation
min_max_noramlize <- function(x){
x_new <- (x-min(x)) / (max(x)-min(x))
return(x_new)
}
# smooth
smoothing <- function(y, lambda, dim){
data_points <- length(y)
# test if we have just a vector of constant values.
which(y != max(y))
if ( length(which(y != max(y))) != 0 ){
x <- c(1:data_points)
y.smooth <- smooth.spline(x, y, spar=lambda)
x_new <- seq(1, data_points, data_points/dim)
y_new <- predict(y.smooth, x_new)$y
y_new <- y_new/max(y_new)
return(y_new)
}else{
x_new <- seq(1, data_points, data_points/dim)
y_new <- rep( max(y), length(x_new) )
return(y_new)}
}
clean_smooth <- function(data_smoothed) {
data_smoothed <- t(data_smoothed)
# get rid of negative values
data_smoothed[which(data_smoothed < 0)] = 0.0
# get rid of diract impulses bigger than max (> 1.0)
data_smoothed[which(data_smoothed > 1.0)] = 1.0
return(data_smoothed)
}
```
## in the beginning of 3'UTRs
```{r eval=FALSE, include=TRUE}
### ============================================
### Crosslink patterns at the start of 3'UTRs
### ============================================
# window at the beginning of the 3'UTR
# select UTRs
three_UTRs <- annotation[annotation$type=="three_prime_UTR"]
three_UTRs <- three_UTRs[!duplicated(three_UTRs$gene_id)]
w = 300
window <- three_UTRs
end(window[strand(window)=="+"]) <- start(window[strand(window)=="+"])+300
start(window[strand(window)=="-"]) <- end(window[strand(window)=="-"])-300
cl_window <- c(map(hela_cl_rle[c(1,3,5)], ~.x[window[strand(window)=="+"]] %>% as.matrix() ),
map(hela_cl_rle[c(2,4,6)], ~.x[window[strand(window)=="-"]] %>% as.matrix() %>% .[,ncol(.):1] ))
# select 3'UTRs with medium coverage
cl_window_coverage_p <- map_dfc(cl_window[1:3], ~ rowSums(.x))
cl_window_coverage_m <- map_dfc(cl_window[4:6], ~ rowSums(.x))
cov_p <- map_dfc(cl_window_coverage_p, ~.x) %>% mutate(medium_cov =
case_when( endo_p > 100 &
oe_pura_p > 100 &
oe_flag_p > 100 &
endo_p < 10^6 &
oe_pura_p < 10^6 &
oe_flag_p < 10^6
~ T,
T~F))
cov_m <- map_dfc(cl_window_coverage_m, ~.x) %>% mutate(medium_cov =
case_when( endo_m > 100 &
oe_pura_m > 100 &
oe_flag_m > 100 &
endo_m < 10^6 &
oe_pura_m < 10^6 &
oe_flag_m < 10^6
~ T,
T~F))
cl_window <- c(map(cl_window[1:3], ~.x[cov_p$medium_cov,] ),
map(cl_window[4:6], ~.x[cov_m$medium_cov,] ))
window_p <- window[strand(window)=="+"]
window_m <- window[strand(window)=="-"]
window <- c(window_p[cov_p$medium_cov], window_m[cov_m$medium_cov])
cl_window <- list(endo = rbind(cl_window[[2]], cl_window[[5]]),
oe = rbind(cl_window[[1]], cl_window[[4]]),
flag = rbind(cl_window[[3]], cl_window[[6]]))
# normalise
cl_window_normalized <- lapply(cl_window, function(x) apply(x, 1, min_max_noramlize) %>% t(.))
# set NaNs to zero
cl_window_normalized[[1]][which(is.na(cl_window_normalized[[1]]))] = 0
cl_window_normalized[[2]][which(is.na(cl_window_normalized[[2]]))] = 0
cl_window_normalized[[3]][which(is.na(cl_window_normalized[[3]]))] = 0
# smoothing
cl_window_norm_smooth <- lapply(cl_window_normalized, apply, 1, function(x) smoothing(y=x, lambda = 0.5, dim =500))
cl_window_norm_smooth <- lapply(cl_window_norm_smooth, function(x) clean_smooth(data_smoothed = x))
# heatmap of all three
#cl_window_norm_smooth <- rbind(cl_window_norm_smooth$endo, cl_window_norm_smooth$oe, cl_window_norm_smooth$flag)
h1 <- Heatmap(cl_window_norm_smooth[[1]], cluster_columns = F, use_raster = T, raster_device = "png", raster_quality = 10)
h2 <- Heatmap(cl_window_norm_smooth[[2]], cluster_columns = F, use_raster = T, raster_device = "png", raster_quality = 10)
h3 <- Heatmap(cl_window_norm_smooth[[3]], cluster_columns = F, use_raster = T, raster_device = "png", raster_quality = 10)
### TODO MB
### resulting heatmaps look a bit strange, maybe try different sorting of ranges
### be aware that clustering is only performed on h1 and all heatmaps get there
### rows arranged by that clustering
###
h1+h2+h3
```
## in the beginning of CDSs
```{r eval=FALSE, include=TRUE}
### ============================================
### Crosslink patterns at the start of CDS
### ============================================
# window at the beginning of the 3'UTR
# select UTRs
CDS <- annotation[annotation$type=="CDS"]
CDS <- CDS[!duplicated(three_UTRs$gene_id)]
w = 300
window <- CDS
end(window[strand(window)=="+"]) <- start(window[strand(window)=="+"])+300
start(window[strand(window)=="-"]) <- end(window[strand(window)=="-"])-300
cl_window <- c(map(hela_cl_rle[c(1,3,5)], ~.x[window[strand(window)=="+"]] %>% as.matrix() ),
map(hela_cl_rle[c(2,4,6)], ~.x[window[strand(window)=="-"]] %>% as.matrix() %>% .[,ncol(.):1] ))
# select 3'UTRs with medium coverage
cl_window_coverage_p <- map_dfc(cl_window[1:3], ~ rowSums(.x))
cl_window_coverage_m <- map_dfc(cl_window[4:6], ~ rowSums(.x))
cov_p <- map_dfc(cl_window_coverage_p, ~.x) %>% mutate(medium_cov =
case_when( endo_p > 100 &
oe_pura_p > 100 &
oe_flag_p > 100 &
endo_p < 10^6 &
oe_pura_p < 10^6 &
oe_flag_p < 10^6
~ T,
T~F))
cov_m <- map_dfc(cl_window_coverage_m, ~.x) %>% mutate(medium_cov =
case_when( endo_m > 100 &
oe_pura_m > 100 &
oe_flag_m > 100 &
endo_m < 10^6 &
oe_pura_m < 10^6 &
oe_flag_m < 10^6
~ T,
T~F))
cl_window <- c(map(cl_window[1:3], ~.x[cov_p$medium_cov,] ),
map(cl_window[4:6], ~.x[cov_m$medium_cov,] ))
window_p <- window[strand(window)=="+"]
window_m <- window[strand(window)=="-"]
window <- c(window_p[cov_p$medium_cov], window_m[cov_m$medium_cov])
cl_window <- list(endo = rbind(cl_window[[2]], cl_window[[5]]),
oe = rbind(cl_window[[1]], cl_window[[4]]),
flag = rbind(cl_window[[3]], cl_window[[6]]))
# normalise
cl_window_normalized <- lapply(cl_window, function(x) apply(x, 1, min_max_noramlize) %>% t(.))
# set NaNs to zero
cl_window_normalized[[1]][which(is.na(cl_window_normalized[[1]]))] = 0
cl_window_normalized[[2]][which(is.na(cl_window_normalized[[2]]))] = 0
cl_window_normalized[[3]][which(is.na(cl_window_normalized[[3]]))] = 0
# smoothing
cl_window_norm_smooth <- lapply(cl_window_normalized, apply, 1, function(x) smoothing(y=x, lambda = 0.5, dim =500))
cl_window_norm_smooth <- lapply(cl_window_norm_smooth, function(x) clean_smooth(data_smoothed = x))
# heatmap of all three
#cl_window_norm_smooth <- rbind(cl_window_norm_smooth$endo, cl_window_norm_smooth$oe, cl_window_norm_smooth$flag)
h1 <- Heatmap(cl_window_norm_smooth[[1]], cluster_columns = F, use_raster = T, raster_device = "png", raster_quality = 10)
h2 <- Heatmap(cl_window_norm_smooth[[2]], cluster_columns = F, use_raster = T, raster_device = "png", raster_quality = 10)
h3 <- Heatmap(cl_window_norm_smooth[[3]], cluster_columns = F, use_raster = T, raster_device = "png", raster_quality = 10)
h1+h2+h3
```
## Metaprofile
```{r}
####################
# get endo flag and oe crosllinks for a window around BS
###################
w = 30
set.seed(42)
window <- BS[sample(1000, x = 1:NROW(BS))] + w
cl_window <- c(map(hela_cl_rle[c(1,3,5)], ~.x[window[strand(window)=="+"]] %>% as.matrix() ),
map(hela_cl_rle[c(2,4,6)], ~.x[window[strand(window)=="-"]] %>% as.matrix() ))
cl_window <- list(endo = rbind(cl_window$endo_p, cl_window$endo_m),
oe = rbind(cl_window$oe_pura_p, cl_window$oe_pura_m),
flag = rbind(cl_window$oe_flag_p, cl_window$oe_flag_m))
# normalise
cl_window_normalized <- lapply(cl_window, function(x) apply(x, 1, min_max_noramlize) %>% t(.))
# Metaprofile sums
meta_profiles_sum <- data.frame( endo = colSums(cl_window_normalized$endo, na.rm = T),
oe = colSums(cl_window_normalized$oe, na.rm = T),
flag = colSums(cl_window_normalized$flag, na.rm = T),
pos = -floor(ncol(cl_window_normalized[[1]])/2): floor(ncol(cl_window_normalized$endo)/2))
meta_profiles_sum_gg <- meta_profiles_sum %>% reshape2::melt(id.vars = "pos")
ggplot(meta_profiles_sum_gg, aes(x =pos, y = value, color = variable))+
geom_line(aes(linetype = variable))+
theme_paper()+
scale_color_manual(values = c("red", "blue", "darkblue"))
ggsave(paste0(output_path, "metaprofile.pdf"), width = 10, height = 4, units ="cm")
```
## GVIZ example
```{r}
###############################
# GVIZ function for 3 CLIP data sets
##############################
plotGviz3 <- function(GR, i, w, cl_plus_1, cl_minus_1, cl_plus_2, cl_minus_2, cl_plus_3, cl_minus_3, name, anno, BS, color){
# get chromosome amd range, make data frame
chr<- as.character(seqnames(GR[i]))
df <- GR[i,] %>% as.data.frame
range <- GRanges(seqnames=chr, strand=strand(GR[i]), ranges=IRanges(start= start(GR[i])-w[1], end = end(GR[i])+w[2]))
# make track of crosslinks depending on which strand
# cl_2
if(df$strand=="+"){
cl1_at_region <- subsetByOverlaps(cl_plus_1, range) %>% keepStandardChromosomes()
dTrack1 <- DataTrack(
cl1_at_region,
genome = gen,
type = "histogram", cex.title=cex, cex.axis= cex.a, background.title = "white", col.axis= "black", fontcolor = "black")
cl2_at_region <- subsetByOverlaps(cl_plus_2, range) %>% keepStandardChromosomes()
dTrack2 <- DataTrack(
cl2_at_region,
genome = gen,
type = "histogram", cex.title=cex, cex.axis= cex.a, background.title = "white", col.axis= "black", fontcolor = "black")
cl3_at_region <- subsetByOverlaps(cl_plus_3, range) %>% keepStandardChromosomes()
dTrack3 <- DataTrack(
cl3_at_region,
genome = gen,
type = "histogram", cex.title=cex, cex.axis= cex.a, background.title = "white", col.axis= "black", fontcolor = "black")
}else{
cl1_at_region <- subsetByOverlaps(cl_minus_1, range) %>% keepStandardChromosomes()
dTrack1 <- DataTrack(
cl1_at_region,
genome = gen,
type = "histogram", cex.title=cex, cex.axis= cex.a, background.title = "white", col.axis= "black", fontcolor = "black")
cl2_at_region <- subsetByOverlaps(cl_minus_2, range) %>% keepStandardChromosomes()
dTrack2 <- DataTrack(
cl2_at_region,
genome = gen,
type = "histogram", cex.title=cex, cex.axis= cex.a, background.title = "white", col.axis= "black", fontcolor = "black")
cl3_at_region <- subsetByOverlaps(cl_minus_3, range) %>% keepStandardChromosomes()
dTrack3 <- DataTrack(
cl3_at_region,
genome = gen,
type = "histogram", cex.title=cex, cex.axis= cex.a, background.title = "white", col.axis= "black", fontcolor = "black")
}
# if(df$strand=="+"){
# seqTrack <- SequenceTrack(Hsapiens, chromosome=chr, complement = F)
# }else{
# seqTrack <-SequenceTrack(Hsapiens, chromosome=chr, complement = T)}
#pureclip track
BS_range <- BS[strand(BS)==strand(GR[i])] %>% subsetByOverlaps(.,GR[i]+w[1])
BS_new_track <- GeneRegionTrack(BS_range, genome = gen, name="BS endo",cex.axis= cex.a,
col.histogram=report_color[13], fill=report_color[5], stacking="hide", rotation.title=0, cex.title=cex, background.title = "white", fontcolor = "black")
# track showing location
gtrack <- GenomeAxisTrack(cex.title= 2, add53 = T)
# annotation track
anno_track <- GeneRegionTrack(anno, genome = gen, chromosome = chr,
name = "anno", fill = color, cex.title=cex, cex.axis= cex.a, stacking = "squish", background.title = "transparent", fontcolor = "black")
plotTracks(list(dTrack1, dTrack2, dTrack3, BS_new_track, anno_track, gtrack),
from = start(range), to = end(range),
chromosome=chr, sizes =c(5,5,5,1,1.5, 1),
main= paste(GR[i]$gene_name, "-", chr, ":", start(range),"-", end(range)), cex.main = 1)
}
plotGviz3(GR = regions_for_gviz_4, i = 1, w = c(50,50),
cl_plus_1 = hela_cl$endo_p, cl_minus_1 = hela_cl$endo_m,
cl_plus_2 = hela_cl$oe_pura_p, cl_minus_2 = hela_cl$oe_pura_m,
cl_plus_3 = hela_cl$oe_flag_p, cl_minus_3 = hela_cl$oe_flag_m,
name ="", anno = anno_txdb, BS = BS, color = "darkred")
plotGviz3(GR = regions_for_gviz_4, i = 3, w = c(20,20),
cl_plus_1 = hela_cl$endo_p, cl_minus_1 = hela_cl$endo_m,
cl_plus_2 = hela_cl$oe_pura_p, cl_minus_2 = hela_cl$oe_pura_m,
cl_plus_3 = hela_cl$oe_flag_p, cl_minus_3 = hela_cl$oe_flag_m,
name ="", anno = anno_txdb, BS = BS, color = "darkred")
plotGviz3(GR = regions_for_gviz_4, i = 4, w = c(20,20),
cl_plus_1 = hela_cl$endo_p, cl_minus_1 = hela_cl$endo_m,
cl_plus_2 = hela_cl$oe_pura_p, cl_minus_2 = hela_cl$oe_pura_m,
cl_plus_3 = hela_cl$oe_flag_p, cl_minus_3 = hela_cl$oe_flag_m,
name ="", anno = anno_txdb, BS = BS, color = "darkred")
```