diff --git a/R/genotype_io.R b/R/genotype_io.R index 8774385d..8fecfdc6 100644 --- a/R/genotype_io.R +++ b/R/genotype_io.R @@ -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", @@ -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) } diff --git a/tests/testthat/test_load_genotype.R b/tests/testthat/test_load_genotype.R index 8ca761d7..60854574 100644 --- a/tests/testthat/test_load_genotype.R +++ b/tests/testthat/test_load_genotype.R @@ -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,