Skip to content

Commit

Permalink
enh: feat meta + matrix joining can select ids
Browse files Browse the repository at this point in the history
- feat meta also properly mixed sorts after a join
  • Loading branch information
jiajic committed Oct 18, 2024
1 parent 2269520 commit 549d6e0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- 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 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

0 comments on commit 549d6e0

Please sign in to comment.