Skip to content

Commit

Permalink
chore: catch up to dev
Browse files Browse the repository at this point in the history
  • Loading branch information
jiajic committed Oct 22, 2024
2 parents 547431a + d47a88c commit 68fa194
Show file tree
Hide file tree
Showing 16 changed files with 444 additions and 68 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ Collate:
'interoperability.R'
'join.R'
'methods-IDs.R'
'methods-XY.R'
'methods-affine.R'
'methods-centroids.R'
'methods-coerce.R'
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ exportMethods("$")
exportMethods("$<-")
exportMethods("+")
exportMethods("-")
exportMethods("XY<-")
exportMethods("[")
exportMethods("[<-")
exportMethods("[[")
Expand All @@ -309,6 +310,7 @@ exportMethods("instructions<-")
exportMethods("objName<-")
exportMethods("prov<-")
exportMethods("spatUnit<-")
exportMethods(XY)
exportMethods(activeFeatType)
exportMethods(activeSpatUnit)
exportMethods(affine)
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
- fix `joinGiottoObject()` for gobjects with only poly and point data [#233](https://github.com/drieslab/GiottoClass/issues/233)
- fix `joinGiottoObject()` for gobjects with image intensity overlaps features
- fix subsetting error due to expression `matrix` drop to `numeric` when only one cell is left
- `shift_vertical_step` and `shift_horizontal_step` args in `createGiottoPolygonsFromMask()` when numeric now shift by steps based on the dims of the image instead of just by the numerical value provided.
- fix feature metadata not being mixedsorted after join

## enhancements
- python packages to install through pip is now settable in `installGiottoEnvironment()` [#224](https://github.com/drieslab/GiottoClass/issues/224)
Expand All @@ -22,11 +24,14 @@
- `ext()` and `ext<-()` can now be used to get and set extent of `affine2d`
- `rownames()`, `colnames()`, `dimnames()` for `giotto`
- `spatValues()` can get values from multiple spatial units.
- `createGiottoPolygonsFromMask()` now works with anything `terra::rast()` can read
- `createGiottoLargeImage()` now works with anything `terra::rast()` can read

## new
- `sliceGiotto()` for pulling out specific spatial units and feature types as independent `giotto` objects
- `splitGiotto()` for splitting a Giotto object into a list of Giotto objects based on a cell metadata column
- `as.list()` method for `giotto` to dump the data as a list of subobjects
- `XY()` and `XY<-()` for accessing and setting coordinate values of subobjects as `matrix`


# GiottoClass 0.3.5 (2024/08/28)
Expand Down
60 changes: 23 additions & 37 deletions R/create.R
Original file line number Diff line number Diff line change
Expand Up @@ -2418,7 +2418,8 @@ setMethod(


#' @rdname createGiottoPolygon
#' @param maskfile path to mask file
#' @param maskfile path to mask file, a terra `SpatRaster`, or some other
#' data class readable by [terra::rast()]
#' @param mask_method how the mask file defines individual segmentation
#' annotations. See *mask_method* section
#' @param name character. Name to assign created `giottoPolygon`
Expand Down Expand Up @@ -2521,15 +2522,16 @@ createGiottoPolygonsFromMask <- function(maskfile,

# if maskfile input is not a spatraster, read it in as spatraster
# if it is spatraster, skip
if (!inherits(maskfile, "SpatRaster")) {
if (inherits(maskfile, "SpatRaster")) {
terra_rast <- maskfile
} else if (is.character(maskfile)) {
# check if mask file exists
maskfile <- path.expand(maskfile)
if (!file.exists(maskfile)) {
stop("path : ", maskfile, " does not exist \n")
}
checkmate::assert_file_exists(maskfile)
terra_rast <- .create_terra_spatraster(maskfile)
} else {
terra_rast <- maskfile
# assume some other class readable by terra::rast()
terra_rast <- .create_terra_spatraster(maskfile)
}

# create polygons from mask
Expand Down Expand Up @@ -2627,14 +2629,14 @@ createGiottoPolygonsFromMask <- function(maskfile,
if (identical(shift_vertical_step, TRUE)) {
shift_vertical_step <- rast_dimensions[1] # nrows of raster
} else if (is.numeric(shift_vertical_step)) {
shift_vertical_step <- shift_vertical_step
shift_vertical_step <- rast_dimensions[1] * shift_vertical_step
} else {
shift_vertical_step <- 0
}
if (identical(shift_horizontal_step, TRUE)) {
shift_horizontal_step <- rast_dimensions[2] # ncols of raster
} else if (is.numeric(shift_horizontal_step)) {
shift_horizontal_step <- shift_horizontal_step
shift_horizontal_step <- rast_dimensions[2] * shift_horizontal_step
} else {
shift_horizontal_step <- 0
}
Expand Down Expand Up @@ -3174,7 +3176,8 @@ createGiottoImage <- function(
#' @name createGiottoLargeImage
#' @description Creates a large giotto image that can be added to a Giotto
#' subcellular object. Generates deep copy of SpatRaster
#' @param raster_object terra SpatRaster image object
#' @param raster_object filepath to an image, a terra `SpatRaster` or, other format
#' openable via [terra::rast()]
#' @param name name for the image
#' @param negative_y Map image to negative y spatial values if TRUE. Meaning
#' that origin is in upper left instead of lower left.
Expand Down Expand Up @@ -3216,42 +3219,25 @@ createGiottoLargeImage <- function(
# create minimum giotto
g_imageL <- new("giottoLargeImage", name = name)


## 1. check raster object and load as SpatRaster if necessary
if (!inherits(raster_object, "SpatRaster")) {
if (file.exists(raster_object)) {
g_imageL@file_path <- raster_object
raster_object <- .create_terra_spatraster(
image_path = raster_object
)
} else {
stop("raster_object needs to be a 'SpatRaster' object from the
terra package or \n an existing path that can be read by
terra::rast()")
}
}

# Prevent updates to original raster object input
if (getNamespaceVersion("terra") >= "1.15-12") {
if (inherits(raster_object, "SpatRaster")) {
# Prevent updates to original raster object input
raster_object <- terra::deepcopy(raster_object)
} else if (is.character(raster_object)) {
checkmate::assert_file_exists(raster_object)
g_imageL@file_path <- raster_object
raster_object <- .create_terra_spatraster(raster_object)
} else {
# raster_object = terra::copy(raster_object)
if (isTRUE(verbose)) {
warning("\n If largeImage was created from a terra raster object,
manipulations to the giotto image may be reflected in the
raster object as well. Update terra to >= 1.15-12 to avoid
this issue. \n")
}
# assume class readable by terra rast
raster_object <- .create_terra_spatraster(raster_object)
}


## 2. image bound spatial extent
if (use_rast_ext == TRUE) {
if (use_rast_ext) {
extent <- terra::ext(raster_object)
if (verbose == TRUE) {
wrap_msg("use_rast_ext == TRUE, extent from input raster_object will
be used.")
}
vmsg(.v = verbose, "use_rast_ext == TRUE
extent from input raster_object will be used.")
}

# By extent object (priority)
Expand Down
19 changes: 11 additions & 8 deletions R/generics.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,6 @@ setGeneric(
)


# Methods and documentations found in methods-spatShift.R
setGeneric("spatShift", function(x, ...) standardGeneric("spatShift"))
setGeneric("affine", function(x, y, ...) standardGeneric("affine"))
setGeneric("shear", function(x, ...) standardGeneric("shear"))

# Methods and documentations found in methods-overlaps.R
setGeneric("overlaps", function(x, ...) standardGeneric("overlaps"))


# Object creation ####
setGeneric(
Expand Down Expand Up @@ -119,6 +111,17 @@ setGeneric("norm_pearson", function(x, ...) standardGeneric("norm_pearson"))
setGeneric("norm_osmfish", function(x, ...) standardGeneric("norm_osmfish"))


# Methods and documentations found in methods-spatShift.R
setGeneric("spatShift", function(x, ...) standardGeneric("spatShift"))
setGeneric("affine", function(x, y, ...) standardGeneric("affine"))
setGeneric("shear", function(x, ...) standardGeneric("shear"))
setGeneric("XY", function(x, ...) standardGeneric("XY"))
setGeneric("XY<-", function(x, ..., value) standardGeneric("XY<-"))

# Methods and documentations found in methods-overlaps.R
setGeneric("overlaps", function(x, ...) standardGeneric("overlaps"))



# Giotto subnesting ####
# All methods and documentations found in methods-nesting.R
Expand Down
33 changes: 23 additions & 10 deletions R/join.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@
#' @name .join_expression_matrices
#' @keywords internal
#' @noRd
.join_expression_matrices <- function(matrix_list) {
.join_expression_matrices <- function(matrix_list, feat_ids = NULL) {

# find all features
final_feats <- list()
for (matr_i in seq_len(length(matrix_list))) {
rowfeats <- rownames(matrix_list[[matr_i]])
final_feats[[matr_i]] <- rowfeats
if (is.null(feat_ids)) {
final_feats <- lapply(matrix_list, rownames)
final_feats <- unique(unlist(final_feats))
} else {
final_feats <- feat_ids
}

final_feats <- unique(unlist(final_feats))
final_feats <- mixedsort(final_feats)



# extend matrices with missing ids
final_mats <- list()
for (matr_i in seq_len(length(matrix_list))) {
Expand Down Expand Up @@ -60,9 +58,20 @@
#' @name .join_feat_meta
#' @keywords internal
#' @noRd
.join_feat_meta <- function(dt_list) {
.join_feat_meta <- function(dt_list, feat_ids = NULL) {
feat_ID <- NULL

if (!is.null(feat_ids)) {
dt_list <- lapply(dt_list, function(dt) {
dt <- dt[feat_ID %in% feat_ids]
missing_feat <- dt[, feat_ids[!feat_ids %in% feat_ID]]
if (length(missing_feat) > 0L) {
dt_append <- data.table::data.table(feat_ID = missing_feat)
dt <- rbind(dt, dt_append, fill = TRUE)
}
})
}

comb_meta <- do.call("rbind", c(dt_list, fill = TRUE))
comb_meta <- unique(comb_meta)

Expand All @@ -77,8 +86,12 @@
"feature metadata: multiple versions of metadata for:\n",
dup_feats,
"\n First entry will be selected for joined object."
# "first" is based on gobject order
))
}

# order by feat_ID
comb_meta <- comb_meta[mixedorder(feat_ID)]

return(comb_meta)
}
Expand Down
Loading

0 comments on commit 68fa194

Please sign in to comment.