Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v0.1.3 #161

Merged
merged 23 commits into from
Jan 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions 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.1.2
Version: 0.1.3
Authors@R: c(
person("Ruben", "Dries", email = "rubendries@gmail.com",
role = c("aut", "cre")),
Expand Down Expand Up @@ -34,7 +34,7 @@ Imports:
data.table (>= 1.12.2),
dbscan (>= 1.1-3),
deldir (>= 1.0.6),
GiottoUtils (>= 0.1.2),
GiottoUtils (>= 0.1.3),
igraph (>= 1.2.4.1),
magick,
Matrix (>= 1.6.2),
Expand All @@ -55,9 +55,11 @@ Suggests:
GiottoData,
HDF5Array (>= 1.18.1),
knitr,
plotly,
R.utils,
raster,
remotes,
rgl,
rhdf5,
rlang,
rmarkdown,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ exportMethods(createGiottoPoints)
exportMethods(createGiottoPolygon)
exportMethods(crop)
exportMethods(dim)
exportMethods(dimnames)
exportMethods(ext)
exportMethods(featIDs)
exportMethods(featType)
Expand Down
19 changes: 17 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@


# GiottoClass 0.1.3 (2024/01/12)

## bug fixes
- fix unexpected sorting in `addCellMetadata()` and `addFeatMetadata()` [#853](https://github.com/drieslab/Giotto/issues/853) by rbutleriii

## new
- `init_gobject` param in `loadGiotto()` to control whether object initialization is also performed after load
- vignette for image tools

## enhancements
- ID sorts now use `gtools::mixedsort()` [#853](https://github.com/drieslab/Giotto/issues/853) by rbutleriii
- more subobjects respond to `colnames`, `rownames`, `dimnames`
- `plot()` and `show()` now handle 3D `spatLocsObj`

# GiottoClass 0.1.2 (2024/01/02)

## Added
Expand All @@ -9,8 +24,8 @@
- Added: vignette for working with spatial classes
- Added: `output` param to `.spatraster_sample_values()`. Can now return as sampled `data.frame`, `array`, `magick`, `EBImage`

## Changes
- Fixes: Updates to raster `calculateOverlap()` workflows
## bug fixes
- param fixes in raster `calculateOverlap()` workflows


# GiottoClass 0.1.1 (2023/12/16)
Expand Down
6 changes: 3 additions & 3 deletions R/aggregate.R
Original file line number Diff line number Diff line change
Expand Up @@ -1135,8 +1135,8 @@ setMethod(
mat_r_names <- rownames(overlapmatrix)
mat_c_names <- colnames(overlapmatrix)
overlapmatrix <- overlapmatrix[
match(sort(mat_r_names), mat_r_names),
match(sort(mat_c_names), mat_c_names)
match(mixedsort(mat_r_names), mat_r_names),
match(mixedsort(mat_c_names), mat_c_names)
]

overlapExprObj <- create_expr_obj(
Expand Down Expand Up @@ -1962,7 +1962,7 @@ aggregateStacksLocations <- function(gobject,
stack_spatvector <- terra::makeValid(stack_spatvector)

# 3. aggregate individual cells/polys
all_poly_ids <- sort(unique(stack_spatvector$poly_ID))
all_poly_ids <- mixedsort(unique(stack_spatvector$poly_ID))

# run in for loop if data is very very big
if (isTRUE(for_loop)) {
Expand Down
132 changes: 84 additions & 48 deletions R/auxilliary.R
Original file line number Diff line number Diff line change
Expand Up @@ -323,10 +323,13 @@
#' @param gobject giotto object
#' @param spat_unit spatial unit
#' @param feat_type feature type
#' @param new_metadata new cell metadata to use (data.table, data.frame, ...)
#' @param vector_name (optional) custom name if you provide a single vector
#' @param by_column merge metadata based on \emph{cell_ID} column in \code{\link{pDataDT}} (default = FALSE)
#' @param column_cell_ID column name of new metadata to use if by_column = TRUE
#' @param new_metadata new cell metadata to use (data.table, data.frame, vector, factor, ...)

Check notice

Code scanning / lintr

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

Lines should not be more than 80 characters. This line is 93 characters.
#' @param vector_name (optional) custom name for new metadata column if single
#' vector or factor is provided
#' @param by_column merge metadata based on \emph{cell_ID} column in
#' \code{\link{pDataDT}} (default = FALSE)
#' @param column_cell_ID column name of new metadata to use if
#' \code{by_column = TRUE}
#' @return giotto object
#' @details You can add additional cell metadata in two manners:
#' \itemize{
Expand All @@ -341,7 +344,11 @@
vector_name = NULL,
by_column = FALSE,
column_cell_ID = NULL) {
# Set feat_type and spat_unit

# NSE variables
cell_ID <- 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.

# 0. set feat_type and spat_unit
spat_unit <- set_default_spat_unit(
gobject = gobject,
spat_unit = spat_unit
Expand All @@ -353,36 +360,47 @@
)


# check hierarchical slots
# 1. check hierarchical slots
# Expression information must first exist in the gobject for the corresponding
# metdata information to be added.
avail_ex <- list_expression(
gobject = gobject,
spat_unit = spat_unit,
feat_type = feat_type
)
if (is.null(avail_ex)) {
stop(wrap_txt(
.gstop(
"No matching expression information discovered for:
spat_unit:", spat_unit,
"\nfeature type:", feat_type,
spat_unit:", spat_unit, "\nfeature type:", feat_type,
"\nPlease add expression information first"
))
)
}


cell_metadata <- get_cell_metadata(gobject,
# 2. get the cell metadata to add to
cell_metadata <- getCellMetadata(
gobject,
spat_unit = spat_unit,
feat_type = feat_type,
output = "cellMetaObj",
copy_obj = TRUE
)

ordered_cell_IDs <- get_cell_id(gobject, spat_unit = spat_unit)
ordered_cell_IDs <- spatIDs(cell_metadata)

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.


if (is.vector(new_metadata) | is.factor(new_metadata)) {
# 3. format input metadata
# [vector/factor input]
# Values are assumed to be in the same order as the existing metadata info.
# Convert vector or factor into a single-column data.table
# Colname is the variable name of the vector or factor.
# [all other inputs]
# Coerce to data.table
if (is.vector(new_metadata) || is.factor(new_metadata)) {
original_name <- deparse(substitute(new_metadata))
new_metadata <- data.table::as.data.table(new_metadata)

if (!is.null(vector_name) & is.character(vector_name)) {
if (!is.null(vector_name) && is.character(vector_name)) {
colnames(new_metadata) <- vector_name
} else {
colnames(new_metadata) <- original_name
Expand All @@ -391,18 +409,21 @@
new_metadata <- data.table::as.data.table(new_metadata)
}

# If no specific column_cell_ID is provided, assume "cell_ID"
if (is.null(column_cell_ID)) {
column_cell_ID <- "cell_ID"
}

# overwrite columns with same name

# 4. combine with existing metadata
# get old and new meta colnames that are not the ID col
new_col_names <- colnames(new_metadata)
new_col_names <- new_col_names[new_col_names != column_cell_ID]
old_col_names <- colnames(cell_metadata[])
old_col_names <- colnames(cell_metadata)
old_col_names <- old_col_names[old_col_names != "cell_ID"]
same_col_names <- new_col_names[new_col_names %in% old_col_names]


# overwrite columns with same name
same_col_names <- new_col_names[new_col_names %in% old_col_names]
if (length(same_col_names) >= 1) {
wrap_msg(
"\nThese column names were already used: ", same_col_names, "\n",
Expand All @@ -412,23 +433,20 @@
}



if (by_column == FALSE) {
if (!isTRUE(by_column)) {
cell_metadata[] <- cbind(cell_metadata[], new_metadata)
} else {
if (is.null(column_cell_ID)) stop("You need to provide cell_ID column")
cell_metadata[] <- data.table::merge.data.table(cell_metadata[],
cell_metadata[] <- data.table::merge.data.table(
x = cell_metadata[],
by.x = "cell_ID",
new_metadata,
y = new_metadata,
by.y = column_cell_ID,
all.x = TRUE
)
}

# data.table variables
cell_ID <- NULL

# reorder
# 5. ensure data is in same order and set data
cell_metadata[] <- cell_metadata[][match(ordered_cell_IDs, cell_ID)]


Expand Down Expand Up @@ -463,13 +481,13 @@
feat_type = NULL,
spat_unit = NULL,
new_metadata,
by_column = F,
column_feat_ID = NULL,
vector_name = NULL) {
# data.table variables
vector_name = NULL,
by_column = FALSE,
column_feat_ID = 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.
# NSE variables
feat_ID <- NULL

# Set feat_type and spat_unit
# 0. set feat_type and spat_unit
spat_unit <- set_default_spat_unit(
gobject = gobject,
spat_unit = spat_unit
Expand All @@ -481,35 +499,47 @@
)


# check hierarchical slots
# 1. check hierarchical slots
# Expression information must first exist in the gobject for the corresponding
# metdata information to be added.
avail_ex <- list_expression(
gobject = gobject,
spat_unit = spat_unit,
feat_type = feat_type
)
if (is.null(avail_ex)) {
stop(wrap_txt(
.gstop(
"No matching expression information discovered for:
spat_unit:", spat_unit,
"\nfeature type:", feat_type,
spat_unit:", spat_unit, "\nfeature type:", feat_type,
"\nPlease add expression information first"
))
)
}


feat_metadata <- get_feature_metadata(gobject,
# 2. get the cell metadata to add to
feat_metadata <- getFeatureMetadata(
gobject,
spat_unit = spat_unit,
feat_type = feat_type,
output = "featMetaObj",
copy_obj = TRUE
)

ordered_feat_IDs <- get_feat_id(gobject, feat_type = feat_type)
ordered_feat_IDs <- featIDs(feat_metadata)

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.

if (is.vector(new_metadata) | is.factor(new_metadata)) {

# 3. format input metadata
# [vector/factor input]
# Values are assumed to be in the same order as the existing metadata info.
# Convert vector or factor into a single-column data.table
# Colname is the variable name of the vector or factor.
# [all other inputs]
# Coerce to data.table
if (is.vector(new_metadata) || is.factor(new_metadata)) {
original_name <- deparse(substitute(new_metadata))
new_metadata <- data.table::as.data.table(new_metadata)
if (!is.null(vector_name) & is.character(vector_name)) {

if (!is.null(vector_name) && is.character(vector_name)) {
colnames(new_metadata) <- vector_name
} else {
colnames(new_metadata) <- original_name
Expand All @@ -518,17 +548,21 @@
new_metadata <- data.table::as.data.table(new_metadata)
}

# If no specific column_cell_ID is provided, assume "cell_ID"
if (is.null(column_feat_ID)) {
column_feat_ID <- "feat_ID"
}

# overwrite columns with same name

# 4. combine with existing metadata
# get old and new meta colnames that are not the ID col
new_col_names <- colnames(new_metadata)
new_col_names <- new_col_names[new_col_names != column_feat_ID]
old_col_names <- colnames(feat_metadata[])
old_col_names <- old_col_names[old_col_names != "feat_ID"]
same_col_names <- new_col_names[new_col_names %in% old_col_names]

# overwrite columns with same name
same_col_names <- new_col_names[new_col_names %in% old_col_names]
if (length(same_col_names) >= 1) {
wrap_msg(
"\nThese column names were already used: ", same_col_names, "\n",
Expand All @@ -538,19 +572,21 @@
}


if (by_column == FALSE) {
if (!isTRUE(by_column)) {
feat_metadata[] <- cbind(feat_metadata[], new_metadata)
} else {
if (is.null(column_feat_ID)) stop("You need to provide feat ID column")
feat_metadata[] <- data.table::merge.data.table(feat_metadata[],
feat_metadata[] <- data.table::merge.data.table(
x = feat_metadata[],
by.x = "feat_ID",
new_metadata,
y = new_metadata,
by.y = column_feat_ID,
all.x = T
all.x = TRUE
)
}

# reorder

# 5. ensure data is in same order and set data
feat_metadata[] <- feat_metadata[][match(ordered_feat_IDs, feat_ID)]

### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
Expand Down Expand Up @@ -1064,7 +1100,7 @@
misc = list(expr_values_used = expression_values)
)

if (return_gobject == TRUE) {
if (isTRUE(return_gobject)) {
## enrichment scores
spenr_names <- list_spatial_enrichments_names(gobject = gobject, spat_unit = spat_unit, feat_type = feat_type)

Expand Down
Loading