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 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705
|
#' @include zzz.R
#'
NULL
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Class definitions
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#' A Logical Map
#'
#' A simple container for storing mappings of values using logical matrices.
#' Keeps track of which values (rows) are present in which observations
#' (columns). \code{LogMap} objects can be created with \code{LogMap()};
#' queries can be performed with \code{[[} and observations can be added
#' or removed with \code{[[<-}
#'
#' @slot .Data A logical matrix with at least one row
#'
#' @exportClass LogMap
#'
#' @family logmap
#'
setClass(
Class = 'LogMap',
contains = 'matrix'
)
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Functions
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#' @rdname LogMap-class
#'
#' @param y A character vector
#'
#' @return \code{LogMap}: A new \code{LogMap} object with zero columns and
#' \code{length(x = x)} rows; rownames are set to \code{x}
#'
#' @export
#'
#' @order 1
#'
#' @examples
#' # Create a LogMap
#' map <- LogMap(letters[1:10])
#' map
#'
#' # Get the names of values in the LogMap
#' map[[NULL]]
#' rownames(map)
#'
#' # Add an observation to the LogMap
#' map[['obs']] <- c(1, 3, 7)
#' map[['entry']] <- c(2, 7, 10)
#' map
#'
#' # Get the names of observations in the LogMap
#' colnames(map)
#'
#' # Fetch an observation from the LogMap
#' map[['obs']]
#'
#' # Get the full logical matrix
#' map[[]]
#'
#' # Remove an observation from the LogMap
#' map[['obs']] <- NULL
#' map[['entry']] <- NULL
#' map
#'
LogMap <- function(y) {
if (!is.character(x = y)) {
y <- as.character(x = y)
}
return(new(
Class = 'LogMap',
.Data = matrix(nrow = length(x = y), ncol = 0, dimnames = list(y, NULL))
))
}
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Methods for Seurat-defined generics
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Methods for R-defined generics
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#' Coerce Logical Maps to Matrices
#'
#' Coerce a logical map to a matrix; this removes all
#' \link[=LogMap]{logical map} class capabilities from
#' the object and returns a base-R matrix object
#'
#' @param x A \code{\link{LogMap}} object
#' @template param-dots-ignored
#'
#' @return A base-R matrix created from \code{x}
#'
#' @method as.matrix LogMap
#' @export
#'
#' @family logmap
#'
#' @examples
#' map <- LogMap(letters[1:10])
#' map[['obs']] <- c(1, 3, 7)
#' mat <- as.matrix(map)
#' mat
#' class(mat)
#'
as.matrix.LogMap <- function(x, ...) {
return(as(object = x, Class = 'matrix'))
}
#' Drop Unused Logical Map Values
#'
#' Remove any unused values from a \link[=LogMap]{logical map}
#'
#' @template param-dots-ignored
#' @param x A \code{LogMap} object
#'
#' @return \code{x} with values not present in any
#' observation removed
#'
#' @method droplevels LogMap
#' @export
#'
#' @family logmap
#'
#' @examples
#' map <- LogMap(letters[1:10])
#' map[['obs']] <- c(1, 3, 7)
#' map[['entry']] <- c(2, 7, 10)
#'
#' # Remove unused values
#' map <- droplevels(map)
#' map
#' map[[]]
#'
droplevels.LogMap <- function(x, ...) {
fidx <- which(x = apply(
X = x,
MARGIN = 1L,
FUN = function(row) {
return(all(vapply(
X = row,
FUN = isFALSE,
FUN.VALUE = logical(length = 1L)
)))
}
))
if (length(x = fidx)) {
x <- as(object = x[-fidx, , drop = FALSE], Class = 'LogMap')
}
validObject(object = x)
return(x)
}
#' Find Common Logical Map Values
#'
#' Identify values in a \link[=LogMap]{logical map} that are
#' common to every observation
#'
#' @inheritParams droplevels.LogMap
#' @param y Ignored
#'
#' @return The values of \code{x} that are present in \strong{every} observation
#'
#' @method intersect LogMap
#' @export
#'
#' @family logmap
#'
#' @examples
#' map <- LogMap(letters[1:10])
#' map[['obs']] <- c(1, 3, 7)
#' map[['entry']] <- c(2, 7, 10)
#'
#' # Identify values that are present in every observation
#' intersect(map)
#'
intersect.LogMap <- function(x, y = missing_arg(), ...) {
if (!is_missing(x = y)) {
abort(message = "'y' must not be provided")
}
idx <- which(x = apply(X = x, MARGIN = 1L, FUN = all))
return(rownames(x = x)[idx])
}
#' Find Observations by Value
#'
#' Identify the observations that contain a specific
#' value in a \link[=LogMap]{logical map}
#'
#' @template param-dots-ignored
#' @param object A \code{\link{LogMap}} object
#' @param values A vector of values to find observations for
#' @param select Observation selection method; choose from:
#' \itemize{
#' \item \dQuote{\code{first}}: the first observation the value is found in
#' \item \dQuote{\code{last}}: the last observation the value is found in
#' \item \dQuote{\code{common}}: the first most-common observation the value
#' is found in; most-common is determined by the observation that contains
#' the most of the values requested
#' \item \dQuote{\code{all}}: all observations the value is found in
#' }
#' @param simplify Simplify the resulting list to a vector
#'
#' @return \code{labels}: A list, or vector if \code{simplify} is \code{TRUE},
#' of all values and the observations they're found in, according
#' to the value of \code{select}
#'
#' @method labels LogMap
#' @export
#'
#' @family logmap
#'
#' @examples
#' map <- LogMap(letters[1:10])
#' map[['obs']] <- c(1, 3, 7)
#' map[['entry']] <- c(2, 7, 10)
#'
#' # Find observations for a set of values
#' labels(map, c('a', 'b', 'g'))
#'
labels.LogMap <- function(
object,
values,
select = c('first', 'last', 'common', 'all'),
simplify = TRUE,
...
) {
select <- select[1L]
select <- match.arg(arg = select)
values <- intersect(x = values, y = rownames(x = object))
mat <- as.matrix(object)
cols <- colnames(object)
idx <- match(values, rownames(object))
obs <- vector("list", length(values))
names(obs) <- values
for (i in seq_along(idx)) {
id <- idx[i]
vals <- cols[mat[id, , drop = FALSE]]
if (length(vals) > 0) {
obs[[i]] <- vals
}
}
obs <- Filter(f = length, x = obs)
obs <- switch(
EXPR = select,
'first' = lapply(X = obs, FUN = '[[', 1L),
'last' = lapply(
X = obs,
FUN = function(x) {
return(x[[length(x = x)]])
}
),
common = {
counts <- table(unlist(x = obs))
tmp <- obs
obs <- vector(mode = 'character', length = length(x = tmp))
names(x = obs) <- names(x = tmp)
for (i in seq_along(along.with = obs)) {
obs[i] <- names(x = which.max(
x = counts[names(x = counts) %in% tmp[[i]]]
))
}
obs
},
obs
)
if (isTRUE(x = simplify)) {
tmp <- obs
obs <- unlist(x = tmp)
names(x = obs) <- make.unique(names = rep.int(
x = names(x = tmp),
times = vapply(X = tmp, FUN = length, FUN.VALUE = numeric(length = 1L))
))
}
return(obs)
}
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Internal
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# S4 methods
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#' Matrix-like Subsetting for \link[=LogMap]{Logical Maps}
#'
#' @inheritParams base::`[`
#' @inheritParams LogMap-class
#' @param i,j Vectors of values (\code{i}) and observations (\code{j}) to pull
#' from \code{x}
#' @template param-dots-method
#'
#' @note \code{i} is not reordable; passing a different order for \code{i}
#' will return a subset with rows in the same order as \code{x}
#'
#' @name [,LogMap
#' @rdname sub-LogMap-method
#'
#' @keywords internal
#'
#' @examples
#' map <- LogMap(letters[1:10])
#' map[['obs']] <- c(1, 3, 7)
#' map[['entry']] <- c(2, 7, 10)
#'
#' map[]
#' map[1:5, 2L]
#' map[c("b", "c", "f"), "obs"]
#'
#' # Pass `drop = TRUE` to cast to `matrix`
#' map[1:3, , drop = TRUE]
#'
#' # Note that `i` is non-reordable
#' rownames(map)[1:3]
#' map[c("b", "c", "a"), , drop = TRUE]
#'
NULL
#' @rdname sub-LogMap-method
#'
setMethod(
f = '[',
signature = c(x = 'LogMap', i = 'missing', j = 'missing'),
definition = function(x, i, j, ..., drop = FALSE) {
return(x)
}
)
#' @rdname sub-LogMap-method
#'
setMethod(
f = '[',
signature = c(x = 'LogMap', i = 'character', j = 'character'),
definition = function(x, i, j, ..., drop = FALSE) {
i <- i[MatchCells(new = i, orig = rownames(x = x), ordered = TRUE)]
x <- as.matrix(x = x)[i, j, drop = drop]
if (!isTRUE(x = drop)) {
x <- as(object = x, Class = 'LogMap')
}
return(x)
}
)
#' @rdname sub-LogMap-method
#'
setMethod(
f = '[',
signature = c(x = 'LogMap', i = 'character', j = 'missing'),
definition = function(x, i, j, ..., drop = FALSE) {
i <- i[MatchCells(new = i, orig = rownames(x = x), ordered = TRUE)]
x <- as.matrix(x = x)[i, , drop = drop]
if (!isTRUE(x = drop)) {
x <- as(object = x, Class = 'LogMap')
}
return(x)
}
)
#' @rdname sub-LogMap-method
#'
setMethod(
f = '[',
signature = c(x = 'LogMap', i = 'missing', j = 'character'),
definition = function(x, i, j, ..., drop = FALSE) {
x <- as.matrix(x = x)[, j, drop = drop]
if (!isTRUE(x = drop)) {
x <- as(object = x, Class = 'LogMap')
}
return(x)
}
)
#' @rdname sub-LogMap-method
#'
setMethod(
f = '[',
signature = c(x = 'LogMap', i = 'numeric', j = 'missing'),
definition = function(x, i, j, ..., drop = FALSE) {
stopifnot(is_bare_integerish(x = i))
if (is.unsorted(x = i)) {
i <- sort(x = i)
}
i <- rownames(x = x)[i]
return(callNextMethod(x, i, ..., drop = drop))
}
)
#' @rdname sub-LogMap-method
#'
setMethod(
f = '[',
signature = c(x = 'LogMap', i = 'missing', j = 'numeric'),
definition = function(x, i, j, ..., drop = FALSE) {
stopifnot(is_bare_integerish(x = j))
j <- colnames(x = x)[j]
return(callNextMethod(x, , j, ..., drop = drop))
}
)
#' @rdname sub-LogMap-method
#'
setMethod(
f = '[',
signature = c(x = 'LogMap', i = 'numeric', j = 'numeric'),
definition = function(x, i, j, ..., drop = FALSE) {
stopifnot(is_bare_integerish(x = i), is_bare_integerish(x = j))
if (is.unsorted(x = i)) {
i <- sort(x = i)
}
i <- rownames(x = x)[i]
j <- colnames(x = x)[j]
return(callNextMethod(x, i, j, ..., drop = drop))
}
)
#' @rdname LogMap-class
#'
#' @param x A \code{LogMap} object
#' @param i A character vector of length 1, or \code{NULL}
#' @param j Not used
#' @param ... Ignored
#'
#' @return \code{[[}: if \code{i} is a character vector, the rownames that are
#' mapped to \code{i}; otherwise the rownames of \code{x}
#'
#' @export
#'
#' @order 2
#'
setMethod(
f = '[[',
signature = c(x = 'LogMap', i = 'character', j = 'missing'),
definition = function(x, i, ...) {
i <- i[1]
i <- match.arg(arg = i, choices = colnames(x = x))
return(rownames(x = x)[x[, i, drop = TRUE]])
}
)
#' \code{\link{LogMap}} Interaction Methods
#'
#' Additional methods for using \code{[[} with \code{\link{LogMap}} objects
#'
#' @inheritParams LogMap
#' @param i An integer or numeric vector of length 1
#'
#' @return The rownames that are mapped to \code{i}
#'
#' @rdname sub-sub-LogMap-internal-method
#'
#' @keywords internal
#'
setMethod(
f = '[[',
signature = c(x = 'LogMap', i = 'integer', j = 'missing'),
definition = function(x, i, ...) {
return(x[[colnames(x = x)[i]]])
}
)
#' @rdname LogMap-class
#'
#' @export
#'
#' @order 3
#'
setMethod(
f = '[[',
signature = c(x = 'LogMap', i = 'missing', j = 'missing'),
definition = function(x, ...) {
return(slot(object = x, name = '.Data'))
}
)
#' @rdname sub-sub-LogMap-internal-method
#'
setMethod(
f = '[[',
signature = c(x = 'LogMap', i = 'numeric', j = 'missing'),
definition = function(x, i, ...) {
return(x[[as.integer(x = i)]])
}
)
#' @rdname LogMap-class
#'
#' @export
#'
#' @order 4
#'
setMethod(
f = '[[',
signature = c(x = 'LogMap', i = 'NULL', j = 'missing'),
definition = function(x, i, ...) {
return(rownames(x = x))
}
)
#' @rdname LogMap-class
#'
#' @param value A character or integer vector of values to record in the map
#' for \code{i}, or \code{NULL} to remove the record for \code{i}
#'
#' @return \code{[[<-}: If \code{value} is \code{NULL}, then \code{x} without
#' the observations for \code{i}; otherwise, \code{x} with a new column for
#' \code{i} recording a \code{TRUE} for all values present in \code{value}
#'
#' @export
#'
#' @order 5
#'
setMethod(
f = '[[<-',
signature = c(
x = 'LogMap',
i = 'character',
j = 'missing',
value = 'character'
),
definition = function(x, i, ..., value) {
value <- MatchCells(new = rownames(x = x), orig = value)
x[[i]] <- value
return(x)
}
)
#' @rdname LogMap-class
#'
#' @export
#'
#' @order 6
#'
setMethod(
f = '[[<-',
signature = c(
x = 'LogMap',
i = 'character',
j = 'missing',
value = 'integer'
),
definition = function(x, i, ..., value) {
if (i %in% colnames(x = x)) {
x[[i]] <- NULL
}
value <- MatchCells(new = value, rownames(x = x))
mat <- slot(object = x, name = '.Data')
if (length(x = value)) {
mat <- cbind(
mat,
matrix(data = FALSE, nrow = nrow(x = x), dimnames = list(NULL, i))
)
mat[value, i] <- TRUE
}
slot(object = x, name = '.Data') <- mat
validObject(object = x)
return(x)
}
)
#' @rdname LogMap-class
#'
#' @export
#'
#' @order 7
#'
setMethod(
f = '[[<-',
signature = c(x = 'LogMap', i = 'character', j = 'missing', value = 'NULL'),
definition = function(x, i, ..., value) {
mat <- slot(object = x, name = '.Data')
for (name in i) {
idx <- which(x = colnames(x = mat) == name)
if (length(x = idx)) {
mat <- mat[, -idx, drop = FALSE]
}
}
slot(object = x, name = '.Data') <- mat
validObject(object = x)
return(x)
}
)
#' @rdname LogMap-class
#'
#' @export
#'
#' @order 8
#'
setMethod(
f = '[[<-',
signature = c(
x = 'LogMap',
i = 'character',
j = 'missing',
value = 'numeric'
),
definition = function(x, i, ..., value) {
value <- as.integer(x = value)
x[[i]] <- value
return(x)
}
)
#' \code{\link{LogMap}} Object Overview
#'
#' Overview of a \code{\link{LogMap}} object
#'
#' @param object A \code{\link{LogMap}} object
#'
#' @template return-show
#'
#' @concept logmap
#'
setMethod(
f = 'show',
signature = 'LogMap',
definition = function(object) {
cat(
"A logical map for",
nrow(x = object),
"values across",
ncol(x = object),
"observations"
)
return(invisible(x = NULL))
}
)
#' Logical Map Validity
#'
#' @templateVar cls LogMap
#' @template desc-validity
#'
#' @section Data Validation:
#' Logical maps must be a logical matrix containing only TRUE or FALSE values
#'
#' @section Value Validation:
#' All values must be named within the rownames of the object. Duplicate or
#' empty (\code{""}) values are not allowed
#'
#' @section Observation Validation:
#' All observations must be named within the column names of the object.
#' Duplicate or empty (\code{""}) observations are not allowed
#'
#' @name LogMap-validity
#'
#' @family logmap
#' @seealso \code{\link[methods]{validObject}}
#'
#' @examples
#' map <- LogMap(letters[1:10])
#' map[['obs']] <- c(1, 3, 7)
#' map[['entry']] <- c(2, 7, 10)
#' validObject(map)
#'
setValidity(
Class = 'LogMap',
method = function(object) {
if (isFALSE(x = getOption(x = "Seurat.object.validate", default = TRUE))) {
warn(
message = paste("Not validating", class(x = object)[1L], "objects"),
class = 'validationWarning'
)
return(TRUE)
}
valid <- NULL
# Ensure we have a logical matrix
if (!is.logical(x = object)) {
valid <- c(valid, "The map must be a logical matrix")
}
if (any(is.na(x = object))) {
valid <- c(valid, "The may may not contain NAs")
}
# Check rownames
if (is.null(x = rownames(x = object))) {
valid <- c(valid, "Rownames must be supplied")
}
if (any(!nzchar(x = rownames(x = object)))) {
valid <- c(valid, "Rownames cannot be empty strings")
}
if (anyDuplicated(x = rownames(x = object))) {
valid <- c(valid, "Duplicate rownames not allowed")
}
# Check colnames
if (!is.null(x = colnames(x = object))) {
if (any(!nzchar(x = colnames(x = object)))) {
valid <- c(valid, "Columnn names cannot be empty strings")
}
if (anyDuplicated(x = colnames(x = object))) {
valid <- c(valid, "Duplicate colnames not allowed")
}
} else if (ncol(x = object)) {
valid <- c(valid, "Colnames must be supplied")
}
return(valid %||% TRUE)
}
)
|