-
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.
- Loading branch information
Showing
50 changed files
with
570,931 additions
and
141 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
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,56 @@ | ||
#' Clip outliers | ||
#' | ||
#' Clip outliers from a ggplot object using Mahalanobis distance. | ||
#' @param gg A ggplot or patchwork object. | ||
#' @param max_dist Maximum Mahalanobis distance to consider as a non-outlier. | ||
#' @inheritParams patchwork::plot_layout | ||
#' @inheritDotParams patchwork::wrap_plots | ||
#' @return A ggplot or patchwork object with outliers clipped. | ||
#' @export | ||
#' @examples | ||
#' gg <- ggplot2::ggplot(mtcars,ggplot2::aes(x=mpg,y=disp))+ggplot2::geom_point() | ||
#' clip_outliers(gg) | ||
clip_outliers <- function(gg, | ||
max_dist=NULL, | ||
guides = "collect", | ||
axes="collect", | ||
axis_titles="collect", | ||
...){ | ||
|
||
if(isFALSE(max_dist)) return(gg) | ||
|
||
clip_outliers_i <- function(gg, max_dist){ | ||
is.outlier <- mahal.dist <- NULL; | ||
|
||
df <- ggplot2::ggplot_build(gg)$data[[1]][,c("x","y")] | ||
outlier_md <- rstatix::mahalanobis_distance(data.table::data.table(df)) | ||
if(!is.null(max_dist)){ | ||
outlier_md[,is.outlier:=mahal.dist>max_dist] | ||
} | ||
messager(formatC(sum(outlier_md$is.outlier),big.mark=","), | ||
"outliers identified.") | ||
messager("max_dist for non-outlier:", | ||
max(outlier_md[is.outlier==FALSE,]$mahal.dist)) | ||
# ggplot(aes(x,y),data = outlier_md)+ | ||
# geom_point(aes(color=factor(is.outlier)),size=5) | ||
gg + | ||
ggplot2::lims(x=c(min(outlier_md[is.outlier==FALSE]$x), | ||
max(outlier_md[is.outlier==FALSE]$x) | ||
), | ||
y=c(min(outlier_md[is.outlier==FALSE]$y), | ||
max(outlier_md[is.outlier==FALSE]$y) | ||
) | ||
) | ||
} | ||
if(methods::is(gg,"patchwork")){ | ||
lapply(seq(length(gg)), function(i){ | ||
clip_outliers_i(gg[[i]],max_dist) | ||
}) |> | ||
patchwork::wrap_plots(...) + | ||
patchwork::plot_layout(guides = guides, | ||
axes = axes, | ||
axis_titles =axis_titles) | ||
} else{ | ||
clip_outliers_i(gg,max_dist) | ||
} | ||
} |
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
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,79 @@ | ||
#' Plot ontological similarity | ||
#' | ||
#' Plot ontological similarity of cells to a set of terms in an ontology. | ||
#' @inheritDotParams Seurat::FeaturePlot | ||
#' @export | ||
plot_ontological_similarity <- function(obj, | ||
id_col, | ||
ont, | ||
### Use pre-computed DOSA results | ||
run_ontological_similarity_out=NULL, | ||
### Args for running new DOSA | ||
ancestors=unique(ont@elementMetadata$ancestor), | ||
group_var="seurat_clusters", | ||
top_n=9, | ||
min.cutoff = "q90", | ||
show_plot=TRUE, | ||
...){ | ||
if(!is.null(run_ontological_similarity_out)){ | ||
messager("Using pre-computed DOSA results.") | ||
out <- run_ontological_similarity_out | ||
} else { | ||
out <- run_ontological_similarity( | ||
obj=obj, | ||
id_col=id_col, | ||
ont=ont, | ||
ancestors=ancestors, | ||
group_var=group_var) | ||
} | ||
#### Plot most differentially similar ancestors #### | ||
terms_to_use <- if(length(out$top_markers)==0){ | ||
messager("No term identified from DOSA. Using ancestors instead.") | ||
intersect(ancestors,colnames(out$sim)) | ||
} else { | ||
unique(out$top_markers) | ||
} | ||
term_map <- KGExplorer::map_ontology_terms(ont = ont, | ||
keep_order = FALSE, | ||
terms = terms_to_use)|> | ||
utils::head(top_n) | ||
term_map <- term_map[!is.na(names(term_map))] | ||
### Order ancestors by their clustered similarity | ||
d <- stats::dist(out$sim[names(term_map),]) | ||
hc <- stats::hclust(d) | ||
term_map <- term_map[hc$labels[hc$order]] | ||
|
||
fo <- Seurat::FeaturePlot(out$obj_sim, | ||
order = TRUE, | ||
min.cutoff = min.cutoff, | ||
features = names(term_map), | ||
...) | ||
fig_ontmarkers <- lapply(seq(length(term_map)), function(i){ | ||
fo_i <- fo[[i]] + | ||
ggplot2::labs(title=NULL, | ||
subtitle=paste(term_map[[i]], | ||
names(term_map)[[i]],sep="\n"), | ||
color="Ontological\nsimilarity" | ||
)+ | ||
phenomix::theme_nightlight() + | ||
phenomix::scale_color_nightlight() | ||
if(i>1){ | ||
fo_i <- fo_i + ggplot2::theme(legend.position = "none") | ||
} | ||
fo_i | ||
})|>patchwork::wrap_plots() + | ||
patchwork::plot_layout(axes = "collect", | ||
axis_titles = "collect", | ||
guides = "collect" | ||
) | ||
|
||
if(isTRUE(show_plot)) methods::show(fig_ontmarkers) | ||
#### Return #### | ||
return( | ||
list(plot=fig_ontmarkers, | ||
run_ontological_similarity=out, | ||
hclust=hc, | ||
term_map=term_map | ||
) | ||
) | ||
} |
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.