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.3542504                        3.0670998
#> TNFRSF17                       -0.1114269                       -0.1114269
#> ICAM3                          -0.4726938                       -0.4726938
#> FAP                             3.2404122                       -0.2911077
#> GZMB                           -0.2119762                       -0.2119762
#> TSC2                           -0.1322338                       -0.1322338
#>          HumanMelanomaPatient1__cell_3658 HumanMelanomaPatient1__cell_3660
#> PDK4                            2.2466611                       -0.3542504
#> TNFRSF17                       -0.1114269                       -0.1114269
#> ICAM3                          -0.4726938                       -0.4726938
#> FAP                            -0.2911077                       -0.2911077
#> GZMB                           -0.2119762                       -0.2119762
#> TSC2                           -0.1322338                       -0.1322338
#>          HumanMelanomaPatient1__cell_3661
#> PDK4                           -0.3542504
#> TNFRSF17                       -0.1114269
#> ICAM3                          -0.4726938
#> FAP                            -0.2911077
#> GZMB                           -0.2119762
#> TSC2                           -0.1322338

# 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.3030389                        2.7015322
#> TNFRSF17                       -0.0982258                       -0.0982258
#> ICAM3                          -0.4115313                       -0.4115313
#> FAP                             2.8160957                       -0.2517499
#> GZMB                           -0.1879627                       -0.1879627
#> TSC2                           -0.1161902                       -0.1161902
#>          HumanMelanomaPatient1__cell_3658 HumanMelanomaPatient1__cell_3660
#> PDK4                            1.9810370                       -0.3030389
#> TNFRSF17                       -0.0982258                       -0.0982258
#> ICAM3                          -0.4115313                       -0.4115313
#> FAP                            -0.2517499                       -0.2517499
#> GZMB                           -0.1879627                       -0.1879627
#> TSC2                           -0.1161902                       -0.1161902
#>          HumanMelanomaPatient1__cell_3661
#> PDK4                           -0.3030389
#> TNFRSF17                       -0.0982258
#> ICAM3                          -0.4115313
#> FAP                            -0.2517499
#> GZMB                           -0.1879627
#> TSC2                           -0.1161902