Skip to content

Commit

Permalink
Merge pull request #59 from jiajic/dev
Browse files Browse the repository at this point in the history
Add: basic DT-based object subsetting
  • Loading branch information
jiajic authored Oct 17, 2023
2 parents f086bf8 + 57514fc commit 505a1f6
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 11 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: GiottoClass
Title: Giotto Suite object definitions and framework
Version: 0.0.0.9006
Version: 0.0.0.9007
Authors@R: c(
person("Ruben", "Dries", email = "rubendries@gmail.com",
role = c("aut", "cre")),
Expand Down
20 changes: 15 additions & 5 deletions R/classes.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,27 @@ setClassUnion('gIndex', c('numeric', 'logical', 'character'))





# VIRTUAL CLASSES ####


# ** giottoSubobject Class ####
#' @keywords internal
#' @noRd
setClass(
'giottoSubobject',
contains = 'VIRTUAL')

# ** gdtData Class ####
#' @description
#' umbrella class for referring to Giotto's normal data.table-based slots for
#' extraction purposes
#' @keywords internal
#' @noRd
setClass(
'gdtData',
contains = 'VIRTUAL'
)


# ** nameData Class ####
#' @keywords internal
Expand Down Expand Up @@ -76,7 +86,7 @@ setClass("exprData",
#' @keywords internal
#' @noRd
setClass("coordDataDT",
contains = "VIRTUAL",
contains = c("VIRTUAL", "gdtData"),
slots = list(coordinates = "data.table"),
prototype = prototype(coordinates = data.table::data.table())
)
Expand All @@ -99,7 +109,7 @@ setClass("coordDataDT",
#' @keywords internal
#' @noRd
setClass("metaData",
contains = "VIRTUAL",
contains = c("VIRTUAL", "gdtData"),
slots = list(
metaDT = "data.table",
col_desc = "character"
Expand All @@ -119,7 +129,7 @@ setClass("metaData",
#' @keywords internal
#' @noRd
setClass("enrData",
contains = "VIRTUAL",
contains = c("VIRTUAL", "gdtData"),
slots = list(
method = "character",
enrichDT = "nullOrDatatable"
Expand Down
58 changes: 56 additions & 2 deletions R/methods-extract.R
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,61 @@ setMethod('$<-', signature(x = 'terraVectData'),

# [ S4 access generic ####

## * gdtData ####

# Make it so that i and j subsets can be written independently
#' @rdname extract-methods
#' @export
setMethod('[', signature(x = 'gdtData', i = 'ANY', j = 'ANY', drop = 'missing'),
function(x, i, j) {
x = x[i = i]
x = x[j = j]
x
})

#' @rdname extract-methods
#' @export
setMethod('[', signature(x = 'gdtData', i = 'logical', j = 'missing', drop = 'missing'),
function(x, i, j) {
x_nrow = nrow(x)
i_len = length(i)

if(i_len > x_nrow) {
stop('logical subset vector is longer than number of rows',
call. = FALSE)
}
if (i_len < x_nrow) { # handle recycling
i = rep(i, length.out = x_nrow)
}

x[] = x[][i]
x
})

#' @rdname extract-methods
#' @export
setMethod('[', signature(x = 'gdtData', i = 'character', j = 'missing', drop = 'missing'),
function(x, i, j) {

# only appropriate for objects where the spatIDs are in the same
# order and number of repeats as the contained data.table
x_colnames = colnames(x[])
ids = if('cell_ID' %in% x_colnames) spatIDs(x)
else if('feat_ID' %in% x_colnames) featIDs(x)
else stop(wrap_txt(
'Subset object does not contain either cell_ID or feat_ID column'
))

# make idx vector
idx = match(i, ids)
x[] = x[][idx]
x
})





## * coordDataDT ####


Expand All @@ -104,7 +159,6 @@ setMethod('[', signature(x = 'coordDataDT', i = 'missing', j = 'ANY', drop = 'mi
x
})


# setMethod('[', signature(x = 'giotto', i = 'character', j = 'missing', drop = 'missing'),
# function(x, i, spat_unit = NULL, feat_type = NULL, name = NULL) {
#
Expand Down Expand Up @@ -184,7 +238,7 @@ setReplaceMethod('[', signature(x = 'coordDataDT', i = 'missing', j = 'missing',

#' @rdname extract-methods
#' @export
setMethod('[', signature(x = 'giottoPoints', i = "ANY", j = "missing", drop = "missing"),
setMethod('[', signature(x = 'giottoPoints', i = "gIndex", j = "missing", drop = "missing"),
function(x, i, j) {
x@spatVector = x@spatVector[i]
x@unique_ID_cache = featIDs(x, uniques = TRUE, use_cache = FALSE)
Expand Down
14 changes: 11 additions & 3 deletions man/extract-methods.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 505a1f6

Please sign in to comment.