Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions R/genotype_io.R
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ computeBlockLdCor <- function(handle, snp_idx, backend = "internal",

ext <- tolower(file_ext(path))
if (nzchar(ext)) {
return(switch(ext,
detected <- switch(ext,
"vcf" = "vcf",
"bcf" = "vcf",
"bed" = "plink1",
Expand All @@ -475,16 +475,19 @@ computeBlockLdCor <- function(handle, snp_idx, backend = "internal",
"annot" = "ldsc_annot",
"bw" = "bigwig",
"bigwig" = "bigwig",
stop("Cannot detect format from extension: ", ext)
))
NULL
)
if (!is.null(detected)) return(detected)
}
# No extension — check for plink stem files
# Check for file stems, including dotted prefixes such as sample.EUR.chr21.
if (file.exists(paste0(path, ".pgen")) || file.exists(paste0(path, ".pvar")))
return("plink2")
if (file.exists(paste0(path, ".bed")) || file.exists(paste0(path, ".bim")))
return("plink1")
if (file.exists(paste0(path, ".gds")))
return("gds")
if (nzchar(ext))
stop("Cannot detect format from extension: ", ext)
stop("Cannot detect genotype format for path: ", path)
}

Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/test_load_genotype.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ region_sub <- "chr21:17513228-17550000"
n_samples <- 100L
n_variants <- 349L

test_that("format detection supports dotted PLINK2 prefixes", {
tmp <- tempfile("plink2_dotted_prefix_")
prefix <- file.path(dirname(tmp), "ADSP.R4.EUR.chr21")
file.create(paste0(prefix, ".pgen"), paste0(prefix, ".pvar"), paste0(prefix, ".psam"))
on.exit(unlink(paste0(prefix, c(".pgen", ".pvar", ".psam"))), add = TRUE)

expect_equal(pecotmr:::.h2_detect_format(prefix), "plink2")
})

# Shared helper: validate the output structure from load_genotype_region
# (with return_variant_info=TRUE)
check_genotype_result <- function(result, expected_nrow = n_samples, expected_ncol = n_variants,
Expand Down