Skip to content

Commit 70caf7a

Browse files
authored
Merge pull request #5 from arkhammknight/master
Update Region_File
2 parents d521054 + c3d8ec2 commit 70caf7a

File tree

1 file changed

+47
-38
lines changed

1 file changed

+47
-38
lines changed

bedgraph-visualizer.R

Lines changed: 47 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,6 @@ colnames(lrr_data) <- c("Chr", "Start", "End", "LRR")
3535
library(segmented)
3636

3737
getSmoothLine <- function(LRRs, region=NULL, smoothNum = 10) {
38-
39-
# if (!is.null(region)){
40-
# lm_fit <- lm(LRR ~ Start, LRRs)
41-
# seg_fit <- segmented(
42-
# lm_fit,
43-
# seg.Z = ~Start,
44-
# psi=list(Start=c(region$Start, region$Start + 1, region$End - 1, region$End)),
45-
# control = seg.control(it.max = 0)
46-
# )
47-
# smoothed <- data.frame(Pos = LRRs$Start, LRR=predict(seg_fit))
48-
# return(smoothed)
49-
# } else {
50-
# https://github.com/gzhmat/ShinyCNV/blob/e0307df97d52f80a540e6eddbaa34df34d22e95e/config.R#L80
5138
rowNum <- nrow(LRRs)
5239
smoothed <- LRRs %>%
5340
arrange(Start) %>%
@@ -57,10 +44,8 @@ getSmoothLine <- function(LRRs, region=NULL, smoothNum = 10) {
5744
ungroup()
5845

5946
return(smoothed)
60-
# }
6147
}
6248

63-
6449
# Function to plot the entire genome
6550
genome_plot <- function() {
6651
# Unique chromosomes
@@ -80,23 +65,27 @@ genome_plot <- function() {
8065
# Create LRR plot
8166
lrr_plot <- ggplot(lrr_chr_data) +
8267
geom_point(aes(x = Start, y = LRR), color = "#2a2a2a") +
83-
geom_line(data = smooth_line, aes(x = Pos, y = LRR), color = "#ff3333") + # Use the custom smooth line
68+
geom_line(data = smooth_line, aes(x = Pos, y = LRR), color = "#ff3333") +
69+
geom_rect(aes(xmin = min(Start), xmax = min(Start) + 100000, ymin = -Inf, ymax = Inf),
70+
fill = "yellow", alpha = 0.005) +
71+
geom_rect(aes(xmin = max(Start) - 100000, xmax = max(Start), ymin = -Inf, ymax = Inf),
72+
fill = "yellow", alpha = 0.005) +
8473
theme_minimal() +
8574
labs(x = NULL, y = "LRR", title = paste("chr", chr, sep="")) +
86-
geom_hline(yintercept = 0, linetype = "dotted", color = "#555555") + # Guide line for LRR at y = 0
87-
coord_cartesian(ylim = c(-1, 1)) + # Set the y-axis limits for LRR
88-
scale_x_continuous(labels = label_number()) + # Format x-axis labels as numeric
89-
scale_y_continuous(expand = c(0, 0)) # Avoid extra space at the plot edges
75+
geom_hline(yintercept = 0, linetype = "dotted", color = "#555555") +
76+
coord_cartesian(ylim = c(-1, 1)) +
77+
scale_x_continuous(labels = label_number()) +
78+
scale_y_continuous(expand = c(0, 0))
9079

9180
# Create BAF plot
9281
baf_plot <- ggplot(baf_chr_data) +
9382
geom_point(aes(x = Start, y = BAF), color = "#2a2a2a") +
9483
theme_minimal() +
9584
labs(x = "Position", y = "BAF") +
96-
geom_hline(yintercept = 0.5, linetype = "dotted", color = "#555555") + # Guide line for BAF at y = 0.5
85+
geom_hline(yintercept = 0.5, linetype = "dotted", color = "#555555") +
9786
coord_cartesian(ylim = c(0, 1)) +
98-
scale_x_continuous(labels = label_number()) + # Format x-axis labels as numeric
99-
scale_y_continuous(expand = c(0, 0)) # Avoid extra space at the plot edges
87+
scale_x_continuous(labels = label_number()) +
88+
scale_y_continuous(expand = c(0, 0))
10089

10190
# Store plots in lists
10291
lrr_plots[[chr]] <- lrr_plot
@@ -117,48 +106,68 @@ genome_plot <- function() {
117106
ggsave(output_file, plot = combined_plot, width = 30, height = 40, dpi = 300)
118107
}
119108

120-
# Function to plot specific regions
121109
region_plot <- function(region_file, min_padding=600000) {
122-
# Load region data
123-
regions <- read.table(region_file, header = FALSE)
110+
# Add tryCatch to handle potential errors when reading the region file
111+
regions <- tryCatch({
112+
regions_data <- read.table(region_file, header = FALSE)
113+
if (nrow(regions_data) == 0) {
114+
message("Region file is empty. No regions to plot.")
115+
return(invisible(NULL)) # Return silently without error
116+
}
117+
regions_data
118+
}, error = function(e) {
119+
message("Error reading region file: ", e$message)
120+
return(invisible(NULL)) # Return silently without error
121+
})
122+
123+
# If regions is NULL, return without processing further
124+
if (is.null(regions)) {
125+
return(invisible(NULL))
126+
}
127+
124128
colnames(regions) <- c("Chr", "Start", "End", "Type")
125129

126130
# Iterate over each region and create plots
127131
for (i in 1:nrow(regions)) {
128132
region <- regions[i, ]
129-
padding = (region$End - region$Start )
133+
padding = (region$End - region$Start)
130134
if (padding <= min_padding) {
131135
padding = min_padding
132136
}
133137
padded_start = region$Start - padding
134138
padded_end = region$End + padding
135139

136140
# Filter BAF and LRR data for the current region
137-
filtered_baf <- baf_data %>%
141+
filtered_baf <- baf_data %>%
138142
filter(Chr == region$Chr & Start >= padded_start & Start <= padded_end)
139143

140-
filtered_lrr <- lrr_data %>%
144+
filtered_lrr <- lrr_data %>%
141145
filter(Chr == region$Chr & Start >= padded_start & Start <= padded_end)
142146

143147
smooth_line <- getSmoothLine(filtered_lrr, region)
148+
144149
# Create the LRR plot with smooth line
145150
lrr_plot <- ggplot(filtered_lrr) +
146-
geom_point(aes(x = Start, y = LRR), color = "#2a2a2a") +
147-
geom_line(data = smooth_line, aes(x = Pos, y = LRR), color = "#ff3333") + # Use the custom smooth line
151+
geom_point(aes(x = Start, y = LRR), color = "#2a2a2a", size= 0.3) +
152+
geom_line(data = smooth_line, aes(x = Pos, y = LRR), color = "#ff3333") +
148153
theme_minimal() +
149154
labs(x = NULL, y = "LRR", title = paste(region$Chr, ":", region$Start, "-", region$End, sep="")) +
150-
geom_hline(yintercept = 0, linetype = "dotted", color = "#555555") + # Guide line for LRR at y = 0
151-
coord_cartesian(ylim = c(-1, 1)) + # Set the y-axis limits for LRR
152-
scale_x_continuous(labels = label_number()) # Format x-axis labels as numeric
155+
geom_hline(yintercept = 0, linetype = "dotted", color = "#555555") +
156+
coord_cartesian(ylim = c(-1, 1)) +
157+
scale_x_continuous(labels = label_number()) +
158+
geom_rect(aes(xmin = region$Start, xmax = region$End, ymin = -Inf, ymax = Inf),
159+
fill = "yellow", alpha = 0.005, inherit.aes = FALSE)
153160

154-
# Create the BAF plot
161+
# Create the BAF plot with highlight at CNV region
155162
baf_plot <- ggplot(filtered_baf) +
156-
geom_point(aes(x = Start, y = BAF), color = "#2a2a2a") +
163+
geom_point(aes(x = Start, y = BAF), color = "#2a2a2a", size= 0.3) +
157164
theme_minimal() +
158165
labs(x = "Position", y = "BAF") +
159-
geom_hline(yintercept = 0.5, linetype = "dotted", color = "#555555") + # Guide line for BAF at y = 0.5
166+
geom_hline(yintercept = 0.5, linetype = "dotted", color = "#555555") +
160167
coord_cartesian(ylim = c(0, 1)) +
161-
scale_x_continuous(labels = label_number()) # Format x-axis labels as numeric
168+
scale_x_continuous(labels = label_number()) +
169+
geom_rect(aes(xmin = region$Start, xmax = region$End, ymin = -Inf, ymax = Inf),
170+
fill = "yellow", alpha = 0.005, inherit.aes = FALSE)
162171

163172
# Combine the plots vertically
164173
combined_plot <- plot_grid(lrr_plot, baf_plot, align = "v", ncol = 1, rel_heights = c(1, 1))

0 commit comments

Comments
 (0)