Skip to content

Commit

Permalink
Fix prepare_ funcs. Add run_integration
Browse files Browse the repository at this point in the history
  • Loading branch information
bschilder committed Apr 8, 2024
1 parent 6375d47 commit c3ce907
Show file tree
Hide file tree
Showing 37 changed files with 1,340 additions and 215 deletions.
7 changes: 5 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,18 @@ Suggests:
downloadR,
HPOExplorer,
Nebulosa,
arrow
arrow,
ggsankey,
Hmisc
Remotes:
github::neurogenomics/scNLP,
github::neurogenomics/scKirby,
github::neurogenomics/KGExplorer,
github::neurogenomics/HPOExplorer,
github::RajLabMSSM/echotabix,
github::RajLabMSSM/downloadR,
github::satijalab/seurat-wrappers
github::satijalab/seurat-wrappers,
github::davidsjoberg/ggsankey
RoxygenNote: 7.3.1
VignetteBuilder: knitr
Config/testthat/edition: 3
7 changes: 7 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Generated by roxygen2: do not edit by hand

export(add_cluster_colors)
export(add_mixingmetric)
export(adjust_zstat)
export(dt_to_granges)
export(find_neighbors)
Expand All @@ -15,11 +16,13 @@ export(get_mhc_genes)
export(get_top_factors)
export(get_top_features)
export(get_top_traits)
export(get_variance_explained)
export(import_abc)
export(is_granges)
export(iterate_gsea)
export(iterate_lm)
export(magma_matrix)
export(map_id_sep)
export(map_phenotypes)
export(map_snps2genes)
export(opengwas_meta)
Expand All @@ -28,10 +31,12 @@ export(phenomix_query)
export(phenomix_query_batched)
export(plot_dag_enrich)
export(plot_enrichment)
export(plot_factors_sankey)
export(plot_feature_density)
export(plot_highlights)
export(plot_hpo_pseudotime)
export(plot_ontological_velocity)
export(plot_preservation_histo)
export(plot_reduction)
export(plot_top_celltypes)
export(plot_trait_cor)
Expand All @@ -44,12 +49,14 @@ export(run_autoencoder)
export(run_dag_enrich)
export(run_gprofiler)
export(run_imputation)
export(run_integration)
export(run_lda)
export(run_mofa2)
export(run_pca)
export(run_sparsesvd)
export(run_umap)
export(run_variancePartition)
export(scale_color_nightlight)
export(theme_nightlight)
import(GenomicFiles)
import(HPOExplorer)
Expand Down
41 changes: 41 additions & 0 deletions R/add_mixingmetric.R
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)
}
33 changes: 30 additions & 3 deletions R/color_nightlight.R
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,
...)
}
21 changes: 21 additions & 0 deletions R/get_correlated_factors.R
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)
}
40 changes: 40 additions & 0 deletions R/get_variance_explained.R
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)
}
3 changes: 2 additions & 1 deletion R/map_xref.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ map_xref <- function(dat,
if(length(r)==0) NA else unlist(r)
})]
}
dat[,(new_col):=map_id_sep(get(new_col))]
dat[get(new_col)=="NA",(new_col):=NA]
messager("% of non-NA rows:",
round(sum(!is.na(dat[[new_col]]))/nrow(dat)*100,2),v=verbose)
}
}
Loading

0 comments on commit c3ce907

Please sign in to comment.