Skip to content

Commit fd042e8

Browse files
committed
Add low-level utilities read_auxdata() and write_auxdata().
Also the 'auxiliary_data' argument of igblastn() can now be a data.frame in addition to the path to a file containing auxiliary data (.aux file).
1 parent 15435e0 commit fd042e8

22 files changed

Lines changed: 385 additions & 139 deletions

‎DESCRIPTION‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Collate:
5151
ndm_data-IO.R
5252
compute_V_gene_delineations.R
5353
clean_allele_set.R
54+
auxdata-IO.R
5455
compute_auxdata.R
5556
LATIN_NAMES.R
5657
IMGT-utils.R

‎NAMESPACE‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ export(
9191
compute_V_gene_delineations,
9292
compute_imgt_intdata,
9393

94+
## auxdata-IO.R:
95+
read_auxdata,
96+
write_auxdata,
97+
9498
## compute_auxata.R:
9599
compute_auxdata,
96100

‎R/auxdata-IO.R‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
### =========================================================================
2+
### Read/write ".aux" files
3+
### -------------------------------------------------------------------------
4+
###
5+
6+
7+
### Not exported!
8+
### Not the true colnames used in IgBLAST ".aux" files: ours are shorter,
9+
### all lowercase, and contain underscores instead of spaces.
10+
AUXDATA_COL2CLASS <- c(
11+
allele_name="character",
12+
coding_frame_start="integer",
13+
chain_type="character",
14+
cdr3_end="integer",
15+
extra_bps="integer"
16+
)
17+
18+
19+
### - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
20+
### check_auxdata_col2class()
21+
###
22+
23+
### Not exported!
24+
check_auxdata_col2class <- function(auxdata, what="'auxdata'")
25+
{
26+
if (!is.data.frame(auxdata))
27+
stop(wmsg(what, " must be a data.frame"))
28+
expected_colnames <- names(AUXDATA_COL2CLASS)
29+
if (!identical(colnames(auxdata), expected_colnames)) {
30+
in1string <- paste(expected_colnames, collapse=", ")
31+
stop(wmsg(what, " must have the following columns ",
32+
"(in this order): ", in1string))
33+
}
34+
col2class <- vapply(auxdata, function(x) class(x)[[1L]], character(1))
35+
if (!identical(col2class, AUXDATA_COL2CLASS)) {
36+
in1string <- paste0(" ", names(AUXDATA_COL2CLASS), " -> ",
37+
AUXDATA_COL2CLASS, collapse="\n")
38+
stop(wmsg(what, " must have the following column types:"), "\n",
39+
in1string)
40+
}
41+
}
42+
43+
44+
### - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
45+
### read_auxdata()
46+
### write_auxdata()
47+
###
48+
49+
read_auxdata <- function(filepath)
50+
{
51+
read_broken_table(filepath, AUXDATA_COL2CLASS)
52+
}
53+
54+
write_auxdata <- function(auxdata, file="")
55+
{
56+
check_auxdata_col2class(auxdata)
57+
header <- paste0("#", paste(colnames(auxdata), collapse=", "))
58+
cat(header, "\n", sep="", file=file)
59+
write.table(auxdata, file, append=TRUE, quote=FALSE,
60+
sep="\t", row.names=FALSE, col.names=FALSE)
61+
}
62+

‎R/auxdata-utils.R‎

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,6 @@ get_igblast_auxiliary_data <- function(...)
3232
### load_auxdata()
3333
###
3434

35-
### Not the true colnames used in IgBLAST auxdata files. However, ours are
36-
### shorter, all lowercase, and contain underscores instead of spaces.
37-
.IGBLAST_AUXDATA_COL2CLASS <- c(
38-
allele_name="character",
39-
coding_frame_start="integer",
40-
chain_type="character",
41-
cdr3_end="integer",
42-
extra_bps="integer"
43-
)
44-
4535
### IMPORTANT NOTE: Unlike with the data.frame returned by load_intdata(),
4636
### all the positions in the data.frame returned by load_auxdata() (that is,
4737
### the positions reported in columns 'coding_frame_start' and 'cdr3_end')
@@ -50,7 +40,7 @@ load_auxdata <- function(organism, which=c("live", "original"))
5040
{
5141
which <- match.arg(which)
5242
auxdata_path <- get_auxdata_path(organism, which=which)
53-
read_broken_table(auxdata_path, .IGBLAST_AUXDATA_COL2CLASS)
43+
read_auxdata(auxdata_path)
5444
}
5545

5646
load_igblast_auxiliary_data <- function(...)

‎R/clean_allele_set.R‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ checkarg_with.intdata <- function(with.intdata, gapped)
119119
stopifnot(is(dna, "DNAStringSet"))
120120
## We only care about the annotations found in the Metadata Columns
121121
## Of Interest listed in 'MCOI' when comparing the V allele annotations.
122-
MCOI <- setdiff(names(NDM_COL2CLASS), "chain_type")
122+
MCOI <- setdiff(names(NDM_DATA_COL2CLASS), "chain_type")
123123
ok <- .repeated_vector_elts_have_identical_metadata(dna, MCOI=MCOI)
124124
if (!ok) {
125125
msg <- c("V alleles with identical ungapped sequences and names ",

‎R/compute_V_gene_delineations.R‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,13 @@ EXTENDED_V_GENE_DELINEATION_COLNAMES <- c(
8484
### Sanity checks.
8585
stopifnot(
8686
identical(
87-
setdiff(names(NDM_COL2CLASS), EXTENDED_V_GENE_DELINEATION_COLNAMES),
87+
setdiff(names(NDM_DATA_COL2CLASS),
88+
EXTENDED_V_GENE_DELINEATION_COLNAMES),
8889
"chain_type"
8990
),
9091
identical(
91-
setdiff(EXTENDED_V_GENE_DELINEATION_COLNAMES, names(NDM_COL2CLASS)),
92+
setdiff(EXTENDED_V_GENE_DELINEATION_COLNAMES,
93+
names(NDM_DATA_COL2CLASS)),
9294
c("seq_len", "starting_gap", "all_gaps_in_frame", "all_gaps_contained")
9395
)
9496
)

‎R/create_region_db.R‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@
149149
internal_data_path <- file.path(destdir, "internal_data")
150150
stopifnot(!dir.exists(internal_data_path))
151151

152-
ndm_data <- as.data.frame(dna_mcols[ , names(NDM_COL2CLASS)])
152+
ndm_data <- as.data.frame(dna_mcols[ , names(NDM_DATA_COL2CLASS)])
153153
check_ndm_data_col2class(ndm_data)
154154
dir.create(internal_data_path)
155155
destfile <- file.path(internal_data_path, "V.ndm.imgt")

‎R/download_OGRDB_germline_json.R‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ makeogrannote <- function(ogrdb_json_file)
156156
"in JSON file: ", ogrdb_json_file))
157157
data <- character(0)
158158
}
159-
col2class <- head(NDM_COL2CLASS, n=-1L)
159+
col2class <- head(NDM_DATA_COL2CLASS, n=-1L)
160160
m <- matrix(data, ncol=length(col2class), byrow=TRUE)
161161
df <- matrix2df(m, col2class)
162162
cbind(df, coding_frame_start=integer(nrow(df)))
@@ -190,7 +190,7 @@ makeogrannote <- function(ogrdb_json_file)
190190
intdata <- compute_V_gene_delineations(V_fasta_file)
191191
locus <- extract_loci_from_OGRDB_set_names(organism, names(germline_set))
192192
intdata$chain_type <- paste0("V", substr(locus, 3L, 3L))
193-
intdata[ , names(NDM_COL2CLASS)]
193+
intdata[ , names(NDM_DATA_COL2CLASS)]
194194
}
195195

196196
### Returns the intdata in a data.frame.

‎R/ndm_data-IO.R‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66

77
### Not exported!
8-
### Not the true colnames used in IgBLAST intdata files: ours are all
8+
### Not the true colnames used in IgBLAST "ndm" files: ours are all
99
### lowercase and we've replaced spaces with underscores.
1010
### Note that many columns are redundant:
1111
### - columns 'cdr1_start', 'fwr2_start', 'cdr2_start', and 'fwr3_start'
@@ -14,7 +14,7 @@
1414
### - columns 'fwr1_start' and 'coding_frame_start' are redundant (and
1515
### column 'fwr1_start' is a dumb column anyways because it should always
1616
### be set to 1).
17-
NDM_COL2CLASS <- c(
17+
NDM_DATA_COL2CLASS <- c(
1818
allele_name="character",
1919
fwr1_start="integer",
2020
fwr1_end="integer",
@@ -34,7 +34,7 @@ NDM_COL2CLASS <- c(
3434
V_GENE_SEGMENTS <- c("fwr1", "cdr1", "fwr2", "cdr2", "fwr3")
3535
V_GENE_DELINEATION_COLNAMES <- paste0(rep(V_GENE_SEGMENTS, each=2L),
3636
c("_start", "_end"))
37-
stopifnot(all(V_GENE_DELINEATION_COLNAMES %in% names(NDM_COL2CLASS)))
37+
stopifnot(all(V_GENE_DELINEATION_COLNAMES %in% names(NDM_DATA_COL2CLASS)))
3838

3939

4040
### - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@@ -46,16 +46,16 @@ check_ndm_data_col2class <- function(ndm_data, what="'ndm_data'")
4646
{
4747
if (!is.data.frame(ndm_data))
4848
stop(wmsg(what, " must be a data.frame"))
49-
expected_colnames <- names(NDM_COL2CLASS)
49+
expected_colnames <- names(NDM_DATA_COL2CLASS)
5050
if (!identical(colnames(ndm_data), expected_colnames)) {
5151
in1string <- paste(expected_colnames, collapse=", ")
5252
stop(wmsg(what, " must have the following columns ",
5353
"(in this order): ", in1string))
5454
}
5555
col2class <- vapply(ndm_data, function(x) class(x)[[1L]], character(1))
56-
if (!identical(col2class, NDM_COL2CLASS)) {
57-
in1string <- paste0(" ", names(NDM_COL2CLASS), " -> ",
58-
NDM_COL2CLASS, collapse="\n")
56+
if (!identical(col2class, NDM_DATA_COL2CLASS)) {
57+
in1string <- paste0(" ", names(NDM_DATA_COL2CLASS), " -> ",
58+
NDM_DATA_COL2CLASS, collapse="\n")
5959
stop(wmsg(what, " must have the following column types:"), "\n",
6060
in1string)
6161
}
@@ -107,7 +107,7 @@ validate_ndm_rows <- function(ndm_data, allow.repeated.rows=FALSE)
107107

108108
read_ndm_data <- function(filepath)
109109
{
110-
read_broken_table(filepath, NDM_COL2CLASS)
110+
read_broken_table(filepath, NDM_DATA_COL2CLASS)
111111
}
112112

113113
write_ndm_data <- function(ndm_data, file="", check.data=FALSE)

0 commit comments

Comments
 (0)