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 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
|
### =========================================================================
### IPos objects
### -------------------------------------------------------------------------
###
setClass("IPos",
contains=c("Pos", "IPosRanges"),
representation(
"VIRTUAL",
NAMES="character_OR_NULL" # R doesn't like @names !!
)
)
### Combine the new "parallel slots" with those of the parent class. Make
### sure to put the new parallel slots **first**. See R/Vector-class.R file
### in the S4Vectors package for what slots should or should not be considered
### "parallel".
setMethod("parallel_slot_names", "IPos",
function(x) c("NAMES", callNextMethod())
)
setClass("UnstitchedIPos",
contains="IPos",
representation(
pos="integer"
)
)
### Combine the new "parallel slots" with those of the parent class. Make
### sure to put the new parallel slots **first**. See R/Vector-class.R file
### in the S4Vectors package for what slots should or should not be considered
### "parallel".
setMethod("parallel_slot_names", "UnstitchedIPos",
function(x) c("pos", callNextMethod())
)
setClass("StitchedIPos",
contains="IPos",
representation(
pos_runs="IRanges" # An unnamed IRanges instance that has
# been "stitched" (see below).
)
)
### - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### Validity
###
.OLD_IPOS_INSTANCE_MSG <- c(
"Starting with BioC 3.10, the class attribute of all ",
"IPos **instances** needs to be set to \"StitchedIPos\". ",
"Please update this object with 'updateObject(object, verbose=TRUE)' ",
"and re-serialize it."
)
.validate_IPos <- function(x)
{
if (class(x) == "IPos")
return(paste(.OLD_IPOS_INSTANCE_MSG, collapse=""))
NULL
}
setValidity2("IPos", .validate_IPos)
### TODO: Add validity methods for UnstitchedIPos and StitchedIPos objects.
### - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### Very low-level UnstitchedIPos and StitchedIPos constructors
###
### For maximum efficiency, these constructors trust all the supplied
### arguments and do not validate the object.
###
.unsafe_new_UnstitchedIPos <- function(pos, names=NULL, mcols=NULL,
metadata=list())
{
new2("UnstitchedIPos", pos=pos,
NAMES=names,
elementMetadata=mcols,
metadata=metadata,
check=FALSE)
}
### Trusts all supplied arguments and does not validate the object.
.unsafe_new_StitchedIPos <- function(pos_runs, names=NULL, mcols=NULL,
metadata=list())
{
new2("StitchedIPos", pos_runs=pos_runs,
NAMES=names,
elementMetadata=mcols,
metadata=metadata,
check=FALSE)
}
### - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### updateObject()
###
### NOT exported but used in the GenomicRanges package.
get_IPos_version <- function(object)
{
if (.hasSlot(object, "NAMES"))
return("current")
if (class(object) != "IPos")
return(">= 2.19.4 and < 2.19.9")
return("< 2.19.4")
}
.updateObject_IPos <- function(object, ..., verbose=FALSE)
{
if (.hasSlot(object, "NAMES")) {
## 'object' was made with IRanges >= 2.19.9.
if (verbose)
message("[updateObject] ", class(object), " object is current.\n",
"[updateObject] Nothing to update.")
return(callNextMethod())
}
if (verbose)
message("[updateObject] ", class(object), " object ",
"uses internal representation from\n",
"[updateObject] IRanges ", get_IPos_version(object), ". ",
"Updating it ... ", appendLF=FALSE)
if (class(object) == "UnstitchedIPos") {
## 'object' is an UnstitchedIPos instance that was made with
## IRanges >= 2.19.4 and < 2.19.9.
object <- .unsafe_new_UnstitchedIPos(object@pos,
NULL,
object@elementMetadata,
object@metadata)
} else {
## 'object' is either an IPos instance that was made with
## IRanges < 2.19.4 or a StitchedIPos instance that was made with
## IRanges >= 2.19.4 and < 2.19.9.
object <- .unsafe_new_StitchedIPos(object@pos_runs,
NULL,
object@elementMetadata,
object@metadata)
}
if (verbose)
message("OK")
callNextMethod()
}
setMethod("updateObject", "IPos", .updateObject_IPos)
### - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### Accessors
###
setMethod("pos", "UnstitchedIPos", function(x) x@pos)
### This really should be the method for StitchedIPos objects but we define a
### method for IPos objects for backward compatibility with old IPos instances.
setMethod("pos", "IPos", function(x) unlist_as_integer(x@pos_runs))
setMethod("length", "UnstitchedIPos", function(x) length(x@pos))
### This really should be the method for StitchedIPos objects but we define a
### method for IPos objects for backward compatibility with old IPos instances.
setMethod("length", "IPos", function(x) sum(width(x@pos_runs)))
setMethod("names", "IPos", function(x) x@NAMES)
setReplaceMethod("names", "IPos",
function(x, value)
{
x@NAMES <- S4Vectors:::normarg_names(value, "IPos", length(x))
x
}
)
### No `pos<-` setter at the moment for IPos objects! Should we have it?
### - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### Collapse runs of "stitchable integer ranges"
###
### In an IntegerRanges object 'x', 2 ranges x[i] and x[i+1] are "stitchable"
### if start(x[i+1]) == end(x[i])+1. For example, in the following object:
### 1: .....xxxx.............
### 2: ...xx.................
### 3: .........xxx..........
### 4: ............xxxxxx....
### 5: ..................x...
### x[3] and x[4] are stitchable, and x[4] and x[5] are stitchable. So
### x[3], x[4], and x[5] form a run of "stitchable ranges" that will collapse
### into the following single range after stitching:
### .........xxxxxxxxxx...
### Note that x[1] and x[3] are not stitchable because they are not
### consecutive vector elements (but they would if we removed x[2]).
### stitch_IntegerRanges() below takes any IntegerRanges derivative and
### returns an IRanges object (so is NOT an endomorphism). Note that this
### transformation preserves 'sum(width(x))'.
### Also note that this is an "inter range transformation". However unlike
### range(), reduce(), gaps(), or disjoin(), its result depends on the order
### of the elements in the input vector. It's also idempotent like range(),
### reduce(), and disjoin() (gaps() is not).
### TODO: Define and export stitch() generic and method for IntegerRanges
### objects (in inter-range-methods.R).
### Maybe it would also make sense to have an isStitched() generic like we
### have isDisjoint() to provide a quick and easy way to check the state of
### the object before applying the transformation to it. In theory each
### idempotent inter range transformation could have a "state checker" so
### maybe add isReduced() too (range() probably doesn't need one).
stitch_IntegerRanges <- function(x)
{
if (length(x) == 0L)
return(IRanges())
x_start <- start(x)
x_end <- end(x)
## Find runs of stitchable elements along 'x'.
## Each run is described by the indices of its first ('run_from') and
## last ('run_to') elements in 'x'.
## The runs form a partitioning of 'x'.
new_run_idx <- which(x_start[-1L] != x_end[-length(x)] + 1L)
run_from <- c(1L, new_run_idx + 1L)
run_to <- c(new_run_idx, length(x))
IRanges(x_start[run_from], x_end[run_to])
}
### - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### Constructor
###
### 'pos' must be an integer vector with no NAs.
.make_StitchedIPos_from_pos <- function(pos, names=NULL, mcols=NULL,
metadata=list())
{
pos_runs <- as(pos, "IRanges")
.unsafe_new_StitchedIPos(pos_runs, names, mcols, metadata)
}
.from_UnstitchedIPos_to_StitchedIPos <- function(from)
{
.make_StitchedIPos_from_pos(from@pos, from@NAMES,
from@elementMetadata,
from@metadata)
}
### 'pos_runs' must be an IRanges object.
.make_UnstitchedIPos_from_pos_runs <- function(pos_runs, names=NULL, mcols=NULL,
metadata=list())
{
pos <- unlist_as_integer(pos_runs)
.unsafe_new_UnstitchedIPos(pos, names, mcols, metadata)
}
.from_StitchedIPos_to_UnstitchedIPos <- function(from)
{
.make_UnstitchedIPos_from_pos_runs(from@pos_runs, from@NAMES,
from@elementMetadata,
from@metadata)
}
### 'pos' must be an integer vector with no NAs or an IntegerRanges derivative.
### This is NOT checked!
new_UnstitchedIPos <- function(pos=integer(0))
{
if (is(pos, "UnstitchedIPos"))
return(pos)
if (is(pos, "StitchedIPos"))
return(.from_StitchedIPos_to_UnstitchedIPos(pos))
if (is.integer(pos)) {
## Treat 'pos' as a vector of single positions.
names <- names(pos)
if (!is.null(names))
names(pos) <- NULL
return(.unsafe_new_UnstitchedIPos(pos, names))
}
## 'pos' is an IntegerRanges derivative. Treat its ranges as runs of
## consecutive positions.
ans_len <- sum(width(pos)) # no more integer overflow in R >= 3.5
if (ans_len > .Machine$integer.max)
stop("too many positions in 'pos'")
.make_UnstitchedIPos_from_pos_runs(pos)
}
### 'pos' must be an integer vector with no NAs or an IntegerRanges derivative.
### This is NOT checked!
new_StitchedIPos <- function(pos=integer(0))
{
if (is(pos, "StitchedIPos"))
return(pos)
if (is(pos, "UnstitchedIPos"))
return(.from_UnstitchedIPos_to_StitchedIPos(pos))
if (is.integer(pos)) {
## Treat 'pos' as a vector of single positions.
names <- names(pos)
if (!is.null(names))
names(pos) <- NULL
return(.make_StitchedIPos_from_pos(pos, names))
}
## 'pos' is an IntegerRanges derivative. Treat its ranges as runs of
## consecutive positions.
ans_len <- sum(width(pos)) # no more integer overflow in R >= 3.5
if (ans_len > .Machine$integer.max)
stop("too many positions in 'pos'")
pos_runs <- stitch_IntegerRanges(pos)
pos_runs <- pos_runs[width(pos_runs) != 0L]
.unsafe_new_StitchedIPos(pos_runs)
}
### Returns an integer vector with no NAs or an IntegerRanges derivative.
.normarg_pos <- function(pos)
{
if (is(pos, "IntegerRanges"))
return(pos)
if (is.numeric(pos)) {
if (!is.integer(pos))
storage.mode(pos) <- "integer" # preserve the names
if (anyNA(pos))
stop("'pos' cannot contain NAs")
return(pos)
}
ans <- try(as(pos, "IRanges"), silent=TRUE)
if (inherits(ans, "try-error"))
stop("'pos' must represent positions")
ans
}
.normarg_stitch <- function(stitch, pos)
{
if (!(is.logical(stitch) && length(stitch) == 1L))
stop("'stitch' must be TRUE, FALSE, or NA")
if (!is.na(stitch))
return(stitch)
is(pos, "IntegerRanges") && !is(pos, "UnstitchedIPos")
}
### If the input object 'pos' is itself an IPos object, its metadata columns
### are propagated.
IPos <- function(pos=integer(0), names=NULL, ..., stitch=NA)
{
mcols <- DataFrame(..., check.names=FALSE)
pos <- .normarg_pos(pos)
stitch <- .normarg_stitch(stitch, pos)
if (stitch) {
ans <- new_StitchedIPos(pos)
} else {
ans <- new_UnstitchedIPos(pos)
}
if (!is.null(names))
names(ans) <- names
if (length(mcols) != 0L)
mcols(ans) <- mcols
ans
}
### - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### Coercion
###
setAs("UnstitchedIPos", "StitchedIPos", .from_UnstitchedIPos_to_StitchedIPos)
setAs("StitchedIPos", "UnstitchedIPos", .from_StitchedIPos_to_UnstitchedIPos)
.check_IntegerRanges_for_coercion_to_IPos <- function(from, to)
{
if (!all(width(from) == 1L))
stop(wmsg("all the ranges in the ", class(from), " object to ",
"coerce to ", to, " must have a width of 1"))
}
.from_IntegerRanges_to_UnstitchedIPos <- function(from)
{
.check_IntegerRanges_for_coercion_to_IPos(from, "UnstitchedIPos")
ans <- new_UnstitchedIPos(from)
names(ans) <- names(from)
mcols(ans) <- mcols(from, use.names=FALSE)
metadata(ans) <- metadata(from)
ans
}
.from_IntegerRanges_to_StitchedIPos <- function(from)
{
.check_IntegerRanges_for_coercion_to_IPos(from, "StitchedIPos")
ans <- new_StitchedIPos(from)
names(ans) <- names(from)
mcols(ans) <- mcols(from, use.names=FALSE)
metadata(ans) <- metadata(from)
ans
}
setAs("IntegerRanges", "UnstitchedIPos", .from_IntegerRanges_to_UnstitchedIPos)
setAs("IntegerRanges", "StitchedIPos", .from_IntegerRanges_to_StitchedIPos)
setAs("IntegerRanges", "IPos", .from_IntegerRanges_to_UnstitchedIPos)
setAs("ANY", "UnstitchedIPos", function(from) IPos(from, stitch=FALSE))
setAs("ANY", "StitchedIPos", function(from) IPos(from, stitch=TRUE))
setAs("ANY", "IPos", function(from) IPos(from))
### S3/S4 combo for as.data.frame.IPos
### The "as.data.frame" method for IntegerRanges objects works on an IPos
### object but returns a data.frame with identical "start" and "end" columns,
### and a "width" column filled with 1. We overwrite it to return a data.frame
### with a "pos" column instead of the "start" and "end" columns, and no
### "width" column.
.as.data.frame.IPos <- function(x, row.names=NULL, optional=FALSE)
{
if (!identical(optional, FALSE))
warning(wmsg("'optional' argument was ignored"))
ans <- data.frame(pos=pos(x), row.names=row.names, stringsAsFactors=FALSE)
x_mcols <- mcols(x, use.names=FALSE) # can be NULL!
if (!is.null(x_mcols))
ans <- cbind(ans, as.data.frame(x_mcols, optional=TRUE))
ans
}
as.data.frame.IPos <- function(x, row.names=NULL, optional=FALSE, ...)
.as.data.frame.IPos(x, row.names=NULL, optional=FALSE, ...)
setMethod("as.data.frame", "IPos", .as.data.frame.IPos)
### - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### Subsetting
###
### NOT exported but used in the GenomicRanges package.
### 'pos_runs' must be an IRanges or GRanges object or any range-based
### object as long as it supports start(), end(), width(), and is subsettable.
### 'i' must be an IntegerRanges object with no zero-width ranges.
extract_pos_runs_by_ranges <- function(pos_runs, i)
{
map <- S4Vectors:::map_ranges_to_runs(width(pos_runs),
start(i), width(i))
## Because 'i' has no zero-width ranges, 'mapped_range_span' cannot
## contain zeroes and so 'mapped_range_Ltrim' and 'mapped_range_Rtrim'
## cannot contain garbbage.
mapped_range_offset <- map[[1L]]
mapped_range_span <- map[[2L]]
mapped_range_Ltrim <- map[[3L]]
mapped_range_Rtrim <- map[[4L]]
run_idx <- S4Vectors:::fancy_mseq(mapped_range_span,
mapped_range_offset)
pos_runs <- pos_runs[run_idx]
if (length(run_idx) != 0L) {
Rtrim_idx <- cumsum(mapped_range_span)
Ltrim_idx <- c(1L, Rtrim_idx[-length(Rtrim_idx)] + 1L)
trimmed_start <- start(pos_runs)[Ltrim_idx] +
mapped_range_Ltrim
trimmed_end <- end(pos_runs)[Rtrim_idx] - mapped_range_Rtrim
start(pos_runs)[Ltrim_idx] <- trimmed_start
end(pos_runs)[Rtrim_idx] <- trimmed_end
new_len <- sum(width(pos_runs)) # no more integer overflow in R >= 3.5
if (new_len > .Machine$integer.max)
stop("subscript is too big")
}
pos_runs
}
### This really should be the method for StitchedIPos objects but we define a
### method for IPos objects for backward compatibility with old IPos instances.
setMethod("extractROWS", "IPos",
function(x, i)
{
ans <- callNextMethod()
if (is(x, "UnstitchedIPos"))
return(ans)
i <- normalizeSingleBracketSubscript(i, x, as.NSBS=TRUE)
## TODO: Maybe make this the coercion method from NSBS to
## IntegerRanges.
if (is(i, "RangesNSBS")) {
ir <- i@subscript
ir <- ir[width(ir) != 0L]
} else {
ir <- as(as.integer(i), "IRanges")
}
new_pos_runs <- extract_pos_runs_by_ranges(x@pos_runs, ir)
new_pos_runs <- stitch_IntegerRanges(new_pos_runs)
BiocGenerics:::replaceSlots(ans, pos_runs=new_pos_runs, check=FALSE)
}
)
### - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### Show
###
.IPos_summary <- function(object)
{
object_class <- classNameForDisplay(object)
object_len <- length(object)
object_mcols <- mcols(object, use.names=FALSE)
object_nmc <- if (is.null(object_mcols)) 0L else ncol(object_mcols)
paste0(object_class, " object with ", object_len, " ",
ifelse(object_len == 1L, "position", "positions"),
" and ", object_nmc, " metadata ",
ifelse(object_nmc == 1L, "column", "columns"))
}
### S3/S4 combo for summary.IPos
summary.IPos <- function(object, ...) .IPos_summary(object, ...)
setMethod("summary", "IPos", summary.IPos)
.from_IPos_to_naked_character_matrix_for_display <- function(x)
{
m <- cbind(pos=showAsCell(pos(x)))
cbind_mcols_for_display(m, x)
}
setMethod("makeNakedCharacterMatrixForDisplay", "IPos",
.from_IPos_to_naked_character_matrix_for_display
)
show_IPos <- function(x, margin="", print.classinfo=FALSE)
{
version <- get_IPos_version(x)
if (version != "current")
stop(c(wmsg("This ", class(x), " object uses internal representation ",
"from IRanges ", version, ", and so needs to be updated ",
"before it can be displayed or used. ",
"Please update it with:"),
"\n\n object <- updateObject(object, verbose=TRUE)",
"\n\n and re-serialize it."))
cat(margin, summary(x), ":\n", sep="")
## makePrettyMatrixForCompactPrinting() assumes that head() and tail()
## work on 'xx'.
xx <- as(x, "IPos")
out <- makePrettyMatrixForCompactPrinting(xx)
if (print.classinfo) {
.COL2CLASS <- c(pos="integer")
classinfo <- makeClassinfoRowForCompactPrinting(x, .COL2CLASS)
## A sanity check, but this should never happen!
stopifnot(identical(colnames(classinfo), colnames(out)))
out <- rbind(classinfo, out)
}
if (nrow(out) != 0L)
rownames(out) <- paste0(margin, " ", rownames(out))
## We set 'max' to 'length(out)' to avoid the getOption("max.print")
## limit that would typically be reached when 'showHeadLines' global
## option is set to Inf.
print(out, quote=FALSE, right=TRUE, max=length(out))
}
setMethod("show", "IPos",
function(object) show_IPos(object, print.classinfo=TRUE)
)
### - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### Concatenation
###
.concatenate_StitchedIPos_objects <-
function(x, objects=list(), use.names=TRUE, ignore.mcols=FALSE, check=TRUE)
{
objects <- S4Vectors:::prepare_objects_to_bind(x, objects)
all_objects <- c(list(x), objects)
ans_len <- sum(lengths(all_objects)) # no more integer overflow
# in R >= 3.5
if (ans_len > .Machine$integer.max)
stop("too many integer positions to concatenate")
## 1. Take care of the parallel slots
## Call method for Vector objects to concatenate all the parallel
## slots (only "elementMetadata" in the case of IPos) and stick them
## into 'ans'. Note that the resulting 'ans' can be an invalid object
## because its "elementMetadata" slot can be longer (i.e. have more rows)
## than 'ans' itself so we use 'check=FALSE' to skip validation.
ans <- callNextMethod(x, objects, use.names=use.names,
ignore.mcols=ignore.mcols,
check=FALSE)
## 2. Take care of the non-parallel slots
## Concatenate the "pos_runs" slots.
pos_runs_list <- lapply(all_objects, slot, "pos_runs")
ans_pos_runs <- stitch_IntegerRanges(
bindROWS(pos_runs_list[[1L]], pos_runs_list[-1L])
)
BiocGenerics:::replaceSlots(ans, pos_runs=ans_pos_runs,
check=check)
}
### This really should be the method for StitchedIPos objects but we define a
### method for IPos objects for backward compatibility with old IPos instances.
setMethod("bindROWS", "IPos",
function(x, objects=list(), use.names=TRUE, ignore.mcols=FALSE, check=TRUE)
{
if (is(x, "UnstitchedIPos"))
return(callNextMethod())
x <- updateObject(x, check=FALSE)
.concatenate_StitchedIPos_objects(x, objects, use.names, ignore.mcols,
check)
}
)
|