Skip to content

Commit

Permalink
Style code
Browse files Browse the repository at this point in the history
  • Loading branch information
lahuuki committed Aug 13, 2024
1 parent 8bbc50e commit 460ef87
Show file tree
Hide file tree
Showing 16 changed files with 169 additions and 175 deletions.
9 changes: 4 additions & 5 deletions R/create_cell_colors.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@
#' @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
11 changes: 5 additions & 6 deletions R/fetch_deconvo_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
#'
#' ## explore bulk data
#' rse_gene
#'
#'
#' ## load example snRNA-seq data
#' ## A SingleCellExperiment (4.79 MB)
#' if (!exists("sce_DLPFC_example")) sce_DLPFC_example <- fetch_deconvo_data("sce_DLPFC_example")
Expand All @@ -64,11 +64,10 @@
#' 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
32 changes: 16 additions & 16 deletions R/findMarkers_1vAll.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
#' @param assay_name Name of the assay to use for calculation
#' @param cellType_col Column name on colData of the sce that denotes the celltype
#' @param add_symbol Add the gene symbol column to the marker stats table
#' @param mod String specifying the model used as design in findMarkers.
#' Can be `NULL` (default) if there are no blocking terms with uninteresting
#' @param mod String specifying the model used as design in findMarkers.
#' Can be `NULL` (default) if there are no blocking terms with uninteresting
#' factors as documented at [pairwiseTTests][scran::pairwiseTTests].
#' @param verbose Boolean choosing to print progress messages or not
#' @param direction Choice of direction tested as markers, "up" (default),
#' @param direction Choice of direction tested as markers, "up" (default),
#' "any", or "down". Impacts p-values, if "up" genes with logFC < 0 will have
#' p.value=1.
#'
Expand Down Expand Up @@ -39,26 +39,26 @@
#'
#' ## Get the 1vALL stats for each gene for each cell type defined in `cellType_broad_hc`
#' marker_stats_1vAll <- findMarkers_1vAll(
#' sce = sce_DLPFC_example,
#' assay_name = "logcounts",
#' cellType_col = "cellType_broad_hc",
#' mod = "~BrNum"
#' sce = sce_DLPFC_example,
#' assay_name = "logcounts",
#' cellType_col = "cellType_broad_hc",
#' mod = "~BrNum"
#' )
#'
#'
#' ## explore output, top markers have high logFC
#' head(marker_stats_1vAll)
#'
#' @importFrom purrr map
#' @importFrom dplyr mutate
#' @importFrom scran findMarkers
#' @importFrom tibble rownames_to_column as_tibble add_column
findMarkers_1vAll <- function(sce,
assay_name = "counts",
cellType_col = "cellType",
add_symbol = FALSE,
mod = NULL,
verbose = TRUE,
direction = "up") {
findMarkers_1vAll <- function(sce,
assay_name = "counts",
cellType_col = "cellType",
add_symbol = FALSE,
mod = NULL,
verbose = TRUE,
direction = "up") {
# RCMD Fix
gene <- rank_marker <- cellType.target <- std.logFC <- rowData <- Symbol <- NULL

Expand All @@ -68,7 +68,7 @@ findMarkers_1vAll <- function(sce,
## Traditional t-test with design as in PB'd/limma approach
pd <- as.data.frame(SummarizedExperiment::colData(sce))
# message("donor" %in% colnames(pd))

if (verbose) message("Running 1vALL Testing for ", direction, "-regulated genes")

if (!is.null(mod)) {
Expand Down
26 changes: 13 additions & 13 deletions R/get_mean_ratio.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
#' Calculate the mean ratio value and rank for each gene for each cell type in the `sce`
#' object, to identify effective marker genes for deconvolution.
#'
#' Note if a cell type has < 10 cells the MeanRatio results may be unstable.
#' Note if a cell type has < 10 cells the MeanRatio results may be unstable.
#' See rational in OSCA: <https://bioconductor.org/books/3.19/OSCA.multisample/multi-sample-comparisons.html#performing-the-de-analysis>
#'
#' @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
#' denotes the cell type or group of interest.
#' denotes the cell type or group of interest.
#' @param assay_name A `character(1)` specifying the name of the
#' [assay()][SummarizedExperiment::SummarizedExperiment-class] in the
#' `sce` object to use to rank expression values. Defaults to `logcounts` since
Expand All @@ -28,7 +28,7 @@
#' * `mean.2nd` is the mean expression of `gene` for `cellType.2nd`.
#' * `MeanRatio` is the ratio of `mean.target/mean.2nd`.
#' * `MeanRatio.rank` is the rank of `MeanRatio` for the cell type.
#' * `MeanRatio.anno` is an annotation of the `MeanRatio` calculation helpful
#' * `MeanRatio.anno` is an annotation of the `MeanRatio` calculation helpful
#' for plotting.
#' * `gene_ensembl` & `gene_name` optional cols from `rowData(sce)`specified by
#' the user to add gene information
Expand Down Expand Up @@ -56,22 +56,22 @@
#' 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")
#' get_mean_ratio(sce_DLPFC_example,
#' cellType_col = "cellType_broad_hc",
#' gene_name = "gene_name",
#' gene_ensembl = "gene_id"
#' )
#'
#' @importFrom dplyr mutate
#' @importFrom dplyr arrange
#' @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 Down
44 changes: 21 additions & 23 deletions R/plot_composition_bar.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,33 @@
#' est_prop_long <- est_prop |>
#' tibble::rownames_to_column("RNum") |>
#' tidyr::pivot_longer(!RNum, names_to = "cell_type", values_to = "prop") |>
#' dplyr::inner_join(pd |> dplyr::select(RNum, Dx))
#'
#' dplyr::inner_join(pd |> dplyr::select(RNum, Dx))
#'
#' est_prop_long
#'
#' # Create composition bar plots
#' # Mean composition of all samples
#' plot_composition_bar(est_prop_long)
#'
#' plot_composition_bar(est_prop_long)
#'
#' # Mean composition by Dx
#' plot_composition_bar(est_prop_long, x_col = "Dx")
#'
#' # control minimum value of text to add
#'
#' # control minimum value of text to add
#' plot_composition_bar(est_prop_long, x_col = "Dx", min_prop_text = 0.1)
#'
#'
#' # plot all samples, then facet by Dx
#' plot_composition_bar(est_prop_long, x_col = "RNum", add_text = FALSE) +
#' ggplot2::facet_wrap(~Dx, scales = "free_x")
#' plot_composition_bar(est_prop_long, x_col = "RNum", add_text = FALSE) +
#' ggplot2::facet_wrap(~Dx, scales = "free_x")
#'
#' @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 @@ -82,12 +81,11 @@ plot_composition_bar <- function(
}


.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: 9 additions & 8 deletions R/plot_gene_express.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@
#'
#' @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: 11 additions & 12 deletions R/plot_marker_express.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,17 @@
#' )
#' @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
21 changes: 10 additions & 11 deletions R/plot_marker_express_ALL.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,16 @@
#' @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 = NULL,
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 = NULL,
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 Down
15 changes: 8 additions & 7 deletions R/plot_marker_express_List.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,14 @@
#' @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 Down
19 changes: 11 additions & 8 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ BiocManager::install("LieberInstitute/DeconvoBuddies")
suppressMessages({
library("DeconvoBuddies")
})
```

### Access Datasets
Expand All @@ -95,9 +94,11 @@ and plotting functions to quickly visualize the expression of selected genes in
snRNA-seq data.
```{r plot_gene_expression, echo=FALSE, warning=FALSE}
plot_gene_express(sce = sce_DLPFC_example,
category = "cellType_broad_hc",
genes = c("GAD2", "CD22"))
plot_gene_express(
sce = sce_DLPFC_example,
category = "cellType_broad_hc",
genes = c("GAD2", "CD22")
)
```

### Plot Deconvoltion Cell Type Proportions
Expand All @@ -109,12 +110,14 @@ set.seed(123)
## pivot data to long format and join with test estimated proportion data
est_prop_long <- est_prop |>
tibble::rownames_to_column("RNum") |>
tidyr::pivot_longer(!RNum, names_to = "cell_type", values_to = "prop")
tidyr::pivot_longer(!RNum, names_to = "cell_type", values_to = "prop")
## composition bar plot
plot_composition_bar(est_prop_long |>
dplyr::filter(RNum %in% sample(rownames(est_prop), 10)),
x_col = "RNum")
plot_composition_bar(
est_prop_long |>
dplyr::filter(RNum %in% sample(rownames(est_prop), 10)),
x_col = "RNum"
)
```

## Citation
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ proportion data from the human DLPFC.
``` r
## Access data with fetch_deconvo_data
sce_DLPFC_example <- fetch_deconvo_data("sce_DLPFC_example")
#> 2024-08-07 12:39:29.443059 loading file /Users/louise.huuki/Library/Caches/org.R-project.R/R/BiocFileCache/58f79a421ca_sce_DLPFC_example.Rdata%3Frlkey%3Dv3z4u8ru0d2y12zgdl1az07q9%26st%3D1dcfqc1i%26dl%3D1
#> 2024-08-13 11:40:40.459055 loading file /Users/louise.huuki/Library/Caches/org.R-project.R/R/BiocFileCache/58f79a421ca_sce_DLPFC_example.Rdata%3Frlkey%3Dv3z4u8ru0d2y12zgdl1az07q9%26st%3D1dcfqc1i%26dl%3D1

## explore the single cell experiment object
sce_DLPFC_example
Expand Down
Loading

0 comments on commit 460ef87

Please sign in to comment.