Skip to contents

Compute Cell-Type-Specific Average Expression of Spatial Clusters

Usage

ComputeAvgs(
  normdata,
  scmeta,
  cluster = "SE",
  Region = NULL,
  scale = TRUE,
  ncores = 4
)

Arguments

normdata

Numeric matrix of normalized expression data, where rows represent genes and columns represent cells.

scmeta

Data frame containing metadata associated with each cell, including spatial cluster and cell type annotations.

cluster

Character string specifying the column name in 'scmeta' containing spatial cluster annotations.

Region

Character string specifying the column name in metadata data frames containing region annotations (default: NULL).

scale

A boolean specifying whether to do univariance normalization.

ncores

Integer specifying the number of CPU cores to use for parallel processing.

Value

A matrix of average expression, where rows represent genes and columns represent spatial clusters from the sample.

Examples

library(data.table)
library(Seurat)
library(SpatialEcoTyper)
library(googledrive)
drive_deauth() # no Google sign-in is required
drive_download(as_id("1CoQmU3u8MoVC8RbLUvTDQmOuJJ703HHB"),
              "HumanMelanomaPatient1_subset_counts.tsv.gz", overwrite = TRUE)
#> File downloaded:
#>HumanMelanomaPatient1_subset_counts.tsv.gz
#>   <id: 1CoQmU3u8MoVC8RbLUvTDQmOuJJ703HHB>
#> Saved locally as:
#>HumanMelanomaPatient1_subset_counts.tsv.gz
drive_download(as_id("1nSPj2zRywFUdbo1fwiz77ds4NuM6bmV2"),
              "Melanoma1_subset_SpatialEcoTyper_results.rds", overwrite = TRUE)
#> Error in map(as_id(id), get_one_file_id):  In index: 1.
#> Caused by error in `.f()`:
#> ! Client error: (404) Not Found
#> File not found: 1nSPj2zRywFUdbo1fwiz77ds4NuM6bmV2.
#>  message: File not found: 1nSPj2zRywFUdbo1fwiz77ds4NuM6bmV2.
#>  domain: global
#>  reason: notFound
#>  location: fileId
#>  locationType: parameter
scdata <- fread("HumanMelanomaPatient1_subset_counts.tsv.gz",
                sep = "\t",header = TRUE, data.table = FALSE)
rownames(scdata) <- scdata[, 1]
scdata <- as.matrix(scdata[, -1])
tmpobj <- CreateSeuratObject(scdata) %>%
        SCTransform(clip.range = c(-10, 10), verbose = FALSE)
#> Warning: Data is of class matrix. Coercing to dgCMatrix.
seurat_version = as.integer(gsub("\\..*", "", as.character(packageVersion("SeuratObject"))))
if(seurat_version<5){
  normdata <- GetAssayData(tmpobj, "data")
}else{
  normdata <- tmpobj[["SCT"]]$data
}
metadata = readRDS("Melanoma1_subset_SpatialEcoTyper_results.rds")$metadata

# Construct cell-type-specific gene expression signatures of SEs
avgexprs <- ComputeAvgs(normdata = normdata, scmeta = metadata)
head(avgexprs)
#>                               SE5         SE7         SE0         SE4
#> Fibroblast..PDK4      0.110804267  0.19775972  0.13120621 -0.04927833
#> Fibroblast..TNFRSF17  0.000000000  0.00000000  0.00000000  0.00000000
#> Fibroblast..ICAM3     0.000000000  0.00000000  0.00000000  0.00000000
#> Fibroblast..FAP       0.118792445 -0.13868930  0.05233653  0.03938155
#> Fibroblast..GZMB     -0.031686314 -0.07422975 -0.03297410  0.02567394
#> Fibroblast..TSC2     -0.001888106 -0.03091562 -0.02679740 -0.02925629
#>                              SE6         SE2          SE1         SE3
#> Fibroblast..PDK4     -0.13236092 -0.17959104 -0.025478747  0.01853931
#> Fibroblast..TNFRSF17  0.00000000  0.00000000  0.000000000  0.00000000
#> Fibroblast..ICAM3     0.00000000  0.00000000  0.000000000  0.00000000
#> Fibroblast..FAP       0.05003947 -0.10671133  0.035710822 -0.07657463
#> Fibroblast..GZMB      0.00161892  0.02988412 -0.001820212  0.04700560
#> Fibroblast..TSC2     -0.02869510 -0.05265459  0.117406740  0.13560073