Skip to content

Commit

Permalink
Merge pull request #93 from drieslab/dev
Browse files Browse the repository at this point in the history
Fix overlapToMatrix()
  • Loading branch information
jiajic authored Nov 19, 2023
2 parents 912f3c1 + 1db5c1e commit c6b3bd3
Show file tree
Hide file tree
Showing 10 changed files with 766 additions and 759 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Imports:
terra (>= 1.7-39)
Suggests:
Biobase,
chihaya,
DelayedArray,
exactextractr,
geometry,
Expand Down
19 changes: 13 additions & 6 deletions R/aggregate.R
Original file line number Diff line number Diff line change
Expand Up @@ -1060,8 +1060,8 @@ setMethod(
'overlapToMatrix', signature('giotto'), function(
x,
name = 'raw',
poly_info = 'cell',
feat_info = 'rna',
poly_info = NULL,
feat_info = NULL,
type = c('point', 'intensity'),
count_info_column = NULL,
aggr_function = "sum",
Expand All @@ -1070,26 +1070,33 @@ setMethod(
...
)
{

type = match.arg(type, choices = c('point', 'intensity'))
checkmate::assert_character(name, len = 1L)
checkmate::assert_character(poly_info, len = 1L)
checkmate::assert_character(feat_info, len = 1L)
if (!is.null(count_info_column)) {
checkmate::assert_character(count_info_column, len = 1L)
}
checkmate::assert_logical(return_gobject)

poly_info <- set_default_spat_unit(gobject = x,
spat_unit = poly_info)
feat_info <- set_default_feat_type(gobject = x,
spat_unit = poly_info,
feat_type = feat_info)

# get data
gpoly = getPolygonInfo(
gobject = x,
polygon_name = poly_info,
return_giottoPolygon = TRUE
return_giottoPolygon = TRUE,
verbose = verbose
)

o2m_args <- list(
x = gpoly,
col_names = spatIDs(x, spat_unit = poly_info),
row_names = featIDs(x, feat_type = feat_info),
feat_info = feat_info,
count_info_column = count_info_column,
aggr_function = aggr_function,
# output = 'Matrix', # Do not specify here. methods must return
Expand Down Expand Up @@ -1237,7 +1244,7 @@ setMethod(
# 2. Perform aggregation to counts DT
if(!is.null(count_info_column)) { # if there is a counts col

if(!count_info_column %in% colnames(dtoverlap)) {
if (!count_info_column %in% colnames(dtoverlap)) {
stop('count_info_column ', count_info_column, ' does not exist')
}

Expand Down
4 changes: 2 additions & 2 deletions R/generics.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ NULL

# Giotto Object Settings ####
# Methods and documentations found in methods-IDs.R
setGeneric('spatIDs', function(x, spat_unit, ...) standardGeneric('spatIDs'))
setGeneric('featIDs', function(x, feat_type, ...) standardGeneric('featIDs'))
setGeneric('spatIDs', function(x, ...) standardGeneric('spatIDs'))
setGeneric('featIDs', function(x, ...) standardGeneric('featIDs'))

## instructions ####
# Methods and documentations found in methods-instructions.R
Expand Down
50 changes: 20 additions & 30 deletions R/methods-IDs.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,74 +27,69 @@ NULL

# spatIDs ####


#' @rdname spatIDs-generic
#' @export
setMethod('spatIDs', signature(x = 'giotto', spat_unit = 'missing'),
function(x, ...) {
as.character(get_cell_id(gobject = x, ...))
})
#' @rdname spatIDs-generic
#' @export
setMethod('spatIDs', signature(x = 'giotto', spat_unit = 'character'),
function(x, spat_unit, ...) {
setMethod('spatIDs', signature(x = 'giotto'),
function(x, spat_unit = NULL, ...) {
as.character(get_cell_id(gobject = x, spat_unit, ...))
})
#' @rdname spatIDs-generic
#' @export
setMethod('spatIDs', signature(x = c('exprObj'), spat_unit = 'missing'),
setMethod('spatIDs', signature(x = c('exprObj')),
function(x, ...) {
as.character(colnames(x[]))
})
#' @rdname spatIDs-generic
#' @export
setMethod('spatIDs', signature(x = c('spatLocsObj'), spat_unit = 'missing'),
setMethod('spatIDs', signature(x = c('spatLocsObj')),
function(x, ...) {
as.character(x[]$cell_ID)
})
#' @rdname spatIDs-generic
#' @export
setMethod('spatIDs', signature(x = c('cellMetaObj'), spat_unit = 'missing'),
setMethod('spatIDs', signature(x = c('cellMetaObj')),
function(x, ...) {
as.character(x[]$cell_ID)
})
#' @rdname spatIDs-generic
#' @export
setMethod('spatIDs', signature(x = c('spatialNetworkObj'), spat_unit = 'missing'),
setMethod('spatIDs', signature(x = c('spatialNetworkObj')),
function(x, ...) {
as.character(unique(c(x[]$from, x[]$to)))
})
#' @rdname spatIDs-generic
#' @export
setMethod('spatIDs', signature(x = 'dimObj', spat_unit = 'missing'),
setMethod('spatIDs', signature(x = 'dimObj'),
function(x, ...) {
as.character(rownames(x@coordinates))
})
#' @rdname spatIDs-generic
#' @param use_cache use cached IDs if available (gpoly and gpoints only)
#' @param uniques return unique ID values only (currently gpoly and gpoints only)
#' @export
setMethod('spatIDs', signature(x = 'giottoPolygon', spat_unit = 'missing'),
setMethod('spatIDs', signature(x = 'giottoPolygon'),
function(x, use_cache = TRUE, uniques = TRUE, ...) {
if (!all(is.na(x@unique_ID_cache)) &&
isTRUE(use_cache) &&
isTRUE(uniques)) {
return(as.character(x@unique_ID_cache))
}

# getting as list first is more performant
out = as.character(terra::as.list(x@spatVector)$poly_ID)
if (isTRUE(uniques)) out = unique(out)
return(out)
})
#' @rdname spatIDs-generic
#' @export
setMethod('spatIDs', signature(x = 'spatEnrObj', spat_unit = 'missing'),
setMethod('spatIDs', signature(x = 'spatEnrObj'),
function(x, ...) {
as.character(x@enrichDT$cell_ID)
})
#' @rdname spatIDs-generic
#' @export
setMethod('spatIDs', signature(x = 'nnNetObj', spat_unit = 'missing'),
setMethod('spatIDs', signature(x = 'nnNetObj'),
function(x, ...) {
as.character(unique(names(igraph::V(x@igraph))))
})
Expand All @@ -106,48 +101,43 @@ setMethod('spatIDs', signature(x = 'nnNetObj', spat_unit = 'missing'),

# featIDs ####


#' @rdname spatIDs-generic
#' @export
setMethod('featIDs', signature(x = 'giotto', feat_type = 'missing'),
function(x, ...) {
as.character(get_feat_id(gobject = x, ...))
})
#' @rdname spatIDs-generic
#' @export
setMethod('featIDs', signature(x = 'giotto', feat_type = 'character'),
function(x, feat_type, ...) {
setMethod('featIDs', signature(x = 'giotto'),
function(x, feat_type = NULL, ...) {
as.character(get_feat_id(gobject = x, feat_type, ...))
})
#' @rdname spatIDs-generic
#' @export
setMethod('featIDs', signature(x = 'exprObj', feat_type = 'missing'),
setMethod('featIDs', signature(x = 'exprObj'),
function(x, ...) {
as.character(rownames(x[]))
})
#' @rdname spatIDs-generic
#' @export
setMethod('featIDs', signature(x = 'featMetaObj', feat_type = 'missing'),
setMethod('featIDs', signature(x = 'featMetaObj'),
function(x, ...) {
as.character(x[]$feat_ID)
})
#' @rdname spatIDs-generic
#' @export
setMethod('featIDs', signature(x = 'giottoPoints', feat_type = 'missing'),
setMethod('featIDs', signature(x = 'giottoPoints'),
function(x, use_cache = TRUE, uniques = TRUE, ...) {
if (!all(is.na(x@unique_ID_cache)) &&
isTRUE(use_cache) &&
isTRUE(uniques)) {
return(as.character(x@unique_ID_cache))
}

# getting as list is more performant than directly using `$`
out = as.character(terra::as.list(x@spatVector)$feat_ID)
if (isTRUE(uniques)) out = unique(out)
return(out)
})
#' @rdname spatIDs-generic
#' @export
setMethod('featIDs', signature(x = 'spatEnrObj', feat_type = 'missing'),
setMethod('featIDs', signature(x = 'spatEnrObj'),
function(x, ...) {
as.character(colnames(x@enrichDT[, -'cell_ID']))
})
Expand Down
3 changes: 2 additions & 1 deletion R/methods-ext.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ setMethod('ext<-', signature(x = 'giottoPolygon', value = 'SpatExtent'), functio
#' @rdname ext-generic
#' @export
setMethod('ext<-', signature(x = 'giottoLargeImage', value = 'SpatExtent'), function(x, value) {
terra::ext(x@raster_object) = value
terra::ext(x@raster_object) <- value
x@extent <- value
x
})

Expand Down
Loading

0 comments on commit c6b3bd3

Please sign in to comment.