-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRefseq_PWD.Rmd
More file actions
1369 lines (1126 loc) · 38.6 KB
/
Refseq_PWD.Rmd
File metadata and controls
1369 lines (1126 loc) · 38.6 KB
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
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "Refseq_PWD"
output: html_document
date: "2025-11-07"
---
```{r}
## ============================================================
## Libraries
## ============================================================
suppressPackageStartupMessages({
library(data.table)
library(dplyr)
library(tidyr)
library(tibble)
library(stringr)
library(ggplot2)
library(GenomicRanges)
library(GenomeInfoDb)
library(BiocParallel)
library(readr)
library(utils) # for URLdecode
})
data.table::setDTthreads(4) # try 3–6 on your Mac; start low if RAM is tight
STD_CHR <- c(paste0("chr",1:22),"chrX","chrY")
```
```{r}
## ============================================================
## 0) Config / helpers
## ============================================================
# Standard chromosomes (UCSC style)
STD_CHR <- c(paste0("chr", 1:22), "chrX", "chrY")
# BiocParallel: safe default for macOS (forked), cap to avoid RAM blow-ups
get_bpparam <- function(n_cores = max(1, parallel::detectCores() - 1L)) {
n_cores <- max(1L, min(n_cores, 6L))
MulticoreParam(workers = n_cores, progressbar = TRUE)
}
# PWD definition at CpG level
pwd_vec <- function(beta1, beta2) abs(beta1 - beta2)
# simple chrom filter for data.frames/tibbles with 'chr' or 'chrom' col
filter_std_chrom <- function(df, col = NULL) {
if (is.null(col)) {
col <- intersect(c("chr","chrom","seqnames"), names(df))[1]
}
if (length(col) == 0) stop("No chromosome column found.")
dplyr::filter(df, .data[[col]] %in% STD_CHR)
}
# Read refseq_all from env or disk (RDS > TSV)
get_refseq_all <- function(rds = "data/refseq_all.rds", tsv = "data/refseq_all.tsv") {
if (exists("refseq_all", inherits = FALSE) && is.data.frame(refseq_all)) return(refseq_all)
if (file.exists(rds)) return(readRDS(rds))
if (file.exists(tsv)) {
x <- readr::read_tsv(tsv, show_col_types = FALSE)
return(mutate(x,
txStart = as.integer(txStart),
txEnd = as.integer(txEnd),
cdsStart = as.integer(cdsStart),
cdsEnd = as.integer(cdsEnd),
exonCount= as.integer(exonCount)))
}
stop("refseq_all not found: supply refseq_all.rds or refseq_all.tsv")
}
## ============================================================
## 1) Build *reduced* interval sets from refseq_all
## type: "tx", "cds", "tss500", "tss1000"
## Global reduction: each base counted at most once per type
## ============================================================
make_intervals_dt <- function(refseq_all,
type = c("tx","cds","tss500","tss1000"),
verbose = TRUE) {
type <- match.arg(type)
rs <- as.data.table(refseq_all)[chrom %in% c(paste0("chr",1:22),"chrX","chrY")]
## 1) Build per-transcript intervals (1-based, closed)
if (type == "tx") {
out <- rs[!is.na(txStart) & !is.na(txEnd) & txEnd > txStart,
.(chr = chrom,
start = txStart + 1L, # 0-based half-open -> 1-based closed
end = txEnd,
name,
gene = name2)]
} else if (type == "cds") {
out <- rs[!is.na(cdsStart) & !is.na(cdsEnd) & cdsEnd > cdsStart,
.(chr = chrom,
start = cdsStart + 1L,
end = cdsEnd,
name,
gene = name2)]
} else {
# TSS-centered windows
w <- if (type == "tss500") 1001L else 2001L
tt <- rs[!is.na(txStart) & !is.na(txEnd),
.(chrom, strand, name, name2,
tss0 = fifelse(strand == "+", txStart, txEnd))]
out <- tt[, .(chr = chrom,
start = pmax(1L, as.integer(tss0 - (w-1L)/2L) + 1L),
end = as.integer(tss0 + (w-1L)/2L),
name = name,
gene = name2)]
}
setDT(out)
# sanity: drop any inverted/NA
out[end < start, `:=`(start = NA_integer_, end = NA_integer_)]
out <- out[!is.na(start) & !is.na(end)]
n_before <- nrow(out)
if (!n_before) {
if (verbose) message(sprintf("[make_intervals_dt:%s] 0 intervals (nothing to reduce)", type))
# still return a data.table with expected columns
out_empty <- data.table(chr = character(), start = integer(), end = integer(),
name = character(), gene = character())
setkey(out_empty, chr, start, end)
return(out_empty[])
}
## 2) Global reduction: union of all intervals (no overlaps)
gr <- GRanges(
seqnames = out$chr,
ranges = IRanges(start = as.integer(out$start),
end = as.integer(out$end))
)
gr_red <- reduce(gr) # <- global union
red_dt <- as.data.table(gr_red)[
, .(chr = as.character(seqnames),
start = as.integer(start),
end = as.integer(end))
]
n_after <- nrow(red_dt)
## 3) Add synthetic IDs; gene is NA (a reduced block may span multiple genes)
red_dt[, name := sprintf("%s_%07d", type, .I)]
red_dt[, gene := NA_character_]
# order columns and key for foverlaps()
setcolorder(red_dt, c("chr","start","end","name","gene"))
setkey(red_dt, chr, start, end)
if (verbose) {
cat(sprintf("[make_intervals_dt:%s] intervals before reduce: %d; after reduce: %d\n",
type, n_before, n_after))
}
red_dt[]
}
## ============================================================
## 2) Read CpG BEDs → GRanges
## Expect columns: chr start end beta cov meth unmeth
## ============================================================
read_cpg_bed_dt <- function(path, min_cov = 8L) {
dt <- fread(path, sep = "\t", header = FALSE,
col.names = c("chr","start","end","beta","cov","meth","unmeth"),
showProgress = FALSE)
dt <- dt[chr %chin% c(paste0("chr",1:22),"chrX","chrY")]
dt <- dt[!is.na(beta) & !is.na(cov) & cov >= min_cov,
.(chr, start = as.integer(start), beta = as.numeric(beta))]
# 1-based position for non-equi overlap
dt[, pos := start + 1L]
dt
}
## ============================================================
## 3) PWD per interval for a pair of samples
## ====================================================
data.table::setDTthreads(4)
# (3) corrected chunked PWD
pwd_for_pair_chunked <- function(pathA, pathB, intervals_dt,
min_cov = 8L, min_cpgs = 1L,
interval_chunk = 50000L) {
A <- read_cpg_bed_dt(pathA, min_cov) # chr, start, beta, pos
B <- read_cpg_bed_dt(pathB, min_cov)
# 1) CpG-level inner join on loci (chr, start)
data.table::setkeyv(A, c("chr","start"))
data.table::setkeyv(B, c("chr","start"))
AB <- B[A, nomatch = 0L] # beta from A, i.beta from B
if (!nrow(AB)) {
return(data.table::data.table(name = character(),
gene = character(),
pwd_mean = numeric(),
n_cpg = integer()))
}
# 2) Represent each CpG as a 1-bp "interval" at pos
AB_pts <- AB[, .(chr,
start = as.integer(pos),
end = as.integer(pos), # or pos+1L if you want half-open
betaA = beta,
betaB = i.beta)]
data.table::setkey(AB_pts, chr, start, end)
# 3) Work per chromosome to keep memory small
chrs <- intersect(unique(AB_pts$chr), unique(intervals_dt$chr))
out_chr <- vector("list", length(chrs))
for (ci in seq_along(chrs)) {
ch <- chrs[ci]
cpg_ch <- AB_pts[chr == ch]
iv_ch <- intervals_dt[chr == ch]
if (!nrow(cpg_ch) || !nrow(iv_ch)) next
# chunk the interval table
idx <- split(seq_len(nrow(iv_ch)),
ceiling(seq_len(nrow(iv_ch)) / interval_chunk))
out_chunks <- vector("list", length(idx))
for (k in seq_along(idx)) {
iv_chunk <- iv_ch[idx[[k]], .(chr, start, end, name, gene)]
iv_chunk[, `:=`(start = as.integer(start), end = as.integer(end))]
data.table::setkey(iv_chunk, chr, start, end)
# non-equi join: CpG points within intervals
j <- data.table::foverlaps(cpg_ch, iv_chunk, nomatch = 0L)
if (!nrow(j)) next
# interval-level PWD: | mean(betaA) - mean(betaB) |
out_chunks[[k]] <- j[, {
mA <- mean(betaA, na.rm = TRUE)
mB <- mean(betaB, na.rm = TRUE)
.(pwd_mean = abs(mA - mB),
n_cpg = .N)
}, by = .(name, gene)]
}
out_chr[[ci]] <- data.table::rbindlist(out_chunks, use.names = TRUE, fill = TRUE)
gc()
}
res <- data.table::rbindlist(out_chr, use.names = TRUE, fill = TRUE)
if (!nrow(res)) return(res)
# enforce minimum CpGs per interval and drop duplicates
unique(res[n_cpg >= min_cpgs])
}
## ============================================================
## 4) Wrapper: run all pairs for one interval set
## ============================================================
run_all_pairs_one_interval_dt <- function(bed_paths, pairs, intervals_dt,
min_cov = 8L, min_cpgs = 1L,
interval_chunk = 50000L) {
out <- vector("list", length(pairs))
nm <- names(pairs)
for (i in seq_along(pairs)) {
s1 <- pairs[[i]][1]; s2 <- pairs[[i]][2]
message(sprintf("Pair %d/%d: %s vs %s", i, length(pairs), s1, s2))
tbl <- pwd_for_pair_chunked(bed_paths[[s1]], bed_paths[[s2]], intervals_dt,
min_cov = min_cov, min_cpgs = min_cpgs,
interval_chunk = interval_chunk)
if (nrow(tbl)) {
tbl[, `:=`(pair = nm[i], sampleA = s1, sampleB = s2)]
out[[i]] <- tbl
}
# free memory aggressively
gc()
}
rbindlist(out, use.names = TRUE, fill = TRUE)
}
## ============================================================
## 5) Plot helper
## ============================================================
plot_pwd <- function(df, title) {
ggplot(df, aes(x = pair, y = pwd_mean)) +
geom_violin(trim = TRUE, scale = "width") +
geom_boxplot(width = 0.12, outlier.size = 0.3) +
# add mean as red dot
stat_summary(
fun = "mean",
geom = "point",
colour = "red",
size = 1.5
) +
labs(title = title, x = NULL, y = "PWD (mean |Δβ| per interval)") +
coord_cartesian(ylim = c(0, 0.5)) +
theme_bw(base_size = 12) +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
}
## ============================================================
## 6) Example driver
## ============================================================
# 6a) bring in refseq_all
refseq_all <- get_refseq_all() %>% filter_std_chrom(col = "chrom")
# 6b) define your BED paths (edit to your actual locations)
# names must be unique sample IDs matching your pairs
bed_paths <- c(
EN = "data/EN.bed",
IN = "data/IN.bed",
JN = "data/JN.bed",
PA = "data/PA.bed", PB = "data/PB.bed",
SA = "data/SA.bed", SB = "data/SB.bed",
XA = "data/XA.bed", XB = "data/XB.bed",
DA = "data/DA.bed", DB = "data/DB.bed",
EA = "data/EA.bed", EB = "data/EB.bed",
FA = "data/FA.bed", FB = "data/FB.bed",
HA = "data/HA.bed", HB = "data/HB.bed",
IA = "data/IA.bed", IB = "data/IB.bed",
JA = "data/JA.bed", JB = "data/JB.bed",
KA = "data/KA.bed", KB = "data/KB.bed",
MA = "data/MA.bed", MB = "data/MB.bed"
)
# 6c) define the 14 pairs (names become group labels on the plots)
pairs <- list(
"Normal EN–IN" = c("EN","IN"),
"Normal EN–JN" = c("EN","JN"),
"Normal IN–JN" = c("IN","JN"),
"Adenoma PA–PB"= c("PA","PB"),
"Adenoma SA–SB"= c("SA","SB"),
"Adenoma XA–XB"= c("XA","XB"),
"CRC DA–DB" = c("DA","DB"),
"CRC EA–EB" = c("EA","EB"),
"CRC FA–FB" = c("FA","FB"),
"CRC HA–HB" = c("HA","HB"),
"CRC IA–IB" = c("IA","IB"),
"CRC JA–JB" = c("JA","JB"),
"CRC KA–KB" = c("KA","KB"),
"CRC MA–MB" = c("MA","MB")
)
# 6d) choose parameters
# build intervals once (globally reduced)
iv_tx_dt <- make_intervals_dt(refseq_all, "tx")
iv_cds_dt <- make_intervals_dt(refseq_all, "cds")
iv_tss500_dt <- make_intervals_dt(refseq_all, "tss500")
iv_tss1000_dt <- make_intervals_dt(refseq_all, "tss1000")
# you can keep these lines; they’re redundant but harmless
iv_tx_dt[, `:=`(start = as.integer(start), end = as.integer(end))]
iv_cds_dt[, `:=`(start = as.integer(start), end = as.integer(end))]
iv_tss500_dt[, `:=`(start = as.integer(start), end = as.integer(end))]
iv_tss1000_dt[, `:=`(start = as.integer(start), end = as.integer(end))]
data.table::setkey(iv_tx_dt, chr, start, end)
data.table::setkey(iv_cds_dt, chr, start, end)
data.table::setkey(iv_tss500_dt, chr, start, end)
data.table::setkey(iv_tss1000_dt, chr, start, end)
```
```{r}
# parameters
min_cov <- 8L
min_cpgs <- 1L
interval_chunk <- 50000L # shrink if still memory tight (e.g., 20k)
# compute
pwd_tx <- run_all_pairs_one_interval_dt(bed_paths, pairs, iv_tx_dt,
min_cov, min_cpgs, interval_chunk)
pwd_cds <- run_all_pairs_one_interval_dt(bed_paths, pairs, iv_cds_dt,
min_cov, min_cpgs, interval_chunk)
pwd_tss500 <- run_all_pairs_one_interval_dt(bed_paths, pairs, iv_tss500_dt,
min_cov, min_cpgs, interval_chunk)
pwd_tss1000 <- run_all_pairs_one_interval_dt(bed_paths, pairs, iv_tss1000_dt,
min_cov, min_cpgs, interval_chunk)
# plotting (unchanged; convert data.table → data.frame if needed)
plot_pwd(as.data.frame(pwd_tx), "PWD — tx")
plot_pwd(as.data.frame(pwd_cds), "PWD — CDS")
plot_pwd(as.data.frame(pwd_tss500), "PWD — TSS ±500")
plot_pwd(as.data.frame(pwd_tss1000), "PWD — TSS ±1000")
# ggsave("pwd_tx.pdf", p_tx, width=10, height=5)
```
```{r}
suppressPackageStartupMessages({ library(dplyr); library(readr); library(tidyr) })
# bind the four result tables you already computed
pwd_all <- bind_rows(
mutate(pwd_tx, interval = "tx"),
mutate(pwd_cds, interval = "cds"),
mutate(pwd_tss500, interval = "tss500"),
mutate(pwd_tss1000, interval = "tss1000")
)
# summary per pair × interval
pwd_summary <- pwd_all %>%
group_by(interval, pair) %>%
summarise(
n_intervals = n(),
# unweighted
mean_pwd = mean(pwd_mean, na.rm = TRUE),
median_pwd = median(pwd_mean, na.rm = TRUE),
q1 = quantile(pwd_mean, 0.25, na.rm = TRUE),
q3 = quantile(pwd_mean, 0.75, na.rm = TRUE),
max_pwd = max(pwd_mean, na.rm = TRUE),
# weighted by number of CpGs contributing to each interval
wmean_pwd = weighted.mean(pwd_mean, w = pmax(n_cpg, 1L), na.rm = TRUE),
.groups = "drop"
) %>%
arrange(interval, pair)
# optional: normals vs adenoma vs CRC label for quick grouping
pwd_summary <- pwd_summary %>%
mutate(group =
case_when(
grepl("^Normal", pair) ~ "Normal",
grepl("^Adenoma", pair) ~ "Adenoma",
grepl("^CRC", pair) ~ "CRC",
TRUE ~ "Other"
))
# save
# write_csv(pwd_summary, "PWD_summary_by_pair_interval.csv")
# peek
print(pwd_summary, n = 50)
# dplyr
pwd_summary %>% dplyr::filter(group == "Normal")
```
```{r}
library(data.table)
library(dplyr)
## Helper: build *raw* (non-reduced) intervals, same definition as before
make_intervals_raw <- function(refseq_all, type = c("tx","cds","tss500","tss1000")) {
type <- match.arg(type)
rs <- as.data.table(refseq_all)[chrom %in% c(paste0("chr",1:22),"chrX","chrY")]
if (type == "tx") {
out <- rs[!is.na(txStart) & !is.na(txEnd) & txEnd > txStart,
.(chr = chrom,
start = txStart + 1L,
end = txEnd)]
} else if (type == "cds") {
out <- rs[!is.na(cdsStart) & !is.na(cdsEnd) & cdsEnd > cdsStart,
.(chr = chrom,
start = cdsStart + 1L,
end = cdsEnd)]
} else {
w <- if (type == "tss500") 1001L else 2001L
tt <- rs[!is.na(txStart) & !is.na(txEnd),
.(chrom, strand,
tss0 = fifelse(strand == "+", txStart, txEnd))]
out <- tt[, .(chr = chrom,
start = pmax(1L, as.integer(tss0 - (w-1L)/2L) + 1L),
end = as.integer(tss0 + (w-1L)/2L))]
}
setDT(out)
out[end < start, `:=`(start = NA_integer_, end = NA_integer_)]
out[!is.na(start) & !is.na(end)]
}
## Helper: summarise a DT of intervals
summarise_intervals <- function(dt) {
if (is.null(dt) || !nrow(dt))
return(data.table(n = 0, min_len = NA_real_, max_len = NA_real_, mean_len = NA_real_))
dt[, len := as.numeric(end - start + 1L)]
dt[, .(
n = .N,
min_len = min(len),
max_len = max(len),
mean_len = mean(len)
)]
}
## Build raw (non-reduced) sets
raw_tx <- make_intervals_raw(refseq_all, "tx")
raw_cds <- make_intervals_raw(refseq_all, "cds")
raw_tss500 <- make_intervals_raw(refseq_all, "tss500")
raw_tss1000 <- make_intervals_raw(refseq_all, "tss1000")
## Summaries before / after reduce
sum_before <- rbindlist(list(
cbind(type = "tx", stage = "before", summarise_intervals(raw_tx)),
cbind(type = "cds", stage = "before", summarise_intervals(raw_cds)),
cbind(type = "tss500", stage = "before", summarise_intervals(raw_tss500)),
cbind(type = "tss1000", stage = "before", summarise_intervals(raw_tss1000))
), use.names = TRUE)
sum_after <- rbindlist(list(
cbind(type = "tx", stage = "after", summarise_intervals(iv_tx_dt)),
cbind(type = "cds", stage = "after", summarise_intervals(iv_cds_dt)),
cbind(type = "tss500", stage = "after", summarise_intervals(iv_tss500_dt)),
cbind(type = "tss1000", stage = "after", summarise_intervals(iv_tss1000_dt))
), use.names = TRUE)
interval_summary <- rbindlist(list(sum_before, sum_after), use.names = TRUE) %>%
arrange(type, stage)
interval_summary
```
```{r}
library(dplyr)
pairs <- refseq_all %>% distinct(txStart, name2)
shared_tx <- pairs %>%
group_by(txStart) %>%
summarise(
n_genes = n_distinct(name2),
genes = list(sort(unique(name2))),
.groups = "drop"
) %>%
filter(n_genes > 1)
shared_tx
```
```{r}
shared_tx_pretty <- shared_tx %>%
mutate(genes = sapply(genes, paste, collapse = ", "))
shared_tx_pretty
```
```{r}
suppressPackageStartupMessages({
library(data.table)
library(ggplot2)
})
STD_CHR <- c(paste0("chr", 1:22), "chrX", "chrY")
## 1) Helper to build *raw* (unreduced) intervals from refseq_all
make_intervals_raw <- function(refseq_all, type = c("tx","cds")) {
type <- match.arg(type)
rs <- as.data.table(refseq_all)[chrom %chin% STD_CHR]
if (type == "tx") {
out <- rs[!is.na(txStart) & !is.na(txEnd) & txEnd > txStart,
.(chr = chrom,
start = txStart + 1L,
end = txEnd)]
} else if (type == "cds") {
out <- rs[!is.na(cdsStart) & !is.na(cdsEnd) & cdsEnd > cdsStart,
.(chr = chrom,
start = cdsStart + 1L,
end = cdsEnd)]
}
out[end < start, `:=`(start = NA_integer_, end = NA_integer_)]
out[!is.na(start)]
}
## 2) Build raw + reduced length table
types <- c("tx","cds")
# raw (before reduce)
raw_list <- lapply(types, function(tp) make_intervals_raw(refseq_all, tp))
names(raw_list) <- types
# reduced (after reduce) – using objects you already have
red_list <- list(
tx = iv_tx_dt,
cds = iv_cds_dt
# tss500 = iv_tss500_dt,
# tss1000 = iv_tss1000_dt
)
# construct long length table
len_long <- rbindlist(lapply(types, function(tp) {
raw <- copy(raw_list[[tp]])[, .(len = end - start + 1L)]
raw[, `:=`(type = tp, stage = "before")]
red <- copy(red_list[[tp]])[, .(len = end - start + 1L)]
red[, `:=`(type = tp, stage = "after")]
rbind(raw, red)
}), use.names = TRUE)
len_means <- len_long %>%
group_by(type, stage) %>%
summarise(mean_len = mean(len), .groups = "drop")
## 2) Histogram with frequency + mean lines
ggplot(len_long, aes(x = len, fill = stage)) +
geom_histogram(
aes(y = after_stat(..count.. / sum(..count..))), # proportion
position = "identity",
alpha = 0.4,
bins = 80
) +
# mean lines (before vs after)
geom_vline(
data = len_means,
aes(xintercept = mean_len, color = stage),
linetype = "dashed",
linewidth = 0.5,
show.legend = FALSE
) +
xlim(1, 100000) +
facet_wrap(~ type, scales = "free_y") +
labs(
x = "Interval length (bp)",
y = "Frequency (proportion within group)",
fill = "Stage",
title = "Interval length distributions before vs after reduction"
) +
theme_bw(base_size = 12)
```
## Adding colon information
```{r}
read_ccres_by_class <- function(path_bed,
classes = c("PLS","pELS","dELS","CA-only","CA-CTCF","CA-H3K4me3","CA-TF","TF")) {
# Read with many columns; keep only chr/start/end + the column that carries class labels
dt <- fread(path_bed, header = FALSE, sep = "\t", quote = "", data.table = TRUE, showProgress = FALSE)
# Force generic names V1..Vn
setnames(dt, paste0("V", seq_len(ncol(dt))))
# Identify candidate character columns
char_cols <- names(dt)[vapply(dt, is.character, logical(1))]
if (length(char_cols) == 0L) stop("No character columns to detect cCRE class.")
# Find the column most likely to contain the class labels
score_col <- function(col) sum(dt[[col]] %chin% classes, na.rm = TRUE)
hits <- vapply(char_cols, score_col, integer(1))
class_col <- if (max(hits) > 0) names(hits)[which.max(hits)] else {
# Fallback heuristic: often second-to-last column stores class in SCREEN BEDs
names(dt)[max(1, ncol(dt)-1)]
}
# Keep skinny table
cc <- dt[, .(chr = as.character(V1),
start = as.integer(V2),
end = as.integer(V3),
class = as.character(get(class_col)))]
cc <- cc[chr %chin% STD_CHR & !is.na(start) & !is.na(end) & end > start]
# Subset to desired classes
cc <- cc[class %chin% classes]
# Split to list by class and key for fast overlaps
cc_list <- split(cc, by = "class", drop = TRUE, keep.by = FALSE)
for (nm in names(cc_list)) {
setkey(cc_list[[nm]], chr, start, end)
}
cc_list
}
# use it
ccre_path <- "data/large_intestine.noccl.cCREs.bed"
ccre_list <- read_ccres_by_class(ccre_path)
names(ccre_list)
```
```{r}
reduce_ccre_list <- function(ccre_list) {
out_list <- vector("list", length(ccre_list))
sum_list <- vector("list", length(ccre_list))
names(out_list) <- names(sum_list) <- names(ccre_list)
for (nm in names(ccre_list)) {
dt0 <- ccre_list[[nm]][chr %chin% STD_CHR]
if (!nrow(dt0)) {
sum_list[[nm]] <- data.table(
class = nm,
n_before = 0L,
min_len_before = NA_real_,
mean_len_before= NA_real_,
max_len_before = NA_real_,
n_after = 0L,
min_len_after = NA_real_,
mean_len_after = NA_real_,
max_len_after = NA_real_
)
out_list[[nm]] <- data.table(chr=character(), start=integer(),
end=integer(), name=character(), gene=character())
next
}
## lengths BEFORE reduction (BED convention: length = end - start)
len_before <- dt0$end - dt0$start
## convert to 1-based inclusive for GRanges & PWD
dt1 <- copy(dt0)[, start := start + 1L]
gr <- GRanges(
seqnames = dt1$chr,
ranges = IRanges::IRanges(start = dt1$start, end = dt1$end)
)
gr_red <- GenomicRanges::reduce(gr, ignore.strand = TRUE)
rd <- as.data.table(gr_red)[,
.(chr = as.character(seqnames),
start = as.integer(start),
end = as.integer(end))
]
## add unique ID per reduced interval (needed for grouping PWD)
rd[, `:=`(
name = sprintf("%s_%06d", nm, .I),
gene = nm
)]
setkey(rd, chr, start, end)
len_after <- rd$end - rd$start + 1L # lengths after reduction (inclusive)
sum_list[[nm]] <- data.table(
class = nm,
n_before = nrow(dt0),
min_len_before = min(len_before),
mean_len_before = mean(len_before),
max_len_before = max(len_before),
n_after = nrow(rd),
min_len_after = min(len_after),
mean_len_after = mean(len_after),
max_len_after = max(len_after)
)
out_list[[nm]] <- rd
}
summary_dt <- rbindlist(sum_list, use.names = TRUE)
list(intervals = out_list, summary = summary_dt)
}
ccre_red <- reduce_ccre_list(ccre_list)
ccre_iv_list <- ccre_red$intervals # reduced intervals per class
ccre_len_summary <- ccre_red$summary # before/after metrics
ccre_len_summary[]
```
```{r}
dt_raw <- data.table::fread("data/large_intestine.noccl.cCREs.bed",
header = FALSE, sep = "\t", quote = "",
data.table = TRUE, showProgress = FALSE)
setnames(dt_raw, paste0("V", seq_len(ncol(dt_raw))))
# what are the character columns?
char_cols <- names(dt_raw)[vapply(dt_raw, is.character, logical(1))]
char_cols
# For each candidate column, look at top labels
for (cn in char_cols) {
cat("\n=== ", cn, " ===\n")
print(dt_raw[, .N, by = get(cn)][order(-N)][1:10])
}
```
```{r}
# parameters already defined earlier:
# bed_paths, pairs, min_cov, min_cpgs, interval_chunk
pwd_ccre_list <- lapply(names(ccre_iv_list), function(nm) {
iv_dt <- ccre_iv_list[[nm]]
message("Computing PWD for cCRE class: ", nm)
tbl <- run_all_pairs_one_interval_dt(
bed_paths,
pairs,
intervals_dt = iv_dt,
min_cov = min_cov,
min_cpgs = min_cpgs,
interval_chunk = interval_chunk
)
if (nrow(tbl)) {
tbl[, feature := nm]
}
tbl
})
pwd_ccre <- data.table::rbindlist(pwd_ccre_list, use.names = TRUE, fill = TRUE)
pwd_ccre[]
unique_features <- unique(pwd_ccre$feature)
for (nm in unique_features) {
df_nm <- as.data.frame(pwd_ccre[feature == nm])
print(
plot_pwd(df_nm, paste0("PWD — cCRE class ", nm))
)
}
```
## CPG islands info
```{r}
read_ucsc_cpg_islands <- function(path_tsv) {
# Typical columns: chrom, chromStart, chromEnd, ...
dt <- fread(path_tsv, sep = "\t", header = TRUE,
data.table = TRUE, showProgress = FALSE)
cn <- names(dt)
chr_col <- cn[grepl("^chr$|^chrom$", tolower(cn))][1]
st_col <- cn[grepl("start", tolower(cn))][1]
en_col <- cn[grepl("end", tolower(cn))][1]
if (any(is.na(c(chr_col, st_col, en_col)))) {
stop("Could not detect chrom/start/end columns in CpG island file.")
}
# UCSC bed: chromStart = 0-based, chromEnd = 1-based exclusive
out <- dt[, .(
chr = as.character(get(chr_col)),
start = as.integer(get(st_col) + 1L), # -> 1-based inclusive
end = as.integer(get(en_col)) # already 1-based
)]
out <- out[chr %chin% STD_CHR & !is.na(start) & !is.na(end) & end > start]
setkey(out, chr, start, end)
out
}
cpgis_path <- "data/ucsc_cpgislandext.tsv"
cpg_islands0 <- read_ucsc_cpg_islands(cpgis_path) # 1-based inclusive
# Interval lengths BEFORE reduction
cgi_len_before <- cpg_islands0$end - cpg_islands0$start + 1L
# Global reduction via GRanges (union of all islands)
gr_cgi <- GRanges(
seqnames = cpg_islands0$chr,
ranges = IRanges(start = cpg_islands0$start,
end = cpg_islands0$end)
)
gr_cgi_red <- reduce(gr_cgi, ignore.strand = TRUE)
cpg_islands <- as.data.table(gr_cgi_red)[
, .(chr = as.character(seqnames),
start = as.integer(start),
end = as.integer(end))
]
# Add interval IDs + "gene" label so it fits PWD pipeline
cpg_islands[, `:=`(
name = sprintf("CGI_%06d", .I),
gene = "CpG_island"
)]
setkey(cpg_islands, chr, start, end)
cgi_len_after <- cpg_islands$end - cpg_islands$start + 1L
# Summary of intervals before/after reduction
cpg_interval_summary <- data.table(
stage = c("before", "after"),
n = c(length(cgi_len_before), length(cgi_len_after)),
min_len = c(min(cgi_len_before), min(cgi_len_after)),
mean_len= c(mean(cgi_len_before), mean(cgi_len_after)),
max_len = c(max(cgi_len_before), max(cgi_len_after))
)
cpg_interval_summary[]
```
```{r}
# PWD on reduced CpG islands
pwd_cgi <- run_all_pairs_one_interval_dt(
bed_paths,
pairs,
intervals_dt = cpg_islands,
min_cov = min_cov,
min_cpgs = min_cpgs,
interval_chunk = interval_chunk
)
# Quick plot
if (nrow(pwd_cgi)) {
plot_pwd(as.data.frame(pwd_cgi), "PWD — CpG islands")
}
```
```{r}
pwd_cgi_summary <- pwd_cgi %>%
as_tibble() %>%
mutate(feature = "CpG_island") %>%
group_by(feature, pair) %>%
summarise(
n_intervals = n(),
median_pwd = median(pwd_mean, na.rm = TRUE),
wmean_pwd = weighted.mean(pwd_mean, n_cpg, na.rm = TRUE),
.groups = "drop"
)
pwd_cgi_summary %>% print(n = nrow(.))
```
## Adding peakdata
```{r}
STD_CHR <- c(paste0("chr", 1:22), "chrX", "chrY")
read_tf_peaks <- function(path_tf_bed) {
# skip the 'track name="..."' header line
dt <- fread(
path_tf_bed,
sep = "\t",
header = FALSE,
skip = 1L,
data.table = TRUE,
showProgress = FALSE
)
# Give columns names (BED9-ish; tolerate extra columns)
setnames(dt, paste0("V", seq_len(ncol(dt))))
cols <- c("chr","start","end","anno","score","strand","thickStart","thickEnd","rgb")
setnames(dt, old = names(dt)[seq_along(cols)], new = cols)
# Keep standard chromosomes and clean coordinates
tf <- dt[
chr %chin% STD_CHR &
!is.na(start) & !is.na(end) & end > start
]
# BED is 0-based half-open -> convert to 1-based inclusive
tf[, `:=`(
chr = as.character(chr),
start = as.integer(start) + 1L,
end = as.integer(end),
score = as.numeric(score),
anno = as.character(anno)
)]
## --------- parse annotation string ---------
# 1) TF name from "Name=...%20(@%20...);" (allow internal %20 etc.)
# capture lazily up to "%20(@"
m_name <- str_match(tf$anno, "Name=([^;]+?)%20\\(@")
tf_name_raw <- m_name[, 2]
# URL-decode (handles %20, %28, etc.)
tf_name_dec <- ifelse(
!is.na(tf_name_raw),
URLdecode(tf_name_raw),
NA_character_
)
# 1b) fallback: if Name= is missing, try hgn=HOXA6;
m_hgn <- str_match(tf$anno, "hgn=([^;]+);")
hgn_raw <- m_hgn[, 2]
hgn_dec <- ifelse(
!is.na(hgn_raw),
URLdecode(hgn_raw),
NA_character_
)
tf_name_final <- ifelse(
!is.na(tf_name_dec) & tf_name_dec != "",
tf_name_dec,
hgn_dec
)
# 2) cell type from "cell%20type=...;"
m_ct <- str_match(tf$anno, "cell%20type=([^;]+);")
ct_raw <- m_ct[, 2]
# URL-decode + clean trailing "cell line(s)"
cell_type_dec <- ifelse(
!is.na(ct_raw),
URLdecode(ct_raw),
NA_character_
)
cell_type_clean <- ifelse(