Skip to content

Commit f904ae2

Browse files
committed
Styling + add a mention about the length
1 parent 224ff2a commit f904ae2

File tree

2 files changed

+56
-41
lines changed

2 files changed

+56
-41
lines changed

R/registration_pseudobulk.R

Lines changed: 48 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222
#' when pseudo-bulking. Pseudo-bulked samples with less than `min_ncells` on
2323
#' `sce_pseudo$ncells` will be dropped.
2424
#' @param filter_expr A `logical(1)` specifying whether to filter pseudobulked
25-
#' counts with `edgeR::filterByExpr`. Defaults to `TRUE`, filtering is recommended for
25+
#' counts with `edgeR::filterByExpr`. Defaults to `TRUE`, filtering is recommended for
2626
#' spatail registratrion workflow.
27-
#' @param mito_gene An optional `logical()` vector indicating which genes are
27+
#' @param mito_gene An optional `logical()` vector indicating which genes are
2828
#' mitochondrial, used to calculate pseudo bulked mitochondrial expression rate
29-
#' `expr_chrM` and `pseudo_expr_chrM` .
29+
#' `expr_chrM` and `pseudo_expr_chrM`. The length has to match the `nrow(sce)`.
3030
#'
3131
#' @return A pseudo-bulked [SingleCellExperiment-class][SingleCellExperiment::SingleCellExperiment-class] object. The `logcounts()` assay are `log2-CPM`
3232
#' values calculated with `edgeR::cpm(log = TRUE)`. See
@@ -61,21 +61,25 @@
6161
#' rowData(sce)$gene_name <- paste0("gene", seq_len(nrow(sce)))
6262
#'
6363
#' ## Pseudo-bulk by Cell Cycle
64-
#' sce_pseudo <- registration_pseudobulk(sce,
65-
#' var_registration = "Cell_Cycle",
66-
#' var_sample_id = "sample_id",
67-
#' covars = c("age"),
68-
#' min_ncells = NULL)
64+
#' sce_pseudo <- registration_pseudobulk(
65+
#' sce,
66+
#' var_registration = "Cell_Cycle",
67+
#' var_sample_id = "sample_id",
68+
#' covars = c("age"),
69+
#' min_ncells = NULL
70+
#' )
6971
#' colData(sce_pseudo)
7072
registration_pseudobulk <-
71-
function(sce,
72-
var_registration,
73-
var_sample_id,
74-
covars = NULL,
75-
min_ncells = 10,
76-
pseudobulk_rds_file = NULL,
77-
filter_expr = TRUE,
78-
mito_gene = NULL) {
73+
function(
74+
sce,
75+
var_registration,
76+
var_sample_id,
77+
covars = NULL,
78+
min_ncells = 10,
79+
pseudobulk_rds_file = NULL,
80+
filter_expr = TRUE,
81+
mito_gene = NULL
82+
) {
7983
## Check that inputs are correct
8084
stopifnot(is(sce, "SingleCellExperiment"))
8185
stopifnot(var_registration %in% colnames(colData(sce)))
@@ -89,7 +93,7 @@ registration_pseudobulk <-
8993
stopifnot(!var_registration %in% covars)
9094
stopifnot(!var_sample_id %in% covars)
9195
stopifnot(var_registration != var_sample_id)
92-
96+
9397
## create var_registration col
9498
sce$var_registration <- sce[[var_registration]]
9599

@@ -142,7 +146,7 @@ registration_pseudobulk <-
142146
"_",
143147
sce_pseudo$registration_variable
144148
)
145-
149+
146150
## rm sce_pseudo$var_registration - redundant with registration variable
147151
sce_pseudo$var_registration <- NULL
148152

@@ -180,29 +184,38 @@ registration_pseudobulk <-
180184
sce_pseudo$registration_variable
181185
)
182186
}
183-
187+
184188
## compute pseudo QC metrics
185189
sce_pseudo$pseudo_sum_umi <- colSums(counts(sce_pseudo))
186-
187-
## if mitochondrial genes are indicated, calculate pseudo mito rate
188-
if(!is.null(mito_gene)){
189-
if(length(mito_gene) == nrow(sce_pseudo)){
190-
sce_pseudo$pseudo_expr_chrM <- colSums(counts(sce_pseudo)[mito_gene, , drop = FALSE])
191-
sce_pseudo$pseudo_expr_chrM_ratio <- sce_pseudo$pseudo_expr_chrM / sce_pseudo$pseudo_sum_umi
192-
} else {
193-
warning("length(mito_gene) != nrow(sce_pseudo) : unable to calc 'pseudo_expr_chrM' metrics")
194-
}
195190

191+
## if mitochondrial genes are indicated, calculate pseudo mito rate
192+
if (!is.null(mito_gene)) {
193+
if (length(mito_gene) == nrow(sce_pseudo)) {
194+
sce_pseudo$pseudo_expr_chrM <- colSums(counts(sce_pseudo)[
195+
mito_gene,
196+
,
197+
drop = FALSE
198+
])
199+
sce_pseudo$pseudo_expr_chrM_ratio <- sce_pseudo$pseudo_expr_chrM /
200+
sce_pseudo$pseudo_sum_umi
201+
} else {
202+
warning(
203+
"length(mito_gene) != nrow(sce_pseudo) : unable to calc 'pseudo_expr_chrM' metrics"
204+
)
205+
}
196206
}
197-
207+
198208
## Drop lowly-expressed genes
199-
if(filter_expr){
200-
message(Sys.time(), " drop lowly expressed genes")
201-
keep_expr <-
202-
edgeR::filterByExpr(sce_pseudo, group = sce_pseudo$registration_variable)
203-
sce_pseudo <- sce_pseudo[which(keep_expr), ]
209+
if (filter_expr) {
210+
message(Sys.time(), " drop lowly expressed genes")
211+
keep_expr <-
212+
edgeR::filterByExpr(
213+
sce_pseudo,
214+
group = sce_pseudo$registration_variable
215+
)
216+
sce_pseudo <- sce_pseudo[which(keep_expr), ]
204217
}
205-
218+
206219
## Compute the logcounts
207220
message(Sys.time(), " normalize expression")
208221
logcounts(sce_pseudo) <-

man/registration_pseudobulk.Rd

Lines changed: 8 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)