Skip to content

Commit

Permalink
Merge pull request #143 from omnideconv/giotto_update
Browse files Browse the repository at this point in the history
Giotto update
  • Loading branch information
czackl committed Apr 3, 2024
2 parents 77044ed + 58db2c2 commit d151f82
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 43 deletions.
62 changes: 25 additions & 37 deletions R/SpatialDWLS.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@
#' @param cell_type_col column of single_cell_obj containing cell type information
#' @param dim_method dimension reduction method
#' @param cluster_method cluster method to use when calculating marker genes
#' @param ... additional paramters
#' @param ... additional paramdataters
build_model_spatial_dwls <- function(single_cell_obj, assay_sc = "counts", marker_method = "scran", topNgenes = 100, cell_type_col = "cell_ontology_class", dim_method = "pca", cluster_method = "leiden", ...) {
# TODO Checks
if (!checkCol(single_cell_obj, cell_type_col)) {
stop("cell_type_col not available")
}

# init_python()

# TODO, implement instructions
if (!exists("giotto_instructions")) {
giotto_instructions <<- Giotto::createGiottoInstructions(python_path = reticulate::conda_list()$python[which(reticulate::conda_list()$name == "r-omnideconv")])
}
Expand All @@ -35,27 +32,21 @@ build_model_spatial_dwls <- function(single_cell_obj, assay_sc = "counts", marke


# markers
# NOTE the makeSignMatrix function requires a list of genes to use in the signature
# They just mention "e.g. markers", so i thought to use the marker gene function provided by giotto
if (marker_method %in% c("scran", "gini", "mast")) {
message("Calculating markers by using the method: ", marker_method)
# turn into giotto for markers!!
obj <- Giotto::createGiottoObject(scExpression, instructions = giotto_instructions)
obj <- Giotto::createGiottoObject(scExpression, instructions = giotto_instructions, ...)

obj <- doGiottoWorkflow(obj, dim_method = dim_method, cluster_method = cluster_method)


message("Calculating Markers")
# calculate based on selection
# TODO add assay to use or select default one by handling all that during Giotto object creation

markers <- Giotto::findMarkers(obj, method = marker_method, cluster_column = paste0(cluster_method, "_clus"))

# markers are a complex list with marker genes for each cluster
# extract the top genes for each cluster and intersect
# extract the top genes for each cluster
genes <- character()
for (cluster in markers) {
genes <- c(genes, cluster$genes[1:topNgenes])
genes <- c(genes, cluster$feats[1:topNgenes])
}

genes <- unique(genes)
Expand All @@ -69,8 +60,7 @@ build_model_spatial_dwls <- function(single_cell_obj, assay_sc = "counts", marke
# get cell type vector from object
cell_types <- as.vector(single_cell_obj[[cell_type_col]])

# TODO Check if markers are necessary!!!
signature <- Giotto::makeSignMatrixDWLSfromMatrix(matrix = scExpression, sign_gene = genes, cell_type_vector = cell_types)
signature <- Giotto::makeSignMatrixDWLSfromMatrix(matrix = scExpression, sign_gene = genes, cell_type_vector = cell_types, ...)

return(signature)
}
Expand All @@ -83,32 +73,29 @@ build_model_spatial_dwls <- function(single_cell_obj, assay_sc = "counts", marke
#' @param result_name token to identify deconvolution results in object, default = "spatialdwls"
#' @param ... additional parameters
deconvolute_spatial_dwls <- function(spatial_obj, signature, assay_sp = "counts", result_name = "spatialdwls", ...) {
# TODO checks

# create Giotto Object
spExpression <- SummarizedExperiment::assay(spatial_obj, assay_sp) %>% as("dgCMatrix")
spCoords <- SpatialExperiment::spatialCoords(spatial_obj)

# init_python()

##########
if (!exists("giotto_instructions")) {
giotto_instructions <<- Giotto::createGiottoInstructions(python_path = reticulate::conda_list()$python[which(reticulate::conda_list()$name == "r-omnideconv")])
}


obj <- Giotto::createGiottoObject(raw_exprs = spExpression, spatial_locs = spCoords, instructions = giotto_instructions)
obj <- Giotto::createGiottoObject(expression = spExpression, spatial_locs = spCoords, instructions = giotto_instructions)

obj <- doGiottoWorkflow(obj, ...)

deconvolution <- Giotto::runDWLSDeconv(obj, sign_matrix = signature, n_cell = 10, return_gobject = FALSE)
deconvolution <- Giotto::runDWLSDeconv(obj, sign_matrix = signature, n_cell = 10, return_gobject = TRUE)

df <- data.frame(getSpatialEnrichment(deconvolution, output = "data.table"))

deconvolution$cell_ID <- NULL
df$cell_ID <- NULL

# attach method token
deconvolution <- attachToken(deconvolution, result_name)
df <- attachToken(df, result_name)

return(deconvolution)
return(df)
}

#' Perform Giotto Workflow of Normalization, HVG, Dimension Reduction and Clustering
Expand All @@ -117,7 +104,11 @@ deconvolute_spatial_dwls <- function(spatial_obj, signature, assay_sp = "counts"
#' @param calculateHVG Wheter to calculate HVG
#' @param dim_method dimension reduction method to use: pca, tsne, umap
#' @param cluster_method cluster method to use c(leiden, kmeans, hclust, louvain)
doGiottoWorkflow <- function(obj, calculateHVG = TRUE, dim_method = "pca", cluster_method = "leiden") {
#' @param show_plot show plot
#' @param return_plot return plot
#' @param save_plot save plot
#' @param ... additional parameters
doGiottoWorkflow <- function(obj, calculateHVG = TRUE, dim_method = "pca", cluster_method = "leiden", show_plot = FALSE, return_plot = FALSE, save_plot = FALSE, ...) {
if (!class(obj)[[1]] == "giotto") {
stop("Object needs to be a Giotto object!")
}
Expand All @@ -126,29 +117,28 @@ doGiottoWorkflow <- function(obj, calculateHVG = TRUE, dim_method = "pca", clust
message("Starting Giotto Workflow")
}

# TODO check if all those steps have already been performed???
obj <- Giotto::normalizeGiotto(obj)

# HVG
# NOTE: the workflow fails often when not calculating hvgs, so this should be default!!!!!
# HVF
if (calculateHVG) {
message("Giotto: Calculating HVGs")
obj <- Giotto::calculateHVG(obj, show_plot = FALSE, return_plot = FALSE, save_plot = FALSE)
message("Giotto: Calculating HVFs")
obj <- Giotto::calculateHVF(obj, show_plot = show_plot, return_plot = return_plot, save_plot = save_plot)
}


# Dimension Reduction
message("Giotto: Performing Dimension Reduction")
obj <- Giotto::runPCA(obj) # run anayway, either pca is used or it needs to be run for tnse and umap?
obj <- Giotto::runPCA(obj)

obj <- switch(dim_method,
pca = {
obj
}, # just return the object, already performed
},
tsne = {
Giotto::runtSNE(obj) # run based on pca, could also be done directly
Giotto::runtSNE(obj)
},
umap = {
Giotto::runUMAP(obj) # run based on pca
Giotto::runUMAP(obj)
}
)

Expand All @@ -160,8 +150,6 @@ doGiottoWorkflow <- function(obj, calculateHVG = TRUE, dim_method = "pca", clust
dim_reduction_name = dim_method
)


# TODO add parameters for all methods
# Clustering
message("Giotto: Clustering")
obj <- switch(cluster_method,
Expand Down
4 changes: 0 additions & 4 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,6 @@ attachToken <- function(deconvolution, token = "deconv") {
stop("Deconvolution result is missing but is required")
}

if (is.null(token)) {
message("No token provided, using 'deconv' as token")
}

# get colnames, attach token and overwrite
celltypes <- colnames(deconvolution)
celltypes <- paste0(token, "_", celltypes)
Expand Down
2 changes: 1 addition & 1 deletion man/build_model_spatial_dwls.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 13 additions & 1 deletion man/doGiottoWorkflow.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d151f82

Please sign in to comment.