Skip to content

Latest commit

 

History

History
executable file
·
687 lines (573 loc) · 78.5 KB

7.Human_breast_cancer.md

File metadata and controls

executable file
·
687 lines (573 loc) · 78.5 KB

Using SpaTrio to reconstruct and analyze single-cell multi-omics data of human breast cancer

Library required R packages

library(Seurat)
## Attaching SeuratObject
library(SpaTrio)
library(ggsci)
library(ggplot2)
library(readr)
library(reshape2)
library(dplyr)
## 
## Attaching package: 'dplyr'

## The following objects are masked from 'package:stats':
## 
##     filter, lag

## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union

Load datasets

We collected single-cell multi-omics data and spatial transcriptome data of human breast cancer. Datasets were obtained from Zenodo data repository and Gene Expression Omnibus

spatial_object <- readRDS("data/Human_breast_cancer/spatial_object.rds")
multi_object <- readRDS("data/Human_breast_cancer/multi_object.rds")
dim(multi_object)
## [1] 27719  6543
dim(spatial_object)
## [1] 19237   357

UMAP projection of input single-cell multi-omics data colored by the RNA annotation.

major_col<-c("T-cells"= "#BC3C29FF",
"PVL"   =   "#0072B5FF" ,
"Endothelial"   =   "#E18727FF" ,
"CAFs"  =   "#20854EFF" ,
"Myeloid"   =   "#7876B1FF" ,
"Plasmablasts"  =   "#6F99ADFF",
"B-cells"   =   "#FFDC91FF")
multi_object$celltype_major=factor(multi_object$celltype_major,levels = rev(names(sort(table(multi_object$celltype_major)))))
Idents(multi_object)<-multi_object$celltype_major
DimPlot(multi_object,cols = major_col)

Spatial plot of input ST data colored by pathological regions.

class_level<-c("Invasive cancer","Invasive cancer + lymphocytes","Lymphocytes","Stroma","Artefact","Uncertain")
class_col<-c("Invasive cancer"= "#3B4992FF",
"Invasive cancer + lymphocytes" =   "#EE0000FF" ,
"Lymphocytes"   =   "#008B45FF" ,
"Stroma"    =   "#631879FF" ,
"Artefact"  =   "#008280FF" ,
"Uncertain" =   "#BB0021FF" )
spatial_object$classification=factor(spatial_object$classification,levels = class_level)
Idents(spatial_object)<-spatial_object$classification
sdplot(spatial_object,pt.size.factor = 4,image.alpha = 1)+scale_fill_manual(values = class_col)
## Scale for fill is already present.
## Adding another scale for fill, which will replace the existing scale.

Run SpaTrio

We enter the following data into SpaTrio, which will build spatial maps of single cells

  • Gene expression count matrix of cells and spots (multi_rna.csv & spatial_rna.csv)
  • Cluster/Cell-type information of cells and spots (multi_meta.csv & spatial_meta.csv)
  • Low-dimensional representation of protein assay (emb.csv)
  • Spatial position coordinates of spots (pos.csv)
spatrio(spatrio_path="/home/yph/SpaTrio-main",
        py_path="/home/yph/anaconda3/envs/spatrio/bin/python",
        input_path="data/Human_breast_cancer",
        output_path="data/Human_breast_cancer",
        top_num =15)
## [1] "Using the Python interpreter with a path of /home/yph/anaconda3/envs/spatrio/bin/python"

Results of SpaTrio

Read in output and create single-cell spatial multi-omics data reconstructed by SpaTrio.

output <- read_csv("data/Human_breast_cancer/output.csv")
## New names:
## Rows: 5355 Columns: 10
## ── Column specification
## ──────────────────────────────────────────────────────── Delimiter: "," chr
## (4): spot, cell, spot_type, cell_type dbl (6): ...1, value, x, y, Cell_xcoord,
## Cell_ycoord
## ℹ Use `spec()` to retrieve the full column specification for this data. ℹ
## Specify the column types or set `show_col_types = FALSE` to quiet this message.
## • `` -> `...1`
pred<-subset(multi_object,cell=output$cell)
coordinate = output[,c("Cell_xcoord","Cell_ycoord")]
coordinate<-as.data.frame(coordinate)
colnames(coordinate)<-c("x","y")
rownames(coordinate)<-output$cell
pred@images[["slice1"]]=NULL
pred@images$image =  new(
Class = 'SlideSeq',
assay = "RNA",
key = "image_",
coordinates = coordinate)
output<-as.data.frame(output)
rownames(output)<-output$cell
pred<-AddMetaData(pred, output[,2:10])

# Modify protein name
adt_count<-as.data.frame(pred@assays$ADT@counts)
adt_count$adt<-rownames(adt_count)
adt_count$adt<-gsub(".1","",adt_count$adt,fixed=T)
adt_count$adt<-gsub(".2","",adt_count$adt,fixed=T)
adt_count<-aggregate(x=adt_count[,1:c(ncol(adt_count)-1)], by=list(adt_count$adt),sum)
index<-adt_count$Group.1
adt_count<-adt_count[,2:ncol(adt_count)]
rownames(adt_count)<-index
pred[['ADT']]<-CreateAssayObject(adt_count)

Spatial plot of single-cell spatial multi-omics data reconstructed by SpaTrio.

pred$celltype_major=factor(pred$celltype_major,levels = rev(names(sort(table(pred$celltype_major)))))
Idents(pred)<-pred$celltype_major
major_col<-c("T-cells"= "#BC3C29FF",
"PVL"   =   "#0072B5FF" ,
"Endothelial"   =   "#E18727FF" ,
"CAFs"  =   "#20854EFF" ,
"Myeloid"   =   "#7876B1FF" ,
"Plasmablasts"  =   "#6F99ADFF",
"B-cells"   =   "#FFDC91FF")
sdplot(pred,pt.size.factor = 5,image.alpha = 1)+scale_fill_manual(values = major_col)+NoLegend()
## Scale for fill is already present.
## Adding another scale for fill, which will replace the existing scale.

Histogram of cell proportions

pred$spot_type=factor(pred$spot_type,levels = class_level)
cell.prop<-as.data.frame(prop.table(table(pred$celltype_major, pred$spot_type),2))
colnames(cell.prop)<-c("cluster","sample","proportion")
cell.prop<-cell.prop[cell.prop$sample%in%c("Invasive cancer","Invasive cancer + lymphocytes","Lymphocytes","Stroma"),]
ggplot(cell.prop,aes(x=sample,y=proportion,fill=cluster))+geom_bar(stat="identity",position="fill",width = 0.9)+theme_bw(base_rect_size=1.5)+theme(panel.grid =element_blank())+FontSize(x.title = 15, y.title = 15,y.text=15,x.text=15)+theme(legend.text = element_text(size=15))+theme(legend.key = element_rect(color = NA, fill = NA),legend.key.size = unit(1, "cm"))+labs(x = "Sample", y = "Proportion")+guides(fill=guide_legend(title=NULL))+ theme(axis.text.x = element_text(color ="black",angle = 45,hjust = 1),axis.text.y  = element_text(color ="black"))+scale_fill_manual(values = major_col)

Spatial protein module analysis of T cells

T cells play an important role in the tumor microenvironment, and occupy a high proportion of cells in most areas, so we first focus on T cells for analysis.

Idents(pred)<-pred$celltype_major
tcell<-subset(pred,ident="T-cells")
Idents(tcell)<-tcell$spot_type
adt<-subset(tcell,ident=c("Invasive cancer + lymphocytes","Invasive cancer","Lymphocytes","Stroma"))
adt@reductions[["apca"]]=NULL
adt@reductions[["pca"]]=NULL
DefaultAssay(adt)<-"ADT"
VariableFeatures(adt)<-rownames(adt)
adt<-NormalizeData(adt, normalization.method = "CLR", margin = 2)%>%ScaleData()%>%
RunPCA(npcs = 10,reduction.name = "apca")%>%FindNeighbors(reduction = "apca", dims = 1:10)%>%
RunUMAP(reduction = "apca",dims = 1:10)
## Normalizing across cells

## Centering and scaling data matrix

## PC_ 1 
## Positive:  KLRG1, HER2, OX40, TIM-3, CD1C, CCR6, CCR4, CRTH2, TCRgd, CD62L 
##     CD34, CD133, FAS, CD11b, CXCR5, LFA-3, CD163, GITR, CD66b, CD28 
##     CXCR3, CD14, TACTILE, CCR7, PD-L2, ICOS, CR2, EGFR, BTLA, CD10 
## Negative:  B7-H4, CD45, CD127, CD40, CD3, PD-1, CD4, CD15, IgG1, CD45RO 
##     PD-L1, CD86, CD8a, TIGIT, CD19, CD45RA, CD2, TCRab, CD69, CD57 
##     CD48, CD44, IgG2a, EPCAM, 2B4, CD1d, CD103, MHCII, CD80, CD49a 
## PC_ 2 
## Positive:  CD2, CD48, CD5, TCRab, CD69, CD4, CD39, CD45RO, CD3, PD-1 
##     ICOS, CD49d, CD1d, FAS, CD27, TCR-V24-J18, MHCII, CD44, CD45, CD103 
##     CD49f, CCR6, CD73, CD127, CD49a, OX40, KLRG1, GITR, CD141, Thy-1 
## Negative:  CD45RA, CTLA-4, IgG2a, CD8a, PD-L1, IgG1, CD14, IgG2b, CD15, TIGIT 
##     CD19, CD40, CD86, CD11b, CD25, PDGFRA, PD-L2, B7-H4, CD24, c-KIT 
##     ICOSL, EGFR, CCR7, CD66b, BTLA, TCRVa7-2, HVEM, CD163, CD34, CR2 
## PC_ 3 
## Positive:  CD103, 2B4, CD49a, CD69, CD57, MHCII, CD45RA, CD8a, CD49d, CD11C 
##     CD2, NKG2D, CD49b, CD39, CD24, CD48, CD31, Thy-1, CD44, CD38 
##     CD86, ICOS, CD27, PD-L1, EPCAM, CD25, PDGFRb, IgG2a, CD11b, PDGFRA 
## Negative:  CD19, CD4, CD45RO, CD28, CD3, CD5, CD127, CD1d, PD-1, CD49f 
##     CD1C, TIM-3, KLRG1, FAS-L, OX40, CR2, HER2, CCR7, BTLA, CCR4 
##     CD62L, CCR6, CD40, LFA-3, 4-1BB, TIGIT, TACTILE, TCRgd, CD138, CD133 
## PC_ 4 
## Positive:  Thy-1, MHCII, CD24, CD11C, CD49b, CD31, CD49f, CCR6, CD19, CD1d 
##     CD127, B7H3, CD3, CD4, Podoplanin, CD45RO, CD34, CD45, EPCAM, RANKL 
##     TCRgd, CD49a, KLRG1, PD-1, CD73, B7-H4, CD38, CR2, CD123, OX40 
## Negative:  CD5, CD2, ICOS, 4-1BB, CD45RA, CD57, CCR5, CXCR5, FAS, TCRab 
##     CD69, PDGFRA, TIGIT, NKP46, CD25, TCR-V24-J18, Nectin-2, CD86, CD80, MCAM 
##     CD163, CD40LG, IgG2a, FAS-L, 4-1BBL, PDGFRb, CD27, BTLA, ICOSL, CD103 
## PC_ 5 
## Positive:  ICOS, MHCII, CD39, CD5, TIGIT, CD49b, CD25, CTLA-4, CD38, CD45RA 
##     FAS, Thy-1, CD86, GITR, PD-L1, TCRab, CD40LG, FAS-L, IgG1, CD24 
##     CD11C, CD10, 4-1BB, CD40, EPCAM, EGFR, CD27, CD80, CXCR5, HVEM 
## Negative:  NKG2D, CD48, 2B4, CD1d, CD31, CD49a, CD8a, ICOSL, NKP46, TLR4 
##     c-KIT, TCRVa7-2, CD49f, CD66b, IgG2a, CD57, CD127, CD138, BTLA, TCRgd 
##     TIM-3, CD73, HER2, CD141, CD123, KLRG1, CD1C, CD45, CD69, CD163

## Warning: Cannot add objects with duplicate keys (offending key: PC_), setting
## key to 'apca_'

## Computing nearest neighbor graph

## Computing SNN

## Warning: The default method for RunUMAP has changed from calling Python UMAP via reticulate to the R-native UWOT using the cosine metric
## To use Python UMAP via reticulate, set umap.method to 'umap-learn' and metric to 'correlation'
## This message will be shown once per session

## 19:03:40 UMAP embedding parameters a = 0.9922 b = 1.112

## 19:03:40 Read 1431 rows and found 10 numeric columns

## 19:03:40 Using Annoy for neighbor search, n_neighbors = 30

## 19:03:40 Building Annoy index with metric = cosine, n_trees = 50

## 0%   10   20   30   40   50   60   70   80   90   100%

## [----|----|----|----|----|----|----|----|----|----|

## **************************************************|
## 19:03:40 Writing NN index file to temp file /tmp/RtmpzK0XZa/file735d6502c397
## 19:03:40 Searching Annoy index using 1 thread, search_k = 3000
## 19:03:41 Annoy recall = 100%
## 19:03:41 Commencing smooth kNN distance calibration using 1 thread with target n_neighbors = 30
## 19:03:42 Initializing from normalized Laplacian + noise (using irlba)
## 19:03:42 Commencing optimization for 500 epochs, with 54236 positive edges
## 19:03:43 Optimization finished
adt$xcoord<-adt@images$image@coordinates$x
adt$ycoord<-adt@images$image@coordinates$y
DefaultAssay(adt)<-"ADT"
feature_select<-rownames(adt)
module_list<-spatrio_resonance(adt, 
                           sigma=110, 
                           assay='ADT', 
                           feature_select=feature_select, 
                           correlation='pearson', 
                           maxK=8, 
                           k=6, 
                           min_avg_con=0.6,
                           min_avg_cor=0.6, 
                           min_featuer=5, 
                           max_featuer=100,
                           smooth = "reduction",
                           reduction = "umap",
                           smooth_k = 10)
## Feature filtering...
## Using all features...
## Data smoothing
## Smoothing with reduction...

## Loading required package: foreach

## Loading required package: iterators

## Loading required package: parallel

##   |                                                                              |                                                                      |   0%Running in parallel using  5 cores ..
##   |                                                                              |                                                                      |   1%  |                                                                              |=                                                                     |   1%  |                                                                              |=                                                                     |   2%  |                                                                              |==                                                                    |   2%  |                                                                              |==                                                                    |   3%  |                                                                              |==                                                                    |   4%  |                                                                              |===                                                                   |   4%  |                                                                              |===                                                                   |   5%  |                                                                              |====                                                                  |   5%  |                                                                              |====                                                                  |   6%  |                                                                              |=====                                                                 |   6%  |                                                                              |=====                                                                 |   7%  |                                                                              |=====                                                                 |   8%  |                                                                              |======                                                                |   8%  |                                                                              |======                                                                |   9%  |                                                                              |=======                                                               |   9%  |                                                                              |=======                                                               |  10%  |                                                                              |=======                                                               |  11%  |                                                                              |========                                                              |  11%  |                                                                              |========                                                              |  12%  |                                                                              |=========                                                             |  12%  |                                                                              |=========                                                             |  13%  |                                                                              |=========                                                             |  14%  |                                                                              |==========                                                            |  14%  |                                                                              |==========                                                            |  15%  |                                                                              |===========                                                           |  15%  |                                                                              |===========                                                           |  16%  |                                                                              |============                                                          |  16%  |                                                                              |============                                                          |  17%  |                                                                              |============                                                          |  18%  |                                                                              |=============                                                         |  18%  |                                                                              |=============                                                         |  19%  |                                                                              |==============                                                        |  19%  |                                                                              |==============                                                        |  20%  |                                                                              |==============                                                        |  21%  |                                                                              |===============                                                       |  21%  |                                                                              |===============                                                       |  22%  |                                                                              |================                                                      |  22%  |                                                                              |================                                                      |  23%  |                                                                              |================                                                      |  24%  |                                                                              |=================                                                     |  24%  |                                                                              |=================                                                     |  25%  |                                                                              |==================                                                    |  25%  |                                                                              |==================                                                    |  26%  |                                                                              |===================                                                   |  26%  |                                                                              |===================                                                   |  27%  |                                                                              |===================                                                   |  28%  |                                                                              |====================                                                  |  28%  |                                                                              |====================                                                  |  29%  |                                                                              |=====================                                                 |  29%  |                                                                              |=====================                                                 |  30%  |                                                                              |=====================                                                 |  31%  |                                                                              |======================                                                |  31%  |                                                                              |======================                                                |  32%  |                                                                              |=======================                                               |  32%  |                                                                              |=======================                                               |  33%  |                                                                              |=======================                                               |  34%  |                                                                              |========================                                              |  34%  |                                                                              |========================                                              |  35%  |                                                                              |=========================                                             |  35%  |                                                                              |=========================                                             |  36%  |                                                                              |==========================                                            |  36%  |                                                                              |==========================                                            |  37%  |                                                                              |==========================                                            |  38%  |                                                                              |===========================                                           |  38%  |                                                                              |===========================                                           |  39%  |                                                                              |============================                                          |  39%  |                                                                              |============================                                          |  40%  |                                                                              |============================                                          |  41%  |                                                                              |=============================                                         |  41%  |                                                                              |=============================                                         |  42%  |                                                                              |==============================                                        |  42%  |                                                                              |==============================                                        |  43%  |                                                                              |==============================                                        |  44%  |                                                                              |===============================                                       |  44%  |                                                                              |===============================                                       |  45%  |                                                                              |================================                                      |  45%  |                                                                              |================================                                      |  46%  |                                                                              |=================================                                     |  46%  |                                                                              |=================================                                     |  47%  |                                                                              |=================================                                     |  48%  |                                                                              |==================================                                    |  48%  |                                                                              |==================================                                    |  49%  |                                                                              |===================================                                   |  49%  |                                                                              |===================================                                   |  50%  |                                                                              |===================================                                   |  51%  |                                                                              |====================================                                  |  51%  |                                                                              |====================================                                  |  52%  |                                                                              |=====================================                                 |  52%  |                                                                              |=====================================                                 |  53%  |                                                                              |=====================================                                 |  54%  |                                                                              |======================================                                |  54%  |                                                                              |======================================                                |  55%  |                                                                              |=======================================                               |  55%  |                                                                              |=======================================                               |  56%  |                                                                              |========================================                              |  56%  |                                                                              |========================================                              |  57%  |                                                                              |========================================                              |  58%  |                                                                              |=========================================                             |  58%  |                                                                              |=========================================                             |  59%  |                                                                              |==========================================                            |  59%  |                                                                              |==========================================                            |  60%  |                                                                              |==========================================                            |  61%  |                                                                              |===========================================                           |  61%  |                                                                              |===========================================                           |  62%  |                                                                              |============================================                          |  62%  |                                                                              |============================================                          |  63%  |                                                                              |============================================                          |  64%  |                                                                              |=============================================                         |  64%  |                                                                              |=============================================                         |  65%  |                                                                              |==============================================                        |  65%  |                                                                              |==============================================                        |  66%  |                                                                              |===============================================                       |  66%  |                                                                              |===============================================                       |  67%  |                                                                              |===============================================                       |  68%  |                                                                              |================================================                      |  68%  |                                                                              |================================================                      |  69%  |                                                                              |=================================================                     |  69%  |                                                                              |=================================================                     |  70%  |                                                                              |=================================================                     |  71%  |                                                                              |==================================================                    |  71%  |                                                                              |==================================================                    |  72%  |                                                                              |===================================================                   |  72%  |                                                                              |===================================================                   |  73%  |                                                                              |===================================================                   |  74%  |                                                                              |====================================================                  |  74%  |                                                                              |====================================================                  |  75%  |                                                                              |=====================================================                 |  75%  |                                                                              |=====================================================                 |  76%  |                                                                              |======================================================                |  76%  |                                                                              |======================================================                |  77%  |                                                                              |======================================================                |  78%  |                                                                              |=======================================================               |  78%  |                                                                              |=======================================================               |  79%  |                                                                              |========================================================              |  79%  |                                                                              |========================================================              |  80%  |                                                                              |========================================================              |  81%  |                                                                              |=========================================================             |  81%  |                                                                              |=========================================================             |  82%  |                                                                              |==========================================================            |  82%  |                                                                              |==========================================================            |  83%  |                                                                              |==========================================================            |  84%  |                                                                              |===========================================================           |  84%  |                                                                              |===========================================================           |  85%  |                                                                              |============================================================          |  85%  |                                                                              |============================================================          |  86%  |                                                                              |=============================================================         |  86%  |                                                                              |=============================================================         |  87%  |                                                                              |=============================================================         |  88%  |                                                                              |==============================================================        |  88%  |                                                                              |==============================================================        |  89%  |                                                                              |===============================================================       |  89%  |                                                                              |===============================================================       |  90%  |                                                                              |===============================================================       |  91%  |                                                                              |================================================================      |  91%  |                                                                              |================================================================      |  92%  |                                                                              |=================================================================     |  92%  |                                                                              |=================================================================     |  93%  |                                                                              |=================================================================     |  94%  |                                                                              |==================================================================    |  94%  |                                                                              |==================================================================    |  95%  |                                                                              |===================================================================   |  95%  |                                                                              |===================================================================   |  96%  |                                                                              |====================================================================  |  96%  |                                                                              |====================================================================  |  97%  |                                                                              |====================================================================  |  98%  |                                                                              |===================================================================== |  98%  |                                                                              |===================================================================== |  99%  |                                                                              |======================================================================|  99%  |                                                                              |======================================================================| 100%
## Merging results ..
## 
## Time Elapsed:  13.7620165348053 secs 
## Calculating with 99 features...
## Data scaling...

## Centering and scaling data matrix

## Spatially weighted correlation calculating...
## Consensus clustering...

## end fraction

## clustered
## clustered
## clustered
## clustered
## clustered
## clustered
## clustered

## Feature module calculating...
## [1] 5
spatrio_heatmap(module_list,ann_cols = pal_d3()(length(module_list$group)),border_color=NA)
## $Group
##     Module1     Module2 
## "#1F77B4FF" "#FF7F0EFF"

adt<-spatrio_score(adt,module_list,nbin = 5,ctrl = 5,clean=T)
module_df=data.frame(module=NULL,motif=NULL)
for (i in names(module_list$group)) {
    module_sub<-data.frame(module=i,motif=module_list$group[[i]])
    module_df <-rbind(module_df,module_sub)
}

data=data.frame(Module1=adt$Module1,Module2=adt$Module2,cluster=adt$spot_type)
my_comparisons <- list(c("Stroma", "Invasive cancer + lymphocytes"),
                       c("Lymphocytes", "Invasive cancer + lymphocytes"),
                       c("Invasive cancer", "Invasive cancer + lymphocytes")
                       )
p1<-spatrio_boxplot(data,x="cluster",
            y="Module1",
            ylab = "ADT module score",
            title = "Module1",
            compare = my_comparisons,
            cols=class_col[c("Stroma","Lymphocytes","Invasive cancer", "Invasive cancer + lymphocytes")],
            x_level = c("Stroma","Lymphocytes","Invasive cancer", "Invasive cancer + lymphocytes"),vjust = 0.5)+
            theme(axis.text.x = element_text(color ="black",angle = 45,hjust = 1))
p2<-spatrio_boxplot(data,x="cluster",
            y="Module2",
            ylab = "ADT module score",
            title = "Module2",
            compare = my_comparisons,
            cols=class_col[c("Stroma","Lymphocytes","Invasive cancer", "Invasive cancer + lymphocytes")],
            x_level = c("Stroma","Lymphocytes","Invasive cancer", "Invasive cancer + lymphocytes"),vjust = 0.5)+
            theme(axis.text.x = element_text(color ="black",angle = 45,hjust = 1))
CombinePlots(list(p1,p2))
## Warning: CombinePlots is being deprecated. Plots should now be combined using
## the patchwork system.

sfplot(adt, grep('Module', colnames(adt@meta.data), value=T),stroke = NA,pt.size.factor = 8,option = "D",max.cutoff="q90")
## Scale for fill is already present.
## Adding another scale for fill, which will replace the existing scale.
## Scale for fill is already present.
## Adding another scale for fill, which will replace the existing scale.

## Warning: CombinePlots is being deprecated. Plots should now be combined using
## the patchwork system.

Spatial cellular multi-modal interactions analysis

Understanding cellular interactions in the tumor microenvironment can provide insights into tumor initiation, growth, metastasis, and drug resistance. Therefore, we selected the invasive cancer+lymphocytes area and performed spatial cellular multi-modal interactions analysis.

Detection of spatial protein modules

Idents(pred)<-pred$spot_type
object<-subset(pred,ident="Invasive cancer + lymphocytes")
Idents(object)<-object$celltype_major

# Transfer feature id of protein assay
library(readr)
id <- read_csv("data/Human_breast_cancer/id.csv")
## New names:
## Rows: 99 Columns: 3
## ── Column specification
## ──────────────────────────────────────────────────────── Delimiter: "," chr
## (2): orig, transfer dbl (1): ...1
## ℹ Use `spec()` to retrieve the full column specification for this data. ℹ
## Specify the column types or set `show_col_types = FALSE` to quiet this message.
## • `` -> `...1`
count<-object@assays$ADT@counts
count<-as.data.frame(count)
adt_name<-rownames(count)
for (i in 1:nrow(count)) {
count$adt[i]<-id$transfer[id$orig==rownames(count)[i]][1]
}
count<-aggregate(x=count[,1:c(ncol(count)-1)], by=list(count$adt),sum)
index<-count$Group.1
count<-count[,2:ncol(count)]
rownames(count)<-index
count<-as.matrix(count)
object@assays$ADT<-CreateAssayObject(count)
object@assays[["ADT"]]@key<-"adt_"

# Extract major cell subpopulations
Idents(object)<-object$celltype_major
adt<-subset(object,idents = c("T-cells","Myeloid","Endothelial","PVL"))
DefaultAssay(adt)<-"ADT"

VariableFeatures(adt)<-rownames(adt)
adt<-NormalizeData(adt, normalization.method = "CLR", margin = 2)%>%ScaleData()%>%RunPCA(npcs = 10,reduction.name = "apca")%>%RunUMAP(dims = 1:10,reduction="apca")
## Normalizing across cells
## Centering and scaling data matrix
## PC_ 1 
## Positive:  VTCN1, FUT4, IL7R, CD274, PTPRC, IGHG1, CD86, CD40, CD4, PDCD1 
##     CD14, TIGIT, CD19, CD3D, CTLA4, IGHG2, CD8A, ITGAX, CD44, HLA-A 
##     CD48, ITGA4, CD38, CD244, B3GAT1, ITGAM, THBD, CD2, IL3RA, CD69 
## Negative:  TCRgd, CCR6, TNFRSF4, CCR4, SELL, TIM3, ERBB2, PTGDR2, KLRG1, CD163 
##     FAS, TNFRSF18, CXCR3, CD96, CXCR5, CR2, EGFR, KIT, CCR7, TNFRSF14 
##     CD27, BTLA, TCRVa7-2, NCR1, SDC1, TNFSF9, PDCD1LG2, CD58, PDGFRB, ICOSLG 
## PC_ 2 
## Positive:  HLA-A, ITGAX, ITGA4, CD44, CD38, ITGAM, TCR-V24-J18, CD48, CD80, CD276 
##     CD69, CD244, THBD, EPCAM, CCR5, NECTIN2, CD58, PROM1, ENTPD1, ITGAE 
##     TCRab, FASLG, ICOSLG, CD14, CD86, CD2, THY1, PDPN, KLRK1, PDCD1LG2 
## Negative:  IGHG2, CTLA4, IL2RA, CD19, TIGIT, CD8A, CD28, IGHG1, IL7R, PDCD1 
##     BTLA, CD34, KIT, PTGDR2, VTCN1, TCRVa7-2, CR2, CD96, TIM3, CCR7 
##     SELL, ERBB2, CCR4, CD3D, KLRG1, FUT4, NCR1, CEACAM8, CD24, TNFRSF4 
## PC_ 3 
## Positive:  CD2, CD5, CD3D, CD69, PTPRC, TCRab, CD48, CD8A, CD4, ICOS 
##     PDCD1, CD244, CD28, ITGAE, CD1D, CCR5, CD27, KLRK1, CD24, TCR-V24-J18 
##     FAS, CD19, TIGIT, TCRVa7-2, TNFRSF18, TLR4, TNFRSF9, CD96, SDC1, ITGA4 
## Negative:  PECAM1, ITGA6, NT5E, ENTPD1, CD34, ITGA2, NECTIN2, THBD, IL3RA, THY1 
##     MCAM, ITGA1, CD276, CD40, TNFSF11, PDPN, B3GAT1, IGHG1, CD274, MME 
##     LAG3, CD58, VTCN1, CEACAM8, IGHG2, HLA-A, CD38, CCR7, PDCD1LG2, ICOSLG 
## PC_ 4 
## Positive:  TCRab, CD2, CD5, ITGA2, CD3D, NT5E, CD1D, THY1, ITGA1, THBD 
##     NECTIN2, CD69, IL3RA, KLRK1, PDCD1, LAG3, ITGAE, CD27, MCAM, B3GAT1 
##     PTPRC, TCR-V24-J18, CD8A, FASLG, ICOSLG, CD4, TLR4, CD48, ICOS, CD34 
## Negative:  CD14, CD274, ITGAM, CD86, CD40LG, CD80, IGHG2, CTLA4, ITGAX, CD40 
##     PROM1, HLA-A, IL2RA, TNFSF11, CCR7, IGHG1, CCR5, CD58, EPCAM, CXCR5 
##     KIT, PDCD1LG2, CD24, CD38, FUT4, CD44, TIGIT, CD163, FAS, PDGFRA 
## PC_ 5 
## Positive:  ITGA1, THY1, MCAM, CD14, ICOSLG, PDGFRB, SDC1, PDGFRA, CTLA4, PDCD1LG2 
##     PTPRC, CD8A, CEACAM8, TNFSF9, TNFRSF14, EGFR, TLR4, KIT, CD276, TIGIT 
##     CR2, TCRVa7-2, NCR1, CD86, ITGA2, IL3RA, CD58, CD80, BTLA, PDPN 
## Negative:  CD40, MME, PECAM1, CD1C, IL7R, ENTPD1, PDCD1, KLRG1, ITGA6, TNFRSF4 
##     B3GAT1, NT5E, TCRgd, CCR6, ERBB2, IGHG1, CD2, TCRab, CXCR3, CD274 
##     THBD, CD5, VTCN1, CD34, CD3D, CD48, TIM3, CCR4, CD1D, NECTIN2

## Warning: Cannot add objects with duplicate keys (offending key: PC_) setting
## key to original value 'APC_'

## 19:04:00 UMAP embedding parameters a = 0.9922 b = 1.112
## 19:04:00 Read 669 rows and found 10 numeric columns
## 19:04:00 Using Annoy for neighbor search, n_neighbors = 30
## 19:04:00 Building Annoy index with metric = cosine, n_trees = 50
## 0%   10   20   30   40   50   60   70   80   90   100%
## [----|----|----|----|----|----|----|----|----|----|
## **************************************************|
## 19:04:00 Writing NN index file to temp file /tmp/RtmpzK0XZa/file735d4917a933
## 19:04:00 Searching Annoy index using 1 thread, search_k = 3000
## 19:04:01 Annoy recall = 100%
## 19:04:01 Commencing smooth kNN distance calibration using 1 thread with target n_neighbors = 30
## 19:04:01 Initializing from normalized Laplacian + noise (using irlba)
## 19:04:01 Commencing optimization for 500 epochs, with 22348 positive edges
## 19:04:02 Optimization finished
feature_select<-rownames(adt)
adt$xcoord<-adt@images$image@coordinates$x
adt$ycoord<-adt@images$image@coordinates$y

module_list<-spatrio_resonance(adt, 
                           sigma=110, 
                           assay='ADT', 
                           feature_select=feature_select, 
                           correlation='pearson', 
                           maxK=8, 
                           k=8, 
                           min_avg_con=0.3,
                           min_avg_cor=0.3, 
                           min_featuer=5, 
                           max_featuer=200,
                           smooth = "reduction",
                           reduction = "umap",
                           smooth_k = 10,
                           min_pct_cutoff = 0.1)
## Feature filtering...
## Filtering features...
## Data smoothing
## Smoothing with reduction...
##   |                                                                              |                                                                      |   0%Running in parallel using  5 cores ..
##   |                                                                              |                                                                      |   1%  |                                                                              |=                                                                     |   1%  |                                                                              |=                                                                     |   2%  |                                                                              |==                                                                    |   2%  |                                                                              |==                                                                    |   3%  |                                                                              |===                                                                   |   4%  |                                                                              |===                                                                   |   5%  |                                                                              |====                                                                  |   5%  |                                                                              |====                                                                  |   6%  |                                                                              |=====                                                                 |   7%  |                                                                              |=====                                                                 |   8%  |                                                                              |======                                                                |   8%  |                                                                              |======                                                                |   9%  |                                                                              |=======                                                               |   9%  |                                                                              |=======                                                               |  10%  |                                                                              |=======                                                               |  11%  |                                                                              |========                                                              |  11%  |                                                                              |========                                                              |  12%  |                                                                              |=========                                                             |  12%  |                                                                              |=========                                                             |  13%  |                                                                              |==========                                                            |  14%  |                                                                              |==========                                                            |  15%  |                                                                              |===========                                                           |  15%  |                                                                              |===========                                                           |  16%  |                                                                              |============                                                          |  16%  |                                                                              |============                                                          |  17%  |                                                                              |============                                                          |  18%  |                                                                              |=============                                                         |  18%  |                                                                              |=============                                                         |  19%  |                                                                              |==============                                                        |  19%  |                                                                              |==============                                                        |  20%  |                                                                              |==============                                                        |  21%  |                                                                              |===============                                                       |  21%  |                                                                              |===============                                                       |  22%  |                                                                              |================                                                      |  22%  |                                                                              |================                                                      |  23%  |                                                                              |=================                                                     |  24%  |                                                                              |=================                                                     |  25%  |                                                                              |==================                                                    |  25%  |                                                                              |==================                                                    |  26%  |                                                                              |===================                                                   |  26%  |                                                                              |===================                                                   |  27%  |                                                                              |===================                                                   |  28%  |                                                                              |====================                                                  |  28%  |                                                                              |====================                                                  |  29%  |                                                                              |=====================                                                 |  29%  |                                                                              |=====================                                                 |  30%  |                                                                              |=====================                                                 |  31%  |                                                                              |======================                                                |  31%  |                                                                              |======================                                                |  32%  |                                                                              |=======================                                               |  32%  |                                                                              |=======================                                               |  33%  |                                                                              |========================                                              |  34%  |                                                                              |========================                                              |  35%  |                                                                              |=========================                                             |  35%  |                                                                              |=========================                                             |  36%  |                                                                              |==========================                                            |  36%  |                                                                              |==========================                                            |  37%  |                                                                              |==========================                                            |  38%  |                                                                              |===========================                                           |  38%  |                                                                              |===========================                                           |  39%  |                                                                              |============================                                          |  39%  |                                                                              |============================                                          |  40%  |                                                                              |============================                                          |  41%  |                                                                              |=============================                                         |  41%  |                                                                              |=============================                                         |  42%  |                                                                              |==============================                                        |  42%  |                                                                              |==============================                                        |  43%  |                                                                              |===============================                                       |  44%  |                                                                              |===============================                                       |  45%  |                                                                              |================================                                      |  45%  |                                                                              |================================                                      |  46%  |                                                                              |=================================                                     |  46%  |                                                                              |=================================                                     |  47%  |                                                                              |=================================                                     |  48%  |                                                                              |==================================                                    |  48%  |                                                                              |==================================                                    |  49%  |                                                                              |===================================                                   |  49%  |                                                                              |===================================                                   |  50%  |                                                                              |===================================                                   |  51%  |                                                                              |====================================                                  |  51%  |                                                                              |====================================                                  |  52%  |                                                                              |=====================================                                 |  52%  |                                                                              |=====================================                                 |  53%  |                                                                              |=====================================                                 |  54%  |                                                                              |======================================                                |  54%  |                                                                              |======================================                                |  55%  |                                                                              |=======================================                               |  55%  |                                                                              |=======================================                               |  56%  |                                                                              |========================================                              |  57%  |                                                                              |========================================                              |  58%  |                                                                              |=========================================                             |  58%  |                                                                              |=========================================                             |  59%  |                                                                              |==========================================                            |  59%  |                                                                              |==========================================                            |  60%  |                                                                              |==========================================                            |  61%  |                                                                              |===========================================                           |  61%  |                                                                              |===========================================                           |  62%  |                                                                              |============================================                          |  62%  |                                                                              |============================================                          |  63%  |                                                                              |============================================                          |  64%  |                                                                              |=============================================                         |  64%  |                                                                              |=============================================                         |  65%  |                                                                              |==============================================                        |  65%  |                                                                              |==============================================                        |  66%  |                                                                              |===============================================                       |  67%  |                                                                              |===============================================                       |  68%  |                                                                              |================================================                      |  68%  |                                                                              |================================================                      |  69%  |                                                                              |=================================================                     |  69%  |                                                                              |=================================================                     |  70%  |                                                                              |=================================================                     |  71%  |                                                                              |==================================================                    |  71%  |                                                                              |==================================================                    |  72%  |                                                                              |===================================================                   |  72%  |                                                                              |===================================================                   |  73%  |                                                                              |===================================================                   |  74%  |                                                                              |====================================================                  |  74%  |                                                                              |====================================================                  |  75%  |                                                                              |=====================================================                 |  75%  |                                                                              |=====================================================                 |  76%  |                                                                              |======================================================                |  77%  |                                                                              |======================================================                |  78%  |                                                                              |=======================================================               |  78%  |                                                                              |=======================================================               |  79%  |                                                                              |========================================================              |  79%  |                                                                              |========================================================              |  80%  |                                                                              |========================================================              |  81%  |                                                                              |=========================================================             |  81%  |                                                                              |=========================================================             |  82%  |                                                                              |==========================================================            |  82%  |                                                                              |==========================================================            |  83%  |                                                                              |==========================================================            |  84%  |                                                                              |===========================================================           |  84%  |                                                                              |===========================================================           |  85%  |                                                                              |============================================================          |  85%  |                                                                              |============================================================          |  86%  |                                                                              |=============================================================         |  87%  |                                                                              |=============================================================         |  88%  |                                                                              |==============================================================        |  88%  |                                                                              |==============================================================        |  89%  |                                                                              |===============================================================       |  89%  |                                                                              |===============================================================       |  90%  |                                                                              |===============================================================       |  91%  |                                                                              |================================================================      |  91%  |                                                                              |================================================================      |  92%  |                                                                              |=================================================================     |  92%  |                                                                              |=================================================================     |  93%  |                                                                              |==================================================================    |  94%  |                                                                              |==================================================================    |  95%  |                                                                              |===================================================================   |  95%  |                                                                              |===================================================================   |  96%  |                                                                              |====================================================================  |  97%  |                                                                              |====================================================================  |  98%  |                                                                              |===================================================================== |  98%  |                                                                              |===================================================================== |  99%  |                                                                              |======================================================================|  99%  |                                                                              |======================================================================| 100%
## Merging results ..
## 
## Time Elapsed:  13.4321479797363 secs 
## Calculating with 96 features...
## Data scaling...

## Centering and scaling data matrix

## Spatially weighted correlation calculating...
## Consensus clustering...

## end fraction
## clustered
## clustered
## clustered
## clustered
## clustered
## clustered
## clustered

## Feature module calculating...
## [1] 5

Cell-cell communication inference

gene-gene

load("~/spatrio_data/tutorial/data/Human_steatosis_liver/lrpairs.rda")
load("~/spatrio_data/tutorial/data/Human_steatosis_liver/pathways.rda")
obj<-gene_cci_analysis(adt,
                       assay = "RNA",
                       species = "Human",
                       xcoord = adt@images$image@coordinates$x,
                       ycoord = adt@images$image@coordinates$y,
                       celltype = adt$celltype_major,
                       lrpairs = lrpairs,
                       pathways = pathways)
## Checking input data 
## Begin to filter lrpairs and pathways 
## ***Done*** 
## Note: there are 4 cell types and 12 pair-wise cell pairs 
## Begin to find LR pairs
df<-gene_cci_plot(obj,
              #celltype_receiver = c("Endothelial","Myeloid","PVL","T-cells"),
              celltype_pair = c("Endothelial_T-cells","Myeloid_T-cells","PVL_T-cells","PVL_Myeloid","Myeloid_PVL","T-cells_PVL"),
              lr_pair = strsplit("HLA-A_CD3D,HLA-B_CD3D,HLA-C_CD3D,HLA-A_CD3G,MIF_CXCR4,APOE_TREM2,PDGFB_PDGFRB,CXCL16_ADRA2A,CALM2_EGFR,TGFB1_SDC2",split = ",")[[1]],
              color="#9a6a9e",return_data = T)
head(df)
##      ligand receptor species celltype_sender celltype_receiver lr_co_exp_num
## 94    TGFB1     SDC2   Human         T-cells               PVL             9
## 412   CALM2     EGFR   Human         T-cells               PVL             5
## 808   HLA-A     CD3G   Human     Endothelial           T-cells            48
## 1332  HLA-A     CD3D   Human     Endothelial           T-cells            65
## 1333  HLA-C     CD3D   Human     Endothelial           T-cells            65
## 1334  HLA-B     CD3D   Human     Endothelial           T-cells            65
##      lr_co_ratio lr_co_ratio_pvalue     score                pair         lr
## 94     0.3461538                  0 0.8597321         T-cells_PVL TGFB1_SDC2
## 412    0.1923077                  0 0.9999960         T-cells_PVL CALM2_EGFR
## 808    0.4615385                  0 0.8473712 Endothelial_T-cells HLA-A_CD3G
## 1332   0.6250000                  0 0.9901887 Endothelial_T-cells HLA-A_CD3D
## 1333   0.6250000                  0 0.9901887 Endothelial_T-cells HLA-C_CD3D
## 1334   0.6250000                  0 0.9901887 Endothelial_T-cells HLA-B_CD3D
gene_cci_plot(obj,
              #celltype_receiver = c("Endothelial","Myeloid","PVL","T-cells"),
              celltype_pair = c("Endothelial_T-cells","Myeloid_T-cells","PVL_T-cells","PVL_Myeloid","Myeloid_PVL","T-cells_PVL"),
              lr_pair = strsplit("HLA-A_CD3D,HLA-B_CD3D,HLA-C_CD3D,HLA-A_CD3G,MIF_CXCR4,APOE_TREM2,PDGFB_PDGFRB,CXCL16_ADRA2A,CALM2_EGFR,TGFB1_SDC2",split = ",")[[1]],
              color="#9a6a9e")

protein-protein

library(data.table)
## 
## Attaching package: 'data.table'

## The following objects are masked from 'package:dplyr':
## 
##     between, first, last

## The following objects are masked from 'package:reshape2':
## 
##     dcast, melt
#Read in data frame containing protein id and gene id information
id <- read_csv("data/Human_breast_cancer/id.csv")
## New names:
## • `` -> `...1`

## Rows: 99 Columns: 3
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): orig, transfer
## dbl (1): ...1
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
#Read in data frame containing information of cell-cell interaction
lr<-fread("data/Human_breast_cancer/human_lr_pair.txt")
df = adt_cci_analysis(adt,input_list =module_list,id=id,lr=lr,celltype_pair =c("Myeloid_T-cells","PVL_T-cells","PVL_Myeloid","Myeloid_PVL","T-cells_PVL"),
                 lr_pair =strsplit("FASLG_FAS,HLA-A_ERBB2,CD40LG_CD40,CD40LG_ITGAM,ICOSLG_ICOS,CD58_CD2,PDCD1LG2_PDCD1,CD14_TLR4,CD274_PDCD1,HLA-A_CD3D",split = ",")[[1]],mode = "2" )
head(df)
##         lr_pair ligand_gene_symbol receptor_gene_symbol sender_module
## 13  ICOSLG_ICOS             ICOSLG                 ICOS            k2
## 17    FASLG_FAS              FASLG                  FAS            k2
## 3     CD14_TLR4               CD14                 TLR4            k3
## 6  CD40LG_ITGAM             CD40LG                ITGAM            k2
## 18     CD58_CD2               CD58                  CD2            k2
## 7   CD40LG_CD40             CD40LG                 CD40            k2
##    reciever_module  sender reciever ligand_exp receptor_exp      score
## 13              k2     PVL  T-cells  0.3123419    0.7903744 0.08023383
## 17              k2 T-cells      PVL  0.1538620    3.6166253 0.12046015
## 3               k2 Myeloid  T-cells  6.7263379    0.1777011 0.17654701
## 6               k3     PVL  Myeloid  0.1586290    9.0196013 0.19315720
## 18              k5     PVL  T-cells  1.7886164    0.9344921 0.20877205
## 7               k6     PVL  Myeloid  0.1586290   14.5374608 0.24522317
##               pair           lr
## 13     PVL_T-cells  ICOSLG_ICOS
## 17     T-cells_PVL    FASLG_FAS
## 3  Myeloid_T-cells    CD14_TLR4
## 6      PVL_Myeloid CD40LG_ITGAM
## 18     PVL_T-cells     CD58_CD2
## 7      PVL_Myeloid  CD40LG_CD40
adt_cci_plot(df,mode="2",color = "#cb6377")