Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: jazzPanda
Type: Package
Title: Finding spatially relevant marker genes in image based spatial transcriptomics data
Version: 0.99.7
Version: 0.2.3
Date: 2024-07-25
Authors@R:
c(person(given = "Melody",
Expand All @@ -11,7 +11,7 @@ Authors@R:
comment=c(ORCID="0000-0002-2222-0958"))
)
LazyData: FALSE
Depends: R (>= 4.4.0)
Depends: R (>= 4.5.0)
Imports: spatstat.geom, dplyr, glmnet, caret, foreach, stats, magrittr, doParallel, BiocParallel, methods, BumpyMatrix,SpatialExperiment
VignetteBuilder: knitr
Suggests:
Expand Down
25 changes: 25 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
# jazzPanda 0.2.3
*Updated on June 24, 2025*

Changes to the package include:

- **function `get_vectors()`**:
- Parameters `w_x` and `w_y` have been removed. Spatial windows are now
automatically determined **per sample** using a 5% buffer around the
coordinate range.
- Improved handling of genes that are only detected in a subset of samples.
If a gene is not present in a sample, a zero-filled vector is used for that
sample to maintain consistent vector lengths across samples.

- **function `create_genesets()`**:
- Parameters `w_x` and `w_y` have been removed. Spatial windows are now
computed **per sample** using a 5% buffer based on input coordinates.
- Improved handling of genes that are only detected in a subset of samples.
If a gene is not present in a sample, a zero-filled vector is used for that
sample to maintain consistent vector lengths across samples.

- **function `compute_permp()`**:
- Parameters `w_x` and `w_y` are no longer required. Spatial boundaries
are now automatically derived **per sample** using a 5% buffer.


# jazzPanda 0.2.2
*Updated on February 23, 2025*

Expand Down
73 changes: 17 additions & 56 deletions R/compute_permp.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@
#' two giving the numbers of rectangular quadrats in the x and y directions. If
#' the \code{bin_type} is "hexagonal", this will be a number giving the side
#' length of hexagons. Positive numbers only.
#' @param w_x a numeric vector of length two specifying the x coordinate
#' limits of enclosing box.
#' @param w_y a numeric vector of length two specifying the y coordinate
#' limits of enclosing box.
#' @param use_cm A boolean value that specifies whether to create spatial
#' vectors for genes using the count matrix and cell coordinates instead of
#' the transcript coordinates when both types of information are available.
Expand All @@ -46,39 +42,13 @@
#' \item{\code{gene_mt} }{ contains the transcript count in each grid.
#' Each row refers to a grid, and each column refers to a gene.}
.compute_observation<- function(x, cluster_info, correlation_method, n_cores,
test_genes,bin_type, bin_param, w_x, w_y,use_cm){
# primary_class <- class(x)[1]
# if (primary_class == "SpatialExperiment" |
# primary_class == "SpatialFeatureExperiment"){
# if (is.null(x$sample_id) == FALSE){
# if (length(unique(x$sample_id))>1){
# stop("Input x has multiple samples")
# }
# cluster_info$sample <- unique(x$sample_id)
# }else{
# cluster_info$sample <- "sample1"
# x$sample_id <- "sample1"
# }
# }else if (primary_class == "SingleCellExperiment"){
# if (length(names(x@assays@data@listData)) == 1){
# cluster_info$sample <- names(x@assays@data@listData)
# }else if (length(names(x@assays@data@listData)) > 1){
# stop("Input x has multiple samples")
# }else {
# cluster_info$sample <- "sample1"
# names(x@assays@data@listData) <- "sample1"
# }
# }else{
# stop("The input class of 'x' is not supported.
# Please convert 'x' to one of the following supported types:
# SingleCellExperiment, SpatialExperiment, or SpatialFeatureExperiment.")
# }
test_genes,bin_type, bin_param, use_cm){
vectors_lst <- get_vectors(x=x,sample_names=unique(cluster_info$sample),
cluster_info = cluster_info,
bin_type=bin_type,
bin_param=bin_param,
test_genes=test_genes, n_cores=n_cores,
w_x=w_x, w_y=w_y,use_cm =use_cm)
use_cm =use_cm)

# calculate correlation of permuted clusters and gene
obs.stat <- cor(x=vectors_lst$gene_mt, y=vectors_lst$cluster_mt,
Expand Down Expand Up @@ -106,10 +76,6 @@
#' @param n_cores A positive number specifying number of cores used for
#' parallelizing permutation testing. Default is one core
#' (sequential processing).
#' @param w_x a numeric vector of length two specifying the x coordinate
#' limits of enclosing box.
#' @param w_y a numeric vector of length two specifying the y coordinate
#' limits of enclosing box.
#' @param gene_mt A matrix contains the transcript count in each grid.
#' Each row refers to a grid, and each column refers to a gene.
#' @param cluster_names A list of strings giving the name and order of the
Expand All @@ -119,13 +85,24 @@
#' @importFrom stats cor
#' @return A matrix with permutation statistics
#'

.compute_permutation<- function(cluster_info, perm.size = 1000,
correlation_method = "pearson", bin_type,
bin_param, n_cores=1, w_x,w_y, gene_mt,
bin_param, n_cores=1, gene_mt,
cluster_names){
n_clusters <- length(unique(cluster_info$cluster))
t.perm.array<- array(0, dim = c(ncol(gene_mt),
length(cluster_names),perm.size))
# Compute padded ranges for this sample
x_rng <- range(cluster_info$x, na.rm = TRUE)
y_rng <- range(cluster_info$y, na.rm = TRUE)

pad_x <- BUFFER_FRAC * diff(x_rng)
pad_y <- BUFFER_FRAC * diff(y_rng)

w_x <- c(x_rng[1] - pad_x, x_rng[2] + pad_x)
w_y <- c(y_rng[1] - pad_y, y_rng[2] + pad_y)

if (bin_type == "hexagon"){
w <-owin(xrange=w_x, yrange=w_y)
H <-hextess(W=w, bin_param[1])
Expand Down Expand Up @@ -209,11 +186,6 @@
#' @param n_cores A positive number specifying number of cores used for
#' parallelizing permutation testing. Default is one core
#' (sequential processing).
#' @param w_x a numeric vector of length two specifying the x coordinate
#' limits of enclosing box.
#' @param w_y a numeric vector of length two specifying the y coordinate
#' limits of enclosing box.
#'
#' @param correction_method A character string pass to \code{\link{p.adjust}}
#' specifying the correction method for multiple testing .
#' @param use_cm A boolean value that specifies whether to create spatial
Expand Down Expand Up @@ -267,14 +239,6 @@
#' row = trans_info$feature_name, col = trans_info$cell )
#' spe_sample1 <- SpatialExperiment(
#' assays = list(molecules = mol),sample_id ="sample1" )
#' w_x <- c(min(floor(min(trans_info$x)),
#' floor(min(clusters$x))),
#' max(ceiling(max(trans_info$x)),
#' ceiling(max(clusters$x))))
#' w_y <- c(min(floor(min(trans_info$y)),
#' floor(min(clusters$y))),
#' max(ceiling(max(trans_info$y)),
#' ceiling(max(clusters$y))))
#' set.seed(100)
#' corr_res <- compute_permp(x=spe_sample1,
#' cluster_info=clusters,
Expand All @@ -284,9 +248,7 @@
#' test_genes=unique(trans_info$feature_name),
#' correlation_method = "pearson",
#' n_cores=1,
#' correction_method="BH",
#' w_x=w_x ,
#' w_y=w_y)
#' correction_method="BH")
#'
#' # raw permutation p-value
#' perm_p <- get_perm_p(corr_res)
Expand All @@ -298,7 +260,7 @@
compute_permp<-function(x, cluster_info, perm.size, bin_type,
bin_param,test_genes,
correlation_method = "pearson", n_cores=1,
correction_method="BH",w_x ,w_y,use_cm = FALSE){
correction_method="BH",use_cm = FALSE){
message(sprintf("Correlation Method = %s", correlation_method))

tm1 <- system.time(
Expand All @@ -307,7 +269,7 @@ compute_permp<-function(x, cluster_info, perm.size, bin_type,
n_cores=n_cores,use_cm =use_cm,
correlation_method = correlation_method,
bin_type=bin_type,test_genes=test_genes,
bin_param=bin_param, w_x=w_x, w_y = w_y)
bin_param=bin_param)
})

obs.stat<- obs_res$obs.stat
Expand All @@ -325,7 +287,6 @@ compute_permp<-function(x, cluster_info, perm.size, bin_type,
correlation_method = correlation_method,
bin_type=bin_type,
bin_param=bin_param, n_cores=n_cores,
w_x=w_x, w_y = w_y,
gene_mt = obs_res$gene_mt,
cluster_names =colnames(obs.stat) )
# permutation stats
Expand Down
38 changes: 5 additions & 33 deletions R/cor_mg_result.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,6 @@
#' row = trans_info$feature_name, col = trans_info$cell )
#' spe_sample1 <- SpatialExperiment(
#' assays = list(molecules = mol),sample_id ="sample1" )
#' w_x <- c(min(floor(min(trans_info$x)),
#' floor(min(clusters$x))),
#' max(ceiling(max(trans_info$x)),
#' ceiling(max(clusters$x))))
#' w_y <- c(min(floor(min(trans_info$y)),
#' floor(min(clusters$y))),
#' max(ceiling(max(trans_info$y)),
#' ceiling(max(clusters$y))))
#' set.seed(100)
#' corr_res <- compute_permp(x=spe_sample1,
#' cluster_info=clusters,
Expand All @@ -76,9 +68,7 @@
#' test_genes=unique(trans_info$feature_name),
#' correlation_method = "pearson",
#' n_cores=1,
#' correction_method="BH",
#' w_x=w_x ,
#' w_y=w_y)
#' correction_method="BH")
#' # observed correlation
#' obs_corr <- get_cor(corr_res)
get_cor <- function(obj) {
Expand Down Expand Up @@ -127,14 +117,7 @@ get_cor <- function(obj) {
#' row = trans_info$feature_name, col = trans_info$cell )
#' spe_sample1 <- SpatialExperiment(
#' assays = list(molecules = mol),sample_id ="sample1" )
#' w_x <- c(min(floor(min(trans_info$x)),
#' floor(min(clusters$x))),
#' max(ceiling(max(trans_info$x)),
#' ceiling(max(clusters$x))))
#' w_y <- c(min(floor(min(trans_info$y)),
#' floor(min(clusters$y))),
#' max(ceiling(max(trans_info$y)),
#' ceiling(max(clusters$y))))

#' set.seed(100)
#' corr_res <- compute_permp(x=spe_sample1,
#' cluster_info=clusters,
Expand All @@ -144,9 +127,7 @@ get_cor <- function(obj) {
#' test_genes=unique(trans_info$feature_name),
#' correlation_method = "pearson",
#' n_cores=1,
#' correction_method="BH",
#' w_x=w_x ,
#' w_y=w_y)
#' correction_method="BH")
#'
#' # raw permutation p-value
#' perm_p <- get_perm_p(corr_res)
Expand Down Expand Up @@ -195,14 +176,7 @@ get_perm_p <- function(obj) {
#' row = trans_info$feature_name, col = trans_info$cell )
#' spe_sample1 <- SpatialExperiment(
#' assays = list(molecules = mol),sample_id ="sample1" )
#' w_x <- c(min(floor(min(trans_info$x)),
#' floor(min(clusters$x))),
#' max(ceiling(max(trans_info$x)),
#' ceiling(max(clusters$x))))
#' w_y <- c(min(floor(min(trans_info$y)),
#' floor(min(clusters$y))),
#' max(ceiling(max(trans_info$y)),
#' ceiling(max(clusters$y))))

#' set.seed(100)
#' corr_res <- compute_permp(x=spe_sample1,
#' cluster_info=clusters,
Expand All @@ -212,9 +186,7 @@ get_perm_p <- function(obj) {
#' test_genes=unique(trans_info$feature_name),
#' correlation_method = "pearson",
#' n_cores=1,
#' correction_method="BH",
#' w_x=w_x ,
#' w_y=w_y)
#' correction_method="BH")
#' # adjusted permutation p-value
#' adjusted_perm_p <- get_perm_adjp(corr_res)
get_perm_adjp <- function(obj) {
Expand Down
21 changes: 6 additions & 15 deletions R/create_genesets.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@
#' two giving the numbers of rectangular quadrats in the x and y directions. If
#' the \code{bin_type} is "hexagonal", this will be a number giving the side
#' length of hexagons. Positive numbers only.
#' @param w_x A numeric vector of length two specifying the x coordinate
#' limits of enclosing box.
#' @param w_y A numeric vector of length two specifying the y coordinate
#' limits of enclosing box.
#' @param use_cm A boolean value that specifies whether to create spatial
#' vectors for genes using the count matrix and cell coordinates instead of
#' the transcript coordinates when both types of information are available.
Expand All @@ -52,10 +48,6 @@
#' two giving the numbers of rectangular quadrats in the x and y directions. If
#' the \code{bin_type} is "hexagonal", this will be a number giving the side
#' length of hexagons. Positive numbers only.
#' @param w_x A numeric vector of length two specifying the x coordinate
#' limits of enclosing box.
#' @param w_y A numeric vector of length two specifying the y coordinate
#' limits of enclosing box.
#' @param cluster_info A dataframe/matrix containing the centroid coordinates,
#' cluster and sample label for each cell.The column names must include
#' "x" (x coordinate), "y" (y coordinate),
Expand Down Expand Up @@ -94,25 +86,24 @@
#' name_lst=list(dummy_A=c("A","C"),
#' dummy_B=c("A","B","C")),
#' bin_type="square",
#' bin_param=c(2,2),
#' w_x=c(0,25), w_y=c(0,25), cluster_info=NULL)
#' bin_param=c(2,2),cluster_info=NULL)
#'
create_genesets<-function(x, name_lst,
cluster_info, sample_names, bin_type, bin_param,
w_x, w_y, use_cm = FALSE, n_cores=1){
use_cm = FALSE, n_cores=1){
primary_class <- class(x)[1]
if (primary_class == "list"){
uni_genes <- unique(unlist(lapply(x, function(df) df$feature_name)))
for (sp in sample_names){
sub_x <- x[[sp]]
req_cols <- c("feature_name","x","y")
if (length(setdiff(req_cols, colnames(sub_x))) != 0){
stop("Invalid columns in input x. Every list element in x must
contain columns 'feature_name','x', 'y',
for every transcipt")}
uni_genes <- unique(sub_x$feature_name)
for every transcipt")} }
if (length(setdiff(as.vector(unique(unlist(name_lst))),uni_genes))>0){
stop("Invalid name_lst,
can not find genes in the input name_lst from x")}}
can not find genes in the input name_lst from x")}
test_genes <- intersect(as.vector(unique(unlist(name_lst))), uni_genes)
}else {
if (length(setdiff(as.vector(unique(unlist(name_lst))), row.names(x)))>0){
Expand All @@ -124,7 +115,7 @@ create_genesets<-function(x, name_lst,

res_lst <- get_vectors(x=x, cluster_info=cluster_info,
sample_names=sample_names, bin_type=bin_type,
bin_param=bin_param,test_genes=test_genes, w_x=w_x, w_y=w_y,
bin_param=bin_param,test_genes=test_genes,
use_cm = use_cm, n_cores=n_cores)
gene_vecs <- as.matrix(res_lst$gene_mt)

Expand Down
Loading