Skip to content

Commit

Permalink
Merge pull request #72 from jiajic/dev
Browse files Browse the repository at this point in the history
Code cleanup, `as.stars()`, `as.sf()`, `centroids()` updates
  • Loading branch information
jiajic authored Nov 10, 2023
2 parents 3f3dca2 + d83ee4e commit 31480a3
Show file tree
Hide file tree
Showing 37 changed files with 482 additions and 235 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.9007
Version: 0.0.0.9008
Authors@R: c(
person("Ruben", "Dries", email = "rubendries@gmail.com",
role = c("aut", "cre")),
Expand Down
6 changes: 6 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export(addGiottoPolygons)
export(addNetworkLayout)
export(addSpatialCentroidLocations)
export(addSpatialCentroidLocationsLayer)
export(add_img_array_alpha)
export(aggregateStacks)
export(aggregateStacksExpression)
export(aggregateStacksLocations)
Expand All @@ -26,13 +27,16 @@ export(anndataToGiotto)
export(annotateGiotto)
export(annotateSpatialGrid)
export(annotateSpatialNetwork)
export(annotate_spatlocs_with_spatgrid_2D)
export(annotate_spatlocs_with_spatgrid_3D)
export(calculateMetaTable)
export(calculateMetaTableCells)
export(calculateOverlapParallel)
export(calculateOverlapPolygonImages)
export(calculateOverlapRaster)
export(calculateOverlapSerial)
export(calculateSpatCellMetadataProportions)
export(calculate_overlap_raster)
export(changeGiottoInstructions)
export(changeImageBg)
export(checkGiottoEnvironment)
Expand Down Expand Up @@ -116,6 +120,7 @@ export(get_adj_rescale_img)
export(get_args)
export(get_cell_metadata)
export(get_dimReduction)
export(get_distance)
export(get_expression_values)
export(get_feature_info)
export(get_feature_metadata)
Expand Down Expand Up @@ -310,6 +315,7 @@ exportMethods("spatUnit<-")
exportMethods(activeFeatType)
exportMethods(activeSpatUnit)
exportMethods(as.sf)
exportMethods(as.stars)
exportMethods(centroids)
exportMethods(colnames)
exportMethods(copy)
Expand Down
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
- Added `crop()` method for `giottoLargeImage`, `giottoPoints`
- Added `terraVectData` as virtual parent class for `giottoPolygon` and `giottoPoints` classes
- Added `$` and `$<-` methods for `terraVectData`
- Added `[` subsetting for `giottoPoints` and `giottoPolygon`
- Added `[` subsetting for `giottoPoints` and `giottoPolygon` with numerical, logical, and character (by ID)
- Added `setGiotto()` generic
- Added `as.sf()` and `as.stars()` converters for `giottoPoints` and `giottoPolygon`

## Changes
- Improved performance of gefToGiotto()
Expand Down
149 changes: 94 additions & 55 deletions R/aggregate.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,13 @@ polygon_to_raster = function(polygon, field = NULL) {

#' @title calculateOverlapRaster
#' @name calculateOverlapRaster
#' @description calculate overlap between cellular structures (polygons) and features (points)
#' @description calculate overlap between cellular structures (polygons) and
#' features (points).
#' @param gobject giotto object
#' @param name_overlap name for the overlap results (default to feat_info parameter)
#' @param spatial_info polygon information
#' @param spatial_info character. name polygon information
#' @param poly_ID_names (optional) list of poly_IDs to use
#' @param feat_info feature information
#' @param feat_info character. name of feature information
#' @param feat_subset_column feature info column to subset features with
#' @param feat_subset_ids ids within feature info column to use for subsetting
#' @param count_info_column column with count information (optional)
Expand All @@ -66,26 +67,20 @@ polygon_to_raster = function(polygon, field = NULL) {
#' @return giotto object or spatVector with overlapping information
#' @details Serial overlapping function.
#' @concept overlap
#' @seealso [calculate_overlap_raster()]
#' @export
calculateOverlapRaster = function(gobject,
name_overlap = NULL,
spatial_info = NULL,
poly_ID_names = NULL,
feat_info = NULL,
feat_subset_column = NULL,
feat_subset_ids = NULL,
count_info_column = NULL,
return_gobject = TRUE,
verbose = TRUE) {

# define for :=
poly_ID = NULL
poly_i = NULL
ID = NULL
x = NULL
y = NULL
feat_ID = NULL
feat_ID_uniq = NULL
calculateOverlapRaster = function(

Check notice

Code scanning / lintr

Variable and function name style should match snake_case or symbols. Note

Variable and function name style should match snake_case or symbols.

Check notice

Code scanning / lintr

Use <-, not =, for assignment. Note

Use <-, not =, for assignment.
gobject,

Check notice

Code scanning / lintr

Indentation should be 2 spaces but is 4 spaces. Note

Indentation should be 2 spaces but is 4 spaces.
name_overlap = NULL,
spatial_info = NULL,
poly_ID_names = NULL,

Check notice

Code scanning / lintr

Variable and function name style should match snake_case or symbols. Note

Variable and function name style should match snake_case or symbols.
feat_info = NULL,
feat_subset_column = NULL,
feat_subset_ids = NULL,
count_info_column = NULL,
return_gobject = TRUE,
verbose = TRUE
) {

# set defaults if not provided
if(is.null(feat_info)) {
Expand All @@ -101,30 +96,84 @@ calculateOverlapRaster = function(gobject,
}


# spatial vector
if(verbose) cat('1. convert polygon to raster \n')
spatvec = gobject@spatial_info[[spatial_info]]@spatVector
# get information from gobject
# * spatial vector
spatvec = getPolygonInfo(

Check notice

Code scanning / lintr

Use <-, not =, for assignment. Note

Use <-, not =, for assignment.
gobject = gobject,
polygon_name = spatial_info,
return_giottoPolygon = FALSE
)

# * point vector
pointvec = getFeatureInfo(

Check notice

Code scanning / lintr

Use <-, not =, for assignment. Note

Use <-, not =, for assignment.
gobject = gobject,
feat_type = feat_info,
return_giottoPoints = FALSE,
set_defaults = FALSE
)


# subset spatvec
# subset points and polys if needed
# * subset spatvec
if(!is.null(poly_ID_names)) {
spatvec = spatvec[spatvec$poly_ID %in% poly_ID_names]
}

# spatial vector to raster
spatrast_res = polygon_to_raster(spatvec, field = 'poly_ID')
spatrast = spatrast_res[['raster']]
ID_vector = spatrast_res[['ID_vector']]

# point vector
pointvec = gobject@feat_info[[feat_info]]@spatVector

# subset points if needed
# * subset points if needed
# e.g. to select transcripts within a z-plane
if(!is.null(feat_subset_column) & !is.null(feat_subset_ids)) {
bool_vector = pointvec[[feat_subset_column]][[1]] %in% feat_subset_ids
pointvec = pointvec[bool_vector]
}

# run overlap workflow
overlap_points = calculate_overlap_raster(

Check notice

Code scanning / lintr

Use <-, not =, for assignment. Note

Use <-, not =, for assignment.
spatvec = spatvec,
pointvec = pointvec,
count_info_column = count_info_column,
verbose = verbose
)

# return values
if(isTRUE(return_gobject)) {

Check notice

Code scanning / lintr

Place a space before left parenthesis, except in a function call. Note

Place a space before left parenthesis, except in a function call.

if(is.null(name_overlap)) name_overlap = feat_info

Check notice

Code scanning / lintr

Place a space before left parenthesis, except in a function call. Note

Place a space before left parenthesis, except in a function call.

Check notice

Code scanning / lintr

Use <-, not =, for assignment. Note

Use <-, not =, for assignment.
gobject@spatial_info[[spatial_info]]@overlaps[[name_overlap]] = overlap_points

Check notice

Code scanning / lintr

Use <-, not =, for assignment. Note

Use <-, not =, for assignment.

Check notice

Code scanning / lintr

Lines should not be more than 80 characters. This line is 82 characters. Note

Lines should not be more than 80 characters. This line is 82 characters.
return(gobject)

} else {
return(overlap_points)
}
}



#' @name calculate_overlap_raster
#' @title Find feature points overlapped by rasterized polygon.
#' @description Core workflow function that accepts simple `SpatVector` inputs,
#' performs rasterization of the polys and then checks for overlaps.
#' @param spatvec `SpatVector` polygon from a `giottoPolygon` object
#' @param pointvec `SpatVector` points from a `giottoPoints` object
#' @param count_info_column column with count information (optional)
#' @param verbose be verbose
#' @return `SpatVector` of overlapped points info
#' @concept overlap
#' @seealso [calculateOverlapRaster()]
#' @export
calculate_overlap_raster = function(spatvec,

Check notice

Code scanning / lintr

Use <-, not =, for assignment. Note

Use <-, not =, for assignment.
pointvec,
count_info_column = NULL,
verbose = TRUE) {

# DT vars
poly_ID = poly_i = ID = x = y = feat_ID = feat_ID_uniq = NULL

Check notice

Code scanning / lintr

Variable and function name style should match snake_case or symbols. Note

Variable and function name style should match snake_case or symbols.

Check notice

Code scanning / lintr

Use <-, not =, for assignment. Note

Use <-, not =, for assignment.

Check notice

Code scanning / lintr

Use <-, not =, for assignment. Note

Use <-, not =, for assignment.

Check notice

Code scanning / lintr

Variable and function name style should match snake_case or symbols. Note

Variable and function name style should match snake_case or symbols.

Check notice

Code scanning / lintr

Use <-, not =, for assignment. Note

Use <-, not =, for assignment.

Check notice

Code scanning / lintr

Use <-, not =, for assignment. Note

Use <-, not =, for assignment.

Check notice

Code scanning / lintr

Use <-, not =, for assignment. Note

Use <-, not =, for assignment.

Check notice

Code scanning / lintr

Variable and function name style should match snake_case or symbols. Note

Variable and function name style should match snake_case or symbols.

Check notice

Code scanning / lintr

Use <-, not =, for assignment. Note

Use <-, not =, for assignment.

Check notice

Code scanning / lintr

Variable and function name style should match snake_case or symbols. Note

Variable and function name style should match snake_case or symbols.

Check notice

Code scanning / lintr

Use <-, not =, for assignment. Note

Use <-, not =, for assignment.

# spatial vector to raster
if(verbose) cat('1. convert polygon to raster \n')

Check notice

Code scanning / lintr

Place a space before left parenthesis, except in a function call. Note

Place a space before left parenthesis, except in a function call.

Check notice

Code scanning / lintr

Only use double-quotes. Note

Only use double-quotes.
spatrast_res = polygon_to_raster(spatvec, field = 'poly_ID')

Check notice

Code scanning / lintr

Use <-, not =, for assignment. Note

Use <-, not =, for assignment.

Check notice

Code scanning / lintr

Only use double-quotes. Note

Only use double-quotes.
spatrast = spatrast_res[['raster']]

Check notice

Code scanning / lintr

Use <-, not =, for assignment. Note

Use <-, not =, for assignment.

Check notice

Code scanning / lintr

Only use double-quotes. Note

Only use double-quotes.
ID_vector = spatrast_res[['ID_vector']]

Check notice

Code scanning / lintr

Variable and function name style should match snake_case or symbols. Note

Variable and function name style should match snake_case or symbols.

Check notice

Code scanning / lintr

Use <-, not =, for assignment. Note

Use <-, not =, for assignment.

Check notice

Code scanning / lintr

Only use double-quotes. Note

Only use double-quotes.

## overlap between raster and point
if(verbose) cat('2. overlap raster and points \n')
overlap_test = terra::extract(x = spatrast, y = pointvec)
Expand All @@ -136,7 +185,7 @@ calculateOverlapRaster = function(gobject,

# add point information
if(verbose) cat('4. add points information \n')
pointvec_dt = spatVector_to_dt(pointvec)
pointvec_dt = data.table::as.data.table(pointvec, geom = "XY")

Check notice

Code scanning / lintr

Use <-, not =, for assignment. Note

Use <-, not =, for assignment.

pointvec_dt_x = pointvec_dt$x ; names(pointvec_dt_x) = pointvec_dt$geom
pointvec_dt_y = pointvec_dt$y ; names(pointvec_dt_y) = pointvec_dt$geom
Expand All @@ -154,25 +203,15 @@ calculateOverlapRaster = function(gobject,
}

if(verbose) cat('5. create overlap polygon information \n')
overlap_test_dt_spatvector = terra::vect(x = as.matrix(overlap_test_dt[, c('x', 'y'), with = F]),
type = "points",
atts = overlap_test_dt[, c('poly_ID', 'feat_ID', 'feat_ID_uniq', count_info_column), with = F])
names(overlap_test_dt_spatvector) = c('poly_ID', 'feat_ID', 'feat_ID_uniq', count_info_column)



if(return_gobject == TRUE) {
if(is.null(name_overlap)) {
name_overlap = feat_info
}

gobject@spatial_info[[spatial_info]]@overlaps[[name_overlap]] = overlap_test_dt_spatvector
return(gobject)

} else {
return(overlap_test_dt_spatvector)
}

overlap_test_dt_spatvector = terra::vect(

Check notice

Code scanning / lintr

Use <-, not =, for assignment. Note

Use <-, not =, for assignment.
x = as.matrix(overlap_test_dt[, c('x', 'y'), with = F]),

Check notice

Code scanning / lintr

Only use double-quotes. Note

Only use double-quotes.

Check notice

Code scanning / lintr

Only use double-quotes. Note

Only use double-quotes.

Check notice

Code scanning / lintr

Use FALSE instead of the symbol F. Note

Use FALSE instead of the symbol F.
type = "points",
atts = overlap_test_dt[, c('poly_ID', 'feat_ID', 'feat_ID_uniq', count_info_column), with = F]

Check notice

Code scanning / lintr

Only use double-quotes. Note

Only use double-quotes.

Check notice

Code scanning / lintr

Only use double-quotes. Note

Only use double-quotes.

Check notice

Code scanning / lintr

Only use double-quotes. Note

Only use double-quotes.

Check notice

Code scanning / lintr

Lines should not be more than 80 characters. This line is 98 characters. Note

Lines should not be more than 80 characters. This line is 98 characters.

Check notice

Code scanning / lintr

Use FALSE instead of the symbol F. Note

Use FALSE instead of the symbol F.
)
names(overlap_test_dt_spatvector) = c(

Check notice

Code scanning / lintr

Use <-, not =, for assignment. Note

Use <-, not =, for assignment.
'poly_ID', 'feat_ID', 'feat_ID_uniq', count_info_column

Check notice

Code scanning / lintr

Only use double-quotes. Note

Only use double-quotes.

Check notice

Code scanning / lintr

Only use double-quotes. Note

Only use double-quotes.

Check notice

Code scanning / lintr

Only use double-quotes. Note

Only use double-quotes.
)
return(overlap_test_dt_spatvector)
}


Expand Down
30 changes: 16 additions & 14 deletions R/classes.R
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,8 @@ updateGiottoObject <- function(gobject) {
#' that are provided in the expression slot.
#'
#'
#' @export
#' @export giotto
#' @exportClass giotto
giotto <- setClass(
"giotto",
slots = c(
Expand Down Expand Up @@ -658,8 +659,8 @@ check_expr_obj <- function(object) {
#' @slot feat_type feature type of expression (e.g. 'rna', 'protein')
#' @slot provenance origin data of expression information (if applicable)
#' @slot misc misc
#' @export
setClass("exprObj",
#' @exportClass exprObj
exprObj = setClass("exprObj",

Check notice

Code scanning / lintr

Variable and function name style should match snake_case or symbols. Note

Variable and function name style should match snake_case or symbols.

Check notice

Code scanning / lintr

Use <-, not =, for assignment. Note

Use <-, not =, for assignment.
contains = c("nameData", "exprData", "spatFeatData", "miscData", 'giottoSubobject'),
validity = check_expr_obj
)
Expand Down Expand Up @@ -713,8 +714,8 @@ check_cell_meta_obj <- function(object) {
#' @slot spat_unit spatial unit of aggregated expression (e.g. 'cell')
#' @slot feat_type feature type of aggregated expression (e.g. 'rna', 'protein')
#' @slot provenance origin data of aggregated expression information (if applicable)
#' @export
setClass("cellMetaObj",
#' @exportClass cellMetaObj
cellMetaObj = setClass("cellMetaObj",

Check notice

Code scanning / lintr

Variable and function name style should match snake_case or symbols. Note

Variable and function name style should match snake_case or symbols.

Check notice

Code scanning / lintr

Use <-, not =, for assignment. Note

Use <-, not =, for assignment.
contains = c("metaData", "spatFeatData", "giottoSubobject"),
validity = check_cell_meta_obj
)
Expand Down Expand Up @@ -758,8 +759,8 @@ check_feat_meta_obj <- function(object) {
#' @slot spat_unit spatial unit of aggregated expression (e.g. 'cell')
#' @slot feat_type feature type of aggregated expression (e.g. 'rna', 'protein')
#' @slot provenance origin data of aggregated expression information (if applicable)
#' @export
setClass("featMetaObj",
#' @exportClass featMetaObj
featMetaObj = setClass("featMetaObj",

Check notice

Code scanning / lintr

Variable and function name style should match snake_case or symbols. Note

Variable and function name style should match snake_case or symbols.

Check notice

Code scanning / lintr

Use <-, not =, for assignment. Note

Use <-, not =, for assignment.
contains = c("metaData", "spatFeatData", 'giottoSubobject'),
validity = check_feat_meta_obj
)
Expand Down Expand Up @@ -825,8 +826,8 @@ check_dim_obj <- function(object) {
#' @slot reduction_method method used to generate dimension reduction
#' @slot coordinates embedding coordinates
#' @slot misc method-specific additional outputs
#' @export
setClass("dimObj",
#' @exportClass dimObj
dimObj = setClass("dimObj",

Check notice

Code scanning / lintr

Variable and function name style should match snake_case or symbols. Note

Variable and function name style should match snake_case or symbols.

Check notice

Code scanning / lintr

Use <-, not =, for assignment. Note

Use <-, not =, for assignment.
contains = c("nameData", "spatFeatData", 'giottoSubobject'),
slots = c(
reduction = "character",
Expand Down Expand Up @@ -888,8 +889,8 @@ S3toS4dimObj <- function(object) {
#' @slot spat_unit spatial unit of data
#' @slot provenance origin of aggregated information (if applicable)
#' @slot misc misc
#' @export
setClass("nnNetObj",
#' @exportClass nnNetObj
nnNetObj = setClass("nnNetObj",

Check notice

Code scanning / lintr

Variable and function name style should match snake_case or symbols. Note

Variable and function name style should match snake_case or symbols.

Check notice

Code scanning / lintr

Use <-, not =, for assignment. Note

Use <-, not =, for assignment.
contains = c("nameData", "nnData", "spatFeatData", "miscData", 'giottoSubobject')
)

Expand Down Expand Up @@ -955,8 +956,8 @@ check_spat_locs_obj <- function(object) {
#' @slot coordinates data.table of spatial coordinates/locations
#' @slot spat_unit spatial unit tag
#' @slot provenance origin of aggregated information (if applicable)
#' @export
setClass("spatLocsObj",
#' @exportClass spatLocsObj
spatLocsObj = setClass("spatLocsObj",

Check notice

Code scanning / lintr

Variable and function name style should match snake_case or symbols. Note

Variable and function name style should match snake_case or symbols.

Check notice

Code scanning / lintr

Use <-, not =, for assignment. Note

Use <-, not =, for assignment.
contains = c("nameData", "coordDataDT", "spatData", "miscData", 'giottoSubobject'),
validity = check_spat_locs_obj
)
Expand Down Expand Up @@ -1467,7 +1468,8 @@ giottoImage <- setClass(
#' @slot is_int values are integers
#' @slot file_path file path to the image if given
#' @slot OS_platform Operating System to run Giotto analysis on
#' @export
#' @export giottoLargeImage
#' @exportClass giottoLargeImage
giottoLargeImage <- setClass(
Class = "giottoLargeImage",
slots = c(
Expand Down
8 changes: 0 additions & 8 deletions R/create.R
Original file line number Diff line number Diff line change
Expand Up @@ -659,12 +659,6 @@ createGiottoObjectSubcellular = function(gpolygons = NULL,
centroidsDT_loc = centroidsDT[, .(poly_ID, x, y)]
colnames(centroidsDT_loc) = c('cell_ID', 'sdimx', 'sdimy')

# gobject = set_spatial_locations(gobject = gobject,
# spat_unit = polygon_info,
# spat_loc_name = 'raw',
# spatlocs = centroidsDT_loc,
# verbose = FALSE)

locsObj = create_spat_locs_obj(name = 'raw',
coordinates = centroidsDT_loc,
spat_unit = polygon_info,
Expand Down Expand Up @@ -778,9 +772,7 @@ createGiottoObjectSubcellular = function(gpolygons = NULL,
gobject@feat_metadata[[feat_type]] = data.table::as.data.table(gobject@feat_metadata[[feat_type]])
gobject@feat_metadata[[feat_type]][, feat_ID := gobject@feat_ID[[feat_type]]]
}

}

}


Expand Down
8 changes: 7 additions & 1 deletion R/generate_poly.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ polyStamp <- function(stamp_dt,
centroid_dt = data.table::as.data.table(terra::centroids(stamp_poly),
geom = "XY",
include_values = F)

stamp_centroid = c(x = centroid_dt$x,
y = centroid_dt$y)

Expand Down Expand Up @@ -279,3 +279,9 @@ makePseudoVisium <- function(extent = NULL,
skip_eval_dfr = TRUE,
copy_dt = FALSE)
}






1 change: 1 addition & 0 deletions R/generics.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,5 @@ setGeneric('setGiotto', function(gobject, x, ...) standardGeneric('setGiotto'))

# coerce ####
setGeneric('as.sf', function(x, ...) standardGeneric('as.sf'))
setGeneric('as.stars', function(x, ...) standardGeneric('as.stars'))

Loading

0 comments on commit 31480a3

Please sign in to comment.