Skip to content

Commit

Permalink
nf scBarplot.CellsPerCluster
Browse files Browse the repository at this point in the history
bf AutoNumber.by.UMAP
  • Loading branch information
vertesy committed Oct 19, 2022
1 parent b5ac38f commit 3320e5a
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 28 deletions.
2 changes: 1 addition & 1 deletion CITATION.cff
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cff-version: 1.2.0
title: vertesy/Seurat.utils
version: v1.5.0
version: v1.5.2
message: >-
If you use this software, please cite it using these metadata.
type: software
Expand Down
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: Seurat.utils
Title: Seurat.utils - utility functions for Seurat
Version: 1.5.0
Version: 1.5.2
Authors@R:
person("Abel", "Vertesy", , "[email protected]", role = c("aut", "cre"))
Author: Abel Vertesy <[email protected]> [aut, cre]
Expand Down Expand Up @@ -51,6 +51,6 @@ Imports:
Suggests:
SoupX
Encoding: UTF-8
Packaged: 2022-10-19 13:57:07
Packaged: 2022-10-19 17:47:22
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.0
2 changes: 1 addition & 1 deletion Development/Create_the_Seurat.utils_Package.v0.1.0.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ require('CodeAndRoll2')

# Setup ------------------------
PackageName = "Seurat.utils"
package.version = "1.5.0"
package.version = "1.5.2"
setwd("~/GitHub/Packages/")

RepositoryDir = kollapse("~/GitHub/Packages/", PackageName, "/")
Expand Down
40 changes: 26 additions & 14 deletions Development/Development.bac
Original file line number Diff line number Diff line change
Expand Up @@ -348,17 +348,21 @@ DimPlot.ClusterNames <- function(obj = combined.obj # Plot UMAP with Cluster nam
#' }
#' @export
AutoNumber.by.UMAP <- function(obj = combined.obj # Relabel cluster numbers along a UMAP (or tSNE) axis
, dim = 1, swap= F, reduction="umap", res = "integrated_snn_res.0.5" ) {
, dim = 1, swap= F, reduction="umap", res = "RNA_snn_res.0.5" ) {

dim_name <- kppu(toupper(reduction),dim)
coord.umap <- as.named.vector(FetchData(object = obj, vars = dim_name))
ls.perCl <- split(coord.umap, f = obj[[res]])
identX <- as.character([email protected][[res]])

ls.perCl <- split(coord.umap, f = identX)
MedianClusterCoordinate <- unlapply(ls.perCl, median)

OldLabel <- names(sort(MedianClusterCoordinate, decreasing = swap))
NewLabel <- as.character(0:(length(MedianClusterCoordinate) - 1))
NewMeta <- translate(vec = obj[[res]], oldvalues = OldLabel, newvalues = NewLabel)
NewMeta <- translate(vec = identX, oldvalues = OldLabel, newvalues = NewLabel)
NewMetaCol <- kpp(res,"ordered")
iprint("NewMetaCol:",NewMetaCol)

obj[[NewMetaCol]] <- NewMeta
return(obj)
}
Expand Down Expand Up @@ -1583,10 +1587,9 @@ get.clustercomposition <- function(obj = combined.obj, ident = 'integrated_snn_r
#' remove.residual.small.clusters
#' @description E.g.: after subsetting often some residual cells remain in clusters originally denfined in the full dataset.
#' @param identitites Identities to scan for residual clusters
#' @param obj
#' @param obj Seurat object, Default: combined.obj
#' @param max.cells Max number of cells in cluster to be removed. Default: 0.5% of the dataset
#' @export
#' @examples

remove.residual.small.clusters <- function(identitites = GetOrderedClusteringRuns()
, obj = combined.obj
Expand Down Expand Up @@ -1623,7 +1626,6 @@ remove.residual.small.clusters <- function(identitites = GetOrderedClusteringRun
#' drop.levels.Seurat
#' @description Drop levels in clustering vectors in metadata (e.g. after subsetting)
#' @param obj Seurat object, Default: combined.obj
#' @examples
#' @export

drop.levels.Seurat <- function(obj = combined.obj) {
Expand Down Expand Up @@ -3464,9 +3466,16 @@ scBarplot.CellFractions <- function(obj = combined.obj
# _________________________________________________________________________________________________
#' @title scBarplot.CellsPerCluster
#' @description Barplot the Fraction of cells per cluster. (dupl?)
#'
#' @param obj Seurat object, Default: combined.obj
#' @param ident identity used, Default: 'cl.names.KnownMarkers.0.5'
#' @param sort PARAM_DESCRIPTION, Default: F
#' @param label True: displays cell count, but you can provide anything in a vector.
#' @param palette Color palette. Default: glasbey.
#' @param return_table Should it return the plotting data instead of the plot?
#' @param ... Pass any other parameter to the internally called functions (most of them should work).
#' @param sort Sort by cluster size? Default: F
#' @param suffix File name suffix
#'
#' @examples
#' \dontrun{
#' if(interactive()){
Expand All @@ -3477,21 +3486,25 @@ scBarplot.CellFractions <- function(obj = combined.obj

scBarplot.CellsPerCluster <- function(ident = GetOrderedClusteringRuns()[1]
, sort = F
, label = T
, label = list(T, 'percent')[[1]]
, suffix = if (label == 'percent') 'percent' else NULL
, palette = c("alphabet", "alphabet2", "glasbey", "polychrome", "stepped")[3]
, obj = combined.obj
, return_table = F
, ...) {
cell.per.cl <- obj[[ident]][,1]
cell.per.cluster <- (table(cell.per.cl))
if (sort) cell.per.cluster <- sort(cell.per.cluster)
lbl <- if (isFALSE(label)) NULL else if(isTRUE(label)) cell.per.cluster else label
lbl <- if (isFALSE(label)) { NULL
} else if (label == 'percent') { percentage_formatter(cell.per.cluster/sum(cell.per.cluster))
} else if (label == 'T') { cell.per.cluster
} else label

n.clusters <- length(cell.per.cluster)
if (return_table) {
cell.per.cluster
} else {
ggExpress::qbarplot(cell.per.cluster, subtitle = ident, suffix = ident
ggExpress::qbarplot(cell.per.cluster, subtitle = ident, suffix = kpp(ident, suffix)
, col = 1:n.clusters
, xlab.angle = 45
, label = lbl
Expand Down Expand Up @@ -5632,9 +5645,8 @@ IntersectWithExpressed <- function(genes, obj=combined.obj, genes.shown = 10) {
# _________________________________________________________________________________________________
#' seu.RemoveMetadata
#'
#' @param obj Seurat object
#' @param obj Seurat object, Default: combined.obj
#' @param cols_remove columns to remove
#' @example # seu.RemoveMetadata()
#' @export

seu.RemoveMetadata <- function(obj = combined.obj
Expand All @@ -5654,11 +5666,11 @@ seu.RemoveMetadata <- function(obj = combined.obj

# _________________________________________________________________________________________________
#' Percent.in.Trome
#' @description Gene expression as fraction of all UMI's
#' @description Gene expression as fraction of all UMI's
#' @param obj Seurat object
#' @param n.genes.barplot number of top genes shows
#' @param width.barplot barplot width
#' @return Seurat object
#' @return Seurat object
#' @export
#' @examples # combined.obj <- Percent.in.Trome()

Expand Down
29 changes: 22 additions & 7 deletions R/Seurat.Utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -348,17 +348,21 @@ DimPlot.ClusterNames <- function(obj = combined.obj # Plot UMAP with Cluster nam
#' }
#' @export
AutoNumber.by.UMAP <- function(obj = combined.obj # Relabel cluster numbers along a UMAP (or tSNE) axis
, dim = 1, swap= F, reduction="umap", res = "integrated_snn_res.0.5" ) {
, dim = 1, swap= F, reduction="umap", res = "RNA_snn_res.0.5" ) {

dim_name <- kppu(toupper(reduction),dim)
coord.umap <- as.named.vector(FetchData(object = obj, vars = dim_name))
ls.perCl <- split(coord.umap, f = obj[[res]])
identX <- as.character(obj@meta.data[[res]])

ls.perCl <- split(coord.umap, f = identX)
MedianClusterCoordinate <- unlapply(ls.perCl, median)

OldLabel <- names(sort(MedianClusterCoordinate, decreasing = swap))
NewLabel <- as.character(0:(length(MedianClusterCoordinate) - 1))
NewMeta <- translate(vec = obj[[res]], oldvalues = OldLabel, newvalues = NewLabel)
NewMeta <- translate(vec = identX, oldvalues = OldLabel, newvalues = NewLabel)
NewMetaCol <- kpp(res,"ordered")
iprint("NewMetaCol:",NewMetaCol)

obj[[NewMetaCol]] <- NewMeta
return(obj)
}
Expand Down Expand Up @@ -3462,9 +3466,16 @@ scBarplot.CellFractions <- function(obj = combined.obj
# _________________________________________________________________________________________________
#' @title scBarplot.CellsPerCluster
#' @description Barplot the Fraction of cells per cluster. (dupl?)
#'
#' @param obj Seurat object, Default: combined.obj
#' @param ident identity used, Default: 'cl.names.KnownMarkers.0.5'
#' @param sort PARAM_DESCRIPTION, Default: F
#' @param label True: displays cell count, but you can provide anything in a vector.
#' @param palette Color palette. Default: glasbey.
#' @param return_table Should it return the plotting data instead of the plot?
#' @param ... Pass any other parameter to the internally called functions (most of them should work).
#' @param sort Sort by cluster size? Default: F
#' @param suffix File name suffix
#'
#' @examples
#' \dontrun{
#' if(interactive()){
Expand All @@ -3475,21 +3486,25 @@ scBarplot.CellFractions <- function(obj = combined.obj

scBarplot.CellsPerCluster <- function(ident = GetOrderedClusteringRuns()[1]
, sort = F
, label = T
, label = list(T, 'percent')[[1]]
, suffix = if (label == 'percent') 'percent' else NULL
, palette = c("alphabet", "alphabet2", "glasbey", "polychrome", "stepped")[3]
, obj = combined.obj
, return_table = F
, ...) {
cell.per.cl <- obj[[ident]][,1]
cell.per.cluster <- (table(cell.per.cl))
if (sort) cell.per.cluster <- sort(cell.per.cluster)
lbl <- if (isFALSE(label)) NULL else if(isTRUE(label)) cell.per.cluster else label
lbl <- if (isFALSE(label)) { NULL
} else if (label == 'percent') { percentage_formatter(cell.per.cluster/sum(cell.per.cluster))
} else if (label == 'T') { cell.per.cluster
} else label

n.clusters <- length(cell.per.cluster)
if (return_table) {
cell.per.cluster
} else {
ggExpress::qbarplot(cell.per.cluster, subtitle = ident, suffix = ident
ggExpress::qbarplot(cell.per.cluster, subtitle = ident, suffix = kpp(ident, suffix)
, col = 1:n.clusters
, xlab.angle = 45
, label = lbl
Expand Down
2 changes: 1 addition & 1 deletion man/AutoNumber.by.UMAP.Rd

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

15 changes: 13 additions & 2 deletions man/scBarplot.CellsPerCluster.Rd

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

0 comments on commit 3320e5a

Please sign in to comment.