Skip to contents

Z-score normalization scales each feature (gene) across all cells to have a mean of 0 and a standard deviation of 1. This function allows for optional weighted univariance normalization, where cells are grouped by a categorical variable.

Usage

Znorm(mat, groups = NULL)

Arguments

mat

A matrix of gene expression data, where rows represent genes and columns represent cells.

groups

A character vector specifying the group labels for each cell. If provided, weighted univariance normalization will be performed.

Value

A matrix of Z-score normalized gene expression data, with rows representing genes and columns representing cells.

Examples

library(Seurat)
library(data.table)
library(SpatialEcoTyper)
scdata <- fread("https://spatialecotyper.stanford.edu/inc/inc.public.vignettes.php?file=Melanoma1_subset_counts.tsv.gz",
                sep = "\t",header = TRUE, data.table = FALSE)
rownames(scdata) <- scdata[, 1]
scdata <- as.matrix(scdata[, -1])
tmpobj <- CreateSeuratObject(scdata) %>% NormalizeData(verbose = FALSE)
#> Warning: Data is of class matrix. Coercing to dgCMatrix.
normdata <- GetAssayData(tmpobj, layer = "data")
# Z-score normalization
znorm_data <- Znorm(normdata)
#> Centering and scaling data matrix
head(znorm_data[, 1:5])
#>          HumanMelanomaPatient1__cell_3655 HumanMelanomaPatient1__cell_3657
#> PDK4                           -0.3542511                        3.0673518
#> TNFRSF17                       -0.1114269                       -0.1114269
#> ICAM3                          -0.4726938                       -0.4726938
#> FAP                             3.2406352                       -0.2911087
#> GZMB                           -0.2421502                       -0.2421502
#> TSC2                           -0.1322335                       -0.1322335
#>          HumanMelanomaPatient1__cell_3658 HumanMelanomaPatient1__cell_3660
#> PDK4                            2.2468524                       -0.3542511
#> TNFRSF17                       -0.1114269                       -0.1114269
#> ICAM3                          -0.4726938                       -0.4726938
#> FAP                            -0.2911087                       -0.2911087
#> GZMB                           -0.2421502                       -0.2421502
#> TSC2                           -0.1322335                       -0.1322335
#>          HumanMelanomaPatient1__cell_3661
#> PDK4                           -0.3542511
#> TNFRSF17                       -0.1114269
#> ICAM3                          -0.4726938
#> FAP                            -0.2911087
#> GZMB                           -0.2421502
#> TSC2                           -0.1322335

# Weighted Z-score normalization
scmeta <- fread("https://spatialecotyper.stanford.edu/inc/inc.public.vignettes.php?file=Melanoma1_subset_scmeta.tsv",
                sep = "\t",header = TRUE, data.table = FALSE)
wtdznorm_data <- Znorm(normdata, groups = scmeta$Region)
head(wtdznorm_data[, 1:5])
#> 6 x 5 Matrix of class "dgeMatrix"
#>          HumanMelanomaPatient1__cell_3655 HumanMelanomaPatient1__cell_3657
#> PDK4                           -0.3030397                        2.7017739
#> TNFRSF17                       -0.0982258                       -0.0982258
#> ICAM3                          -0.4115313                       -0.4115313
#> FAP                             2.8162990                       -0.2517507
#> GZMB                           -0.2150584                       -0.2150584
#> TSC2                           -0.1161900                       -0.1161900
#>          HumanMelanomaPatient1__cell_3658 HumanMelanomaPatient1__cell_3660
#> PDK4                            1.9812205                       -0.3030397
#> TNFRSF17                       -0.0982258                       -0.0982258
#> ICAM3                          -0.4115313                       -0.4115313
#> FAP                            -0.2517507                       -0.2517507
#> GZMB                           -0.2150584                       -0.2150584
#> TSC2                           -0.1161900                       -0.1161900
#>          HumanMelanomaPatient1__cell_3661
#> PDK4                           -0.3030397
#> TNFRSF17                       -0.0982258
#> ICAM3                          -0.4115313
#> FAP                            -0.2517507
#> GZMB                           -0.2150584
#> TSC2                           -0.1161900