Skip to content

Commit

Permalink
Merge pull request #92 from morinlab/kdreval-dev
Browse files Browse the repository at this point in the history
init prettyCoOncoplot
  • Loading branch information
Kdreval authored May 20, 2022
2 parents 85fff8d + 9e0e383 commit 93e88d7
Show file tree
Hide file tree
Showing 6 changed files with 207 additions and 5 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ Imports:
SRAdb,
stats,
tidyverse,
workflowr
workflowr,
ggpubr
Remotes: morinlab/g3viz
VignetteBuilder: knitr
Depends:
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export(plot_sample_circos)
export(populate_each_tool_result)
export(populate_tool_results)
export(prettyChromoplot)
export(prettyCoOncoplot)
export(prettyForestPlot)
export(prettyOncoplot)
export(prettyRainfallPlot)
Expand Down Expand Up @@ -104,10 +105,12 @@ import(dbplyr)
import(dplyr)
import(g3viz)
import(ggplot2)
import(ggpubr)
import(ggrepel)
import(ggsci)
import(ggthemes)
import(grid)
import(maftools)
import(metaviz)
import(reshape2)
import(rtracklayer)
Expand Down
4 changes: 3 additions & 1 deletion R/database.R
Original file line number Diff line number Diff line change
Expand Up @@ -1093,8 +1093,10 @@ get_cn_states = function(regions_list,
cn_matrix = pivot_wider(all_cn, id_cols = "sample_id", names_from = "region_name", values_from = "CN") %>%
column_to_rownames("sample_id")

names(cn_matrix) = region_names

#order the regions the same way the user provided them for convenience
return(cn_matrix[,region_names])
return(cn_matrix)
}


Expand Down
6 changes: 3 additions & 3 deletions R/utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -1725,11 +1725,11 @@ get_gambl_colours = function(classification = "all",
"NEG" = "#E5A4CB")

all_colours[["BL"]] = c("M53-BL" = "#A6CEE3",
"DLBCL-1" = "#721F0F",
"DLBCL-A" = "#721F0F",
"IC-BL" = "#45425A",
"DGG-BL" = "#E90C8BFF",
"DLBCL-2" = "#FB9A99",
"DLBCL-3" = "#C41230")
"DLBCL-B" = "#FB9A99",
"DLBCL-C" = "#C41230")

all_colours[["FL"]] = c(dFL = "#99C1B9", cFL = "#D16666", DLBCL = "#479450")

Expand Down
131 changes: 131 additions & 0 deletions R/viz.R
Original file line number Diff line number Diff line change
Expand Up @@ -1624,6 +1624,137 @@ prettyOncoplot = function(maftools_obj,
}


#' Display 2 prettyOncoplots side-by-side.
#'
#' `prettyCoOncoplot` returns ggplot-compatible figure of 2 prettyOncoplots side-by-side.
#'
#' This function will generate a graphic displaying 2 oncoplots side-by-side. Optionally user can
#' annotate each oncoplot with it's own title that will be displayed at the top. All the arguments
#' recognized by prettyOncoplot are supported and can be specified when calling this function.
#' For both oncoplots the same specified parameters will be applied (e.g. genes to display, split columns,
#' font size, top annotation etc). If the provided argument is not recognized by prettyOncoplot,
#' it will be discarded. If you want a specific order of oncoplots on the left and right, please
#' ensure the argument `comparison_column` is a factor with first level being the group
#' you want to be plotted on the left side. For developers: new arguments added to prettyOncoplot in the future
#' are expected to be out-of-the-box compatible with this function nd would not need code modifications.
#'
#' @param maf Required argument. A maftools object containing the mutations you want to plot on both oncoplots.
#' @param metadata Required argument. A data.frame with metadata for both oncoplots.
#' @param comparison_values Optional: If the comparison column contains more than two values or is not a factor, specify a character vector of length two in the order you would like the factor levels to be set, reference group first.
#' @param comparison_column Required: the name of the metadata column containing the comparison values.
#' @param label1 Optional argument. Label to be shown as a title for the oncoplot #1.
#' @param label2 Optional argument. Label to be shown as a title for the oncoplot #2.
#'
#' @return A ggplot object with 2 oncoplots side-by-side.
#' @export
#' @import ComplexHeatmap ggpubr maftools
#'
#' @examples
#' ssm=get_coding_ssm(limit_cohort = c("BL_Adult", "BL_Pediatric"))
#' ssm=maftools::read.maf(ssm)
#' meta=get_gambl_metadata() %>% dplyr::filter(cohort %in% c("BL_Adult", "BL_Pediatric"))
#' prettyCoOncoplot(maf=ssm,
#' metadata = meta,
#' comparison_column = "cohort",
#' include_noncoding = NULL,
#' minMutationPercent = 0,
#' genes=c("MYC", "TET2", "TP53", "DDX3X", "ID3"),
#' metadataColumns=c("pathology", "EBV_status_inf", "pairing_status", "cohort"),
#' splitColumnName="EBV_status_inf",
#' metadataBarHeight=10,
#' fontSizeGene=12,
#' metadataBarFontsize=10,
#' label1="Adult",
#' label2="Pediatric")
#'
prettyCoOncoplot <- function(maf,
metadata,
comparison_column,
comparison_values,
label1,
label2,
...) {
# check for required arguments
required = c("maf", "metadata", "comparison_column")

defined = names(as.list(match.call())[-1])

if (any(!required %in% defined)) {
stop("Please provide mutation data and metadata for 2 pretty Oncoplots with specified comparison_column.")
}

#If no comparison_values are specified, derive the comparison_values from the specified comparison_column
if(missing(comparison_values)){
if(class(metadata[[comparison_column]]) == "factor"){
comparison_values = levels(metadata[[comparison_column]])
} else {
comparison_values = unique(metadata[[comparison_column]])
}
}

#Ensure there are only two comparison_values
{
if(length(comparison_values) != 2)
stop(paste0("Your comparison must have two values. \nEither specify comparison_values as a vector of length 2 or subset your metadata so your comparison_column has only two unique values or factor levels."))
}

#Subset the metadata to the specified comparison_values and the maf to the remaining sample_ids
meta1 = metadata[metadata[[comparison_column]] %in% comparison_values[1], ]
meta2 = metadata[metadata[[comparison_column]] %in% comparison_values[2], ]

# Subset maf to only samples in the comparison values
ssm1 = maftools::subsetMaf(maf,tsb=pull(meta1, Tumor_Sample_Barcode))
ssm2 = maftools::subsetMaf(maf,tsb=pull(meta2, Tumor_Sample_Barcode))

# Arguments to pass into prettyOncoplot
oncoplot_args = list(...)
# Discard any arguments not supported by prettyOncoplot
oncoplot_args = oncoplot_args[names(oncoplot_args) %in% intersect(names(oncoplot_args),
formalArgs(prettyOncoplot))]
# Build oncoplot No1
op1 = do.call(prettyOncoplot, c(
list(
maftools_obj = ssm1,
these_samples_metadata = meta1
),
oncoplot_args
))
# convert it to ggplot object
op1 = grid.grabExpr(draw(op1), width = 10, height = 17)
# if user provided annotation label, place it as a name for oncoplot No1
if (!missing(label1)) {
op1 = annotate_figure(op1,
top = text_grob(label1,
face = "bold",
size = 20))
}
# Build oncoplot No2
op2 = do.call(prettyOncoplot, c(
list(
maftools_obj = ssm2,
these_samples_metadata = meta2
),
oncoplot_args
))
# convert it to ggplot object
op2 = grid.grabExpr(draw(op2), width = 10, height = 17)
# if user provided annotation label, place it as a name for oncoplot No2
if (!missing(label2)) {
op2 = annotate_figure(op2,
top = text_grob(label2,
face = "bold",
size = 20))
}
# arrange 2 oncoplots together side by side
p = ggarrange(op1,
op2,
ncol = 2,
nrow = 1)

return(p)
}


#' Generate a colourful multi-panel overview of hypermutation in regions of interest across many samples.
#'
#' @param regions_bed Bed file with chromosome coordinates, should contain columns chr, start, end, name (with these exact names).
Expand Down
65 changes: 65 additions & 0 deletions man/prettyCoOncoplot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 93e88d7

Please # to comment.