diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml new file mode 100644 index 0000000..d9500fd --- /dev/null +++ b/.github/workflows/R-CMD-check.yaml @@ -0,0 +1,61 @@ +# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples +# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help +on: + push: + branches: [main, master] + paths: + - 'R-package/**' + - '.github/workflows/R-CMD-check.yaml' + pull_request: + branches: [main, master] + paths: + - 'R-package/**' + - '.github/workflows/R-CMD-check.yaml' + +name: R-CMD-check + +permissions: read-all + +jobs: + R-CMD-check: + runs-on: ${{ matrix.config.os }} + + name: ${{ matrix.config.os }} (${{ matrix.config.r }}) + + strategy: + fail-fast: false + matrix: + config: + - {os: macos-latest, r: 'release'} + - {os: windows-latest, r: 'release'} + - {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'} + - {os: ubuntu-latest, r: 'release'} + - {os: ubuntu-latest, r: 'oldrel-1'} + + env: + GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} + R_KEEP_PKG_SOURCE: yes + + steps: + - uses: actions/checkout@v4 + + - uses: r-lib/actions/setup-pandoc@v2 + + - uses: r-lib/actions/setup-r@v2 + with: + r-version: ${{ matrix.config.r }} + http-user-agent: ${{ matrix.config.http-user-agent }} + use-public-rspm: true + + - uses: r-lib/actions/setup-r-dependencies@v2 + with: + extra-packages: any::rcmdcheck + needs: check + working-directory: R-package + + - uses: r-lib/actions/check-r-package@v2 + with: + upload-snapshots: true + args: 'c("--as-cran", "--no-manual")' + build_args: 'c("--no-manual", "--compact-vignettes=gs+qpdf")' + working-directory: R-package diff --git a/.gitignore b/.gitignore index 2685784..6c9b191 100644 --- a/.gitignore +++ b/.gitignore @@ -26,13 +26,6 @@ venv/ *.asv octave-workspace -# Generated R package extdata copied from canonical tests/fixtures sources -/R-package/inst/extdata/*.bim -/R-package/inst/extdata/*.bed -/R-package/inst/extdata/*.fam -/R-package/inst/extdata/*.ploidy -/R-package/inst/extdata/*.tsv.gz - # Claude Code .claude/ diff --git a/R-package/.Rbuildignore b/R-package/.Rbuildignore new file mode 100644 index 0000000..b292b85 --- /dev/null +++ b/R-package/.Rbuildignore @@ -0,0 +1,3 @@ +^cran-comments\.md$ +^revdep$ +^LICENSE\.md$ diff --git a/R-package/DESCRIPTION b/R-package/DESCRIPTION index 0d7eea0..f3bea8c 100644 --- a/R-package/DESCRIPTION +++ b/R-package/DESCRIPTION @@ -2,12 +2,11 @@ Package: statgen Type: Package Title: Statistical Genetics Data Objects and Loaders Version: 0.3.0 -Authors@R: person("Oleksandr", "Frei", role = c("aut", "cre"), +Authors@R: person("Oleksandr", "Frei", role = c("aut", "cre", "cph"), email = "oleksandr.frei@gmail.com") -Author: Oleksandr Frei [aut, cre] -Maintainer: Oleksandr Frei -Description: CRAN-compatible R runtime for statgen statistical genetics - reference, genotype, LD, annotation, and summary-statistic objects. +Description: Loads and manages statistical genetics data objects including reference + panels, genotypes, LD matrices, annotations, and summary statistics. Implements the + language-agnostic statgen specifications for coordinated multi-runtime analysis. License: MIT + file LICENSE URL: https://github.com/precimed/statgen BugReports: https://github.com/precimed/statgen/issues diff --git a/R-package/LICENSE.md b/R-package/LICENSE.md new file mode 100644 index 0000000..601b9cf --- /dev/null +++ b/R-package/LICENSE.md @@ -0,0 +1,9 @@ +# MIT License + +Copyright (c) 2026 Oleksandr Frei and contributors + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/R-package/NEWS.md b/R-package/NEWS.md new file mode 100644 index 0000000..ae9b787 --- /dev/null +++ b/R-package/NEWS.md @@ -0,0 +1,3 @@ +# statgen 0.3.0 + +* Initial CRAN submission. diff --git a/R-package/R/annotations.R b/R-package/R/annotations.R index 0783dc0..f654c83 100644 --- a/R-package/R/annotations.R +++ b/R-package/R/annotations.R @@ -334,39 +334,48 @@ print.AnnotationPanel <- function(x, ...) { } .parse_bed <- function(path) { + # First pass: scan line-by-line to validate structure and count header lines to + # skip. Avoids seek(), which R documents as unreliable on Windows text connections. con <- file(path, open = "rt") on.exit(close(con), add = TRUE) + + skip_count <- 0L first_data <- NULL - first_pos <- 0 + found_data <- FALSE + repeat { - pos <- seek(con, where = NA) line <- readLines(con, n = 1L, warn = FALSE) if (!length(line)) { - stop(sprintf("%s: BED file is empty", path), call. = FALSE) - } - if (!identical(line, "") && !startsWith(line, "#")) { - first_data <- line - first_pos <- pos + if (!found_data) { + stop(sprintf("%s: BED file is empty", path), call. = FALSE) + } break } + if (!found_data) { + if (identical(line, "") || startsWith(line, "#")) { + skip_count <- skip_count + 1L + } else { + first_data <- line + found_data <- TRUE + } + } else { + if (identical(line, "") || startsWith(line, "#")) { + stop(sprintf("%s: blank or comment line after BED data row", path), call. = FALSE) + } + } } + close(con) + on.exit(NULL) + if (length(strsplit(first_data, "\t", fixed = TRUE)[[1L]]) < 3L) { stop(sprintf("%s: BED must have at least 3 tab-separated columns", path), call. = FALSE) } - seek(con, where = first_pos) - repeat { - line <- readLines(con, n = 1L, warn = FALSE) - if (!length(line)) { - break - } - if (identical(line, "") || startsWith(line, "#")) { - stop(sprintf("%s: blank or comment line after BED data row", path), call. = FALSE) - } - } - seek(con, where = first_pos) + + # Second pass: read.table opens the file fresh, no seek() needed. df <- tryCatch( utils::read.table( - file = con, + file = path, + skip = skip_count, header = FALSE, sep = "\t", quote = "", diff --git a/R-package/README.md b/R-package/README.md new file mode 100644 index 0000000..1189212 --- /dev/null +++ b/R-package/README.md @@ -0,0 +1,30 @@ +# statgen R Package + +Statistical genetics data objects and loaders for R. This package is part of the larger `statgen` repository, which also includes Python and MATLAB/Octave implementations. + +## Installation + +### From GitHub (development) + +```r +# Install devtools if needed +if (!require("devtools")) install.packages("devtools") + +# Install from the R-package directory +devtools::install() + +# Or from the repository root +devtools::install("R-package") +``` + +### From CRAN (release) + +```r +install.packages("statgen") +``` + +## Documentation + +- [Main statgen repository](https://github.com/precimed/statgen) +- Package vignette: `vignette("statgen")` + diff --git a/R-package/cran-comments.md b/R-package/cran-comments.md new file mode 100644 index 0000000..7c5285b --- /dev/null +++ b/R-package/cran-comments.md @@ -0,0 +1,9 @@ +## R CMD check results + +0 errors | 0 warnings | 1 note + +* This is a new release. + +* NOTE: "Possibly misspelled words in DESCRIPTION: LD, statgen" + Both are intentional: 'LD' is a standard abbreviation for Linkage Disequilibrium + in statistical genetics; 'statgen' is the name of this package and software suite. diff --git a/R-package/inst/extdata/anno1.bed b/R-package/inst/extdata/anno1.bed new file mode 100644 index 0000000..2f65ef7 --- /dev/null +++ b/R-package/inst/extdata/anno1.bed @@ -0,0 +1,5 @@ +1 99 150 +1 120 200 +1 299 350 +1 350 400 +1 499 500 diff --git a/R-package/inst/extdata/anno2.bed b/R-package/inst/extdata/anno2.bed new file mode 100644 index 0000000..6f43168 --- /dev/null +++ b/R-package/inst/extdata/anno2.bed @@ -0,0 +1,3 @@ +X 99 100 +X 195 205 +X 198 302 diff --git a/R-package/inst/extdata/genotype_1.bed b/R-package/inst/extdata/genotype_1.bed new file mode 100644 index 0000000..e45ce38 Binary files /dev/null and b/R-package/inst/extdata/genotype_1.bed differ diff --git a/R-package/inst/extdata/genotype_1.bim b/R-package/inst/extdata/genotype_1.bim new file mode 100644 index 0000000..d3bc14a --- /dev/null +++ b/R-package/inst/extdata/genotype_1.bim @@ -0,0 +1,5 @@ +1 rs1001 0 100 A G +1 rs1002 0 200 C T +1 rs1003 0 300 A C +1 rs1004 0 400 G A +1 rs1005 0 500 T C diff --git a/R-package/inst/extdata/genotype_1.fam b/R-package/inst/extdata/genotype_1.fam new file mode 100644 index 0000000..0404210 --- /dev/null +++ b/R-package/inst/extdata/genotype_1.fam @@ -0,0 +1,4 @@ +FAM1 IND1 0 0 1 -9 +FAM1 IND2 0 0 2 -9 +FAM2 IND3 0 0 1 -9 +FAM2 IND4 0 0 2 -9 diff --git a/R-package/inst/extdata/genotype_X.bed b/R-package/inst/extdata/genotype_X.bed new file mode 100644 index 0000000..cca7f59 Binary files /dev/null and b/R-package/inst/extdata/genotype_X.bed differ diff --git a/R-package/inst/extdata/genotype_X.bim b/R-package/inst/extdata/genotype_X.bim new file mode 100644 index 0000000..a90c087 --- /dev/null +++ b/R-package/inst/extdata/genotype_X.bim @@ -0,0 +1,3 @@ +X rsX001 0 100 A G +X rsX002 0 200 C T +X rsX003 0 300 A C diff --git a/R-package/inst/extdata/genotype_X.fam b/R-package/inst/extdata/genotype_X.fam new file mode 100644 index 0000000..0404210 --- /dev/null +++ b/R-package/inst/extdata/genotype_X.fam @@ -0,0 +1,4 @@ +FAM1 IND1 0 0 1 -9 +FAM1 IND2 0 0 2 -9 +FAM2 IND3 0 0 1 -9 +FAM2 IND4 0 0 2 -9 diff --git a/R-package/inst/extdata/genotype_X.ploidy b/R-package/inst/extdata/genotype_X.ploidy new file mode 100644 index 0000000..37e32ce --- /dev/null +++ b/R-package/inst/extdata/genotype_X.ploidy @@ -0,0 +1,3 @@ +1 2 +1 2 +1 2 diff --git a/R-package/inst/extdata/reference_chr1.bim b/R-package/inst/extdata/reference_chr1.bim new file mode 100644 index 0000000..d3bc14a --- /dev/null +++ b/R-package/inst/extdata/reference_chr1.bim @@ -0,0 +1,5 @@ +1 rs1001 0 100 A G +1 rs1002 0 200 C T +1 rs1003 0 300 A C +1 rs1004 0 400 G A +1 rs1005 0 500 T C diff --git a/R-package/inst/extdata/reference_chrX.bim b/R-package/inst/extdata/reference_chrX.bim new file mode 100644 index 0000000..a90c087 --- /dev/null +++ b/R-package/inst/extdata/reference_chrX.bim @@ -0,0 +1,3 @@ +X rsX001 0 100 A G +X rsX002 0 200 C T +X rsX003 0 300 A C diff --git a/R-package/inst/extdata/traits.tsv.gz b/R-package/inst/extdata/traits.tsv.gz new file mode 100644 index 0000000..5ae5829 Binary files /dev/null and b/R-package/inst/extdata/traits.tsv.gz differ diff --git a/R-package/man/ld.Rd b/R-package/man/ld.Rd index f7dfed6..30a5969 100644 --- a/R-package/man/ld.Rd +++ b/R-package/man/ld.Rd @@ -68,4 +68,23 @@ a vector or matrix with the same shape as \code{M}, and \code{fast_prune()} returns a numeric vector or one-dimensional matrix with pruned entries set to \code{NaN}. } +\examples{ +ld_src <- system.file("extdata", "ld", "python", package = "statgen") +if (nzchar(ld_src)) { + ld_root <- tempfile("ld_npz_") + dir.create(ld_root) + file.copy(list.files(ld_src, full.names = TRUE), ld_root, recursive = TRUE) + + prepare_ld_npz_for_r(ld_root) + ld <- load_ld(ld_root) + + num_snp(ld) + head(a1freq(ld)) + + scores <- seq_len(num_snp(ld)) + multiply_r2(ld, scores) + + fast_prune(c(9, 9, 7, 1, 2, 6, 5, 4), ld, r2_threshold = 0.35) +} +} \keyword{datasets}