Skip to content

Commit c593da5

Browse files
committed
add reverse focus for metacodon plots
1 parent 86e0455 commit c593da5

File tree

3 files changed

+38
-19
lines changed

3 files changed

+38
-19
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,8 @@ Arguments:
282282
Optional arguments:
283283
- The flag "-c [method]" can be used to change the strategy used for displaying confidence intervals between loess (default) or binoial confidence intervals (-c "binom")
284284
- The flag "-o [/path/to/table.tsv]" can be used to save the aggregated metacodon data as a tab-separated file
285+
- The flag "-r" reverses the focus of the plot, so that codons are centered at x = 0 and feature abundance is shown around junctions, rather than vice-versa. See example *metajunction* plots above for more information.
286+
285287
```
286288
**Plot** the **metajunction** distribution of RNA features:
287289

@@ -299,6 +301,8 @@ Arguments:
299301
Optional arguments:
300302
- The flag "-c [method]" can be used to change the strategy used for displaying confidence intervals between loess (default) or binoial confidence intervals (-c "binom")
301303
- The flag "-o [/path/to/table.tsv]" can be used to save the aggregated metajunction data as a tab-separated file
304+
- The flag "-r" reverses the focus of the plot, so that junctions are centered at x = 0 and feature abundance is shown around junctions, rather than vice-versa. See example *metajunction* plots above for more information.
305+
302306
```
303307

304308
More information on ```r2d``` plot functions can be found on the [R2Dtool wiki pages](https://github.com/comprna/R2Dtool/wiki/Visualising-RNA-feature-distributions-with-R2Dtool)

scripts/R2_plotMetaCodon.R

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ cat("Arguments received:\n")
99
cat(paste(commandArgs(trailingOnly = TRUE), collapse = " "), "\n")
1010

1111
help_message <- function() {
12-
cat("\nUsage: Rscript script.R '/path/to/annotated.bed' '/path/to/output.png' '<probability field>' '<cutoff>' '<upper/lower>' [-c 'loess'/'binom'] [-o '/path/to/output.tsv'] [-l] (-s | -e)\n")
12+
cat("\nUsage: Rscript script.R '/path/to/annotated.bed' '/path/to/output.png' '<probability field>' '<cutoff>' '<upper/lower>' [-c 'loess'/'binom'] [-o '/path/to/output.tsv'] [-l] (-s | -e) [-R]\n")
1313
cat("Options:\n")
1414
cat(" -c Set confidence interval method; default is 'loess', alternative is 'binom'.\n")
1515
cat(" -o Set output path for processed data table.\n")
1616
cat(" -l Add metagene labels to the plot; default is no labels.\n")
1717
cat(" -s Plot from the start codon using 'abs_cds_start'.\n")
1818
cat(" -e Plot from the end codon using 'abs_cds_end'.\n")
19+
cat(" -R Reverse x-axis sign and show distribution of m6A sites.\n")
1920
cat(" -h Show this help message and exit.\n")
2021
}
2122

@@ -36,7 +37,7 @@ col_name <- required_args[3]
3637
cutoff <- as.numeric(required_args[4])
3738
direction <- required_args[5]
3839

39-
options <- list(ci_method = "loess", output_path = NULL, add_labels = FALSE, plot_from = NULL)
40+
options <- list(ci_method = "loess", output_path = NULL, add_labels = FALSE, plot_from = NULL, reverse_x = FALSE)
4041

4142
# parse optional arguments
4243
i <- 1
@@ -66,6 +67,9 @@ while (i <= length(optional_args)) {
6667
stop("Error: Both -s and -e flags cannot be used simultaneously.")
6768
}
6869
i <- i + 1
70+
} else if (flag == "-R") {
71+
options$reverse_x <- TRUE
72+
i <- i + 1
6973
} else {
7074
warning(paste("Unrecognized flag:", flag))
7175
i <- i + 1
@@ -91,12 +95,12 @@ filter_calls <- function(file, col, cutoff, direction) {
9195
message("Error reading the file: ", e$message)
9296
stop(e)
9397
})
94-
98+
9599
# ensure the filter field column is present in the R2Dtool annotate output
96100
if (!col %in% names(calls)) {
97101
stop(paste("Column", col, "does not exist in the input file. R2Dtool annotate must be run in header mode (i.e. with -H flag) for plotMetaCodon to work"))
98102
}
99-
103+
100104
calls <- calls %>%
101105
mutate(filter = if_else(.data[[col]] >= cutoff, "sig", "ns")) %>%
102106
mutate(filter = if (direction == "lower") if_else(filter == "sig", "ns", "sig") else filter)
@@ -106,11 +110,9 @@ filter_calls <- function(file, col, cutoff, direction) {
106110

107111
# calculate ratio of significant sites
108112
compute_ratio <- function(calls, interval) {
109-
110-
111113
calls <- calls %>%
112114
filter(.data[[interval]] >= -100 & .data[[interval]] <= 100)
113-
115+
114116
out_ratio <- calls %>%
115117
group_by(.data[[interval]], filter) %>%
116118
summarise(n = n(), .groups = 'drop') %>%
@@ -119,8 +121,7 @@ compute_ratio <- function(calls, interval) {
119121
ns = if("ns" %in% names(.)) ns else 0,
120122
ratio = sig / (sig + ns + 1e-9)) %>%
121123
rename(interval = 1)
122-
123-
124+
124125
if (options$ci_method == "binom") {
125126
conf_int <- binom.confint(out_ratio$sig, out_ratio$sig + out_ratio$ns, methods = "wilson")
126127
out_ratio <- mutate(out_ratio, lower = conf_int$lower, upper = conf_int$upper)
@@ -133,35 +134,37 @@ plot_ratio <- function(out_ratio) {
133134
if (nrow(out_ratio) == 0 || is.infinite(max(out_ratio$interval))) {
134135
stop("out_ratio is empty or contains invalid interval data.")
135136
}
136-
137+
137138
title_prefix <- ifelse(options$plot_from == "abs_cds_start", "start", "stop")
138139
if (options$plot_from == "abs_cds_start") {
139140
labels <- c("5' UTR", "CDS")
140141
} else {
141142
labels <- c("CDS", "3' UTR")
142143
}
143-
144-
p <- ggplot(out_ratio, aes(x = interval, y = ratio)) +
144+
145+
x_values <- if(options$reverse_x) -out_ratio$interval else out_ratio$interval
146+
x_label <- if(options$reverse_x) "Distribution of m6A sites around codon" else "Distance from m6A site to codon"
147+
148+
p <- ggplot(out_ratio, aes(x = x_values, y = ratio)) +
145149
geom_point(alpha = 0.5, color = "red") +
146150
geom_vline(xintercept = 0, color = "blue", linetype = "dashed") +
147151
geom_smooth(method = "loess", se = options$ci_method == "loess") +
148152
theme_minimal() +
149153
labs(title = paste("Proportion of significant sites around", title_prefix, "codon"),
150-
x = "Absolute metatranscriptomic location", y = "Proportion of significant sites")
151-
154+
x = x_label, y = "Proportion of significant sites")
155+
152156
if (options$add_labels) {
153157
max_ratio <- max(out_ratio$ratio, na.rm = TRUE)
154158
print(labels[1])
155159
print(labels[2])
156160
p <- p + geom_text(aes(label = labels[1]), x = -50, y = 0.9*max_ratio, vjust = -1, size=12) +
157-
geom_text(aes(label = labels[2]), x = 50, y = 0.9*max_ratio, vjust = -1, size=12)
158-
161+
geom_text(aes(label = labels[2]), x = 50, y = 0.9*max_ratio, vjust = -1, size=12)
159162
}
160-
163+
161164
if (options$ci_method == "binom") {
162165
p <- p + geom_ribbon(aes(ymin = lower, ymax = upper), alpha = 0.2)
163166
}
164-
167+
165168
return(p)
166169
}
167170

@@ -178,4 +181,4 @@ if (!is.null(options$output_path)) {
178181

179182
p <- plot_ratio(out_ratio)
180183

181-
ggsave(output_file, p, scale = 4, width = 600, height = 400, units = "px")
184+
ggsave(output_file, p, scale = 4, width = 600, height = 400, units = "px")

src/main.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,14 @@ fn main() {
325325
.value_name("FILE")
326326
.help("Save the aggregated metacodon data as a tab-separated file")
327327
)
328+
.arg(
329+
Arg::new("reverse_focus")
330+
.short('r')
331+
.long("reverse")
332+
.help("Reverse x-axis sign and show distribution of m6A sites")
333+
.action(clap::ArgAction::SetTrue)
334+
.required(false)
335+
)
328336
.group(
329337
clap::ArgGroup::new("codon_type")
330338
.required(true)
@@ -456,6 +464,10 @@ if let Some(matches) = matches.subcommand_matches("plotMetaCodon") {
456464
args.extend_from_slice(&["-o".to_string(), save_table_path.to_string_lossy().into_owned()]);
457465
}
458466

467+
if matches.get_flag("reverse_focus") {
468+
args.push("-R".to_string());
469+
}
470+
459471
println!("Arguments being passed to R2_plotMetaCodon.R:");
460472
println!("Rscript R2_plotMetaCodon.R {}", args.join(" "));
461473

0 commit comments

Comments
 (0)