Skip to content

Add opt-in BV-BRC Data API download - #46

Open
eboyer221 wants to merge 8 commits into
mainfrom
bvbrc-api-integration
Open

Add opt-in BV-BRC Data API download#46
eboyer221 wants to merge 8 commits into
mainfrom
bvbrc-api-integration

Conversation

@eboyer221

@eboyer221 eboyer221 commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

Adds an opt-in API download path for BV-BRC data (fixes the stochastic download failures, issue #30)

📄 Full background, benchmarks, and the data behind this: [docs/bvbrc-api-feasibility.md]

What this does
Adds a native-R way to download genome metadata and antibiotic-resistance (AMR) data straight from the BV-BRC web API, as an alternative to the current Docker/CLI path. You turn it on with retrieveMetadata(method = "api"); the default stays "docker", so nothing changes unless you opt in.

Why
The current download path shells out to Docker and can't tell an error apart from real data, so an occasional BV-BRC server hiccup corrupts a batch and stops the run (#30). It also silently stops at 25,000 records, which is far less than many species have. The API path fixes both:

Retries on transient server errors instead of crashing.
Pages through results to fetch the complete set, past the 25,000 limit.
Stays pure R (no Python/reticulate), which keeps things clean for Bioconductor.
It's a drop-in: the API path produces the same database tables as before, so everything downstream works unchanged.

What's validated:

  • End-to-end run with method = "api" (Docker off) produces all the expected tables, correctly populated.
  • Pulls a full result set well past the 25k limit.
  • Tests pass (offline helpers + live API checks).

Still needs testing before this leaves draft:

  • Side-by-side check vs. the Docker output on the same species.
  • A larger run so the batching and paging get exercised at real scale.
  • The antibiotic-filter and microTraits paths, plus edge cases.

Scope / not included
retrieveMetadata(method="api") is now fully Docker-free (ID resolution + metadata + AMR). Only genome sequence files (.fna/.faa/.gff, in retrieveGenomes) still use Docker which should be addressed in a separate follow-up.

Adds .resolveGenomeIDs_api(): resolves species names / taxon IDs to
Good-quality WGS/Complete genome IDs and writes bac_data, mirroring
.retrieveQueryIDs(). retrieveMetadata(method='api') now needs no
genome_id_file and no Docker end-to-end. Adds a resolver test.
# Conflicts:
#	DESCRIPTION
#	R/data_curation.R
#	man/retrieveMetadata.Rd
@eboyer221
eboyer221 marked this pull request as ready for review August 19, 2026 21:52
Integrating @eboyer221's excellent API download alternative to subsequent functions, and making API download path the default. Minor tweaks to variable naming (introducing metadata_method parameter for "api" vs. "cli"), moving parallelism block so only kick in for CLI branch, boosted the chunk_size for downloads to 500 (works faster in my testing without erroring, but may not be the best final number).

Co-Authored-By: Emily Boyer <130874527+eboyer221@users.noreply.github.com>

@epbrenner epbrenner left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've now added my own commit to this so I won't approve my own tweaks, but Emily's test code worked very, very well and is a great feature addition. Some of the dev test materials can be removed before merging, along with potentially removing bvbrcData.duckdb from the repo considering the API branch is just so much nicer to use. Recommending we make API the default, keep CLI as an option, and get this into main soon!

Emily, can you make sure none of these introduce breaking changes?

Co-Authored-By: Emily Boyer <130874527+eboyer221@users.noreply.github.com>
@jananiravi
jananiravi force-pushed the bvbrc-api-integration branch from fdbb983 to 2a83847 Compare August 21, 2026 02:28

@jananiravi jananiravi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work, Emily! Thanks for the quick and efficient implementation.

few quick points:

  • The PR description says "the default stays docker, so nothing changes unless you opt in" — that's no longer accurate now that metadata_method defaults to "api" (and what Evan posted). Can you update it for reviewers, calling attention to the changed default?
  • Also, now that the API path doesn't need it, can we drop data/bvbrc/bvbrcData.duckdb (non-trivial size)? R CMD check already flags data/ for containing non-R-data files — this is one of them. Worth removing here or in a quick follow-up before we submit.
  • Finally, the calls in bvbrc_api_prototype.R: these three scripts (.bvbrc_req/.bvbrc_page/.enc/.keyset_walk here, plus pull_genome_metadata/run_pipeline in bvbrc_pipeline_prototype.R, bvbrc_species_row/bvbrc_roster in bvbrc_species_roster.R) are superseded by the real implementation in R/bvbrc_api.R -- I don't think they are sourced by the package. Naming is snake_case throughout, inconsistent with the rest of the repo. Can we delete these rather than rename them, since you'd already flagged dev materials for cleanup before merge? [Evan may have pointed this out, too.]

Comment thread dev/bvbrc_api_prototype.R
# URL-encode ONE RQL value (BV-BRC wants field names/values encoded individually)
.enc <- function(x) utils::URLencode(as.character(x), reserved = TRUE)

.bvbrc_req <- function(collection, rql, accept = "application/json") {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changing function names to usual .lowerCamelCase.

Comment thread R/bvbrc_api.R
Comment on lines +150 to +153
key_filter <- if (grepl("^[0-9]+$", ub)) {
sprintf("eq(taxon_lineage_ids,%s)", ub) # taxon ID (any rank)
} else {
sprintf("eq(species,%s)", .bvbrcEnc(ub)) # species name

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check semantics against the CLI path here: species names now match exactly (eq(species, ub)) vs. the CLI's case-insensitive substring match, and numeric taxon IDs match taxon_lineage_ids — any rank in the lineage — vs. the CLI's exact match on the genome's own taxon_id. metadata_method="api" and "cli" can silently return different genome sets for the same input. The zero-match case now warns (see below), but can we align the two paths, or is this an intentional change we should document in ?retrieveMetadata?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants