Skip to content

Commit

Permalink
source functions as child report
Browse files Browse the repository at this point in the history
  • Loading branch information
allyhawkins committed Oct 12, 2023
1 parent 2d07a7b commit 605ebd0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 63 deletions.
65 changes: 5 additions & 60 deletions templates/qc_report/celltypes_qc.rmd
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Cell type Annotation Summary

```{r, child='utils/celltype_functions.rmd'}
```


```{r}
## function definitions ##
Expand Down Expand Up @@ -112,66 +117,6 @@ plot_umap <- function(
theme(legend.position = "bottom")
}
#' Create `celltype_df` data frame for use in cell type QC reports
#'
#' @param processed_sce The processed sce object with cell type annotations in colData
#'
#' @return `celltype_df` with column of cell types, as factors, for each annotation method
create_celltype_df <- function(processed_sce) {
celltype_df <- processed_sce |>
scuttle::makePerCellDF(use.dimred = "UMAP") |>
# rename UMAP columns as needed to remove potential period added by `scuttle::makePerCellDF`
dplyr::rename_with(
\(x) stringr::str_replace(x, "^UMAP\\.", "UMAP"),
starts_with("UMAP")
) |>
# only keep columns of interest
dplyr::select(
barcodes,
cluster,
contains("UMAP"),
contains("singler"),
contains("cellassign"),
contains("submitter")
)
if ("singler_celltype_annotation" %in% names(celltype_df)) {
celltype_df <- celltype_df |>
prepare_annotation_values(singler_celltype_annotation)
}
if ("cellassign_celltype_annotation" %in% names(celltype_df)) {
celltype_df <- celltype_df |>
prepare_annotation_values(cellassign_celltype_annotation)
}
return (celltype_df)
}
#' Prepare and reformat cell type annotation values for use in QC reports
#' Unknown cell types are updated with the label "Unknown cell type", and
#' cell types are ordered in order of descending frequency, but with
#' "Unknown cell type" as the last level
#'
#' @param df The data frame containing cell type annotations, one row per cell
#' @param annotation_column The column (written plainly, not a string) containing annotations to reformat
#'
#' @return Updated data frame with the `annotation_column` reformatted
prepare_annotation_values <- function(df, annotation_column) {
df |>
dplyr::mutate(
{{ annotation_column }} := dplyr::case_when(
# singler condition
is.na({{ annotation_column }}) ~ "Unknown cell type",
# cellassign conditon
{{ annotation_column }} == "other" ~ "Unknown cell type",
# otherwise, keep it
.default = {{ annotation_column }}
) |>
forcats::fct_infreq() |>
forcats::fct_relevel("Unknown cell type", after = Inf)
)
}
```


Expand Down
9 changes: 7 additions & 2 deletions templates/qc_report/celltypes_supplemental_report.rmd
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,15 @@ theme_set(
strip.background = element_rect(fill = "transparent")
)
)
```


```{r, child='utils/celltype_functions.rmd'}
```

# Source functions for preparing cell type data
source(file.path("utils", "celltype_functions.R"))

```{r setup, message = FALSE, echo = FALSE}
# define library and sce object
library_id <- params$library
processed_sce <- params$processed_sce
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
```{r}
library(SingleCellExperiment)
# This script contains function definitions that are used by both
Expand Down Expand Up @@ -65,5 +66,5 @@ prepare_annotation_values <- function(df, annotation_column) {
forcats::fct_relevel("Unknown cell type", after = Inf)
)
}

```

0 comments on commit 605ebd0

Please sign in to comment.