Add option to show all chromosomes in positions plot

This commit is contained in:
Elias Projahn 2022-05-23 11:40:28 +02:00
parent d83dfa0574
commit a1e6147466
3 changed files with 36 additions and 20 deletions

View file

@ -17,7 +17,7 @@ Encoding: UTF-8
LazyData: true LazyData: true
LazyDataCompression: xz LazyDataCompression: xz
Roxygen: list(markdown = TRUE) Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.2 RoxygenNote: 7.2.0
Depends: Depends:
R (>= 2.10) R (>= 2.10)
Imports: Imports:

View file

@ -74,7 +74,7 @@ plot_positions <- function(species_ids, gene_sets) {
x = ~species, x = ~species,
y = ~distance, y = ~distance,
name = gene_set_name, name = gene_set_name,
text = ~glue::glue( text = ~ glue::glue(
"<b>{name}</b><br>", "<b>{name}</b><br>",
"{round(distance / 1000000, digits = 2)} MBp" "{round(distance / 1000000, digits = 2)} MBp"
), ),
@ -155,7 +155,7 @@ plot_rankings <- function(rankings, gene_sets) {
x = ranking_name, x = ranking_name,
y = ~score, y = ~score,
name = gene_set_name, name = gene_set_name,
text = ~glue::glue( text = ~ glue::glue(
"<b>{name}</b><br>", "<b>{name}</b><br>",
"Score: {round(score, digits = 2)}<br>", "Score: {round(score, digits = 2)}<br>",
"Rank: {rank}<br>", "Rank: {rank}<br>",
@ -238,7 +238,7 @@ plot_scores <- function(ranking, gene_sets = NULL, max_rank = NULL) {
x = ~percentile, x = ~percentile,
y = ~score, y = ~score,
name = gene_set_name, name = gene_set_name,
text = ~glue::glue( text = ~ glue::glue(
"<b>{name}</b><br>", "<b>{name}</b><br>",
"Score: {round(score, digits = 2)}<br>", "Score: {round(score, digits = 2)}<br>",
"Rank: {rank}<br>", "Rank: {rank}<br>",
@ -385,7 +385,9 @@ plot_chromosomes <- function(ranking) {
#' Plot scores in relation to chromosomal position of genes. #' Plot scores in relation to chromosomal position of genes.
#' #'
#' @param ranking The ranking to visualize. #' @param ranking The ranking to visualize.
#' @param chromosome_name The chromosome to visualize. #' @param chromosome_name The chromosome to visualize. If this is `NULL` all,
#' chromosomes will be included and the x-axis will show distances instead of
#' positions.
#' @param gene_sets Named list of vectors of genes to highlight. The list names #' @param gene_sets Named list of vectors of genes to highlight. The list names
#' will be used as labels. #' will be used as labels.
#' #'
@ -394,22 +396,23 @@ plot_chromosomes <- function(ranking) {
#' #'
#' @export #' @export
plot_scores_by_position <- function(ranking, plot_scores_by_position <- function(ranking,
chromosome_name, chromosome_name = NULL,
gene_sets = NULL) { gene_sets = NULL) {
if (!requireNamespace("plotly", quietly = TRUE)) { if (!requireNamespace("plotly", quietly = TRUE)) {
stop("Please install \"plotly\" to use this function.") stop("Please install \"plotly\" to use this function.")
} }
chromosome_name_ <- chromosome_name distance_data <- if (!is.null(chromosome_name)) {
chromosome_name_ <- chromosome_name
data <- merge(
ranking,
geposan::distances[ geposan::distances[
species == "hsapiens" & species == "hsapiens" &
chromosome_name == chromosome_name_ chromosome_name == chromosome_name_
], ]
by = "gene" } else {
) geposan::distances[species == "hsapiens"]
}
data <- merge(ranking, distance_data, by = "gene")
data <- merge( data <- merge(
data, data,
@ -434,16 +437,23 @@ plot_scores_by_position <- function(ranking,
index <- index + 1 index <- index + 1
} }
# Use distances instead of positions in case all chromosomes are included.
if (is.null(chromosome_name)) {
data[, x := distance]
} else {
data[, x := start_position]
}
plotly::plot_ly() |> plotly::plot_ly() |>
plotly::add_markers( plotly::add_markers(
data = data, data = data,
x = ~start_position, x = ~x,
y = ~score, y = ~score,
name = ~gene_set, name = ~gene_set,
text = ~glue::glue( text = ~ glue::glue(
"<b>{name}</b><br>", "<b>{name}</b><br>",
"Position: ", if (is.null(chromosome_name)) "Distance: " else "Position: ",
"{round(start_position / 1000000, digits = 2)} MBp<br>", "{round(x / 1000000, digits = 2)} MBp<br>",
"Score: {round(score, digits = 2)}<br>", "Score: {round(score, digits = 2)}<br>",
"Rank: {rank}<br>", "Rank: {rank}<br>",
"Percentile: {round(percentile * 100, digits = 2)}%" "Percentile: {round(percentile * 100, digits = 2)}%"
@ -451,7 +461,11 @@ plot_scores_by_position <- function(ranking,
hoverinfo = "text", hoverinfo = "text",
) |> ) |>
plotly::layout( plotly::layout(
xaxis = list(title = "Position (Bp)"), xaxis = list(title = if (is.null(chromosome_name)) {
"Distance (Bp)"
} else {
"Position (Bp)"
}),
yaxis = list(title = "Score") yaxis = list(title = "Score")
) )
} }

View file

@ -4,12 +4,14 @@
\alias{plot_scores_by_position} \alias{plot_scores_by_position}
\title{Plot scores in relation to chromosomal position of genes.} \title{Plot scores in relation to chromosomal position of genes.}
\usage{ \usage{
plot_scores_by_position(ranking, chromosome_name, gene_sets = NULL) plot_scores_by_position(ranking, chromosome_name = NULL, gene_sets = NULL)
} }
\arguments{ \arguments{
\item{ranking}{The ranking to visualize.} \item{ranking}{The ranking to visualize.}
\item{chromosome_name}{The chromosome to visualize.} \item{chromosome_name}{The chromosome to visualize. If this is \code{NULL} all,
chromosomes will be included and the x-axis will show distances instead of
positions.}
\item{gene_sets}{Named list of vectors of genes to highlight. The list names \item{gene_sets}{Named list of vectors of genes to highlight. The list names
will be used as labels.} will be used as labels.}