Skip to content

Commit ebdca1e

Browse files
committed
update length_plot
1 parent d033874 commit ebdca1e

3 files changed

Lines changed: 114 additions & 99 deletions

File tree

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ import(RcppArmadillo)
102102
import(dplyr)
103103
import(ggplot2)
104104
import(ggside)
105+
import(methods)
105106
importFrom(Biostrings,AAStringSet)
106107
importFrom(Biostrings,DNAStringSet)
107108
importFrom(Biostrings,GENETIC_CODE)

R/visualization-QC.R

Lines changed: 63 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -296,79 +296,85 @@ setMethod("period_plot",
296296
# function for length plot
297297
# ==============================================================================
298298

299-
#' Generate Length Distribution Plot
300-
#'
301-
#' @description This generic function generates a length distribution plot for sequencing reads.
302-
#'
303-
#' @param object An object containing experimental data.
304-
#' @param ... Additional arguments passed to class-specific methods.
305-
#'
306-
#' @return A ggplot object representing the length distribution of reads.
307-
#'
308-
#' @export
309-
#' @rdname length_plot
310-
setGeneric("length_plot",function(object,...) standardGeneric("length_plot"))
311-
312-
313-
314-
#' @title Length Distribution Plot for Ribo-seq Reads
315-
#' @description The `length_plot` function visualizes the distribution of read lengths in a Ribo-seq dataset.
316-
#' It can either show the total counts of different read lengths (`type = "length"`)
317-
#' or display frame-periodicity information (`type = "frame_length"`).
299+
#' Plot Read Length Distribution or Frame Periodicity for ribotrans Objects
300+
#'
301+
#' This function generates a ggplot visualization of read length distribution
302+
#' or frame-specific read counts (with optional periodicity labels) from a
303+
#' \code{ribotrans} object. It can also aggregate replicates and/or return the
304+
#' underlying summary data.
305+
#'
306+
#' @param object A \code{ribotrans} object. It must contain a \code{summary_info}
307+
#' slot/data.frame with columns \code{sample}, \code{sample_group}, \code{qwidth},
308+
#' \code{count}, \code{pos}, \code{mstart}, and \code{mstop}.
309+
#' @param read_length Numeric vector of length two. Read length range
310+
#' (inclusive) to include in the plot. Default is \code{c(20, 35)}.
311+
#' @param merge_rep Logical. If \code{TRUE}, replicates (same \code{sample_group})
312+
#' are aggregated by mean counts. Default is \code{FALSE}.
313+
#' @param text_size Numeric. Text size for periodicity labels. Default is 4.
314+
#' @param col_width Numeric. Width of the bars in the plot. Default is 0.9.
315+
#' @param col_color Character. Fill color of bars when \code{type = "length"}.
316+
#' Default is \code{"grey30"}.
317+
#' @param add_periodicity_label Logical. If \code{TRUE} and
318+
#' \code{type = "frame_length"}, adds the percentage of reads in the dominant
319+
#' frame as text labels. Default is \code{TRUE}.
320+
#' @param labely_extend Numeric. Proportion by which to extend the y-coordinate
321+
#' of periodicity labels above the top of their bars. Default is 0.05.
322+
#' @param facet A ggplot2 faceting specification, e.g.
323+
#' \code{ggplot2::facet_wrap(~sample, scales = "free")}.
324+
#' Default is \code{ggplot2::facet_wrap(~sample, scales = "free")}.
325+
#' @param return_data Logical. If \code{FALSE} (default), returns a \code{ggplot}
326+
#' object. If \code{TRUE}, returns the processed data.frame used for plotting.
327+
#' @param type Character string specifying the plot type.
328+
#' \code{"length"} for read length distribution,
329+
#' \code{"frame_length"} for frame-resolved counts.
330+
#' Default is \code{c("length", "frame_length")}.
331+
#' @param ... Additional arguments (currently unused).
318332
#'
319-
#' @param object A `ribotrans` object that contains summary read count information.
320-
#' @param read_length A numeric vector of length 2 (default: `c(20, 35)`). Defines the range of read lengths to visualize.
321-
#' @param merge_rep Logical. Whether to merge replicate samples by \code{sample_group}. Default is \code{FALSE}.
322-
#' @param text_size Numeric (default: `4`). Specifies the font size for periodicity labels, applicable when `type = "frame_length"`.
323-
#' @param add_periodicity_label Logical (default: `TRUE`). If `TRUE`, adds periodicity percentage labels to the plot (only for `type = "frame_length"`).
324-
#' @param labely_extend A numeric proportion to extend the y-position of periodicity labels above bars
325-
#' (default: 0.05).
326-
#' @param return_data Logical (default: `FALSE`). If `TRUE`, returns the processed data frame instead of a plot.
327-
#' @param type Character string, either `"length"` (default) or `"frame_length"`.
328-
#' - `"length"`: Displays a bar plot showing the distribution of read lengths.
329-
#' - `"frame_length"`: Displays a bar plot with frame-periodicity information, highlighting the reading frame (0, 1, 2).
330333
#'
331-
#' @return A `ggplot2` object showing the read length distribution or a processed data frame if `return_data = TRUE`.
334+
#' @return If \code{return_data = FALSE}, a \code{ggplot} object. If
335+
#' \code{return_data = TRUE}, a data.frame with columns corresponding to
336+
#' the selected \code{type} and grouping variables.
332337
#'
333-
#' @details
334-
#' - If `type = "length"`, the function summarizes read counts by length.
335-
#' - If `type = "frame_length"`, the function calculates frame-periodicity and shows the proportion of reads mapped to each frame.
336-
#' - Frame-periodicity is computed as: ***percentage of reads in frame-0 relative to all frames***.
337-
#' - Reads outside annotated coding regions (`mstart !=0` or `mstop !=0`) are analyzed for periodicity.
338+
#' @seealso \code{\link[ggplot2]{ggplot}}, \code{\link[dplyr]{group_by}},
339+
#' \code{\link[fastplyr]{f_summarise}}, \code{\link[methods]{setGeneric}}
338340
#'
339341
#' @examples
340342
#' \dontrun{
341-
#' data(ribo_obj) # Assume ribo_obj is a 'ribotrans' object
342-
#'
343-
#' # Plot read length distribution
344-
#' length_plot(ribo_obj, read_length = c(25, 30), type = "length")
345-
#'
346-
#' # Plot frame-periodicity with periodicity labels
347-
#' length_plot(ribo_obj, type = "frame_length", add_periodicity_label = TRUE)
348-
#'
349-
#' # Retrieve processed data
350-
#' df <- length_plot(ribo_obj, return_data = TRUE, type = "frame_length")
351-
#' head(df)
343+
#' ## Assuming rt is a valid ribotrans object:
344+
#' # Simple length distribution plot
345+
#' length_plot(rt)
346+
#'
347+
#' # Frame-resolved plot, merging replicates and returning data
348+
#' df <- length_plot(rt,
349+
#' type = "frame_length",
350+
#' merge_rep = TRUE,
351+
#' return_data = TRUE)
352352
#' }
353353
#'
354-
#' @seealso [ggplot2::geom_col()], [ggplot2::facet_wrap()], [dplyr::mutate()], [scales::label_log()]
355-
#'
356-
#'
357-
#' @importFrom ggplot2 ggplot geom_col scale_y_continuous facet_wrap
358-
#' @importFrom ggplot2 theme element_text element_blank position_dodge2
354+
#' @import ggplot2
359355
#' @importFrom scales label_log
360-
#' @importFrom fastplyr f_group_by f_summarise f_filter f_select
361-
#'
356+
#' @importFrom dplyr group_by summarise rename filter left_join slice_max mutate
357+
#' @importFrom fastplyr f_group_by f_summarise f_filter
358+
#' @import methods
362359
#' @export
363360
#' @rdname length_plot
361+
setGeneric("length_plot",function(object,...) standardGeneric("length_plot"))
362+
363+
364+
365+
#' @rdname length_plot
366+
#' @export
364367
setMethod("length_plot",
365368
signature(object = "ribotrans"),
366369
function(object,
367370
read_length = c(20,35),
368371
merge_rep = FALSE,
369372
text_size = 4,
373+
col_width = 0.9,
374+
col_color = "grey30",
370375
add_periodicity_label = TRUE,
371376
labely_extend = 0.05,
377+
facet = ggplot2::facet_wrap(~sample,scales = "free"),
372378
return_data = FALSE,
373379
type = c("length","frame_length")){
374380
type <- match.arg(type, choices = c("length","frame_length"))
@@ -388,7 +394,7 @@ setMethod("length_plot",
388394
dplyr::rename(sample = sample_group)
389395
}
390396

391-
ptlayer <- geom_col(aes(x = qwidth, y = counts),width = 0.9,fill = "grey30")
397+
ptlayer <- geom_col(aes(x = qwidth, y = counts),width = col_width,fill = col_color)
392398
periodicity.layer <- NULL
393399
col <- NULL
394400
}else{
@@ -419,7 +425,7 @@ setMethod("length_plot",
419425
}
420426

421427
ptlayer <- geom_col(aes(x = qwidth, y = counts, fill = factor(frame)),
422-
width = 0.9, position = position_dodge2())
428+
width = col_width, position = position_dodge2())
423429

424430
# whether add periodicity info
425431
if(add_periodicity_label == TRUE){
@@ -452,7 +458,7 @@ setMethod("length_plot",
452458
theme(panel.grid = element_blank(),
453459
strip.text = element_text(colour = "black",face = "bold",size = rel(1)),
454460
axis.text = element_text(colour = "black")) +
455-
facet_wrap(~sample,scales = "free") +
461+
facet +
456462
scale_y_continuous(labels = scales::label_log(base = 10,digits = 1)) +
457463
xlab("Read length (nt)") + ylab("Number of reads")
458464

man/length_plot.Rd

Lines changed: 50 additions & 42 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)