Randomly subsample reads to a fixed depth across all samples in a qseaSet. Useful for equalising library sizes prior to comparison.

downSample(qseaSet, nReads)

Arguments

qseaSet

qseaSet. Input object whose per-window counts will be downsampled. Default: none (must be supplied).

nReads

integer(1). Target number of reads to retain per sample. If a sample has fewer than nReads, it is left unchanged. Default: none (must be supplied).

Value

A qseaSet with downsampled counts. Library metadata (valid_fragments, offset, library_factor) are updated accordingly.

Details

Downsampling is performed independently per sample. To obtain reproducible results across runs, call set.seed() before invoking downSample(). Samples with total reads < nReads are not upsampled.

Examples

data(exampleTumourNormal, package = "mesa")

set.seed(1)
# Downsample to 100 fragments per sample, then check per-sample totals
exampleTumourNormal %>%
  downSample(100) %>%
  getCountTable() %>%
  dplyr::select(tidyselect::matches("_[NT]")) %>%
  colSums() %>%
  range()  # should be ~ c(100, 100) if all samples had >= 100 reads
#> Downsampling all samples to 100 each
#> Generating table of counts values for 819 regions across 10 samples.
#> [1] 100 100

# Also inspect the updated library sizes recorded in the sample table
exampleTumourNormal %>%
  downSample(100) %>%
  qsea::getSampleTable() %>%
  dplyr::select(patient, type, gender)
#> Downsampling all samples to 100 each
#>          patient        type gender
#> Colon1_N  Colon1 NormalColon      F
#> Colon1_T  Colon1         CRC      F
#> Colon2_N  Colon2 NormalColon      F
#> Colon2_T  Colon2         CRC      F
#> Lung1_N    Lung1  NormalLung      F
#> Lung1_T    Lung1        LUAD      F
#> Lung2_N    Lung2  NormalLung      F
#> Lung2_T    Lung2        LUSC      F
#> Lung3_N    Lung3  NormalLung      M
#> Lung3_T    Lung3        LUAD      M