From fd94b09f960d5c0b5f8938e812e0b5d5683c00a9 Mon Sep 17 00:00:00 2001 From: jiajic <72078254+jiajic@users.noreply.github.com> Date: Sun, 19 Nov 2023 08:11:07 -0500 Subject: [PATCH] Fix overlapToMatrix() - Add: some verbose params to polygon accessors - Fix: `overlapToMatrix()` `giotto` method - Remove: `spat_unit` and `feat_type` params from `spatIDs` and `featIDs` signatures. (They are only used with Giotto objects, but will no longer be autocompleted. Otherwise, passing NULL to spat_unit or feat_type param is impossible.) --- R/aggregate.R | 19 +++++++++---- R/generics.R | 4 +-- R/methods-IDs.R | 50 +++++++++++++-------------------- R/methods-ext.R | 3 +- R/slot_accessors.R | 24 +++++++++++----- man/getPolygonInfo.Rd | 5 +++- man/get_polygon_info.Rd | 5 +++- man/overlapToMatrix.Rd | 4 +-- man/spatIDs-generic.Rd | 62 +++++++++++++++++++---------------------- 9 files changed, 92 insertions(+), 84 deletions(-) diff --git a/R/aggregate.R b/R/aggregate.R index b171a65c..d72db1c4 100644 --- a/R/aggregate.R +++ b/R/aggregate.R @@ -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", @@ -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 @@ -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') } diff --git a/R/generics.R b/R/generics.R index de1f0b0f..352ab493 100644 --- a/R/generics.R +++ b/R/generics.R @@ -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 diff --git a/R/methods-IDs.R b/R/methods-IDs.R index 99c3fe67..ed8e2b0a 100644 --- a/R/methods-IDs.R +++ b/R/methods-IDs.R @@ -27,45 +27,40 @@ 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)) }) @@ -73,14 +68,14 @@ setMethod('spatIDs', signature(x = 'dimObj', spat_unit = 'missing'), #' @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) @@ -88,13 +83,13 @@ setMethod('spatIDs', signature(x = 'giottoPolygon', spat_unit = 'missing'), }) #' @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)))) }) @@ -106,40 +101,35 @@ 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) @@ -147,7 +137,7 @@ setMethod('featIDs', signature(x = 'giottoPoints', feat_type = 'missing'), }) #' @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'])) }) diff --git a/R/methods-ext.R b/R/methods-ext.R index a7121eb8..2f05f482 100644 --- a/R/methods-ext.R +++ b/R/methods-ext.R @@ -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 }) diff --git a/R/slot_accessors.R b/R/slot_accessors.R index 25f37044..f72287c9 100644 --- a/R/slot_accessors.R +++ b/R/slot_accessors.R @@ -3724,13 +3724,15 @@ setSpatialGrid = function(gobject, #' @param polygon_name name of polygons. Default "cell" #' @param polygon_overlap include polygon overlap information #' @param return_giottoPolygon (Defaults to FALSE) Return as giottoPolygon S4 object +#' @param verbose be verbose #' @family polygon info data accessor functions #' @family functions to get data from giotto object #' @export get_polygon_info = function(gobject, polygon_name = NULL, polygon_overlap = NULL, - return_giottoPolygon = FALSE) { + return_giottoPolygon = FALSE, + verbose = TRUE) { deprecate_soft('3.3.0', what = 'get_polygon_info()', with = 'getPolygonInfo()') @@ -3743,7 +3745,10 @@ get_polygon_info = function(gobject, polygon_name = 'cell' # Default to 'cell' as polygon_name if available } else { polygon_name = potential_names[1] # Select 1st available name if 'cell' is missing - message('No polygon information named "cell" discovered.\n selecting first available ("',polygon_name,'")') + if (isTRUE(verbose)) { + wrap_msg('No polygon information named "cell" discovered. + Selecting first available ("',polygon_name,'")') + } } } @@ -3780,23 +3785,28 @@ get_polygon_info = function(gobject, #' @param polygon_name name of polygons. Default is "cell" #' @param polygon_overlap include polygon overlap information #' @param return_giottoPolygon (Defaults to FALSE) Return as giottoPolygon S4 object +#' @param verbose be verbose #' @family polygon info data accessor functions #' @family functions to get data from giotto object #' @export getPolygonInfo = function(gobject = NULL, polygon_name = NULL, polygon_overlap = NULL, - return_giottoPolygon = FALSE) { + return_giottoPolygon = FALSE, + verbose = TRUE) { if (!inherits(gobject, 'giotto')){ wrap_msg("Unable to get polygon spatVector from non-Giotto object.") stop(wrap_txt("Please provide a Giotto object to the gobject argument.", errWidth = TRUE)) } - poly_info = get_polygon_info(gobject = gobject, - polygon_name = polygon_name, - polygon_overlap = polygon_overlap, - return_giottoPolygon = return_giottoPolygon) + poly_info = get_polygon_info( + gobject = gobject, + polygon_name = polygon_name, + polygon_overlap = polygon_overlap, + return_giottoPolygon = return_giottoPolygon, + verbose = verbose + ) return (poly_info) } diff --git a/man/getPolygonInfo.Rd b/man/getPolygonInfo.Rd index c04ecc6c..0f81017e 100644 --- a/man/getPolygonInfo.Rd +++ b/man/getPolygonInfo.Rd @@ -8,7 +8,8 @@ getPolygonInfo( gobject = NULL, polygon_name = NULL, polygon_overlap = NULL, - return_giottoPolygon = FALSE + return_giottoPolygon = FALSE, + verbose = TRUE ) } \arguments{ @@ -19,6 +20,8 @@ getPolygonInfo( \item{polygon_overlap}{include polygon overlap information} \item{return_giottoPolygon}{(Defaults to FALSE) Return as giottoPolygon S4 object} + +\item{verbose}{be verbose} } \description{ Get giotto polygon spatVector diff --git a/man/get_polygon_info.Rd b/man/get_polygon_info.Rd index 18cc23c5..dfde3838 100644 --- a/man/get_polygon_info.Rd +++ b/man/get_polygon_info.Rd @@ -8,7 +8,8 @@ get_polygon_info( gobject, polygon_name = NULL, polygon_overlap = NULL, - return_giottoPolygon = FALSE + return_giottoPolygon = FALSE, + verbose = TRUE ) } \arguments{ @@ -19,6 +20,8 @@ get_polygon_info( \item{polygon_overlap}{include polygon overlap information} \item{return_giottoPolygon}{(Defaults to FALSE) Return as giottoPolygon S4 object} + +\item{verbose}{be verbose} } \description{ Get giotto polygon spatVector diff --git a/man/overlapToMatrix.Rd b/man/overlapToMatrix.Rd index a8d43e49..d10631f7 100644 --- a/man/overlapToMatrix.Rd +++ b/man/overlapToMatrix.Rd @@ -11,8 +11,8 @@ \S4method{overlapToMatrix}{giotto}( 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", diff --git a/man/spatIDs-generic.Rd b/man/spatIDs-generic.Rd index e16ea62c..f2e64256 100644 --- a/man/spatIDs-generic.Rd +++ b/man/spatIDs-generic.Rd @@ -4,22 +4,20 @@ \alias{spatIDs-generic} \alias{spatIDs} \alias{featIDs} -\alias{spatIDs,giotto,missing-method} -\alias{spatIDs,giotto,character-method} -\alias{spatIDs,exprObj,missing-method} -\alias{spatIDs,spatLocsObj,missing-method} -\alias{spatIDs,cellMetaObj,missing-method} -\alias{spatIDs,spatialNetworkObj,missing-method} -\alias{spatIDs,dimObj,missing-method} -\alias{spatIDs,giottoPolygon,missing-method} -\alias{spatIDs,spatEnrObj,missing-method} -\alias{spatIDs,nnNetObj,missing-method} -\alias{featIDs,giotto,missing-method} -\alias{featIDs,giotto,character-method} -\alias{featIDs,exprObj,missing-method} -\alias{featIDs,featMetaObj,missing-method} -\alias{featIDs,giottoPoints,missing-method} -\alias{featIDs,spatEnrObj,missing-method} +\alias{spatIDs,giotto-method} +\alias{spatIDs,exprObj-method} +\alias{spatIDs,spatLocsObj-method} +\alias{spatIDs,cellMetaObj-method} +\alias{spatIDs,spatialNetworkObj-method} +\alias{spatIDs,dimObj-method} +\alias{spatIDs,giottoPolygon-method} +\alias{spatIDs,spatEnrObj-method} +\alias{spatIDs,nnNetObj-method} +\alias{featIDs,giotto-method} +\alias{featIDs,exprObj-method} +\alias{featIDs,featMetaObj-method} +\alias{featIDs,giottoPoints-method} +\alias{featIDs,spatEnrObj-method} \title{Spatial and feature IDs} \usage{ spatIDs(x, spat_unit, ...) @@ -29,37 +27,33 @@ featIDs(x, feat_type, ...) ## Default S4 method for signatures: ## 'giotto', 'exprObj', 'spatLocsObj', 'cellMetaObj', 'spatialNetworkObj' 'dimObj' -\S4method{spatIDs}{giotto,missing}(x, spat_unit, ...) +\S4method{spatIDs}{giotto}(x, spat_unit = NULL, ...) -\S4method{spatIDs}{giotto,character}(x, spat_unit, ...) +\S4method{spatIDs}{exprObj}(x, ...) -\S4method{spatIDs}{exprObj,missing}(x, spat_unit, ...) +\S4method{spatIDs}{spatLocsObj}(x, ...) -\S4method{spatIDs}{spatLocsObj,missing}(x, spat_unit, ...) +\S4method{spatIDs}{cellMetaObj}(x, ...) -\S4method{spatIDs}{cellMetaObj,missing}(x, spat_unit, ...) +\S4method{spatIDs}{spatialNetworkObj}(x, ...) -\S4method{spatIDs}{spatialNetworkObj,missing}(x, spat_unit, ...) +\S4method{spatIDs}{dimObj}(x, ...) -\S4method{spatIDs}{dimObj,missing}(x, spat_unit, ...) +\S4method{spatIDs}{giottoPolygon}(x, use_cache = TRUE, uniques = TRUE, ...) -\S4method{spatIDs}{giottoPolygon,missing}(x, use_cache = TRUE, uniques = TRUE, ...) +\S4method{spatIDs}{spatEnrObj}(x, ...) -\S4method{spatIDs}{spatEnrObj,missing}(x, spat_unit, ...) +\S4method{spatIDs}{nnNetObj}(x, ...) -\S4method{spatIDs}{nnNetObj,missing}(x, spat_unit, ...) +\S4method{featIDs}{giotto}(x, feat_type = NULL, ...) -\S4method{featIDs}{giotto,missing}(x, feat_type, ...) +\S4method{featIDs}{exprObj}(x, ...) -\S4method{featIDs}{giotto,character}(x, feat_type, ...) +\S4method{featIDs}{featMetaObj}(x, ...) -\S4method{featIDs}{exprObj,missing}(x, feat_type, ...) +\S4method{featIDs}{giottoPoints}(x, use_cache = TRUE, uniques = TRUE, ...) -\S4method{featIDs}{featMetaObj,missing}(x, feat_type, ...) - -\S4method{featIDs}{giottoPoints,missing}(x, use_cache = TRUE, uniques = TRUE, ...) - -\S4method{featIDs}{spatEnrObj,missing}(x, feat_type, ...) +\S4method{featIDs}{spatEnrObj}(x, ...) } \arguments{ \item{x}{an object}