Skip to content

Commit a13091a

Browse files
authored
Merge branch 'main' into ia/cran-release-prep
2 parents 44f70e5 + 814226d commit a13091a

6 files changed

Lines changed: 83 additions & 10 deletions

File tree

r/R/atac_utils.R

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,17 +206,29 @@ qc_scATAC <- function(fragments, genes, blacklist) {
206206

207207
# Compute signal & background regions for TSSEnrichment calculation
208208
tss_window_width <- 101L
209-
tss_window <- tss %>% dplyr::mutate(start = start - 50L, end = start + tss_window_width)
209+
tss_window <- tss %>%
210+
dplyr::mutate(
211+
start = as.integer(pmax(start - 50L, 0L)),
212+
end = start + tss_window_width
213+
)
210214

211215
tss_flank_width <- 100L
212216
tss_flank <- dplyr::bind_rows(
213-
dplyr::mutate(tss, start = start + 1901L, end = start + tss_flank_width),
214-
dplyr::mutate(tss, start = start - 2000L, end = start + tss_flank_width)
217+
dplyr::mutate(
218+
tss,
219+
start = as.integer(pmax(start + 1901L, 0L)),
220+
end = start + tss_flank_width
221+
),
222+
dplyr::mutate(
223+
tss,
224+
start = as.integer(pmax(start - 2000L, 0L)),
225+
end = start + tss_flank_width
226+
)
215227
)
216228

217229
promoters <- genes %>%
218230
dplyr::mutate(
219-
start = dplyr::if_else(strand, start - 2000L, end - 101L),
231+
start = as.integer(pmax(dplyr::if_else(strand, start - 2000L, end - 101L), 0L)),
220232
end = start + 2000L + 101L
221233
)
222234

r/R/errorChecking.R

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2021 BPCells contributors
1+
# Copyright 2025 BPCells contributors
22
#
33
# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
44
# https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
@@ -16,11 +16,46 @@
1616
# skip over during error printing.
1717

1818
argument_name <- function(arg, n) {
19+
substitute_if_present <- function(expr, env) {
20+
if (is.null(env) || !is.symbol(expr)) return(expr)
21+
symbol <- as.character(expr)
22+
if (!exists(symbol, envir = env, inherits = FALSE)) return(expr)
23+
result <- do.call(substitute, list(expr, env))
24+
if (!is.language(result)) return(expr)
25+
result
26+
}
27+
1928
arg_name <- substitute(arg)
20-
for (f in seq_len(n)) {
21-
arg_name <- do.call(substitute, list(arg_name, parent.frame(f)))
29+
if (n > 0) {
30+
for (f in seq_len(n)) {
31+
env <- parent.frame(f)
32+
arg_name <- substitute_if_present(arg_name, env)
33+
}
2234
}
23-
arg_name <- deparse(arg_name)
35+
36+
# Continue walking up the call stack so wrappers (e.g., pkgdown example helpers)
37+
# don't collapse names to temporary argument placeholders.
38+
frame <- n
39+
extra_steps <- 0
40+
max_extra <- 200
41+
while (extra_steps < max_extra) {
42+
frame <- frame + 1
43+
env <- parent.frame(frame)
44+
arg_name_new <- substitute_if_present(arg_name, env)
45+
if (identical(arg_name_new, arg_name)) {
46+
if (identical(env, globalenv()) || identical(env, baseenv()) || identical(env, emptyenv())) break
47+
extra_steps <- extra_steps + 1
48+
next
49+
}
50+
arg_name <- arg_name_new
51+
if (identical(env, globalenv()) || identical(env, baseenv()) || identical(env, emptyenv())) break
52+
extra_steps <- extra_steps + 1
53+
}
54+
55+
# `deparse()` can return multi-line output (length > 1) for large expressions.
56+
# Collapse to a single string so downstream consumers that expect a scalar
57+
# name (e.g., column labels) don't get tripped up by long vectors.
58+
arg_name <- paste(deparse(arg_name), collapse = "")
2459
return(arg_name)
2560
}
2661

r/R/plots.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ collect_features <- function(source, features = NULL, gene_mapping = human_gene_
552552
#' cluster_graph_louvain()
553553
#'
554554
#'
555-
#' ## Plot embedding
555+
#' ## Plot embeddings
556556
#' print(length(clusts))
557557
#'
558558
#' plot_embedding(clusts, umap)

r/man/plot_embedding.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

r/tests/testthat/test-atac_utils.R

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,4 +715,29 @@ test_that("Regression test for gene_score_archr() Issues 185 + 188", {
715715
# Check that tile_width changing doesn't cause crashes in `gene_score_archr`
716716
bp_ans <- gene_score_archr(bp_frags, genes, chromosome_sizes, tile_width=1000)
717717
expect_identical(as(bp_ans, "dgCMatrix"), ans)
718+
})
719+
720+
test_that("qc_scATAC handles genes near the start of a chromosome", {
721+
frag_tbl <- tibble::tibble(
722+
chr = "chr1",
723+
start = c(0L, 25L, 50L, 75L),
724+
end = start + 20L,
725+
cell_id = rep(c("cell1", "cell2"), each = 2)
726+
)
727+
fragments <- convert_to_fragments(frag_tbl)
728+
729+
genes <- tibble::tibble(
730+
chr = "chr1",
731+
start = c(0L, 25L),
732+
end = c(75L, 80L),
733+
strand = c("+", "-")
734+
)
735+
blacklist <- tibble::tibble(
736+
chr = "chr1",
737+
start = 500L,
738+
end = 550L
739+
)
740+
741+
qc <- qc_scATAC(fragments, genes, blacklist)
742+
expect_identical(sort(qc$cellName), sort(cellNames(fragments)))
718743
})

r/tests/testthat/test-fragment_utils.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ test_that("Merge cells works", {
191191

192192
test_that("GRanges conversion round-trips", {
193193
skip_if_not_installed("GenomicRanges")
194+
skip_if_not_installed("GenomeInfoDb")
194195
frags <- open_fragments_10x("../data/mini_fragments.tsv.gz") #nolint
195196
raw <- write_fragments_memory(frags)
196197
ranges <- as(raw, "GRanges")

0 commit comments

Comments
 (0)