Skip to content

Commit 734f6c4

Browse files
committed
cleans up some methods, removes <<- assignment, disable braille
1 parent ef2fca1 commit 734f6c4

File tree

3 files changed

+54
-11
lines changed

3 files changed

+54
-11
lines changed

R/XString-class.R

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,19 @@ setMethod("extract_character_from_XString_by_ranges", "XString",
142142
### BString methods to support 0:255 input
143143

144144
## BSTRING_RAW_LOOKUP is initialized in `zzz.R`
145-
## this value is just a backup on the offchance `zzz.R:.onLoad` fails
146-
BSTRING_RAW_LOOKUP <- rawToChar(as.raw(0:255), multiple=TRUE)
147145
setMethod("extract_character_from_XString_by_ranges", "BString",
148146
function(x, start, width, collapse=FALSE)
149147
{
150148
SHOW_RAW <- getOption("Biostrings.showRaw")
149+
if(!is.logical(SHOW_RAW)){
150+
warning("Invalid value for option 'Biostrings.showRaw', ",
151+
"resetting to FALSE")
152+
SHOW_RAW <- FALSE
153+
options(Biostrings.showRaw=FALSE)
154+
}
151155
if(!SHOW_RAW) callNextMethod()
156+
157+
bstring_lookup <- get("BSTRING_RAW_LOOKUP", envir=.pkgenv)
152158
lkup <- xs_dec_lkup(x)
153159

154160
## need to remap null bytes, they have to be in 0:255
@@ -159,7 +165,7 @@ setMethod("extract_character_from_XString_by_ranges", "BString",
159165
lkup=lkup)
160166
## replace all undisplayable characters
161167
for(i in seq_along(xs))
162-
xs[i] <- paste(BSTRING_RAW_LOOKUP[as.integer(charToRaw(xs[i]))+1L],
168+
xs[i] <- paste(bstring_lookup[as.integer(charToRaw(xs[i]))+1L],
163169
collapse='')
164170
xs
165171
}
@@ -169,8 +175,15 @@ setMethod("extract_character_from_XString_by_positions", "BString",
169175
function(x, pos, collapse=FALSE)
170176
{
171177
SHOW_RAW <- getOption("Biostrings.showRaw")
178+
if(!is.logical(SHOW_RAW)){
179+
warning("Invalid value for option 'Biostrings.showRaw', ",
180+
"resetting to FALSE")
181+
SHOW_RAW <- FALSE
182+
options(Biostrings.showRaw=FALSE)
183+
}
172184
if(!SHOW_RAW) callNextMethod()
173185
lkup <- xs_dec_lkup(x)
186+
bstring_lookup <- get("BSTRING_RAW_LOOKUP", envir=.pkgenv)
174187

175188
## need to remap null bytes, they have to be in 0:255
176189
## so we have to overload some value
@@ -180,7 +193,7 @@ setMethod("extract_character_from_XString_by_positions", "BString",
180193
lkup=lkup)
181194
## replace all undisplayable characters
182195
for(i in seq_along(xs))
183-
xs[i] <- paste(BSTRING_RAW_LOOKUP[as.integer(charToRaw(xs[i]))+1L],
196+
xs[i] <- paste(bstring_lookup[as.integer(charToRaw(xs[i]))+1L],
184197
collapse='')
185198
xs
186199
}

R/zzz.R

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
###
22

3+
.pkgenv <- new.env(parent=emptyenv())
4+
35
.onLoad <- function(libname, pkgname)
46
{
57
.Call2("init_DNAlkups",
@@ -28,17 +30,22 @@
2830
encoding_details <- l10n_info()
2931
bstring_lookup <- rawToChar(as.raw(0:255), multiple=TRUE)
3032
invalid_chars <- c(1:32,128:256)
31-
if(encoding_details$`UTF-8`){
32-
# braille is nice if supported
33-
# allows for char comparisons after as.character() comparisons
34-
bstring_lookup[invalid_chars] <-
35-
as.character(parse(text=paste0("'\\U28", as.raw(95:255), "'")))
36-
} else if (encoding_details$MBCS){
33+
# if(encoding_details$`UTF-8`){
34+
# # braille is nice if supported
35+
# # allows for char comparisons after as.character() comparisons
36+
# # I think it's overkill, though...uncomment this section if we need it
37+
# bstring_lookup[invalid_chars] <-
38+
# as.character(parse(text=paste0("'\\U28", as.raw(95:255), "'")))
39+
# } else
40+
if (encoding_details$MBCS){
3741
# use multibyte question mark if supported
3842
compact_unknown <- rawToChar(as.raw(c(0xef, 0xbf, 0xbd)))
3943
bstring_lookup[invalid_chars] <- compact_unknown
44+
} else {
45+
# otherwise just use the regular '?'
46+
bstring_lookup[invalid_chars] <- "?"
4047
}
41-
BSTRING_RAW_LOOKUP <<- bstring_lookup
48+
assign("BSTRING_RAW_LOOKUP", bstring_lookup, envir=.pkgenv)
4249
}
4350

4451
.onUnload <- function(libpath)

tests/testthat/test-XString-class.R

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,29 @@ test_that("reverse, complement, reverseComplement work correctly", {
250250
expect_equal(as.character(reverseComplement(mrna)), .revString(mr_comp))
251251
})
252252

253+
test_that("BStrings display correctly with full 0-255 value range", {
254+
orig_setting <- getOption("Biostrings.showRaw")
255+
full_bstring <- as(as(as.raw(0:255),"XRaw"),"BString")
256+
options(Biostrings.showRaw=FALSE)
257+
expect_error(extract_character_from_XString_by_ranges(full_bstring, 1L, 256L),
258+
"embedded nul in string")
259+
260+
## can't really test MBCS vs. non-MBCS because we can't guarantee
261+
## the test suites will run on a platform with(out) MBCS
262+
options(Biostrings.showRaw=TRUE)
263+
expect_is(extract_character_from_XString_by_ranges(full_bstring, 1L, 256L),
264+
"character")
265+
266+
options(Biostrings.showRaw=10)
267+
expect_warning(extract_character_from_XString_by_ranges(BString("ABCD"), 1L, 4L),
268+
"Invalid value for option 'Biostrings.showRaw'")
269+
expect_false(getOption("Biostrings.showRaw"))
270+
271+
## make sure we leave the system as we found it
272+
options(Biostrings.showRaw=orig_setting)
273+
274+
})
275+
253276
## Porting RUnit tests
254277
test_that("alphabet finds the correct values", {
255278
expect_equal(alphabet(DNAString(dnastr)), strsplit(dnastr, "")[[1]])

0 commit comments

Comments
 (0)