-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add plot_granges
#6
Changes from 1 commit
ffbeac3
f3dcd5a
b4c4136
1886cb4
b6a7358
224e5f2
2aba65e
7fa6954
123c788
4bac60a
75428d2
ea6f464
1790d52
236aa27
9e9170a
424fe6b
4eefe87
70ca8d6
a4a6785
94837cf
7f44587
64e4de0
6dfd4b2
bbc4def
b9b7388
af48390
96fa7f5
e6137b2
b4a28bb
c265cf2
00c57a7
0c66dc9
963a9ff
008b5d6
e32322f
48a0558
916e254
a24edaa
73f173d
57cbc97
c9e9088
c65a351
b0b5a45
aa46568
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,11 @@ | |
#' "GRanges Plot". | ||
#' @param subtitle Character string. The subtitle of the plot. Default | ||
#' is "Stacked nucleotide example". | ||
#' @param plot_type Character string. Select the type of gosling plot. | ||
#' Default is "bar" | ||
#' - "bars": Stacked bar plot with height based on pathogenicity score | ||
#' - "lolipop": variation of a bar chart where the bar is replaced with a | ||
#' line and a dot at the end to show mutation variations. | ||
#' | ||
#' @return A Shiny app object that, when run, displays the Gosling | ||
#' plot. | ||
|
@@ -45,37 +50,36 @@ | |
plot_granges <- | ||
function(gr, | ||
title = "GRanges Plot", | ||
subtitle = "Stacked nucleotide example") | ||
subtitle = "Stacked nucleotide example", | ||
plot_type = "bars") | ||
{ | ||
## Validate input | ||
stopifnot( | ||
"Input must be a GRanges or GPos object" = | ||
inherits(gr, c("GRanges", "GPos")) | ||
) | ||
stopifnot( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Generally, combine assertions into a single stopifnot(
is(granges, "GRanges"),
isScalarCharacter(title),
isScalarCharacter(subtitle),
isScalarCharacter(plot_type)
) Actually I think the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
"Option must be a " = | ||
"Title must be a scalar string" = | ||
is.character(title) && length (title) == 1 && !is.na(title) && | ||
nzchar(title) | ||
) | ||
stopifnot( | ||
"Option must be a " = | ||
"Subtitle must be a scalar string" = | ||
is.character(subtitle) && length (subtitle) == 1 && !is.na(subtitle) | ||
&& nzchar(subtitle) | ||
) | ||
|
||
|
||
|
||
stopifnot( | ||
"Type must be a scalar string" = | ||
is.character(plot_type) && length (plot_type) == 1 && !is.na(plot_type) | ||
&& nzchar(plot_type) | ||
) | ||
|
||
## Define categories and color mapping | ||
categories <- c("likely_benign", "ambiguous", "likely_pathogenic") | ||
colormapping <- c("#89d5f5", "gray", "#f56c6c") | ||
|
||
## Get range from GRanges object | ||
r <- range(gr) | ||
|
||
## This fixes the bug if .gosling directory does not already exist | ||
gosling_cache <- file.path(R_user_dir("AlphaMissenseR", which = "cache"), ".gosling") | ||
if (!dir.exists(cache_dir)) | ||
|
||
## Prepare track data | ||
track_data <- track_data_gr( | ||
|
@@ -84,11 +88,61 @@ plot_granges <- | |
genomicFields = c("start", "end") | ||
) | ||
|
||
## Define tracks | ||
## This fixes the bug if .gosling directory does not already exist | ||
cache_dir <- file.path(tools::R_user_dir("AlphaMissenseR", which = "cache"), ".gosling") | ||
if (!dir.exists(cache_dir)) | ||
## TODO: check return value to ensure directory is created successfully | ||
dir.create(cache_dir, recursive = TRUE) | ||
|
||
## trigger the option for bars or lolipop | ||
if (plot_type =="bars"){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As best practice use 'identical(plot_type, "bars") There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
#define single track | ||
track_bar <- add_single_track( | ||
width = 800, | ||
height = 180, | ||
data = track_data, | ||
mark = "bar", | ||
x = visual_channel_x( | ||
field = "start", type = "genomic", axis = "bottom" | ||
), | ||
xe = visual_channel_x(field = "end", type = "genomic"), | ||
y = visual_channel_y( | ||
field = "am_pathogenicity", type = "quantitative", axis = "right" | ||
), | ||
color = visual_channel_color( | ||
field = "am_pathogenicity", | ||
type = "quantitative" | ||
), | ||
tooltip = visual_channel_tooltips( | ||
visual_channel_tooltip(field = "REF", type = "nominal", | ||
alt = "Reference"), | ||
visual_channel_tooltip(field = "ALT", type = "nominal", | ||
alt = "Alternative / Mutation"), | ||
visual_channel_tooltip( | ||
field = "am_pathogenicity", | ||
type = "quantitative", | ||
alt = "AM_Pathogenicity Score", | ||
format = "0.2" | ||
) ), | ||
size = list(value = 5) | ||
) | ||
|
||
composed_view <- compose_view( | ||
layout = "linear", | ||
xDomain = list(chromosome = as.character(seqnames(r)), | ||
interval = c(start(r), end(r))), | ||
tracks = track_bar | ||
|
||
) | ||
|
||
## other track | ||
} else if (plot_type == "lolipop"){ | ||
|
||
## Define multi tracks | ||
track_ref <- add_single_track( | ||
data = track_data, | ||
mark = "rect", | ||
x = visual_channel_x(field = "start", type = "genomic", axis = "bottom"), | ||
x = visual_channel_x(field = "start", type = "genomic", axis = "top"), | ||
xe = visual_channel_x(field = "end", type = "genomic"), | ||
size = list(value = 50), | ||
stroke = "lightgrey", | ||
|
@@ -99,9 +153,10 @@ plot_granges <- | |
track_alt <- add_single_track( | ||
data = track_data, | ||
mark = "point", | ||
x = visual_channel_x(field = "start", type = "genomic", axis = "bottom"), | ||
x = visual_channel_x(field = "start", type = "genomic", axis = "top"), | ||
xe = visual_channel_x(field = "end", type = "genomic"), | ||
y = visual_channel_y(field = "am_class", type = "nominal", axis = "right"), | ||
y = visual_channel_y(field = "am_class", type="nominal", | ||
domain= categories, axis = "left",baseline = "ambiguous" ), | ||
text = list(field = "ALT", type = "nominal"), | ||
size = list(value = 5), | ||
tooltip = visual_channel_tooltips( | ||
|
@@ -118,7 +173,7 @@ plot_granges <- | |
) | ||
|
||
## Compose view | ||
composed_view_a <- compose_view( | ||
composed_view <- compose_view( | ||
width = 800, | ||
height = 180, | ||
multi = TRUE, | ||
|
@@ -132,17 +187,23 @@ plot_granges <- | |
field = "am_class", | ||
type = "nominal", | ||
domain = categories, | ||
baseline = "ambiguous", | ||
range = colormapping, | ||
legend = TRUE | ||
), | ||
tracks = add_multi_tracks(track_ref, track_alt) | ||
) | ||
|
||
## Arrange view | ||
|
||
} | ||
## trigger check | ||
else { | ||
stop("Invalid plot_type. Use 'bars' or 'lolipop'") | ||
} | ||
## Arrange into view | ||
arranged_view3 <- arrange_views( | ||
title = title, | ||
subtitle = subtitle, | ||
views = composed_view_a | ||
views = composed_view | ||
) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I'm following correctly, I think it would be useful to break this function into two INTERNAL, the first constructing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
## Create Shiny app | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here the indentation when function arguments span more than one line is (also avoid 'cryptic' names where possible)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
73f173d