From 4acf60463074105b7c6f85c50938e16f2840c4cd Mon Sep 17 00:00:00 2001 From: "A.J. Sethi" Date: Tue, 2 Jul 2024 01:07:51 +1000 Subject: [PATCH] add -h flag to Rscripts --- scripts/R2_plotMetaCodon.R | 2 ++ scripts/R2_plotMetaJunction.R | 9 +++++++-- scripts/R2_plotMetaTranscript.R | 13 +++++++++---- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/scripts/R2_plotMetaCodon.R b/scripts/R2_plotMetaCodon.R index 9820609..1b3d34e 100644 --- a/scripts/R2_plotMetaCodon.R +++ b/scripts/R2_plotMetaCodon.R @@ -19,6 +19,7 @@ help_message <- function() { args <- commandArgs(trailingOnly = TRUE) options <- list(ci_method = "loess", output_path = NULL, add_labels = FALSE, plot_from = NULL) +# help message if ("-h" %in% args || length(args) == 0) { help_message() quit(status = 0) @@ -53,6 +54,7 @@ if (!plot_from_specified) { stop("Error: Either -s or -e flag must be specified.") } +# check remaining positional arguments if (length(remaining_args) != 5) { help_message() stop("Error: Incorrect number of positional arguments.") diff --git a/scripts/R2_plotMetaJunction.R b/scripts/R2_plotMetaJunction.R index 603251e..92bca56 100644 --- a/scripts/R2_plotMetaJunction.R +++ b/scripts/R2_plotMetaJunction.R @@ -1,8 +1,13 @@ #!/usr/bin/env Rscript -## read positional arguments while ensuring that the correct number of inputs are provided +# read positional arguments while ensuring that the correct number of inputs are provided args <- commandArgs(trailingOnly = TRUE) +# help message +if ("-h" %in% args || length(args) == 0) { + stop("\nUsage: Rscript R2_plotMetaJunction.R '/path/to/annotated.bed' '/path/to/output.png' '' '' '' [-c 'loess'/'binom'] [-o '/path/to/output_table.tsv']", call. = FALSE) +} + # check for positional argument for the confidence interval method # options are "loess" (default) or binomial confidence interval (-c binom) ci_method <- "loess" @@ -20,7 +25,7 @@ if ("-o" %in% args) { args <- args[-c(index, index + 1)] # Remove the flag and its value from the args } -# check number of positional arguments +# check remaining positional arguments if (length(args) != 5) { stop("\nUsage: Rscript R2_plotMetaJunction.R '/path/to/annotated.bed' '/path/to/output.png' '' '' '' [-c 'loess'/'binom'] [-o '/path/to/output_table.tsv']", call. = FALSE) } diff --git a/scripts/R2_plotMetaTranscript.R b/scripts/R2_plotMetaTranscript.R index acca959..de9f4f5 100644 --- a/scripts/R2_plotMetaTranscript.R +++ b/scripts/R2_plotMetaTranscript.R @@ -1,9 +1,12 @@ #!/usr/bin/env Rscript -## read positional arguments while ensuring that the correct number of inputs are provided +# read positional arguments args <- commandArgs(trailingOnly = TRUE) -## check for positional arguments +# help message +if ("-h" %in% args || length(args) == 0) { + stop("\nUsage: Rscript R2_plotMetaTranscript.R '/path/to/annotated.bed' '/path/to/output.png' '' '' '' [-c 'loess'/'binom'] [-o '/path/to/output.tsv']", call. = FALSE) +} # -c flag for selecting confidence interval method, options are loess (default), or binom confint (-c binom) ci_method <- "loess" @@ -29,6 +32,7 @@ if ("-l" %in% args) { args <- args[-index] } +# check remaining positional arguments if (length(args) != 5) { stop("\nUsage: Rscript R2_plotMetaTranscript.R '/path/to/annotated.bed' '/path/to/output.png' '' '' '' [-c 'loess'/'binom'] [-o '/path/to/output.tsv']", call. = FALSE) } @@ -76,7 +80,7 @@ filter_calls <- function(input_file, col_name, cutoff, direction) { return(calls) } -# define function to calculate the proportion of significant sites in metatranscript bins based on the "transcript_metacoordinate" column from R2D_annotate +# calculate the proportion of significant sites in metatranscript bins based on the "transcript_metacoordinate" column from R2D_annotate compute_ratio <- function(calls, ci_method) { breaks <- seq(0, 3, 0.025) # bin interval can be changed here, default, 120 bins of width 0.025 out_ratio <- calls %>% @@ -96,6 +100,7 @@ compute_ratio <- function(calls, ci_method) { return(out_ratio) } +# make metatranscript plot plot_ratio <- function(out_ratio, ci_method, add_labels) { if (nrow(out_ratio) == 0 || is.infinite(max(out_ratio$interval))) { stop("out_ratio is empty or contains invalid interval data.") @@ -120,7 +125,7 @@ plot_ratio <- function(out_ratio, ci_method, add_labels) { xlab("Relative metatranscriptomic location") + ylab("Proportion of significant sites") + geom_vline(xintercept = c(80,40), col = "black") + scale_x_continuous(breaks = seq(0, 3, by = 0.5), limits = c(0, 3)) + # show x-axis in original metatranscript coordinates - expand_limits(y = y_limit) # Use expand_limits instead of coord_cartesian + expand_limits(y = y_limit) if (add_labels) { p <- p +