2025-09-11
# Libraries
library(karyoploteR)
library(rtracklayer)
library(dplyr)
library(ragg) # headless PNG device
# knitr defaults: write images to a tracked folder for GitHub
knitr::opts_chunk$set(
fig.path = "figures/S1G-J-",
dev = "ragg_png",
dpi = 150,
out.width = "100%",
message = FALSE,
warning = FALSE
)
# -------- Parameters --------
bed_path <- "/groups/gerlich/labinfo/Projects/FedericoTeloni/Results/Datasummeries/jupyter/HS17/HS17.bed"
chroms <- paste0("chr", c(1:22, "X")) # no chrY
genome <- "hg19"
# -------- Load once --------
bed_df <- read.table(bed_path, header = FALSE, sep = "\t", stringsAsFactors = FALSE)
colnames(bed_df) <- c("chr", "start", "mismatch", "cut_state")
# -------- Helpers --------
init_karyoplot <- function(genome, chroms) {
kp <- plotKaryotype(genome = genome, chromosomes = chroms, plot.type = 6)
kpDataBackground(kp, color = "white")
kp
}
plot_segments <- function(kp, df, col = "red", lwd = 2, lty = 1) {
if (nrow(df) == 0) return(invisible(NULL))
kpSegments(kp,
chr = df$chr,
x0 = df$start, x1 = df$start,
y0 = 0, y1 = 1,
col = col, lwd = lwd, lty = lty)
}# Subsets
mismatch_0 <- bed_df %>% filter(mismatch == 0)
mismatch_1 <- bed_df %>% filter(mismatch == 1)
# Plot
kp <- init_karyoplot(genome, chroms)
plot_segments(kp, mismatch_0, col = "red", lwd = 2, lty = 1) # solid
plot_segments(kp, mismatch_1, col = "red", lwd = 2, lty = 3) # dottedyes_data <- bed_df %>% filter(cut_state == "YES")
kp <- init_karyoplot(genome, chroms)
plot_segments(kp, yes_data, col = "red", lwd = 2, lty = 1)
