Skip to content

Commit fdb8ace

Browse files
committed
Add 'loci' argument to install_IMGT_germline_db()
This lets the user control what loci they want to include in the germline db. Documentation and unit tests to follow.
1 parent 23e1333 commit fdb8ace

13 files changed

Lines changed: 450 additions & 214 deletions

File tree

‎DESCRIPTION‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Collate:
3232
utils.R
3333
long_to_wide_airr.R
3434
file-utils.R
35+
loci-utils.R
3536
db-utils.R
3637
LATIN_NAMES.R
3738
IMGT-utils.R

‎R/builtin_db-utils.R‎

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,33 @@
66
###
77

88

9+
.read_version_file <- function(dirpath)
10+
{
11+
stopifnot(isSingleNonWhiteString(dirpath))
12+
version_path <- file.path(dirpath, "version")
13+
if (!file.exists(version_path))
14+
stop(wmsg("missing 'version' file in ", dirpath, "/"))
15+
version <- readLines(version_path)
16+
if (length(version) != 1L)
17+
stop(wmsg("file '", version_path, "' should contain exactly one line"))
18+
version <- trimws2(version)
19+
if (version == "")
20+
stop(wmsg("file '", version_path, "' contains only white spaces"))
21+
version
22+
}
23+
24+
925
### - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1026
### .create_missing_builtin_AIRR_human_germline_dbs()
1127
###
1228

1329
.form_builtin_AIRR_human_germline_db_name <- function(fasta_dir)
1430
{
15-
loci <- get_loci_from_input_germline_fasta_set(fasta_dir)
31+
loci_in1string <- paste(IG_LOCI, collapse="+")
32+
version <- basename(dirname(fasta_dir))
33+
db_name <- sprintf("_AIRR.human.%s.%s", loci_in1string, version)
1634
flavor <- basename(fasta_dir)
1735
stopifnot(flavor %in% c("ref", "src"))
18-
version <- basename(dirname(fasta_dir))
19-
db_name <- sprintf("_AIRR.human.%s.%s", paste(loci, collapse="+"), version)
2036
if (flavor == "src")
2137
db_name <- paste0(db_name, ".src")
2238
db_name
@@ -30,18 +46,17 @@
3046
db_name <- .form_builtin_AIRR_human_germline_db_name(fasta_dir)
3147
db_path <- file.path(destdir, db_name)
3248
if (!(only.if.missing && dir.exists(db_path)))
33-
create_germline_db(fasta_dir, db_path)
49+
create_germline_db(fasta_dir, IG_LOCI, db_path)
3450
}
3551

3652
.create_missing_builtin_AIRR_human_germline_dbs <- function(human_dir, destdir)
3753
{
3854
stopifnot(isSingleNonWhiteString(human_dir), dir.exists(human_dir))
3955
human_subdirs <- list.dirs(human_dir, recursive=FALSE)
4056
fasta_dirs <- file.path(rep(human_subdirs, each=2L), c("ref", "src"))
41-
for (fasta_dir in fasta_dirs) {
57+
for (fasta_dir in fasta_dirs)
4258
.create_builtin_AIRR_human_germline_db(fasta_dir, destdir,
4359
only.if.missing=TRUE)
44-
}
4560
}
4661

4762

@@ -51,10 +66,10 @@
5166

5267
.form_builtin_AIRR_mouse_germline_db_name <- function(fasta_dir)
5368
{
54-
loci <- get_loci_from_input_germline_fasta_set(fasta_dir)
5569
strain <- basename(fasta_dir)
56-
version <- read_version_file(fasta_dir)
57-
sprintf("_AIRR.mouse.%s.%s.%s", strain, paste(loci, collapse="+"), version)
70+
loci_in1string <- paste(IG_LOCI, collapse="+")
71+
version <- .read_version_file(fasta_dir)
72+
sprintf("_AIRR.mouse.%s.%s.%s", strain, loci_in1string, version)
5873
}
5974

6075
.create_builtin_AIRR_mouse_germline_db <- function(fasta_dir, destdir,
@@ -65,16 +80,15 @@
6580
db_name <- .form_builtin_AIRR_mouse_germline_db_name(fasta_dir)
6681
db_path <- file.path(destdir, db_name)
6782
if (!(only.if.missing && dir.exists(db_path)))
68-
create_germline_db(fasta_dir, db_path)
83+
create_germline_db(fasta_dir, IG_LOCI, db_path)
6984
}
7085

7186
.create_missing_builtin_AIRR_mouse_germline_dbs <- function(mouse_dir, destdir)
7287
{
7388
fasta_dirs <- list.dirs(mouse_dir, recursive=FALSE)
74-
for (fasta_dir in fasta_dirs) {
89+
for (fasta_dir in fasta_dirs)
7590
.create_builtin_AIRR_mouse_germline_db(fasta_dir, destdir,
7691
only.if.missing=TRUE)
77-
}
7892
}
7993

8094

@@ -119,7 +133,7 @@ create_all_builtin_germline_dbs <- function(destdir)
119133

120134
create_missing_builtin_germline_dbs(tmp_destdir)
121135

122-
## Everyting went fine so we can rename 'tmp_destdir' to 'destdir'.
136+
## Everything went fine so we can rename 'tmp_destdir' to 'destdir'.
123137
rename_file(tmp_destdir, destdir)
124138
}
125139

@@ -128,23 +142,21 @@ create_all_builtin_germline_dbs <- function(destdir)
128142
### .create_builtin_IMGT_c_region_db()
129143
###
130144

131-
.form_builtin_IMGT_c_region_db_name <- function(organism, fasta_dir,
132-
tcr.db=FALSE)
145+
.form_builtin_IMGT_c_region_db_name <- function(organism, loci, version)
133146
{
134147
stopifnot(isSingleNonWhiteString(organism))
135-
loci <- get_loci_from_input_c_region_fasta_set(fasta_dir, tcr.db=tcr.db)
136-
version <- read_version_file(fasta_dir)
137148
sprintf("_IMGT.%s.%s.%s", organism, paste(loci, collapse="+"), version)
138149
}
139150

140-
.create_builtin_IMGT_c_region_db <- function(fasta_dir, organism, destdir,
141-
tcr.db=FALSE)
151+
.create_builtin_IMGT_c_region_db <- function(fasta_dir, loci_prefix,
152+
organism, destdir)
142153
{
143154
stopifnot(isSingleNonWhiteString(destdir), dir.exists(destdir))
144-
db_name <- .form_builtin_IMGT_c_region_db_name(organism, fasta_dir,
145-
tcr.db=tcr.db)
155+
loci <- list_loci_in_c_region_fasta_dir(fasta_dir, loci_prefix)
156+
version <- .read_version_file(fasta_dir)
157+
db_name <- .form_builtin_IMGT_c_region_db_name(organism, loci, version)
146158
db_path <- file.path(destdir, db_name)
147-
create_c_region_db(fasta_dir, db_path, tcr.db=tcr.db)
159+
create_c_region_db(fasta_dir, loci, db_path)
148160
}
149161

150162

@@ -170,16 +182,17 @@ create_all_builtin_c_region_dbs <- function(destdir)
170182
for (organism in names(LATIN_NAMES)) {
171183
organism_dir <- file.path(IMGT_c_region_dir, organism)
172184
fasta_dir <- file.path(organism_dir, "IG", "14.1")
173-
.create_builtin_IMGT_c_region_db(fasta_dir, organism, tmp_destdir)
185+
.create_builtin_IMGT_c_region_db(fasta_dir, "IG", organism,
186+
tmp_destdir)
174187
fasta_dir <- file.path(organism_dir, "TR")
175188
if (dir.exists(fasta_dir))
176-
.create_builtin_IMGT_c_region_db(fasta_dir, organism, tmp_destdir,
177-
tcr.db=TRUE)
189+
.create_builtin_IMGT_c_region_db(fasta_dir, "TR", organism,
190+
tmp_destdir)
178191
}
179192

180193
## Any other built-in C-region dbs to create?
181194

182-
## Everyting went fine so we can rename 'tmp_destdir' to 'destdir'.
195+
## Everything went fine so we can rename 'tmp_destdir' to 'destdir'.
183196
rename_file(tmp_destdir, destdir, replace=TRUE)
184197
}
185198

‎R/create_c_region_db.R‎

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,52 @@
66
###
77

88

9-
.list_C_fasta_files <- function(fasta_dir, tcr.db=FALSE)
9+
### - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
10+
### list_loci_in_c_region_fasta_dir()
11+
###
12+
13+
.list_C_fasta_files <- function(fasta_dir, loci_prefix)
1014
{
11-
stopifnot(isSingleNonWhiteString(fasta_dir), dir.exists(fasta_dir))
12-
if (!isTRUEorFALSE(tcr.db))
13-
stop(wmsg("'tcr.db' must be TRUE or FALSE"))
14-
prefix <- if (tcr.db) "TR" else "IG"
15-
pattern <- paste0("^", prefix, ".C\\.fasta$")
15+
stopifnot(isSingleNonWhiteString(fasta_dir), dir.exists(fasta_dir),
16+
isSingleNonWhiteString(loci_prefix))
17+
pattern <- paste0("^", loci_prefix, ".C\\.fasta$")
1618
fasta_files <- list.files(fasta_dir, pattern=pattern)
1719
stopifnot(length(fasta_files) != 0L)
1820
fasta_files
1921
}
2022

21-
22-
### - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
23-
### get_loci_from_input_c_region_fasta_set()
24-
###
25-
2623
### Returns a character vector of loci in canonical order.
27-
.get_loci_from_c_region_fasta_set <- function(fasta_files, tcr.db=FALSE)
24+
.get_loci_from_c_region_fasta_set <- function(fasta_files, loci_prefix)
2825
{
29-
stopifnot(is.character(fasta_files), isTRUEorFALSE(tcr.db))
26+
stopifnot(is.character(fasta_files),
27+
isSingleString(loci_prefix), loci_prefix %in% c("IG", "TR"))
3028
loci <- unique(sub("C\\.fasta$", "", fasta_files))
31-
valid_loci <- if (tcr.db) TR_LOCI else IG_LOCI
29+
valid_loci <- if (loci_prefix == "IG") IG_LOCI else TR_LOCI
3230
stopifnot(all(loci %in% valid_loci))
3331
valid_loci[valid_loci %in% loci] # return loci in canonical order
3432
}
3533

36-
get_loci_from_input_c_region_fasta_set <-
37-
function(fasta_dir, tcr.db=FALSE)
34+
list_loci_in_c_region_fasta_dir <- function(fasta_dir, loci_prefix)
35+
{
36+
fasta_files <- .list_C_fasta_files(fasta_dir, loci_prefix)
37+
.get_loci_from_c_region_fasta_set(fasta_files, loci_prefix)
38+
}
39+
40+
41+
### - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
42+
### .collect_C_fasta_files()
43+
###
44+
45+
.collect_C_fasta_files <- function(fasta_dir, loci)
3846
{
39-
fasta_files <- .list_C_fasta_files(fasta_dir, tcr.db=tcr.db)
40-
.get_loci_from_c_region_fasta_set(fasta_files, tcr.db=tcr.db)
47+
if (!isSingleNonWhiteString(fasta_dir))
48+
stop(wmsg("'fasta_dir' must be a single (non-empty) string"))
49+
if (!dir.exists(fasta_dir))
50+
stop(wmsg("directory ", fasta_dir, " not found"))
51+
stop_if_bad_loci(loci)
52+
wanted_files <- file.path(fasta_dir, paste0(loci, "C.fasta"))
53+
stopifnot(all(file.exists(wanted_files)))
54+
wanted_files
4155
}
4256

4357

@@ -61,22 +75,21 @@ get_loci_from_input_c_region_fasta_set <-
6175
### C_REGION_DBS cache compartment (see R/cache-utils.R for details about
6276
### igblastr's cache organization). This subdir or any of its parent
6377
### directories don't need to exist yet.
64-
create_c_region_db <- function(fasta_dir, destdir, tcr.db=FALSE, force=FALSE)
78+
create_c_region_db <- function(fasta_dir, loci, destdir, force=FALSE)
6579
{
6680
stopifnot(isSingleNonWhiteString(destdir))
6781
if (!isTRUEorFALSE(force))
6882
stop(wmsg("'force' must be TRUE or FALSE"))
6983
if (dir.exists(destdir) && !force)
7084
.stop_on_existing_c_region_db(destdir)
7185

72-
fasta_files <- .list_C_fasta_files(fasta_dir, tcr.db=tcr.db)
73-
fasta_files <- file.path(fasta_dir, fasta_files)
86+
fasta_files <- .collect_C_fasta_files(fasta_dir, loci)
7487

7588
## We first create the db in a temporary folder, and, only if successful,
76-
## replace 'destdir' with the temporary folder. Otherwise we destroy the
77-
## temporary folder and raise an error. This achieves atomicity and avoids
78-
## loosing the content of the existing 'destdir' in case something goes
79-
## wrong.
89+
## we replace 'destdir' with the temporary folder. Otherwise we destroy
90+
## the temporary folder and raise an error. This achieves atomicity and
91+
## avoids loosing the content of the existing 'destdir' in case something
92+
## goes wrong.
8093
tmp_destdir <- tempfile("c_region_db_")
8194
dir.create(tmp_destdir, recursive=TRUE)
8295
on.exit(nuke_file(tmp_destdir))

0 commit comments

Comments
 (0)