cleanSizeFactors {scuttle} | R Documentation

## Clean out non-positive size factors

### Description

Coerce non-positive size factors (occasionally generated by `pooledSizeFactors`) to positive values based on the number of detected features.

### Usage

    cleanSizeFactors(
      size.factors,
      num.detected,
      control = nls.control(warnOnly = TRUE),
      iterations = 3,
      nmads = 3,
      ...
    )

### Arguments

`size.factors` | A numeric vector containing size factors for all libraries.
`num.detected` | A numeric vector of the same length as `size.factors`, containing the number of features detected in each library.
`control` | Argument passed to `nls` to control the fitting, see `?nls.control` for details.
`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 `nls`.

### Details

This function will first fit a non-linear curve of the form

y = 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.

### Value

A numeric vector identical to `size.factors` but with all non-positive size factors replaced with fitted values from the curve.

### Author(s)

Aaron Lun

### See Also

`pooledSizeFactors`, which can occasionally generate negative size factors.

`nls`, which performs the curve fitting.

### Examples

    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)

[Package scuttle version 1.2.1 Index]