Skip to main content

remove_batch_effect

Function remove_batch_effect 

Source
pub fn remove_batch_effect<T: EdgeFloat>(
    x: &[T],
    n_genes: usize,
    n_samples: usize,
    batch: Option<&[usize]>,
    batch2: Option<&[usize]>,
    covariates: Option<(&[f64], usize)>,
    design: Option<(&[f64], usize)>,
    weights: Option<&Recycled<f64>>,
) -> Result<Vec<f64>, EdgeErrors>
Expand description

Removes batch effects and covariates from a log-expression matrix.

Port of limma’s removeBatchEffect without group. Fits cbind(design, X_batch) per gene with lm_fit, where X_batch = cbind(contr.sum(batch), contr.sum(batch2), covariates), and returns x - beta_batch %*% t(X_batch). As in limma, covariates are column-centred first, aliased coefficients count as zero, and with no batch, second batch or covariates the input comes back unchanged.

§Params

  • x - Log-expression, row-major n_genes * n_samples. Non-finite entries are treated as missing by the fit and stay non-finite in the output.
  • n_genes - Number of genes
  • n_samples - Number of samples
  • batch - Optional batch label per sample
  • batch2 - Optional second batch label per sample
  • covariates - Optional numeric covariates, row-major n_samples * n_cov, with n_cov
  • design - Optional design of interest, row-major n_samples * n_coef, with n_coef. None is an intercept only.
  • weights - Optional observation weights, forwarded to lm_fit as limma’s ... does

§Returns

The corrected matrix, row-major n_genes * n_samples, or EdgeErrors if a shape disagrees or a batch factor has fewer than two levels.

§References

Smyth, Statistical Applications in Genetics and Molecular Biology 3(1), 2004