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
61 changes: 61 additions & 0 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
@@ -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
7 changes: 0 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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/

Expand Down
3 changes: 3 additions & 0 deletions R-package/.Rbuildignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
^cran-comments\.md$
^revdep$
^LICENSE\.md$
9 changes: 4 additions & 5 deletions R-package/DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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 <oleksandr.frei@gmail.com>
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
Expand Down
9 changes: 9 additions & 0 deletions R-package/LICENSE.md
Original file line number Diff line number Diff line change
@@ -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.
3 changes: 3 additions & 0 deletions R-package/NEWS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# statgen 0.3.0

* Initial CRAN submission.
47 changes: 28 additions & 19 deletions R-package/R/annotations.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "",
Expand Down
30 changes: 30 additions & 0 deletions R-package/README.md
Original file line number Diff line number Diff line change
@@ -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")`

9 changes: 9 additions & 0 deletions R-package/cran-comments.md
Original file line number Diff line number Diff line change
@@ -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.
5 changes: 5 additions & 0 deletions R-package/inst/extdata/anno1.bed
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
1 99 150
1 120 200
1 299 350
1 350 400
1 499 500
3 changes: 3 additions & 0 deletions R-package/inst/extdata/anno2.bed
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
X 99 100
X 195 205
X 198 302
Binary file added R-package/inst/extdata/genotype_1.bed
Binary file not shown.
5 changes: 5 additions & 0 deletions R-package/inst/extdata/genotype_1.bim
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions R-package/inst/extdata/genotype_1.fam
Original file line number Diff line number Diff line change
@@ -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
Binary file added R-package/inst/extdata/genotype_X.bed
Binary file not shown.
3 changes: 3 additions & 0 deletions R-package/inst/extdata/genotype_X.bim
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
X rsX001 0 100 A G
X rsX002 0 200 C T
X rsX003 0 300 A C
4 changes: 4 additions & 0 deletions R-package/inst/extdata/genotype_X.fam
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions R-package/inst/extdata/genotype_X.ploidy
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
1 2
1 2
1 2
5 changes: 5 additions & 0 deletions R-package/inst/extdata/reference_chr1.bim
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions R-package/inst/extdata/reference_chrX.bim
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
X rsX001 0 100 A G
X rsX002 0 200 C T
X rsX003 0 300 A C
Binary file added R-package/inst/extdata/traits.tsv.gz
Binary file not shown.
19 changes: 19 additions & 0 deletions R-package/man/ld.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Loading