Skip to content

Commit a78209e

Browse files
committed
enabled application of vector of atomicclasses to plate type data
1 parent 52bde9e commit a78209e

14 files changed

+223
-13
lines changed

R/read.R

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,30 @@
99
#' @noRd
1010
#'
1111
read_cells <- function(drfile, sheet, variables, translate = FALSE, translations = NULL, atomicclass = 'character') {
12+
# Process each variable
1213
result <- lapply(variables, function(v) {
13-
dims <- dim(cellranger::as.cell_limits(v$cell))
14-
if (any(dims > 1)) {
14+
# Ensure the cell address points to a single cell
15+
if (any(dim(cellranger::as.cell_limits(v$cell)) > 1)) {
1516
rlang::abort(glue::glue("A cell address should point to a single cell."))
1617
}
17-
x <- suppressMessages(readxl::read_excel(drfile, sheet = sheet, range = v$cell, col_names = FALSE))
18-
if (nrow(x) == 0) {
19-
x <- NA
18+
19+
# Read the cell value
20+
cell_data <- suppressMessages(
21+
readxl::read_excel(drfile, sheet = sheet, range = v$cell, col_names = FALSE)
22+
)
23+
24+
# Handle empty cells
25+
if (nrow(cell_data) == 0) {
26+
NA
2027
} else {
21-
x <- x[[1]][1]
28+
cell_data[[1]][1]
2229
}
2330
})
24-
result <- stats::setNames(result, sapply(variables, function(x) x$name))
31+
32+
# Assign names to the results
33+
result <- stats::setNames(result, sapply(variables, `[[`, "name"))
34+
35+
# Coerce values to the specified atomic class
2536
lapply(result, coerce, atomicclass)
2637
}
2738

@@ -126,12 +137,33 @@ plate_to_df <- function(d) {
126137
#' @return A data frame in long format
127138
#' @noRd
128139
read_key_plate <- function(drfile, sheet, ranges, translate = FALSE, translations = NULL, atomicclass = "character", ...) {
140+
# Read and convert each range to a long-format data frame
129141
chunks <- lapply(ranges, function(range) {
130142
plate <- readxl::read_excel(drfile, sheet = sheet, range = range) |>
131143
plate_to_df()
132144
})
133-
# TODO: handle vectors of atomicclass
134-
suppressMessages(Reduce(dplyr::full_join, chunks))
145+
146+
# Combine all chunks into a single data frame
147+
combined <- suppressMessages(Reduce(dplyr::full_join, chunks))
148+
149+
# Handle vectors of atomicclass
150+
if (length(atomicclass) == 1) {
151+
combined[] <- lapply(combined, coerce, atomicclass)
152+
} else {
153+
if (length(atomicclass) != ncol(combined)) {
154+
rlang::abort(glue::glue(
155+
"The number of atomic classes ({length(atomicclass)}) must be 1 or equal to the number of columns ({ncol(combined)}) in the combined data frame."
156+
))
157+
}
158+
combined[] <- Map(coerce, combined, atomicclass)
159+
}
160+
161+
# Translate column names if required
162+
if (translate) {
163+
names(combined) <- long_to_shortnames(names(combined), translations)
164+
}
165+
166+
combined
135167
}
136168

137169
#' Translation between long and short variable names
@@ -144,25 +176,34 @@ read_key_plate <- function(drfile, sheet, ranges, translate = FALSE, translation
144176
#' @return A vector of long or short variable names
145177
#' @export
146178
long_to_shortnames <- function(v, translations) {
179+
# Match long names to their corresponding short names
147180
positions <- match(v, translations$long)
148181
shortnames <- translations$short[positions]
149-
if (any (is.na(positions))) {
182+
183+
# Handle missing translations
184+
missing_indices <- is.na(positions)
185+
if (any(missing_indices)) {
150186
rlang::warn("Missing translations. Using original long names.")
151-
shortnames[is.na(positions)] <- v[is.na(positions)]
187+
shortnames[missing_indices] <- v[missing_indices]
152188
}
189+
153190
shortnames
154191
}
155192

156193
#' @return A vector of long variable names
157194
#' @rdname long_to_shortnames
158195
#' @export
159196
short_to_longnames <- function(v, translations) {
197+
# Match short names to their corresponding long names
160198
positions <- match(v, translations$short)
161199
longnames <- translations$long[positions]
162-
if (any(is.na(positions))) {
163-
rlang::warn("Missing reverse translations. Using short names.")
200+
201+
# Handle missing translations
202+
if (anyNA(positions)) {
203+
rlang::warn("Missing reverse translations. Using original short names.")
164204
longnames[is.na(positions)] <- v[is.na(positions)]
165205
}
206+
166207
longnames
167208
}
168209

data-raw/guide_competition_1_0_source.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,19 @@ locations:
3636
- sheet: _data
3737
type: platedata
3838
translate: false
39+
atomicclass:
40+
- character
41+
- integer
42+
- character
43+
- numeric
44+
- numeric
45+
- numeric
46+
- numeric
47+
- numeric
48+
- numeric
49+
- character
50+
- character
51+
- character
3952
varname: plate
4053
ranges:
4154
- A1:M9

data-raw/schema_tests/invalid_atomicclass.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,19 @@ locations:
2222
- sheet: _data
2323
type: platedata
2424
translate: false
25+
atomicclass:
26+
- character
27+
- integer
28+
- character
29+
- numeric
30+
- numeric
31+
- numeric
32+
- numeric
33+
- numeric
34+
- numeric
35+
- character
36+
- character
37+
- character
2538
varname: plate
2639
ranges:
2740
- A1:M9

data-raw/schema_tests/invalid_plate_format.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,19 @@ locations:
3636
- sheet: _data
3737
type: platedata
3838
translate: false
39+
atomicclass:
40+
- character
41+
- integer
42+
- character
43+
- numeric
44+
- numeric
45+
- numeric
46+
- numeric
47+
- numeric
48+
- numeric
49+
- character
50+
- character
51+
- character
3952
varname: plate
4053
ranges:
4154
- A1:M9

data-raw/schema_tests/invalid_type.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,19 @@ locations:
3636
- sheet: _data
3737
type: platedata
3838
translate: false
39+
atomicclass:
40+
- character
41+
- integer
42+
- character
43+
- numeric
44+
- numeric
45+
- numeric
46+
- numeric
47+
- numeric
48+
- numeric
49+
- character
50+
- character
51+
- character
3952
varname: plate
4053
ranges:
4154
- A1:M9

data-raw/schema_tests/maximal_version.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,19 @@ locations:
3636
- sheet: _data
3737
type: platedata
3838
translate: false
39+
atomicclass:
40+
- character
41+
- integer
42+
- character
43+
- numeric
44+
- numeric
45+
- numeric
46+
- numeric
47+
- numeric
48+
- numeric
49+
- character
50+
- character
51+
- character
3952
varname: plate
4053
ranges:
4154
- A1:M9

data-raw/schema_tests/missing_location_element.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,19 @@ locations:
3535
- sheet: _data
3636
type: platedata
3737
translate: false
38+
atomicclass:
39+
- character
40+
- integer
41+
- character
42+
- numeric
43+
- numeric
44+
- numeric
45+
- numeric
46+
- numeric
47+
- numeric
48+
- character
49+
- character
50+
- character
3851
varname: plate
3952
ranges:
4053
- A1:M9

data-raw/schema_tests/missing_translations.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,19 @@ locations:
3636
- sheet: _data
3737
type: platedata
3838
translate: false
39+
atomicclass:
40+
- character
41+
- integer
42+
- character
43+
- numeric
44+
- numeric
45+
- numeric
46+
- numeric
47+
- numeric
48+
- numeric
49+
- character
50+
- character
51+
- character
3952
varname: plate
4053
ranges:
4154
- A1:M9

data-raw/schema_tests/missing_version.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,19 @@ locations:
3535
- sheet: _data
3636
type: platedata
3737
translate: false
38+
atomicclass:
39+
- character
40+
- integer
41+
- character
42+
- numeric
43+
- numeric
44+
- numeric
45+
- numeric
46+
- numeric
47+
- numeric
48+
- character
49+
- character
50+
- character
3851
varname: plate
3952
ranges:
4053
- A1:M9

tests/testthat/testdata/erroneousguides/conflicting_max_version.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,19 @@ locations:
3636
- sheet: _data
3737
type: platedata
3838
translate: false
39+
atomicclass:
40+
- character
41+
- integer
42+
- character
43+
- numeric
44+
- numeric
45+
- numeric
46+
- numeric
47+
- numeric
48+
- numeric
49+
- character
50+
- character
51+
- character
3952
varname: plate
4053
ranges:
4154
- A1:M9

0 commit comments

Comments
 (0)