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
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
^.*\.Rproj$
^\.Rproj\.user$
^README\.Rmd$
^CONTRIBUTING\.md$
^LICENSE\.md$
^pkgdown$
^_pkgdown\.yml$
Expand Down
7 changes: 6 additions & 1 deletion .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ jobs:
config:
- {os: macos-latest, r: 'release'}
- {os: windows-latest, r: 'release'}
- {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'}
# Pinned to 24.04: ubuntu-latest moved to 26.04 (resolute) on
# 2026-10-26, and RSPM does not yet serve R-devel binaries for
# duckdb there, forcing a ~1h source build that fails to install.
# R-release/oldrel-1 binaries on resolute are fine; revisit the
# pin once RSPM's resolute R-devel coverage matures.
- {os: ubuntu-24.04, r: 'devel', http-user-agent: 'release'}
- {os: ubuntu-latest, r: 'release'}
- {os: ubuntu-latest, r: 'oldrel-1'}

Expand Down
34 changes: 34 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Contributing to geokit

Thanks for considering a contribution!

## Development setup

geokit uses a Rust backend for URL resolution and SOFT parsing. To build from
source you need the Rust toolchain (`cargo` and `rustc`, see the
`SystemRequirements` field in DESCRIPTION) in addition to R.

```sh
git clone https://github.com/WangLabCSU/geokit.git
Rscript -e 'pkgload::load_all(".")'
```

## Pull requests

- Work on a feature branch and keep pull requests focused on one change.
- Add or update tests in `tests/testthat/` for behavior changes. Tests that
hit NCBI should use `skip_if_download_fail()` (download layer) so they skip
gracefully when the network is unavailable.
- Keep roxygen documentation in `R/*.R` in sync with the code and regenerate
the `man/` pages with `roxygen2::roxygenise()` when docs change.
- Record user-visible changes in `NEWS.md` under the development-version
section.
- CI runs `R CMD check` on Linux, macOS, and Windows plus test coverage;
please keep it green.

## Reporting issues

Include your R version, geokit version (`packageVersion("geokit")`), and a
minimal reproducible example. For download problems, mention whether an NCBI
API key (`ENTREZ_KEY`) is set and whether your environment can reach
`ftp.ncbi.nlm.nih.gov`.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ Imports:
data.table (>= 1.14.9),
curl (>= 6.0.0),
rentrez,
stats,
xml2,
utils
Suggests:
BiocGenerics,
Biobase,
stats,
dplyr,
ggplot2,
stringr,
Expand All @@ -39,6 +39,6 @@ ByteCompile: true
Config/testthat/edition: 3
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 8.0.0
SystemRequirements: Cargo (Rust's package manager), rustc >= 1.87.0
VignetteBuilder: knitr
Config/roxygen2/version: 8.1.1
18 changes: 10 additions & 8 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,14 @@ exportMethods(rcd_name)
exportMethods(rcd_type)
exportMethods(show)
importFrom(methods,show)
importFrom(rlang,abort)
importFrom(rlang,caller_arg)
importFrom(rlang,caller_env)
importFrom(rlang,ffi_standalone_check_number_1.0.7)
importFrom(rlang,ffi_standalone_is_bool_1.0.7)
importFrom(rlang,is_missing)
importFrom(rlang,is_string)
importFrom(rlang,is_vector)
importFrom(rlang,
abort,
caller_arg,
caller_env,
ffi_standalone_check_number_1.0.7,
ffi_standalone_is_bool_1.0.7,
is_missing,
is_string,
is_vector
)
useDynLib(geokit, .registration = TRUE)
20 changes: 17 additions & 3 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
# geokit (development version)

* Added a public Agent Skills file for AI agents, published once with the
pkgdown site at
<https://WangLabCSU.github.io/geokit/skills/geokit/SKILL.md>.
* Added three vignettes: case studies for diabetic nephropathy and
urothelial cancer meta-analysis workflows, and a `geo_chat()` guide for
LLM-assisted exploration of GEO metadata.
* Added a public Agent Skills file for AI agents, published with the pkgdown
site at <https://wanglabcsu.github.io/geokit/skills/geokit/SKILL.md>.
* Fixed `parse_sample_data()` ignoring its `sep` argument when parsing
characteristic key-value pairs, silently skipping non-contiguous
`characteristics_ch` columns, and crashing on custom `fields` names
without a `ch<N>` suffix (they now prefix parsed columns with the field
name).
* Fixed plain-FTP downloads (`ftp_over_https = FALSE`) being deleted and
reported as failures after a success-detection regression.
* Fixed a guaranteed crash in `geo_matrix()` when a platform annotation
download fails, and clarified the error when sample metadata lacks
`platform_id`.
* CI resolves `querychat` from the posit-dev R-universe after its CRAN
archive; workflows and the skill file keep working unchanged.

# geokit 0.0.2

Expand Down
11 changes: 10 additions & 1 deletion R/class.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
#'
#' @param object A [GEOSoft][GEOSoft-class] Class Object.
#' @param value A R object with the same class of corresponding slots.
#' @return
#' The accessor generics return the value of the corresponding slot:
#' `accession()`, `rcd_name()`, `rcd_type()` and `metadata()` return the
#' record accession, name, type, and header metadata list; `columns()` and
#' `datatable()` return the column descriptions and the data table as
#' data.frames; `gsm()`, `gse()` and `gpl()` return lists of related
#' `GEOSoft` objects where applicable. The replacement forms return the
#' modified object.
#' @name GEOSoft-class
#' @docType class
#' @keywords internal
Expand Down Expand Up @@ -75,6 +83,7 @@ methods::setValidity("GEOSoft", function(object) {
methods::setMethod("show", "GEOSoft", function(object) print(object))

#' @export
#' @rdname GEOSoft-class
print.GEOSoft <- function(x, ...) {
soft_print_header(x, ...)
soft_print_data(x, ...)
Expand Down Expand Up @@ -321,7 +330,7 @@ methods::setValidity("GEOPlatform", function(object) {
if (!all(vapply(object@gse, function(x) {
methods::is(x, "GEOSoft")
}, logical(1L), USE.NAMES = FALSE))) {
return("the element of slot @gpl list should only contain Class `GEOSoft` object.")
return("the element of slot @gse list should only contain Class `GEOSoft` object.")
}
TRUE
})
Expand Down
27 changes: 17 additions & 10 deletions R/download.R
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,18 @@ list_directory_url <- function(accession, format, ftp_over_https,
urls
}

#' Download utils function with good message.
#' @return If fail is `TRUE`, always return a character path if downloading
#' succeed, otherwise, stop with error message. If fail is `FALSE`, always
#' return a list.
#' Download files with informative messages.
#'
#' Download `urls` into `ofiles`, remove partially downloaded files on
#' failure, and optionally raise an error when any download fails.
#'
#' @param urls Character of URLs to download.
#' @param ofiles Character of local destination paths, aligned with `urls`.
#' @param handle_opts Options passed to [curl::multi_download()].
#' @param error If `TRUE` (default), raise `geokit_download_error` when any
#' download fails.
#' @return A list with elements `urls`, `paths`, and `success` (a logical
#' vector aligned with `urls`).
#' @noRd
download_inform <- function(urls, ofiles, handle_opts, error = TRUE) {
out <- list(
Expand Down Expand Up @@ -111,10 +119,9 @@ download_inform <- function(urls, ofiles, handle_opts, error = TRUE) {
urls = urls, destfiles = ofiles, resume = FALSE,
!!!handle_opts
))
# FTP: 200L, 206L, 416L, 226L
# ADB: 200L, 206L, 416L
# success codes: HTTP 200/206/416 and FTP 226 (transfer complete)
success <- !is.na(status$success) & status$success &
status$status_code == 200L
status$status_code %in% c(200L, 206L, 416L, 226L)
removed <- !success & file.exists(ofiles)
if (any(removed)) file.remove(ofiles[removed])
if (!all(success) && error) {
Expand All @@ -132,10 +139,10 @@ download_inform <- function(urls, ofiles, handle_opts, error = TRUE) {
}

setup_handle_opts <- function(handle_opts) {
# the empty Expect header disables curl's default `Expect: 100-continue`,
# which otherwise stalls FTP transfers
handle_opts$httpheader <- handle_opts$httpheader %||%
utils::getFromNamespace("format_request_headers", "curl")(
list("User-Agent" = "geokit")
)
c("User-Agent: geokit", "Expect: ")
handle_opts$followlocation <- handle_opts$followlocation %||% TRUE
handle_opts$connecttimeout <- handle_opts$connecttimeout %||% 60L
# this is recommended by GEO FTP site
Expand Down
22 changes: 17 additions & 5 deletions R/geo-matrix.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ geo_matrix <- function(accession, add_gpl = NULL, pdata_from_soft = FALSE,
return_object_or_list(matrix_list, accession)
}

geo_matrix_impl <- function(accession, pdata_from_soft = TRUE,
add_gpl = NULL, ftp_over_https = TRUE,
geo_matrix_impl <- function(accession, pdata_from_soft = FALSE,
add_gpl = NULL, ftp_over_https = NULL,
handle_opts = list(), odir = getwd()) {
collected <- list(
ftp_over_https = ftp_over_https,
Expand Down Expand Up @@ -92,6 +92,8 @@ geo_matrix_impl <- function(accession, pdata_from_soft = TRUE,
parse_gse_matrix <- function(path, gse_soft = NULL, add_gpl = NULL,
ftp_over_https = TRUE, handle_opts = list(),
odir = getwd()) {
# series matrix files are named "<GSEID>_series_matrix.txt[.gz]"
accession <- sub("_series_matrix.*$", "", basename(path))
entity_list <- parse_soft_rust(path, "matrix")
gse_matrix <- parse_entity_list(.subset2(entity_list, 1L))

Expand Down Expand Up @@ -159,7 +161,17 @@ parse_gse_matrix <- function(path, gse_soft = NULL, add_gpl = NULL,
"platform_id", colnames(metadata$Sample),
ignore.case = TRUE
)
annotation <- .subset(.subset2(metadata$Sample, gpl_column[1L]), 1L)
annotation <- if (length(gpl_column)) {
.subset(.subset2(metadata$Sample, gpl_column[1L]), 1L)
} else {
NULL
}
if (is.null(annotation) || all(is.na(annotation))) {
cli::cli_abort(c(
"No {.field platform_id} found in the sample metadata of {.val {accession}}.",
"i" = "Cannot resolve platform annotation for the series matrix."
))
}

if (is.null(add_gpl) || !add_gpl) {
bioc_pkg <- gpl_map(annotation)$bioc_pkg
Expand Down Expand Up @@ -210,8 +222,8 @@ parse_gse_matrix <- function(path, gse_soft = NULL, add_gpl = NULL,
)
}
} else {
cli::cli_alert_info(paste(
"Failed to download annotation file for {.val {accession[!downloaded$success]}},",
cli::cli_alert_info(c(
"Failed to download annotation file for {.val {accession}},",
"Platform information will not be added."
))
}
Expand Down
13 changes: 8 additions & 5 deletions R/geo-search.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,22 @@
#' }
#' @export
geo_search <- function(query, step = 500L, interval = NULL) {
assert_string(query)
assert_number_whole(step, min = 1)
assert_number_decimal(interval, min = 0, allow_null = TRUE)
interval <- interval %||% 0
records_num <- rentrez::entrez_search("gds", query, retmax = 0L)$count
# one search call provides both the total count and the web history used
# by the paged fetches below
search_res <- rentrez::entrez_search(
"gds", query,
use_history = TRUE, retmax = 0L
)
records_num <- search_res$count
if (records_num == 0L) {
cli::cli_abort("No items found.")
}
retstarts <- seq.int(0L, records_num - 1L, by = step)
records <- character(length(retstarts))
search_res <- rentrez::entrez_search(
"gds", query,
use_history = TRUE, retmax = 0L
)
bar <- cli::cli_progress_bar(
name = "NCBI",
format = "{cli::pb_bar} {cli::pb_current}/{cli::pb_total} [{cli::pb_rate}] | {cli::pb_eta_str}",
Expand Down
4 changes: 3 additions & 1 deletion R/geo-soft.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
#' [`multi_download`][curl::multi_download].
#' @param odir Destination directory for downloads. Defaults to the current
#' working directory.
#' @return A [GEOSoft][GEOSoft-class] object
#' @return A [GEOSoft][GEOSoft-class] object (or a list of them when
#' multiple accessions are provided; subclasses `GEOPlatform` and
#' `GEOSeries` are returned for GPL and GSE accessions respectively).
#'
#' @details
#'
Expand Down
4 changes: 2 additions & 2 deletions R/geo-suppl.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
#' @param pattern character string containing a
#' [regular expression][base::regex] to be matched in the supplementary file
#' names.
#' @return A list (or a character atomic verctor if only one `accession` is
#' @return A list (or a character atomic vector if only one `accession` is
#' provided) of the full file paths of the resulting downloaded files.
#' @examples
#' \donttest{
#' geo_suppl("GSM1137", odir = tempdir())
#' }
#' @export
geo_suppl <- function(accession, pattern = NULL, ftp_over_https = TRUE,
geo_suppl <- function(accession, pattern = NULL, ftp_over_https = NULL,
handle_opts = list(), odir = getwd()) {
odir <- dir_create(odir, recursive = TRUE)
file_paths <- download_url_directory(
Expand Down
23 changes: 12 additions & 11 deletions R/phenodata.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ parse_sample_data.data.frame <- function(x, ..., fields = NULL, sep = ":") {
}, logical(1L), USE.NAMES = FALSE)
mean(have_sep, na.rm = TRUE) >= 0.5
}, logical(1L), USE.NAMES = FALSE)
fields <- colnames(x)[cols[column_with_sep]]
fields <- colnames(x)[which(cols)[column_with_sep]]
}
if (length(fields) == 0L) {
return(set_rownames(x, "geo_accession"))
Expand Down Expand Up @@ -121,20 +121,21 @@ parse_characteristics <- function(data, characteristic_cols, sep = ":") {
))
}
characteristics <- lapply(characteristic_cols, function(col) {
data <- parse_key_value_elements(.subset2(data, col), sep = ":")
data <- parse_key_value_elements(.subset2(data, col), sep = sep)
if (nrow(data) && ncol(data)) {
# we extract the last "ch\\d*" pattern as the column
# name, which is the first group defined by parentheses.
# This is just the second column of `str_match`.
# Sometimes there may be a "\\.\\d*" tail
new_names <- paste0(
str_match(col, "(ch\\d*)(\\.\\d*)?(_\\d*)?$")[
, 2L,
drop = TRUE
],
"_",
colnames(data)
)
# Sometimes there may be a "\\.\\d*" tail.
# Columns without a "ch\\d*" suffix (custom `fields`) have no
# match group; fall back to the column name itself as the prefix.
matched <- str_match(col, "(ch\\d*)(\\.\\d*)?(_\\d*)?$")
prefix <- if (ncol(matched) >= 2L) {
matched[, 2L, drop = TRUE]
} else {
col
}
new_names <- paste0(prefix, "_", colnames(data))
colnames(data) <- new_names
data
} else {
Expand Down
Loading
Loading