statgen stores each analysis object on a shared
reference SNP axis. The R package loads reference panels, summary
statistics, annotations, genotypes, and LD panels into those
coordinates.
We use quiet to hide progress messages and loading
summaries. Warnings remain visible.
The bundled package fixtures are intentionally tiny so that examples and CRAN checks do not require external tools or large data.
This vignette reads those installed fixtures with
system.file(..., package = "statgen"). They are
demonstration inputs only. In real analyses, replace these paths with
post-harmonization reference, summary statistics, annotation, genotype,
and LD files prepared for one analysis input set. For an end-to-end
repository workflow that prepares source inputs and then runs the same R
analysis pattern on those files, see
docs/TUTORIAL_1_PREPARE_DATA.md and
docs/TUTORIAL_4_R.md.
reference_template <- file.path(
dirname(system.file("extdata", "reference_chr1.bim", package = "statgen")),
"reference_chr@.bim"
)
reference <- load_reference(reference_template)
sumstats_path <- system.file("extdata", "traits_complete.tsv.gz", package = "statgen")
sumstats <- load_sumstats(sumstats_path, reference)
annotation_paths <- system.file(
"extdata",
c("anno1.bed", "anno2.bed"),
package = "statgen"
)
binary_annotations <- load_annotations(annotation_paths, reference)
grouped_annotations <- load_annotation(
system.file("extdata", "grouped.annot", package = "statgen"),
reference,
has_header = TRUE,
group_column = "group"
)
continuous_path <- tempfile(fileext = ".annot")
writeLines(c(
"chrom\tstart0\tend0\tscore",
"1\t99\t200\t0.5",
"1\t299\t400\t1.5",
"X\t99\t301\t2.5"
), continuous_path)
continuous_annotations <- load_annotation(
continuous_path,
reference,
has_header = TRUE,
value_columns = "score"
)
annotations <- union_annotations(
union_annotations(binary_annotations, grouped_annotations),
continuous_annotations
)
num_snp(reference)
#> [1] 8
head(logpvec(sumstats))
#> [1] 1.920819 0.301030 1.301030 0.698970 0.397940 2.522879
colnames(annomat(annotations))
#> [1] "anno1" "anno2" "coding" "regulatory" "score"
is_binary(annotations)
#> [1] TRUE TRUE TRUE TRUE FALSE
annotation_metadata(annotations)[!is_binary(annotations)]
#> [1] "{\"source_file\":\"/var/folders/nh/zxbq40293414vwvtxbyjw8hc0000gn/T//RtmpOrB2oD/file6d9bfdcf5e6.annot\",\"source_file_has_header\":1,\"num_source_intervals\":3,\"value_column\":\"score\"}"R-native object caches are RDS files. They are useful for repeated analyses in the same R runtime.
Loaders default to match_by = "genomic_position",
matching positions and alleles. This chrX example enables indel swaps in
that mode. See ?allele_matching for allele roles, matching
options, and cache behavior.
swap_root <- system.file("extdata", "allele_swaps", package = "statgen", mustWork = TRUE)
swap_reference <- load_reference(file.path(swap_root, "reference.bim"), shards = "X")
swap_stats <- load_sumstats(file.path(swap_root, "sumstats.tsv.gz"), swap_reference,
allow_indel_swaps = TRUE)
swap_genotype <- load_genotype(file.path(swap_root, "genotype", "@"), swap_reference,
allow_indel_swaps = TRUE)
eaf_vec(swap_stats)
#> [1] 0.8 0.2 0.8
is_allele_swapped(swap_genotype)
#> [1] TRUE FALSE TRUE
fetch_genotypes_int8(swap_genotype, c(1, 3, 1))
#> [,1] [,2] [,3]
#> [1,] 2 0 2
#> [2,] 0 2 0eaf_vec() gives the frequency of the reference panel’s
a1 (ALT) allele, and fetch_genotypes_int8()
counts copies of that allele. is_allele_swapped() shows
which variants required count inversion.
To select marker IDs instead of positions, both loaders accept
match_by = "id"; allele matching is still required. For
example:
trait_by_id <- load_sumstats(path, reference, match_by = "id")
genotype_by_id <- load_genotype(bfile_prefix, reference, match_by = "id")ID mode supports non-palindromic SNVs, including strand complements.
Comparison is case-sensitive: rs123 and RS123
do not match. Sumstats require SNP, the allele columns, and
P and ignore coordinates, searching all source rows;
genotypes search only the corresponding chromosome. Reference ID/allele
keys are checked for ambiguity across the supplied panel in both
loaders. See ?allele_matching for eligibility and duplicate
handling.
Python builds the canonical LD .npz distribution. For
package examples, ld_root points to a bundled toy
distribution that already includes the R reference-cache sidecar. For
real analyses, run prepare_ld_npz_for_r() once on the
Python-built ld_npz/ directory for the same harmonized
input set, then load that directory with load_ld().
ld_root <- system.file("extdata", "ld", package = "statgen")
ld <- load_ld(ld_root)
num_snp(ld)
#> [1] 8
a1freq(ld)
#> [1] 0.30 0.40 0.20 0.35 0.15 0.28 0.32 0.22multiply_r2() multiplies aligned vectors or matrices by
LD r^2. fast_prune() applies greedy LD pruning
to aligned scores.
Genotype panels read calls from PLINK BED files when you request
them. Keep those files available; if you move them, supply their new
location with bed_path. A replacement file must keep the
same variants, samples, and allele order. Counts use the usual 0/1/2
scale; use haploid_mode = "ploidy_scaled" to adjust for
biological ploidy, such as male chrX. See ?allele_matching
for allele orientation and BED overrides.
ref_chr1 <- load_reference(system.file("extdata", "reference_chr1.bim", package = "statgen"))
bed_chr1 <- system.file("extdata", "genotype_1.bed", package = "statgen")
genotype <- load_genotype(sub("\\.bed$", "", bed_chr1), ref_chr1)
geno <- fetch_genotypes_int8(genotype, c(1, 3))
dim(geno)
#> [1] 4 2
head(geno)
#> [,1] [,2]
#> [1,] 2 2
#> [2,] 2 2
#> [3,] 2 2
#> [4,] 2 2