From 1abb1731613468ced8bb44d9ac74cd902c60cf8f Mon Sep 17 00:00:00 2001 From: jiajic <72078254+jiajic@users.noreply.github.com> Date: Mon, 21 Nov 2022 13:17:07 -0500 Subject: [PATCH 01/45] update show function exprObj Add abbreviated show functionality for 'denseMatrix' based exprObj --- R/classes.R | 2 ++ R/utilities.R | 12 ++++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/R/classes.R b/R/classes.R index 773eb502d..19e2a5463 100644 --- a/R/classes.R +++ b/R/classes.R @@ -548,6 +548,8 @@ setMethod( writeLines(gsub(pattern = "in show(.*?))'", replacement = '', x = print_cap)) cat('\n First four colnames:') cat('\n', wrap_txt(head(colnames(slot(object, 'exprMat')), 4), strWidth = 40), '\n') + } else if(inherits(slot(object, 'exprMat'), 'denseMatrix')) { + abb_mat(object, nrows = 10, ncols = 10, header = FALSE) } else { # * other matrices * print(slot(object, 'exprMat')) diff --git a/R/utilities.R b/R/utilities.R index 25f39725f..3024eb141 100644 --- a/R/utilities.R +++ b/R/utilities.R @@ -453,7 +453,7 @@ is_utf8_output = function() { #' @description print abbreviated matrix exprObj. Works for Matrix pkg denseMatrix, #' matrix, data.frame and classes that inherit them. #' @keywords internal -abb_mat = function(exprObj, nrows, ncols) { +abb_mat = function(exprObj, nrows, ncols, header = TRUE) { mat = as.matrix(exprObj[]) four_names = head(colnames(mat), 4) mat_cols = ncol(mat) @@ -465,9 +465,13 @@ abb_mat = function(exprObj, nrows, ncols) { colnames(mat) = NULL # prints - cat('An object of class', class(exprObj), '\n') - cat(paste0('for spatial unit: "', exprObj@spat_unit, '" and feature type: "', exprObj@feat_type, '"\n')) - cat(' Provenance:', exprObj@provenance, '\n\ncontains:\n') + if(isTRUE(header)) { + cat('An object of class', class(exprObj), '\n') + cat(paste0('for spatial unit: "', exprObj@spat_unit, '" and feature type: "', exprObj@feat_type, '"\n')) + } + cat(' Provenance:', exprObj@provenance) + if(isTRUE(header)) cat('\n\ncontains:\n') + else cat('\n') cat(paste0(mat_rows, ' x ', mat_cols, ' dense matrix of class "', class(exprObj[]), '"\n\n')) print(mat) cat('\n First four colnames:') From 00ef06dd2ae25335a6dfad39c69a0aa62043dc80 Mon Sep 17 00:00:00 2001 From: RubD Date: Mon, 21 Nov 2022 16:40:29 -0500 Subject: [PATCH 02/45] fix join expression bug --- R/giotto.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/giotto.R b/R/giotto.R index d6e709ebe..92641cf4c 100644 --- a/R/giotto.R +++ b/R/giotto.R @@ -4794,7 +4794,7 @@ joinGiottoObjects = function(gobject_list, combmat = join_expression_matrices(matrix_list = savelist) - S4_expr_object = create_expr_obj(exprMat = combmat, + S4_expr_object = create_expr_obj(exprMat = combmat[['matrix']], name = mode, spat_unit = spat_unit, feat_type = feat_type) @@ -4803,7 +4803,7 @@ joinGiottoObjects = function(gobject_list, #comb_gobject@expression[[spat_unit]][[feat_type]][[mode]] = combmat$matrix - comb_gobject@feat_ID[[feat_type]] = combmat$sort_all_feats + comb_gobject@feat_ID[[feat_type]] = combmat[['sort_all_feats']] S4_feat_metadata = create_feat_meta_obj(spat_unit = spat_unit, feat_type = feat_type, From fc51aee14ebe144d7ab98de36cedfa9e30d34b96 Mon Sep 17 00:00:00 2001 From: jiajic <72078254+jiajic@users.noreply.github.com> Date: Tue, 22 Nov 2022 12:41:14 -0500 Subject: [PATCH 03/45] Update giotto_structures.R add hexVertices --- R/giotto_structures.R | 46 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/R/giotto_structures.R b/R/giotto_structures.R index 945d599bc..452b6e093 100644 --- a/R/giotto_structures.R +++ b/R/giotto_structures.R @@ -962,7 +962,7 @@ polyStamp = function(stamp_dt, #' given radius. Modified from \pkg{packcircles}. #' @param radius radius of circle to be drawn #' @param npoints number of vertices to generate -#' @seealso polyStamp rectVertices +#' @seealso polyStamp rectVertices hexVertices #' @return a data.table of circle vertices #' @export circleVertices = function(radius, @@ -981,7 +981,7 @@ circleVertices = function(radius, #' through \code{dims} param. #' @param dims named vector in the style of c(x = \code{numeric}, y = \code{numeric}) #' that defines the width (x) and height (y) of the generated rectangle polygon. -#' @seealso polyStamp circleVertices +#' @seealso polyStamp circleVertices hexVertices #' @return a data.table of rectangle vertices #' @export rectVertices = function(dims) { @@ -994,6 +994,48 @@ rectVertices = function(dims) { } +#' @title Generate regular hexagon vertices +#' @name hexVertices +#' @description Generates vertex coordinates for a regular hexagon. +#' @param radius radius of the hexagon +#' @param major_axis orientation of the major axis 'v' is vertical (default) +#' and 'h' is horizontal +#' @seealso polyStamp circleVertices rectVertices +#' @return a data.table of regular hexagon vertices +#' @export +hexVertices = function(radius, major_axis = c('v', 'h')) { + major_axis = match.arg(major_axis, choices = c('v', 'h')) + r = radius + v = data.table::data.table( + # counter clockwise + x = c( + 0, # A + (sqrt(3) * r)/2, # B + (sqrt(3) * r)/2, # C + 0, # D + -(sqrt(3) * r)/2, # E + -(sqrt(3) * r)/2 # F + ), + y = c( + r, # A + r/2, # B + -r/2, # C + -r, # D + -r/2, # E + r/2 # F + )) + if(major_axis == 'v') { + return(v) + } + if(major_axis == 'h') { + h = data.table::data.table() + h$x = v$y + h$y = v$x + return(h) + } +} + + ## ** feature points #### From 8ea5b4d9444b98f0674c9276e36730bcedf4cf76 Mon Sep 17 00:00:00 2001 From: jiajic <72078254+jiajic@users.noreply.github.com> Date: Tue, 22 Nov 2022 15:42:08 -0500 Subject: [PATCH 04/45] patch1 - cosmx convenience function ** WORK IN PROGRESS ** - split cosmx function into each different workflow for readability - added splitting of subcellular features into features vs negprobes feat_types - added documentation for unicode printing and color detection - ran document - added protein mean intensity expr matrix generation from metadata --- NAMESPACE | 1 + R/accessors.R | 11 +- R/auxiliary_giotto.R | 12 +- R/giotto.R | 844 ++++++++++++++------- R/giotto_structures.R | 2 +- R/utilities.R | 12 +- man/abb_mat.Rd | 2 +- man/box_chars.Rd | 17 +- man/circleVertices.Rd | 2 +- man/color_tag.Rd | 27 + man/createGiottoCosMxObject_aggregate.Rd | 24 + man/createGiottoCosMxObject_all.Rd | 39 + man/createGiottoCosMxObject_subcellular.Rd | 27 + man/hexVertices.Rd | 23 + man/joinGiottoObjects.Rd | 3 +- man/load_cosmx_folder_aggregate.Rd | 17 + man/load_cosmx_folder_subcellular.Rd | 21 + man/read_cosmx_folder.Rd | 24 + man/rectVertices.Rd | 2 +- man/set_default_feat_type.Rd | 4 +- man/set_default_spat_unit.Rd | 4 +- 21 files changed, 828 insertions(+), 290 deletions(-) create mode 100644 man/createGiottoCosMxObject_aggregate.Rd create mode 100644 man/createGiottoCosMxObject_all.Rd create mode 100644 man/createGiottoCosMxObject_subcellular.Rd create mode 100644 man/hexVertices.Rd create mode 100644 man/load_cosmx_folder_aggregate.Rd create mode 100644 man/load_cosmx_folder_subcellular.Rd create mode 100644 man/read_cosmx_folder.Rd diff --git a/NAMESPACE b/NAMESPACE index 56604d1ab..8ce3bd719 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -188,6 +188,7 @@ export(giottoToSeurat) export(giottoToSpatialExperiment) export(heatmSpatialCorFeats) export(heatmSpatialCorGenes) +export(hexVertices) export(hyperGeometricEnrich) export(insertCrossSectionGenePlot3D) export(insertCrossSectionSpatPlot3D) diff --git a/R/accessors.R b/R/accessors.R index 538d59439..33961579a 100644 --- a/R/accessors.R +++ b/R/accessors.R @@ -879,12 +879,15 @@ get_spatial_locations = function(gobject, if(!is.null(spat_unit)) { potential_spat_unit = names(slot(gobject, 'spatial_locs')) - if(!spat_unit %in% potential_spat_unit) stop('No spatial locations for spatial unit "', spat_unit, '" exist in spatial_locs slot.') + if(!spat_unit %in% potential_spat_unit) stop(wrap_txt('No spatial locations for spatial unit "', spat_unit, '" exist in spatial_locs slot.')) } # spatial locations spat_unit = set_default_spat_unit(gobject = gobject, - spat_unit = spat_unit) + spat_unit = spat_unit, + verbose = FALSE) + + # if NULL (not given) and spatial locations have been added, then use first one # if NULL (not given) and spatial locations have NOT been added, then keep NULL @@ -931,11 +934,11 @@ get_spatial_locations = function(gobject, } } } else { - if(isTRUE(verbose)) message('Other spatial locations outputs not yet supported') + if(isTRUE(verbose)) wrap_msg('Other spatial locations outputs not yet supported') } return(spatloc) } else { # case if not found - stop("The spatial locations with name ","'", spat_loc_name, "'"," can not be found \n") + stop(wrap_txt("The spatial locations with name ","'", spat_loc_name, "'"," can not be found \n")) } } diff --git a/R/auxiliary_giotto.R b/R/auxiliary_giotto.R index ae68851ab..90facd414 100644 --- a/R/auxiliary_giotto.R +++ b/R/auxiliary_giotto.R @@ -7,9 +7,11 @@ #' @name set_default_spat_unit #' @param gobject gobject #' @param spat_unit spatial unit +#' @param verbose be verbose about warning #' @keywords internal set_default_spat_unit = function(gobject, - spat_unit = NULL) { + spat_unit = NULL, + verbose = TRUE) { # set spatial unit @@ -22,7 +24,7 @@ set_default_spat_unit = function(gobject, } else if(!is.null(gobject@spatial_info)){ spat_unit = names(gobject@spatial_info)[[1]] } else { - warning('No default for spat_unit could be set \n') + if(isTRUE(verbose)) (warning('No default for spat_unit could be set \n')) } } @@ -39,10 +41,12 @@ set_default_spat_unit = function(gobject, #' @param gobject gobject #' @param feat_type feature type #' @param spat_unit spatial unit +#' @param verbose be verbose about warning #' @keywords internal set_default_feat_type = function(gobject, feat_type = NULL, - spat_unit) { + spat_unit, + verbose = TRUE) { # set spatial unit @@ -57,7 +61,7 @@ set_default_feat_type = function(gobject, } else if(!is.null(gobject@feat_info)){ feat_type = names(gobject@feat_info)[[1]] } else { - warning('No default for feat_type could be set \n') + if(isTRUE(verbose)) warning('No default for feat_type could be set \n') } } diff --git a/R/giotto.R b/R/giotto.R index bee47767f..584022cec 100644 --- a/R/giotto.R +++ b/R/giotto.R @@ -2964,52 +2964,7 @@ createGiottoCosMxObject = function(cosmx_dir = NULL, cores = NA, verbose = TRUE) { - # Define for data.table - fov = target = x_local_px = y_local_px = z = cell_ID = CenterX_global_px = CenterY_global_px = - CenterX_local_px = CenterY_local_px = NULL - - # 0. test if folder structure exists and is as expected - if(is.null(cosmx_dir) | !dir.exists(cosmx_dir)) stop('The full path to a cosmx directory must be given.\n') - if(isTRUE(verbose)) message('A structured CosMx directory will be used\n') - - # find directories (length = 1 if present, length = 0 if missing) - dir_items = list(`CellLabels folder` = '*CellLabels', - `CompartmentLabels folder` = '*CompartmentLabels', - `CellComposite folder` = '*CellComposite', - `CellOverlay folder` = '*CellOverlay', - `transcript locations file` = '*tx_file*', - `fov positions file` = '*fov_positions_file*', - `expression matrix file` = '*exprMat_file*', - `metadata file` = '*metadata_file*') - dir_items = lapply(dir_items, function(x) Sys.glob(paths = file.path(cosmx_dir, x))) - dir_items_lengths = lengths(dir_items) - - if(isTRUE(verbose)) { - message('Checking directory contents...') - for(item in names(dir_items)) { - if(dir_items_lengths[[item]] > 0) { - message('--| ' ,item, ' found') - } else { - warning(item, ' is missing\n') - } - } - } - - # select first directory in list if multiple are detected - if(any(dir_items_lengths > 1)) { - warning('Multiple matches for expected subdirectory item(s).\n First matching item selected') - - multiples = which(dir_items_lengths > 1) - for(mult_i in multiples) { - message(names(dir_items)[[mult_i]], 'multiple matches found:') - print(dir_items[[mult_i]]) - dir_items[[mult_i]] = dir_items[[mult_i]][[1]] - } - } - if(isTRUE(verbose)) message('Directory check done') - - - # 1. read in data + # 0. setup # set number of cores automatically, but with limit of 10 cores = determine_cores(cores) data.table::setDTthreads(threads = cores) @@ -3017,273 +2972,394 @@ createGiottoCosMxObject = function(cosmx_dir = NULL, # determine data to use data_to_use = match.arg(arg = data_to_use, choices = c('all','subcellular','aggregate')) - # load in subcellular information, subcellular FOV objects, then join - if(data_to_use == 'all' | data_to_use == 'subcellular') { - - if(isTRUE(verbose)) message('Loading subcellular information...') + # Define for data.table + fov = target = x_local_px = y_local_px = z = cell_ID = CenterX_global_px = CenterY_global_px = + CenterX_local_px = CenterY_local_px = NULL - # subcellular checks - if(!file.exists(dir_items$`transcript locations file`)) stop('No transcript locations file (.csv) detected') - if(!file.exists(dir_items$`fov positions file`)) stop('No fov positions file (.csv) detected') - # FOVs to load - fov_offset_file = fread(input = dir_items$`fov positions file`, nThread = cores) - if(is.null(FOVs)) FOVs = fov_offset_file$fov - FOV_ID = as.list(sprintf('%03d', FOVs)) + # 1. test if folder structure exists and is as expected + dir_items = read_cosmx_folder(cosmx_dir = cosmx_dir, + verbose = verbose) - #TODO Load only relevant portions of file? - tx_coord_all = fread(input = dir_items$`transcript locations file`, nThread = cores) - if(isTRUE(verbose)) message('Subcellular load done') + # 2. load and create giotto object + if(data_to_use == 'subcellular') { + cosmx_gobject = createGiottoCosMxObject_subcellular(dir_items, + FOVs = FOVs, + cores = cores, + verbose = verbose, + instructions = instructions) - fov_gobjects_list = lapply(FOV_ID, function(x) { + } - if(isTRUE(verbose)) message('Starting FOV ', x) + if(data_to_use == 'aggregate') { - # Build image paths - if(isTRUE(verbose)) message('Loading image information...') + cosmx_gobject = createGiottoCosMxObject_aggregate(dir_items, + cores = cores, + verbose = verbose, + instructions = instructions) - composite_dir = Sys.glob(paths = file.path(dir_items$`CellComposite folder`, paste0('*',x, '*'))) - cellLabel_dir = Sys.glob(paths = file.path(dir_items$`CellLabels folder`, paste0('*',x, '*'))) - compartmentLabel_dir = Sys.glob(paths = file.path(dir_items$`CompartmentLabels folder`, paste0('*',x, '*'))) - cellOverlay_dir = Sys.glob(paths = file.path(dir_items$`CellOverlay folder`, paste0('*',x, '*'))) - # Missing warnings - if(length(composite_dir) == 0) {warning('No composite images found') ; composite_dir = NULL} - if(length(cellLabel_dir) == 0) {stop('No cell mask images found')} # cell masks are necessary - if(length(compartmentLabel_dir) == 0) {warning('No compartment label images found') ; compartmentLabel_dir = NULL} - if(length(cellOverlay_dir) == 0) {warning('No cell polygon overlay images found') ; cellOverlay_dir = NULL} + } - if(isTRUE(verbose)) message('Image load done') + if(data_to_use == 'all') { - # get FOV specific tx locations - tx_coord = tx_coord_all[fov == as.numeric(x)] - tx_coord = tx_coord[,.(target, x_local_px, y_local_px, z)] - colnames(tx_coord) = c('feat_ID','x','y','z') + cosmx_gobject = createGiottoCosMxObject_all(dir_items, + FOVs = FOVs, + cores = cores, + verbose = verbose, + instructions = instructions) - # build giotto object - if(isTRUE(verbose)) message('Building subcellular giotto object...') - fov_subset = createGiottoObjectSubcellular(gpoints = list('rna' = tx_coord), - gpolygons = list('cell' = cellLabel_dir), - polygon_mask_list_params = list(mask_method = 'guess', - flip_vertical = TRUE, - flip_horizontal = FALSE, - shift_horizontal_step = FALSE), - instructions = instructions, - cores = cores) + } + # load in subcellular information, subcellular FOV objects, then join - # find centroids as spatial locations - if(isTRUE(verbose)) message('Finding polygon centroids as cell spatial locations...') - fov_subset = addSpatialCentroidLocations(fov_subset, - poly_info = 'cell', - spat_loc_name = 'raw') + # load in pre-generated aggregated expression matrix + if(data_to_use == 'aggregate' | data_to_use == 'all') { + } - # create and add giotto image objects - if(isTRUE(verbose)) message('Attaching image files...') - print(composite_dir) - print(cellOverlay_dir) - print(compartmentLabel_dir) - gImage_list = list() - # load image if files are found - if(!is.null(composite_dir)) gImage_list$composite = createGiottoLargeImage(raster_object = composite_dir, negative_y = F, name = 'composite') - if(!is.null(cellOverlay_dir)) gImage_list$overlay = createGiottoLargeImage(raster_object = cellOverlay_dir, negative_y = F, name = 'overlay') - if(!is.null(compartmentLabel_dir)) gImage_list$compartment = createGiottoLargeImage(raster_object = compartmentLabel_dir, negative_y = F, name = 'compartment') #TODO + message('done') + return(cosmx_gobject) +} - if(length(gImage_list) > 0) { - fov_subset = addGiottoImage(gobject = fov_subset, - largeImages = gImage_list) - # convert to MG for faster loading (particularly relevant for pulling from server) - fov_subset = convertGiottoLargeImageToMG(giottoLargeImage = gImage_list$composite, gobject = fov_subset, return_gobject = TRUE) - # fov_subset = convertGiottoLargeImageToMG(giottoLargeImage = gImage_list$overlay, gobject = fov_subset, return_gobject = TRUE) - # fov_subset = convertGiottoLargeImageToMG(giottoLargeImage = gImage_list$compartment, gobject = fov_subset, return_gobject = TRUE) - } else { - message('No images found for fov') - } +#' @title Load and create a CosMx Giotto object from subcellular info +#' @name createGiottoCosMxObject_subcellular +#' @inheritParams createGiottoCosMxObject +#' @keywords internal +createGiottoCosMxObject_subcellular = function(dir_items, + FOVs = NULL, + cores, + verbose = TRUE, + instructions = NULL) { + + # load tx detections and FOV offsets + data_list = load_cosmx_folder_subcellular(dir_items = dir_items, + FOVs = FOVs, + cores = cores, + verbose = verbose) + + # unpack data_list + FOV_ID = data_list$FOV_ID + fov_offset_file = data_list$fov_offset_file + tx_coord_all = data_list$tx_coord_all + + if(isTRUE(verbose)) wrap_msg('Splitting detections by feature vs neg probe') + all_IDs = tx_coord_all[, unique(target)] + neg_IDs = all_IDs[grepl(pattern = 'NegPrb', all_IDs)] + feat_IDs = all_IDs[!all_IDs %in% neg_IDs] + + # split detections DT + feat_coords_all = tx_coord_all[target %in% feat_IDs] + neg_coords_all = tx_coord_all[target %in% neg_IDs] + if(isTRUE(verbose)) { + message(' > Features: ', feat_coords_all[, .N]) + message(' > NegProbes: ', neg_coords_all[, .N]) + } + + # Start FOV lapply + fov_gobjects_list = lapply(FOV_ID, function(x) { + + if(isTRUE(verbose)) wrap_msg('[ FOV ', x, ']') + + # get FOV specific tx locations + if(isTRUE(verbose)) wrap_msg('Assigning FOV feature detections...') + + + # feature info + feat_coord = feat_coords_all[fov == as.numeric(x)] + feat_coord = feat_coord[,.(target, x_local_px, y_local_px, z)] + colnames(feat_coord) = c('feat_ID','x','y','z') + # neg probe info + neg_coord = neg_coords_all[fov == as.numeric(x)] + neg_coord = neg_coord[,.(target, x_local_px, y_local_px, z)] + colnames(neg_coord) = c('feat_ID','x','y','z') + + + # build giotto object + if(isTRUE(verbose)) wrap_msg('Building subcellular giotto object...') + fov_subset = createGiottoObjectSubcellular( + gpoints = list('rna' = feat_coord, + 'neg_probe' = neg_coord), + gpolygons = list('cell' = cellLabel_dir), + polygon_mask_list_params = list( + mask_method = 'guess', + flip_vertical = TRUE, + flip_horizontal = FALSE, + shift_horizontal_step = FALSE + ), + instructions = instructions, + cores = cores + ) + + + # find centroids as spatial locations + if(isTRUE(verbose)) wrap_msg('Finding polygon centroids as cell spatial locations...') + fov_subset = addSpatialCentroidLocations(fov_subset, + poly_info = 'cell', + spat_loc_name = 'raw') + + + # Build image paths + if(isTRUE(verbose)) message('Loading image information...') + + composite_dir = Sys.glob(paths = file.path(dir_items$`CellComposite folder`, paste0('*',x, '*'))) + cellLabel_dir = Sys.glob(paths = file.path(dir_items$`CellLabels folder`, paste0('*',x, '*'))) + compartmentLabel_dir = Sys.glob(paths = file.path(dir_items$`CompartmentLabels folder`, paste0('*',x, '*'))) + cellOverlay_dir = Sys.glob(paths = file.path(dir_items$`CellOverlay folder`, paste0('*',x, '*'))) + # Missing warnings + if(length(composite_dir) == 0) {warning('[ FOV ', x, ' ] No composite images found') ; composite_dir = NULL} + if(length(cellLabel_dir) == 0) {stop('[ FOV ', x, ' ] No cell mask images found')} # cell masks are necessary + if(length(compartmentLabel_dir) == 0) {warning('[ FOV ', x, ' ] No compartment label images found') ; compartmentLabel_dir = NULL} + if(length(cellOverlay_dir) == 0) {warning('[ FOV ', x, ' ] No cell polygon overlay images found') ; cellOverlay_dir = NULL} + + if(isTRUE(verbose)) message('Image load done') + + # create and add giotto image objects + if(isTRUE(verbose)) message('Attaching image files...') + print(composite_dir) + print(cellOverlay_dir) + print(compartmentLabel_dir) + + gImage_list = list() + + # load image if files are found + if(!is.null(composite_dir)) + gImage_list$composite = createGiottoLargeImage(raster_object = composite_dir, + negative_y = F, + name = 'composite') + if(!is.null(cellOverlay_dir)) + gImage_list$overlay = createGiottoLargeImage(raster_object = cellOverlay_dir, + negative_y = F, + name = 'overlay') + if(!is.null(compartmentLabel_dir)) + gImage_list$compartment = createGiottoLargeImage(raster_object = compartmentLabel_dir, + negative_y = F, + name = 'compartment') #TODO + + + + if(length(gImage_list) > 0) { + fov_subset = addGiottoImage(gobject = fov_subset, + largeImages = gImage_list) + + # convert to MG for faster loading (particularly relevant for pulling from server) + fov_subset = convertGiottoLargeImageToMG(giottoLargeImage = gImage_list$composite, + gobject = fov_subset, + return_gobject = TRUE, + verbose = FALSE) + # fov_subset = convertGiottoLargeImageToMG(giottoLargeImage = gImage_list$overlay, gobject = fov_subset, return_gobject = TRUE) + # fov_subset = convertGiottoLargeImageToMG(giottoLargeImage = gImage_list$compartment, gobject = fov_subset, return_gobject = TRUE) + } else { + message('No images found for fov') + } - }) #lapply end - # join giotto objects according to FOV positions file - if(isTRUE(verbose)) message('Joining FOV gobjects...') - new_gobj_names = paste0('fov', FOV_ID) + }) #lapply end - id_match = match(as.numeric(FOV_ID), fov_offset_file$fov) - x_shifts = fov_offset_file[id_match]$x_global_px - y_shifts = fov_offset_file[id_match]$y_global_px + # join giotto objects according to FOV positions file + if(isTRUE(verbose)) message('Joining FOV gobjects...') + new_gobj_names = paste0('fov', FOV_ID) - # Join giotto objects - cosmx_gobject = joinGiottoObjects(gobject_list = fov_gobjects_list, - gobject_names = new_gobj_names, - join_method = 'shift', - x_shift = x_shifts, - y_shift = y_shifts) + id_match = match(as.numeric(FOV_ID), fov_offset_file$fov) + x_shifts = fov_offset_file[id_match]$x_global_px + y_shifts = fov_offset_file[id_match]$y_global_px - } + # Join giotto objects + cosmx_gobject = joinGiottoObjects(gobject_list = fov_gobjects_list, + gobject_names = new_gobj_names, + join_method = 'shift', + x_shift = x_shifts, + y_shift = y_shifts) - # load in pre-generated aggregated expression matrix - if(data_to_use == 'aggregate' | data_to_use == 'all') { +} - # load aggregate information - message('Loading provided aggregated information...') - # aggregate checks - if(!file.exists(dir_items$`expression matrix file`)) stop('No expression matrix file (.csv) detected') - if(!file.exists(dir_items$`metadata file`)) stop('No metadata file (.csv) detected. Needed for cell spatial locations.') - # read in aggregate data - expr_mat = fread(input = dir_items$`expression matrix file`, nThread = cores) - metadata = fread(input = dir_items$`metadata file`, nThread = cores) +#' @title Load and create a CosMx Giotto object from aggregate info +#' @name createGiottoCosMxObject_aggregate +#' @inheritParams createGiottoCosMxObject +#' @keywords internal +createGiottoCosMxObject_aggregate = function(dir_items, + cores, + verbose = TRUE, + instructions = NULL) { - # setorder expression and spatlocs - data.table::setorder(metadata, fov, cell_ID) - data.table::setorder(expr_mat, fov, cell_ID) - # generate unique cell IDs - expr_mat$cell_ID = paste0('fov', sprintf('%03d', expr_mat$fov), '-', 'cell_', expr_mat$cell_ID) - expr_mat = expr_mat[,-'fov'] + data_list = load_cosmx_folder_aggregate(dir_items = dir_items, + cores = cores, + verbose = verbose) - metadata$fov_cell_ID = metadata$cell_ID - metadata$cell_ID = paste0('fov', sprintf('%03d', metadata$fov), '-', 'cell_', metadata$cell_ID) - # reorder - metadata = metadata[,.SD, c('cell_ID','fov','fov_cell_ID')] + # unpack data_list + spatlocs = data_list$spatlocs + spatlocs_fov = data_list$spatlocs_fov + metadata = data_list$metadata + protM = data_list$protM + spM = data_list$spM + fov_shifts = data_list$fov_shifts - # extract spatial locations - spatlocs = metadata[,.(CenterX_global_px, CenterY_global_px, cell_ID)] - spatlocs_fov = metadata[,.(CenterX_local_px, CenterY_local_px, cell_ID)] - #TODO FOV shifts can be regenerated from this if necessary + # create standard gobject from aggregate matrix + if(data_to_use == 'aggregate') { - # rename spatloc column names - spatloc_colnames = c('sdimx', 'sdimy', 'cell_ID') - colnames(spatlocs) = spatloc_colnames - colnames(spatlocs_fov) = spatloc_colnames + # Create aggregate gobject + if(isTRUE(verbose)) message('Building giotto object...') + cosmx_gobject = createGiottoObject(expression = list('raw' = spM, 'protein' = protM), + cell_metadata = list('cell' = list('rna' = metadata, + 'protein' = metadata)), + spatial_locs = spatlocs, + instructions = instructions, + cores = cores) - # cleanup metadata and spatlocs - metadata = metadata[,c('CenterX_global_px', 'CenterY_global_px', 'CenterX_local_px', 'CenterY_local_px'):=NULL] - # find unique cell_IDs present in both expression and metadata - giotto_cell_ID = unique(intersect(expr_mat$cell_ID, metadata$cell_ID)) - # subset to only unique cell_IDs - expr_mat = expr_mat[cell_ID %in% giotto_cell_ID,] - metadata = metadata[cell_ID %in% giotto_cell_ID,] + # load in images + img_ID = data.table::data.table(fov = fov_shifts[, fov], + img_name = paste0('fov', sprintf('%03d', fov_shifts[, fov]), '-image')) - # convert expression to sparse matrix - spM = Matrix::Matrix(as.matrix(expr_mat[,-1]), dimnames = list(expr_mat[[1]], colnames(expr_mat[,-1])), sparse = TRUE) - spM = t_flex(spM) + if(isTRUE(verbose)) message('Attaching image files...') + composite_dir = Sys.glob(paths = file.path(dir_items$`CellComposite folder`, paste0('/*'))) + cellLabel_dir = Sys.glob(paths = file.path(dir_items$`CellLabels folder`, paste0('/*'))) + compartmentLabel_dir = Sys.glob(paths = file.path(dir_items$`CompartmentLabels folder`, paste0('/*'))) + overlay_dir = Sys.glob(paths = file.path(dir_items$`CellOverlay folder`, paste0('/*'))) - ## Ready for downstream aggregate gobject creation or appending into existing subcellular Giotto object ## + if(length(cellLabel_imgList) > 0) cellLabel_imgList = lapply(cellLabel_dir, function(x) {createGiottoLargeImage(x,name = 'cellLabel',negative_y = TRUE)}) + if(length(composite_imgList) > 0) composite_imgList = lapply(composite_dir, function(x) {createGiottoLargeImage(x,name = 'composite',negative_y = TRUE)}) + if(length(compartmentLabel_dir) > 0) compartmentLabel_imgList = lapply(compartmentLabel_dir, function(x) {createGiottoLargeImage(x,name = 'composite',negative_y = TRUE)}) + if(length(overlay_dir) > 0) overlay_imgList = lapply(overlay_dir, function(x) {createGiottoLargeImage(x,name = 'composite',negative_y = TRUE)}) - # create standard gobject from aggregate matrix - if(data_to_use == 'aggregate') { + } - # Create aggregate gobject - if(isTRUE(verbose)) message('Building giotto object...') - cosmx_gobject = createGiottoObject(expression = list('raw' = spM), - cell_metadata = list('cell' = list('rna' = metadata)), - spatial_locs = spatlocs, - instructions = instructions, - cores = cores) +} - # # load in images if FOV positions are available - # if(file.exists(dir_items$`fov positions file`)) { - # fov_offset_file = fread(input = dir_items$`fov positions file`, nThread = cores) - # - # # load images - # if(isTRUE(verbose)) message('Attaching image files...') - # composite_dir = Sys.glob(paths = file.path(dir_items$`CellComposite folder`, paste0('/*'))) - # cellLabel_dir = Sys.glob(paths = file.path(dir_items$`CellLabels folder`, paste0('/*'))) - # compartmentLabel_dir = Sys.glob(paths = file.path(dir_items$`CompartmentLabels folder`, paste0('/*'))) - # overlay_dir = Sys.glob(paths = file.path(dir_items$`CellOverlay folder`, paste0('/*'))) - # - # if(length(cellLabel_imgList) > 0) cellLabel_imgList = lapply(cellLabel_dir, function(x) {createGiottoLargeImage(x,name = 'cellLabel',negative_y = TRUE)}) - # if(length(composite_imgList) > 0) composite_imgList = lapply(composite_dir, function(x) {createGiottoLargeImage(x,name = 'composite',negative_y = TRUE)}) - # if(length(compartmentLabel_dir) > 0) compartmentLabel_imgList = lapply(compartmentLabel_dir, function(x) {createGiottoLargeImage(x,name = 'composite',negative_y = TRUE)}) - # if(length(overlay_dir) > 0) overlay_imgList = lapply(overlay_dir, function(x) {createGiottoLargeImage(x,name = 'composite',negative_y = TRUE)}) - # - # - # } else {warning('No FOV positions file (.csv) detected\n No images or polygons will be loaded.')} - } - } +#' @title Load and create a CosMx Giotto object from subcellular and aggregate info +#' @name createGiottoCosMxObject_all +#' @param dir_items list of full directory paths from \code{read_cosmx_folder} +#' @inheritParams createGiottoCosMxObject +#' @details Both \emph{subcellular} (subellular transcript detection information) and +#' \emph{aggregate} (aggregated detection count matrices by cell polygon from NanoString) +#' data will be loaded in. The two will be separated into 'cell' and 'cell_agg' +#' spatial units in order to denote the difference in origin of the two. +#' @seealso createGiottoCosMxObject createGiottoCosMxObject_aggregate +#' createGiottoCosMxObject_subcellular +#' @keywords internal +createGiottoCosMxObject_all = function(dir_items, + FOVs, + cores, + verbose = TRUE, + instructions = NULL) { + + # 1. create subcellular giotto as spat_unit 'cell' + cosmx_gobject = createGiottoCosMxObject_subcellular(dir_items = dir_items, + FOVs = FOVs, + cores = cores, + verbose = verbose, + instructions = instructions) + + # 2. load and append aggregated information in spat_unit 'cell_agg' + agg_data = load_cosmx_folder_aggregate(dir_items = dir_items, + cores = cores, + verbose = verbose) + + # unpack data_list + spatlocs = agg_data$spatlocs + spatlocs_fov = agg_data$spatlocs_fov + metadata = agg_data$metadata + protM = agg_data$protM + spM = agg_data$spM # add in pre-generated aggregated expression matrix information for 'all' workflow - if(data_to_use == 'all') { - - # Add aggregate expression information - if(isTRUE(verbose)) message('Appending provided aggregate expression data as...\n spat_unit: "cell_agg"\n feat_type: "rna"\n name: "raw"') - # add expression data to expression slot - cosmx_gobject = set_expression_values(cosmx_gobject, - spat_unit = 'cell_agg', - feat_type = 'rna', - name = 'raw', - values = spM) - - # Add spatial locations - if(isTRUE(verbose)) message('Appending metadata provided spatial locations data as...\n --> spat_unit: "cell_agg" name: "raw"\n --> spat_unit: "cell" name: "raw_fov"') - if(isTRUE(verbose)) message('Polygon centroid derived spatial locations assigned as...\n --> spat_unit: "cell" name: "raw" (default)') - locsObj = new('spatLocsObj', - name = 'raw', - coordinates = spatlocs, - spat_unit = 'cell_agg', - provenance = 'cell_agg', - misc = NULL) - locsObj_fov = new('spatLocsObj', - name = 'raw_fov', - coordinates = spatlocs_fov, - spat_unit = 'cell_agg', - provenance = 'cell_agg', - misc = NULL) - - cosmx_gobject = set_spatial_locations(cosmx_gobject, spatlocs = locsObj) - cosmx_gobject = set_spatial_locations(cosmx_gobject, spatlocs = locsObj_fov) - - # cosmx_gobject = set_spatial_locations(cosmx_gobject, - # spat_unit = 'cell_agg', - # spat_loc_name = 'raw', - # spatlocs = spatlocs) - # cosmx_gobject = set_spatial_locations(cosmx_gobject, - # spat_unit = 'cell_agg', - # spat_loc_name = 'raw_fov', - # spatlocs = spatlocs_fov) - - # initialize cell and feat IDs and metadata slots for 'cell_agg' spat_unit - cosmx_gobject@cell_ID[['cell_agg']] = colnames(cosmx_gobject@expression[['cell_agg']][[1]][[1]]) - cosmx_gobject@feat_ID[['rna']] = unique(c(cosmx_gobject@feat_ID, rownames(cosmx_gobject@expression[['cell_agg']][['rna']][[1]]))) - cosmx_gobject@cell_metadata[['cell_agg']][['rna']] = data.table::data.table(cell_ID = cosmx_gobject@cell_ID[['cell_agg']]) - cosmx_gobject@feat_metadata[['cell_agg']][['rna']] = data.table::data.table(feat_ID = cosmx_gobject@feat_ID[['rna']]) - - # Add metadata to both the given and the poly spat_units - if(isTRUE(verbose)) message('Appending provided cell metadata...') - cosmx_gobject = addCellMetadata(cosmx_gobject, - spat_unit = 'cell', - feat_type = 'rna', - new_metadata = metadata, - by_column = TRUE, - column_cell_ID = 'cell_ID') - cosmx_gobject = addCellMetadata(cosmx_gobject, - spat_unit = 'cell_agg', - feat_type = 'rna', - new_metadata = metadata, - by_column = TRUE, - column_cell_ID = 'cell_ID') - } - message('done') - return(cosmx_gobject) + # Add aggregate expression information + if(isTRUE(verbose)) wrap_msg('Appending provided aggregate expression data as... + spat_unit: "cell_agg" + feat_type: "rna" + name: "raw"') + # add expression data to expression slot + s4_expr = create_expr_obj(name = 'raw', + exprMat = spM, + spat_unit = 'cell_agg', + feat_type = 'rna', + provenance = 'cell_agg') + + cosmx_gobject = set_expression_values(cosmx_gobject, values = s4_expr) + + # Add spatial locations + if(isTRUE(verbose)) wrap_msg('Appending metadata provided spatial locations data as... + --> spat_unit: "cell_agg" name: "raw" + --> spat_unit: "cell" name: "raw_fov"') + if(isTRUE(verbose)) wrap_msg('Polygon centroid derived spatial locations assigned as... + --> spat_unit: "cell" name: "raw" (default)') + + locsObj = create_spat_locs_obj(name = 'raw', + coordinates = spatlocs, + spat_unit = 'cell_agg', + provenance = 'cell_agg') + locsObj_fov = create_spat_locs_obj(name = 'raw_fov', + coordinates = spatlocs_fov, + spat_unit = 'cell_agg', + provenance = 'cell_agg') + + cosmx_gobject = set_spatial_locations(cosmx_gobject, spatlocs = locsObj) + cosmx_gobject = set_spatial_locations(cosmx_gobject, spatlocs = locsObj_fov) + + # cosmx_gobject = set_spatial_locations(cosmx_gobject, + # spat_unit = 'cell_agg', + # spat_loc_name = 'raw', + # spatlocs = spatlocs) + # cosmx_gobject = set_spatial_locations(cosmx_gobject, + # spat_unit = 'cell_agg', + # spat_loc_name = 'raw_fov', + # spatlocs = spatlocs_fov) + + # initialize cell and feat IDs and metadata slots for 'cell_agg' spat_unit + agg_cell_ID = colnames(s4_expr[]) + agg_feat_ID = rownames(s4_expr[]) + + sub_feat_ID = get_feat_id(cosmx_gobject, feat_type = 'rna') + feat_ID_new = unique(c(agg_feat_ID, sub_feat_ID)) + + cosmx_gobject = set_cell_id(gobject = cosmx_gobject, + spat_unit = 'cell_agg', + cell_IDs = agg_cell_ID) + cosmx_gobject = set_feat_id(gobject = cosmx_gobject, + feat_type = 'rna', + feat_IDs = feat_ID_new) + + # cell metadata + + # cosmx_gobject@cell_ID[['cell_agg']] = colnames(cosmx_gobject@expression[['cell_agg']][[1]][[1]]) + # cosmx_gobject@feat_ID[['rna']] = unique(c(cosmx_gobject@feat_ID, rownames(cosmx_gobject@expression[['cell_agg']][['rna']][[1]]))) + # cosmx_gobject@cell_metadata[['cell_agg']][['rna']] = data.table::data.table(cell_ID = cosmx_gobject@cell_ID[['cell_agg']]) + # cosmx_gobject@feat_metadata[['cell_agg']][['rna']] = data.table::data.table(feat_ID = cosmx_gobject@feat_ID[['rna']]) + + # Add metadata to both the given and the poly spat_units + if(isTRUE(verbose)) message('Appending provided cell metadata...') + cosmx_gobject = addCellMetadata(cosmx_gobject, + spat_unit = 'cell', + feat_type = 'rna', + new_metadata = metadata, + by_column = TRUE, + column_cell_ID = 'cell_ID') + cosmx_gobject = addCellMetadata(cosmx_gobject, + spat_unit = 'cell_agg', + feat_type = 'rna', + new_metadata = metadata, + by_column = TRUE, + column_cell_ID = 'cell_ID') } @@ -3615,6 +3691,202 @@ createGiottoXeniumObject_aggregate = function(data_list, #### folder detection and loading #### +#' @title Read a structured CosMx folder +#' @name read_cosmx_folder +#' @inheritParams createGiottoCosMxObject +#' @seealso createGiottoCosMxObject load_cosmx_folder +#' @return path_list a list of cosmx files discovered and their filepaths. NULL +#' values denote missing items +#' @keywords internal +read_cosmx_folder = function(cosmx_dir, + verbose = TRUE) { + + ch = box_chars() + + if(is.null(cosmx_dir) | !dir.exists(cosmx_dir)) stop('The full path to a cosmx directory must be given.\n') + if(isTRUE(verbose)) wrap_msg('A structured CosMx directory will be used\n') + + # find directories (length = 1 if present, length = 0 if missing) + dir_items = list(`CellLabels folder` = '*CellLabels', + `CompartmentLabels folder` = '*CompartmentLabels', + `CellComposite folder` = '*CellComposite', + `CellOverlay folder` = '*CellOverlay', + `transcript locations file` = '*tx_file*', + `fov positions file` = '*fov_positions_file*', + `expression matrix file` = '*exprMat_file*', + `metadata file` = '*metadata_file*') + dir_items = lapply(dir_items, function(x) Sys.glob(paths = file.path(cosmx_dir, x))) + dir_items_lengths = lengths(dir_items) + + if(isTRUE(verbose)) { + message('Checking directory contents...') + for(item in names(dir_items)) { + if(dir_items_lengths[[item]] > 0) { + message(ch$s, '> ' ,item, ' found') + } else { + warning(item, ' is missing\n') + } + } + } + + # select first directory in list if multiple are detected + if(any(dir_items_lengths > 1)) { + warning('Multiple matches for expected subdirectory item(s).\n First matching item selected') + + multiples = which(dir_items_lengths > 1) + for(mult_i in multiples) { + message(names(dir_items)[[mult_i]], 'multiple matches found:') + print(dir_items[[mult_i]]) + dir_items[[mult_i]] = dir_items[[mult_i]][[1]] + } + } + if(isTRUE(verbose)) message('Directory check done') + + return(dir_items) + +} + + +#' @title Load CosMx folder subcellular info +#' @name load_cosmx_folder_subcellular +#' @description loads in the feature detections information. Note that the mask +#' images are still required for a working subcellular object, and those are loaded +#' in \code{\link{createGiottoCosMxObject_subcellular}} +#' @inheritParams createGiottoCosMxObject +#' @keywords internal +load_cosmx_folder_subcellular = function(dir_items, + FOVs = NULL, + cores, + verbose = TRUE) { + + if(isTRUE(verbose)) wrap_msg('Loading subcellular information...') + + # subcellular checks + if(!file.exists(dir_items$`transcript locations file`)) + stop(wrap_txt('No transcript locations file (.csv) detected')) + if(!file.exists(dir_items$`fov positions file`)) + stop(wrap_txt('No fov positions file (.csv) detected')) + + # FOVs to load + if(isTRUE(verbose)) wrap_msg('Loading FOV offsets...') + fov_offset_file = fread(input = dir_items$`fov positions file`, nThread = cores) + if(is.null(FOVs)) FOVs = fov_offset_file$fov # default to ALL FOVs + FOV_ID = as.list(sprintf('%03d', FOVs)) + + #TODO Load only relevant portions of file? + + if(isTRUE(verbose)) wrap_msg('Loading transcript level info...') + tx_coord_all = fread(input = dir_items$`transcript locations file`, nThread = cores) + if(isTRUE(verbose)) wrap_msg('Subcellular load done') + + data_list = list( + 'FOV_ID' = FOV_ID, + 'fov_offset_file' = fov_offset_file, + 'tx_coord_all' = tx_coord_all + ) + + return(data_list) + +} + + + +#' @title Load CosMx folder aggregate info +#' @name load_cosmx_folder_aggregate +#' @inheritParams createGiottoCosMxObject +#' @keywords internal +load_cosmx_folder_aggregate = function(dir_items, + cores, + verbose = TRUE) { + + # load aggregate information + wrap_msg('Loading provided aggregated information...') + + # aggregate checks + if(!file.exists(dir_items$`expression matrix file`)) stop(wrap_txt('No expression matrix file (.csv) detected')) + if(!file.exists(dir_items$`metadata file`)) stop(wrap_txt('No metadata file (.csv) detected. Needed for cell spatial locations.')) + + # read in aggregate data + expr_mat = fread(input = dir_items$`expression matrix file`, nThread = cores) + metadata = fread(input = dir_items$`metadata file`, nThread = cores) + + # setorder expression and spatlocs + data.table::setorder(metadata, fov, cell_ID) + data.table::setorder(expr_mat, fov, cell_ID) + + + # generate unique cell IDs + expr_mat[, cell_ID := paste0('fov', sprintf('%03d', fov), '-', 'cell_', cell_ID)] + # expr_mat$cell_ID = paste0('fov', sprintf('%03d', expr_mat$fov), '-', 'cell_', expr_mat$cell_ID) + expr_mat = expr_mat[, fov := NULL] + + metadata[, fov_cell_ID := cell_ID] + metadata[, cell_ID := paste0('fov', sprintf('%03d', fov), '-', 'cell_', cell_ID)] + # metadata$cell_ID = paste0('fov', sprintf('%03d', metadata$fov), '-', 'cell_', metadata$cell_ID) + # reorder + metadata = metadata[,.SD, c('cell_ID','fov','fov_cell_ID')] + + + # extract spatial locations + spatlocs = metadata[,.(CenterX_global_px, CenterY_global_px, cell_ID)] + spatlocs_fov = metadata[,.(CenterX_local_px, CenterY_local_px, cell_ID)] + # regenerate FOV shifts + metadata[, x_shift := CenterX_global_px - CenterX_local_px] + metadata[, y_shift := CenterY_global_px - CenterY_local_px] + fov_shifts = metadata[, .(mean(x_shift), mean(y_shift)), fov] + colnames(fov_shifts) = c('fov', 'x_shift', 'y_shift') + + + # rename spatloc column names + spatloc_oldnames = c('CenterX_global_px', 'CenterY_global_px', 'cell_ID') + spatloc_oldnames_fov = c('CenterX_local_px', 'CenterY_local_px', 'cell_ID') + spatloc_newnames = c('sdimx', 'sdimy', 'cell_ID') + data.table::setnames(spatlocs, old = spatloc_oldnames, new = spatloc_newnames) + data.table::setnames(spatlocs_fov, old = spatloc_oldnames_fov, new = spatloc_newnames) + + # cleanup metadata and spatlocs + metadata = metadata[,c('CenterX_global_px', 'CenterY_global_px', 'CenterX_local_px', 'CenterY_local_px') := NULL] + # find unique cell_IDs present in both expression and metadata + giotto_cell_ID = unique(intersect(expr_mat$cell_ID, metadata$cell_ID)) + + # subset to only unique cell_IDs + expr_mat = expr_mat[cell_ID %in% giotto_cell_ID,] + metadata = metadata[cell_ID %in% giotto_cell_ID,] + + + # convert protein metadata to expr mat + # take all mean intensity protein information except for MembraneStain and DAPI + protein_meta_cols = colnames(metadata) + protein_meta_cols = protein_meta_cols[grepl(pattern = 'Mean.*', x = protein_meta_cols)] + protein_meta_cols = protein_meta_cols[!protein_meta_cols %in% c('Mean.MembraneStain', 'Mean.DAPI')] + protein_meta_cols = c('cell_ID', protein_meta_cols) + + prot_expr = metadata[, protein_meta_cols, with = FALSE] + prot_cell_ID = metadata[, cell_ID] + protM = Matrix::Matrix(as.matrix(prot_expr[,-1]), dimnames = list(prot_expr[[1]], colnames(prot_expr[,-1])), sparse = FALSE) + protM = t_flex(protM) + + # convert expression to sparse matrix + spM = Matrix::Matrix(as.matrix(expr_mat[,-1]), dimnames = list(expr_mat[[1]], colnames(expr_mat[,-1])), sparse = TRUE) + spM = t_flex(spM) + + ## Ready for downstream aggregate gobject creation or appending into existing subcellular Giotto object ## + + data_list = list( + 'spatlocs' = spatlocs, + 'spatlocs_fov' = spatlocs_fov, + 'metadata' = metadata, + 'protM' = protM, + 'spM' = spM, + 'fov_shifts' = fov_shifts + ) + + return(data_list) + +} + + + #' @title Read a structured xenium folder #' @name read_xenium_folder #' @inheritParams createGiottoXeniumObject @@ -4165,7 +4437,9 @@ join_cell_meta = function(dt_list) { #' @param y_shift list of values to shift along y-axis if method is shift #' @param x_padding padding between datasets/images if method is shift #' @param y_padding padding between datasets/images if method is shift +# @param dry_run (experimental) Works best for join_method 'shift' or 'no_change'. #' @param verbose be verbose +#' Preview where each gobject will be in space with bounding polygons #' @return giotto object #' @details This function joins both the expression and spatial information of #' multiple giotto objects into a single one. Giotto supports multiple ways of @@ -4216,6 +4490,7 @@ joinGiottoObjects = function(gobject_list, y_shift = NULL, x_padding = NULL, y_padding = NULL, + # dry_run = FALSE, verbose = TRUE) { # define for data.table @@ -4224,11 +4499,11 @@ joinGiottoObjects = function(gobject_list, n_gobjects = length(gobject_list) ## check general input params - if(n_gobjects == 0) stop('A list of Giotto objects to be joined must be provided.') - if(n_gobjects == 1) stop('Only one gobject provided in gobject_list.') - if(!is.vector(gobject_names) | !is.character(gobject_names)) stop('gobject_names need to be a vector with unique names for the giotto objects') - if(n_gobjects != length(gobject_names)) stop('each giotto object in the list needs to have a unique (short) name') - if(is.null(join_method)) message('No join_method given. Defaulting to "shift"') + if(n_gobjects == 0) stop(wrap_txt('A list of Giotto objects to be joined must be provided.')) + if(n_gobjects == 1) stop(wrap_txt('Only one gobject provided in gobject_list.')) + if(!is.vector(gobject_names) | !is.character(gobject_names)) stop(wrap_txt('gobject_names need to be a vector with unique names for the giotto objects')) + if(n_gobjects != length(gobject_names)) stop(wrap_txt('each giotto object in the list needs to have a unique (short) name')) + if(is.null(join_method)) wrap_msg('No join_method given. Defaulting to "shift"') ## determine join method @@ -4236,16 +4511,15 @@ joinGiottoObjects = function(gobject_list, if(isTRUE(verbose)) message('Join method:', join_method) - # **** For shift workflow **** if(join_method == 'shift') { # Make sure enough x_shift and y_shift values are given to cover all gobjects - if(!is.null(x_shift)) if(length(x_shift) != n_gobjects) stop('A numeric vector with an x_shift value for each gobject in gobject_list must be given.\n') - if(!is.null(y_shift)) if(length(y_shift) != n_gobjects) stop('A numeric vector with a y_shift value for each gobject in gobject_list must be given.\n') + if(!is.null(x_shift)) if(length(x_shift) != n_gobjects) stop(wrap_txt('A numeric vector with an x_shift value for each gobject in gobject_list must be given.\n')) + if(!is.null(y_shift)) if(length(y_shift) != n_gobjects) stop(wrap_txt('A numeric vector with a y_shift value for each gobject in gobject_list must be given.\n')) # Set defaults if no shift params are given if(is.null(x_shift) & is.null(y_shift) & is.null(x_padding) & is.null(y_padding)) { - message('No xy shift or specific padding values given. Using defaults: x_padding = 1000') + wrap_msg('No xy shift or specific padding values given. Using defaults: x_padding = 1000') x_padding = 1000 } # Assign default padding values if NULL @@ -4283,6 +4557,41 @@ joinGiottoObjects = function(gobject_list, } + # TODO # **** dry run **** + # if(isTRUE(dry_run)) { + # if(isTRUE(verbose)) wrap_msg('dry_run = TRUE: + # Spatial preview of join operation.') + # # Detect sources of bounds info + # avail_bound_info = list( + # avail_img = lapply(gobject_list, list_images), + # avail_spat_info = lapply(gobject_list, list_spatial_info), + # avail_feat_info = lapply(gobject_list, list_feature_info), + # avail_spatlocs = lapply(gobject_list, list_spatial_locations) + # ) + # avail_bound = lapply(avail_bound_info, function(avail) { + # isTRUE(!is.null(unlist(avail))) + # }) + # if(is.null(unlist(avail_bound))) stop(wrap_txt('dry_run error: No shared sources of bounds info + # Previewing from heterogenous sources not yet implemented')) + # + # bound_to_use = avail_bound_info[names(avail_bound_info)[1]] + # + # # get bound info + # if(names(bound_to_use) == 'avail_img') { + # + # } + # if(names(bound_to_use) == 'avail_spat_info') { + # + # } + # if(names(bound_to_use) == 'avail_feat_info') { + # + # } + # if(names(bound_to_use) == 'avail_spatlocs') { + # + # } + # + # } + # keep instructions from first giotto object first_instructions = gobject_list[[1]]@instructions @@ -4528,7 +4837,8 @@ joinGiottoObjects = function(gobject_list, spat_unit = spat_unit_i, spat_loc_name = locs_i, output = 'spatLocsObj', - copy_obj = TRUE) + copy_obj = TRUE, + verbose = FALSE) myspatlocs = slot(spat_obj, 'coordinates') if(join_method == 'z_stack') { diff --git a/R/giotto_structures.R b/R/giotto_structures.R index 452b6e093..6518ccf79 100644 --- a/R/giotto_structures.R +++ b/R/giotto_structures.R @@ -1014,7 +1014,7 @@ hexVertices = function(radius, major_axis = c('v', 'h')) { (sqrt(3) * r)/2, # C 0, # D -(sqrt(3) * r)/2, # E - -(sqrt(3) * r)/2 # F + -(sqrt(3) * r)/2 # F ), y = c( r, # A diff --git a/R/utilities.R b/R/utilities.R index 3024eb141..5b8e4bcf4 100644 --- a/R/utilities.R +++ b/R/utilities.R @@ -422,7 +422,7 @@ box_chars = function() { } -#' @describeIn box_chars +#' @describeIn box_chars Determine if print is latex output #' @keywords internal is_latex_output = function() { if(!('knitr' %in% loadedNamespaces())) return(FALSE) @@ -430,7 +430,7 @@ is_latex_output = function() { } -#' @describeIn box_chars +#' @describeIn box_chars Determine if system is using UTF-8 encoding #' @keywords internal is_utf8_output = function() { opt = getOption('cli.unicode', default = NULL) @@ -551,7 +551,7 @@ color_tag = function() { } -#' @describeIn color_tag +#' @describeIn color_tag Determine if system should print color #' @keywords internal use_color_text = function() { opt = getOption('giotto.color_show', default = NULL) @@ -567,7 +567,7 @@ use_color_text = function() { } -#' @describeIn color_tag +#' @describeIn color_tag Determine if system can print at least 8 colors #' @keywords internal ansi_colors = function() { @@ -620,7 +620,7 @@ ansi_colors = function() { } -#' @describeIn color_tag +#' @describeIn color_tag Determine if emacs can print color #' @keywords internal is_emacs_with_color = function() { (Sys.getenv('EMACS') != '' || Sys.getenv('INSIDE_EMACS') != @@ -629,7 +629,7 @@ is_emacs_with_color = function() { } -#' @describeIn color_tag +#' @describeIn color_tag Determine emacs version #' @keywords internal emacs_version = function() { ver <- Sys.getenv('INSIDE_EMACS') diff --git a/man/abb_mat.Rd b/man/abb_mat.Rd index 54b27c85e..7c9fbe1a7 100644 --- a/man/abb_mat.Rd +++ b/man/abb_mat.Rd @@ -4,7 +4,7 @@ \alias{abb_mat} \title{Print abbreviated matrix} \usage{ -abb_mat(exprObj, nrows, ncols) +abb_mat(exprObj, nrows, ncols, header = TRUE) } \description{ print abbreviated matrix exprObj. Works for Matrix pkg denseMatrix, diff --git a/man/box_chars.Rd b/man/box_chars.Rd index 91df604af..134f5ffb4 100644 --- a/man/box_chars.Rd +++ b/man/box_chars.Rd @@ -2,14 +2,27 @@ % Please edit documentation in R/utilities.R \name{box_chars} \alias{box_chars} +\alias{is_latex_output} +\alias{is_utf8_output} \title{Box characters} \usage{ box_chars() + +is_latex_output() + +is_utf8_output() } \description{ -Box characters +Helper function to print unicode characters using escape codes. } \details{ -Much inspiration taken from https://rdrr.io/cran/fs/src/R/tree.R +Much inspiration taken from \pkg{fs} \href{https://rdrr.io/cran/fs/src/R/tree.R}{tree.R} } +\section{Functions}{ +\itemize{ +\item \code{is_latex_output()}: Determine if print is latex output + +\item \code{is_utf8_output()}: Determine if system is using UTF-8 encoding + +}} \keyword{internal} diff --git a/man/circleVertices.Rd b/man/circleVertices.Rd index 3887319e6..24e6886c5 100644 --- a/man/circleVertices.Rd +++ b/man/circleVertices.Rd @@ -19,5 +19,5 @@ Generates vertex coordinates for a circle around (0,0) with the given radius. Modified from \pkg{packcircles}. } \seealso{ -polyStamp rectVertices +polyStamp rectVertices hexVertices } diff --git a/man/color_tag.Rd b/man/color_tag.Rd index fe0f44d0b..42570b2be 100644 --- a/man/color_tag.Rd +++ b/man/color_tag.Rd @@ -2,11 +2,38 @@ % Please edit documentation in R/utilities.R \name{color_tag} \alias{color_tag} +\alias{use_color_text} +\alias{ansi_colors} +\alias{is_emacs_with_color} +\alias{emacs_version} \title{Colorize print text} \usage{ color_tag() + +use_color_text() + +ansi_colors() + +is_emacs_with_color() + +emacs_version() } \description{ Colorize print text } +\details{ +supported colors checking is modified from \pkg{cli} +\href{https://github.com/r-lib/cli/blob/HEAD/R/num-ansi-colors.R}{aab-num-ansi-colors.R} +} +\section{Functions}{ +\itemize{ +\item \code{use_color_text()}: Determine if system should print color + +\item \code{ansi_colors()}: Determine if system can print at least 8 colors + +\item \code{is_emacs_with_color()}: Determine if emacs can print color + +\item \code{emacs_version()}: Determine emacs version + +}} \keyword{internal} diff --git a/man/createGiottoCosMxObject_aggregate.Rd b/man/createGiottoCosMxObject_aggregate.Rd new file mode 100644 index 000000000..2bdd6d595 --- /dev/null +++ b/man/createGiottoCosMxObject_aggregate.Rd @@ -0,0 +1,24 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/giotto.R +\name{createGiottoCosMxObject_aggregate} +\alias{createGiottoCosMxObject_aggregate} +\title{Load and create a CosMx Giotto object from aggregate info} +\usage{ +createGiottoCosMxObject_aggregate( + dir_items, + cores, + verbose = TRUE, + instructions = NULL +) +} +\arguments{ +\item{cores}{how many cores or threads to use to read data if paths are provided} + +\item{verbose}{be verbose when building Giotto object} + +\item{instructions}{list of instructions or output result from \code{\link{createGiottoInstructions}}} +} +\description{ +Load and create a CosMx Giotto object from aggregate info +} +\keyword{internal} diff --git a/man/createGiottoCosMxObject_all.Rd b/man/createGiottoCosMxObject_all.Rd new file mode 100644 index 000000000..15559138a --- /dev/null +++ b/man/createGiottoCosMxObject_all.Rd @@ -0,0 +1,39 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/giotto.R +\name{createGiottoCosMxObject_all} +\alias{createGiottoCosMxObject_all} +\title{Load and create a CosMx Giotto object from subcellular and aggregate info} +\usage{ +createGiottoCosMxObject_all( + dir_items, + FOVs, + cores, + verbose = TRUE, + instructions = NULL +) +} +\arguments{ +\item{dir_items}{list of full directory paths from \code{read_cosmx_folder}} + +\item{FOVs}{field of views to load (only affects subcellular data and images)} + +\item{cores}{how many cores or threads to use to read data if paths are provided} + +\item{verbose}{be verbose when building Giotto object} + +\item{instructions}{list of instructions or output result from \code{\link{createGiottoInstructions}}} +} +\description{ +Load and create a CosMx Giotto object from subcellular and aggregate info +} +\details{ +Both \emph{subcellular} (subellular transcript detection information) and +\emph{aggregate} (aggregated detection count matrices by cell polygon from NanoString) +data will be loaded in. The two will be separated into 'cell' and 'cell_agg' +spatial units in order to denote the difference in origin of the two. +} +\seealso{ +createGiottoCosMxObject createGiottoCosMxObject_aggregate +createGiottoCosMxObject_subcellular +} +\keyword{internal} diff --git a/man/createGiottoCosMxObject_subcellular.Rd b/man/createGiottoCosMxObject_subcellular.Rd new file mode 100644 index 000000000..fef11853c --- /dev/null +++ b/man/createGiottoCosMxObject_subcellular.Rd @@ -0,0 +1,27 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/giotto.R +\name{createGiottoCosMxObject_subcellular} +\alias{createGiottoCosMxObject_subcellular} +\title{Load and create a CosMx Giotto object from subcellular info} +\usage{ +createGiottoCosMxObject_subcellular( + dir_items, + FOVs = NULL, + cores, + verbose = TRUE, + instructions = NULL +) +} +\arguments{ +\item{FOVs}{field of views to load (only affects subcellular data and images)} + +\item{cores}{how many cores or threads to use to read data if paths are provided} + +\item{verbose}{be verbose when building Giotto object} + +\item{instructions}{list of instructions or output result from \code{\link{createGiottoInstructions}}} +} +\description{ +Load and create a CosMx Giotto object from subcellular info +} +\keyword{internal} diff --git a/man/hexVertices.Rd b/man/hexVertices.Rd new file mode 100644 index 000000000..0955d0138 --- /dev/null +++ b/man/hexVertices.Rd @@ -0,0 +1,23 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/giotto_structures.R +\name{hexVertices} +\alias{hexVertices} +\title{Generate regular hexagon vertices} +\usage{ +hexVertices(radius, major_axis = c("v", "h")) +} +\arguments{ +\item{radius}{radius of the hexagon} + +\item{major_axis}{orientation of the major axis 'v' is vertical (default) +and 'h' is horizontal} +} +\value{ +a data.table of regular hexagon vertices +} +\description{ +Generates vertex coordinates for a regular hexagon. +} +\seealso{ +polyStamp circleVertices rectVertices +} diff --git a/man/joinGiottoObjects.Rd b/man/joinGiottoObjects.Rd index 96bc8793b..760cd5699 100644 --- a/man/joinGiottoObjects.Rd +++ b/man/joinGiottoObjects.Rd @@ -33,7 +33,8 @@ joinGiottoObjects( \item{y_padding}{padding between datasets/images if method is shift} -\item{verbose}{be verbose} +\item{verbose}{be verbose +Preview where each gobject will be in space with bounding polygons} } \value{ giotto object diff --git a/man/load_cosmx_folder_aggregate.Rd b/man/load_cosmx_folder_aggregate.Rd new file mode 100644 index 000000000..f1641b9dd --- /dev/null +++ b/man/load_cosmx_folder_aggregate.Rd @@ -0,0 +1,17 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/giotto.R +\name{load_cosmx_folder_aggregate} +\alias{load_cosmx_folder_aggregate} +\title{Load CosMx folder aggregate info} +\usage{ +load_cosmx_folder_aggregate(dir_items, cores, verbose = TRUE) +} +\arguments{ +\item{cores}{how many cores or threads to use to read data if paths are provided} + +\item{verbose}{be verbose when building Giotto object} +} +\description{ +Load CosMx folder aggregate info +} +\keyword{internal} diff --git a/man/load_cosmx_folder_subcellular.Rd b/man/load_cosmx_folder_subcellular.Rd new file mode 100644 index 000000000..7b2a90031 --- /dev/null +++ b/man/load_cosmx_folder_subcellular.Rd @@ -0,0 +1,21 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/giotto.R +\name{load_cosmx_folder_subcellular} +\alias{load_cosmx_folder_subcellular} +\title{Load CosMx folder subcellular info} +\usage{ +load_cosmx_folder_subcellular(dir_items, FOVs = NULL, cores, verbose = TRUE) +} +\arguments{ +\item{FOVs}{field of views to load (only affects subcellular data and images)} + +\item{cores}{how many cores or threads to use to read data if paths are provided} + +\item{verbose}{be verbose when building Giotto object} +} +\description{ +loads in the feature detections information. Note that the mask +images are still required for a working subcellular object, and those are loaded +in \code{\link{createGiottoCosMxObject_subcellular}} +} +\keyword{internal} diff --git a/man/read_cosmx_folder.Rd b/man/read_cosmx_folder.Rd new file mode 100644 index 000000000..8e444c717 --- /dev/null +++ b/man/read_cosmx_folder.Rd @@ -0,0 +1,24 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/giotto.R +\name{read_cosmx_folder} +\alias{read_cosmx_folder} +\title{Read a structured CosMx folder} +\usage{ +read_cosmx_folder(cosmx_dir, verbose = TRUE) +} +\arguments{ +\item{cosmx_dir}{full path to the exported cosmx directory} + +\item{verbose}{be verbose when building Giotto object} +} +\value{ +path_list a list of cosmx files discovered and their filepaths. NULL +values denote missing items +} +\description{ +Read a structured CosMx folder +} +\seealso{ +createGiottoCosMxObject load_cosmx_folder +} +\keyword{internal} diff --git a/man/rectVertices.Rd b/man/rectVertices.Rd index 6249d79ed..05be34af2 100644 --- a/man/rectVertices.Rd +++ b/man/rectVertices.Rd @@ -18,5 +18,5 @@ Generates vertex coordinates for a rectangle with dimensions given through \code{dims} param. } \seealso{ -polyStamp circleVertices +polyStamp circleVertices hexVertices } diff --git a/man/set_default_feat_type.Rd b/man/set_default_feat_type.Rd index 621120ee0..fca4ea00d 100644 --- a/man/set_default_feat_type.Rd +++ b/man/set_default_feat_type.Rd @@ -4,7 +4,7 @@ \alias{set_default_feat_type} \title{set_default_feat_type} \usage{ -set_default_feat_type(gobject, feat_type = NULL, spat_unit) +set_default_feat_type(gobject, feat_type = NULL, spat_unit, verbose = TRUE) } \arguments{ \item{gobject}{gobject} @@ -12,6 +12,8 @@ set_default_feat_type(gobject, feat_type = NULL, spat_unit) \item{feat_type}{feature type} \item{spat_unit}{spatial unit} + +\item{verbose}{be verbose about warning} } \description{ set_default_feat_type diff --git a/man/set_default_spat_unit.Rd b/man/set_default_spat_unit.Rd index 6781feb2f..d0f7481e4 100644 --- a/man/set_default_spat_unit.Rd +++ b/man/set_default_spat_unit.Rd @@ -4,12 +4,14 @@ \alias{set_default_spat_unit} \title{set_default_spat_unit} \usage{ -set_default_spat_unit(gobject, spat_unit = NULL) +set_default_spat_unit(gobject, spat_unit = NULL, verbose = TRUE) } \arguments{ \item{gobject}{gobject} \item{spat_unit}{spatial unit} + +\item{verbose}{be verbose about warning} } \description{ set_default_spat_unit From 7b0c688094376249dca220fe0d786f9e4066828c Mon Sep 17 00:00:00 2001 From: jiajic <72078254+jiajic@users.noreply.github.com> Date: Tue, 22 Nov 2022 16:06:12 -0500 Subject: [PATCH 05/45] update cosmx - fix subcellular image loading - add CellComp to point metadata --- R/giotto.R | 42 ++++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/R/giotto.R b/R/giotto.R index 584022cec..47d476bfa 100644 --- a/R/giotto.R +++ b/R/giotto.R @@ -3051,6 +3051,12 @@ createGiottoCosMxObject_subcellular = function(dir_items, fov_offset_file = data_list$fov_offset_file tx_coord_all = data_list$tx_coord_all + # remove global xy values + tx_coord_all[, x_global_px := NULL] + tx_coord_all[, y_global_px := NULL] + + data.table::setcolorder(tx_coord_all, c('target', 'x_local_px', 'y_local_px', 'z')) + if(isTRUE(verbose)) wrap_msg('Splitting detections by feature vs neg probe') all_IDs = tx_coord_all[, unique(target)] neg_IDs = all_IDs[grepl(pattern = 'NegPrb', all_IDs)] @@ -3068,6 +3074,21 @@ createGiottoCosMxObject_subcellular = function(dir_items, # Start FOV lapply fov_gobjects_list = lapply(FOV_ID, function(x) { + # Build image paths + if(isTRUE(verbose)) message('Loading image information...') + + composite_dir = Sys.glob(paths = file.path(dir_items$`CellComposite folder`, paste0('*',x, '*'))) + cellLabel_dir = Sys.glob(paths = file.path(dir_items$`CellLabels folder`, paste0('*',x, '*'))) + compartmentLabel_dir = Sys.glob(paths = file.path(dir_items$`CompartmentLabels folder`, paste0('*',x, '*'))) + cellOverlay_dir = Sys.glob(paths = file.path(dir_items$`CellOverlay folder`, paste0('*',x, '*'))) + # Missing warnings + if(length(composite_dir) == 0) {warning('[ FOV ', x, ' ] No composite images found') ; composite_dir = NULL} + if(length(cellLabel_dir) == 0) {stop('[ FOV ', x, ' ] No cell mask images found')} # cell masks are necessary + if(length(compartmentLabel_dir) == 0) {warning('[ FOV ', x, ' ] No compartment label images found') ; compartmentLabel_dir = NULL} + if(length(cellOverlay_dir) == 0) {warning('[ FOV ', x, ' ] No cell polygon overlay images found') ; cellOverlay_dir = NULL} + + if(isTRUE(verbose)) message('Image load done') + if(isTRUE(verbose)) wrap_msg('[ FOV ', x, ']') # get FOV specific tx locations @@ -3076,11 +3097,11 @@ createGiottoCosMxObject_subcellular = function(dir_items, # feature info feat_coord = feat_coords_all[fov == as.numeric(x)] - feat_coord = feat_coord[,.(target, x_local_px, y_local_px, z)] + feat_coord = feat_coord[,.(target, x_local_px, y_local_px, z, CellComp)] colnames(feat_coord) = c('feat_ID','x','y','z') # neg probe info neg_coord = neg_coords_all[fov == as.numeric(x)] - neg_coord = neg_coord[,.(target, x_local_px, y_local_px, z)] + neg_coord = neg_coord[,.(target, x_local_px, y_local_px, z, CellComp)] colnames(neg_coord) = c('feat_ID','x','y','z') @@ -3108,21 +3129,6 @@ createGiottoCosMxObject_subcellular = function(dir_items, spat_loc_name = 'raw') - # Build image paths - if(isTRUE(verbose)) message('Loading image information...') - - composite_dir = Sys.glob(paths = file.path(dir_items$`CellComposite folder`, paste0('*',x, '*'))) - cellLabel_dir = Sys.glob(paths = file.path(dir_items$`CellLabels folder`, paste0('*',x, '*'))) - compartmentLabel_dir = Sys.glob(paths = file.path(dir_items$`CompartmentLabels folder`, paste0('*',x, '*'))) - cellOverlay_dir = Sys.glob(paths = file.path(dir_items$`CellOverlay folder`, paste0('*',x, '*'))) - # Missing warnings - if(length(composite_dir) == 0) {warning('[ FOV ', x, ' ] No composite images found') ; composite_dir = NULL} - if(length(cellLabel_dir) == 0) {stop('[ FOV ', x, ' ] No cell mask images found')} # cell masks are necessary - if(length(compartmentLabel_dir) == 0) {warning('[ FOV ', x, ' ] No compartment label images found') ; compartmentLabel_dir = NULL} - if(length(cellOverlay_dir) == 0) {warning('[ FOV ', x, ' ] No cell polygon overlay images found') ; cellOverlay_dir = NULL} - - if(isTRUE(verbose)) message('Image load done') - # create and add giotto image objects if(isTRUE(verbose)) message('Attaching image files...') print(composite_dir) @@ -3824,7 +3830,7 @@ load_cosmx_folder_aggregate = function(dir_items, metadata[, cell_ID := paste0('fov', sprintf('%03d', fov), '-', 'cell_', cell_ID)] # metadata$cell_ID = paste0('fov', sprintf('%03d', metadata$fov), '-', 'cell_', metadata$cell_ID) # reorder - metadata = metadata[,.SD, c('cell_ID','fov','fov_cell_ID')] + data.table::setcolorder(x = metadata, c('cell_ID','fov','fov_cell_ID')) # extract spatial locations From dda290ac102b4bf12c3460fcad7b577ca12b6dd7 Mon Sep 17 00:00:00 2001 From: jiajic <72078254+jiajic@users.noreply.github.com> Date: Tue, 22 Nov 2022 16:13:38 -0500 Subject: [PATCH 06/45] Update giotto.R --- R/giotto.R | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/R/giotto.R b/R/giotto.R index 47d476bfa..089d006d0 100644 --- a/R/giotto.R +++ b/R/giotto.R @@ -3055,7 +3055,7 @@ createGiottoCosMxObject_subcellular = function(dir_items, tx_coord_all[, x_global_px := NULL] tx_coord_all[, y_global_px := NULL] - data.table::setcolorder(tx_coord_all, c('target', 'x_local_px', 'y_local_px', 'z')) + data.table::setcolorder(tx_coord_all, c('target', 'x_local_px', 'y_local_px', 'z', 'fov', 'cell_ID')) if(isTRUE(verbose)) wrap_msg('Splitting detections by feature vs neg probe') all_IDs = tx_coord_all[, unique(target)] @@ -3096,13 +3096,14 @@ createGiottoCosMxObject_subcellular = function(dir_items, # feature info + coord_oldnames = c('target', 'x_local_px', 'y_local_px', 'cell_ID') + coord_newnames = c('feat_ID', 'x', 'y', 'fov_cell_ID') + feat_coord = feat_coords_all[fov == as.numeric(x)] - feat_coord = feat_coord[,.(target, x_local_px, y_local_px, z, CellComp)] - colnames(feat_coord) = c('feat_ID','x','y','z') + data.table::setnames(feat_coord, old = coord_oldnames, new = coord_newnames) # neg probe info neg_coord = neg_coords_all[fov == as.numeric(x)] - neg_coord = neg_coord[,.(target, x_local_px, y_local_px, z, CellComp)] - colnames(neg_coord) = c('feat_ID','x','y','z') + data.table::setnames(neg_coord, old = coord_oldnames, new = coord_newnames) # build giotto object From dbad16fdebd6a1e6f35a50b52b6f5681e060da73 Mon Sep 17 00:00:00 2001 From: jiajic <72078254+jiajic@users.noreply.github.com> Date: Tue, 22 Nov 2022 21:40:32 -0500 Subject: [PATCH 07/45] test subcellular --- R/giotto.R | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/R/giotto.R b/R/giotto.R index 089d006d0..23677b086 100644 --- a/R/giotto.R +++ b/R/giotto.R @@ -3054,8 +3054,9 @@ createGiottoCosMxObject_subcellular = function(dir_items, # remove global xy values tx_coord_all[, x_global_px := NULL] tx_coord_all[, y_global_px := NULL] + tx_coord_all[, cell_ID := NULL] - data.table::setcolorder(tx_coord_all, c('target', 'x_local_px', 'y_local_px', 'z', 'fov', 'cell_ID')) + data.table::setcolorder(tx_coord_all, c('target', 'x_local_px', 'y_local_px', 'z', 'fov')) if(isTRUE(verbose)) wrap_msg('Splitting detections by feature vs neg probe') all_IDs = tx_coord_all[, unique(target)] @@ -3096,8 +3097,8 @@ createGiottoCosMxObject_subcellular = function(dir_items, # feature info - coord_oldnames = c('target', 'x_local_px', 'y_local_px', 'cell_ID') - coord_newnames = c('feat_ID', 'x', 'y', 'fov_cell_ID') + coord_oldnames = c('target', 'x_local_px', 'y_local_px') + coord_newnames = c('feat_ID', 'x', 'y') feat_coord = feat_coords_all[fov == as.numeric(x)] data.table::setnames(feat_coord, old = coord_oldnames, new = coord_newnames) From d7aa6e35c6414fa33943748896d55bc7f2f16721 Mon Sep 17 00:00:00 2001 From: jiajic <72078254+jiajic@users.noreply.github.com> Date: Wed, 23 Nov 2022 00:43:41 -0500 Subject: [PATCH 08/45] accessors update --- R/accessors.R | 301 ++++++++++++++++++++++++++----------------- R/auxiliary_giotto.R | 12 +- 2 files changed, 187 insertions(+), 126 deletions(-) diff --git a/R/accessors.R b/R/accessors.R index 33961579a..664296766 100644 --- a/R/accessors.R +++ b/R/accessors.R @@ -9,6 +9,8 @@ #' @param feat_type feature type (e.g. "rna", "dna", "protein") #' @param return_uniques return unique nesting names (ignores if final object exists/is correct class) #' @param output what format in which to get information (e.g. "data.table") +#' @param set_defaults set default spat_unit and feat_type. Change to FALSE only when +#' expression and spat_info are not expected to exist. #' @keywords internal NULL @@ -37,11 +39,14 @@ NULL #' @family functions to set data in giotto object #' @keywords internal get_cell_id = function(gobject, - spat_unit = NULL) { + spat_unit = NULL, + set_defaults = TRUE) { if(!inherits(gobject, 'giotto')) stop('Only Giotto Objects are supported for this function.') - spat_unit = set_default_spat_unit(gobject = gobject, - spat_unit = spat_unit) + if(isTRUE(set_defaults)) { + spat_unit = set_default_spat_unit(gobject = gobject, + spat_unit = spat_unit) + } return(slot(gobject, 'cell_ID')[[spat_unit]]) @@ -63,10 +68,13 @@ get_cell_id = function(gobject, #' @keywords internal set_cell_id = function(gobject, spat_unit = NULL, - cell_IDs) { + cell_IDs, + set_defaults = TRUE) { if(!inherits(gobject, 'giotto')) stop('Only Giotto Objects are supported for this function.') - spat_unit = set_default_spat_unit(gobject = gobject, - spat_unit = spat_unit) + if(isTRUE(set_defaults)) { + spat_unit = set_default_spat_unit(gobject = gobject, + spat_unit = spat_unit) + } if(!is.null(cell_IDs)) { if(!inherits(cell_IDs, 'character')) stop('cell_IDs must be a character vector.') @@ -92,13 +100,16 @@ set_cell_id = function(gobject, #' @family functions to set data in giotto object #' @keywords internal get_feat_id = function(gobject, - feat_type = NULL) { + feat_type = NULL, + set_defaults = TRUE) { if(!inherits(gobject, 'giotto')) stop('Only Giotto Objects are supported for this function.') - spat_unit = set_default_spat_unit(gobject = gobject, - spat_unit = NULL) - feat_type = set_default_feat_type(gobject = gobject, - spat_unit = spat_unit, - feat_type = feat_type) + if(isTRUE(set_defaults)) { + spat_unit = set_default_spat_unit(gobject = gobject, + spat_unit = NULL) + feat_type = set_default_feat_type(gobject = gobject, + spat_unit = spat_unit, + feat_type = feat_type) + } return(slot(gobject, 'feat_ID')[[feat_type]]) @@ -118,13 +129,16 @@ get_feat_id = function(gobject, #' @keywords internal set_feat_id = function(gobject, feat_type = NULL, - feat_IDs) { + feat_IDs, + set_defaults = TRUE) { if(!inherits(gobject, 'giotto')) stop('Only Giotto Objects are supported for this function.') - spat_unit = set_default_spat_unit(gobject = gobject, - spat_unit = NULL) - feat_type = set_default_feat_type(gobject = gobject, - spat_unit = spat_unit, - feat_type = feat_type) + if(isTRUE(set_defaults)) { + spat_unit = set_default_spat_unit(gobject = gobject, + spat_unit = NULL) + feat_type = set_default_feat_type(gobject = gobject, + spat_unit = spat_unit, + feat_type = feat_type) + } if(!is.null(feat_IDs)) { if(!inherits(feat_IDs, 'character')) stop('feat_IDs must be a character vector.') @@ -249,16 +263,19 @@ get_cell_metadata = function(gobject, spat_unit = NULL, feat_type = NULL, output = c('cellMetaObj', 'data.table'), - copy_obj = TRUE) { + copy_obj = TRUE, + set_defaults = TRUE) { output = match.arg(output, choices = c('cellMetaObj', 'data.table')) # 1. Set feat_type and spat_unit - spat_unit = set_default_spat_unit(gobject = gobject, - spat_unit = spat_unit) - feat_type = set_default_feat_type(gobject = gobject, - spat_unit = spat_unit, - feat_type = feat_type) + if(isTRUE(set_defaults)) { + spat_unit = set_default_spat_unit(gobject = gobject, + spat_unit = spat_unit) + feat_type = set_default_feat_type(gobject = gobject, + spat_unit = spat_unit, + feat_type = feat_type) + } # 2. Find object - note that metadata objects do not have names cellMeta = gobject@cell_metadata[[spat_unit]][[feat_type]] @@ -302,7 +319,8 @@ set_cell_metadata = function(gobject, spat_unit = NULL, feat_type = NULL, provenance = NULL, - verbose = TRUE) { + verbose = TRUE, + set_defaults = TRUE) { if(!inherits(gobject, 'giotto')) stop("Only Giotto Objects are supported for this function.") @@ -311,11 +329,13 @@ set_cell_metadata = function(gobject, nospec_feat = ifelse(is.null(feat_type), yes = TRUE, no = FALSE) # 2. set spat unit/ feat type if needed - spat_unit = set_default_spat_unit(gobject = gobject, - spat_unit = spat_unit) - feat_type = set_default_feat_type(gobject = gobject, - spat_unit = spat_unit, - feat_type = feat_type) + if(isTRUE(set_defaults)) { + spat_unit = set_default_spat_unit(gobject = gobject, + spat_unit = spat_unit) + feat_type = set_default_feat_type(gobject = gobject, + spat_unit = spat_unit, + feat_type = feat_type) + } # 3.1 if input is NULL, remove object if(is.null(metadata)) { @@ -457,16 +477,19 @@ get_feature_metadata = function(gobject, spat_unit = NULL, feat_type = NULL, output = c('featMetaObj', 'data.table'), - copy_obj = TRUE) { + copy_obj = TRUE, + set_defaults = TRUE) { output = match.arg(output, choices = c('featMetaObj', 'data.table')) # 1. Set feat_type and spat_unit - spat_unit = set_default_spat_unit(gobject = gobject, - spat_unit = spat_unit) - feat_type = set_default_feat_type(gobject = gobject, - spat_unit = spat_unit, - feat_type = feat_type) + if(isTRUE(set_defaults)) { + spat_unit = set_default_spat_unit(gobject = gobject, + spat_unit = spat_unit) + feat_type = set_default_feat_type(gobject = gobject, + spat_unit = spat_unit, + feat_type = feat_type) + } # 2. Find object - note that metadata objects do not have names featMeta = gobject@feat_metadata[[spat_unit]][[feat_type]] @@ -523,7 +546,8 @@ set_feature_metadata = function(gobject, spat_unit = NULL, feat_type = NULL, provenance = NULL, - verbose = TRUE) { + verbose = TRUE, + set_defaults = TRUE) { if(!inherits(gobject, 'giotto')) stop("Only Giotto Objects are supported for this function.") @@ -532,15 +556,17 @@ set_feature_metadata = function(gobject, nospec_feat = ifelse(is.null(feat_type), yes = TRUE, no = FALSE) # 2. set spat unit/ feat type if needed - spat_unit = set_default_spat_unit(gobject = gobject, - spat_unit = spat_unit) - feat_type = set_default_feat_type(gobject = gobject, - spat_unit = spat_unit, - feat_type = feat_type) + if(isTRUE(set_defaults)) { + spat_unit = set_default_spat_unit(gobject = gobject, + spat_unit = spat_unit) + feat_type = set_default_feat_type(gobject = gobject, + spat_unit = spat_unit, + feat_type = feat_type) + } # 3.1 if input is NULL, remove object if(is.null(metadata)) { - if(isTRUE(verbose)) message('NULL passed to metadata.\n Removing specified metadata.') + if(isTRUE(verbose)) wrap_msg('NULL passed to metadata.\n Removing specified metadata.') gobject@feat_metadata[[spat_unit]][[feat_type]] = NULL return(gobject) } @@ -655,18 +681,20 @@ get_expression_values = function(gobject, values = NULL, spat_unit = NULL, feat_type = NULL, - output = c('exprObj', 'matrix')) { + output = c('exprObj', 'matrix'), + set_defaults = TRUE) { output = match.arg(output, choices = c('exprObj', 'matrix')) # 1. Set feat_type and spat_unit - - spat_unit = set_default_spat_unit(gobject = gobject, - spat_unit = spat_unit) - feat_type = set_default_feat_type(gobject = gobject, - spat_unit = spat_unit, - feat_type = feat_type) + if(isTRUE(set_defaults)) { + spat_unit = set_default_spat_unit(gobject = gobject, + spat_unit = spat_unit) + feat_type = set_default_feat_type(gobject = gobject, + spat_unit = spat_unit, + feat_type = feat_type) + } # 2. Find object @@ -758,7 +786,8 @@ set_expression_values = function(gobject, feat_type = NULL, name = 'test', provenance = NULL, - verbose = TRUE) { + verbose = TRUE, + set_defaults = TRUE) { if(!inherits(gobject, 'giotto')) stop('Only Giotto objects are supported for this function.') @@ -768,7 +797,7 @@ set_expression_values = function(gobject, nospec_name = ifelse(is.null(match.call()$name), yes = TRUE, no = FALSE) # 2. Set feat_type and spat_unit - if(depth(slot(gobject, 'expression')) > 0) { + if(isTRUE(set_defaults)) { spat_unit = set_default_spat_unit(gobject = gobject, spat_unit = spat_unit) feat_type = set_default_feat_type(gobject = gobject, @@ -873,7 +902,8 @@ get_spatial_locations = function(gobject, spat_loc_name = NULL, output = c('spatLocsObj', 'data.table'), copy_obj = TRUE, - verbose = TRUE) { + verbose = TRUE, + set_defaults = TRUE) { output = match.arg(output, choices = c('spatLocsObj', 'data.table')) @@ -883,10 +913,10 @@ get_spatial_locations = function(gobject, } # spatial locations - spat_unit = set_default_spat_unit(gobject = gobject, - spat_unit = spat_unit, - verbose = FALSE) - + if(isTRUE(set_defaults)) { + spat_unit = set_default_spat_unit(gobject = gobject, + spat_unit = spat_unit) + } # if NULL (not given) and spatial locations have been added, then use first one @@ -987,15 +1017,18 @@ set_spatial_locations = function(gobject, spat_unit = NULL, spat_loc_name = 'raw', provenance = NULL, - verbose = TRUE) { + verbose = TRUE, + set_defaults = TRUE) { # 1. determine if input was supplied to spat_unit and spat_loc_name nospec_unit = ifelse(is.null(spat_unit), yes = TRUE, no = FALSE) nospec_name = ifelse(is.null(match.call()$spat_loc_name), yes = TRUE, no = FALSE) # 2. spatial locations - spat_unit = set_default_spat_unit(gobject = gobject, - spat_unit = spat_unit) + if(isTRUE(set_defaults)) { + spat_unit = set_default_spat_unit(gobject = gobject, + spat_unit = spat_unit) + } # 3. remove if input is NULL if(is.null(spatlocs)) { @@ -1072,16 +1105,19 @@ get_dimReduction = function(gobject, reduction = c('cells', 'feats'), reduction_method = c('pca', 'umap', 'tsne'), name = 'pca', - output = c('dimObj', 'data.table')) { + output = c('dimObj', 'data.table'), + set_defaults = TRUE) { output = match.arg(output, choices = c('dimObj', 'data.table')) # Set feat_type and spat_unit - spat_unit = set_default_spat_unit(gobject = gobject, - spat_unit = spat_unit) - feat_type = set_default_feat_type(gobject = gobject, - spat_unit = spat_unit, - feat_type = feat_type) + if(isTRUE(set_defaults)) { + spat_unit = set_default_spat_unit(gobject = gobject, + spat_unit = spat_unit) + feat_type = set_default_feat_type(gobject = gobject, + spat_unit = spat_unit, + feat_type = feat_type) + } ## check parameters reduction = match.arg(arg = reduction, choices = c('cells', 'feats')) @@ -1157,15 +1193,18 @@ set_dimReduction = function(gobject, reduction_method = c('pca', 'umap', 'tsne'), name = 'pca', provenance = NULL, - verbose = TRUE) { + verbose = TRUE, + set_defaults = TRUE) { # Set feat_type and spat_unit - # spat_unit = set_default_spat_unit(gobject = gobject, - # spat_unit = spat_unit) - # feat_type = set_default_feat_type(gobject = gobject, - # spat_unit = spat_unit, - # feat_type = feat_type) + # if(isTRUE(set_defaults)) { + # spat_unit = set_default_spat_unit(gobject = gobject, + # spat_unit = spat_unit) + # feat_type = set_default_feat_type(gobject = gobject, + # spat_unit = spat_unit, + # feat_type = feat_type) + # } # get set location from S4 name = slot(dimObject, 'name') @@ -1221,17 +1260,19 @@ get_NearestNetwork = function(gobject, feat_type = NULL, nn_network_to_use = NULL, network_name = NULL, - output = c('nnNetObj', 'igraph', 'data.table')) { + output = c('nnNetObj', 'igraph', 'data.table'), + set_defaults = TRUE) { output = match.arg(arg = output, choices = c('nnNetObj', 'igraph', 'data.table')) # 1. Set feat_type and spat_unit - - spat_unit = set_default_spat_unit(gobject = gobject, - spat_unit = spat_unit) - feat_type = set_default_feat_type(gobject = gobject, - spat_unit = spat_unit, - feat_type = feat_type) + if(isTRUE(set_defaults)) { + spat_unit = set_default_spat_unit(gobject = gobject, + spat_unit = spat_unit) + feat_type = set_default_feat_type(gobject = gobject, + spat_unit = spat_unit, + feat_type = feat_type) + } # 2 Find the object @@ -1380,7 +1421,8 @@ set_NearestNetwork = function(gobject, nn_network_to_use = 'sNN', network_name = 'sNN.pca', provenance = NULL, - verbose = TRUE) { + verbose = TRUE, + set_defaults = TRUE) { # 1. determine user input nospec_unit = ifelse(is.null(spat_unit), yes = TRUE, no = FALSE) @@ -1389,11 +1431,13 @@ set_NearestNetwork = function(gobject, nospec_name = ifelse(is.null(match.call()$network_name), yes = TRUE, no = FALSE) # 2. Set feat_type and spat_unit - spat_unit = set_default_spat_unit(gobject = gobject, - spat_unit = spat_unit) - feat_type = set_default_feat_type(gobject = gobject, - spat_unit = spat_unit, - feat_type = feat_type) + if(isTRUE(set_defaults)) { + spat_unit = set_default_spat_unit(gobject = gobject, + spat_unit = spat_unit) + feat_type = set_default_feat_type(gobject = gobject, + spat_unit = spat_unit, + feat_type = feat_type) + } # 3. If input is null, remove object if(is.null(nn_network)) { @@ -1481,7 +1525,8 @@ get_spatialNetwork = function(gobject, output = c('spatialNetworkObj', 'networkDT', 'networkDT_before_filter', - 'outputObj')) { + 'outputObj'), + set_defaults = TRUE) { output = match.arg(output, choices = c('spatialNetworkObj', 'networkDT', @@ -1490,8 +1535,10 @@ get_spatialNetwork = function(gobject, # Set spat_unit - spat_unit = set_default_spat_unit(gobject = gobject, - spat_unit = spat_unit) + if(isTRUE(set_defaults)) { + spat_unit = set_default_spat_unit(gobject = gobject, + spat_unit = spat_unit) + } # set name to get if missing if(is.null(name)) name = names(slot(gobject, 'spatial_network')[[spat_unit]])[[1]] @@ -1550,7 +1597,8 @@ set_spatialNetwork = function(gobject, spat_unit = NULL, name = NULL, spatial_network, - verbose = TRUE) { + verbose = TRUE, + set_defaults = TRUE) { # 1. determmine if input was supplied to spat_unit and name if(is.null(spat_unit)) { @@ -1561,8 +1609,10 @@ set_spatialNetwork = function(gobject, } else nospec_name = FALSE # 2. Set feat_type and spat_unit - spat_unit = set_default_spat_unit(gobject = gobject, - spat_unit = spat_unit) + if(isTRUE(set_defaults)) { + spat_unit = set_default_spat_unit(gobject = gobject, + spat_unit = spat_unit) + } # 3. If input is NULL, remove object if(is.null(spatial_network)) { @@ -1629,14 +1679,17 @@ get_spatialGrid = function(gobject, spat_unit = NULL, feat_type = NULL, name = NULL, - return_grid_Obj = FALSE) { + return_grid_Obj = FALSE, + set_defaults = TRUE) { # Set feat_type and spat_unit - spat_unit = set_default_spat_unit(gobject = gobject, - spat_unit = spat_unit) - feat_type = set_default_feat_type(gobject = gobject, - spat_unit = spat_unit, - feat_type = feat_type) + if(isTRUE(set_defaults)) { + spat_unit = set_default_spat_unit(gobject = gobject, + spat_unit = spat_unit) + feat_type = set_default_feat_type(gobject = gobject, + spat_unit = spat_unit, + feat_type = feat_type) + } # **To be deprecated** - check for old nesting if(is.null(names(gobject@spatial_grid[[spat_unit]][[feat_type]])[[1]])) { # If gobject has nothing for this feat_type @@ -1724,7 +1777,8 @@ set_spatialGrid = function(gobject, spat_unit = NULL, feat_type = NULL, name = NULL, - verbose = TRUE) { + verbose = TRUE, + set_defaults = TRUE) { # 1. check input nospec_unit = ifelse(is.null(spat_unit), yes = TRUE, no = FALSE) @@ -1732,11 +1786,13 @@ set_spatialGrid = function(gobject, nospec_name = ifelse(is.null(name), yes = TRUE, no = FALSE) # 2. Set feat_type and spat_unit - spat_unit = set_default_spat_unit(gobject = gobject, - spat_unit = spat_unit) - feat_type = set_default_feat_type(gobject = gobject, - spat_unit = spat_unit, - feat_type = feat_type) + if(isTRUE(set_defaults)) { + spat_unit = set_default_spat_unit(gobject = gobject, + spat_unit = spat_unit) + feat_type = set_default_feat_type(gobject = gobject, + spat_unit = spat_unit, + feat_type = feat_type) + } # 3. if input is null, remove object if(is.null(spatial_grid)) { @@ -1909,11 +1965,14 @@ set_polygon_info = function(gobject, #' @family functions to get data from giotto object #' @export get_feature_info = function(gobject, - feat_type = NULL) { + feat_type = NULL, + set_defaults = TRUE) { # specify feat_type - feat_type = set_default_feat_type(gobject = gobject, - feat_type = feat_type) + if(isTRUE(set_defaults)) { + feat_type = set_default_feat_type(gobject = gobject, + feat_type = feat_type) + } potential_names = names(gobject@feat_info) @@ -1993,16 +2052,19 @@ get_spatial_enrichment = function(gobject, feat_type = NULL, enrichm_name = 'DWLS', output = c('spatEnrObj', 'data.table'), - copy_obj = TRUE) { + copy_obj = TRUE, + set_defaults = TRUE) { output = match.arg(output, choices = c('spatEnrObj', 'data.table')) # Set feat_type and spat_unit - spat_unit = set_default_spat_unit(gobject = gobject, - spat_unit = spat_unit) - feat_type = set_default_feat_type(gobject = gobject, - spat_unit = spat_unit, - feat_type = feat_type) + if(isTRUE(set_defaults)) { + spat_unit = set_default_spat_unit(gobject = gobject, + spat_unit = spat_unit) + feat_type = set_default_feat_type(gobject = gobject, + spat_unit = spat_unit, + feat_type = feat_type) + } # spatial locations # if NULL (not given) and spatial locations have been added, then use first one @@ -2058,7 +2120,8 @@ set_spatial_enrichment = function(gobject, spat_unit = NULL, feat_type = NULL, enrichm_name = 'enrichment', - verbose = TRUE) { + verbose = TRUE, + set_defaults = TRUE) { # 1. Check user input nospec_unit = ifelse(is.null(spat_unit), yes = TRUE, no = FALSE) @@ -2066,11 +2129,13 @@ set_spatial_enrichment = function(gobject, nospec_name = ifelse(is.null(match.call()$enrichm_name), yes = TRUE, no = FALSE) # 2. Set feat_type and spat_unit - spat_unit = set_default_spat_unit(gobject = gobject, - spat_unit = spat_unit) - feat_type = set_default_feat_type(gobject = gobject, - spat_unit = spat_unit, - feat_type = feat_type) + if(isTRUE(set_defaults)) { + spat_unit = set_default_spat_unit(gobject = gobject, + spat_unit = spat_unit) + feat_type = set_default_feat_type(gobject = gobject, + spat_unit = spat_unit, + feat_type = feat_type) + } # 3. Remove object if input is NULL if(is.null(spatenrichment)) { diff --git a/R/auxiliary_giotto.R b/R/auxiliary_giotto.R index 90facd414..c1391f12a 100644 --- a/R/auxiliary_giotto.R +++ b/R/auxiliary_giotto.R @@ -7,11 +7,9 @@ #' @name set_default_spat_unit #' @param gobject gobject #' @param spat_unit spatial unit -#' @param verbose be verbose about warning #' @keywords internal set_default_spat_unit = function(gobject, - spat_unit = NULL, - verbose = TRUE) { + spat_unit = NULL) { # set spatial unit @@ -24,7 +22,7 @@ set_default_spat_unit = function(gobject, } else if(!is.null(gobject@spatial_info)){ spat_unit = names(gobject@spatial_info)[[1]] } else { - if(isTRUE(verbose)) (warning('No default for spat_unit could be set \n')) + warning('No default for spat_unit could be set \n') } } @@ -41,12 +39,10 @@ set_default_spat_unit = function(gobject, #' @param gobject gobject #' @param feat_type feature type #' @param spat_unit spatial unit -#' @param verbose be verbose about warning #' @keywords internal set_default_feat_type = function(gobject, feat_type = NULL, - spat_unit, - verbose = TRUE) { + spat_unit,) { # set spatial unit @@ -61,7 +57,7 @@ set_default_feat_type = function(gobject, } else if(!is.null(gobject@feat_info)){ feat_type = names(gobject@feat_info)[[1]] } else { - if(isTRUE(verbose)) warning('No default for feat_type could be set \n') + warning('No default for feat_type could be set \n') } } From 15f54c89d32c8297fa0fb60b4845275eac8ba3b5 Mon Sep 17 00:00:00 2001 From: jiajic <72078254+jiajic@users.noreply.github.com> Date: Wed, 23 Nov 2022 00:47:31 -0500 Subject: [PATCH 09/45] test --- R/auxiliary_giotto.R | 2 +- R/giotto.R | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/R/auxiliary_giotto.R b/R/auxiliary_giotto.R index c1391f12a..ae68851ab 100644 --- a/R/auxiliary_giotto.R +++ b/R/auxiliary_giotto.R @@ -42,7 +42,7 @@ set_default_spat_unit = function(gobject, #' @keywords internal set_default_feat_type = function(gobject, feat_type = NULL, - spat_unit,) { + spat_unit) { # set spatial unit diff --git a/R/giotto.R b/R/giotto.R index 23677b086..221b832a5 100644 --- a/R/giotto.R +++ b/R/giotto.R @@ -3051,10 +3051,8 @@ createGiottoCosMxObject_subcellular = function(dir_items, fov_offset_file = data_list$fov_offset_file tx_coord_all = data_list$tx_coord_all - # remove global xy values - tx_coord_all[, x_global_px := NULL] - tx_coord_all[, y_global_px := NULL] - tx_coord_all[, cell_ID := NULL] + # remove global xy values and cell_ID + tx_coord_all[, c('x_global_px', 'y_global_px', 'cell_ID') := NULL] data.table::setcolorder(tx_coord_all, c('target', 'x_local_px', 'y_local_px', 'z', 'fov')) @@ -5089,7 +5087,9 @@ joinGiottoObjects = function(gobject_list, S4_feat_metadata = create_feat_meta_obj(spat_unit = spat_unit, feat_type = feat_type, metaDT = data.table::data.table(feat_ID = combined_feat_ID)) - comb_gobject = set_feature_metadata(gobject = comb_gobject, S4_feat_metadata) + comb_gobject = set_feature_metadata(gobject = comb_gobject, + S4_feat_metadata, + set_defaults = FALSE) } From 2b11e522e77b163245ff6de25985d17330ad2a78 Mon Sep 17 00:00:00 2001 From: jiajic <72078254+jiajic@users.noreply.github.com> Date: Wed, 23 Nov 2022 01:02:54 -0500 Subject: [PATCH 10/45] Update giotto.R remove set default warnings. spat_unit and feat_type should already be set during S4 creation --- R/giotto.R | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/R/giotto.R b/R/giotto.R index 221b832a5..0b4e0d81a 100644 --- a/R/giotto.R +++ b/R/giotto.R @@ -5169,7 +5169,9 @@ joinGiottoObjects = function(gobject_list, spat_unit = spat_unit_i, coordinates = combspatlocs, provenance = NULL) - comb_gobject = set_spatial_locations(comb_gobject, spatlocs = combspat_obj) + comb_gobject = set_spatial_locations(comb_gobject, + spatlocs = combspat_obj, + set_defaults = FALSE) } } @@ -5197,7 +5199,8 @@ joinGiottoObjects = function(gobject_list, feat_type = feat_type) comb_gobject = set_cell_metadata(gobject = comb_gobject, - S4_cell_meta) + metadata = S4_cell_meta, + set_defaults = FALSE) #comb_gobject@cell_metadata[[spat_unit]][[feat_type]] = combcellmeta From ab2cbe3803749993642b8ab571d53724f7bba472 Mon Sep 17 00:00:00 2001 From: jiajic <72078254+jiajic@users.noreply.github.com> Date: Wed, 23 Nov 2022 10:04:48 -0500 Subject: [PATCH 11/45] prov generic add provenance access and set generics --- R/generics.R | 24 ++++++++++++++++++++++++ R/giotto.R | 36 ++++++++++++++++++++++++++++++++---- 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/R/generics.R b/R/generics.R index 5672ff409..e2a2709fd 100644 --- a/R/generics.R +++ b/R/generics.R @@ -166,6 +166,30 @@ setMethod('copy', signature(x = 'coordDataDT'), function(x) { +# prov() S4 generic #### +#' @title Provenance information +#' @name prov-generic +#' @description access and set provenance slot of S4 subobject +#' @param x a Giotto S4 class subobject +#' @param value value to set as provenance +#' @include classes.R +#' @export +setGeneric('prov', function(x) standardGeneric('prov')) +setGeneric('prov<-', function(x, value) standardGeneric('prov<-')) + + +#' @describeIn prov-generic Get provenance information +#' @export +setMethod('prov', signature = 'provData', function(x) x@provenance) + + +#' @describeIn prov-generic Set provenance information +#' @export +setMethod('prov<-', signature = 'provData', function(x, value) { + x@provenance = value + x +}) + diff --git a/R/giotto.R b/R/giotto.R index 0b4e0d81a..1653ab7a3 100644 --- a/R/giotto.R +++ b/R/giotto.R @@ -4371,6 +4371,21 @@ objHistory = function(object) { +#### provenance #### + +#' @title Check provenance info matches across list of S4 subobjects +#' @name check_prov_match +#' @keywords internal +#' @return NULL +check_prov_match = function(s4_list) { + + c() + +} + + + + #### joining giotto object #### #' @title join_expression_matrices @@ -4844,7 +4859,7 @@ joinGiottoObjects = function(gobject_list, spat_loc_name = locs_i, output = 'spatLocsObj', copy_obj = TRUE, - verbose = FALSE) + set_defaults = FALSE) myspatlocs = slot(spat_obj, 'coordinates') if(join_method == 'z_stack') { @@ -5072,6 +5087,7 @@ joinGiottoObjects = function(gobject_list, ## expression and feat IDs ## if no expression matrices are provided, then just combine all feature IDs + ## additionally, do feature metadata if(verbose) wrap_msg('2. expression data \n') expr_names = names(first_obj@expression) @@ -5081,15 +5097,21 @@ joinGiottoObjects = function(gobject_list, ## feat IDS for(feat in first_features) { combined_feat_ID = unique(unlist(all_feat_ID_list[[feat]])) - comb_gobject@feat_ID[[feat]] = combined_feat_ID + ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### + comb_gobject = set_feat_id(gobject = comb_gobject, + feat_type = feat, + feat_IDs = combined_feat_ID) + ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### # TODO: figure out best way S4_feat_metadata = create_feat_meta_obj(spat_unit = spat_unit, feat_type = feat_type, metaDT = data.table::data.table(feat_ID = combined_feat_ID)) + ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### comb_gobject = set_feature_metadata(gobject = comb_gobject, S4_feat_metadata, set_defaults = FALSE) + ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### } @@ -5117,7 +5139,10 @@ joinGiottoObjects = function(gobject_list, spat_unit = spat_unit, feat_type = feat_type) - comb_gobject = set_expression_values(gobject = comb_gobject, S4_expr_object) + ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### + comb_gobject = set_expression_values(gobject = comb_gobject, + S4_expr_object) + ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### #comb_gobject@expression[[spat_unit]][[feat_type]][[mode]] = combmat$matrix @@ -5126,7 +5151,10 @@ joinGiottoObjects = function(gobject_list, S4_feat_metadata = create_feat_meta_obj(spat_unit = spat_unit, feat_type = feat_type, metaDT = data.table::data.table(feat_ID = combmat$sort_all_feats)) - comb_gobject = set_feature_metadata(gobject = comb_gobject, S4_feat_metadata) + ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### + comb_gobject = set_feature_metadata(gobject = comb_gobject, + metadata = S4_feat_metadata) + ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### #comb_gobject@feat_metadata[[spat_unit]][[feat_type]] = data.table::data.table(feat_ID = combmat$sort_all_feats) From 277c1debee2afbcc7d0859ed4940db80057e2589 Mon Sep 17 00:00:00 2001 From: jiajic <72078254+jiajic@users.noreply.github.com> Date: Wed, 23 Nov 2022 10:36:21 -0500 Subject: [PATCH 12/45] Add generics and document - added spatUnit, spatUnit<-, featType, featType<- --- NAMESPACE | 9 ++++++ R/generics.R | 56 +++++++++++++++++++++++++++++++++ R/giotto.R | 3 +- man/check_prov_match.Rd | 12 +++++++ man/data_access.Rd | 3 ++ man/extractNearestNetwork.Rd | 2 ++ man/featType-generic.Rd | 29 +++++++++++++++++ man/get_NearestNetwork.Rd | 6 +++- man/get_cell_id.Rd | 5 ++- man/get_cell_metadata.Rd | 6 +++- man/get_dimReduction.Rd | 6 +++- man/get_expression_values.Rd | 6 +++- man/get_feat_id.Rd | 5 ++- man/get_feature_info.Rd | 5 ++- man/get_feature_metadata.Rd | 6 +++- man/get_spatialGrid.Rd | 6 +++- man/get_spatialNetwork.Rd | 6 +++- man/get_spatial_enrichment.Rd | 6 +++- man/get_spatial_locations.Rd | 6 +++- man/prov-generic.Rd | 29 +++++++++++++++++ man/select_NearestNetwork.Rd | 2 ++ man/select_dimReduction.Rd | 2 ++ man/select_expression_values.Rd | 2 ++ man/select_feature_info.Rd | 2 ++ man/select_spatialGrid.Rd | 2 ++ man/select_spatialNetwork.Rd | 2 ++ man/select_spatial_locations.Rd | 2 ++ man/set_NearestNetwork.Rd | 6 +++- man/set_cell_id.Rd | 5 ++- man/set_cell_metadata.Rd | 6 +++- man/set_default_feat_type.Rd | 4 +-- man/set_default_spat_unit.Rd | 4 +-- man/set_dimReduction.Rd | 6 +++- man/set_expression_values.Rd | 6 +++- man/set_feat_id.Rd | 5 ++- man/set_feature_metadata.Rd | 6 +++- man/set_spatialGrid.Rd | 6 +++- man/set_spatialNetwork.Rd | 6 +++- man/set_spatial_enrichment.Rd | 6 +++- man/set_spatial_locations.Rd | 6 +++- man/spatUnit-generic.Rd | 29 +++++++++++++++++ 41 files changed, 297 insertions(+), 30 deletions(-) create mode 100644 man/check_prov_match.Rd create mode 100644 man/featType-generic.Rd create mode 100644 man/prov-generic.Rd create mode 100644 man/spatUnit-generic.Rd diff --git a/NAMESPACE b/NAMESPACE index 8ce3bd719..20b5e9955 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -136,6 +136,7 @@ export(estimateImageBg) export(exportGiottoViewer) export(exprCellCellcom) export(fDataDT) +export(featType) export(featureNetwork) export(fiji) export(filterCPG) @@ -246,6 +247,7 @@ export(plotUMAP_3D) export(plot_spat_voronoi_layer_ggplot) export(polyStamp) export(processGiotto) +export(prov) export(rankEnrich) export(rankSpatialCorGroups) export(readExprMatrix) @@ -350,6 +352,7 @@ export(spatNetwDistributionsKneighbors) export(spatPlot) export(spatPlot2D) export(spatPlot3D) +export(spatUnit) export(spatialAEH) export(spatialDE) export(specificCellCellcommunicationScores) @@ -390,11 +393,17 @@ exportMethods("$") exportMethods("$<-") exportMethods("[") exportMethods("[<-") +exportMethods("featType<-") +exportMethods("prov<-") +exportMethods("spatUnit<-") exportMethods(copy) exportMethods(dim) +exportMethods(featType) exportMethods(ncol) exportMethods(nrow) exportMethods(plot) +exportMethods(prov) +exportMethods(spatUnit) import(data.table) import(ggplot2) import(magrittr) diff --git a/R/generics.R b/R/generics.R index e2a2709fd..dcb17d394 100644 --- a/R/generics.R +++ b/R/generics.R @@ -193,6 +193,62 @@ setMethod('prov<-', signature = 'provData', function(x, value) { +# spatUnit() S4 generic #### +#' @title Spatial unit information +#' @name spatUnit-generic +#' @description access and set spat_unit slot of S4 subobject +#' @param x a Giotto S4 class subobject with spatial unit +#' @param value value to set as spatial unit +#' @include classes.R +#' @export +setGeneric('spatUnit', function(x) standardGeneric('spatUnit')) +setGeneric('spatUnit<-', function(x, value) standardGeneric('spatUnit<-')) + + +#' @describeIn spatUnit-generic Get spatial unit information +#' @export +setMethod('spatUnit', signature = 'spatData', function(x) x@spat_unit) + + +#' @describeIn spatUnit-generic Set spatial unit information +#' @export +setMethod('spatUnit<-', signature = 'spatData', function(x, value) { + x@spat_unit = value + x +}) + + + + +# featType() S4 generic #### +#' @title Feature type information +#' @name featType-generic +#' @description access and set feat_type slot of S4 subobject +#' @param x a Giotto S4 class subobject with feature type +#' @param value value to set as feature type +#' @include classes.R +#' @export +setGeneric('featType', function(x) standardGeneric('featType')) +setGeneric('featType<-', function(x, value) standardGeneric('featType<-')) + + +#' @describeIn featType-generic Get feature type information +#' @export +setMethod('featType', signature = 'featData', function(x) x@feat_type) + + +#' @describeIn featType-generic Set feature type information +#' @export +setMethod('featType<-', signature = 'featData', function(x, value) { + x@feat_type = value + x +}) + + + + + + #' @title Extract or replace parts of an object #' @name extract-generic #' @description Operators Giotto S4 internal data.tables to extract diff --git a/R/giotto.R b/R/giotto.R index 1653ab7a3..0f176ceeb 100644 --- a/R/giotto.R +++ b/R/giotto.R @@ -4379,7 +4379,8 @@ objHistory = function(object) { #' @return NULL check_prov_match = function(s4_list) { - c() + prov_list = lapply(s4_list, prov) + length(unique(prov_list)) == 1 } diff --git a/man/check_prov_match.Rd b/man/check_prov_match.Rd new file mode 100644 index 000000000..f3e31e639 --- /dev/null +++ b/man/check_prov_match.Rd @@ -0,0 +1,12 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/giotto.R +\name{check_prov_match} +\alias{check_prov_match} +\title{Check provenance info matches across list of S4 subobjects} +\usage{ +check_prov_match(s4_list) +} +\description{ +Check provenance info matches across list of S4 subobjects +} +\keyword{internal} diff --git a/man/data_access.Rd b/man/data_access.Rd index 9b47a9c3a..b06690136 100644 --- a/man/data_access.Rd +++ b/man/data_access.Rd @@ -13,6 +13,9 @@ \item{return_uniques}{return unique nesting names (ignores if final object exists/is correct class)} \item{output}{what format in which to get information (e.g. "data.table")} + +\item{set_defaults}{set default spat_unit and feat_type. Change to FALSE only when +expression and spat_info are not expected to exist.} } \description{ Access or examine slots within the giotto object diff --git a/man/extractNearestNetwork.Rd b/man/extractNearestNetwork.Rd index f6abd051e..3eba78373 100644 --- a/man/extractNearestNetwork.Rd +++ b/man/extractNearestNetwork.Rd @@ -16,6 +16,8 @@ extractNearestNetwork(...) \item{\code{gobject}}{giotto object} \item{\code{spat_unit}}{spatial unit (e.g. "cell")} \item{\code{feat_type}}{feature type (e.g. "rna", "dna", "protein")} + \item{\code{set_defaults}}{set default spat_unit and feat_type. Change to FALSE only when +expression and spat_info are not expected to exist.} }} } \description{ diff --git a/man/featType-generic.Rd b/man/featType-generic.Rd new file mode 100644 index 000000000..ceea7450e --- /dev/null +++ b/man/featType-generic.Rd @@ -0,0 +1,29 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/generics.R +\name{featType-generic} +\alias{featType-generic} +\alias{featType,featData-method} +\alias{featType<-,featData-method} +\title{Feature type information} +\usage{ +featType(x) + +\S4method{featType}{featData}(x) + +\S4method{featType}{featData}(x) <- value +} +\arguments{ +\item{x}{a Giotto S4 class subobject with feature type} + +\item{value}{value to set as feature type} +} +\description{ +access and set feat_type slot of S4 subobject +} +\section{Functions}{ +\itemize{ +\item \code{featType(featData)}: Get feature type information + +\item \code{featType(featData) <- value}: Set feature type information + +}} diff --git a/man/get_NearestNetwork.Rd b/man/get_NearestNetwork.Rd index 2f9e6bc0a..13d7dacf7 100644 --- a/man/get_NearestNetwork.Rd +++ b/man/get_NearestNetwork.Rd @@ -10,7 +10,8 @@ get_NearestNetwork( feat_type = NULL, nn_network_to_use = NULL, network_name = NULL, - output = c("nnNetObj", "igraph", "data.table") + output = c("nnNetObj", "igraph", "data.table"), + set_defaults = TRUE ) } \arguments{ @@ -25,6 +26,9 @@ get_NearestNetwork( \item{network_name}{name of NN network to be used} \item{output}{return a igraph or data.table object. Default 'igraph'} + +\item{set_defaults}{set default spat_unit and feat_type. Change to FALSE only when +expression and spat_info are not expected to exist.} } \value{ igraph or data.table object diff --git a/man/get_cell_id.Rd b/man/get_cell_id.Rd index 9ffc9dd52..08f3c7385 100644 --- a/man/get_cell_id.Rd +++ b/man/get_cell_id.Rd @@ -4,12 +4,15 @@ \alias{get_cell_id} \title{Get cell IDs for a given spatial unit} \usage{ -get_cell_id(gobject, spat_unit = NULL) +get_cell_id(gobject, spat_unit = NULL, set_defaults = TRUE) } \arguments{ \item{gobject}{giotto object} \item{spat_unit}{spatial unit (e.g. "cell")} + +\item{set_defaults}{set default spat_unit and feat_type. Change to FALSE only when +expression and spat_info are not expected to exist.} } \value{ character vector of cell_IDs diff --git a/man/get_cell_metadata.Rd b/man/get_cell_metadata.Rd index 64ed66511..7132493f8 100644 --- a/man/get_cell_metadata.Rd +++ b/man/get_cell_metadata.Rd @@ -9,7 +9,8 @@ get_cell_metadata( spat_unit = NULL, feat_type = NULL, output = c("cellMetaObj", "data.table"), - copy_obj = TRUE + copy_obj = TRUE, + set_defaults = TRUE ) } \arguments{ @@ -20,6 +21,9 @@ get_cell_metadata( \item{feat_type}{feature type (e.g. "rna", "dna", "protein")} \item{output}{return as either 'data.table' or 'cellMetaObj'} + +\item{set_defaults}{set default spat_unit and feat_type. Change to FALSE only when +expression and spat_info are not expected to exist.} } \description{ Get cell metadata from giotto object diff --git a/man/get_dimReduction.Rd b/man/get_dimReduction.Rd index 7ef7fa416..199fba90e 100644 --- a/man/get_dimReduction.Rd +++ b/man/get_dimReduction.Rd @@ -11,7 +11,8 @@ get_dimReduction( reduction = c("cells", "feats"), reduction_method = c("pca", "umap", "tsne"), name = "pca", - output = c("dimObj", "data.table") + output = c("dimObj", "data.table"), + set_defaults = TRUE ) } \arguments{ @@ -29,6 +30,9 @@ get_dimReduction( \item{output}{object type to return as. Either 'dimObj' (default) or 'data.table of the embedding coordinates.} + +\item{set_defaults}{set default spat_unit and feat_type. Change to FALSE only when +expression and spat_info are not expected to exist.} } \value{ dim reduction object (default) or dim reduction coordinates diff --git a/man/get_expression_values.Rd b/man/get_expression_values.Rd index 895d3d085..c3ef2b60c 100644 --- a/man/get_expression_values.Rd +++ b/man/get_expression_values.Rd @@ -9,7 +9,8 @@ get_expression_values( values = NULL, spat_unit = NULL, feat_type = NULL, - output = c("exprObj", "matrix") + output = c("exprObj", "matrix"), + set_defaults = TRUE ) } \arguments{ @@ -24,6 +25,9 @@ get_expression_values( \item{output}{what object type to retrieve the expression as. Currently either 'matrix' for the matrix object contained in the exprObj or 'exprObj' (default) for the exprObj itself are allowed.} + +\item{set_defaults}{set default spat_unit and feat_type. Change to FALSE only when +expression and spat_info are not expected to exist.} } \value{ expression matrix diff --git a/man/get_feat_id.Rd b/man/get_feat_id.Rd index 6ba601cd5..1d66e0d2d 100644 --- a/man/get_feat_id.Rd +++ b/man/get_feat_id.Rd @@ -4,12 +4,15 @@ \alias{get_feat_id} \title{Get feat IDs for a given feature type} \usage{ -get_feat_id(gobject, feat_type = NULL) +get_feat_id(gobject, feat_type = NULL, set_defaults = TRUE) } \arguments{ \item{gobject}{giotto object} \item{feat_type}{feature type (e.g. "rna", "dna", "protein")} + +\item{set_defaults}{set default spat_unit and feat_type. Change to FALSE only when +expression and spat_info are not expected to exist.} } \description{ Across a single modality/feature type, all feature information is diff --git a/man/get_feature_info.Rd b/man/get_feature_info.Rd index e29da025b..7df8ae080 100644 --- a/man/get_feature_info.Rd +++ b/man/get_feature_info.Rd @@ -4,12 +4,15 @@ \alias{get_feature_info} \title{Get feature info} \usage{ -get_feature_info(gobject, feat_type = NULL) +get_feature_info(gobject, feat_type = NULL, set_defaults = TRUE) } \arguments{ \item{gobject}{giotto object} \item{feat_type}{feature type (e.g. "rna", "dna", "protein")} + +\item{set_defaults}{set default spat_unit and feat_type. Change to FALSE only when +expression and spat_info are not expected to exist.} } \description{ Get giotto points spatVector diff --git a/man/get_feature_metadata.Rd b/man/get_feature_metadata.Rd index 726c8f9dc..3c0bc2c0a 100644 --- a/man/get_feature_metadata.Rd +++ b/man/get_feature_metadata.Rd @@ -9,7 +9,8 @@ get_feature_metadata( spat_unit = NULL, feat_type = NULL, output = c("featMetaObj", "data.table"), - copy_obj = TRUE + copy_obj = TRUE, + set_defaults = TRUE ) } \arguments{ @@ -22,6 +23,9 @@ get_feature_metadata( \item{output}{return as either 'data.table' or 'featMetaObj'} \item{copy_obj}{whether to perform a deepcopy of the data.table information} + +\item{set_defaults}{set default spat_unit and feat_type. Change to FALSE only when +expression and spat_info are not expected to exist.} } \description{ Get feature metadata from giotto object diff --git a/man/get_spatialGrid.Rd b/man/get_spatialGrid.Rd index 7324810ed..62b4b8764 100644 --- a/man/get_spatialGrid.Rd +++ b/man/get_spatialGrid.Rd @@ -9,7 +9,8 @@ get_spatialGrid( spat_unit = NULL, feat_type = NULL, name = NULL, - return_grid_Obj = FALSE + return_grid_Obj = FALSE, + set_defaults = TRUE ) } \arguments{ @@ -22,6 +23,9 @@ get_spatialGrid( \item{name}{name of spatial grid} \item{return_grid_Obj}{return grid object (default = FALSE)} + +\item{set_defaults}{set default spat_unit and feat_type. Change to FALSE only when +expression and spat_info are not expected to exist.} } \description{ Function to get spatial grid diff --git a/man/get_spatialNetwork.Rd b/man/get_spatialNetwork.Rd index 4b86a5c4c..adef953e6 100644 --- a/man/get_spatialNetwork.Rd +++ b/man/get_spatialNetwork.Rd @@ -8,7 +8,8 @@ get_spatialNetwork( gobject, spat_unit = NULL, name = NULL, - output = c("spatialNetworkObj", "networkDT", "networkDT_before_filter", "outputObj") + output = c("spatialNetworkObj", "networkDT", "networkDT_before_filter", "outputObj"), + set_defaults = TRUE ) } \arguments{ @@ -20,6 +21,9 @@ get_spatialNetwork( \item{output}{object type to return as. Options: 'spatialNetworkObj' (default), 'networkDT' and 'networkDT_before_filter' for data.table outputs.} + +\item{set_defaults}{set default spat_unit and feat_type. Change to FALSE only when +expression and spat_info are not expected to exist.} } \description{ Function to get a spatial network diff --git a/man/get_spatial_enrichment.Rd b/man/get_spatial_enrichment.Rd index 96e0d7b52..17e92bba4 100644 --- a/man/get_spatial_enrichment.Rd +++ b/man/get_spatial_enrichment.Rd @@ -10,7 +10,8 @@ get_spatial_enrichment( feat_type = NULL, enrichm_name = "DWLS", output = c("spatEnrObj", "data.table"), - copy_obj = TRUE + copy_obj = TRUE, + set_defaults = TRUE ) } \arguments{ @@ -23,6 +24,9 @@ get_spatial_enrichment( \item{enrichm_name}{name of spatial enrichment results. Default "DWLS"} \item{output}{what format in which to get information (e.g. "data.table")} + +\item{set_defaults}{set default spat_unit and feat_type. Change to FALSE only when +expression and spat_info are not expected to exist.} } \value{ data.table with fractions diff --git a/man/get_spatial_locations.Rd b/man/get_spatial_locations.Rd index 911ef16ec..a4670f3d2 100644 --- a/man/get_spatial_locations.Rd +++ b/man/get_spatial_locations.Rd @@ -10,7 +10,8 @@ get_spatial_locations( spat_loc_name = NULL, output = c("spatLocsObj", "data.table"), copy_obj = TRUE, - verbose = TRUE + verbose = TRUE, + set_defaults = TRUE ) } \arguments{ @@ -26,6 +27,9 @@ a 'spatLocsObj'. Returning as 'data.table' is also possible.} \item{copy_obj}{whether to copy/duplicate when getting the object (default = TRUE)} \item{verbose}{be verbose} + +\item{set_defaults}{set default spat_unit and feat_type. Change to FALSE only when +expression and spat_info are not expected to exist.} } \value{ data.table with coordinates or spatLocsObj depending on \code{output} diff --git a/man/prov-generic.Rd b/man/prov-generic.Rd new file mode 100644 index 000000000..fdc6a3e49 --- /dev/null +++ b/man/prov-generic.Rd @@ -0,0 +1,29 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/generics.R +\name{prov-generic} +\alias{prov-generic} +\alias{prov,provData-method} +\alias{prov<-,provData-method} +\title{Provenance information} +\usage{ +prov(x) + +\S4method{prov}{provData}(x) + +\S4method{prov}{provData}(x) <- value +} +\arguments{ +\item{x}{a Giotto S4 class subobject} + +\item{value}{value to set as provenance} +} +\description{ +access and set provenance slot of S4 subobject +} +\section{Functions}{ +\itemize{ +\item \code{prov(provData)}: Get provenance information + +\item \code{prov(provData) <- value}: Set provenance information + +}} diff --git a/man/select_NearestNetwork.Rd b/man/select_NearestNetwork.Rd index 00eab996c..0838fa28c 100644 --- a/man/select_NearestNetwork.Rd +++ b/man/select_NearestNetwork.Rd @@ -16,6 +16,8 @@ select_NearestNetwork(...) \item{\code{gobject}}{giotto object} \item{\code{spat_unit}}{spatial unit (e.g. "cell")} \item{\code{feat_type}}{feature type (e.g. "rna", "dna", "protein")} + \item{\code{set_defaults}}{set default spat_unit and feat_type. Change to FALSE only when +expression and spat_info are not expected to exist.} }} } \description{ diff --git a/man/select_dimReduction.Rd b/man/select_dimReduction.Rd index b96be0cc7..13191f13c 100644 --- a/man/select_dimReduction.Rd +++ b/man/select_dimReduction.Rd @@ -18,6 +18,8 @@ of the embedding coordinates.} \item{\code{gobject}}{giotto object} \item{\code{spat_unit}}{spatial unit (e.g. "cell")} \item{\code{feat_type}}{feature type (e.g. "rna", "dna", "protein")} + \item{\code{set_defaults}}{set default spat_unit and feat_type. Change to FALSE only when +expression and spat_info are not expected to exist.} }} } \description{ diff --git a/man/select_expression_values.Rd b/man/select_expression_values.Rd index ddc0b576c..e75bd3fe5 100644 --- a/man/select_expression_values.Rd +++ b/man/select_expression_values.Rd @@ -17,6 +17,8 @@ the exprObj itself are allowed.} \item{\code{gobject}}{giotto object} \item{\code{spat_unit}}{spatial unit (e.g. "cell")} \item{\code{feat_type}}{feature type (e.g. "rna", "dna", "protein")} + \item{\code{set_defaults}}{set default spat_unit and feat_type. Change to FALSE only when +expression and spat_info are not expected to exist.} }} } \description{ diff --git a/man/select_feature_info.Rd b/man/select_feature_info.Rd index 04006fad1..755cbae74 100644 --- a/man/select_feature_info.Rd +++ b/man/select_feature_info.Rd @@ -12,6 +12,8 @@ select_feature_info(...) \describe{ \item{\code{gobject}}{giotto object} \item{\code{feat_type}}{feature type (e.g. "rna", "dna", "protein")} + \item{\code{set_defaults}}{set default spat_unit and feat_type. Change to FALSE only when +expression and spat_info are not expected to exist.} }} } \description{ diff --git a/man/select_spatialGrid.Rd b/man/select_spatialGrid.Rd index 33b12404c..e31c264c2 100644 --- a/man/select_spatialGrid.Rd +++ b/man/select_spatialGrid.Rd @@ -15,6 +15,8 @@ select_spatialGrid(...) \item{\code{gobject}}{giotto object} \item{\code{spat_unit}}{spatial unit (e.g. "cell")} \item{\code{feat_type}}{feature type (e.g. "rna", "dna", "protein")} + \item{\code{set_defaults}}{set default spat_unit and feat_type. Change to FALSE only when +expression and spat_info are not expected to exist.} }} } \description{ diff --git a/man/select_spatialNetwork.Rd b/man/select_spatialNetwork.Rd index e1a4212c2..e6b4f6e8b 100644 --- a/man/select_spatialNetwork.Rd +++ b/man/select_spatialNetwork.Rd @@ -15,6 +15,8 @@ select_spatialNetwork(...) 'networkDT' and 'networkDT_before_filter' for data.table outputs.} \item{\code{gobject}}{giotto object} \item{\code{spat_unit}}{spatial unit (e.g. "cell")} + \item{\code{set_defaults}}{set default spat_unit and feat_type. Change to FALSE only when +expression and spat_info are not expected to exist.} }} } \description{ diff --git a/man/select_spatial_locations.Rd b/man/select_spatial_locations.Rd index a5390de63..57186b731 100644 --- a/man/select_spatial_locations.Rd +++ b/man/select_spatial_locations.Rd @@ -17,6 +17,8 @@ a 'spatLocsObj'. Returning as 'data.table' is also possible.} \item{\code{verbose}}{be verbose} \item{\code{gobject}}{giotto object} \item{\code{spat_unit}}{spatial unit (e.g. "cell")} + \item{\code{set_defaults}}{set default spat_unit and feat_type. Change to FALSE only when +expression and spat_info are not expected to exist.} }} } \description{ diff --git a/man/set_NearestNetwork.Rd b/man/set_NearestNetwork.Rd index dc21fb739..200afd7de 100644 --- a/man/set_NearestNetwork.Rd +++ b/man/set_NearestNetwork.Rd @@ -12,7 +12,8 @@ set_NearestNetwork( nn_network_to_use = "sNN", network_name = "sNN.pca", provenance = NULL, - verbose = TRUE + verbose = TRUE, + set_defaults = TRUE ) } \arguments{ @@ -32,6 +33,9 @@ yet supported.} \item{provenance}{provenance information (optional)} \item{verbose}{be verbose} + +\item{set_defaults}{set default spat_unit and feat_type. Change to FALSE only when +expression and spat_info are not expected to exist.} } \value{ giotto object diff --git a/man/set_cell_id.Rd b/man/set_cell_id.Rd index 946176227..b93890e85 100644 --- a/man/set_cell_id.Rd +++ b/man/set_cell_id.Rd @@ -4,7 +4,7 @@ \alias{set_cell_id} \title{Set cell IDs for a given spatial unit} \usage{ -set_cell_id(gobject, spat_unit = NULL, cell_IDs) +set_cell_id(gobject, spat_unit = NULL, cell_IDs, set_defaults = TRUE) } \arguments{ \item{gobject}{giotto object} @@ -12,6 +12,9 @@ set_cell_id(gobject, spat_unit = NULL, cell_IDs) \item{spat_unit}{spatial unit (e.g. "cell")} \item{cell_IDs}{character vector of cell IDs to set} + +\item{set_defaults}{set default spat_unit and feat_type. Change to FALSE only when +expression and spat_info are not expected to exist.} } \value{ giotto object with set cell_ID slot diff --git a/man/set_cell_metadata.Rd b/man/set_cell_metadata.Rd index d0299ca17..b2fead6db 100644 --- a/man/set_cell_metadata.Rd +++ b/man/set_cell_metadata.Rd @@ -10,7 +10,8 @@ set_cell_metadata( spat_unit = NULL, feat_type = NULL, provenance = NULL, - verbose = TRUE + verbose = TRUE, + set_defaults = TRUE ) } \arguments{ @@ -26,6 +27,9 @@ the object. Passing 'initialize' will reset the object.} \item{provenance}{provenance information to set} \item{verbose}{be verbose} + +\item{set_defaults}{set default spat_unit and feat_type. Change to FALSE only when +expression and spat_info are not expected to exist.} } \value{ giotto object diff --git a/man/set_default_feat_type.Rd b/man/set_default_feat_type.Rd index fca4ea00d..621120ee0 100644 --- a/man/set_default_feat_type.Rd +++ b/man/set_default_feat_type.Rd @@ -4,7 +4,7 @@ \alias{set_default_feat_type} \title{set_default_feat_type} \usage{ -set_default_feat_type(gobject, feat_type = NULL, spat_unit, verbose = TRUE) +set_default_feat_type(gobject, feat_type = NULL, spat_unit) } \arguments{ \item{gobject}{gobject} @@ -12,8 +12,6 @@ set_default_feat_type(gobject, feat_type = NULL, spat_unit, verbose = TRUE) \item{feat_type}{feature type} \item{spat_unit}{spatial unit} - -\item{verbose}{be verbose about warning} } \description{ set_default_feat_type diff --git a/man/set_default_spat_unit.Rd b/man/set_default_spat_unit.Rd index d0f7481e4..6781feb2f 100644 --- a/man/set_default_spat_unit.Rd +++ b/man/set_default_spat_unit.Rd @@ -4,14 +4,12 @@ \alias{set_default_spat_unit} \title{set_default_spat_unit} \usage{ -set_default_spat_unit(gobject, spat_unit = NULL, verbose = TRUE) +set_default_spat_unit(gobject, spat_unit = NULL) } \arguments{ \item{gobject}{gobject} \item{spat_unit}{spatial unit} - -\item{verbose}{be verbose about warning} } \description{ set_default_spat_unit diff --git a/man/set_dimReduction.Rd b/man/set_dimReduction.Rd index 4d0f59771..7178e1cf8 100644 --- a/man/set_dimReduction.Rd +++ b/man/set_dimReduction.Rd @@ -13,7 +13,8 @@ set_dimReduction( reduction_method = c("pca", "umap", "tsne"), name = "pca", provenance = NULL, - verbose = TRUE + verbose = TRUE, + set_defaults = TRUE ) } \arguments{ @@ -34,6 +35,9 @@ set_dimReduction( \item{provenance}{provenance information (optional)} \item{verbose}{be verbose} + +\item{set_defaults}{set default spat_unit and feat_type. Change to FALSE only when +expression and spat_info are not expected to exist.} } \value{ giotto object diff --git a/man/set_expression_values.Rd b/man/set_expression_values.Rd index 34b776af4..12a4a1aac 100644 --- a/man/set_expression_values.Rd +++ b/man/set_expression_values.Rd @@ -11,7 +11,8 @@ set_expression_values( feat_type = NULL, name = "test", provenance = NULL, - verbose = TRUE + verbose = TRUE, + set_defaults = TRUE ) } \arguments{ @@ -29,6 +30,9 @@ will be removed.} \item{provenance}{provenance information (optional)} \item{verbose}{be verbose} + +\item{set_defaults}{set default spat_unit and feat_type. Change to FALSE only when +expression and spat_info are not expected to exist.} } \value{ giotto object diff --git a/man/set_feat_id.Rd b/man/set_feat_id.Rd index 63516fd72..81edafa74 100644 --- a/man/set_feat_id.Rd +++ b/man/set_feat_id.Rd @@ -4,12 +4,15 @@ \alias{set_feat_id} \title{Set feat IDs for a given feature type} \usage{ -set_feat_id(gobject, feat_type = NULL, feat_IDs) +set_feat_id(gobject, feat_type = NULL, feat_IDs, set_defaults = TRUE) } \arguments{ \item{gobject}{giotto object} \item{feat_type}{feature type (e.g. "rna", "dna", "protein")} + +\item{set_defaults}{set default spat_unit and feat_type. Change to FALSE only when +expression and spat_info are not expected to exist.} } \value{ giotto object with set cell_ID slot diff --git a/man/set_feature_metadata.Rd b/man/set_feature_metadata.Rd index b2ca95854..f7e9a8e9a 100644 --- a/man/set_feature_metadata.Rd +++ b/man/set_feature_metadata.Rd @@ -10,7 +10,8 @@ set_feature_metadata( spat_unit = NULL, feat_type = NULL, provenance = NULL, - verbose = TRUE + verbose = TRUE, + set_defaults = TRUE ) } \arguments{ @@ -26,6 +27,9 @@ Setting NULL will remove the object.} \item{provenance}{provenance information (optional)} \item{verbose}{be verbose} + +\item{set_defaults}{set default spat_unit and feat_type. Change to FALSE only when +expression and spat_info are not expected to exist.} } \value{ giotto object diff --git a/man/set_spatialGrid.Rd b/man/set_spatialGrid.Rd index 7f3319804..c7b06977c 100644 --- a/man/set_spatialGrid.Rd +++ b/man/set_spatialGrid.Rd @@ -10,7 +10,8 @@ set_spatialGrid( spat_unit = NULL, feat_type = NULL, name = NULL, - verbose = TRUE + verbose = TRUE, + set_defaults = TRUE ) } \arguments{ @@ -25,6 +26,9 @@ set_spatialGrid( \item{name}{name of spatial grid} \item{verbose}{be verbose} + +\item{set_defaults}{set default spat_unit and feat_type. Change to FALSE only when +expression and spat_info are not expected to exist.} } \value{ giotto object diff --git a/man/set_spatialNetwork.Rd b/man/set_spatialNetwork.Rd index 6e0326f26..78a89dff8 100644 --- a/man/set_spatialNetwork.Rd +++ b/man/set_spatialNetwork.Rd @@ -9,7 +9,8 @@ set_spatialNetwork( spat_unit = NULL, name = NULL, spatial_network, - verbose = TRUE + verbose = TRUE, + set_defaults = TRUE ) } \arguments{ @@ -22,6 +23,9 @@ set_spatialNetwork( \item{spatial_network}{spatial network} \item{verbose}{be verbose} + +\item{set_defaults}{set default spat_unit and feat_type. Change to FALSE only when +expression and spat_info are not expected to exist.} } \value{ giotto object diff --git a/man/set_spatial_enrichment.Rd b/man/set_spatial_enrichment.Rd index a94f353d4..c238ca610 100644 --- a/man/set_spatial_enrichment.Rd +++ b/man/set_spatial_enrichment.Rd @@ -10,7 +10,8 @@ set_spatial_enrichment( spat_unit = NULL, feat_type = NULL, enrichm_name = "enrichment", - verbose = TRUE + verbose = TRUE, + set_defaults = TRUE ) } \arguments{ @@ -25,6 +26,9 @@ set_spatial_enrichment( \item{enrichm_name}{name of spatial enrichment results. Default "DWLS"} \item{verbose}{be verbose} + +\item{set_defaults}{set default spat_unit and feat_type. Change to FALSE only when +expression and spat_info are not expected to exist.} } \value{ giotto object diff --git a/man/set_spatial_locations.Rd b/man/set_spatial_locations.Rd index 8d74e4289..b84bccc92 100644 --- a/man/set_spatial_locations.Rd +++ b/man/set_spatial_locations.Rd @@ -10,7 +10,8 @@ set_spatial_locations( spat_unit = NULL, spat_loc_name = "raw", provenance = NULL, - verbose = TRUE + verbose = TRUE, + set_defaults = TRUE ) } \arguments{ @@ -26,6 +27,9 @@ set_spatial_locations( \item{provenance}{provenance information (optional)} \item{verbose}{be verbose} + +\item{set_defaults}{set default spat_unit and feat_type. Change to FALSE only when +expression and spat_info are not expected to exist.} } \value{ giotto object diff --git a/man/spatUnit-generic.Rd b/man/spatUnit-generic.Rd new file mode 100644 index 000000000..30302f2c2 --- /dev/null +++ b/man/spatUnit-generic.Rd @@ -0,0 +1,29 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/generics.R +\name{spatUnit-generic} +\alias{spatUnit-generic} +\alias{spatUnit,spatData-method} +\alias{spatUnit<-,spatData-method} +\title{Spatial unit information} +\usage{ +spatUnit(x) + +\S4method{spatUnit}{spatData}(x) + +\S4method{spatUnit}{spatData}(x) <- value +} +\arguments{ +\item{x}{a Giotto S4 class subobject with spatial unit} + +\item{value}{value to set as spatial unit} +} +\description{ +access and set spat_unit slot of S4 subobject +} +\section{Functions}{ +\itemize{ +\item \code{spatUnit(spatData)}: Get spatial unit information + +\item \code{spatUnit(spatData) <- value}: Set spatial unit information + +}} From c43b4d0e8eaa8cdfb4d1b7447abce40bcec059fb Mon Sep 17 00:00:00 2001 From: jiajic <72078254+jiajic@users.noreply.github.com> Date: Wed, 23 Nov 2022 10:52:00 -0500 Subject: [PATCH 13/45] update provenance matching check_prov_match now takes a ... instead of a list --- R/giotto.R | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/R/giotto.R b/R/giotto.R index 0f176ceeb..d6aaf0f3d 100644 --- a/R/giotto.R +++ b/R/giotto.R @@ -4375,11 +4375,20 @@ objHistory = function(object) { #' @title Check provenance info matches across list of S4 subobjects #' @name check_prov_match +#' @param ... list of s4 subobjects to match provenance #' @keywords internal #' @return NULL -check_prov_match = function(s4_list) { +check_prov_match = function(...) { + s4_list = list(...) prov_list = lapply(s4_list, prov) + + # if(isTRUE(verbose)) { + # out_dt = data.table::data.table( + # class() + # ) + # } + length(unique(prov_list)) == 1 } From d1b0649a4e3724ad82c1243c7d07ab054072f6f0 Mon Sep 17 00:00:00 2001 From: jiajic <72078254+jiajic@users.noreply.github.com> Date: Wed, 23 Nov 2022 11:13:44 -0500 Subject: [PATCH 14/45] update --- R/giotto.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/giotto.R b/R/giotto.R index d6aaf0f3d..93656ced2 100644 --- a/R/giotto.R +++ b/R/giotto.R @@ -4674,8 +4674,8 @@ joinGiottoObjects = function(gobject_list, if(verbose) wrap_msg('0. Update cell and feature IDs \n') for(spat_unit in names(gobj@cell_ID)) { - gobj@cell_ID[[spat_unit]] = paste0(gname,'-',gobj@cell_ID[[spat_unit]]) - all_cell_ID_list[[spat_unit]][[gobj_i]] = gobj@cell_ID[[spat_unit]] + old_cell_ID = get_cell_id(gobject = gobj, spat_unit = spat_unit) + all_cell_ID_list[[spat_unit]][[gobj_i]] = paste0(gname, '-', old_cell_ID) } From 2e77c3f59d873388e585a23de14dd84c80458e4d Mon Sep 17 00:00:00 2001 From: jiajic <72078254+jiajic@users.noreply.github.com> Date: Wed, 23 Nov 2022 11:51:52 -0500 Subject: [PATCH 15/45] join bugfix --- R/giotto.R | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/R/giotto.R b/R/giotto.R index 93656ced2..eb7b29f3d 100644 --- a/R/giotto.R +++ b/R/giotto.R @@ -4681,7 +4681,7 @@ joinGiottoObjects = function(gobject_list, for(feat_type in names(gobj@feat_ID)) { - all_feat_ID_list[[feat_type]][[gobj_i]] = gobj@feat_ID[[feat_type]] + all_feat_ID_list[[feat_type]][[gobj_i]] = get_feat_id(gobject = gobj, feat_type = feat_type) } @@ -4907,7 +4907,9 @@ joinGiottoObjects = function(gobject_list, slot(spat_obj, 'coordinates') = myspatlocs - gobj = set_spatial_locations(gobj, spatlocs = spat_obj) + ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### + gobj = set_spatial_locations(gobject = gobj, spatlocs = spat_obj) + ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### } } @@ -5089,7 +5091,7 @@ joinGiottoObjects = function(gobject_list, ## feat IDs for(feat_type in names(all_feat_ID_list)) { - combined_feat_ID = unique(unlist(all_cell_ID_list[[feat_type]])) + combined_feat_ID = unique(unlist(all_feat_ID_list[[feat_type]])) comb_gobject@feat_ID[[feat_type]] = combined_feat_ID } From e0a325ff6159a19d40b0ffb0c33d5b0e28668d82 Mon Sep 17 00:00:00 2001 From: RubD Date: Wed, 23 Nov 2022 17:04:24 -0500 Subject: [PATCH 16/45] update subc seq --- R/giotto_structures.R | 13 ++++- R/spatial_in_situ_visuals.R | 97 +++++++++++++++++++++++++++++++++---- 2 files changed, 98 insertions(+), 12 deletions(-) diff --git a/R/giotto_structures.R b/R/giotto_structures.R index 452b6e093..0473640f7 100644 --- a/R/giotto_structures.R +++ b/R/giotto_structures.R @@ -1924,6 +1924,7 @@ polygon_to_raster = function(polygon, field = NULL) { #' @param feat_info 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) #' @param return_gobject return giotto object (default: TRUE) #' @param verbose be verbose #' @return giotto object or spatVector with overlapping information @@ -1937,6 +1938,7 @@ calculateOverlapRaster = function(gobject, feat_info = NULL, feat_subset_column = NULL, feat_subset_ids = NULL, + count_info_column = NULL, return_gobject = TRUE, verbose = TRUE) { @@ -2010,11 +2012,16 @@ calculateOverlapRaster = function(gobject, overlap_test_dt[, feat_ID := pointvec_dt_feat_ID[ID]] overlap_test_dt[, feat_ID_uniq := pointvec_dt_feat_ID_uniq[ID]] + if(!is.null(count_info_column)) { + pointvec_dt_count = pointvec_dt[[count_info_column]] ; names(pointvec_dt_count) = pointvec_dt$geom + overlap_test_dt[, c(count_info_column) := pointvec_dt_count[ID]] + } + 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'), with = F]) - names(overlap_test_dt_spatvector) = c('poly_ID', 'feat_ID', 'feat_ID_uniq') + 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) @@ -2457,6 +2464,7 @@ calculateOverlapParallel = function(gobject, #' @param name name for the overlap count matrix #' @param poly_info polygon information #' @param feat_info feature information +#' @param count_info_column column with count information #' @param return_gobject return giotto object (default: TRUE) #' @return giotto object or count matrix #' @concept overlap @@ -2465,6 +2473,7 @@ overlapToMatrix = function(gobject, name = 'raw', poly_info = 'cell', feat_info = 'rna', + count_info_column = NULL, return_gobject = TRUE) { # define for data.table diff --git a/R/spatial_in_situ_visuals.R b/R/spatial_in_situ_visuals.R index 036be7cff..b6412ae1c 100644 --- a/R/spatial_in_situ_visuals.R +++ b/R/spatial_in_situ_visuals.R @@ -165,6 +165,56 @@ select_gimage = function(gobject, } + + +#' @title expand_feature_info +#' @name expand_feature_info +#' @description low level function to expand feature coordinates +#' @return data.table +#' @keywords internal +expand_feature_info = function(spatial_feat_info, + expand_counts = FALSE, + count_info_column = 'count', + jitter = c(0,0), + verbose = TRUE) { + + + # 1. expand feature locations with multiple counts (e.g. in seq-Scope or Stereo-seq) + if(isTRUE(expand_counts)) { + + if(!count_info_column %in% colnames(spatial_feat_info)) stop('count_info_column ', count_info_column, ' does not exist') + + if(isTRUE(verbose)) {wrap_msg('Start expanding feature information based on count column')} + #print(spatial_feat_info) + + extra_feats = spatial_feat_info[get(count_info_column) > 1] + extra_feats = extra_feats[,rep(get(count_info_column), get(count_info_column)), by = .(feat_ID, x, y, feat, spat_unit)] + spatial_feat_info = rbind(extra_feats[,.(feat_ID, x, y, feat, spat_unit)], spatial_feat_info[get(count_info_column) == 1, .(feat_ID, x, y, feat, spat_unit)]) + + #print(spatial_feat_info) + + } + + # 2. add jitter to x and y coordinates + + if(!identical(c(0,0), jitter)) { + + if(isTRUE(verbose)) {wrap_msg('Start adding jitter to x and y based on provided max jitter information')} + + # create jitter for x and y coordinates: from 0 to max-x or max-y + tx_number = nrow(spatial_feat_info) + x_jitter = sample(0:jitter[[1]], size = tx_number, replace = TRUE) + y_jitter = sample(0:jitter[[2]], size = tx_number, replace = TRUE) + + spatial_feat_info[, c('x', 'y') := list(x+x_jitter, y+y_jitter)] + } + + return(spatial_feat_info) + +} + + + #' @title plot_feature_points_layer #' @name plot_feature_points_layer #' @description low level function to plot a points at the spatial in situ level @@ -182,12 +232,26 @@ plot_feature_points_layer = function(ggobject, shape = 'feat', point_size = 1.5, show_legend = TRUE, - plot_method = c('ggplot', 'scattermore', 'scattermost')) { + plot_method = c('ggplot', 'scattermore', 'scattermost'), + expand_counts = FALSE, + count_info_column = 'count', + jitter = c(0,0), + verbose = TRUE) { # data.table variables feat_ID = NULL spatial_feat_info_subset = spatial_feat_info[feat_ID %in% unlist(feats)] + + # expand feature coordinates and/or add jitter to coordiantes + if(isTRUE(expand_counts) | !identical(c(0,0), jitter)) { + spatial_feat_info_subset = expand_feature_info(spatial_feat_info = spatial_feat_info_subset, + expand_counts = expand_counts, + count_info_column = count_info_column, + jitter = jitter, + verbose = verbose) + } + cat(' --| Plotting ', nrow(spatial_feat_info_subset), ' feature points\n') if(!is.null(ggobject) & inherits(ggobject, 'ggplot')) { @@ -237,6 +301,9 @@ plot_feature_points_layer = function(ggobject, #' @param sdimx spatial dimension x #' @param sdimy spatial dimension y #' @param point_size size of the points +#' @param expand_counts expand feature coordinate counts (see details) +#' @param count_info_column column name with count information (if expand_counts = TRUE) +#' @param jitter maximum x,y jitter provided as c(x, y) #' @param show_polygon overlay polygon information (e.g. cell shape) #' @param use_overlap use polygon and feature coordinates overlap results #' @param polygon_feat_type feature type associated with polygon information @@ -262,6 +329,7 @@ plot_feature_points_layer = function(ggobject, #' @param save_plot directly save the plot [boolean] #' @param save_param list of saving parameters, see \code{\link{showSaveParameters}} #' @param default_save_name default save name for saving, don't change, change save_name in save_param +#' @param verbose verbosity #' @return ggplot #' @details TODO #' @family In Situ visualizations @@ -280,6 +348,9 @@ spatInSituPlotPoints = function(gobject, sdimx = 'x', sdimy = 'y', point_size = 1.5, + expand_counts = FALSE, + count_info_column = 'count', + jitter = c(0,0), show_polygon = TRUE, use_overlap = TRUE, polygon_feat_type = 'cell', @@ -303,7 +374,8 @@ spatInSituPlotPoints = function(gobject, return_plot = NA, save_plot = NA, save_param = list(), - default_save_name = 'spatInSituPlotPoints') { + default_save_name = 'spatInSituPlotPoints', + verbose = TRUE) { if(is.null(feats)) { @@ -316,7 +388,6 @@ spatInSituPlotPoints = function(gobject, save_plot = ifelse(is.na(save_plot), readGiottoInstructions(gobject, param = 'save_plot'), save_plot) return_plot = ifelse(is.na(return_plot), readGiottoInstructions(gobject, param = 'return_plot'), return_plot) - print('ok 1') ## giotto image ## if(show_image == TRUE) { @@ -330,10 +401,12 @@ spatInSituPlotPoints = function(gobject, feat_type = feat_type, polygon_feat_type = polygon_feat_type) + if(isTRUE(verbose)) wrap_msg('select image done') + } - print('ok 2') + @@ -351,9 +424,10 @@ spatInSituPlotPoints = function(gobject, gimage = gimage, sdimx = 'sdimx', sdimy = 'sdimy') + + if(isTRUE(verbose)) wrap_msg('plot image layer done') } - print('ok 3') ## 1. plot morphology first if(show_polygon == TRUE) { @@ -397,10 +471,11 @@ spatInSituPlotPoints = function(gobject, alpha = polygon_alpha, size = polygon_line_size) - } + if(isTRUE(verbose)) wrap_msg('plot polygon layer done') - print('ok 4') + } + ## 2. plot features second @@ -416,7 +491,6 @@ spatInSituPlotPoints = function(gobject, poly_info = polygon_feat_type) } else { - print('start') spatial_feat_info = combineFeatureData(gobject = gobject, spat_unit = polygon_feat_type, feat_type = feat_type, @@ -430,6 +504,9 @@ spatInSituPlotPoints = function(gobject, feats = feats, feats_color_code = feats_color_code, feat_shape_code = feat_shape_code, + expand_counts = expand_counts, + count_info_column = count_info_column, + jitter = jitter, sdimx = 'x', sdimy = 'y', color = 'feat_ID', @@ -438,9 +515,9 @@ spatInSituPlotPoints = function(gobject, show_legend = show_legend, plot_method = plot_method) - } + if(isTRUE(verbose)) wrap_msg('plot feature points layer done') - print('ok 5') + } ## 3. adjust theme settings From 660f2e51132202b702bf8aa056a7391955b3797f Mon Sep 17 00:00:00 2001 From: RubD Date: Wed, 23 Nov 2022 21:31:44 -0500 Subject: [PATCH 17/45] update functions for subc sequencing technologies --- R/general_help.R | 25 +++++++++++++++++++++++++ R/giotto_structures.R | 17 ++++++++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/R/general_help.R b/R/general_help.R index bb36f769e..552ac3e77 100644 --- a/R/general_help.R +++ b/R/general_help.R @@ -1502,3 +1502,28 @@ readPolygonFilesVizgen = function(gobject, } +#' @title getGEFtxCoords +#' @name getGEFtxCoords +#' @description Converts .gef file (output stereo-seq pipeline) into +#' transcript with coordinates +#' @param gef_file path to .gef file +#' @param bin_size bin size to select from .gef file +#' @export +getGEFtxCoords = function(gef_file, + bin_size = 'bin100') { + + # package check + package_check(pkg_name = 'rhdf5', repository = 'Bioc') + if(!file.exists(gef_file)) stop('File path to .gef file does not exist') + + # step 1: read expression and gene data from gef file + geneExpData = rhdf5::h5read(file = gef_file, name = 'geneExp') + exprDT = data.table::as.data.table(geneExpData[[bin_size]][['expression']]) + geneDT = data.table::as.data.table(geneExpData[[bin_size]][['gene']]) + + # step 2: combine gene information from the geneDT to the exprDT + exprDT[, genes := rep(x = geneDT$gene, geneDT$count)] + + return(exprDT) + +} diff --git a/R/giotto_structures.R b/R/giotto_structures.R index 0473640f7..c83a3325f 100644 --- a/R/giotto_structures.R +++ b/R/giotto_structures.R @@ -2491,7 +2491,22 @@ overlapToMatrix = function(gobject, dtoverlap = spatVector_to_dt(overlap_spatvec) dtoverlap = dtoverlap[!is.na(poly_ID)] # removes points that have no overlap with any polygons #dtoverlap[, poly_ID := ifelse(is.na(poly_ID), 'no_overlap', poly_ID), by = 1:nrow(dtoverlap)] - aggr_dtoverlap = dtoverlap[, .N, by = c('poly_ID', 'feat_ID')] + + + if(!is.null(count_info_column)) { + + if(!count_info_column %in% colnames(dtoverlap)) stop('count_info_column ', count_info_column, ' does not exist') + + # aggregate counts of features + dtoverlap[, c(count_info_column) := as.numeric(get(count_info_column))] + aggr_dtoverlap = dtoverlap[, base::sum(get(count_info_column)), by = c('poly_ID', 'feat_ID')] + data.table::setnames(aggr_dtoverlap, 'V1', 'N') + } else { + + # aggregate individual features + aggr_dtoverlap = dtoverlap[, .N, by = c('poly_ID', 'feat_ID')] + } + # get all feature and cell information From 568674de027d1d5f0d6b3881ec7f0ab0926b6e74 Mon Sep 17 00:00:00 2001 From: RubD Date: Wed, 23 Nov 2022 21:37:49 -0500 Subject: [PATCH 18/45] update documentation --- NAMESPACE | 2 ++ R/utilities.R | 16 +++++++++++++++- man/abb_mat.Rd | 2 +- man/ansi_colors.Rd | 12 ++++++++++++ man/box_chars.Rd | 9 +++++++-- man/calculateOverlapRaster.Rd | 3 +++ man/circleVertices.Rd | 2 +- man/color_tag.Rd | 4 ++++ man/emacs_version.Rd | 12 ++++++++++++ man/expand_feature_info.Rd | 21 +++++++++++++++++++++ man/getGEFtxCoords.Rd | 17 +++++++++++++++++ man/hexVertices.Rd | 23 +++++++++++++++++++++++ man/is_emacs_with_color.Rd | 12 ++++++++++++ man/is_latex_output.Rd | 12 ++++++++++++ man/overlapToMatrix.Rd | 3 +++ man/plot_feature_points_layer.Rd | 6 +++++- man/rectVertices.Rd | 2 +- man/spatInSituPlotPoints.Rd | 14 +++++++++++++- man/use_color_text.Rd | 12 ++++++++++++ 19 files changed, 176 insertions(+), 8 deletions(-) create mode 100644 man/ansi_colors.Rd create mode 100644 man/emacs_version.Rd create mode 100644 man/expand_feature_info.Rd create mode 100644 man/getGEFtxCoords.Rd create mode 100644 man/hexVertices.Rd create mode 100644 man/is_emacs_with_color.Rd create mode 100644 man/is_latex_output.Rd create mode 100644 man/use_color_text.Rd diff --git a/NAMESPACE b/NAMESPACE index 56604d1ab..e013af283 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -169,6 +169,7 @@ export(getCellsFromPolygon) export(getClusterSimilarity) export(getDendrogramSplits) export(getDistinctColors) +export(getGEFtxCoords) export(get_NearestNetwork) export(get_dimReduction) export(get_expression_values) @@ -188,6 +189,7 @@ export(giottoToSeurat) export(giottoToSpatialExperiment) export(heatmSpatialCorFeats) export(heatmSpatialCorGenes) +export(hexVertices) export(hyperGeometricEnrich) export(insertCrossSectionGenePlot3D) export(insertCrossSectionSpatPlot3D) diff --git a/R/utilities.R b/R/utilities.R index 3024eb141..061e7cda4 100644 --- a/R/utilities.R +++ b/R/utilities.R @@ -389,6 +389,7 @@ print_leaf = function(level_index, } + #' @title Box characters #' @name box_chars #' @description Helper function to print unicode characters using escape codes. @@ -422,6 +423,9 @@ box_chars = function() { } +#' @title is_latex_output +#' @name is_latex_output +#' @description is_latex_output #' @describeIn box_chars #' @keywords internal is_latex_output = function() { @@ -430,6 +434,9 @@ is_latex_output = function() { } +#' @title box_chars +#' @name box_chars +#' @description box_chars #' @describeIn box_chars #' @keywords internal is_utf8_output = function() { @@ -551,6 +558,8 @@ color_tag = function() { } +#' @title use_color_text +#' @name use_color_text #' @describeIn color_tag #' @keywords internal use_color_text = function() { @@ -566,7 +575,8 @@ use_color_text = function() { } else ansi8_color } - +#' @title ansi_colors +#' @name ansi_colors #' @describeIn color_tag #' @keywords internal ansi_colors = function() { @@ -620,6 +630,8 @@ ansi_colors = function() { } +#' @title is_emacs_with_color +#' @name is_emacs_with_color #' @describeIn color_tag #' @keywords internal is_emacs_with_color = function() { @@ -629,6 +641,8 @@ is_emacs_with_color = function() { } +#' @title emacs_version +#' @name emacs_version #' @describeIn color_tag #' @keywords internal emacs_version = function() { diff --git a/man/abb_mat.Rd b/man/abb_mat.Rd index 54b27c85e..7c9fbe1a7 100644 --- a/man/abb_mat.Rd +++ b/man/abb_mat.Rd @@ -4,7 +4,7 @@ \alias{abb_mat} \title{Print abbreviated matrix} \usage{ -abb_mat(exprObj, nrows, ncols) +abb_mat(exprObj, nrows, ncols, header = TRUE) } \description{ print abbreviated matrix exprObj. Works for Matrix pkg denseMatrix, diff --git a/man/ansi_colors.Rd b/man/ansi_colors.Rd new file mode 100644 index 000000000..a4ccf8f8d --- /dev/null +++ b/man/ansi_colors.Rd @@ -0,0 +1,12 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utilities.R +\name{ansi_colors} +\alias{ansi_colors} +\title{ansi_colors} +\usage{ +ansi_colors() +} +\description{ +ansi_colors +} +\keyword{internal} diff --git a/man/box_chars.Rd b/man/box_chars.Rd index 91df604af..dcd89bf24 100644 --- a/man/box_chars.Rd +++ b/man/box_chars.Rd @@ -2,14 +2,19 @@ % Please edit documentation in R/utilities.R \name{box_chars} \alias{box_chars} +\alias{is_utf8_output} \title{Box characters} \usage{ box_chars() + +is_utf8_output() } \description{ -Box characters +Helper function to print unicode characters using escape codes. + +box_chars } \details{ -Much inspiration taken from https://rdrr.io/cran/fs/src/R/tree.R +Much inspiration taken from \pkg{fs} \href{https://rdrr.io/cran/fs/src/R/tree.R}{tree.R} } \keyword{internal} diff --git a/man/calculateOverlapRaster.Rd b/man/calculateOverlapRaster.Rd index ccfabd0d9..e0623cbab 100644 --- a/man/calculateOverlapRaster.Rd +++ b/man/calculateOverlapRaster.Rd @@ -12,6 +12,7 @@ calculateOverlapRaster( feat_info = NULL, feat_subset_column = NULL, feat_subset_ids = NULL, + count_info_column = NULL, return_gobject = TRUE, verbose = TRUE ) @@ -31,6 +32,8 @@ calculateOverlapRaster( \item{feat_subset_ids}{ids within feature info column to use for subsetting} +\item{count_info_column}{column with count information (optional)} + \item{return_gobject}{return giotto object (default: TRUE)} \item{verbose}{be verbose} diff --git a/man/circleVertices.Rd b/man/circleVertices.Rd index 3887319e6..24e6886c5 100644 --- a/man/circleVertices.Rd +++ b/man/circleVertices.Rd @@ -19,5 +19,5 @@ Generates vertex coordinates for a circle around (0,0) with the given radius. Modified from \pkg{packcircles}. } \seealso{ -polyStamp rectVertices +polyStamp rectVertices hexVertices } diff --git a/man/color_tag.Rd b/man/color_tag.Rd index fe0f44d0b..b386ac34e 100644 --- a/man/color_tag.Rd +++ b/man/color_tag.Rd @@ -9,4 +9,8 @@ color_tag() \description{ Colorize print text } +\details{ +supported colors checking is modified from \pkg{cli} +\href{https://github.com/r-lib/cli/blob/HEAD/R/num-ansi-colors.R}{aab-num-ansi-colors.R} +} \keyword{internal} diff --git a/man/emacs_version.Rd b/man/emacs_version.Rd new file mode 100644 index 000000000..58148e0db --- /dev/null +++ b/man/emacs_version.Rd @@ -0,0 +1,12 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utilities.R +\name{emacs_version} +\alias{emacs_version} +\title{emacs_version} +\usage{ +emacs_version() +} +\description{ +emacs_version +} +\keyword{internal} diff --git a/man/expand_feature_info.Rd b/man/expand_feature_info.Rd new file mode 100644 index 000000000..90c855af2 --- /dev/null +++ b/man/expand_feature_info.Rd @@ -0,0 +1,21 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/spatial_in_situ_visuals.R +\name{expand_feature_info} +\alias{expand_feature_info} +\title{expand_feature_info} +\usage{ +expand_feature_info( + spatial_feat_info, + expand_counts = FALSE, + count_info_column = "count", + jitter = c(0, 0), + verbose = TRUE +) +} +\value{ +data.table +} +\description{ +low level function to expand feature coordinates +} +\keyword{internal} diff --git a/man/getGEFtxCoords.Rd b/man/getGEFtxCoords.Rd new file mode 100644 index 000000000..79d5d0c9a --- /dev/null +++ b/man/getGEFtxCoords.Rd @@ -0,0 +1,17 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/general_help.R +\name{getGEFtxCoords} +\alias{getGEFtxCoords} +\title{getGEFtxCoords} +\usage{ +getGEFtxCoords(gef_file, bin_size = "bin100") +} +\arguments{ +\item{gef_file}{path to .gef file} + +\item{bin_size}{bin size to select from .gef file} +} +\description{ +Converts .gef file (output stereo-seq pipeline) into +transcript with coordinates +} diff --git a/man/hexVertices.Rd b/man/hexVertices.Rd new file mode 100644 index 000000000..0955d0138 --- /dev/null +++ b/man/hexVertices.Rd @@ -0,0 +1,23 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/giotto_structures.R +\name{hexVertices} +\alias{hexVertices} +\title{Generate regular hexagon vertices} +\usage{ +hexVertices(radius, major_axis = c("v", "h")) +} +\arguments{ +\item{radius}{radius of the hexagon} + +\item{major_axis}{orientation of the major axis 'v' is vertical (default) +and 'h' is horizontal} +} +\value{ +a data.table of regular hexagon vertices +} +\description{ +Generates vertex coordinates for a regular hexagon. +} +\seealso{ +polyStamp circleVertices rectVertices +} diff --git a/man/is_emacs_with_color.Rd b/man/is_emacs_with_color.Rd new file mode 100644 index 000000000..461fefd8c --- /dev/null +++ b/man/is_emacs_with_color.Rd @@ -0,0 +1,12 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utilities.R +\name{is_emacs_with_color} +\alias{is_emacs_with_color} +\title{is_emacs_with_color} +\usage{ +is_emacs_with_color() +} +\description{ +is_emacs_with_color +} +\keyword{internal} diff --git a/man/is_latex_output.Rd b/man/is_latex_output.Rd new file mode 100644 index 000000000..7dd7b7356 --- /dev/null +++ b/man/is_latex_output.Rd @@ -0,0 +1,12 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utilities.R +\name{is_latex_output} +\alias{is_latex_output} +\title{is_latex_output} +\usage{ +is_latex_output() +} +\description{ +is_latex_output +} +\keyword{internal} diff --git a/man/overlapToMatrix.Rd b/man/overlapToMatrix.Rd index bee25957a..caa649ca4 100644 --- a/man/overlapToMatrix.Rd +++ b/man/overlapToMatrix.Rd @@ -9,6 +9,7 @@ overlapToMatrix( name = "raw", poly_info = "cell", feat_info = "rna", + count_info_column = NULL, return_gobject = TRUE ) } @@ -21,6 +22,8 @@ overlapToMatrix( \item{feat_info}{feature information} +\item{count_info_column}{column with count information} + \item{return_gobject}{return giotto object (default: TRUE)} } \value{ diff --git a/man/plot_feature_points_layer.Rd b/man/plot_feature_points_layer.Rd index 13355f987..27277f0a1 100644 --- a/man/plot_feature_points_layer.Rd +++ b/man/plot_feature_points_layer.Rd @@ -16,7 +16,11 @@ plot_feature_points_layer( shape = "feat", point_size = 1.5, show_legend = TRUE, - plot_method = c("ggplot", "scattermore", "scattermost") + plot_method = c("ggplot", "scattermore", "scattermost"), + expand_counts = FALSE, + count_info_column = "count", + jitter = c(0, 0), + verbose = TRUE ) } \value{ diff --git a/man/rectVertices.Rd b/man/rectVertices.Rd index 6249d79ed..05be34af2 100644 --- a/man/rectVertices.Rd +++ b/man/rectVertices.Rd @@ -18,5 +18,5 @@ Generates vertex coordinates for a rectangle with dimensions given through \code{dims} param. } \seealso{ -polyStamp circleVertices +polyStamp circleVertices hexVertices } diff --git a/man/spatInSituPlotPoints.Rd b/man/spatInSituPlotPoints.Rd index 3d9cd13e2..21a71573d 100644 --- a/man/spatInSituPlotPoints.Rd +++ b/man/spatInSituPlotPoints.Rd @@ -19,6 +19,9 @@ spatInSituPlotPoints( sdimx = "x", sdimy = "y", point_size = 1.5, + expand_counts = FALSE, + count_info_column = "count", + jitter = c(0, 0), show_polygon = TRUE, use_overlap = TRUE, polygon_feat_type = "cell", @@ -42,7 +45,8 @@ spatInSituPlotPoints( return_plot = NA, save_plot = NA, save_param = list(), - default_save_name = "spatInSituPlotPoints" + default_save_name = "spatInSituPlotPoints", + verbose = TRUE ) } \arguments{ @@ -74,6 +78,12 @@ spatInSituPlotPoints( \item{point_size}{size of the points} +\item{expand_counts}{expand feature coordinate counts (see details)} + +\item{count_info_column}{column name with count information (if expand_counts = TRUE)} + +\item{jitter}{maximum x,y jitter provided as c(x, y)} + \item{show_polygon}{overlay polygon information (e.g. cell shape)} \item{use_overlap}{use polygon and feature coordinates overlap results} @@ -122,6 +132,8 @@ left as \code{NULL}, the median value detected will be chosen} \item{save_param}{list of saving parameters, see \code{\link{showSaveParameters}}} \item{default_save_name}{default save name for saving, don't change, change save_name in save_param} + +\item{verbose}{verbosity} } \value{ ggplot diff --git a/man/use_color_text.Rd b/man/use_color_text.Rd new file mode 100644 index 000000000..31121617a --- /dev/null +++ b/man/use_color_text.Rd @@ -0,0 +1,12 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utilities.R +\name{use_color_text} +\alias{use_color_text} +\title{use_color_text} +\usage{ +use_color_text() +} +\description{ +use_color_text +} +\keyword{internal} From ceb94361e84ae365e0561e75885ada3a64c0bdfa Mon Sep 17 00:00:00 2001 From: jiajic <72078254+jiajic@users.noreply.github.com> Date: Fri, 25 Nov 2022 02:50:15 -0500 Subject: [PATCH 19/45] patch join function --- R/giotto.R | 237 +++++++++++++-------- man/join_feat_meta.Rd | 12 ++ man/{check_prov_match.Rd => prov_match.Rd} | 11 +- 3 files changed, 166 insertions(+), 94 deletions(-) create mode 100644 man/join_feat_meta.Rd rename man/{check_prov_match.Rd => prov_match.Rd} (57%) diff --git a/R/giotto.R b/R/giotto.R index eb7b29f3d..f8b153d22 100644 --- a/R/giotto.R +++ b/R/giotto.R @@ -4374,20 +4374,26 @@ objHistory = function(object) { #### provenance #### #' @title Check provenance info matches across list of S4 subobjects -#' @name check_prov_match +#' @name prov_match #' @param ... list of s4 subobjects to match provenance +#' @param verbose print discovered provenance info #' @keywords internal #' @return NULL -check_prov_match = function(...) { +prov_match = function(..., verbose = FALSE) { + + if(length(match.call()) == 2) { + if(isS4(...)) stop('more than one object must be provided') + s4_list = as.list(...) + } else if(length(match.call()) > 2) { + s4_list = list(...) + } + + if(!all(unlist(lapply(s4_list, inherits, 'provData')))) + stop('Not all items to test contain provenance information\n') - s4_list = list(...) prov_list = lapply(s4_list, prov) - # if(isTRUE(verbose)) { - # out_dt = data.table::data.table( - # class() - # ) - # } + if(isTRUE(verbose)) print(unlist(prov_list)) length(unique(prov_list)) == 1 @@ -4456,6 +4462,32 @@ join_cell_meta = function(dt_list) { } +#' @title join_feat_meta +#' @name join_feat_meta +#' @keywords internal +join_feat_meta = function(dt_list) { + + feat_ID = NULL + + comb_meta = do.call('rbind', c(dt_list, fill = TRUE)) + comb_meta = unique(comb_meta) + + # find feat_IDs with multiple metadata versions + dup_feats = unique(comb_meta[duplicated(feat_ID), feat_ID]) + + # pick first entry of duplicates + comb_meta = comb_meta[!duplicated(feat_ID)] + + if(length(dup_feats) > 0) { + warning(wrap_txt('feature metadata: multiple versions of metadata for:\n', + dup_feats, + '\n First entry will be selected for joined object.')) + } + + return(comb_meta) + +} + #' @title Join giotto objects #' @name joinGiottoObjects @@ -4675,7 +4707,12 @@ joinGiottoObjects = function(gobject_list, for(spat_unit in names(gobj@cell_ID)) { old_cell_ID = get_cell_id(gobject = gobj, spat_unit = spat_unit) - all_cell_ID_list[[spat_unit]][[gobj_i]] = paste0(gname, '-', old_cell_ID) + new_cell_ID = paste0(gname, '-', old_cell_ID) + all_cell_ID_list[[spat_unit]][[gobj_i]] = new_cell_ID + gobj = set_cell_id(gobject = gobj, + spat_unit = spat_unit, + cell_IDs = new_cell_ID, + set_defaults = FALSE) } @@ -4919,6 +4956,7 @@ joinGiottoObjects = function(gobject_list, ## 4. cell metadata + # * (feat metadata happens during joined object creation) # rbind metadata # create capture area specific names if(verbose) wrap_msg('4. rbind cell metadata') @@ -5099,12 +5137,11 @@ joinGiottoObjects = function(gobject_list, ## expression and feat IDs ## if no expression matrices are provided, then just combine all feature IDs - ## additionally, do feature metadata if(verbose) wrap_msg('2. expression data \n') - expr_names = names(first_obj@expression) + avail_expr = list_expression(gobject = first_obj) - if(is.null(expr_names)) { + if(is.null(avail_expr)) { ## feat IDS for(feat in first_features) { @@ -5112,68 +5149,53 @@ joinGiottoObjects = function(gobject_list, ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### comb_gobject = set_feat_id(gobject = comb_gobject, feat_type = feat, - feat_IDs = combined_feat_ID) - ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### - - # TODO: figure out best way - S4_feat_metadata = create_feat_meta_obj(spat_unit = spat_unit, - feat_type = feat_type, - metaDT = data.table::data.table(feat_ID = combined_feat_ID)) + feat_IDs = combined_feat_ID, + set_defaults = FALSE) ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### - comb_gobject = set_feature_metadata(gobject = comb_gobject, - S4_feat_metadata, - set_defaults = FALSE) - ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### - } + # Moved de novo feature metadata generation as a catch to the end of the fxn + # Done through init_feat_meta() + # S4_feat_metadata = create_feat_meta_obj(spat_unit = spat_unit, + # feat_type = feat_type, + # metaDT = data.table::data.table(feat_ID = combined_feat_ID)) - } else { - - for(spat_unit in names(first_obj@expression)) { - - for(feat_type in names(first_obj@expression[[spat_unit]])) { - - for(mode in names(first_obj@expression[[spat_unit]][[feat_type]])) { + # comb_gobject = set_feature_metadata(gobject = comb_gobject, + # S4_feat_metadata, + # set_defaults = FALSE) - savelist = list() - for(gobj_i in seq_along(updated_object_list)) { - - mat = updated_object_list[[gobj_i]]@expression[[spat_unit]][[feat_type]][[mode]][] - savelist[[gobj_i]] = mat - } - combmat = join_expression_matrices(matrix_list = savelist) - - S4_expr_object = create_expr_obj(exprMat = combmat[['matrix']], - name = mode, - spat_unit = spat_unit, - feat_type = feat_type) - - ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### - comb_gobject = set_expression_values(gobject = comb_gobject, - S4_expr_object) - ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### + } else { - #comb_gobject@expression[[spat_unit]][[feat_type]][[mode]] = combmat$matrix + for(exprObj_i in seq(nrow(avail_expr))) { - comb_gobject@feat_ID[[feat_type]] = combmat[['sort_all_feats']] + expr_list = lapply(updated_object_list, function(gobj) { + get_expression_values(gobject = gobj, + spat_unit = avail_expr$spat_unit[[exprObj_i]], + feat_type = avail_expr$feat_type[[exprObj_i]], + values = avail_expr$name[[exprObj_i]], + output = 'exprObj', + set_defaults = FALSE) + }) - S4_feat_metadata = create_feat_meta_obj(spat_unit = spat_unit, - feat_type = feat_type, - metaDT = data.table::data.table(feat_ID = combmat$sort_all_feats)) - ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### - comb_gobject = set_feature_metadata(gobject = comb_gobject, - metadata = S4_feat_metadata) - ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### + if(!prov_match(expr_list)) warning(wrap_txt('expression: provenance mismatch')) - #comb_gobject@feat_metadata[[spat_unit]][[feat_type]] = data.table::data.table(feat_ID = combmat$sort_all_feats) + combmat = join_expression_matrices(matrix_list = lapply(expr_list, function(expr) expr[])) + expr_list[[1]][] = combmat[['matrix']] - } + ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### + comb_gobject = set_expression_values(gobject = comb_gobject, + values = expr_list[[1]], + set_defaults = FALSE) + comb_gobject = set_feat_id(gobject = comb_gobject, + feat_type = avail_expr$feat_type[[exprObj_i]], + feat_IDs = combmat[['sort_all_feats']], + set_defaults = FALSE) + ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### - } + # Moved de novo feat metadata generation to end of fxn as a catch } @@ -5182,38 +5204,31 @@ joinGiottoObjects = function(gobject_list, - ## spatial locations if(verbose) wrap_msg('3. spatial locations \n') available_locs = list_spatial_locations(first_obj) - for(spat_unit_i in available_locs[['spat_unit']]) { + for(slObj_i in seq(nrow(available_locs))) { - for(name_i in available_locs[spat_unit == spat_unit_i, name]) { + sl_list = lapply(updated_object_list, function(gobj) { + get_spatial_locations(gobject = gobj, + spat_unit = available_locs$spat_unit[[slObj_i]], + spat_loc_name = available_locs$name[[slObj_i]], + output = 'spatLocsObj', + copy_obj = FALSE) + }) - savelist = list() - for(gobj_i in seq_along(updated_object_list)) { - spatlocs = get_spatial_locations(gobject = updated_object_list[[gobj_i]], - spat_unit = spat_unit_i, - spat_loc_name = name_i, - output = 'data.table', - copy_obj = FALSE) + if(!prov_match(sl_list)) warning(wrap_txt('spatial locations: provenance mismatch')) - savelist[[gobj_i]] = spatlocs - } - - combspatlocs = join_spatlocs(dt_list = savelist) - combspat_obj = new('spatLocsObj', - name = name_i, - spat_unit = spat_unit_i, - coordinates = combspatlocs, - provenance = NULL) - comb_gobject = set_spatial_locations(comb_gobject, - spatlocs = combspat_obj, - set_defaults = FALSE) - } + combspatlocs = join_spatlocs(dt_list = lapply(sl_list, function(sl) sl[])) + sl_list[[1]][] = combspatlocs + ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### + comb_gobject = set_spatial_locations(comb_gobject, + spatlocs = sl_list[[1]], + set_defaults = FALSE) + ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### } @@ -5221,7 +5236,7 @@ joinGiottoObjects = function(gobject_list, ## cell metadata - if(verbose) wrap_msg('4. cell metadata \n') + if(isTRUE(verbose)) wrap_msg('4. cell metadata \n') for(spat_unit in names(first_obj@cell_metadata)) { @@ -5234,30 +5249,65 @@ joinGiottoObjects = function(gobject_list, } combcellmeta = join_cell_meta(dt_list = savelist) - S4_cell_meta = create_cell_meta_obj(metaDT = combcellmeta, - spat_unit = spat_unit, - feat_type = feat_type) + S4_cell_meta = get_cell_metadata(gobject = first_obj, + spat_unit = spat_unit, + feat_type = feat_type, + copy_obj = TRUE, + set_defaults = FALSE, + output = 'cellMetaObj') + S4_cell_meta[] = combcellmeta + ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### comb_gobject = set_cell_metadata(gobject = comb_gobject, metadata = S4_cell_meta, set_defaults = FALSE) - - #comb_gobject@cell_metadata[[spat_unit]][[feat_type]] = combcellmeta + ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### } } + ## feat metadata + # check if any already exists in first_obj + # otherwise, skip and generate feat_metadata de novo at end + avail_featmeta = list_feat_metadata(gobject = first_obj) + if(!is.null(avail_featmeta)) { + if(isTRUE(verbose)) message(' feature metadata \n') + for(fmObj_i in seq(nrow(avail_featmeta))) { + + fm_list = lapply(updated_object_list, function(gobj) { + get_feature_metadata(gobject = gobj, + spat_unit = avail_featmeta$spat_unit[[fmObj_i]], + feat_type = avail_featmeta$feat_type[[fmObj_i]], + output = 'featMetaObj', + copy_obj = TRUE) + }) + + if(!prov_match(fm_list)) warning(wrap_txt('feature metadata: provenance mismatch')) + + comb_fm = join_feat_meta(dt_list = lapply(fm_list, function(fm) fm[])) + fm_list[[1]][] = comb_fm + + ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### + comb_gobject = set_feature_metadata(gobject = comb_gobject, + metadata = fm_list[[1]], + set_defaults = FALSE) + ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### + + } + + } + ## spatial info - if(verbose) wrap_msg('5. spatial polygon information \n') + if(isTRUE(verbose)) wrap_msg('5. spatial polygon information \n') available_spat_info = unique(unlist(all_spatinfo_list)) - if(verbose) { + if(isTRUE(verbose)) { wrap_msg ('available_spat_info: \n') print(available_spat_info) } @@ -5335,6 +5385,11 @@ joinGiottoObjects = function(gobject_list, } + ## If no feature_metadata exists, then generate now + if(is.null(list_cell_metadata(comb_gobject))) { + comb_gobject = init_feat_metadata() + } + diff --git a/man/join_feat_meta.Rd b/man/join_feat_meta.Rd new file mode 100644 index 000000000..06f84140b --- /dev/null +++ b/man/join_feat_meta.Rd @@ -0,0 +1,12 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/giotto.R +\name{join_feat_meta} +\alias{join_feat_meta} +\title{join_feat_meta} +\usage{ +join_feat_meta(dt_list) +} +\description{ +join_feat_meta +} +\keyword{internal} diff --git a/man/check_prov_match.Rd b/man/prov_match.Rd similarity index 57% rename from man/check_prov_match.Rd rename to man/prov_match.Rd index f3e31e639..985c36810 100644 --- a/man/check_prov_match.Rd +++ b/man/prov_match.Rd @@ -1,10 +1,15 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/giotto.R -\name{check_prov_match} -\alias{check_prov_match} +\name{prov_match} +\alias{prov_match} \title{Check provenance info matches across list of S4 subobjects} \usage{ -check_prov_match(s4_list) +prov_match(..., verbose = FALSE) +} +\arguments{ +\item{...}{list of s4 subobjects to match provenance} + +\item{verbose}{print discovered provenance info} } \description{ Check provenance info matches across list of S4 subobjects From 1e175d3d5419e7cfe745c567d078c747c5d63060 Mon Sep 17 00:00:00 2001 From: jiajic <72078254+jiajic@users.noreply.github.com> Date: Fri, 25 Nov 2022 03:18:12 -0500 Subject: [PATCH 20/45] fix conflicts and document --- R/classes.R | 5 ++--- man/ansi_colors.Rd | 12 ------------ man/cellMetaObj-class.Rd | 2 -- man/dimObj-class.Rd | 2 ++ man/emacs_version.Rd | 12 ------------ man/featMetaObj-class.Rd | 4 +--- man/is_emacs_with_color.Rd | 12 ------------ man/is_latex_output.Rd | 12 ------------ man/use_color_text.Rd | 12 ------------ 9 files changed, 5 insertions(+), 68 deletions(-) delete mode 100644 man/ansi_colors.Rd delete mode 100644 man/emacs_version.Rd delete mode 100644 man/is_emacs_with_color.Rd delete mode 100644 man/is_latex_output.Rd delete mode 100644 man/use_color_text.Rd diff --git a/R/classes.R b/R/classes.R index 19e2a5463..99ca6a090 100644 --- a/R/classes.R +++ b/R/classes.R @@ -604,7 +604,6 @@ 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) -#' @slot misc misc #' @export setClass('cellMetaObj', contains = c('metaData', 'spatFeatData'), @@ -655,12 +654,11 @@ check_feat_meta_obj = function(object) { # * Definition #### #' @title S4 featMetaObj #' @description Framework to store feature metadata -#' @slot metadata metadata info +#' @slot metaDT metadata info #' @slot col_desc (optional) character vector describing columns of the metadata #' @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) -#' @slot misc misc #' @export setClass('featMetaObj', contains = c('metaData', 'spatFeatData'), @@ -725,6 +723,7 @@ check_dim_obj = function(object) { #' @slot feat_type feature type of data #' @slot spat_unit spatial unit of data #' @slot provenance origin of aggregated information (if applicable) +#' @slot reduction whether reduction was performed on 'feats' or 'cells' #' @slot reduction_method method used to generate dimension reduction #' @slot coordinates embedding coordinates #' @slot misc method-specific additional outputs diff --git a/man/ansi_colors.Rd b/man/ansi_colors.Rd deleted file mode 100644 index a4ccf8f8d..000000000 --- a/man/ansi_colors.Rd +++ /dev/null @@ -1,12 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/utilities.R -\name{ansi_colors} -\alias{ansi_colors} -\title{ansi_colors} -\usage{ -ansi_colors() -} -\description{ -ansi_colors -} -\keyword{internal} diff --git a/man/cellMetaObj-class.Rd b/man/cellMetaObj-class.Rd index c0efc086d..d911c7eda 100644 --- a/man/cellMetaObj-class.Rd +++ b/man/cellMetaObj-class.Rd @@ -19,7 +19,5 @@ Framework to store cell metadata \item{\code{feat_type}}{feature type of aggregated expression (e.g. 'rna', 'protein')} \item{\code{provenance}}{origin data of aggregated expression information (if applicable)} - -\item{\code{misc}}{misc} }} diff --git a/man/dimObj-class.Rd b/man/dimObj-class.Rd index fa3d5475c..a776e4e1e 100644 --- a/man/dimObj-class.Rd +++ b/man/dimObj-class.Rd @@ -18,6 +18,8 @@ Framework to store dimension reduction information \item{\code{provenance}}{origin of aggregated information (if applicable)} +\item{\code{reduction}}{whether reduction was performed on 'feats' or 'cells'} + \item{\code{reduction_method}}{method used to generate dimension reduction} \item{\code{coordinates}}{embedding coordinates} diff --git a/man/emacs_version.Rd b/man/emacs_version.Rd deleted file mode 100644 index 58148e0db..000000000 --- a/man/emacs_version.Rd +++ /dev/null @@ -1,12 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/utilities.R -\name{emacs_version} -\alias{emacs_version} -\title{emacs_version} -\usage{ -emacs_version() -} -\description{ -emacs_version -} -\keyword{internal} diff --git a/man/featMetaObj-class.Rd b/man/featMetaObj-class.Rd index a0aca3c17..e9805eb8f 100644 --- a/man/featMetaObj-class.Rd +++ b/man/featMetaObj-class.Rd @@ -10,7 +10,7 @@ Framework to store feature metadata \section{Slots}{ \describe{ -\item{\code{metadata}}{metadata info} +\item{\code{metaDT}}{metadata info} \item{\code{col_desc}}{(optional) character vector describing columns of the metadata} @@ -19,7 +19,5 @@ Framework to store feature metadata \item{\code{feat_type}}{feature type of aggregated expression (e.g. 'rna', 'protein')} \item{\code{provenance}}{origin data of aggregated expression information (if applicable)} - -\item{\code{misc}}{misc} }} diff --git a/man/is_emacs_with_color.Rd b/man/is_emacs_with_color.Rd deleted file mode 100644 index 461fefd8c..000000000 --- a/man/is_emacs_with_color.Rd +++ /dev/null @@ -1,12 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/utilities.R -\name{is_emacs_with_color} -\alias{is_emacs_with_color} -\title{is_emacs_with_color} -\usage{ -is_emacs_with_color() -} -\description{ -is_emacs_with_color -} -\keyword{internal} diff --git a/man/is_latex_output.Rd b/man/is_latex_output.Rd deleted file mode 100644 index 7dd7b7356..000000000 --- a/man/is_latex_output.Rd +++ /dev/null @@ -1,12 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/utilities.R -\name{is_latex_output} -\alias{is_latex_output} -\title{is_latex_output} -\usage{ -is_latex_output() -} -\description{ -is_latex_output -} -\keyword{internal} diff --git a/man/use_color_text.Rd b/man/use_color_text.Rd deleted file mode 100644 index 31121617a..000000000 --- a/man/use_color_text.Rd +++ /dev/null @@ -1,12 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/utilities.R -\name{use_color_text} -\alias{use_color_text} -\title{use_color_text} -\usage{ -use_color_text() -} -\description{ -use_color_text -} -\keyword{internal} From e73aa96118e894311f600727f1391cfcd073f819 Mon Sep 17 00:00:00 2001 From: RubD Date: Fri, 25 Nov 2022 16:52:37 -0500 Subject: [PATCH 21/45] set coord_fix_ratio to 1 --- R/spatial_in_situ_visuals.R | 2 +- R/spatial_visuals.R | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/R/spatial_in_situ_visuals.R b/R/spatial_in_situ_visuals.R index b6412ae1c..bf91fa0e2 100644 --- a/R/spatial_in_situ_visuals.R +++ b/R/spatial_in_situ_visuals.R @@ -366,7 +366,7 @@ spatInSituPlotPoints = function(gobject, axis_text = 8, axis_title = 8, legend_text = 6, - coord_fix_ratio = NULL, + coord_fix_ratio = 1, background_color = 'black', show_legend = TRUE, plot_method = c('ggplot', 'scattermore', 'scattermost'), diff --git a/R/spatial_visuals.R b/R/spatial_visuals.R index df04a2460..47855f796 100644 --- a/R/spatial_visuals.R +++ b/R/spatial_visuals.R @@ -2751,7 +2751,7 @@ spatPlot2D_single = function(gobject, other_cell_color = 'lightgrey', other_point_size = 1, other_cells_alpha = 0.1, - coord_fix_ratio = NULL, + coord_fix_ratio = 1, title = NULL, show_legend = T, legend_text = 8, @@ -3575,7 +3575,7 @@ spatDeconvPlot = function(gobject, title = NULL, axis_text = 8, axis_title = 8, - coord_fix_ratio = TRUE, + coord_fix_ratio = 1, show_plot = NA, return_plot = NA, save_plot = NA, @@ -4031,7 +4031,7 @@ spatDimPlot2D <- function(gobject, other_cell_color = other_cell_color, other_point_size = spat_other_point_size, other_cells_alpha = spat_other_cells_alpha, - coord_fix_ratio = NULL, + coord_fix_ratio = 1, title = '', show_legend = spat_show_legend, legend_text = legend_text, @@ -5743,7 +5743,7 @@ spatCellPlot2D = function(gobject, other_cell_color = 'lightgrey', other_point_size = 1, other_cells_alpha = 0.1, - coord_fix_ratio = NULL, + coord_fix_ratio = 1, show_legend = T, legend_text = 8, legend_symbol_size = 1, @@ -6300,7 +6300,7 @@ spatDimCellPlot2D <- function(gobject, vor_alpha = 1, axis_text = 8, axis_title = 8, - coord_fix_ratio = NULL, + coord_fix_ratio = 1, cow_n_col = 2, cow_rel_h = 1, cow_rel_w = 1, From 75c7818779c30aa2740aa9f5f529f93542ac1c55 Mon Sep 17 00:00:00 2001 From: RubD Date: Sat, 26 Nov 2022 14:14:13 -0500 Subject: [PATCH 22/45] update subcellular fx for large images --- R/giotto.R | 6 +++--- R/giotto_structures.R | 14 +++++++++++--- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/R/giotto.R b/R/giotto.R index f8b153d22..0ef15e8d0 100644 --- a/R/giotto.R +++ b/R/giotto.R @@ -2888,9 +2888,9 @@ createGiottoObjectSubcellular = function(gpolygons = NULL, name = im_name, largeImages_list_params)) - glargeImage = createGiottoLargeImage(raster_object = im, - negative_y = FALSE, - name = im_name) + # glargeImage = createGiottoLargeImage(raster_object = im, + # negative_y = FALSE, + # name = im_name) gobject = addGiottoImage(gobject = gobject, largeImages = list(glargeImage)) diff --git a/R/giotto_structures.R b/R/giotto_structures.R index c7722ff19..1b70f551a 100644 --- a/R/giotto_structures.R +++ b/R/giotto_structures.R @@ -2092,7 +2092,7 @@ overlap_points_single_polygon = function(spatvec, #' @concept overlap #' @export calculateOverlapPolygonImages = function(gobject, - name_overlap = 'images', + name_overlap = 'protein', spatial_info = 'cell', poly_ID_names = NULL, image_names = NULL, @@ -2147,10 +2147,14 @@ calculateOverlapPolygonImages = function(gobject, intensity_image = get_giottoLargeImage(gobject = gobject, name = img_name) intensity_image = intensity_image@raster_object + names(intensity_image) = img_name + image_list[[i]] = intensity_image } + print('0. create image list') + image_vector_c = do.call('c', image_list) # convert spatVectot to sf object @@ -2161,6 +2165,8 @@ calculateOverlapPolygonImages = function(gobject, } + print('1. start extraction') + extract_intensities_exact = exactextractr::exact_extract(x = image_vector_c, y = poly_info_spatvector_sf, include_cols = 'poly_ID', @@ -2169,8 +2175,10 @@ calculateOverlapPolygonImages = function(gobject, dt_exact = data.table::as.data.table(do.call('rbind', extract_intensities_exact)) # prepare output - colnames(dt_exact)[2:(length(image_names)+1)] = image_names + print(dt_exact) + colnames(dt_exact)[2:(length(image_names)+1)] = image_names # probably not needed anymore dt_exact[, coverage_fraction := NULL] + print(dt_exact) if(return_gobject) { @@ -2708,7 +2716,7 @@ overlapImagesToMatrix = function(gobject, cell_IDs = unique(as.character(aggr_comb$poly_ID)) feat_IDs = unique(as.character(aggr_comb$feat_ID)) - print(feat_IDs[1:10]) + #print(feat_IDs[1:10]) # create cell and feature metadata S4_cell_meta = create_cell_meta_obj(metaDT = data.table::data.table(cell_ID = cell_IDs), From 3cc23e64e29bf87dd374f6d6938232a17cd11c27 Mon Sep 17 00:00:00 2001 From: RubD Date: Sat, 26 Nov 2022 16:45:28 -0500 Subject: [PATCH 23/45] tag cells and feats --- R/auxiliary_giotto.R | 48 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 4 deletions(-) diff --git a/R/auxiliary_giotto.R b/R/auxiliary_giotto.R index ae68851ab..b7dbf4518 100644 --- a/R/auxiliary_giotto.R +++ b/R/auxiliary_giotto.R @@ -2025,6 +2025,10 @@ filterCombinations <- function(gobject, #' @param all_feat_types apply cells to remove filtering results from current #' spatial unit/feature type combination across ALL feature types (default = TRUE) #' @param poly_info polygon information to use +#' @param tag_cells tag filtered cells in metadata vs. remove cells +#' @param tag_cell_name column name for tagged cells in metadata +#' @param tag_feats tag features in metadata vs. remove features +#' @param tag_feats_name column name for tagged features in metadata #' @param verbose verbose #' @return giotto object #' @details The function \code{\link{filterCombinations}} can be used to explore the effect of different parameter values. @@ -2041,6 +2045,10 @@ filterGiotto = function(gobject, all_spat_units = TRUE, all_feat_types = TRUE, poly_info = 'cell', + tag_cells = FALSE, + tag_cell_name = 'tag', + tag_feats = FALSE, + tag_feats_name = 'tag', verbose = TRUE) { ## deprecated arguments @@ -2079,15 +2087,36 @@ filterGiotto = function(gobject, filter_index_feats = rowSums_flex(expr_values >= expression_threshold) >= feat_det_in_min_cells selected_feat_ids = names(filter_index_feats[filter_index_feats == TRUE]) - #selected_feat_ids = gobject@feat_ID[[feat_type]][filter_index_feats] ## filter cells filter_index_cells = colSums_flex(expr_values[filter_index_feats, ] >= expression_threshold) >= min_det_feats_per_cell selected_cell_ids = names(filter_index_cells[filter_index_cells == TRUE]) - #selected_cell_ids = gobject@cell_ID[[spat_unit]][filter_index_cells] + + # update cell metadata + if(isTRUE(tag_cells)) { + cell_meta = get_cell_metadata(gobject = gobject, copy_obj = TRUE) + cell_meta[][, c(tag_cell_name) := ifelse(cell_ID %in% selected_cell_ids, 0, 1)] + gobject = set_cell_metadata(gobject = gobject, metadata = cell_meta) + + # set selected cells back to all cells + selected_cell_ids = names(filter_index_cells) + } + + if(isTRUE(tag_feats)) { + feat_meta = get_feature_metadata(gobject = gobject, copy_obj = TRUE) + feat_meta[][, c(tag_feats_name) := ifelse(feat_ID %in% selected_feat_ids, 0, 1)] + gobject = set_feature_metadata(gobject = gobject, metadata = feat_meta) + + # set selected feats back to all feats + selected_feat_ids = names(filter_index_feats) + } + + + + # update feature metadata newGiottoObject = subsetGiotto(gobject = gobject, feat_type = feat_type, spat_unit = spat_unit, @@ -2106,9 +2135,20 @@ filterGiotto = function(gobject, total_cells = length(filter_index_cells) if(verbose == TRUE) { + cat('\n') cat('Feature type: ', feat_type, '\n') - cat('Number of cells removed: ', removed_cells, ' out of ', total_cells, '\n') - cat('Number of feats removed: ', removed_feats, ' out of ', total_feats, '\n') + + if(isTRUE(tag_cells)) { + cat('Number of cells tagged: ', removed_cells, ' out of ', total_cells, '\n') + } else { + cat('Number of cells removed: ', removed_cells, ' out of ', total_cells, '\n') + } + + if(isTRUE(tag_feats)) { + cat('Number of feats tagged: ', removed_feats, ' out of ', total_feats, '\n') + } else { + cat('Number of feats removed: ', removed_feats, ' out of ', total_feats, '\n') + } } From a2f972b2c01adf58c77ef496306904228149ef02 Mon Sep 17 00:00:00 2001 From: jiajic <72078254+jiajic@users.noreply.github.com> Date: Sat, 26 Nov 2022 17:56:44 -0500 Subject: [PATCH 24/45] allow set by reference for DT classes --- R/classes.R | 39 +++++++++++++++++++++++++++++++++++++++ R/generics.R | 2 +- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/R/classes.R b/R/classes.R index 99ca6a090..cb12d0127 100644 --- a/R/classes.R +++ b/R/classes.R @@ -53,6 +53,13 @@ setClass('coordDataDT', representation = list(coordinates = 'data.table'), prototype = prototype(coordinates = data.table::data.table())) +setMethod('initialize', 'coordDataDT', + function(.Object, ...) { + # prepare DT for set by reference + .Object@coordinates = data.table::setalloccol(.Object@coordinates) + .Object + }) + # setClass('coordDataMT', # representation = list(coordinates = 'matrix'), # prototype = prototype(coordinates = matrix())) @@ -71,6 +78,14 @@ setClass('metaData', col_desc = NA_character_)) +setMethod('initialize', 'metaData', + function(.Object, ...) { + # prepare DT for set by reference + .Object@metaDT = data.table::setalloccol(.Object@metaDT) + .Object + }) + + # ** enrData #### setClass('enrData', representation = list(method = 'character', @@ -78,6 +93,14 @@ setClass('enrData', prototype = prototype(method = NA_character_, enrichDT = NULL)) +setMethod('initialize', 'enrData', + function(.Object, ...) { + # prepare DT for set by reference + .Object@enrichDT = data.table::setalloccol(.Object@enrichDT) + .Object + }) + + # ** nnData #### setClass('nnData', @@ -102,6 +125,15 @@ setClass('spatNetData', networkDT_before_filter = NULL, cellShapeObj = NULL)) +setMethod('initialize', 'spatNetData', + function(.Object, ...) { + # prepare DT for set by reference + .Object@networkDT = data.table::setalloccol(.Object@networkDT) + .Object@networkDT_before_filter = data.table::setalloccol(.Object@networkDT_before_filter) + .Object + }) + + # ** spatGridData #### setClass('spatGridData', representation = list(method = 'character', @@ -112,6 +144,13 @@ setClass('spatGridData', gridDT = NULL)) +setMethod('initialize', 'spatGridData', + function(.Object, ...) { + # prepare DT for set by reference + .Object@gridDT = data.table::setalloccol(.Object@gridDT) + .Object + }) + # ** provData Class #### #' Basic class for classes with provenance information. diff --git a/R/generics.R b/R/generics.R index dcb17d394..73a53083e 100644 --- a/R/generics.R +++ b/R/generics.R @@ -1,6 +1,6 @@ # Methods and Generics #### - +# NOTE: initialize generics are in classes.R # From ee9254b5a2075419c9003ce8b553ff8fb6c88da1 Mon Sep 17 00:00:00 2001 From: jiajic <72078254+jiajic@users.noreply.github.com> Date: Sat, 26 Nov 2022 19:05:38 -0500 Subject: [PATCH 25/45] fix initialize method --- R/classes.R | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/R/classes.R b/R/classes.R index cb12d0127..ed45584e2 100644 --- a/R/classes.R +++ b/R/classes.R @@ -55,6 +55,7 @@ setClass('coordDataDT', setMethod('initialize', 'coordDataDT', function(.Object, ...) { + .Object = callNextMethod() # prepare DT for set by reference .Object@coordinates = data.table::setalloccol(.Object@coordinates) .Object @@ -80,6 +81,7 @@ setClass('metaData', setMethod('initialize', 'metaData', function(.Object, ...) { + .Object = callNextMethod() # prepare DT for set by reference .Object@metaDT = data.table::setalloccol(.Object@metaDT) .Object @@ -95,6 +97,7 @@ setClass('enrData', setMethod('initialize', 'enrData', function(.Object, ...) { + .Object = callNextMethod() # prepare DT for set by reference .Object@enrichDT = data.table::setalloccol(.Object@enrichDT) .Object @@ -127,6 +130,7 @@ setClass('spatNetData', setMethod('initialize', 'spatNetData', function(.Object, ...) { + .Object = callNextMethod() # prepare DT for set by reference .Object@networkDT = data.table::setalloccol(.Object@networkDT) .Object@networkDT_before_filter = data.table::setalloccol(.Object@networkDT_before_filter) @@ -146,6 +150,7 @@ setClass('spatGridData', setMethod('initialize', 'spatGridData', function(.Object, ...) { + .Object = callNextMethod() # prepare DT for set by reference .Object@gridDT = data.table::setalloccol(.Object@gridDT) .Object From 5c0be39768819834893d514fbf59a96e50c0dbee Mon Sep 17 00:00:00 2001 From: jiajic <72078254+jiajic@users.noreply.github.com> Date: Sun, 27 Nov 2022 19:10:09 -0500 Subject: [PATCH 26/45] Update spatial_in_situ_visuals.R --- R/spatial_in_situ_visuals.R | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/R/spatial_in_situ_visuals.R b/R/spatial_in_situ_visuals.R index bf91fa0e2..bff4ce3a7 100644 --- a/R/spatial_in_situ_visuals.R +++ b/R/spatial_in_situ_visuals.R @@ -615,6 +615,7 @@ spatInSituPlotHex_single = function(gobject, polygon_fill_as_factor = NULL, polygon_alpha = 0.5, polygon_size = 0.5, + coord_fix_ratio = NULL, axis_text = 8, axis_title = 8, legend_text = 6, @@ -687,6 +688,10 @@ spatInSituPlotHex_single = function(gobject, panel.grid = element_blank(), panel.background = element_rect(fill = background_color)) + # fix coord ratio + if(!is.null(coord_fix_ratio)) { + plot = plot + ggplot2::coord_fixed(ratio = coord_fix_ratio) + } return(plot) @@ -712,6 +717,7 @@ spatInSituPlotHex_single = function(gobject, #' @param polygon_fill_as_factor is fill color a factor #' @param polygon_alpha alpha of polygon #' @param polygon_size size of polygon border +#' @param coord_fix_ratio fix ratio between x and y-axis #' @param axis_text axis text size #' @param axis_title title text size #' @param legend_text legend text size @@ -743,6 +749,7 @@ spatInSituPlotHex = function(gobject, polygon_fill_as_factor = NULL, polygon_alpha = 0.5, polygon_size = 0.5, + coord_fix_ratio = NULL, axis_text = 8, axis_title = 8, legend_text = 6, @@ -786,6 +793,7 @@ spatInSituPlotHex = function(gobject, polygon_fill_as_factor = polygon_fill_as_factor, polygon_alpha = polygon_alpha, polygon_size = polygon_size, + coord_fix_ratio = coord_fix_ratio, axis_text = axis_text, axis_title = axis_title, legend_text = legend_text, @@ -885,6 +893,7 @@ spatInSituPlotDensity_single = function(gobject, polygon_fill_as_factor = NULL, polygon_alpha = 0.5, polygon_size = 0.5, + coord_fix_ratio = NULL, axis_text = 8, axis_title = 8, legend_text = 6, @@ -955,6 +964,10 @@ spatInSituPlotDensity_single = function(gobject, panel.grid = element_blank(), panel.background = element_rect(fill = background_color)) + # fix coord ratio + if(!is.null(coord_fix_ratio)) { + plot = plot + ggplot2::coord_fixed(ratio = coord_fix_ratio) + } return(plot) @@ -979,6 +992,7 @@ spatInSituPlotDensity_single = function(gobject, #' @param polygon_fill_as_factor is fill color a factor #' @param polygon_alpha alpha of polygon #' @param polygon_size size of polygon border +#' @param coord_fix_ratio fix ratio between x and y-axis #' @param axis_text axis text size #' @param axis_title title text size #' @param legend_text legend text size @@ -1009,6 +1023,7 @@ spatInSituPlotDensity = function(gobject, polygon_fill_as_factor = NULL, polygon_alpha = 0.5, polygon_size = 0.5, + coord_fix_ratio = NULL, axis_text = 8, axis_title = 8, legend_text = 6, @@ -1054,6 +1069,7 @@ spatInSituPlotDensity = function(gobject, polygon_fill_as_factor = polygon_fill_as_factor, polygon_alpha = polygon_alpha, polygon_size = polygon_size, + coord_fix_ratio = coord_fix_ratio, axis_text = axis_text, axis_title = axis_title, legend_text = legend_text, From 3b68d7701333253a4173b5eec279a6f64554864b Mon Sep 17 00:00:00 2001 From: jiajic <72078254+jiajic@users.noreply.github.com> Date: Sun, 27 Nov 2022 19:35:34 -0500 Subject: [PATCH 27/45] Update spatial_in_situ_visuals.R Update spatial_in_situ_visuals.R bypass cowplot for length = 1 and coord_fix_ratio edits --- R/spatial_in_situ_visuals.R | 38 ++++++++++++++++------------ man/calculateOverlapPolygonImages.Rd | 2 +- man/filterGiotto.Rd | 12 +++++++++ man/spatCellPlot2D.Rd | 2 +- man/spatDeconvPlot.Rd | 2 +- man/spatDimCellPlot2D.Rd | 2 +- man/spatInSituPlotDensity.Rd | 3 +++ man/spatInSituPlotDensity_single.Rd | 1 + man/spatInSituPlotHex.Rd | 3 +++ man/spatInSituPlotHex_single.Rd | 1 + man/spatInSituPlotPoints.Rd | 2 +- man/spatPlot2D_single.Rd | 2 +- 12 files changed, 48 insertions(+), 22 deletions(-) diff --git a/R/spatial_in_situ_visuals.R b/R/spatial_in_situ_visuals.R index bff4ce3a7..f70df8b68 100644 --- a/R/spatial_in_situ_visuals.R +++ b/R/spatial_in_situ_visuals.R @@ -693,7 +693,6 @@ spatInSituPlotHex_single = function(gobject, plot = plot + ggplot2::coord_fixed(ratio = coord_fix_ratio) } - return(plot) } @@ -749,7 +748,7 @@ spatInSituPlotHex = function(gobject, polygon_fill_as_factor = NULL, polygon_alpha = 0.5, polygon_size = 0.5, - coord_fix_ratio = NULL, + coord_fix_ratio = 1, axis_text = 8, axis_title = 8, legend_text = 6, @@ -803,12 +802,16 @@ spatInSituPlotHex = function(gobject, } - # combine plots with cowplot - combo_plot <- cowplot::plot_grid(plotlist = savelist, - ncol = cow_n_col, - rel_heights = cow_rel_h, - rel_widths = cow_rel_w, - align = cow_align) + if(length(savelist) == 1) { + combo_plot = savelist[[1]] + } else { + # combine plots with cowplot + combo_plot <- cowplot::plot_grid(plotlist = savelist, + ncol = cow_n_col, + rel_heights = cow_rel_h, + rel_widths = cow_rel_w, + align = cow_align) + } ## print plot @@ -969,7 +972,6 @@ spatInSituPlotDensity_single = function(gobject, plot = plot + ggplot2::coord_fixed(ratio = coord_fix_ratio) } - return(plot) } @@ -1023,7 +1025,7 @@ spatInSituPlotDensity = function(gobject, polygon_fill_as_factor = NULL, polygon_alpha = 0.5, polygon_size = 0.5, - coord_fix_ratio = NULL, + coord_fix_ratio = 1, axis_text = 8, axis_title = 8, legend_text = 6, @@ -1079,13 +1081,17 @@ spatInSituPlotDensity = function(gobject, } + if(length(savelist) == 1) { + combo_plot = savelist[[1]] + } else { + # combine plots with cowplot + combo_plot <- cowplot::plot_grid(plotlist = savelist, + ncol = cow_n_col, + rel_heights = cow_rel_h, + rel_widths = cow_rel_w, + align = cow_align) + } - # combine plots with cowplot - combo_plot <- cowplot::plot_grid(plotlist = savelist, - ncol = cow_n_col, - rel_heights = cow_rel_h, - rel_widths = cow_rel_w, - align = cow_align) ## print plot diff --git a/man/calculateOverlapPolygonImages.Rd b/man/calculateOverlapPolygonImages.Rd index 4b86fa61d..fc87ace86 100644 --- a/man/calculateOverlapPolygonImages.Rd +++ b/man/calculateOverlapPolygonImages.Rd @@ -6,7 +6,7 @@ \usage{ calculateOverlapPolygonImages( gobject, - name_overlap = "images", + name_overlap = "protein", spatial_info = "cell", poly_ID_names = NULL, image_names = NULL, diff --git a/man/filterGiotto.Rd b/man/filterGiotto.Rd index 853ce68d8..e0cff9af0 100644 --- a/man/filterGiotto.Rd +++ b/man/filterGiotto.Rd @@ -17,6 +17,10 @@ filterGiotto( all_spat_units = TRUE, all_feat_types = TRUE, poly_info = "cell", + tag_cells = FALSE, + tag_cell_name = "tag", + tag_feats = FALSE, + tag_feats_name = "tag", verbose = TRUE ) } @@ -47,6 +51,14 @@ spatial unit/feature type combination across ALL feature types (default = TRUE)} \item{poly_info}{polygon information to use} +\item{tag_cells}{tag filtered cells in metadata vs. remove cells} + +\item{tag_cell_name}{column name for tagged cells in metadata} + +\item{tag_feats}{tag features in metadata vs. remove features} + +\item{tag_feats_name}{column name for tagged features in metadata} + \item{verbose}{verbose} } \value{ diff --git a/man/spatCellPlot2D.Rd b/man/spatCellPlot2D.Rd index e72151551..9ed198f4e 100644 --- a/man/spatCellPlot2D.Rd +++ b/man/spatCellPlot2D.Rd @@ -44,7 +44,7 @@ spatCellPlot2D( other_cell_color = "lightgrey", other_point_size = 1, other_cells_alpha = 0.1, - coord_fix_ratio = NULL, + coord_fix_ratio = 1, show_legend = T, legend_text = 8, legend_symbol_size = 1, diff --git a/man/spatDeconvPlot.Rd b/man/spatDeconvPlot.Rd index 281415f05..ad75a67c0 100644 --- a/man/spatDeconvPlot.Rd +++ b/man/spatDeconvPlot.Rd @@ -25,7 +25,7 @@ spatDeconvPlot( title = NULL, axis_text = 8, axis_title = 8, - coord_fix_ratio = TRUE, + coord_fix_ratio = 1, show_plot = NA, return_plot = NA, save_plot = NA, diff --git a/man/spatDimCellPlot2D.Rd b/man/spatDimCellPlot2D.Rd index 46aa4aca7..5d7035361 100644 --- a/man/spatDimCellPlot2D.Rd +++ b/man/spatDimCellPlot2D.Rd @@ -76,7 +76,7 @@ spatDimCellPlot2D( vor_alpha = 1, axis_text = 8, axis_title = 8, - coord_fix_ratio = NULL, + coord_fix_ratio = 1, cow_n_col = 2, cow_rel_h = 1, cow_rel_w = 1, diff --git a/man/spatInSituPlotDensity.Rd b/man/spatInSituPlotDensity.Rd index 4968d2580..53d3455c6 100644 --- a/man/spatInSituPlotDensity.Rd +++ b/man/spatInSituPlotDensity.Rd @@ -18,6 +18,7 @@ spatInSituPlotDensity( polygon_fill_as_factor = NULL, polygon_alpha = 0.5, polygon_size = 0.5, + coord_fix_ratio = NULL, axis_text = 8, axis_title = 8, legend_text = 6, @@ -60,6 +61,8 @@ spatInSituPlotDensity( \item{polygon_size}{size of polygon border} +\item{coord_fix_ratio}{fix ratio between x and y-axis} + \item{axis_text}{axis text size} \item{axis_title}{title text size} diff --git a/man/spatInSituPlotDensity_single.Rd b/man/spatInSituPlotDensity_single.Rd index b69cfe6b4..d037e7edf 100644 --- a/man/spatInSituPlotDensity_single.Rd +++ b/man/spatInSituPlotDensity_single.Rd @@ -18,6 +18,7 @@ spatInSituPlotDensity_single( polygon_fill_as_factor = NULL, polygon_alpha = 0.5, polygon_size = 0.5, + coord_fix_ratio = NULL, axis_text = 8, axis_title = 8, legend_text = 6, diff --git a/man/spatInSituPlotHex.Rd b/man/spatInSituPlotHex.Rd index 73b5cbbef..a10890422 100644 --- a/man/spatInSituPlotHex.Rd +++ b/man/spatInSituPlotHex.Rd @@ -19,6 +19,7 @@ spatInSituPlotHex( polygon_fill_as_factor = NULL, polygon_alpha = 0.5, polygon_size = 0.5, + coord_fix_ratio = NULL, axis_text = 8, axis_title = 8, legend_text = 6, @@ -63,6 +64,8 @@ spatInSituPlotHex( \item{polygon_size}{size of polygon border} +\item{coord_fix_ratio}{fix ratio between x and y-axis} + \item{axis_text}{axis text size} \item{axis_title}{title text size} diff --git a/man/spatInSituPlotHex_single.Rd b/man/spatInSituPlotHex_single.Rd index 0978bbffb..27350d527 100644 --- a/man/spatInSituPlotHex_single.Rd +++ b/man/spatInSituPlotHex_single.Rd @@ -19,6 +19,7 @@ spatInSituPlotHex_single( polygon_fill_as_factor = NULL, polygon_alpha = 0.5, polygon_size = 0.5, + coord_fix_ratio = NULL, axis_text = 8, axis_title = 8, legend_text = 6, diff --git a/man/spatInSituPlotPoints.Rd b/man/spatInSituPlotPoints.Rd index 21a71573d..ac056690f 100644 --- a/man/spatInSituPlotPoints.Rd +++ b/man/spatInSituPlotPoints.Rd @@ -37,7 +37,7 @@ spatInSituPlotPoints( axis_text = 8, axis_title = 8, legend_text = 6, - coord_fix_ratio = NULL, + coord_fix_ratio = 1, background_color = "black", show_legend = TRUE, plot_method = c("ggplot", "scattermore", "scattermost"), diff --git a/man/spatPlot2D_single.Rd b/man/spatPlot2D_single.Rd index ae73f2db1..46c311b56 100644 --- a/man/spatPlot2D_single.Rd +++ b/man/spatPlot2D_single.Rd @@ -47,7 +47,7 @@ spatPlot2D_single( other_cell_color = "lightgrey", other_point_size = 1, other_cells_alpha = 0.1, - coord_fix_ratio = NULL, + coord_fix_ratio = 1, title = NULL, show_legend = T, legend_text = 8, From 4a7fdce2da1e60ddd60b5270a767cfe3f8e5c8e0 Mon Sep 17 00:00:00 2001 From: jiajic <72078254+jiajic@users.noreply.github.com> Date: Sun, 27 Nov 2022 20:45:45 -0500 Subject: [PATCH 28/45] Update spatial_in_situ_visuals.R hexbin change to use binwidth with default of 1/10 of minor axis --- R/spatial_in_situ_visuals.R | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/R/spatial_in_situ_visuals.R b/R/spatial_in_situ_visuals.R index f70df8b68..4410ac122 100644 --- a/R/spatial_in_situ_visuals.R +++ b/R/spatial_in_situ_visuals.R @@ -565,18 +565,24 @@ spatInSituPlotPoints = function(gobject, #' @details This function can plot one feature for one modality. #' @keywords internal plot_feature_hexbin_layer = function(ggobject = NULL, - spatial_feat_info, - sel_feat, - sdimx = 'x', - sdimy = 'y', - bins = 10, - alpha = 0.5) { + spatial_feat_info, + sel_feat, + sdimx = 'x', + sdimy = 'y', + binwidth = NULL, + alpha = 0.5) { # data.table variables feat_ID = NULL spatial_feat_info_subset = spatial_feat_info[feat_ID %in% sel_feat] + # set default binwidth to 1/10 of minor axis + if(is.null(binwidth)) { + minorRange = spatial_feat_info_subset[, min(diff(sapply(.SD, range))), .SDcols = c('x','y')] + binwidth = as.integer(minorRange/10L) + } + if(!is.null(ggobject) & methods::is(ggobject, 'ggplot')) { pl = ggobject } else { @@ -586,7 +592,7 @@ plot_feature_hexbin_layer = function(ggobject = NULL, pl = pl + ggplot2::geom_hex(data = spatial_feat_info_subset, ggplot2::aes_string(x = sdimx, y = sdimy), - bins = bins, + binwidth = binwidth, alpha = alpha) pl = pl + labs(title = sel_feat) return(pl) @@ -606,7 +612,7 @@ spatInSituPlotHex_single = function(gobject, feat_type = 'rna', sdimx = 'x', sdimy = 'y', - bins = 10, + binwidth = NULL, alpha = 0.5, show_polygon = TRUE, polygon_feat_type = 'cell', @@ -672,11 +678,11 @@ spatInSituPlotHex_single = function(gobject, spatial_feat_info = do.call('rbind', spatial_feat_info) plot = plot_feature_hexbin_layer(ggobject = plot, - spatial_feat_info = spatial_feat_info, - sel_feat = feat, - sdimx = sdimx, - sdimy = sdimy, - bins = bins) + spatial_feat_info = spatial_feat_info, + sel_feat = feat, + sdimx = sdimx, + sdimy = sdimy, + bins = bins) ## adjust theme settings @@ -707,7 +713,7 @@ spatInSituPlotHex_single = function(gobject, #' @param feat_type feature types of the feats #' @param sdimx spatial dimension x #' @param sdimy spatial dimension y -#' @param bins number of hexbins in one direction +#' @param binwidth numeric vector for x and y width of bins (default is 1/10 of minor axis) #' @param alpha alpha of hexbin plot #' @param show_polygon overlay polygon information (cell shape) #' @param polygon_feat_type feature type associated with polygon information @@ -739,7 +745,7 @@ spatInSituPlotHex = function(gobject, feat_type = 'rna', sdimx = 'x', sdimy = 'y', - bins = 10, + binwidth = NULL, alpha = 0.5, show_polygon = TRUE, polygon_feat_type = 'cell', From 5fa9c7fece11724a8ca24be8e472b5f1c6c06a8b Mon Sep 17 00:00:00 2001 From: jiajic <72078254+jiajic@users.noreply.github.com> Date: Sun, 27 Nov 2022 20:48:11 -0500 Subject: [PATCH 29/45] fix param --- R/spatial_in_situ_visuals.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/spatial_in_situ_visuals.R b/R/spatial_in_situ_visuals.R index 4410ac122..7784d4b5b 100644 --- a/R/spatial_in_situ_visuals.R +++ b/R/spatial_in_situ_visuals.R @@ -682,7 +682,7 @@ spatInSituPlotHex_single = function(gobject, sel_feat = feat, sdimx = sdimx, sdimy = sdimy, - bins = bins) + binwidth = binwidth) ## adjust theme settings @@ -789,7 +789,7 @@ spatInSituPlotHex = function(gobject, feat_type = feat_type, sdimx = sdimx, sdimy = sdimy, - bins = bins, + binwidth = binwidth, alpha = alpha, show_polygon = show_polygon, polygon_feat_type = polygon_feat_type, From e38321aed3fee5a0a83283e1cd338b550a0657bb Mon Sep 17 00:00:00 2001 From: jiajic <72078254+jiajic@users.noreply.github.com> Date: Sun, 27 Nov 2022 21:30:32 -0500 Subject: [PATCH 30/45] ..density.. warning fix --- R/spatial_in_situ_visuals.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/spatial_in_situ_visuals.R b/R/spatial_in_situ_visuals.R index 7784d4b5b..a84773787 100644 --- a/R/spatial_in_situ_visuals.R +++ b/R/spatial_in_situ_visuals.R @@ -870,7 +870,7 @@ plot_feature_raster_density_layer = function(ggobject = NULL, pl = pl + ggplot2::stat_density_2d(data = spatial_feat_info_subset, ggplot2::aes_string(x = sdimx, y = sdimy, - fill = '..density..'), + fill = 'after_stat(density)'), geom = "raster", alpha = alpha, contour = FALSE) From fa9f512b4a37b091bd205d081e541acec74f479a Mon Sep 17 00:00:00 2001 From: jiajic <72078254+jiajic@users.noreply.github.com> Date: Sun, 27 Nov 2022 21:58:34 -0500 Subject: [PATCH 31/45] Make bins setup more flexible --- R/spatial_in_situ_visuals.R | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/R/spatial_in_situ_visuals.R b/R/spatial_in_situ_visuals.R index a84773787..92f80eb03 100644 --- a/R/spatial_in_situ_visuals.R +++ b/R/spatial_in_situ_visuals.R @@ -570,6 +570,7 @@ plot_feature_hexbin_layer = function(ggobject = NULL, sdimx = 'x', sdimy = 'y', binwidth = NULL, + min_axis_bins = 10L, alpha = 0.5) { # data.table variables @@ -580,7 +581,7 @@ plot_feature_hexbin_layer = function(ggobject = NULL, # set default binwidth to 1/10 of minor axis if(is.null(binwidth)) { minorRange = spatial_feat_info_subset[, min(diff(sapply(.SD, range))), .SDcols = c('x','y')] - binwidth = as.integer(minorRange/10L) + binwidth = as.integer(minorRange/min_axis_bins) } if(!is.null(ggobject) & methods::is(ggobject, 'ggplot')) { @@ -613,6 +614,7 @@ spatInSituPlotHex_single = function(gobject, sdimx = 'x', sdimy = 'y', binwidth = NULL, + min_axis_bins = NULL, alpha = 0.5, show_polygon = TRUE, polygon_feat_type = 'cell', @@ -713,7 +715,10 @@ spatInSituPlotHex_single = function(gobject, #' @param feat_type feature types of the feats #' @param sdimx spatial dimension x #' @param sdimy spatial dimension y -#' @param binwidth numeric vector for x and y width of bins (default is 1/10 of minor axis) +#' @param binwidth numeric vector for x and y width of bins (default is minor axis +#' range/10, where the 10 is from \code{min_axis_bins}) +#' @param min_axis_bins number of bins to create per range defined by minor axis. +#' (default value is 10) #' @param alpha alpha of hexbin plot #' @param show_polygon overlay polygon information (cell shape) #' @param polygon_feat_type feature type associated with polygon information @@ -746,6 +751,7 @@ spatInSituPlotHex = function(gobject, sdimx = 'x', sdimy = 'y', binwidth = NULL, + min_axis_bins = 10, alpha = 0.5, show_polygon = TRUE, polygon_feat_type = 'cell', From 0fbc2953682a5126fe5c6889ce639a145671697b Mon Sep 17 00:00:00 2001 From: jiajic <72078254+jiajic@users.noreply.github.com> Date: Sun, 27 Nov 2022 22:03:04 -0500 Subject: [PATCH 32/45] Update spatial_in_situ_visuals.R --- R/spatial_in_situ_visuals.R | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/R/spatial_in_situ_visuals.R b/R/spatial_in_situ_visuals.R index 92f80eb03..13f4a7207 100644 --- a/R/spatial_in_situ_visuals.R +++ b/R/spatial_in_situ_visuals.R @@ -684,7 +684,8 @@ spatInSituPlotHex_single = function(gobject, sel_feat = feat, sdimx = sdimx, sdimy = sdimy, - binwidth = binwidth) + binwidth = binwidth, + min_axis_bins = min_axis_bins) ## adjust theme settings @@ -796,6 +797,7 @@ spatInSituPlotHex = function(gobject, sdimx = sdimx, sdimy = sdimy, binwidth = binwidth, + min_axis_bins = min_axis_bins, alpha = alpha, show_polygon = show_polygon, polygon_feat_type = polygon_feat_type, From 45cdfebfed70b0864be46ad50b086b94e63b11a3 Mon Sep 17 00:00:00 2001 From: jiajic <72078254+jiajic@users.noreply.github.com> Date: Tue, 29 Nov 2022 16:51:04 -0500 Subject: [PATCH 33/45] document --- R/clustering.R | 38 ++++++++++++++++---------------- man/plot_feature_hexbin_layer.Rd | 3 ++- man/spatInSituPlotDensity.Rd | 2 +- man/spatInSituPlotHex.Rd | 11 ++++++--- man/spatInSituPlotHex_single.Rd | 3 ++- 5 files changed, 32 insertions(+), 25 deletions(-) diff --git a/R/clustering.R b/R/clustering.R index 7684bdec0..b43a6ae09 100644 --- a/R/clustering.R +++ b/R/clustering.R @@ -238,7 +238,7 @@ doLouvainCluster_community <- function(gobject, } # prepare python path and louvain script - reticulate::use_python(required = T, python = python_path) + reticulate::use_python(required = TRUE, python = python_path) python_louvain_function = system.file("python", "python_louvain.py", package = 'Giotto') reticulate::source_python(file = python_louvain_function) @@ -292,8 +292,8 @@ doLouvainCluster_community <- function(gobject, gobject@cell_metadata = cell_metadata } - gobject = addCellMetadata(gobject = gobject, new_metadata = ident_clusters_DT[, c('cell_ID', name), with = F], - by_column = T, column_cell_ID = 'cell_ID') + gobject = addCellMetadata(gobject = gobject, new_metadata = ident_clusters_DT[, c('cell_ID', name), with = FALSE], + by_column = TRUE, column_cell_ID = 'cell_ID') ## update parameters used ## @@ -584,8 +584,8 @@ doRandomWalkCluster <- function(gobject, gobject@cell_metadata = cell_metadata } - gobject = addCellMetadata(gobject = gobject, new_metadata = ident_clusters_DT[, c('cell_ID', name), with = F], - by_column = T, column_cell_ID = 'cell_ID') + gobject = addCellMetadata(gobject = gobject, new_metadata = ident_clusters_DT[, c('cell_ID', name), with = FALSE], + by_column = TRUE, column_cell_ID = 'cell_ID') ## update parameters used ## @@ -688,8 +688,8 @@ doSNNCluster <- function(gobject, gobject@cell_metadata = cell_metadata } - gobject = addCellMetadata(gobject = gobject, new_metadata = ident_clusters_DT[, c('cell_ID', name), with = F], - by_column = T, column_cell_ID = 'cell_ID') + gobject = addCellMetadata(gobject = gobject, new_metadata = ident_clusters_DT[, c('cell_ID', name), with = FALSE], + by_column = TRUE, column_cell_ID = 'cell_ID') ## update parameters used ## @@ -1354,7 +1354,7 @@ doLeidenSubCluster = function(gobject, name = 'sub_pleiden_clus', cluster_column = NULL, selected_clusters = NULL, - hvf_param = list(reverse_log_scale = T, difference_in_cov = 1, expression_values = 'normalized'), + hvf_param = list(reverse_log_scale = TRUE, difference_in_cov = 1, expression_values = 'normalized'), hvg_param = NULL, hvf_min_perc_cells = 5, hvg_min_perc_cells = NULL, @@ -1364,7 +1364,7 @@ doLeidenSubCluster = function(gobject, use_all_genes_as_hvg = NULL, min_nr_of_hvf = 5, min_nr_of_hvg = NULL, - pca_param = list(expression_values = 'normalized', scale_unit = T), + pca_param = list(expression_values = 'normalized', scale_unit = TRUE), nn_param = list(dimensions_to_use = 1:20), k_neighbors = 10, resolution = 0.5, @@ -1373,7 +1373,7 @@ doLeidenSubCluster = function(gobject, nn_network_to_use = 'sNN', network_name = 'sNN.pca', return_gobject = TRUE, - verbose = T) { + verbose = TRUE) { # specify feat_type @@ -1559,7 +1559,7 @@ doLouvainSubCluster_community = function(gobject, name = 'sub_louvain_comm_clus', cluster_column = NULL, selected_clusters = NULL, - hvg_param = list(reverse_log_scale = T, difference_in_cov = 1, expression_values = 'normalized'), + hvg_param = list(reverse_log_scale = TRUE, difference_in_cov = 1, expression_values = 'normalized'), hvg_min_perc_cells = 5, hvg_mean_expr_det = 1, use_all_genes_as_hvg = FALSE, @@ -1678,8 +1678,8 @@ doLouvainSubCluster_community = function(gobject, gobject@cell_metadata = cell_metadata } - gobject <- addCellMetadata(gobject, new_metadata = together[, c('cell_ID', name), with = F], - by_column = T, column_cell_ID = 'cell_ID') + gobject <- addCellMetadata(gobject, new_metadata = together[, c('cell_ID', name), with = FALSE], + by_column = TRUE, column_cell_ID = 'cell_ID') ## update parameters used ## parameters_list = gobject@parameters @@ -1741,12 +1741,12 @@ doLouvainSubCluster_multinet = function(gobject, name = 'sub_louvain_mult_clus', cluster_column = NULL, selected_clusters = NULL, - hvg_param = list(reverse_log_scale = T, difference_in_cov = 1, expression_values = 'normalized'), + hvg_param = list(reverse_log_scale = TRUE, difference_in_cov = 1, expression_values = 'normalized'), hvg_min_perc_cells = 5, hvg_mean_expr_det = 1, use_all_genes_as_hvg = FALSE, min_nr_of_hvg = 5, - pca_param = list(expression_values = 'normalized', scale_unit = T), + pca_param = list(expression_values = 'normalized', scale_unit = TRUE), nn_param = list(dimensions_to_use = 1:20), k_neighbors = 10, gamma = 1, @@ -1754,7 +1754,7 @@ doLouvainSubCluster_multinet = function(gobject, nn_network_to_use = 'sNN', network_name = 'sNN.pca', return_gobject = TRUE, - verbose = T) { + verbose = TRUE) { if("multinet" %in% rownames(installed.packages()) == FALSE) { @@ -1929,12 +1929,12 @@ doLouvainSubCluster = function(gobject, version = c('community', 'multinet'), cluster_column = NULL, selected_clusters = NULL, - hvg_param = list(reverse_log_scale = T, difference_in_cov = 1, expression_values = 'normalized'), + hvg_param = list(reverse_log_scale = TRUE, difference_in_cov = 1, expression_values = 'normalized'), hvg_min_perc_cells = 5, hvg_mean_expr_det = 1, use_all_genes_as_hvg = FALSE, min_nr_of_hvg = 5, - pca_param = list(expression_values = 'normalized', scale_unit = T), + pca_param = list(expression_values = 'normalized', scale_unit = TRUE), nn_param = list(dimensions_to_use = 1:20), k_neighbors = 10, resolution = 0.5, @@ -1944,7 +1944,7 @@ doLouvainSubCluster = function(gobject, nn_network_to_use = 'sNN', network_name = 'sNN.pca', return_gobject = TRUE, - verbose = T) { + verbose = TRUE) { ## louvain clustering version to use version = match.arg(version, c('community', 'multinet')) diff --git a/man/plot_feature_hexbin_layer.Rd b/man/plot_feature_hexbin_layer.Rd index 2fc9f2918..b52ca34d2 100644 --- a/man/plot_feature_hexbin_layer.Rd +++ b/man/plot_feature_hexbin_layer.Rd @@ -10,7 +10,8 @@ plot_feature_hexbin_layer( sel_feat, sdimx = "x", sdimy = "y", - bins = 10, + binwidth = NULL, + min_axis_bins = 10L, alpha = 0.5 ) } diff --git a/man/spatInSituPlotDensity.Rd b/man/spatInSituPlotDensity.Rd index 53d3455c6..19523cb46 100644 --- a/man/spatInSituPlotDensity.Rd +++ b/man/spatInSituPlotDensity.Rd @@ -18,7 +18,7 @@ spatInSituPlotDensity( polygon_fill_as_factor = NULL, polygon_alpha = 0.5, polygon_size = 0.5, - coord_fix_ratio = NULL, + coord_fix_ratio = 1, axis_text = 8, axis_title = 8, legend_text = 6, diff --git a/man/spatInSituPlotHex.Rd b/man/spatInSituPlotHex.Rd index a10890422..9e91cca6b 100644 --- a/man/spatInSituPlotHex.Rd +++ b/man/spatInSituPlotHex.Rd @@ -10,7 +10,8 @@ spatInSituPlotHex( feat_type = "rna", sdimx = "x", sdimy = "y", - bins = 10, + binwidth = NULL, + min_axis_bins = 10, alpha = 0.5, show_polygon = TRUE, polygon_feat_type = "cell", @@ -19,7 +20,7 @@ spatInSituPlotHex( polygon_fill_as_factor = NULL, polygon_alpha = 0.5, polygon_size = 0.5, - coord_fix_ratio = NULL, + coord_fix_ratio = 1, axis_text = 8, axis_title = 8, legend_text = 6, @@ -46,7 +47,11 @@ spatInSituPlotHex( \item{sdimy}{spatial dimension y} -\item{bins}{number of hexbins in one direction} +\item{binwidth}{numeric vector for x and y width of bins (default is minor axis +range/10, where the 10 is from \code{min_axis_bins})} + +\item{min_axis_bins}{number of bins to create per range defined by minor axis. +(default value is 10)} \item{alpha}{alpha of hexbin plot} diff --git a/man/spatInSituPlotHex_single.Rd b/man/spatInSituPlotHex_single.Rd index 27350d527..5e9e39863 100644 --- a/man/spatInSituPlotHex_single.Rd +++ b/man/spatInSituPlotHex_single.Rd @@ -10,7 +10,8 @@ spatInSituPlotHex_single( feat_type = "rna", sdimx = "x", sdimy = "y", - bins = 10, + binwidth = NULL, + min_axis_bins = NULL, alpha = 0.5, show_polygon = TRUE, polygon_feat_type = "cell", From c69d5ebc425c8ea29d37d7cb4d8fa4d4086cccf7 Mon Sep 17 00:00:00 2001 From: jiajic <72078254+jiajic@users.noreply.github.com> Date: Tue, 29 Nov 2022 17:34:24 -0500 Subject: [PATCH 34/45] update wrap_msg to use sep param --- R/dimension_reduction.R | 12 ++++++++---- R/utilities.R | 14 +++++++------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/R/dimension_reduction.R b/R/dimension_reduction.R index f160d7527..ed45ca093 100644 --- a/R/dimension_reduction.R +++ b/R/dimension_reduction.R @@ -425,18 +425,22 @@ create_feats_to_use_matrix = function(gobject, # for hvf genes if(is.character(feats_to_use) & length(feats_to_use) == 1) { if(feats_to_use %in% colnames(feat_metadata)) { - if(verbose == TRUE) cat(feats_to_use, ' was found in the feats metadata information and will be used to select highly variable features \n') + if(verbose == TRUE) { + wrap_msg('"', feats_to_use, '" was found in the feats metadata information and will be used to select highly variable features', + sep = '') + } feats_to_use = feat_metadata[get(feats_to_use) == 'yes'][['feat_ID']] sel_matrix = sel_matrix[rownames(sel_matrix) %in% feats_to_use, ] } else { - if(verbose == TRUE) cat(feats_to_use, ' was not found in the gene metadata information, all genes will be used \n') + if(verbose == TRUE) wrap_msg('"', feats_to_use, '" was not found in the gene metadata information, all genes will be used', + sep = '') } } else { - if(verbose == TRUE) cat('a custom vector of genes will be used to subset the matrix \n') + if(verbose == TRUE) wrap_msg('a custom vector of genes will be used to subset the matrix') sel_matrix = sel_matrix[rownames(sel_matrix) %in% feats_to_use, ] } - if(verbose == TRUE) cat('class of selected matrix: ', class(sel_matrix)) + if(verbose == TRUE) cat('class of selected matrix: ', class(sel_matrix), '\n') return(sel_matrix) } diff --git a/R/utilities.R b/R/utilities.R index 52d682af9..f75239f48 100644 --- a/R/utilities.R +++ b/R/utilities.R @@ -510,23 +510,23 @@ abb_spatlocs = function(spatLocsObj, nrows) { #' @title Wrap message #' @name wrap_msg #' @param ... additional strings and/or elements to pass to cat -#' @param collapse how to join elements of string (default is no space) +#' @param sep how to join elements of string (default is one space) #' @param strWidth externally set wrapping width. (default value of 100 is not effected) #' @param keywords internal -wrap_msg = function(..., collapse = '') { - message(wrap_txt(..., collapse = '')) +wrap_msg = function(..., sep = ' ') { + message(wrap_txt(..., sep = sep)) } #' @title Wrap text #' @name wrap_txt #' @param ... additional params to pass -#' @param collapse how to join elements of string (default is no space) +#' @param sep how to join elements of string (default is one space) #' @param strWidth externally set wrapping width. (default value of 100 is not effected) #' @keywords internal -wrap_txt = function(..., collapse = '', strWidth = 100) { - cat(...) %>% +wrap_txt = function(..., sep = ' ', strWidth = 100) { + cat(..., sep = sep) %>% capture.output() %>% - strwrap(., prefix = ' ', initial = '', + strwrap(., prefix = ' ', initial = '', # indent later lines, no indent first line width = min(80, getOption("width"), strWidth)) %>% paste(., collapse = '\n') } From 05b3eaf9efb4d7f36f27ca462a946be37baab812 Mon Sep 17 00:00:00 2001 From: jiajic <72078254+jiajic@users.noreply.github.com> Date: Wed, 30 Nov 2022 10:17:32 -0500 Subject: [PATCH 35/45] Update grid plotting behavior Added a function to set a cow_n_col value dependent on how many plots there are to combine. --- R/spatial_in_situ_visuals.R | 10 ++++--- R/spatial_visuals.R | 59 +++++++++++++++++++++++++++---------- 2 files changed, 49 insertions(+), 20 deletions(-) diff --git a/R/spatial_in_situ_visuals.R b/R/spatial_in_situ_visuals.R index 13f4a7207..94a2e1b2c 100644 --- a/R/spatial_in_situ_visuals.R +++ b/R/spatial_in_situ_visuals.R @@ -766,7 +766,7 @@ spatInSituPlotHex = function(gobject, axis_title = 8, legend_text = 6, background_color = 'white', - cow_n_col = 2, + cow_n_col = NULL, cow_rel_h = 1, cow_rel_w = 1, cow_align = 'h', @@ -821,7 +821,8 @@ spatInSituPlotHex = function(gobject, } else { # combine plots with cowplot combo_plot <- cowplot::plot_grid(plotlist = savelist, - ncol = cow_n_col, + ncol = set_default_cow_n_col(cow_n_col = cow_n_col, + nr_plots = length(savelist)), rel_heights = cow_rel_h, rel_widths = cow_rel_w, align = cow_align) @@ -1044,7 +1045,7 @@ spatInSituPlotDensity = function(gobject, axis_title = 8, legend_text = 6, background_color = 'black', - cow_n_col = 2, + cow_n_col = NULL, cow_rel_h = 1, cow_rel_w = 1, cow_align = 'h', @@ -1100,7 +1101,8 @@ spatInSituPlotDensity = function(gobject, } else { # combine plots with cowplot combo_plot <- cowplot::plot_grid(plotlist = savelist, - ncol = cow_n_col, + ncol = set_default_cow_n_col(cow_n_col = cow_n_col, + nr_plots = length(savelist)), rel_heights = cow_rel_h, rel_widths = cow_rel_w, align = cow_align) diff --git a/R/spatial_visuals.R b/R/spatial_visuals.R index 47855f796..10eb94b18 100644 --- a/R/spatial_visuals.R +++ b/R/spatial_visuals.R @@ -9,6 +9,26 @@ ## 2-D ggplots #### ## ----------- ## + + + +## ** set default plot n cols #### +#' @title Set default ncols in plotting grid +#' @name set_default_cow_n_col +#' @keywords internal +set_default_cow_n_col = function(cow_n_col = NULL, + nr_plots) { + + if(is.null(cow_n_col)) { + cow_n_col = ceiling(sqrt(nr_plots)) + } else { + cow_n_col + } + return(cow_n_col) +} + + + ## ** image object compatibility #### #' @title Optimized largeImage resampling @@ -1187,7 +1207,7 @@ dimPlot2D = function(gobject, background_color = 'white', axis_text = 8, axis_title = 8, - cow_n_col = 2, + cow_n_col = NULL, cow_rel_h = 1, cow_rel_w = 1, cow_align = 'h', @@ -1372,7 +1392,8 @@ dimPlot2D = function(gobject, # combine plots with cowplot combo_plot <- cowplot::plot_grid(plotlist = savelist, - ncol = cow_n_col, + ncol = set_default_cow_n_col(cow_n_col = cow_n_col, + nr_plots = length(savelist)), rel_heights = cow_rel_h, rel_widths = cow_rel_w, align = cow_align) @@ -3253,7 +3274,7 @@ spatPlot2D = function(gobject, vor_alpha = 1, axis_text = 8, axis_title = 8, - cow_n_col = 2, + cow_n_col = NULL, cow_rel_h = 1, cow_rel_w = 1, cow_align = 'h', @@ -3469,7 +3490,8 @@ spatPlot2D = function(gobject, # combine plots with cowplot combo_plot <- cowplot::plot_grid(plotlist = savelist, - ncol = cow_n_col, + ncol = set_default_cow_n_col(cow_n_col = cow_n_col, + nr_plots = length(savelist)), rel_heights = cow_rel_h, rel_widths = cow_rel_w, align = cow_align) @@ -4193,7 +4215,7 @@ spatFeatPlot2D_single <- function(gobject, vor_max_radius = 200, axis_text = 8, axis_title = 8, - cow_n_col = 2, + cow_n_col = NULL, cow_rel_h = 1, cow_rel_w = 1, cow_align = 'h', @@ -4559,7 +4581,8 @@ spatFeatPlot2D_single <- function(gobject, # combine plots with cowplot combo_plot <- cowplot::plot_grid(plotlist = savelist, - ncol = cow_n_col, + ncol = set_default_cow_n_col(cow_n_col = cow_n_col, + nr_plots = length(savelist)), rel_heights = cow_rel_h, rel_widths = cow_rel_w, align = cow_align) @@ -4677,7 +4700,7 @@ spatFeatPlot2D <- function(gobject, vor_max_radius = 200, axis_text = 8, axis_title = 8, - cow_n_col = 2, + cow_n_col = NULL, cow_rel_h = 1, cow_rel_w = 1, cow_align = 'h', @@ -4851,7 +4874,8 @@ spatFeatPlot2D <- function(gobject, # combine plots with cowplot combo_plot <- cowplot::plot_grid(plotlist = savelist, - ncol = cow_n_col, + ncol = set_default_cow_n_col(cow_n_col = cow_n_col, + nr_plots = length(savelist)), rel_heights = cow_rel_h, rel_widths = cow_rel_w, align = cow_align) @@ -5001,7 +5025,7 @@ dimFeatPlot2D <- function(gobject, background_color = 'white', axis_text = 8, axis_title = 8, - cow_n_col = 2, + cow_n_col = NULL, cow_rel_h = 1, cow_rel_w = 1, cow_align = 'h', @@ -5265,7 +5289,8 @@ dimFeatPlot2D <- function(gobject, # combine plots with cowplot combo_plot <- cowplot::plot_grid(plotlist = savelist, - ncol = cow_n_col, + ncol = set_default_cow_n_col(cow_n_col = cow_n_col, + nr_plots = length(savelist)), rel_heights = cow_rel_h, rel_widths = cow_rel_w, align = cow_align) @@ -5446,7 +5471,7 @@ spatDimFeatPlot2D <- function(gobject, cell_color_gradient = c('blue', 'white', 'red'), gradient_midpoint = NULL, gradient_limits = NULL, - cow_n_col = 2, + cow_n_col = NULL, cow_rel_h = 1, cow_rel_w = 1, cow_align = 'h', @@ -5753,7 +5778,7 @@ spatCellPlot2D = function(gobject, vor_alpha = 1, axis_text = 8, axis_title = 8, - cow_n_col = 2, + cow_n_col = NULL, cow_rel_h = 1, cow_rel_w = 1, cow_align = 'h', @@ -5861,7 +5886,8 @@ spatCellPlot2D = function(gobject, # combine plots with cowplot combo_plot <- cowplot::plot_grid(plotlist = savelist, - ncol = cow_n_col, + ncol = set_default_cow_n_col(cow_n_col = cow_n_col, + nr_plots = length(savelist)), rel_heights = cow_rel_h, rel_widths = cow_rel_w, align = cow_align) @@ -5999,7 +6025,7 @@ dimCellPlot2D = function(gobject, background_color = 'white', axis_text = 8, axis_title = 8, - cow_n_col = 2, + cow_n_col = NULL, cow_rel_h = 1, cow_rel_w = 1, cow_align = 'h', @@ -6097,7 +6123,8 @@ dimCellPlot2D = function(gobject, # combine plots with cowplot combo_plot <- cowplot::plot_grid(plotlist = savelist, - ncol = cow_n_col, + ncol = set_default_cow_n_col(cow_n_col = cow_n_col, + nr_plots = length(savelist)), rel_heights = cow_rel_h, rel_widths = cow_rel_w, align = cow_align) @@ -6301,7 +6328,7 @@ spatDimCellPlot2D <- function(gobject, axis_text = 8, axis_title = 8, coord_fix_ratio = 1, - cow_n_col = 2, + cow_n_col = NULL, cow_rel_h = 1, cow_rel_w = 1, cow_align = 'h', From eb6db713807128c3f51a4c81a1a505bf516fd7f6 Mon Sep 17 00:00:00 2001 From: jiajic <72078254+jiajic@users.noreply.github.com> Date: Thu, 1 Dec 2022 01:06:13 -0500 Subject: [PATCH 36/45] Update DESCRIPTION Increment version number --- DESCRIPTION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 52400b424..8ffd3f0ac 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: Giotto Title: Spatial Single-Cell Transcriptomics Toolbox -Version: 3.0.1 +Version: 3.1 Authors@R: c( person("Ruben", "Dries", email = "rubendries@gmail.com", role = c("aut", "cre")), From 856f11ed71cae93f7fc870ccef5909e97e6c64de Mon Sep 17 00:00:00 2001 From: jiajic <72078254+jiajic@users.noreply.github.com> Date: Thu, 1 Dec 2022 02:19:37 -0500 Subject: [PATCH 37/45] Create NEWS.md --- NEWS.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 NEWS.md diff --git a/NEWS.md b/NEWS.md new file mode 100644 index 000000000..1239178cc --- /dev/null +++ b/NEWS.md @@ -0,0 +1,30 @@ +# Giotto Suite 3.1.0 (2202-12-01) + +## Changes +- Update `createGiottoCosMxObject()` for 3.0 and modularization of functions. 'subcellular' workflow has been tested to work along with an updated tutorial. +- Update grid plotting behavior to set a default number columns to use based on number of elements to plot. Can be overridden by explicitly providing input to `cow_n_col` param +- Add `initialize()` generic that calls `setalloccol()` for data.table-based S4 subobjects to allow setting by reference +- Add `spatUnit`, `spatUnit<-`, `featType`, and `featType<-` feat type generics for S4 subobjects for setting the relevant slots +- Fix bug in `annotateGiotto()` after 3.0 update ([#455](https://github.com/drieslab/Giotto/issues/433#issuecomment-1324211224)) +- Update seed setting behavior + + +# Giotto Suite 3.0.1 (2022-11-20) + +## Changes +- Fix bug in `extract_polygon_list()` ([#455](https://github.com/drieslab/Giotto/issues/433#issuecomment-1321221382)) +- Update Unicode character printing with `show` functions for Latin1 systems +- Add system color support detection (based on crayon package logic) +- Add ability to turn off colored text in `show` functions with `options("giotto.color_show" = FALSE)` + +# Giotto Suite 3.0.0 (2022-11-18) + +## Changes +- New S4 subobjects. Details can be found in [classes.R](https://github.com/drieslab/Giotto/blob/suite_dev/R/classes.R) +- New basic generics for S4 subobjects. Mainly the use of `[]` and `[]<-` to get or set information into the main data slot +- Update `setter` functions to read the `@spat_unit` and `@feat_type` slots of subobjects to determine nesting +- New `@provenance` slot in S4 subobjects to track provenance of aggregated information (z_layers used for example) +- Update of `show` functions to display color coded nesting names and tree structure +- Update of python environment to **3.10** +- Update `anndataToGiotto()` + From 23f1e1a606681719c12966dbb280502449e0876a Mon Sep 17 00:00:00 2001 From: jiajic <72078254+jiajic@users.noreply.github.com> Date: Thu, 1 Dec 2022 02:33:17 -0500 Subject: [PATCH 38/45] Update NEWS.md --- NEWS.md | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/NEWS.md b/NEWS.md index 1239178cc..829a83d52 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,30 +1,38 @@ # Giotto Suite 3.1.0 (2202-12-01) + +## Added +- Add `initialize()` generic that calls `setalloccol()` for data.table-based S4 subobjects to allow setting by reference +- Add `spatUnit`, `spatUnit<-`, `featType`, and `featType<-` feat type generics for S4 subobjects for setting the relevant slots + ## Changes - Update `createGiottoCosMxObject()` for 3.0 and modularization of functions. 'subcellular' workflow has been tested to work along with an updated tutorial. - Update grid plotting behavior to set a default number columns to use based on number of elements to plot. Can be overridden by explicitly providing input to `cow_n_col` param -- Add `initialize()` generic that calls `setalloccol()` for data.table-based S4 subobjects to allow setting by reference -- Add `spatUnit`, `spatUnit<-`, `featType`, and `featType<-` feat type generics for S4 subobjects for setting the relevant slots - Fix bug in `annotateGiotto()` after 3.0 update ([#455](https://github.com/drieslab/Giotto/issues/433#issuecomment-1324211224)) - Update seed setting behavior # Giotto Suite 3.0.1 (2022-11-20) +## Added +- Add system color support detection (based on crayon package logic) +- Add ability to turn off colored text in `show` functions with `options("giotto.color_show" = FALSE)` + ## Changes - Fix bug in `extract_polygon_list()` ([#455](https://github.com/drieslab/Giotto/issues/433#issuecomment-1321221382)) - Update Unicode character printing with `show` functions for Latin1 systems -- Add system color support detection (based on crayon package logic) -- Add ability to turn off colored text in `show` functions with `options("giotto.color_show" = FALSE)` + # Giotto Suite 3.0.0 (2022-11-18) -## Changes +## Added - New S4 subobjects. Details can be found in [classes.R](https://github.com/drieslab/Giotto/blob/suite_dev/R/classes.R) - New basic generics for S4 subobjects. Mainly the use of `[]` and `[]<-` to get or set information into the main data slot -- Update `setter` functions to read the `@spat_unit` and `@feat_type` slots of subobjects to determine nesting - New `@provenance` slot in S4 subobjects to track provenance of aggregated information (z_layers used for example) -- Update of `show` functions to display color coded nesting names and tree structure + + +## Changes - Update of python environment to **3.10** - Update `anndataToGiotto()` - +- Update `setter` functions to read the `@spat_unit` and `@feat_type` slots of subobjects to determine nesting +- Update of `show` functions to display color coded nesting names and tree structure From 877032ab2d21c15eeb19749426d228c9a56a5c65 Mon Sep 17 00:00:00 2001 From: jiajic <72078254+jiajic@users.noreply.github.com> Date: Thu, 1 Dec 2022 02:34:16 -0500 Subject: [PATCH 39/45] Update NEWS.md fix typo --- NEWS.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/NEWS.md b/NEWS.md index 829a83d52..ded1b7b83 100644 --- a/NEWS.md +++ b/NEWS.md @@ -8,7 +8,7 @@ ## Changes - Update `createGiottoCosMxObject()` for 3.0 and modularization of functions. 'subcellular' workflow has been tested to work along with an updated tutorial. - Update grid plotting behavior to set a default number columns to use based on number of elements to plot. Can be overridden by explicitly providing input to `cow_n_col` param -- Fix bug in `annotateGiotto()` after 3.0 update ([#455](https://github.com/drieslab/Giotto/issues/433#issuecomment-1324211224)) +- Fix bug in `annotateGiotto()` after 3.0 update ([#433](https://github.com/drieslab/Giotto/issues/433#issuecomment-1324211224)) - Update seed setting behavior @@ -19,7 +19,7 @@ - Add ability to turn off colored text in `show` functions with `options("giotto.color_show" = FALSE)` ## Changes -- Fix bug in `extract_polygon_list()` ([#455](https://github.com/drieslab/Giotto/issues/433#issuecomment-1321221382)) +- Fix bug in `extract_polygon_list()` ([#433](https://github.com/drieslab/Giotto/issues/433#issuecomment-1321221382)) - Update Unicode character printing with `show` functions for Latin1 systems From f241b3ef52e57a3a7830c73252dd3ff453c2c3d9 Mon Sep 17 00:00:00 2001 From: jiajic <72078254+jiajic@users.noreply.github.com> Date: Thu, 1 Dec 2022 02:39:57 -0500 Subject: [PATCH 40/45] Update readme and fix links for suite --- NEWS.md | 2 +- README.Rmd | 1 + README.md | 23 ++++++++++++----------- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/NEWS.md b/NEWS.md index ded1b7b83..fe70b9fa3 100644 --- a/NEWS.md +++ b/NEWS.md @@ -26,7 +26,7 @@ # Giotto Suite 3.0.0 (2022-11-18) ## Added -- New S4 subobjects. Details can be found in [classes.R](https://github.com/drieslab/Giotto/blob/suite_dev/R/classes.R) +- New S4 subobjects. Details can be found in [classes.R](https://github.com/drieslab/Giotto/blob/suite/R/classes.R) - New basic generics for S4 subobjects. Mainly the use of `[]` and `[]<-` to get or set information into the main data slot - New `@provenance` slot in S4 subobjects to track provenance of aggregated information (z_layers used for example) diff --git a/README.Rmd b/README.Rmd index 978edc7d3..da2ee802c 100644 --- a/README.Rmd +++ b/README.Rmd @@ -26,6 +26,7 @@ We have created a new [readthedocs website](https://giottosuite.readthedocs.io/e Giotto Suite is a major upgrade to the Giotto package that provides tools to process, analyze and visualize **spatial multi-omics data at all scales and multiple resolutions**. The underlying framework is generalizable to virtually all current and emerging spatial technologies. Our Giotto Suite prototype pipeline is generally applicable on various different datasets, such as those created by state-of-the-art spatial technologies, including *in situ* hybridization (seqFISH+, merFISH, osmFISH, CosMx), sequencing (Slide-seq, Visium, STARmap, Seq-Scope, Stereo-Seq) and imaging-based multiplexing/proteomics (CyCIF, MIBI, CODEX). These technologies differ in terms of resolution (subcellular, single cell or multiple cells), spatial dimension (2D vs 3D), molecular modality (protein, RNA, DNA, ...), and throughput (number of cells and analytes). The package is in heavy development. Please check back often! +For a version history/changelog, please see the [NEWS file](https://github.com/drieslab/Giotto/blob/suite/NEWS.md). diff --git a/README.md b/README.md index b81419132..7851b6721 100644 --- a/README.md +++ b/README.md @@ -50,19 +50,20 @@ in terms of resolution (subcellular, single cell or multiple cells), spatial dimension (2D vs 3D), molecular modality (protein, RNA, DNA, …), and throughput (number of cells and analytes). -The package is in heavy development. Please check back often! +The package is in heavy development. Please check back often! For a +version history/changelog, please see the [NEWS +file](https://github.com/drieslab/Giotto/blob/suite/NEWS.md). ## References -- [Dries, R., Zhu, Q. et al. Giotto: a toolbox for integrative - analysis and visualization of spatial expression data. Genome - Biology - (2021).](https://genomebiology.biomedcentral.com/articles/10.1186/s13059-021-02286-2) -- [Dries, R., Chen, J. et al. Advances in spatial transcriptomic data - analysis. Genome Research - (2021).](https://genome.cshlp.org/content/31/10/1706.long) -- [Del Rossi, N., Chen, J. et al. Analyzing Spatial Transcriptomics - Data Using Giotto. Current Protocols - (2022).](https://currentprotocols.onlinelibrary.wiley.com/doi/abs/10.1002/cpz1.405) +- [Dries, R., Zhu, Q. et al. Giotto: a toolbox for integrative analysis + and visualization of spatial expression data. Genome Biology + (2021).](https://genomebiology.biomedcentral.com/articles/10.1186/s13059-021-02286-2) +- [Dries, R., Chen, J. et al. Advances in spatial transcriptomic data + analysis. Genome Research + (2021).](https://genome.cshlp.org/content/31/10/1706.long) +- [Del Rossi, N., Chen, J. et al. Analyzing Spatial Transcriptomics Data + Using Giotto. Current Protocols + (2022).](https://currentprotocols.onlinelibrary.wiley.com/doi/abs/10.1002/cpz1.405) From 79a467365730d8ed7bbe53a4db6e2fe546f9ec70 Mon Sep 17 00:00:00 2001 From: jiajic <72078254+jiajic@users.noreply.github.com> Date: Thu, 1 Dec 2022 02:41:42 -0500 Subject: [PATCH 41/45] fix typo in readme --- README.Rmd | 2 +- README.md | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.Rmd b/README.Rmd index da2ee802c..97456169b 100644 --- a/README.Rmd +++ b/README.Rmd @@ -25,7 +25,7 @@ We have created a new [readthedocs website](https://giottosuite.readthedocs.io/e Giotto Suite is a major upgrade to the Giotto package that provides tools to process, analyze and visualize **spatial multi-omics data at all scales and multiple resolutions**. The underlying framework is generalizable to virtually all current and emerging spatial technologies. Our Giotto Suite prototype pipeline is generally applicable on various different datasets, such as those created by state-of-the-art spatial technologies, including *in situ* hybridization (seqFISH+, merFISH, osmFISH, CosMx), sequencing (Slide-seq, Visium, STARmap, Seq-Scope, Stereo-Seq) and imaging-based multiplexing/proteomics (CyCIF, MIBI, CODEX). These technologies differ in terms of resolution (subcellular, single cell or multiple cells), spatial dimension (2D vs 3D), molecular modality (protein, RNA, DNA, ...), and throughput (number of cells and analytes). -The package is in heavy development. Please check back often! +The package is in heavy development. Please check back often! For a version history/changelog, please see the [NEWS file](https://github.com/drieslab/Giotto/blob/suite/NEWS.md). diff --git a/README.md b/README.md index 7851b6721..bfc1dd3ac 100644 --- a/README.md +++ b/README.md @@ -50,8 +50,8 @@ in terms of resolution (subcellular, single cell or multiple cells), spatial dimension (2D vs 3D), molecular modality (protein, RNA, DNA, …), and throughput (number of cells and analytes). -The package is in heavy development. Please check back often! For a -version history/changelog, please see the [NEWS +The package is in heavy development. Please check back often! +For a version history/changelog, please see the [NEWS file](https://github.com/drieslab/Giotto/blob/suite/NEWS.md). From 8aed98f46af5695a1881e735c60706c968feeb6c Mon Sep 17 00:00:00 2001 From: jiajic <72078254+jiajic@users.noreply.github.com> Date: Thu, 1 Dec 2022 03:07:30 -0500 Subject: [PATCH 42/45] Update NEWS.md --- NEWS.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/NEWS.md b/NEWS.md index fe70b9fa3..ebbb530ec 100644 --- a/NEWS.md +++ b/NEWS.md @@ -9,6 +9,7 @@ - Update `createGiottoCosMxObject()` for 3.0 and modularization of functions. 'subcellular' workflow has been tested to work along with an updated tutorial. - Update grid plotting behavior to set a default number columns to use based on number of elements to plot. Can be overridden by explicitly providing input to `cow_n_col` param - Fix bug in `annotateGiotto()` after 3.0 update ([#433](https://github.com/drieslab/Giotto/issues/433#issuecomment-1324211224)) +- Fix bug in `joinGiottoObjects()` metadata processing - Update seed setting behavior @@ -26,9 +27,11 @@ # Giotto Suite 3.0.0 (2022-11-18) ## Added +- New `createGiottoXeniumObject()` for loading 10x Xenium data - New S4 subobjects. Details can be found in [classes.R](https://github.com/drieslab/Giotto/blob/suite/R/classes.R) - New basic generics for S4 subobjects. Mainly the use of `[]` and `[]<-` to get or set information into the main data slot - New `@provenance` slot in S4 subobjects to track provenance of aggregated information (z_layers used for example) +- New `saveGiotto()` and `loadGiotto()` for preserving memory-pointer based objects ## Changes From 558cdb26a4ab003540803dd6cd5253ebea2fe01e Mon Sep 17 00:00:00 2001 From: jiajic <72078254+jiajic@users.noreply.github.com> Date: Thu, 1 Dec 2022 13:35:49 -0500 Subject: [PATCH 43/45] Update NEWS.md include information from RTD website --- NEWS.md | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 73 insertions(+), 8 deletions(-) diff --git a/NEWS.md b/NEWS.md index ebbb530ec..aaf3c42b5 100644 --- a/NEWS.md +++ b/NEWS.md @@ -2,21 +2,25 @@ ## Added -- Add `initialize()` generic that calls `setalloccol()` for data.table-based S4 subobjects to allow setting by reference -- Add `spatUnit`, `spatUnit<-`, `featType`, and `featType<-` feat type generics for S4 subobjects for setting the relevant slots +- New `initialize()` generic that calls `setalloccol()` for data.table-based S4 subobjects to allow setting by reference +- New `spatUnit`, `spatUnit<-`, `featType`, and `featType<-` feat type generics for S4 subobjects for setting the relevant slots +- Add `hexVertices()` to polygon shape array generation functionality ## Changes - Update `createGiottoCosMxObject()` for 3.0 and modularization of functions. 'subcellular' workflow has been tested to work along with an updated tutorial. - Update grid plotting behavior to set a default number columns to use based on number of elements to plot. Can be overridden by explicitly providing input to `cow_n_col` param - Fix bug in `annotateGiotto()` after 3.0 update ([#433](https://github.com/drieslab/Giotto/issues/433#issuecomment-1324211224)) - Fix bug in `joinGiottoObjects()` metadata processing -- Update seed setting behavior +- Update seed setting behavior in [dimension_reduction.R](https://github.com/drieslab/Giotto/blob/suite/R/dimension_reduction.R) and [clustering.R](https://github.com/drieslab/Giotto/blob/suite/R/clustering.R) + + + # Giotto Suite 3.0.1 (2022-11-20) ## Added -- Add system color support detection (based on crayon package logic) +- New system color support detection (based on crayon package logic) - Add ability to turn off colored text in `show` functions with `options("giotto.color_show" = FALSE)` ## Changes @@ -24,18 +28,79 @@ - Update Unicode character printing with `show` functions for Latin1 systems + + + # Giotto Suite 3.0.0 (2022-11-18) +## Breaking Changes +- S4 subobjects framework will require giotto objects to be remade + ## Added - New `createGiottoXeniumObject()` for loading 10x Xenium data - New S4 subobjects. Details can be found in [classes.R](https://github.com/drieslab/Giotto/blob/suite/R/classes.R) - New basic generics for S4 subobjects. Mainly the use of `[]` and `[]<-` to get or set information into the main data slot - New `@provenance` slot in S4 subobjects to track provenance of aggregated information (z_layers used for example) -- New `saveGiotto()` and `loadGiotto()` for preserving memory-pointer based objects - +- New `calculateOverlapPolygonImages()` for calculating overlapped intensities from image-based information (e.g. IMC, IF, MIBI, ...) and polygon data (e.g. cell) +- New `overlapImagesToMatrix()` converts intensity-polygon overlap info into an expression matrix (e.g. cell by protein) +- New `aggregateStacks()` set of functions work with multiple subcellular layers when generating aggregated expression matrices ## Changes -- Update of python environment to **3.10** -- Update `anndataToGiotto()` - Update `setter` functions to read the `@spat_unit` and `@feat_type` slots of subobjects to determine nesting - Update of `show` functions to display color coded nesting names and tree structure + + + + + +# Giotto Suite 2.1.0 (2202-11-09) + +## Breaking Changes +- Update of python version to **3.10.2** [details](https://giottosuite.readthedocs.io/en/latest/additionalinformation.html#id1) + +## Added +- New `anndataToGiotto()` to convert scanpy anndata to Giotto [details](https://giottosuite.readthedocs.io/en/latest/additionalinformation.html#id2) + + + + + + +# Giotto Suite 2.0.0.998 + +## Added +- New `GiottoData` package to work with spatial datasets associated with Giotto + - Stores the minidatasets: preprocessed giotto objects that are ready to be used in any function + - Moved: `getSpatialDataset()` and `loadGiottoMini()` functions to this package + +- New `saveGiotto()` and `loadGiotto()` for preserving memory-pointer based objects. In [general_help.R](https://github.com/drieslab/Giotto/blob/suite/R/general_help.R) + + - It saves a Giotto object into a folder using a specific structure. Essentially a wrapper around `saveRDS()` that also works with spatVector and spatRaster pointers. +- New `plotInteractivePolygon()` for plot-interactive polygonal selection of points. +- New polygon shape array creation through `polyStamp()`, `circleVertices`, `rectVertices`. In [giotto_structures.R](https://github.com/drieslab/Giotto/blob/suite/R/giotto_structures.R) +- Add accessor functions `get_CellMetadata` (alias of `pDataDT()`), `set_CellMetadata`, `get_FeatMetadata` (alias of `fDataDT()`), `set_FeatMetadata`. See [accessors.R](https://github.com/drieslab/Giotto/blob/suite/R/accessors.R) +- New `filterDistributions()` to generate histogram plots from expression statistics + +## Changed +- Deprecate `plotInteractionChangedGenes()` ,`plotICG()`, `plotCPG()` in favor of `plotInteractionChangedFeatures()` and `plotICF()` and `plotCPF()` +- Deprecate `plotCellProximityGenes()`, in favor of `plotCellProximityFeatures()` +- Deprecate `plotCombineInteractionChangedGenes()`, `plotCombineICG()`, `plotCombineCPG()` in favor of `plotCombineInteractionChangedFeatures()` and `plotCombineICF()` +- Deprecate `findInteractionChangedGenes()`, `findICG()`, `findCPG()` in favor of `findInteractionChangedFeats()` and `findICF` +- Deprecate `filterInteractionChangedGenes()`, `filterICG()`, `filterCPG()` in favor of `filterInteractionChangedFeats()` and `filterICF()` +- Deprecate `combineInteractionChangedGenes()`, `combineICG()`, `combineCPG()` in favor of `combineInteractionChangedFeats()` and `combineICF()` +- Deprecate `combineCellProximityGenes_per_interaction()` in favor of `combineCellProximityFeatures_per_interaction()` + +## Breaking Changes +- ICF output internal object structure names have changed to use feats instead of genes + + + + + + + + + + + + From 65ba09c9a3b91b31c4c4ca5ca0f5bf3f009bd9cf Mon Sep 17 00:00:00 2001 From: jiajic <72078254+jiajic@users.noreply.github.com> Date: Thu, 1 Dec 2022 13:43:37 -0500 Subject: [PATCH 44/45] Update NEWS.md typos --- NEWS.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/NEWS.md b/NEWS.md index aaf3c42b5..49df311a0 100644 --- a/NEWS.md +++ b/NEWS.md @@ -72,16 +72,14 @@ - New `GiottoData` package to work with spatial datasets associated with Giotto - Stores the minidatasets: preprocessed giotto objects that are ready to be used in any function - Moved: `getSpatialDataset()` and `loadGiottoMini()` functions to this package - - New `saveGiotto()` and `loadGiotto()` for preserving memory-pointer based objects. In [general_help.R](https://github.com/drieslab/Giotto/blob/suite/R/general_help.R) - - It saves a Giotto object into a folder using a specific structure. Essentially a wrapper around `saveRDS()` that also works with spatVector and spatRaster pointers. - New `plotInteractivePolygon()` for plot-interactive polygonal selection of points. - New polygon shape array creation through `polyStamp()`, `circleVertices`, `rectVertices`. In [giotto_structures.R](https://github.com/drieslab/Giotto/blob/suite/R/giotto_structures.R) - Add accessor functions `get_CellMetadata` (alias of `pDataDT()`), `set_CellMetadata`, `get_FeatMetadata` (alias of `fDataDT()`), `set_FeatMetadata`. See [accessors.R](https://github.com/drieslab/Giotto/blob/suite/R/accessors.R) - New `filterDistributions()` to generate histogram plots from expression statistics -## Changed +## Changes - Deprecate `plotInteractionChangedGenes()` ,`plotICG()`, `plotCPG()` in favor of `plotInteractionChangedFeatures()` and `plotICF()` and `plotCPF()` - Deprecate `plotCellProximityGenes()`, in favor of `plotCellProximityFeatures()` - Deprecate `plotCombineInteractionChangedGenes()`, `plotCombineICG()`, `plotCombineCPG()` in favor of `plotCombineInteractionChangedFeatures()` and `plotCombineICF()` From 98584a96063d98fae86bd1e78e74cfea8dfdfd3d Mon Sep 17 00:00:00 2001 From: jiajic <72078254+jiajic@users.noreply.github.com> Date: Thu, 1 Dec 2022 14:36:51 -0500 Subject: [PATCH 45/45] document --- man/dimCellPlot2D.Rd | 2 +- man/dimFeatPlot2D.Rd | 2 +- man/dimPlot2D.Rd | 2 +- man/doLeidenSubCluster.Rd | 6 +++--- man/doLouvainSubCluster.Rd | 6 +++--- man/doLouvainSubCluster_community.Rd | 2 +- man/doLouvainSubCluster_multinet.Rd | 6 +++--- man/set_default_cow_n_col.Rd | 12 ++++++++++++ man/spatCellPlot2D.Rd | 2 +- man/spatDimCellPlot2D.Rd | 2 +- man/spatDimFeatPlot2D.Rd | 2 +- man/spatFeatPlot2D.Rd | 2 +- man/spatFeatPlot2D_single.Rd | 2 +- man/spatInSituPlotDensity.Rd | 2 +- man/spatInSituPlotHex.Rd | 2 +- man/spatPlot2D.Rd | 2 +- man/wrap_msg.Rd | 4 ++-- man/wrap_txt.Rd | 4 ++-- 18 files changed, 37 insertions(+), 25 deletions(-) create mode 100644 man/set_default_cow_n_col.Rd diff --git a/man/dimCellPlot2D.Rd b/man/dimCellPlot2D.Rd index f4b897efc..8126d5496 100644 --- a/man/dimCellPlot2D.Rd +++ b/man/dimCellPlot2D.Rd @@ -45,7 +45,7 @@ dimCellPlot2D( background_color = "white", axis_text = 8, axis_title = 8, - cow_n_col = 2, + cow_n_col = NULL, cow_rel_h = 1, cow_rel_w = 1, cow_align = "h", diff --git a/man/dimFeatPlot2D.Rd b/man/dimFeatPlot2D.Rd index 32d79337b..30d3dfe23 100644 --- a/man/dimFeatPlot2D.Rd +++ b/man/dimFeatPlot2D.Rd @@ -33,7 +33,7 @@ dimFeatPlot2D( background_color = "white", axis_text = 8, axis_title = 8, - cow_n_col = 2, + cow_n_col = NULL, cow_rel_h = 1, cow_rel_w = 1, cow_align = "h", diff --git a/man/dimPlot2D.Rd b/man/dimPlot2D.Rd index 5d2451dcf..2e6687720 100644 --- a/man/dimPlot2D.Rd +++ b/man/dimPlot2D.Rd @@ -49,7 +49,7 @@ dimPlot2D( background_color = "white", axis_text = 8, axis_title = 8, - cow_n_col = 2, + cow_n_col = NULL, cow_rel_h = 1, cow_rel_w = 1, cow_align = "h", diff --git a/man/doLeidenSubCluster.Rd b/man/doLeidenSubCluster.Rd index 40466da93..1d3da2019 100644 --- a/man/doLeidenSubCluster.Rd +++ b/man/doLeidenSubCluster.Rd @@ -10,7 +10,7 @@ doLeidenSubCluster( name = "sub_pleiden_clus", cluster_column = NULL, selected_clusters = NULL, - hvf_param = list(reverse_log_scale = T, difference_in_cov = 1, expression_values = + hvf_param = list(reverse_log_scale = TRUE, difference_in_cov = 1, expression_values = "normalized"), hvg_param = NULL, hvf_min_perc_cells = 5, @@ -21,7 +21,7 @@ doLeidenSubCluster( use_all_genes_as_hvg = NULL, min_nr_of_hvf = 5, min_nr_of_hvg = NULL, - pca_param = list(expression_values = "normalized", scale_unit = T), + pca_param = list(expression_values = "normalized", scale_unit = TRUE), nn_param = list(dimensions_to_use = 1:20), k_neighbors = 10, resolution = 0.5, @@ -30,7 +30,7 @@ doLeidenSubCluster( nn_network_to_use = "sNN", network_name = "sNN.pca", return_gobject = TRUE, - verbose = T + verbose = TRUE ) } \arguments{ diff --git a/man/doLouvainSubCluster.Rd b/man/doLouvainSubCluster.Rd index c0834ea8f..ca7dcbfa0 100644 --- a/man/doLouvainSubCluster.Rd +++ b/man/doLouvainSubCluster.Rd @@ -10,13 +10,13 @@ doLouvainSubCluster( version = c("community", "multinet"), cluster_column = NULL, selected_clusters = NULL, - hvg_param = list(reverse_log_scale = T, difference_in_cov = 1, expression_values = + hvg_param = list(reverse_log_scale = TRUE, difference_in_cov = 1, expression_values = "normalized"), hvg_min_perc_cells = 5, hvg_mean_expr_det = 1, use_all_genes_as_hvg = FALSE, min_nr_of_hvg = 5, - pca_param = list(expression_values = "normalized", scale_unit = T), + pca_param = list(expression_values = "normalized", scale_unit = TRUE), nn_param = list(dimensions_to_use = 1:20), k_neighbors = 10, resolution = 0.5, @@ -26,7 +26,7 @@ doLouvainSubCluster( nn_network_to_use = "sNN", network_name = "sNN.pca", return_gobject = TRUE, - verbose = T + verbose = TRUE ) } \arguments{ diff --git a/man/doLouvainSubCluster_community.Rd b/man/doLouvainSubCluster_community.Rd index 24c418223..b2f9cb684 100644 --- a/man/doLouvainSubCluster_community.Rd +++ b/man/doLouvainSubCluster_community.Rd @@ -9,7 +9,7 @@ doLouvainSubCluster_community( name = "sub_louvain_comm_clus", cluster_column = NULL, selected_clusters = NULL, - hvg_param = list(reverse_log_scale = T, difference_in_cov = 1, expression_values = + hvg_param = list(reverse_log_scale = TRUE, difference_in_cov = 1, expression_values = "normalized"), hvg_min_perc_cells = 5, hvg_mean_expr_det = 1, diff --git a/man/doLouvainSubCluster_multinet.Rd b/man/doLouvainSubCluster_multinet.Rd index 20fbefe3b..147954b77 100644 --- a/man/doLouvainSubCluster_multinet.Rd +++ b/man/doLouvainSubCluster_multinet.Rd @@ -9,13 +9,13 @@ doLouvainSubCluster_multinet( name = "sub_louvain_mult_clus", cluster_column = NULL, selected_clusters = NULL, - hvg_param = list(reverse_log_scale = T, difference_in_cov = 1, expression_values = + hvg_param = list(reverse_log_scale = TRUE, difference_in_cov = 1, expression_values = "normalized"), hvg_min_perc_cells = 5, hvg_mean_expr_det = 1, use_all_genes_as_hvg = FALSE, min_nr_of_hvg = 5, - pca_param = list(expression_values = "normalized", scale_unit = T), + pca_param = list(expression_values = "normalized", scale_unit = TRUE), nn_param = list(dimensions_to_use = 1:20), k_neighbors = 10, gamma = 1, @@ -23,7 +23,7 @@ doLouvainSubCluster_multinet( nn_network_to_use = "sNN", network_name = "sNN.pca", return_gobject = TRUE, - verbose = T + verbose = TRUE ) } \arguments{ diff --git a/man/set_default_cow_n_col.Rd b/man/set_default_cow_n_col.Rd new file mode 100644 index 000000000..95d91bcbf --- /dev/null +++ b/man/set_default_cow_n_col.Rd @@ -0,0 +1,12 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/spatial_visuals.R +\name{set_default_cow_n_col} +\alias{set_default_cow_n_col} +\title{Set default ncols in plotting grid} +\usage{ +set_default_cow_n_col(cow_n_col = NULL, nr_plots) +} +\description{ +Set default ncols in plotting grid +} +\keyword{internal} diff --git a/man/spatCellPlot2D.Rd b/man/spatCellPlot2D.Rd index 9ed198f4e..f87faa288 100644 --- a/man/spatCellPlot2D.Rd +++ b/man/spatCellPlot2D.Rd @@ -54,7 +54,7 @@ spatCellPlot2D( vor_alpha = 1, axis_text = 8, axis_title = 8, - cow_n_col = 2, + cow_n_col = NULL, cow_rel_h = 1, cow_rel_w = 1, cow_align = "h", diff --git a/man/spatDimCellPlot2D.Rd b/man/spatDimCellPlot2D.Rd index 5d7035361..b7426b3f0 100644 --- a/man/spatDimCellPlot2D.Rd +++ b/man/spatDimCellPlot2D.Rd @@ -77,7 +77,7 @@ spatDimCellPlot2D( axis_text = 8, axis_title = 8, coord_fix_ratio = 1, - cow_n_col = 2, + cow_n_col = NULL, cow_rel_h = 1, cow_rel_w = 1, cow_align = "h", diff --git a/man/spatDimFeatPlot2D.Rd b/man/spatDimFeatPlot2D.Rd index 2d1569c96..85ec39e22 100644 --- a/man/spatDimFeatPlot2D.Rd +++ b/man/spatDimFeatPlot2D.Rd @@ -47,7 +47,7 @@ spatDimFeatPlot2D( cell_color_gradient = c("blue", "white", "red"), gradient_midpoint = NULL, gradient_limits = NULL, - cow_n_col = 2, + cow_n_col = NULL, cow_rel_h = 1, cow_rel_w = 1, cow_align = "h", diff --git a/man/spatFeatPlot2D.Rd b/man/spatFeatPlot2D.Rd index 58c8d3a20..521f9a7c4 100644 --- a/man/spatFeatPlot2D.Rd +++ b/man/spatFeatPlot2D.Rd @@ -44,7 +44,7 @@ spatFeatPlot2D( vor_max_radius = 200, axis_text = 8, axis_title = 8, - cow_n_col = 2, + cow_n_col = NULL, cow_rel_h = 1, cow_rel_w = 1, cow_align = "h", diff --git a/man/spatFeatPlot2D_single.Rd b/man/spatFeatPlot2D_single.Rd index d76871984..ab7c299df 100644 --- a/man/spatFeatPlot2D_single.Rd +++ b/man/spatFeatPlot2D_single.Rd @@ -42,7 +42,7 @@ spatFeatPlot2D_single( vor_max_radius = 200, axis_text = 8, axis_title = 8, - cow_n_col = 2, + cow_n_col = NULL, cow_rel_h = 1, cow_rel_w = 1, cow_align = "h", diff --git a/man/spatInSituPlotDensity.Rd b/man/spatInSituPlotDensity.Rd index 19523cb46..01f9dd2e4 100644 --- a/man/spatInSituPlotDensity.Rd +++ b/man/spatInSituPlotDensity.Rd @@ -23,7 +23,7 @@ spatInSituPlotDensity( axis_title = 8, legend_text = 6, background_color = "black", - cow_n_col = 2, + cow_n_col = NULL, cow_rel_h = 1, cow_rel_w = 1, cow_align = "h", diff --git a/man/spatInSituPlotHex.Rd b/man/spatInSituPlotHex.Rd index 9e91cca6b..8a7a3adf4 100644 --- a/man/spatInSituPlotHex.Rd +++ b/man/spatInSituPlotHex.Rd @@ -25,7 +25,7 @@ spatInSituPlotHex( axis_title = 8, legend_text = 6, background_color = "white", - cow_n_col = 2, + cow_n_col = NULL, cow_rel_h = 1, cow_rel_w = 1, cow_align = "h", diff --git a/man/spatPlot2D.Rd b/man/spatPlot2D.Rd index b035f46a0..11216f0f7 100644 --- a/man/spatPlot2D.Rd +++ b/man/spatPlot2D.Rd @@ -60,7 +60,7 @@ spatPlot2D( vor_alpha = 1, axis_text = 8, axis_title = 8, - cow_n_col = 2, + cow_n_col = NULL, cow_rel_h = 1, cow_rel_w = 1, cow_align = "h", diff --git a/man/wrap_msg.Rd b/man/wrap_msg.Rd index 6413d3d4c..0c4261bd0 100644 --- a/man/wrap_msg.Rd +++ b/man/wrap_msg.Rd @@ -4,12 +4,12 @@ \alias{wrap_msg} \title{Wrap message} \usage{ -wrap_msg(..., collapse = "") +wrap_msg(..., sep = " ") } \arguments{ \item{...}{additional strings and/or elements to pass to cat} -\item{collapse}{how to join elements of string (default is no space)} +\item{sep}{how to join elements of string (default is one space)} \item{strWidth}{externally set wrapping width. (default value of 100 is not effected)} diff --git a/man/wrap_txt.Rd b/man/wrap_txt.Rd index bfda9c187..9fde84815 100644 --- a/man/wrap_txt.Rd +++ b/man/wrap_txt.Rd @@ -4,12 +4,12 @@ \alias{wrap_txt} \title{Wrap text} \usage{ -wrap_txt(..., collapse = "", strWidth = 100) +wrap_txt(..., sep = " ", strWidth = 100) } \arguments{ \item{...}{additional params to pass} -\item{collapse}{how to join elements of string (default is no space)} +\item{sep}{how to join elements of string (default is one space)} \item{strWidth}{externally set wrapping width. (default value of 100 is not effected)} }