From ee84e9757ea0426fe1bc3790ddc660337fb6be4e Mon Sep 17 00:00:00 2001 From: Eddie Ruiz <32622519+Ed2uiz@users.noreply.github.com> Date: Wed, 12 Jul 2023 12:16:47 -0400 Subject: [PATCH] update polyStamp with crossJoin --- R/giotto_structures.R | 71 +++++++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 33 deletions(-) diff --git a/R/giotto_structures.R b/R/giotto_structures.R index 2524efdfa..98a45cf43 100644 --- a/R/giotto_structures.R +++ b/R/giotto_structures.R @@ -910,40 +910,45 @@ rbind2_giotto_polygon_hetero = function(x, y, new_name, add_list_ID = TRUE) { #' @param verbose be verbose #' @return returns a data.table of polygon vertices #' @export -polyStamp = function(stamp_dt, - spatlocs, - id_col = 'cell_ID', - x_col = 'sdimx', - y_col = 'sdimy', - verbose = TRUE) { - - if(inherits(spatlocs, 'spatLocsObj')) { - spatlocs = spatlocs[] +polyStamp <- function(stamp_dt, + spatlocs, + id_col = 'cell_ID', + x_col = 'sdimx', + y_col = 'sdimy', + verbose = TRUE) { + + if(!all(c(id_col, x_col, y_col) %in% colnames(spatlocs))) { + stop(wrap_txt('Not all colnames found in spatlocs')) } - - if(!all(c(id_col, x_col, y_col) %in% colnames(spatlocs))) { - stop(wrap_txt('Not all colnames found in spatlocs')) - } - - # define polys relative to centroid - stamp_centroid = c(x = mean(stamp_dt[['x']]), - y = mean(stamp_dt[['y']])) - rel_vertices = data.table::data.table(x = stamp_dt$x - stamp_centroid[['x']], - y = stamp_dt$y - stamp_centroid[['y']]) - - # generate poly vertices around given spatlocs - poly_dt = apply(X = spatlocs, - MARGIN = 1, - function(r) { - return(data.table::data.table(x = rel_vertices[['x']] + as.numeric(r[[x_col]]), - y = rel_vertices[['y']] + as.numeric(r[[y_col]]), - poly_ID = as.character(r[[id_col]]))) - }) - - if(isTRUE(verbose)) wrap_msg(nrow(spatlocs), 'polygons stamped') - - return(do.call(rbind, poly_dt)) - + + # define polys relative to centroid + stamp_centroid = c(x = mean(stamp_dt[['x']]), + y = mean(stamp_dt[['y']])) + rel_vertices = data.table::data.table(x = stamp_dt$x - stamp_centroid[['x']], + y = stamp_dt$y - stamp_centroid[['y']]) + + # generate poly vertices around given spatlocs + poly_dt = data.table::CJ(1:nrow(spatlocs), 1:nrow(rel_vertices), sorted = FALSE) + colnames(poly_dt) = c("spatlocs_idx", "rel_vertices_idx") + + # compute the absolute coordinates of the polygon vertices for each spatial location + poly_dt[, c(x_col, y_col) := { + spat_row = spatlocs[spatlocs_idx] + rel_row = rel_vertices[rel_vertices_idx] + list(spat_row[[x_col]] + rel_row$x, spat_row[[y_col]] + rel_row$y) + }] + + # add a new column 'poly_ID' to 'poly_dt' with the ID of each spatial location. + poly_dt[, poly_ID := spatlocs[poly_dt$spatlocs_idx, id_col, with = FALSE]] + poly_dt$poly_ID <- as.character(poly_dt$poly_ID) + + if(isTRUE(verbose)) wrap_msg(nrow(spatlocs), 'polygons stamped') + + res <- poly_dt[, c(x_col, y_col, "poly_ID"), with = FALSE] + setnames(res, c(x_col, y_col), c("x", "y")) + + return(res) + }