Skip to contents

Correlation between variants, from either a dosage matrix already in memory or a genotype panel read on demand.

Three backends compute the same sample correlation different ways. "internal" is a single BLAS crossprod (Rfast::cora, or cor() without it) and is both the fastest and the default; "snprelate" and "snpstats" exist to cross-check it and do strictly more work, since each converts the same matrix into its own representation first.

Usage

computeLd(
  X,
  method = c("sample", "population", "gcta"),
  backend = c("internal", "snprelate", "snpstats"),
  trimSamples = FALSE,
  shrinkage = 0,
  onDisk = FALSE,
  snpIdx = NULL
)

Arguments

X

A numeric dosage matrix (samples x variants), or a genotype panel – a RangedSummarizedExperiment whose dosage assay reads through a genotype handle.

method

Estimator: "sample" (default), "population" or "gcta". The last two are "internal"-only.

backend

"internal" (default), "snprelate" or "snpstats".

trimSamples

Logical; drop samples to a multiple of four, matching DENTIST's block handling. Ignored for method = "sample".

shrinkage

Numeric in (0, 1]; shrink towards the identity (lassosum, Mak et al. 2017). Zero (default) applies none.

onDisk

Logical, default FALSE. Read the correlation straight off a GDS panel without materialising dosages. The dosage matrix scales with samples x variants while the result scales with variants^2, so on a wide panel the input dwarfs the output and keeping it on disk is what makes a large block feasible at all.

Not a drop-in substitute: SNPRelate applies its own missing-data policy where the in-memory backends mean-impute first. On complete data the two agree to ~2e-15, but with missing calls they diverge and the gap widens with the missing rate. Requires backend = "snprelate", method = "sample" and a GDS-backed panel.

snpIdx

Optional variant selection when X is a panel; row indices into it. NULL (default) uses every variant. Not meaningful for a dosage matrix, which is already the block.

Value

A numeric correlation matrix (variants x variants), with dimnames taken from the block where available.

Examples

data(qtlSumStatsExample)
panel <- getLdSketch(qtlSumStatsExample)
R <- computeLd(panel, snpIdx = 1:5)
dim(R)
#> [1] 5 5
# A dosage matrix already in memory is correlated directly.
X <- matrix(rbinom(200, 2, 0.3), nrow = 50, ncol = 4)
dim(computeLd(X))
#> [1] 4 4