forked from Bioconductor/Biostrings
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXString-class.R
More file actions
538 lines (474 loc) · 17.4 KB
/
XString-class.R
File metadata and controls
538 lines (474 loc) · 17.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
### =========================================================================
### XString objects
### -------------------------------------------------------------------------
###
### The XString virtual class is a general container for storing an "external
### string".
###
setClass("XString", contains="XRaw", representation("VIRTUAL"))
### XString subclasses (no additional slots)
setClass("BString", contains="XString")
setClass("DNAString", contains="XString")
setClass("RNAString", contains="XString")
setClass("AAString", contains="XString")
### - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### Accessor-like methods.
###
setMethod("nchar", "XString", function(x, type="chars", allowNA=FALSE) length(x))
### - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### The "seqtype" and "seqtype<-" methods.
###
setMethod("seqtype", "BString", function(x) "B")
setMethod("seqtype", "DNAString", function(x) "DNA")
setMethod("seqtype", "RNAString", function(x) "RNA")
setMethod("seqtype", "AAString", function(x) "AA")
.copySubSharedRaw <- function(x, start=1, nchar=NA, lkup=NULL)
{
ans <- SharedRaw(nchar)
SharedVector.copy(ans, start, start + nchar - 1L, src=x, lkup=lkup)
}
### Downgrades 'x' to a B/DNA/RNA/AAString instance!
setReplaceMethod("seqtype", "XString",
function(x, value)
{
from_seqtype <- seqtype(x)
to_seqtype <- value
ans_class <- paste(to_seqtype, "String", sep="")
lkup <- get_seqtype_conversion_lookup(from_seqtype, to_seqtype)
if (is.null(lkup))
return(new(ans_class, shared=x@shared, offset=x@offset, length=x@length))
shared <- .copySubSharedRaw(x@shared, start=x@offset+1L, nchar=x@length, lkup=lkup)
new(ans_class, shared=shared, length=length(shared))
}
)
### - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### XString.readCodes()
###
XString.readCodes <- function(x, i, imax=integer(0))
{
SharedRaw.readInts(x@shared, x@offset + i, x@offset + imax)
}
### - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### extract_character_from_XString_by_positions() and
### extract_character_from_XString_by_ranges()
###
### Low-level generics called by the as.character(), show(), and letter()
### methods for XString and XStringViews objects. Not intended to be called
### directly by the end user.
### Purpose is to facilitate support for XString derivatives defined in
### other packages. For example, defining the following methods in the
### Modstrings package will make as.character(), show(), and letter()
### work as expected on ModString and ModStringViews objects (granted
### that seqtype() works properly on ModString derivatives via appropriate
### methods):
###
### setMethod("extract_character_from_XString_by_positions", "ModString",
### function(x, pos, collapse=FALSE)
### {
### ans <- callNextMethod()
### codec <- modscodec(seqtype(x))
### .convert_one_byte_codes_to_letters(ans, codec)
### }
### )
### setMethod("extract_character_from_XString_by_ranges", "ModString",
### function(x, start, width, collapse=FALSE)
### {
### ans <- callNextMethod()
### codec <- modscodec(seqtype(x))
### .convert_one_byte_codes_to_letters(ans, codec)
### }
### )
###
setGeneric("extract_character_from_XString_by_positions", signature="x",
function(x, pos, collapse=FALSE)
{
## Only light checking of 'pos' (i.e. we don't check that it contains
## valid positions on 'x').
stopifnot(is(x, "XString"), is.integer(pos))
ans <- standardGeneric("extract_character_from_XString_by_positions")
stopifnot(is.character(ans))
ans
}
)
### Default method.
setMethod("extract_character_from_XString_by_positions", "XString",
function(x, pos, collapse=FALSE)
{
XVector:::extract_character_from_XRaw_by_positions(x, pos,
collapse=collapse,
lkup=xs_dec_lkup(x))
}
)
setGeneric("extract_character_from_XString_by_ranges", signature="x",
function(x, start, width, collapse=FALSE)
{
## Only light checking of 'start' and 'width' (i.e. we don't check
## that they have the same length and define valid ranges on 'x').
stopifnot(is(x, "XString"), is.integer(start), is.integer(width))
ans <- standardGeneric("extract_character_from_XString_by_ranges")
stopifnot(is.character(ans))
ans
}
)
### Default method.
setMethod("extract_character_from_XString_by_ranges", "XString",
function(x, start, width, collapse=FALSE)
{
XVector:::extract_character_from_XRaw_by_ranges(x, start, width,
collapse=collapse,
lkup=xs_dec_lkup(x))
}
)
### - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### make_XString_from_string()
###
### Low-level generic called by XString() constructor. Not intended to be
### called directly by the end user.
### Purpose is to make it easy to extend the XString() constructor to
### support XString derivatives defined in other packages. For example,
### defining the following method in the Modstrings package will make calls
### of the form 'XString("ModDNA", ...)' work (granted that seqtype() works
### properly on ModDNAString objects via appropriate methods):
###
### setMethod("make_XString_from_string", "ModString",
### function(x0, string, start, width)
### {
### codec <- modscodec(seqtype(x0))
### string <- .convert_letters_to_one_byte_codes(string, codec)
### callNextMethod()
### }
### )
###
setGeneric("make_XString_from_string", signature="x0",
function(x0, string, start, width)
{
## Only light checking of 'start' and 'width' (i.e. we don't check
## that they define a valid range on 'string').
stopifnot(is(x0, "XString"),
isSingleInteger(start),
isSingleInteger(width))
if (!isSingleString(string))
stop(wmsg("input must be a single non-NA string"))
ans <- standardGeneric("make_XString_from_string")
stopifnot(class(ans) == class(x0))
ans
}
)
### Default method.
setMethod("make_XString_from_string", "XString",
function(x0, string, start, width)
{
lkup <- get_seqtype_conversion_lookup("B", seqtype(x0))
.Call2("new_XString_from_CHARACTER",
class(x0), string, start, width, lkup,
PACKAGE="Biostrings")
}
)
### - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### The XString() constructor. NOT exported.
###
### This constructor and its helper functions use the uSEW (user-specified
### Start/End/Width) interface.
###
setGeneric("XString", signature="x",
function(seqtype, x, start=NA, end=NA, width=NA)
standardGeneric("XString")
)
.charToXString <- function(seqtype, string, start, end, width)
{
if (!isSingleString(string))
stop(wmsg("input must be a single non-NA string"))
x0 <- new2(paste0(seqtype, "String"), check=FALSE)
solved_SEW <- solveUserSEW(width(string),
start=start, end=end, width=width)
make_XString_from_string(x0, string, start(solved_SEW), width(solved_SEW))
}
setMethod("XString", "character",
function(seqtype, x, start=NA, end=NA, width=NA)
{
if (is.null(seqtype))
seqtype <- "B"
.charToXString(seqtype, x, start, end, width)
}
)
setMethod("XString", "factor",
function(seqtype, x, start=NA, end=NA, width=NA)
{
if (is.null(seqtype))
seqtype <- "B"
.charToXString(seqtype, as.character(x), start, end, width)
}
)
setMethod("XString", "XString",
function(seqtype, x, start=NA, end=NA, width=NA)
{
ans <- subseq(x, start=start, end=end, width=width)
## `seqtype<-` must be called even when user supplied 'seqtype' is
## NULL because we want to enforce downgrade to a B/DNA/RNA/AAString
## instance
if (is.null(seqtype))
seqtype <- seqtype(x)
seqtype(ans) <- seqtype
ans
}
)
### Just because of the silly "AsIs" objects found in the probe packages
### (e.g. drosophila2probe$sequence)
setMethod("XString", "AsIs",
function(seqtype, x, start=NA, end=NA, width=NA)
{
if (!is.character(x))
stop("unsupported input type")
class(x) <- "character" # keeps the names (unlike as.character())
XString(seqtype, x, start=start, end=end, width=width)
}
)
### - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### The user interfaces to the XString() constructor.
###
BString <- function(x="", start=1, nchar=NA)
XString("B", x, start=start, width=nchar)
DNAString <- function(x="", start=1, nchar=NA)
XString("DNA", x, start=start, width=nchar)
RNAString <- function(x="", start=1, nchar=NA)
XString("RNA", x, start=start, width=nchar)
AAString <- function(x="", start=1, nchar=NA)
XString("AA", x, start=start, width=nchar)
### - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### Coercion.
###
setAs("XString", "BString",
function(from) {seqtype(from) <- "B"; from}
)
setAs("XString", "DNAString",
function(from) {seqtype(from) <- "DNA"; from}
)
setAs("XString", "RNAString",
function(from) {seqtype(from) <- "RNA"; from}
)
setAs("XString", "AAString",
function(from) {seqtype(from) <- "AA"; from}
)
setAs("character", "BString", function(from) BString(from))
setAs("character", "DNAString", function(from) DNAString(from))
setAs("character", "RNAString", function(from) RNAString(from))
setAs("character", "AAString", function(from) AAString(from))
setAs("character", "XString", function(from) BString(from))
setMethod("as.character", "XString",
function(x)
extract_character_from_XString_by_ranges(x, 1L, length(x))
)
setMethod("toString", "XString", function(x, ...) as.character(x))
### FIXME: Sometimes returns a vector sometimes a factor. This needs to be
### sorted out. The use case is that as.data.frame() relies on this.
setMethod("as.vector", "XString",
function(x)
{
codes <- xscodes(x)
x_alphabet <- names(codes)
if (is.null(x_alphabet)) {
ans <- rawToChar(as.raw(x), multiple=TRUE)
x_alphabet <- alphabet(x)
if (!is.null(x_alphabet))
ans <- factor(ans, levels=x_alphabet)
return(ans)
}
code2pos <- integer(length(codes))
code2pos[codes] <- seq_along(codes)
ans <- code2pos[as.integer(x)]
attributes(ans) <- list(levels=x_alphabet, class="factor")
ans
}
)
### - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### The "show" method.
###
compact_ellipsis <- rawToChar(as.raw(c(0xe2, 0x80, 0xa6)))
### NOT exported but used in the BSgenome package.
### 'x' must be a single character string, or an XString or
### MaskedXString object.
### Return a character vector possibly with a class attribute on it for
### later S3 dispatch in add_colors().
toSeqSnippet <- function(x, width)
{
if (width < 7L)
width <- 7L
## Do NOT use nchar() here as it wouldn't do the right thing on a
## MaskedXString object!
x_len <- length(x)
if (x_len <= width) {
ans <- as.character(x)
} else {
w1 <- (width - 2L) %/% 2L
w2 <- (width - 3L) %/% 2L
ans <- paste0(as.character(subseq(x, start=1, width=w1)),
#compact_ellipsis,
"...",
as.character(subseq(x, end=x_len, width=w2)))
}
if (is(x, "XString") || is(x, "MaskedXString"))
class(ans) <- c(seqtype(x), class(ans)) # for S3 dispatch
# in add_colors()
ans
}
setMethod("show", "XString",
function(object)
{
object_len <- object@length
cat(object_len, "-letter ", class(object), " object\n", sep="")
snippet <- toSeqSnippet(object, getOption("width") - 5L)
cat("seq: ", add_colors(snippet), "\n", sep="")
}
)
setMethod("showAsCell", "XString",
function(object)
{
ans <- safeExplode(as.character(object))
class(ans) <- c(seqtype(object), class(ans))
ans
}
)
### - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### Equality.
###
### We want:
### BString("ab") == "ab" # TRUE
### DNAString("TG") == RNAString("UG") # TRUE!!!
### library(BSgenome.Hsapiens.UCSC.hg18)
### dna <- Hsapiens$chr1
### dna != Hsapiens$chr1 # FALSE
### dnav <- Views(dna, start=1:7, end=101:107)
### dnav[[1]] == dnav[[7]] # TRUE
### dnav <- Views(dna, start=1:7, end=(length(dna)-6):length(dna))
### This is fast:
### dnav[[1]] == dnav[[7]] # FALSE
### But this would have killed your machine:
### s1 <- toString(dnav[[1]])
### s7 <- toString(dnav[[7]])
### s1 == s7
### 'x' and 'y' must be XString objects
.XString.equal <- function(x, y)
{
if (x@length != y@length)
return(FALSE)
ans <- !SharedVector.compare(x@shared, x@offset + 1L, y@shared, y@offset + 1L, x@length)
as.logical(ans)
}
setMethod("==", signature(e1="XString", e2="XString"),
function(e1, e2)
{
if (!comparable_seqtypes(seqtype(e1), seqtype(e2))) {
class1 <- class(e1)
class2 <- class(e2)
stop("comparison between a \"", class1, "\" instance ",
"and a \"", class2, "\" instance ",
"is not supported")
}
.XString.equal(e1, e2)
}
)
setMethod("==", signature(e1="BString", e2="character"),
function(e1, e2)
{
if (length(e2) != 1 || e2 %in% c("", NA))
stop("comparison between a \"BString\" object and a character vector ",
"of length != 1 or an empty string or an NA ",
"is not supported")
.XString.equal(e1, BString(e2))
}
)
setMethod("==", signature(e1="character", e2="BString"),
function(e1, e2) e2 == e1
)
setMethod("==", signature(e1="XString", e2="character"),
## Coerce to BString to handle things like RNAString("U") == "T"
function(e1, e2) as(e1, "BString") == e2
)
setMethod("==", signature(e1="character", e2="XString"),
function(e1, e2) e1 == as(e2, "BString")
)
setMethod("!=", signature(e1="XString", e2="XString"),
function(e1, e2) !(e1 == e2)
)
setMethod("!=", signature(e1="BString", e2="character"),
function(e1, e2) !(e1 == e2)
)
setMethod("!=", signature(e1="character", e2="BString"),
function(e1, e2) !(e1 == e2)
)
setMethod("!=", signature(e1="XString", e2="character"),
function(e1, e2) as(e1, "BString") != e2
)
setMethod("!=", signature(e1="character", e2="XString"),
function(e1, e2) e1 != as(e2, "BString")
)
### Comparisons are already implemented for XStringSet objects,
### so we can just dispatch to that code here.
setMethod("<=", signature(e1="XString", e2="XString"),
function(e1, e2)
{
if (!comparable_seqtypes(seqtype(e1), seqtype(e2))) {
class1 <- class(e1)
class2 <- class(e2)
stop("comparison between a \"", class1, "\" instance ",
"and a \"", class2, "\" instance ",
"is not supported")
}
as(e1, "XStringSet") <= as(e2, "XStringSet")
}
)
setMethod("<=", signature(e1="XString", e2="character"),
function(e1, e2) as(e1, "BStringSet") <= e2
)
setMethod("<=", signature(e1="character", e2="XString"),
function(e1, e2) e1 <= as(e2, "BStringSet")
)
### - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### The "substr" and "substring" methods.
###
setMethod("substr", "XString",
function(x, start, stop) subseq(x, start=start, end=stop)
)
setMethod("substring", "XString",
function(text, first, last=1000000L) subseq(text, start=first, end=last)
)
### - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### updateObject()
###
### Update XString objects created before the big internal renaming I made
### in IRanges 1.3.76.
setMethod("updateObject", "XString",
function(object, ..., verbose=FALSE)
{
if (!is(try(object@shared, silent=TRUE), "try-error"))
return(object)
xdata <- object@xdata
ans_shared <- new("SharedRaw")
ans_shared@xp <- xdata@xp
ans_shared@.link_to_cached_object=xdata@.link_to_cached_object
new2(class(object),
shared=ans_shared,
offset=object@offset,
length=object@length,
check=FALSE)
}
)
### Update AAString objects created before AA_ALPHABET was enforced
setMethod("updateObject", "AAString",
function(object, ..., verbose=FALSE)
{
## Start by calling the updateObject() method for XString objects.
object <- callNextMethod()
codec <- xscodec(AAString())
class(object) <- "BString"
mapping <- vapply(uniqueLetters(object), utf8ToInt, integer(1L))
missingVals <- is.na(codec@enc_lkup[mapping+1L])
if(any(missingVals)){
errorChars <- paste(names(mapping)[which(missingVals)],
collapse=', ')
stop("Cannot decode, AAString contains invalid character(s): ",
errorChars)
}
AAString(object)
}
)