Skip to content

Commit

Permalink
Auto-style code with dev/04_update.R
Browse files Browse the repository at this point in the history
  • Loading branch information
lcolladotor committed Aug 6, 2024
1 parent 2a2532c commit db1d236
Show file tree
Hide file tree
Showing 14 changed files with 142 additions and 139 deletions.
9 changes: 5 additions & 4 deletions R/create_cell_colors.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@
#' @importFrom graphics barplot par
#' @importFrom grDevices hcl
#' @importFrom utils head
create_cell_colors <- function(cell_types = c("Astro", "Micro", "Oligo", "OPC", "Inhib", "Excit"),
pallet = c("classic", "gg", "tableau"),
split = NA,
preview = FALSE) {
create_cell_colors <- function(
cell_types = c("Astro", "Micro", "Oligo", "OPC", "Inhib", "Excit"),
pallet = c("classic", "gg", "tableau"),
split = NA,
preview = FALSE) {
pallet <- match.arg(pallet)

base_cell_types <- unique(ss(cell_types, pattern = split))
Expand Down
19 changes: 10 additions & 9 deletions R/fetch_deconvo_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
#' Based on spatialLIBD::fetch_data()
#'
#' @param type A `character(1)` specifying which file you want to download.
#' * `rse_gene`: A [RangedSummarizedExperiment-class][SummarizedExperiment::RangedSummarizedExperiment-class]
#' * `rse_gene`: A [RangedSummarizedExperiment-class][SummarizedExperiment::RangedSummarizedExperiment-class]
#' with 110 bulk RNA-seq samples x 21k genes. (41 MB)
#' * `sce`: A [SingleCellExperiment][SingleCellExperiment::SingleCellExperiment-class]
#' * `sce`: A [SingleCellExperiment][SingleCellExperiment::SingleCellExperiment-class]
#' object with Human DLPFC snRNA-seq data. 77k nuclei x 36k genes (172 MB)
#' * `sce_DLPFC_example`: An example subset of `sec`
#' [SingleCellExperiment][SingleCellExperiment::SingleCellExperiment-class]
#' * `sce_DLPFC_example`: An example subset of `sec`
#' [SingleCellExperiment][SingleCellExperiment::SingleCellExperiment-class]
#' with 10k nuclei x 557 genes (49 MB)
#'
#'
#' @param destdir The destination directory to where files will be downloaded
#' to in case the `ExperimentHub` resource is not available. If you already
#' downloaded the files, you can set this to the current path where the files
Expand Down Expand Up @@ -83,10 +83,11 @@
#' file.path(tempdir(), "sce_DLPFC_annotated")
#' )
#' }
fetch_deconvo_data <- function(type = c("rse_gene", "sce", "sce_DLPFC_example"),
destdir = tempdir(),
eh = ExperimentHub::ExperimentHub(),
bfc = BiocFileCache::BiocFileCache()) {
fetch_deconvo_data <- function(
type = c("rse_gene", "sce", "sce_DLPFC_example"),
destdir = tempdir(),
eh = ExperimentHub::ExperimentHub(),
bfc = BiocFileCache::BiocFileCache()) {
rse_gene <- sce_DLPFC_example <- NULL

## Choose a type among the valid options
Expand Down
27 changes: 14 additions & 13 deletions R/get_mean_ratio.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#'
#' Improved argument names and documentaion, but same functionalty from `get_mean_ratio2()`.
#'
#' @param sce [SummarizedExperiment-class][SummarizedExperiment::SummarizedExperiment-class]
#' @param sce [SummarizedExperiment-class][SummarizedExperiment::SummarizedExperiment-class]
#' (or any derivative class) object containing single cell/nucleus gene expression data
#' @param cellType_col A `character(1)` name of the column in the
#' [colData()][SummarizedExperiment::SummarizedExperiment-class] of `sce` that
Expand Down Expand Up @@ -39,19 +39,19 @@
#' ## Explore properties of the sce object
#' sce_DLPFC_example
#'
#' ## this data contains logcounts of gene expression
#' ## this data contains logcounts of gene expression
#' SummarizedExperiment::assays(sce_DLPFC_example)$logcounts[1:5, 1:5]
#'
#'
#' ## nuclei are classified in to cell types
#' table(sce_DLPFC_example$cellType_broad_hc)
#'
#'
#' ## Get the mean ratio for each gene for each cell type defined in `cellType_broad_hc`
#' get_mean_ratio(sce_DLPFC_example, cellType_col = "cellType_broad_hc")
#'
#' # Option to specify gene_name as the "Symbol" column from rowData
#' # this will be added to the marker stats output
#' SummarizedExperiment::rowData(sce_DLPFC_example)
#'
#'
#' ## specify rowData col names for gene_name and gene_ensembl
#' get_mean_ratio(sce_DLPFC_example, cellType_col = "cellType_broad_hc", gene_name = "gene_name", gene_ensembl = "gene_id")
#'
Expand All @@ -60,11 +60,12 @@
#' @importFrom purrr map
#' @importFrom purrr map2
#' @importFrom matrixStats rowMedians
get_mean_ratio <- function(sce,
cellType_col,
assay_name = "logcounts",
gene_ensembl = NULL,
gene_name = NULL) {
get_mean_ratio <- function(
sce,
cellType_col,
assay_name = "logcounts",
gene_ensembl = NULL,
gene_name = NULL) {
# RCMD fix
cellType.target <- NULL
cellType <- NULL
Expand All @@ -78,10 +79,10 @@ get_mean_ratio <- function(sce,

cell_types <- unique(sce[[cellType_col]])
names(cell_types) <- cell_types

ct_table <- table(sce[[cellType_col]])
if(any(ct_table < 10)) warning("One or more cell types has < 10 cells, this may result in unstable marker genes results")

if (any(ct_table < 10)) warning("One or more cell types has < 10 cells, this may result in unstable marker genes results")

sce_assay <- as.matrix(SummarizedExperiment::assays(sce)[[assay_name]])

Expand Down
26 changes: 14 additions & 12 deletions R/plot_composition_bar.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@
#'
#' @importFrom dplyr rename group_by summarise mutate arrange
#' @importFrom ggplot2 ggplot geom_bar geom_text aes theme element_text
plot_composition_bar <- function(prop_long,
sample_col = "RNum",
x_col = "ALL",
prop_col = "prop",
ct_col = "cell_type",
add_text = TRUE,
min_prop_text = 0) {
plot_composition_bar <- function(
prop_long,
sample_col = "RNum",
x_col = "ALL",
prop_col = "prop",
ct_col = "cell_type",
add_text = TRUE,
min_prop_text = 0) {
x_cat <- cell_type <- anno_y <- NULL

# ct_col <- dplyr::enquo(ct_col)
Expand Down Expand Up @@ -68,11 +69,12 @@ plot_composition_bar <- function(prop_long,
}


.get_cat_prop <- function(prop_long,
sample_col = "RNum",
x_col = "ALL",
prop_col = "prop",
ct_col = "cell_type") {
.get_cat_prop <- function(
prop_long,
sample_col = "RNum",
x_col = "ALL",
prop_col = "prop",
ct_col = "cell_type") {
cell_type <- prop <- mean_prop <- x_cat <- anno_y <- sum_prop <- n <- NULL

prop_long <- prop_long |>
Expand Down
17 changes: 8 additions & 9 deletions R/plot_gene_express.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,14 @@
#'
#' @family expression plotting functions
#'
plot_gene_express <- function(
sce,
genes,
assay_name = "logcounts",
category = "cellType",
color_pal = NULL,
title = NULL,
plot_points = FALSE,
ncol = 2) {
plot_gene_express <- function(sce,
genes,
assay_name = "logcounts",
category = "cellType",
color_pal = NULL,
title = NULL,
plot_points = FALSE,
ncol = 2) {
stopifnot(any(genes %in% rownames(sce)))

if (!category %in% colnames(colData(sce))) {
Expand Down
23 changes: 12 additions & 11 deletions R/plot_marker_express.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,18 @@
#' )
#' @family expression plotting functions
#' @importFrom ggplot2 ggplot geom_violin geom_text facet_wrap stat_summary
plot_marker_express <- function(sce,
stats,
cell_type,
n_genes = 4,
rank_col = "MeanRatio.rank",
anno_col = "MeanRatio.anno",
gene_col = "gene",
cellType_col = "cellType",
color_pal = NULL,
plot_points = FALSE,
ncol = 2) {
plot_marker_express <- function(
sce,
stats,
cell_type,
n_genes = 4,
rank_col = "MeanRatio.rank",
anno_col = "MeanRatio.anno",
gene_col = "gene",
cellType_col = "cellType",
color_pal = NULL,
plot_points = FALSE,
ncol = 2) {
stopifnot(cellType_col %in% colnames(colData(sce)))
stopifnot(cell_type %in% sce[[cellType_col]])
stopifnot(cell_type %in% stats$cellType.target)
Expand Down
68 changes: 34 additions & 34 deletions R/plot_marker_express_ALL.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#' @examples
#' #' ## Fetch sce example data
#' if (!exists("sce_DLPFC_example")) sce_DLPFC_example <- fetch_deconvo_data("sce_DLPFC_example")
#'
#'
#' # Plot marker gene expression to PDF, one page per cell type in stats
#' pdf_file <- tempfile("test_marker_expression_ALL", fileext = ".pdf")
#'
Expand All @@ -42,16 +42,17 @@
#' @importFrom ggplot2 ggplot geom_violin geom_text facet_wrap stat_summary
#' @importFrom SummarizedExperiment colData
#' @importFrom purrr map
plot_marker_express_ALL <- function(sce,
stats,
pdf_fn = "marker_expression.pdf",
n_genes = 10,
rank_col = "MeanRatio.rank",
anno_col = "MeanRatio.anno",
gene_col = "gene",
cellType_col = "cellType",
color_pal = NULL,
plot_points = FALSE) {
plot_marker_express_ALL <- function(
sce,
stats,
pdf_fn = "marker_expression.pdf",
n_genes = 10,
rank_col = "MeanRatio.rank",
anno_col = "MeanRatio.anno",
gene_col = "gene",
cellType_col = "cellType",
color_pal = NULL,
plot_points = FALSE) {
stopifnot(cellType_col %in% colnames(colData(sce)))

if (is.factor(sce[[cellType_col]])) {
Expand All @@ -64,29 +65,28 @@ plot_marker_express_ALL <- function(sce,
# missing <- cell_types[!cell_types %in% stats$cellType.target]
stop("Stats is missing cell types, check you're using the correct marker stats data and cellType_col")
}

marker_plots <- purrr::map(
cell_types,
~ plot_marker_express(
sce = sce,
stats = stats,
cell_type = .x,
n_genes = n_genes,
rank_col = rank_col,
anno_col = anno_col,
gene_col = gene_col,
cellType_col = cellType_col,
color_pal = color_pal,
plot_points = plot_points

marker_plots <- purrr::map(
cell_types,
~ plot_marker_express(
sce = sce,
stats = stats,
cell_type = .x,
n_genes = n_genes,
rank_col = rank_col,
anno_col = anno_col,
gene_col = gene_col,
cellType_col = cellType_col,
color_pal = color_pal,
plot_points = plot_points
)
)
)

if(is.null(pdf_fn)){
return(marker_plots)
} else {
grDevices::pdf(pdf_fn)
print(marker_plots)
grDevices::dev.off()
}

if (is.null(pdf_fn)) {
return(marker_plots)
} else {
grDevices::pdf(pdf_fn)
print(marker_plots)
grDevices::dev.off()
}
}
59 changes: 29 additions & 30 deletions R/plot_marker_express_List.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@
#'
#' ## Create list-of-lists of genes to plot, names of sub-list become title of page
#' my_gene_list <- list(Inhib = c("GAD2", "SAMD5"), Astro = c("RGS20", "PRDM16"))
#'
#' # Return a list of plots
#'
#' # Return a list of plots
#' plots <- plot_marker_express_List(
#' sce_DLPFC_example,
#' gene_list = my_gene_list,
#' cellType_col = "cellType_broad_hc")
#'
#' cellType_col = "cellType_broad_hc"
#' )
#'
#' print(plots[[1]])
#'
#'
#' # Plot marker gene expression to PDF, one page per cell type in stats
#' pdf_file <- tempfile("test_marker_expression_List", fileext = ".pdf")
#'
Expand All @@ -44,18 +45,17 @@
#' )
#'
#' if (interactive()) browseURL(pdf_file)
#'
#'
#' @family expression plotting functions
#' @importFrom ggplot2 ggplot geom_violin geom_text facet_wrap stat_summary
#' @importFrom SummarizedExperiment colData
plot_marker_express_List <- function(
sce,
gene_list,
pdf_fn = NULL,
cellType_col = "cellType",
gene_name_col = "gene_name",
color_pal = NULL,
plot_points = FALSE) {
plot_marker_express_List <- function(sce,
gene_list,
pdf_fn = NULL,
cellType_col = "cellType",
gene_name_col = "gene_name",
color_pal = NULL,
plot_points = FALSE) {
stopifnot(cellType_col %in% colnames(colData(sce)))

if (!identical(rownames(sce), SummarizedExperiment::rowData(sce)[[gene_name_col]])) {
Expand All @@ -64,23 +64,22 @@ plot_marker_express_List <- function(
}

marker_plots <- purrr::map2(
gene_list, names(gene_list),
~ plot_gene_express(
sce = sce,
genes = .x,
cat = cellType_col,
color_pal = color_pal,
plot_points = plot_points,
title = .y
)
gene_list, names(gene_list),
~ plot_gene_express(
sce = sce,
genes = .x,
cat = cellType_col,
color_pal = color_pal,
plot_points = plot_points,
title = .y
)
)
if(is.null(pdf_fn)){
return(marker_plots)

if (is.null(pdf_fn)) {
return(marker_plots)
} else {
grDevices::pdf(pdf_fn)
print(marker_plots)
grDevices::dev.off()
grDevices::pdf(pdf_fn)
print(marker_plots)
grDevices::dev.off()
}

}
Loading

0 comments on commit db1d236

Please sign in to comment.