Skip to content

Commit aea5043

Browse files
committed
Add 'source_set' argument to download_OGRDB_germline_sequences()
1 parent 5ad6748 commit aea5043

23 files changed

Lines changed: 186 additions & 447 deletions

File tree

‎R/OGRDB-utils.R‎

Lines changed: 2 additions & 252 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,8 @@ download_OGRDB_germline_set <-
215215
vapply(query, .encode_OGRDB_URL_component, character(1))
216216
}
217217

218+
219+
### No longer used. Superseded by download_OGRDB_germline_set() above.
218220
### Retrieves:
219221
### /germline/set/
220222
### {species}/{set_name}/{release_version}/{format}
@@ -314,255 +316,3 @@ download_OGRDB_germline_set <-
314316
ans # named list
315317
}
316318

317-
318-
### - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
319-
### download_germline_sequences_from_OGRDB()
320-
###
321-
322-
.infer_region_type_from_seqid <- function(seqid, locus)
323-
{
324-
stopifnot(is.character(seqid), isSingleNonWhiteString(locus),
325-
all(has_prefix(seqid, locus)))
326-
pos <- nchar(locus) + 1L
327-
region_type <- substr(seqid, pos, pos)
328-
stopifnot(all(region_type %in% VDJ_REGION_TYPES))
329-
region_type
330-
}
331-
332-
.check_ungapped_seqs <- function(ungapped_seqs,
333-
species, species_subgroup=NULL, set_name,
334-
release_version="published",
335-
extended=FALSE)
336-
{
337-
format <- if (extended) "airr_ex" else "airr"
338-
parsed_json <- .fetch_germline_set_from_OGRDB(species,
339-
species_subgroup=species_subgroup,
340-
set_name=set_name,
341-
release_version=release_version,
342-
format=format)
343-
stop("paranoid.mode not ready yet")
344-
}
345-
346-
### Downloads the germline sequences (ungapped) for the specified
347-
### species/species_subgroup/set_name/release_version.
348-
### Produces a subset of the following 7 FASTA files: IGHV.fasta,
349-
### IGHD.fasta, IGHJ.fasta, IGKV.fasta, IGKJ.fasta, IGLV.fasta, and
350-
### IGLJ.fasta. The exact subset produced depends on the species/set_name.
351-
### Returns the number of files produced.
352-
### NOTE: Superseded by download_OGRDB_germline_sequences()!
353-
download_germline_sequences_from_OGRDB <-
354-
function(species, species_subgroup=NULL, set_name,
355-
locus=c("IGH", "IGK", "IGL"),
356-
release_version="published",
357-
extended=FALSE, destdir=".",
358-
paranoid.mode=FALSE)
359-
{
360-
stopifnot(isSingleNonWhiteString(release_version),
361-
isTRUEorFALSE(extended),
362-
isSingleNonWhiteString(destdir),
363-
isTRUEorFALSE(paranoid.mode))
364-
locus <- match.arg(locus)
365-
366-
format <- if (extended) "ungapped_ex" else "ungapped"
367-
fasta_lines <- .fetch_germline_set_from_OGRDB(species,
368-
species_subgroup=species_subgroup,
369-
set_name=set_name,
370-
release_version=release_version,
371-
format=format)
372-
filepath <- tempfile(fileext=".fasta")
373-
writeLines(fasta_lines, filepath)
374-
ungapped_seqs <- readDNAStringSet(filepath)
375-
seq_region_types <- .infer_region_type_from_seqid(names(ungapped_seqs),
376-
locus)
377-
378-
if (paranoid.mode)
379-
.check_ungapped_seqs(ungapped_seqs,
380-
species, species_subgroup=species_subgroup,
381-
set_name=set_name,
382-
release_version=release_version,
383-
extended=extended)
384-
385-
from <- paste0("germline set ", set_name, " (", species, ")")
386-
file_count <- 0L
387-
for (region_type in VDJ_REGION_TYPES) {
388-
selected_seqs <- ungapped_seqs[seq_region_types == region_type]
389-
if (length(selected_seqs) == 0L)
390-
next
391-
filename <- paste0(locus, region_type, ".fasta")
392-
destfile <- file.path(destdir, filename)
393-
message("Write ", length(selected_seqs), " ", region_type, " regions ",
394-
"from ", from, " to ", filename, " ... ", appendLF=FALSE)
395-
if (file.exists(destfile))
396-
stop(wmsg(filename, " file already exists in ", destdir, "/"))
397-
writeXStringSet(selected_seqs, destfile)
398-
message("ok")
399-
file_count <- file_count + 1L
400-
}
401-
file_count
402-
}
403-
404-
405-
### - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
406-
### download_human_germline_sequences_from_OGRDB()
407-
###
408-
409-
### Downloads the germline sequences (ungapped) for all the Human germline
410-
### sets. See list of Human germline sets here:
411-
### https://ogrdb.airr-community.org/germline_sets/Homo%20sapiens
412-
### Returns the number of files produced (should be 7).
413-
###
414-
### NOTE: No longer needed! Use download_OGRDB_germline_sequences() instead.
415-
download_human_germline_sequences_from_OGRDB <-
416-
function(release_version="published",
417-
extended=FALSE, destdir=".",
418-
paranoid.mode=FALSE)
419-
{
420-
stopifnot(isSingleNonWhiteString(release_version),
421-
isTRUEorFALSE(extended),
422-
isSingleNonWhiteString(destdir),
423-
isTRUEorFALSE(paranoid.mode))
424-
425-
## Produces files: IGHV.fasta, IGHD.fasta, IGHJ.fasta
426-
file_count <-
427-
download_germline_sequences_from_OGRDB("Human",
428-
set_name="IGH_VDJ", locus="IGH",
429-
release_version=release_version, extended=extended,
430-
destdir=destdir, paranoid.mode=paranoid.mode)
431-
## Produces files: IGKV.fasta, IGKJ.fasta
432-
file_count <- file_count +
433-
download_germline_sequences_from_OGRDB("Human",
434-
set_name="IGKappa_VJ", locus="IGK",
435-
release_version=release_version, extended=extended,
436-
destdir=destdir, paranoid.mode=paranoid.mode)
437-
## Produces files: IGLV.fasta, IGLJ.fasta
438-
file_count <- file_count +
439-
download_germline_sequences_from_OGRDB("Human",
440-
set_name="IGLambda_VJ", locus="IGL",
441-
release_version=release_version, extended=extended,
442-
destdir=destdir, paranoid.mode=paranoid.mode)
443-
file_count
444-
}
445-
446-
447-
### - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
448-
### download_mouse_germline_sequences_from_OGRDB()
449-
###
450-
451-
### 'set_names' is expected to be a vector of Set Names from
452-
### the Germline Sets table displayed at
453-
### https://ogrdb.airr-community.org/germline_sets/Mus%20musculus
454-
### except special Set Names "IGKJ (all strains)" and "IGLJ (all strains)".
455-
### Assumes that all the Set Names in 'set_names' are of the
456-
### form "<Species subgroup> <group>", where <group> is
457-
### one of IGH, IGKV, an IGLV.
458-
### Returns a 4-column matrix with colum names: set_name, species_subgroup,
459-
### group, and locus.
460-
.set_names_as_matrix <- function(set_names)
461-
{
462-
if (!is.character(set_names) || anyNA(set_names))
463-
stop(wmsg("'set_names' must be a character vector with no NAs"))
464-
set_names <- trimws2(set_names)
465-
if (anyDuplicated(set_names))
466-
stop(wmsg("'set_names' cannot contain duplicates"))
467-
FORBIDDEN_SET_NAMES <- c("IGKJ (all strains)", "IGLJ (all strains)")
468-
if (any(tolower(set_names) %in% tolower(FORBIDDEN_SET_NAMES))) {
469-
in1string <- paste0('"', FORBIDDEN_SET_NAMES, '"', collapse=" or ")
470-
stop(wmsg("'set_names' cannot contain ", in1string))
471-
}
472-
split_set_names <- strsplit(set_names, split=" ", fixed=TRUE)
473-
if (!all(lengths(split_set_names) == 2L))
474-
stop(wmsg("each Set Name in 'set_names' must contain exactly 1 space"))
475-
data <- unlist(split_set_names)
476-
if (is.null(data))
477-
data <- character(0)
478-
m <- matrix(data, ncol=2L, byrow=TRUE,
479-
dimnames=list(NULL, c("species_subgroup", "group")))
480-
loci <- substr(m[ , "group"], 1L, 3L)
481-
bad_idx <- which(!(loci %in% c("IGH", "IGK", "IGL")))
482-
if (length(bad_idx) != 0L) {
483-
in1string <- paste(set_names[bad_idx], collapse=", ")
484-
stop(wmsg("Bad Set Name(s): ", in1string, "."),
485-
"\n ",
486-
wmsg("The first 3 letters of the 2nd part of the name ",
487-
"is not a valid locus name."))
488-
}
489-
set_name <- matrix(set_names, dimnames=list(NULL, "set_name"))
490-
locus <- matrix(loci, dimnames=list(NULL, "locus"))
491-
ans <- cbind(set_name, m, locus)
492-
species_subgroup <- unique(ans[ , "species_subgroup"])
493-
if (length(species_subgroup) > 1L)
494-
warning(wmsg("the supplied Set Names are from ",
495-
"more than one Species subgroup"))
496-
ans
497-
}
498-
499-
### Downloads the germline sequences (ungapped) for the specified Mouse
500-
### germline sets. See list of Mouse germline sets here:
501-
### https://ogrdb.airr-community.org/germline_sets/Mus%20musculus
502-
### Returns the number of files produced (will be typically between 4 and 7).
503-
###
504-
### To download all the germline sequences for Mouse strain A/J:
505-
###
506-
### set_names <- c("A/J IGKV", "A/J IGLV")
507-
### download_mouse_germline_sequences_from_OGRDB(set_names)
508-
###
509-
### --> produces 4 FASTA files (partial germline db).
510-
###
511-
### To download all the germline sequences for Mouse strain C57BL/6:
512-
###
513-
### download_mouse_germline_sequences_from_OGRDB("C57BL/6 IGH")
514-
###
515-
### --> produces 5 FASTA files (partial germline db).
516-
###
517-
### To download all the germline sequences for Mouse strain C57BL/6J:
518-
###
519-
### set_names <- c("C57BL/6J IGKV", "C57BL/6J IGLV")
520-
### download_mouse_germline_sequences_from_OGRDB(set_names)
521-
###
522-
### --> produces 4 FASTA files (partial germline db).
523-
###
524-
### To download all the germline sequences for Mouse strain CAST/EiJ:
525-
###
526-
### set_names <- c("CAST/EiJ IGH", "CAST/EiJ IGKV", "CAST/EiJ IGLV")
527-
### download_mouse_germline_sequences_from_OGRDB(set_names)
528-
###
529-
### --> produces 7 FASTA files (full germline db).
530-
###
531-
### NOTE: No longer needed! Use download_OGRDB_germline_sequences() instead.
532-
download_mouse_germline_sequences_from_OGRDB <-
533-
function(set_names,
534-
release_version="published",
535-
extended=FALSE, destdir=".",
536-
paranoid.mode=FALSE)
537-
{
538-
stopifnot(isSingleNonWhiteString(release_version),
539-
isTRUEorFALSE(extended),
540-
isSingleNonWhiteString(destdir),
541-
isTRUEorFALSE(paranoid.mode))
542-
543-
m <- .set_names_as_matrix(set_names)
544-
set_names <- m[ , "set_name"]
545-
species_subgroups <- m[ , "species_subgroup"]
546-
loci <- m[ , "locus"]
547-
file_count <- 0L
548-
for (i in seq_len(nrow(m))) {
549-
file_count <- file_count +
550-
download_germline_sequences_from_OGRDB("Mouse",
551-
species_subgroup=species_subgroups[[i]],
552-
set_name=set_names[[i]], locus=loci[[i]],
553-
release_version=release_version, extended=extended,
554-
destdir=destdir, paranoid.mode=paranoid.mode)
555-
}
556-
file_count <- file_count +
557-
download_germline_sequences_from_OGRDB("Mouse",
558-
set_name="IGKJ (all strains)", locus="IGK",
559-
release_version=release_version, extended=extended,
560-
destdir=destdir, paranoid.mode=paranoid.mode)
561-
file_count <- file_count +
562-
download_germline_sequences_from_OGRDB("Mouse",
563-
set_name="IGLJ (all strains)", locus="IGL",
564-
release_version=release_version, extended=extended,
565-
destdir=destdir, paranoid.mode=paranoid.mode)
566-
file_count
567-
}
568-

‎R/download_OGRDB_germline_sequences.R‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,9 @@
159159
### Returns the list of FASTA files that were produced in an invisible
160160
### character vector that carries the names of the corresponding germline
161161
### sets.
162-
### TODO: Add 'source_set' argument like in download_V_ndm_data_from_OGRDB()
163-
### below.
164162
download_OGRDB_germline_sequences <- function(organism="Homo sapiens",
165163
germline_sets, gapped=TRUE,
164+
source_set=FALSE,
166165
destdir=".", overwrite=FALSE,
167166
recache=FALSE, ...)
168167
{
@@ -172,6 +171,8 @@ download_OGRDB_germline_sequences <- function(organism="Homo sapiens",
172171
set_loci <- .infer_loci_from_OGRDB_set_names(set_names)
173172
if (!isTRUEorFALSE(gapped))
174173
stop(wmsg("'gapped' must be TRUE or FALSE"))
174+
if (!isTRUEorFALSE(source_set))
175+
stop(wmsg("'source_set' must be TRUE or FALSE"))
175176
if (!isSingleNonWhiteString(destdir))
176177
stop(wmsg("'destdir' must be a single (non-empty) string"))
177178
if (!dir.exists(destdir)) {
@@ -198,7 +199,8 @@ download_OGRDB_germline_sequences <- function(organism="Homo sapiens",
198199
## already there.
199200
local_file <- .download_OGRDB_germline_set_to_OGRDB_store(organism,
200201
set_name, set_version,
201-
format=format, recache=recache, ...)
202+
format=format, source_set=source_set,
203+
recache=recache, ...)
202204
.split_OGRDB_fasta_file(local_file, set_name, set_locus,
203205
destdir=tmp_destdir)
204206
}

‎TODO‎

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -197,30 +197,15 @@ o Great resource for how to use AIRR Community Reference germline sets with
197197
In particular, the author seems to be using an OGRDB REST API version 2:
198198
https://ogrdb.airr-community.org/api_v2
199199
but where is this API documented?
200-
All the download utilities implemented in igblastr/R/OGRDB-utils.R use
201-
the OGRDB REST API at
200+
Note that internal utility igblastr:::.fetch_germline_set_from_OGRDB()
201+
(implemented in R/OGRDB-utils.R) uses the OGRDB REST API at
202202
https://ogrdb.airr-community.org/api
203-
which is poorly documented and is somewhat confusing (see below).
204-
205-
o Investigate the following mysteries about the germline sets provided
206-
by AIRR/OGRDB:
207-
208-
1. The OGRDB API at https://ogrdb.airr-community.org/api/ allows downloading
209-
the germline sequences in 2 formats: ungapped or ungapped_ex.
210-
Which format is appropriate to use with IgBLAST?
211-
Note that downloading germline sets directly by clicking on
212-
the "FASTA Ungapped" links here
213-
https://ogrdb.airr-community.org/germline_sets/Homo%20sapiens
214-
or
215-
https://ogrdb.airr-community.org/germline_sets/Mus%20musculus
216-
seems to retrieve the "ungapped_ex" sequences for Human and the "ungapped"
217-
sequences for Mouse. Confusing!
218-
219-
2. For some Mouse strains, OGRDB seems to provide germline sequences
220-
only for a limited number of loci/groups. For example for strain A/J,
221-
only sequences from the light chain (i.e. groups IGKV, IGKJ, IGLV,
222-
and IGLJ) seem to be available.
223-
See https://ogrdb.airr-community.org/germline_sets/Mus%20musculus
203+
which is poorly documented and is somewhat confusing.
204+
205+
o Why are some loci/groups missing for some mouse strains at OGRDB?
206+
For example for strain A/J, only sequences from the light chain (i.e.
207+
groups IGKV, IGKJ, IGLV, and IGLJ) seem to be available.
208+
See https://ogrdb.airr-community.org/germline_sets/Mus%20musculus
224209

225210
o Implement install_OGRDB_germline_db(). Will download the germline sequences
226211
from https://ogrdb.airr-community.org/ (link provided by Kellie).

‎inst/extdata/germline_sets/AIRR/human/202309/ref/README.md‎

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,8 @@ However, the FASTA files in this folder were obtained programmatically
2525
by running the following code in the folder on Oct 20, 2025:
2626
```r
2727
library(igblastr)
28-
download_germline_sequences_from_OGRDB <- igblastr:::download_germline_sequences_from_OGRDB
29-
30-
Hfile_count <- download_germline_sequences_from_OGRDB("Human", set_name="IGH_VDJ", locus="IGH", release_version="7", extended=TRUE)
31-
stopifnot(identical(Hfile_count, 3L))
32-
33-
Kfile_count <- download_germline_sequences_from_OGRDB("Human", set_name="IGKappa_VJ", locus="IGK", release_version="2", extended=TRUE)
34-
stopifnot(identical(Kfile_count, 2L))
35-
36-
Jfile_count <- download_germline_sequences_from_OGRDB("Human", set_name="IGLambda_VJ", locus="IGL", release_version="1", extended=TRUE)
37-
stopifnot(identical(Jfile_count, 2L))
28+
germline_sets <- c(IGH_VDJ=7, IGKappa_VJ=2, IGLambda_VJ=1)
29+
download_OGRDB_germline_sequences("Homo sapiens", germline_sets, gapped=FALSE)
3830
```
3931

4032
Files `*.ndm.imgt` were obtained programmatically by running the

‎inst/extdata/germline_sets/AIRR/human/202309/src/README.md‎

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,7 @@ However, the FASTA files in this folder were obtained programmatically
2222
by running the following code in the folder on Oct 20, 2025:
2323
```r
2424
library(igblastr)
25-
download_germline_sequences_from_OGRDB <- igblastr:::download_germline_sequences_from_OGRDB
26-
27-
Hfile_count <- download_germline_sequences_from_OGRDB("Human", set_name="IGH_VDJ", locus="IGH", release_version="7")
28-
stopifnot(identical(Hfile_count, 3L))
29-
30-
Kfile_count <- download_germline_sequences_from_OGRDB("Human", set_name="IGKappa_VJ", locus="IGK", release_version="2")
31-
stopifnot(identical(Kfile_count, 2L))
32-
33-
Jfile_count <- download_germline_sequences_from_OGRDB("Human", set_name="IGLambda_VJ", locus="IGL", release_version="1")
34-
stopifnot(identical(Jfile_count, 2L))
25+
germline_sets <- c(IGH_VDJ=7, IGKappa_VJ=2, IGLambda_VJ=1)
26+
download_OGRDB_germline_sequences("Homo sapiens", germline_sets, gapped=FALSE, source_set=TRUE)
3527
```
3628

0 commit comments

Comments
 (0)