Skip to content

Commit

Permalink
Minor bugfixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
LTLA committed Dec 7, 2024
1 parent 142296a commit 4b5186b
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 20 deletions.
18 changes: 6 additions & 12 deletions R/barcodeRanks.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@
#' \describe{
#' \item{\code{rank}:}{Numeric, the rank of each barcode (averaged across ties).}
#' \item{\code{total}:}{Numeric, the total counts for each barcode.}
#' \item{\code{fitted}:}{Numeric, the fitted value from the spline for each barcode.
#' This is \code{NA} for points with \code{x} outside of \code{fit.bounds}.}
#' }
#'
#' The metadata contains \code{knee}, a numeric scalar containing the total count at the knee point;
Expand All @@ -76,7 +74,6 @@
#' # Making a plot.
#' plot(br.out$rank, br.out$total, log="xy", xlab="Rank", ylab="Total")
#' o <- order(br.out$rank)
#' lines(br.out$rank[o], br.out$fitted[o], col="red")
#' abline(h=metadata(br.out)$knee, col="dodgerblue", lty=2)
#' abline(h=metadata(br.out)$inflection, col="forestgreen", lty=2)
#' legend("bottomleft", lty=2, col=c("dodgerblue", "forestgreen"),
Expand Down Expand Up @@ -124,21 +121,19 @@ NULL
if (is.null(fit.bounds)) {
new.keep <- left.edge:right.edge
} else {
new.keep <- y > log10(fit.bounds[1]) & y < log10(fit.bounds[2])
new.keep <- which(y > log10(fit.bounds[1]) & y < log10(fit.bounds[2]))
}

# Using the maximum distance to identify the knee point.
fitted.vals <- rep(NA_real_, length(keep))

if (length(new.keep) >= 4) {
curx <- x[new.keep]
cury <- y[new.keep]
xbounds <- curx[c(1L, length(new.keep))]
ybounds <- cury[c(1L, length(new.keep))]
m <- diff(ybounds)/diff(xbounds)
b <- ybounds[1] - xbounds[1] * m
above <- which(cury >= curx * m + b)
dist <- abs(m * curx[above] - cury[above] + b)/sqrt(m^2 + 1)
gradient <- diff(ybounds)/diff(xbounds)
intercept <- ybounds[1] - xbounds[1] * gradient
above <- which(cury >= curx * gradient + intercept)
dist <- abs(gradient * curx[above] - cury[above] + intercept)/sqrt(gradient^2 + 1)
knee <- 10^(cury[above[which.max(dist)]])
} else {
# Sane fallback upon overly aggressive filtering by 'exclude.from', 'lower'.
Expand All @@ -148,8 +143,7 @@ NULL
# Returning a whole stack of useful stats.
out <- DataFrame(
rank=.reorder(run.rank, stuff$lengths, o),
total=.reorder(run.totals, stuff$lengths, o),
fitted=.reorder(fitted.vals, stuff$lengths, o)
total=.reorder(run.totals, stuff$lengths, o)
)
rownames(out) <- colnames(m)
metadata(out) <- list(knee=knee, inflection=inflection)
Expand Down
3 changes: 0 additions & 3 deletions man/barcodeRanks.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions tests/testthat/test-misc.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ test_that("barcodeRanks runs to completion", {
brout <- barcodeRanks(my.counts, lower=limit)
expect_equal(brout$total, totals)
expect_identical(brout$rank, rank(-totals, ties.method="average"))
expect_true(all(is.na(brout$fitted[totals <= limit])))

# Trying again with a higher limit.
limit2 <- 200
Expand All @@ -21,9 +20,8 @@ test_that("barcodeRanks runs to completion", {
# Specifying the boundaries.
bounds <- c(200, 1000)
brout3 <- barcodeRanks(my.counts, lower=limit, fit.bounds=bounds)
is.okay <- totals > bounds[1] & totals < bounds[2]
expect_true(all(is.na(brout3$fitted[!is.okay])))
expect_true(all(!is.na(brout3$fitted[is.okay])))
knee <- metadata(brout3)$knee
expect_true(knee >= bounds[1] && knee <= bounds[2])

# Respecting column names.
alt <- my.counts
Expand All @@ -32,7 +30,6 @@ test_that("barcodeRanks runs to completion", {
expect_identical(rownames(brout2), colnames(alt))
expect_identical(names(brout2$rank), NULL)
expect_identical(names(brout2$total), NULL)
expect_identical(names(brout2$fitted), NULL)

# Trying out silly inputs.
expect_error(barcodeRanks(my.counts[,0]), "insufficient")
Expand Down

0 comments on commit 4b5186b

Please sign in to comment.