Skip to content

Commit

Permalink
upload
Browse files Browse the repository at this point in the history
  • Loading branch information
multitalk committed Apr 5, 2022
1 parent f962e66 commit b441a3a
Show file tree
Hide file tree
Showing 7 changed files with 999 additions and 204 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: SpaTalk
Title: Infer Cell-Cell Communication for Spatial Transcriptomics
Version: 1.0
Depends: R (>= 4.0.0)
Depends: R (>= 4.0.0), ggalluvial
Authors@R:
person("Xin", "Shao", , "xin_shao@zju.edu.cn", role = c("aut", "cre"),
comment = c(ORCID = "0000-0002-1928-3878"))
Expand Down
15 changes: 7 additions & 8 deletions R/methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,7 @@ dec_celltype <- function(object, sc_data, sc_celltype, min_percent = 0.5, min_nF
}
st_ndata <- st_ndata[genename, ]
sc_ndata <- sc_ndata[genename, ]
sc_celltype <- data.frame(cell = colnames(sc_ndata), celltype = sc_celltype,
stringsAsFactors = F)
sc_celltype <- data.frame(cell = colnames(sc_ndata), celltype = sc_celltype, stringsAsFactors = F)
sc_celltype$celltype <- stringr::str_replace_all(sc_celltype$celltype, pattern = "-",
replacement = "_")
# if_use_all_cores
Expand Down Expand Up @@ -278,8 +277,8 @@ find_lr_path <- function(object, lrpairs, pathways, max_hop = NULL) {
st_data <- .get_st_data(object)
species <- object@para$species
lrpair <- lrpairs[lrpairs$species == species, ]
lrpair <- lrpair[lrpair$ligand %in% rownames(st_data) & lrpair$receptor %in%
rownames(st_data), ]
lrpair <- lrpair[lrpair$ligand %in% rownames(st_data) & lrpair$receptor %in% rownames(st_data),
]
if (nrow(lrpair) == 0) {
stop("No ligand-recepotor pairs found in st_data!")
}
Expand Down Expand Up @@ -361,8 +360,8 @@ find_lr_path <- function(object, lrpairs, pathways, max_hop = NULL) {
#' @importFrom crayon cyan green
#' @export

dec_cci <- function(object, celltype_sender, celltype_receiver, n_neighbor = 10,
min_pairs = 5, min_pairs_ratio = 0, per_num = 1000, pvalue = 0.05, co_exp_ratio = 0.1) {
dec_cci <- function(object, celltype_sender, celltype_receiver, n_neighbor = 10, min_pairs = 5,
min_pairs_ratio = 0, per_num = 1000, pvalue = 0.05, co_exp_ratio = 0.1) {
# check input data
if (!is(object, "SpaTalk")) {
stop("Invalid class for object: must be 'SpaTalk'!")
Expand Down Expand Up @@ -482,8 +481,8 @@ dec_cci_all <- function(object, n_neighbor = 10, min_pairs = 5, min_pairs_ratio
### [2] Downstream targets and TFs
max_hop <- object@para$max_hop
if (nrow(lrdb) > 0) {
receptor_tf <- .get_tf_res(celltype_sender, celltype_receiver,
lrdb, ggi_tf, cell_pair, st_data, max_hop, co_exp_ratio)
receptor_tf <- .get_tf_res(celltype_sender, celltype_receiver, lrdb,
ggi_tf, cell_pair, st_data, max_hop, co_exp_ratio)
if (!is.null(receptor_tf)) {
# calculate score
lrdb <- .get_score(lrdb, receptor_tf)
Expand Down
296 changes: 130 additions & 166 deletions R/plot.R

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,7 @@
if (nrow(ggi_hop_yes) > 0) {
ggi_hop_tf <- ggi_res[ggi_res$hop == k + 1, ]
if (nrow(ggi_hop_tf) > 0) {
ggi_hop_yes <- ggi_hop_yes[ggi_hop_yes$dest %in% ggi_hop_tf$src,
]
ggi_hop_yes <- ggi_hop_yes[ggi_hop_yes$dest %in% ggi_hop_tf$src, ]
if (nrow(ggi_hop_yes) > 0) {
tf_gene <- ggi_hop_yes$hop
names(tf_gene) <- ggi_hop_yes$dest
Expand Down
205 changes: 178 additions & 27 deletions vignettes/sc_tutorial.Rmd
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
---
title: "SpaTalk tutorial (single-cell ST data)"
title: "SpaTalk tutorial (spot-based ST data)"
author: "Xin Shao"
date: "`r Sys.Date()`"
output:
prettydoc::html_pretty:
theme: cayman
highlight: github
vignette: >
%\VignetteIndexEntry{SpaTalk tutorial (single-cell ST data)}
%\VignetteIndexEntry{SpaTalk tutorial (spot-based ST data)}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---

## **Preprocess**
## **Main**

Before create SpaTalk object, we suggest to revise gene symbols of ST data `matrix` and scRNA-seq data `matrix` using `rev_gene()`
After pre-processing, we create a SpaTalk object with st_data and st_meta for single-cell ST data by setting `if_st_is_sc` and `spot_max_cell` as `TRUE` and `1`

```{r load st data, echo=TRUE}
library(SpaTalk)
Expand All @@ -23,45 +23,196 @@ library(SpaTalk)
load(paste0(system.file(package = 'SpaTalk'), "/extdata/starmap_data.rda"))
load(paste0(system.file(package = 'SpaTalk'), "/extdata/starmap_meta.rda"))
# revise gene symbols according to the NCNI gene symbols
starmap_data <- rev_gene(data = as.matrix(starmap_data),
data_type = "count",
species = "Mouse",
geneinfo = geneinfo)
# create SpaTalk data
obj <- createSpaTalk(st_data = as.matrix(starmap_data),
st_meta = starmap_meta[, -4],
species = "Mouse",
if_st_is_sc = T,
spot_max_cell = 1)
```

## **Cell-type decomposition**

## **Note**
First, we apply [NNLM](https://github.com/linxihui/NNLM) and spatial mapping to reconstruct the ST data at single-cell resolution using `dec_celltype()`

```{r cell-type decomposition, echo=TRUE}
# decode the cell-type composition
obj <- dec_celltype(object = obj,
sc_data = as.matrix(starmap_data),
sc_celltype = starmap_meta$celltype)
```

### Use `plot_st_celltype_percent()` to view cell-type percent

```{r plot_st_celltype_percent, fig.width=6, fig.height=6, fig.align='center', echo=TRUE}
# plot cell-type percent across spatial cells
plot_st_celltype_percent(object = obj, celltype = 'eL2_3',size = 3)
```

### Use `plot_st_gene()` to view gene expression

```{r plot_st_gene, fig.width=6, fig.height=6, fig.align='center', echo=TRUE}
# plot marker gene expression across spatial cells
plot_st_gene(object = obj, gene = 'Lamp5',size = 3, if_use_newmeta = F)
```

### Use `plot_st_cor_heatmap()` to view correlation heatmap

```{r plot_st_cor_heatmap, fig.width=7, fig.height=6, fig.align='center', echo=TRUE}
# correlation between marker gene expression and cell type percent across spatial cells
plot_st_cor_heatmap(object = obj,
marker_genes = c("Plp1","Vip","Sst","Lamp5","Pcp4","Mfge8","Pvalb"),
celltypes = c("Oligo","VIP","SST","eL2_3","eL6","Astro","PVALB"),
scale = "none",
color_low = 'blue',
color_high = 'yellow',
color_mid = 'yellow')
```

In order to guide users on how to create the right data type, we provide several `demo_data` to follow:
## **ST at single-cell resolution**
### Use `plot_st_celltype()` to view cell type in space

```{r demo data, echo=TRUE}
# demo spot-based ST data
demo_st_data()
```{r plot_st_celltype, fig.width=6, fig.height=6, fig.align='center', echo=TRUE}
# plot cell type in reconstructed ST atlas
plot_st_celltype(object = obj, celltype = 'eL2_3')
# demo spot-based ST meta
demo_st_meta()
```

# demo single-cell ST data
demo_st_sc_data()
### Use `plot_st_gene()` to view gene expression in space

# demo single-cell ST meta
demo_st_sc_meta()
```{r plot_st_gene2, fig.width=6, fig.height=6, fig.align='center', echo=TRUE}
# plot marker gene expression in single-cell ST data
plot_st_gene(object = obj,gene = 'Lamp5', if_use_newmeta = T)
# demo scRNA-seq data
demo_sc_data()
```

# demo geneinfo
demo_geneinfo()
### Use `plot_st_celltype_density()` to view cell-type density in space

```{r plot_st_celltype_density, fig.width=6, fig.height=6, fig.align='center', echo=TRUE}
# plot cell-type density in single-cell ST data
plot_st_celltype_density(object = obj,
celltype = 'eL2_3',
type = 'raster',
color_low = 'purple',
color_high = 'yellow')
plot_st_celltype_density(object = obj,
celltype = 'eL2_3',
type = 'contour',
color_low = 'purple',
color_high = 'yellow')
```

### Use `plot_st_celltype_all()` to view all cell types in space

```{r plot_st_celltype_all, fig.width=6, fig.height=6, fig.align='center', echo=TRUE}
# plot all cell types in single-cell ST data
plot_st_celltype_all(object = obj,size = 2)
```

After revising gene symbols, we create a SpaTalk object with st_data and st_meta. Given the two types of ST data, namely single-cell and spot-based ST data, we provided two standard processing examples. Please refer to:
## **Infer cell-cell communications**
Based on the single-cell ST data, we use `find_lr_path()` and `dec_cci()` to infer cell-cell communications mediated by ligand-receptor interactions (LRIs) in space using [LRIs from CellTalkDB](http://tcm.zju.edu.cn/celltalkdb/), pathways from [KEGG](https://www.genome.jp/kegg/pathway.html) and [Reactome](https://reactome.org/), and transcriptional factors (TFs) from [AnimalTFDB](http://bioinfo.life.hust.edu.cn/AnimalTFDB/#!/). In practise, we use the real cell type of STARmap

```{r dec_cci, echo=TRUE}
obj@meta$rawmeta$celltype <- starmap_meta$celltype
# Filter LRIs with downstream targets
obj <- find_lr_path(object = obj, lrpairs = lrpairs, pathways = pathways)
## [**`single-cell tutorial vignette`**](https://raw.githack.com/ZJUFanLab/SpaTalk/main/vignettes/sc_tutorial.pdf)
# Infer cell-cell communications from SST to PVALB neurons
obj <- dec_cci(object = obj,
celltype_sender = 'eL5',
celltype_receiver = 'Astro')
## [**`spot-based tutorial vignette`**](https://raw.githack.com/ZJUFanLab/SpaTalk/main/vignettes/spot_tutorial.pdf)
# Get LR and downstream pathways
obj_lr_path <- get_lr_path(object = obj,
celltype_sender = 'eL5',
celltype_receiver = 'Astro',
ligand = 'Cort',
receptor = 'Sstr2')
obj_lr_path$tf_path
obj_lr_path$path_pvalue
```

### Use `plot_ccdist()` to view distribution of senders and receivers

```{r plot_ccdist, fig.width=6, fig.height=6, fig.align='center', echo=TRUE}
# Point plot with spatial distribution of celltype_sender and celltype_receiver
plot_ccdist(object = obj, celltype_sender = 'eL5', celltype_receiver = 'Astro')
```

### Use `plot_cci_lrpairs()` to view all LRIs of senders and receivers

```{r plot_cci_lrpairs, fig.width=3, fig.height=3, fig.align='center', echo=TRUE}
# Heatmap with LR pairs of celltype_sender and celltype_receiver
plot_cci_lrpairs(object = obj, celltype_sender = 'eL5', celltype_receiver = 'Astro')
```

### Use `plot_lrpair()` to view the specific LRI

```{r plot_lrpair, fig.width=6, fig.height=6, fig.align='center', echo=TRUE}
# Point plot with LR pair from celltype_sender to celltype_receiver
plot_lrpair(object = obj,
celltype_sender = 'eL5',
ligand = 'Cort',
celltype_receiver = 'Astro',
receptor = 'Sstr2',
if_plot_density = F)
```

### Use `plot_lrpair_vln()` to view violin plot with spatial distance of LRI

```{r plot_lrpair_vln, fig.width=8, fig.height=6, fig.align='center', echo=TRUE}
# Violin plot with spatial distance of LR pair between senders and receivers and between all cell-cell pairs
plot_lrpair_vln(object = obj,
celltype_sender = 'eL5',
ligand = 'Cort',
celltype_receiver = 'Astro',
receptor = 'Sstr2')
```

### Use `plot_lr_path()` to view network with LR and downstream pathways

```{r plot_lr_path, fig.width=6, fig.height=6, fig.align='center', echo=TRUE}
# Plot network with LR and downstream pathways
plot_lr_path(object = obj,
celltype_sender = 'eL5',
ligand = 'Cort',
celltype_receiver = 'Astro',
receptor = 'Sstr2')
```

### Use `plot_path2gene()` to view river plot of significantly activated pathways

```{r plot_path2gene, fig.width=8, fig.height=6, fig.align='center', echo=TRUE}
# River plot of significantly activated pathways and related downstream genes of receptors
library(ggalluvial)
plot_path2gene(object = obj,
celltype_sender = 'eL5',
ligand = 'Cort',
celltype_receiver = 'Astro',
receptor = 'Sstr2')
```

## **Note**

To infer all paired cell-cell communications, use `dec_cci_all()` instead of `dec_cci()`

```{r dec_cci_all, echo=TRUE}
# Infer cell-cell communications from SST to PVALB neurons
# obj <- dec_cci_all(object = obj)
```

```{r sessionInfo, echo=TRUE}
sessionInfo()
Expand Down
Loading

0 comments on commit b441a3a

Please sign in to comment.