-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix prepare_ funcs. Add run_integration
- Loading branch information
Showing
37 changed files
with
1,340 additions
and
215 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#' Add mixing metric | ||
#' | ||
#' Compute the mixing metric for a given reduction and add it to the metadata | ||
#' of a \link{Seurat} object. See \link[Seurat]{MixingMetric} for more details. | ||
#' @param invert Invert the mixing metric such that higher values | ||
#' indicate better mixing. | ||
#' @param normalise Normalise the mixing metric to the range [0,1]. | ||
#' @inheritParams Seurat::MixingMetric | ||
#' @inheritDotParams Seurat::MixingMetric | ||
#' @export | ||
add_mixingmetric <- function(obj, | ||
reduction, | ||
new_col=paste0("MixingMetric_",reduction), | ||
grouping.var = "source", | ||
k = 5, | ||
max.k = 300, | ||
dims = NULL, | ||
invert = TRUE, | ||
normalise = FALSE, | ||
...){ | ||
if(is.null(dims)){ | ||
dims <- seq(ncol(obj[[reduction]])) | ||
} | ||
message("Computing mixing metric for reduction: ",reduction, | ||
" (",max(dims)," dimensions)") | ||
obj@meta.data[[new_col]] <- Seurat::MixingMetric( | ||
obj, | ||
reduction = reduction, | ||
grouping.var = grouping.var, | ||
k = k, | ||
max.k = max.k, | ||
dims = dims, | ||
...) | ||
if(isTRUE(invert)){ | ||
obj@meta.data[[new_col]] <- max.k - obj@meta.data[[new_col]] | ||
} | ||
if(isTRUE(normalise)){ | ||
obj@meta.data[[new_col]] <- obj@meta.data[[new_col]] / max.k | ||
} | ||
return(obj) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,31 @@ | ||
color_nightlight <- function(n = 40){ | ||
ggplot2::scale_color_gradientn( | ||
colors=pals::gnuplot(n = n)[-(seq(as.integer(n*0.2)))]) | ||
#' Scale color nightlight | ||
#' | ||
#' Add color gradient with nightlight colors. | ||
#' @export | ||
#' @param reverse Reverse the color gradient. | ||
#' @param alpha Transparency of the colors. | ||
#' @inheritParams pals::gnuplot | ||
#' @inheritParams ggplot2::scale_color_gradientn | ||
#' @inheritDotParams ggplot2::scale_color_gradientn | ||
scale_color_nightlight <- function(n = 3, | ||
colors=pals::gnuplot(n = n+1)[ | ||
-(seq(as.integer((n+1)*0.2))) | ||
], | ||
reverse=FALSE, | ||
alpha=NULL, | ||
...){ | ||
if(!is.null(alpha)) { | ||
if(length(alpha)==1){ | ||
colors <- ggplot2::alpha(colors,alpha) | ||
} else if(length(alpha)==length(colors)){ | ||
for(i in seq_along(colors)){ | ||
colors[[i]] <- ggplot2::alpha(colors[[i]],alpha[i]) | ||
} | ||
} else { | ||
stopper("Length of alpha must be 1 or equal to length of colors.") | ||
} | ||
} | ||
if(isTRUE(reverse)) colors <- rev(colors) | ||
ggplot2::scale_color_gradientn(colors=colors, | ||
...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
get_correlated_factors <- function(obj, | ||
keys, | ||
metadata_var, | ||
p_threshold=.05, | ||
r_threshold=.1){ | ||
requireNamespace("Hmisc") | ||
obsm <- scKirby::get_obsm(obj, | ||
keys = keys, | ||
n=1) | ||
obsm <- cbind(obsm, | ||
nFeature_score=obj$nFeature_score[rownames(obsm)]) | ||
obsm_rcor <- Hmisc::rcorr(obsm) | ||
obsm_cor_sig <- sort( | ||
abs(obsm_rcor$r[metadata_var,][obsm_rcor$P[metadata_var,]<p_threshold]), | ||
decreasing = TRUE | ||
) | ||
omit_dims <- as.numeric(gsub(".*_","", | ||
names(obsm_cor_sig[obsm_cor_sig>r_threshold])) | ||
) | ||
return(omit_dims) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#' Get variance explained | ||
#' | ||
#' Get the proportion of total variance explained | ||
#' by a given dimensionality reduction | ||
#' (0-1 where 1 indicates 100% of variance explained). | ||
#' @export | ||
#' @examples | ||
#' obj <- get_HPO() | ||
#' out <- get_variance_explained(obj) | ||
get_variance_explained <- function(obj, | ||
reduction = names(obj@reductions)[1], | ||
layer = "scale.data", | ||
dims=NULL | ||
){ | ||
dr <- obj[[reduction]] | ||
mat <- Seurat::GetAssayData(obj, layer = layer) | ||
total_variance <- sum(matrixStats::rowVars(mat)) | ||
## EigenValues | ||
if(length(dr@stdev)==0){ | ||
messager("Computing stdev for reduction.") | ||
dr@stdev <- apply(obj[[reduction]]@cell.embeddings,2, | ||
stats::sd)|>unname() | ||
} | ||
eigValues <- (dr@stdev)^2|>`names<-`(colnames(dr@cell.embeddings)) | ||
if(!is.null(dims)) { | ||
if(length(dims)>length(eigValues) || | ||
max(dims)>length(eigValues) ){ | ||
stopper( | ||
"The length of dims must be less than or equal to", | ||
"the number of dimensions." | ||
) | ||
} | ||
eigValues <- eigValues[dims] | ||
} | ||
varExplained <- eigValues / total_variance | ||
messager("Proportion of total variance explained by", | ||
length(eigValues),"dimensions:", | ||
round(sum(varExplained),3)) | ||
return(varExplained) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.