Skip to content

Commit

Permalink
regenerate subobjects and clean up code
Browse files Browse the repository at this point in the history
jiajic committed May 22, 2024
1 parent 090bb36 commit 0fcc11a
Showing 28 changed files with 277 additions and 110 deletions.
7 changes: 5 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: GiottoData
Type: Package
Title: Datasets for Giotto
Version: 0.2.11.0
Author: c(
Version: 0.2.12.0
Authors@R: c(
person("Ruben", "Dries", email = "[email protected]",
role = c("aut", "cre"))
)
@@ -21,9 +21,12 @@ Depends:
Imports:
data.table (>= 1.14.0),
GiottoUtils (>= 0.1.7),
rprojroot
Suggests:
devtools,
GiottoVisuals,
Giotto,
reticulate,
terra (>= 1.5-12)
Remotes:
drieslab/GiottoClass,
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Generated by roxygen2: do not edit by hand

export(gDataDir)
export(generate_mini_subobjects)
export(getSODBDataset)
export(getSpatialDataset)
export(listSODBDatasetExperimentNames)
23 changes: 23 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

# GiottoData 0.2.12.0 (2024/05/22)

## Bug fixes
- fix onload image subobject reconnection when generated from dev directory as opposed to lib directory

## Breaking changes
- `gDataDir()` has been made an internal

## Enhancements
- Additional params can now be passed to `GiottoClass::loadGiotto()` from `loadGiottoMini()` through `...`
- `init_gobject` param for `loadGiottoMini()`

## New
- `generate_mini_subobjects()` for rebuilding the mini subobjects


# GiottoData 0.2.11.0 (2024/05/21)

## Changes
- Mini CosMx rebuilt for GiottoClass 0.3.1
- Mini Visium rebuilt for GiottoClass 0.3.1. Added both the "alignment" and "image" (H&E) images. "image" was also spatially re-aligned
- Mini starmap rebuilt for GiottoClass 0.3.1
102 changes: 63 additions & 39 deletions R/dataset_helpers.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@


# list of available mini gobjects
# Each entry is named by the dataset it points to.
# Entries contain filepath terms to get to where the data exists.
mini_gobject_manifest = list(
'visium' = list("Visium", "VisiumObject"),
'vizgen' = list("Vizgen", "VizgenObject"),
'cosmx' = list("CosMx", "CosMxObject"),
'seqfish' = list("seqfish", "seqfishObject"),
'starmap' = list("3D_starmap", "3DStarmapObject"),
'spatialgenomics' = list("SpatialGenomics", "SpatialGenomicsObject")
)




#' @title loadGiottoMini
#' @name loadGiottoMini
#' @param dataset mini dataset giotto object to load
#' @param python_path pythan path to use
#' @param init_gobject logical. Whether to initialize gobject on load
#' @param \dots additional params to pass to `GiottoClass::loadGiotto()`
#' @description This function will automatically load one of the existing mini
#' giotto objects. These are processed giotto objects that can be used to test
#' Giotto functions and run examples. If no python path is provided it will try
@@ -17,49 +36,54 @@
#' Instructions, such as for saving plots, can be changed
#' using the \code{\link{changeGiottoInstructions}}
#' @export
loadGiottoMini = function(dataset = c('visium', 'seqfish', 'starmap', 'vizgen', 'cosmx', 'spatialgenomics'),
python_path = NULL) {


dataset = match.arg(dataset, choices = c('visium', 'seqfish', 'starmap', 'vizgen', 'cosmx', 'spatialgenomics'))

mini_gobject = switch(
dataset,
'visium' = loadGiotto(path_to_folder = system.file('/Mini_datasets/Visium/VisiumObject/', package = 'GiottoData'),
python_path = python_path,
reconnect_giottoImage = FALSE),
'vizgen' = loadGiotto(path_to_folder = system.file('/Mini_datasets/Vizgen/VizgenObject/', package = 'GiottoData'),
python_path = python_path,
reconnect_giottoImage = FALSE),
'cosmx' = loadGiotto(path_to_folder = system.file('/Mini_datasets/CosMx/CosMxObject/', package = 'GiottoData'),
python_path = python_path,
reconnect_giottoImage = FALSE),
'seqfish' = loadGiotto(path_to_folder = system.file('/Mini_datasets/seqfish/seqfishObject/', package = 'GiottoData'),
python_path = python_path,
reconnect_giottoImage = FALSE),
# {
# wrap_msg('To be implemented \n')
# return(invisible(NULL)) # exit early
# },
'starmap' = loadGiotto(path_to_folder = system.file('/Mini_datasets/3D_starmap/3DStarmapObject/', package = 'GiottoData'),
python_path = python_path),
'spatialgenomics' = loadGiotto(path_to_folder = system.file('/Mini_datasets/SpatialGenomics/SpatialGenomicsObject/', package = 'GiottoData'),
python_path = python_path)
)


# 1. change default instructions
# Only mini object-specific instructions should be updated here. The python
# path update was taken care of inside of `loadGiotto()`
mini_gobject = changeGiottoInstructions(gobject = mini_gobject,
params = c('show_plot', 'return_plot', 'save_plot', 'save_dir'),
new_values = c(TRUE, FALSE, FALSE, NA))

return(mini_gobject)
loadGiottoMini = function(
dataset = c(
'visium',
'seqfish',
'starmap',
'vizgen',
'cosmx',
'spatialgenomics'
),
python_path = NULL,
init_gobject = TRUE,
...
) {
dataset = match.arg(dataset, choices = c(
names(mini_gobject_manifest)
))

load_fun <- function(x) {
GiottoClass::loadGiotto(
path_to_folder = x,
python_path = python_path,
reconnect_giottoImage = FALSE,
init_gobject = init_gobject,
...
)
}

# use libdir since this function is user-facing
path <- do.call(gdata_dataset_libdir, mini_gobject_manifest[[dataset]])
mini_gobject <- load_fun(path)

# 1. change default instructions
# Only mini object-specific instructions should be updated here. The python
# path update was taken care of inside of `loadGiotto()`
mini_gobject = changeGiottoInstructions(
gobject = mini_gobject,
params = c('show_plot', 'return_plot', 'save_plot', 'save_dir'),
new_values = c(TRUE, FALSE, FALSE, NA),
init_gobject = init_gobject
)

return(mini_gobject)
}





#' @title getSpatialDataset
#' @name getSpatialDataset
#' @param dataset dataset to download
17 changes: 17 additions & 0 deletions R/mini_objects.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

#' @name generate_mini_subobjects
#' @title Generate mini subobjects
#' @description
#' Generate the GiottoData mini subobjects based on Mini_objects_script.R
#' in `/inst/Mini_objects/` subdirectory.
#' @returns NULL invisibly
#' @export
generate_mini_subobjects <- function() {

script_path <- file.path(
gdata_devdir(), "inst", "Mini_objects", "Mini_objects_script.R"
)

source(script_path, echo = TRUE)
return(invisible())
}
25 changes: 8 additions & 17 deletions R/test_objects.R
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@
#' is available (see \code{\link{listSubObjectMini}})
#' @export
loadSubObjectMini = function(x, idx = 1L) {

# declare data.table variables
type = index = path = NULL

@@ -22,20 +22,11 @@ loadSubObjectMini = function(x, idx = 1L) {
load_data = GiottoClass::vect(load_data)
}

if(x == 'giottoLargeImage') {
original_path = load_data@file_path
new_path = gsub(pattern = '.*[/]GiottoData/', replacement = '', x = original_path)
new_path = paste0(gDataDir(), new_path)
load_data = GiottoClass::reconnect_giottoLargeImage(giottoLargeImage = load_data,
image_path = new_path)
}

if(x == 'giottoImage') {
if(x %in% c('giottoImage', 'giottoLargeImage')) {
original_path = load_data@file_path
new_path = gsub(pattern = '.*[/]GiottoData/', replacement = '', x = original_path)
new_path = paste0(gDataDir(), new_path)
load_data = GiottoClass::reconnect_giottoImage_MG(giottoImage = load_data,
image_path = new_path)
new_path = gsub(pattern = '.*[/]GiottoData/|.*[/]GiottoData/inst/', replacement = '', x = original_path)
new_path = paste0(gdata_libdir(), new_path)
load_data = GiottoClass::reconnect(load_data, mage_path = new_path)
}

return(load_data)
@@ -52,7 +43,7 @@ loadSubObjectMini = function(x, idx = 1L) {
#' @param x subobject type (NULL lists all subobject types)
#' @export
listSubObjectMini = function(x = NULL) {

# declare data.table variables
path = type = NULL

@@ -76,11 +67,11 @@ listSubObjectMini = function(x = NULL) {
#' for the external version which provides those functions.
#' @keywords internal
list_subobject_mini = function() {

# declare data.table variables
path = type = NULL

miniobj_path = paste0(gDataDir(), '/Mini_objects/subobjects')
miniobj_path = paste0(gdata_libdir(), '/Mini_objects/subobjects')

avail_obj = list.files(path = miniobj_path, full.names = TRUE, recursive = TRUE)
avail_obj_dt = data.table::data.table(file = basename(avail_obj), path = avail_obj)
66 changes: 59 additions & 7 deletions R/utilities.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,62 @@

#' @title Get GiottoData library path
#' @name gDataDir
#' @description Utility function to find library install location of the GiottoData
#' package. Provided as shorthand to make loading files easier.
#' @export
gDataDir = function() {
system.file(package = 'GiottoData')
#' @title Get GiottoData paths
#' @name giottodata_paths
#' @description Utility functions to get helpful filepaths within the
#' \pkg{GiottoData} package.
#' @param \dots passed to `file.path()`
NULL


# library paths ####

#' @describeIn giottodata_paths Get the library install path
#' of the package. Should not be used in contexts where package is loaded with
#' `devtools::load_all()`
#' @keywords internal
gdata_libdir = function(...) {
file.path(system.file(package = 'GiottoData'), ...)
}

#' @describeIn giottodata_paths Get the library path to the mini
#' subobjects directory
#' @keywords internal
gdata_subobject_libdir <- function(...) {
gdata_libdir("Mini_objects", ...)
}

#' @describeIn giottodata_paths Get the library path to the mini
#' datasets directory
#' @keywords internal
gdata_dataset_libdir <- function(...) {
gdata_libdir("Mini_datasets", ...)
}



# development paths ####

#' @describeIn giottodata_paths Get the development root path
#' @keywords internal
gdata_devdir <- function(...) {
file.path(rprojroot::find_package_root_file(), ...)
}

#' @describeIn giottodata_paths Get the development path to the mini
#' subobjects directory
#' @keywords internal
gdata_subobject_devdir <- function(...) {
gdata_devdir("inst", "Mini_objects", ...)
}


#' @describeIn giottodata_paths Get the development path to the mini
#' datasets directory
#' @keywords internal
gdata_dataset_devdir <- function(...) {
gdata_devdir("inst", "Mini_datasets", ...)
}

# https://stackoverflow.com/questions/7963898/extracting-the-last-n-characters-from-a-string-in-r
str_tail <- function(x, n){
substr(x, nchar(x)-n+1, nchar(x))
}
57 changes: 26 additions & 31 deletions inst/Mini_objects/Mini_objects_script.R
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
## MINI OBJECTS script for use in subobject testing ##

# remotes::install_github("drieslab/GiottoData")
library(GiottoData) # devtools::load_all()

# remotes::install_github("drieslab/Giotto@suite_dev")

# 0. preparation ####
# ----------------- #

devtools::load_all(GiottoData:::gdata_devdir())
# load dataset from development directory
viz <- loadGiottoMini("vizgen")





# 1. extract subobjects ####
# ----------------- #

@@ -122,9 +115,9 @@ spatial_grid_obj <- getSpatialGrid(viz)

# images
# Freshly generate to allow reconnection
data_path <- system.file("/Mini_datasets/Vizgen/Raw/", package = "GiottoData")
DAPI_z0_image_path <- paste0(data_path, "/", "images/mini_dataset_dapi_z0.jpg")
polyT_z0_image_path <- paste0(data_path, "/", "images/mini_dataset_polyT_z0.jpg")
im_dir <- gdata_dataset_libdir("Vizgen", "Raw", "images")
DAPI_z0_image_path <- file.path(im_dir, "mini_dataset_dapi_z0.jpg")
polyT_z0_image_path <- file.path(im_dir, "mini_dataset_polyT_z0.jpg")

# x and y information from original script
ultra_mini_extent <- terra::ext(c(6400.029, 6900.037, -5150.007, -4699.967))
@@ -144,47 +137,49 @@ imagelist <- createGiottoLargeImageList(
# 2. save subobjects ####
# ----------------- #

# you need to use your local GiottoData repo
giottodata_repo <- "/Users/gsi-local/Documents/GitHub/GiottoData/"
miniobj_path <- paste0(giottodata_repo, "inst/Mini_objects/subobjects/")
miniobj_path <- gdata_subobject_devdir("subobjects")

# expression
saveRDS(expr_vals_raw, file = paste0(miniobj_path, "/", "exprObj/viz_agg_expr_raw.RDS"))
saveRDS(expr_vals_norm, file = paste0(miniobj_path, "/", "exprObj/viz_agg_expr_norm.RDS"))
expr_path <- file.path(miniobj_path, "exprObj")
saveRDS(expr_vals_raw, file = file.path(expr_path, "viz_agg_expr_raw.RDS"))
saveRDS(expr_vals_norm, file = file.path(expr_path, "viz_agg_expr_norm.RDS"))

# cell metadata
saveRDS(cm, file = paste0(miniobj_path, "/", "cellMetaObj/viz_agg_cellmeta.RDS"))
saveRDS(cm, file = file.path(miniobj_path, "cellMetaObj", "viz_agg_cellmeta.RDS"))

# feat metadata
saveRDS(fm, file = paste0(miniobj_path, "/", "featMetaObj/viz_agg_featmeta.RDS"))
saveRDS(fm, file = file.path(miniobj_path, "featMetaObj", "viz_agg_featmeta.RDS"))

# spatial locations
saveRDS(spat_locs, file = paste0(miniobj_path, "/", "spatLocsObj/viz_agg_spatlocs.RDS"))
saveRDS(spat_locs, file = file.path(miniobj_path, "spatLocsObj", "viz_agg_spatlocs.RDS"))

# spatial network
saveRDS(del_net, file = paste0(miniobj_path, "/", "spatialNetworkObj/viz_agg_spatnet_del.RDS"))
saveRDS(knn_net, file = paste0(miniobj_path, "/", "spatialNetworkObj/viz_agg_spatnet_knn.RDS"))
sn_path <- file.path(miniobj_path, "spatialNetworkObj")
saveRDS(del_net, file = file.path(sn_path, "viz_agg_spatnet_del.RDS"))
saveRDS(knn_net, file = file.path(sn_path, "viz_agg_spatnet_knn.RDS"))

# spatial enrichment
saveRDS(mg_enr, file = paste0(miniobj_path, "/", "spatEnrObj/viz_agg_metagene.RDS"))
saveRDS(mg_enr, file = file.path(miniobj_path, "spatEnrObj", "viz_agg_metagene.RDS"))

# nearest neighbor network
saveRDS(nn, file = paste0(miniobj_path, "/", "nnNetObj/viz_agg_sNN.RDS"))
saveRDS(nn, file = file.path(miniobj_path, "nnNetObj", "viz_agg_sNN.RDS"))

# dim reduction
saveRDS(dim_red_pca, file = paste0(miniobj_path, "/", "dimObj/viz_agg_pca.RDS"))
saveRDS(dim_red_umap, file = paste0(miniobj_path, "/", "dimObj/viz_agg_umap.RDS"))
dr_path <- file.path(miniobj_path, "dimObj")
saveRDS(dim_red_pca, file = file.path(dr_path, "viz_agg_pca.RDS"))
saveRDS(dim_red_umap, file = file.path(dr_path, "viz_agg_umap.RDS"))

gpoints <- GiottoClass::wrap(gpoints)
saveRDS(gpoints, file = paste0(miniobj_path, "/", "giottoPoints/viz_agg_gpoints.RDS"))
saveRDS(gpoints, file = file.path(miniobj_path, "giottoPoints", "viz_agg_gpoints.RDS"))

gpoly <- GiottoClass::wrap(gpoly)
saveRDS(gpoly, file = paste0(miniobj_path, "/", "giottoPolygon/viz_agg_gpoly.RDS"))
saveRDS(gpoly, file = file.path(miniobj_path, "giottoPolygon", "viz_agg_gpoly.RDS"))

# largeImages
# loaded by replacing a portion of the path with the end user's library path location
saveRDS(imagelist[[1]], file = paste0(miniobj_path, "/", "giottoLargeImage/viz_dapi_z0.RDS"))
saveRDS(imagelist[[2]], file = paste0(miniobj_path, "/", "giottoLargeImage/viz_polyT_z0.RDS"))
im_path <- file.path(miniobj_path, "giottoLargeImage")
saveRDS(imagelist[[1]], file = file.path(im_path, "viz_dapi_z0.RDS"))
saveRDS(imagelist[[2]], file = file.path(im_path, "viz_polyT_z0.RDS"))

# spatial grid
saveRDS(spatial_grid_obj, file = paste0(miniobj_path, "/", "spatialGridObj/viz_agg_spatialGridObj.RDS"))
saveRDS(spatial_grid_obj, file = file.path(miniobj_path, "spatialGridObj", "viz_agg_spatialGridObj.RDS"))
Binary file modified inst/Mini_objects/subobjects/cellMetaObj/viz_agg_cellmeta.RDS
Binary file not shown.
Binary file modified inst/Mini_objects/subobjects/dimObj/viz_agg_pca.RDS
Binary file not shown.
Binary file modified inst/Mini_objects/subobjects/dimObj/viz_agg_umap.RDS
Binary file not shown.
Binary file modified inst/Mini_objects/subobjects/exprObj/viz_agg_expr_norm.RDS
Binary file not shown.
Binary file modified inst/Mini_objects/subobjects/exprObj/viz_agg_expr_raw.RDS
Binary file not shown.
Binary file modified inst/Mini_objects/subobjects/featMetaObj/viz_agg_featmeta.RDS
Binary file not shown.
Binary file modified inst/Mini_objects/subobjects/giottoLargeImage/viz_dapi_z0.RDS
Binary file not shown.
Binary file modified inst/Mini_objects/subobjects/giottoLargeImage/viz_polyT_z0.RDS
Binary file not shown.
Binary file modified inst/Mini_objects/subobjects/giottoPoints/viz_agg_gpoints.RDS
Binary file not shown.
Binary file modified inst/Mini_objects/subobjects/giottoPolygon/viz_agg_gpoly.RDS
Binary file not shown.
Binary file modified inst/Mini_objects/subobjects/nnNetObj/viz_agg_sNN.RDS
Binary file not shown.
Binary file modified inst/Mini_objects/subobjects/spatEnrObj/viz_agg_metagene.RDS
Binary file not shown.
Binary file modified inst/Mini_objects/subobjects/spatLocsObj/viz_agg_spatlocs.RDS
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
12 changes: 0 additions & 12 deletions man/gDataDir.Rd

This file was deleted.

15 changes: 15 additions & 0 deletions man/generate_mini_subobjects.Rd
53 changes: 53 additions & 0 deletions man/giottodata_paths.Rd
8 changes: 7 additions & 1 deletion man/loadGiottoMini.Rd

0 comments on commit 0fcc11a

Please sign in to comment.