View source: R/cleanSizeFactors.R
cleanSizeFactors | R Documentation |
Coerce non-positive size factors (occasionally generated by pooledSizeFactors
) to positive values based on the number of detected features.
cleanSizeFactors(
size.factors,
num.detected,
control = nls.control(warnOnly = TRUE),
iterations = 3,
nmads = 3,
...
)
size.factors |
A numeric vector containing size factors for all libraries. |
num.detected |
A numeric vector of the same length as |
control |
Argument passed to |
iterations |
Integer scalar specifying the number of robustness iterations. |
nmads |
Numeric scalar specifying the multiple of MADs to use for the tricube bandwidth in robustness iterations. |
... |
Further arguments to pass to |
This function will first fit a non-linear curve of the form
y = \frac{ax}{1 + bx}
where y
is num.detected
and x
is size.factors
for all positive size factors.
This is a purely empirical expression, chosen because it is passes through the origin, is linear near zero and asymptotes at large x
.
The fitting is done robustly with iterations of tricube weighting to eliminate outliers.
We then consider the number of detected features for all samples with non-positive size factors.
This is treated as y
and used to solve for x
based on the curve fitted above.
The result is the “cleaned” size factor, which must always be positive for y < a/b
.
For y > a/b
, there is no solution so the cleaned size factor is defined as the largest positive value in size.factors
.
Negative size factors can occasionally be generated by pooledSizeFactors
, see the documentation there for more details.
By coercing them to positive values, we can proceed to normalization and downstream analyses.
Here, we use the number of detected features as this is more robust to differential expression that would cause biases in the library size.
Of course, it is not theoretically guaranteed to yield the correct size factor, but a rough guess is better than a negative value.
A numeric vector identical to size.factors
but with all non-positive size factors replaced with fitted values from the curve.
Aaron Lun
pooledSizeFactors
, which can occasionally generate negative size factors.
nls
, which performs the curve fitting.
set.seed(100)
counts <- matrix(rpois(20000, lambda=1), ncol=100)
library(scuttle)
sf <- librarySizeFactors(counts)
ngenes <- colSums(counts > 0)
# Adding negative size factor values to be cleaned.
out <- cleanSizeFactors(c(-1, -1, sf), c(100, 50, ngenes))
head(out)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.