Constructs a loader function that retrieves per-block LD matrices on demand. This avoids loading all blocks into memory simultaneously, which is critical for genome-wide analyses with hundreds of blocks.
Usage
ld_loader(
R_list = NULL,
X_list = NULL,
ld_meta_path = NULL,
regions = NULL,
LD_info = NULL,
return_genotype = FALSE,
max_variants = NULL
)Arguments
- R_list
List of G precomputed LD correlation matrices (p_g x p_g).
- X_list
List of G genotype matrices (n x p_g).
- ld_meta_path
Path to a pecotmr LD metadata TSV file (as used by
load_LD_matrix).- regions
Character vector of G region strings (e.g.,
"chr22:17238266-19744294"). Required whenld_meta_pathis used.- LD_info
A data.frame with column
LD_file(paths to genotype files or.cor.xzLD matrix files) and optionallySNP_file(paths to companion.bimfiles for pre-computed blocks; defaults topaste0(LD_file, ".bim")if absent). Genotype paths can be PLINK2 prefixes, PLINK1 prefixes, VCF files, or GDS files. As returned by cTWAS meta-data utilities.- return_genotype
Logical. When using region mode, return the genotype matrix X (
TRUE) or LD correlation R (FALSE, default).- max_variants
Integer or
NULL. If set, randomly subsample blocks larger than this to control memory usage.
Value
A function loader(g) that, given a block index g,
returns the corresponding LD matrix or genotype matrix.
Details
Four modes are supported:
- list mode (R)
Pre-loaded list of LD correlation matrices. Simple but uses more memory. Set
R_list.- list mode (X)
Pre-loaded list of genotype matrices (n x p_g). Set
X_list.- region mode
Loads LD from a pecotmr metadata TSV file on the fly via
load_LD_matrix. Memory-efficient for large datasets. Setld_meta_pathandregions.- LD_info mode
Loads pre-computed LD blocks from
.cor.xzfiles listed in anLD_infodata.frame (as returned by cTWAS meta-data utilities). SetLD_info.
Examples
# List mode with pre-computed LD
R1 <- diag(10)
R2 <- diag(15)
loader <- ld_loader(R_list = list(R1, R2))
loader(1) # returns R1
#> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#> [1,] 1 0 0 0 0 0 0 0 0 0
#> [2,] 0 1 0 0 0 0 0 0 0 0
#> [3,] 0 0 1 0 0 0 0 0 0 0
#> [4,] 0 0 0 1 0 0 0 0 0 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