引自https://yingma0107.github.io/CARD/documentation/04_CARD_Example.html
# install the CARD package
devtools::install_github('YingMa0107/CARD')
# load package
library(CARD)
wget https://github.com/YingMa0107/CARD/raw/master/data/spatial_location.RData
wget https://github.com/YingMa0107/CARD/raw/master/data/spatial_count.RData
wget https://github.com/YingMa0107/CARD/raw/master/data/sc_meta.RData
wget https://github.com/YingMa0107/CARD/raw/master/data/sc_count.RData
1.CARD去卷积需要的input数据
1)单细胞sc_count.RData、sc_meta.RData
#### load the example scRNA-seq count data,
load("./sc-data/sc_count.RData")
sc_count[1:4,1:4]
#The scRNA-seq count data must be in the format of matrix or sparseMatrix,
#while each row represents a gene and each column represents a cell.
#### load the example scRNA-seq meta data,
load("./sc-data/sc_meta.RData")
sc_meta[1:4,]
#The scRNAseq meta data must be in the format of data frame while each row represents a cell.
#The rownames of the scRNAseq meta data should match exactly with the column names of the scRNAseq count data.
#The sc_meta data must contain the column indicating the cell type assignment for each cell (e.g., “cellType” column in the example sc_meta data).
#Sample/subject information should be provided, if there is only one sample, we can add a column by
#sc_meta$sampleInfo = "sample1".
#We suggest the users to check their single cell RNASeq data carefully before running CARD.
#We suggest the users to input the single cell RNAseq data with each cell type containing at least 2 cells.
#i.e. print(table(sc_meta$cellType,useNA = “ifany”))
2)空转spatial_count.RData、spatial_location.RData
#### load the example spatial transcriptomics count data,
load("./spa-data/spatial_count.RData")
spatial_count[1:4,1:4]
#The spatial transcriptomics count data must be in the format of matrix or sparseMatrix,
#while each row represents a gene and each column represents a spatial location.
#The column names of the spatial data can be in the “XcoordxYcoord” (i.e., 10x10) format,
#but you can also maintain your original spot names, for example, barcode names.
#### load the example spatial location data,
load("./spa-data/spatial_location.RData")
spatial_location[1:4,]
#The spatial location data must be in the format of data frame while each row represents a spatial location,
# the first column represents the x coordinate and the second column represents the y coordinate.
# The rownames of the spatial location data frame should match exactly with the column names of the spatial_count.
2.细胞类型去卷积
1)Create an CARD object
library(CARD)
CARD_obj = createCARDObject(
sc_count = sc_count,
sc_meta = sc_meta,
spatial_count = spatial_count,
spatial_location = spatial_location,
ct.varname = "cellType",
ct.select = unique(sc_meta$cellType),
sample.varname = "sampleInfo",
minCountGene = 100,
minCountSpot = 5)
#The spatial data are stored in CARD_obj@spatial_countMat and CARD_obj@spatial_location
#while the scRNA-seq data is stored in CARD_obj@sc_eset in the format of SingleCellExperiment.
2)Deconvolution using CARD
CARD_obj = CARD_deconvolution(CARD_object = CARD_obj)
#The results are stored in CARD_obj@Proportion_CARD.
3)Visualize the cell type proportion
colors = c("#FFD92F","#4DAF4A","#FCCDE5","#D9D9D9","#377EB8","#7FC97F","#BEAED4",
"#FDC086","#FFFF99","#386CB0","#F0027F","#BF5B17","#666666","#1B9E77","#D95F02",
"#7570B3","#E7298A","#66A61E","#E6AB02","#A6761D")
p1 <- CARD.visualize.pie(proportion = CARD_obj@Proportion_CARD,spatial_location = CARD_obj@spatial_location, colors = colors)
ggsave(p1,file="pie_plot.pdf",width=15,height=7)
#We do not recommend users to visualize this plot when the number of spots is > 500
## select the cell type that we are interested
ct.visualize = c("Acinar_cells","Cancer_clone_A","Cancer_clone_B","Ductal_terminal_ductal_like","Ductal_CRISP3_high-centroacinar_like","Ductal_MHC_Class_II","Ductal_APOL1_high-hypoxic","Fibroblasts")
## visualize the spatial distribution of the cell type proportion
p2 <- CARD.visualize.prop(
proportion = CARD_obj@Proportion_CARD,
spatial_location = CARD_obj@spatial_location,
ct.visualize = ct.visualize, ### selected cell types to visualize
colors = c("lightblue","lightyellow","red"), ### if not provide, we will use the default colors
NumCols = 4) ### number of columns in the figure panel
ggsave(p2,file="selected_cell_types.pdf",width=15,height=14)
4) Visualize the cell type proportion correlation
p3 <- CARD.visualize.Cor(CARD_obj@Proportion_CARD,colors = NULL)
ggsave(p3,file="correlation.pdf",width=14,height=14)
3.精细化空间图谱
1)Imputation on the newly grided spatial locations
CARD_obj = CARD.imputation(CARD_obj,NumGrids = 2000,ineibor = 10,exclude = NULL)
#The results are store in CARD_obj@refined_prop and CARD_obj@refined_expression
## Visualize the newly grided spatial locations to see if the shape is correctly detected. If not, the user can provide the row names of the excluded spatial location data into the CARD.imputation function
location_imputation = cbind.data.frame(x=as.numeric(sapply(strsplit(rownames(CARD_obj@refined_prop),split="x"),"[",1)),
y=as.numeric(sapply(strsplit(rownames(CARD_obj@refined_prop),split="x"),"[",2)))
rownames(location_imputation) = rownames(CARD_obj@refined_prop)
library(ggplot2)
p4 <- ggplot(location_imputation,
aes(x = x, y = y)) + geom_point(shape=22,color = "#7dc7f5")+
theme(plot.margin = margin(0.1, 0.1, 0.1, 0.1, "cm"),
legend.position="bottom",
panel.background = element_blank(),
plot.background = element_blank(),
panel.border = element_rect(colour = "grey89", fill=NA, size=0.5))
ggsave(p4,file="newly_grid.pdf",width=14,height=14)
2)Visualize the cell type proportion at an enhanced resolution
p5 <- CARD.visualize.prop(
proportion = CARD_obj@refined_prop,
spatial_location = location_imputation,
ct.visualize = ct.visualize,
colors = c("lightblue","lightyellow","red"),
NumCols = 4)
ggsave(p5,file="enhanced_resolution_proportion.pdf",width=14,height=14)
3)Visualize the marker gene expression at an enhanced resolution
p6 <- CARD.visualize.gene(
spatial_expression = CARD_obj@refined_expression,
spatial_location = location_imputation,
gene.visualize = c("Tm4sf1","S100a4","Tff3","Apol1","Crisp3","CD248"),
colors = NULL,
NumCols = 6)
ggsave(p6,file="enhanced_resolution_gene_expression.pdf",width=14,height=7)
#original gene expression
p7 <- CARD.visualize.gene(
spatial_expression = CARD_obj@spatial_countMat,
spatial_location = CARD_obj@spatial_location,
gene.visualize = c("Tm4sf1","S100a4","Tff3","Apol1","Crisp3","CD248"),
colors = NULL,
NumCols = 6)
ggsave(p7,file="original_gene_expression.pdf",width=14,height=7)
4.不用单细胞作为参考对空转进行反卷积
不用单细胞数据,则需要一些能指明细胞类型的marker gene
wget https://github.com/YingMa0107/CARD/raw/master/data/markerList.RData
1)Create an CARDfree object
## load the marker gene list
load("./markerList.RData")
CARDfree_obj = createCARDfreeObject(
markerList = markerList,
spatial_count = spatial_count,
spatial_location = spatial_location,
minCountGene = 100,
minCountSpot =5)
#The spatial data are stored in CARDfree_obj@spatial_countMat and CARDfree_obj@spatial_location
#while the marker list is stored in CARDfree_objj@markerList in the format of list.
2)Deconvolution using CARDfree
## deconvolution using CARDfree
CARDfree_obj = CARD_refFree(CARDfree_obj)
#The results are stored in CARDfree_obj@Proportion_CARD.
## One limitation of reference-free version of CARD is that the cell types inferred from CARDfree do not come with a cell type label. It might be difficult to interpret the results.
print(CARDfree_obj@Proportion_CARD[1:2,])
3)Visualization of the results of CARDfree
colors = c("#FFD92F","#4DAF4A","#FCCDE5","#D9D9D9","#377EB8","#7FC97F","#BEAED4","#FDC086","#FFFF99","#386CB0","#F0027F","#BF5B17","#666666","#1B9E77","#D95F02","#7570B3","#E7298A","#66A61E","#E6AB02","#A6761D")
### In order to maximumply match with the original results of CARD, we order the colors to generally match with the results infered by CARD
CARDfree_obj@Proportion_CARD = CARDfree_obj@Proportion_CARD[,c(8,10,14,2,1,6,12,18,7,13,20,19,16,17,11,15,4,9,3,5)]
colnames(CARDfree_obj@Proportion_CARD) = paste0("CT",1:20)
p8 <- CARD.visualize.pie(CARDfree_obj@Proportion_CARD,CARDfree_obj@spatial_location,colors = colors)
ggsave(p8,file="pie_plot2.pdf",width=15,height=7)
5.绘制单细胞分辨率的空间图谱
#### Note that here the shapeSpot is the user defined variable which indicates the capturing area of single cells. Details see above.
scMapping = CARD_SCMapping(CARD_obj,shapeSpot="Square",numCell=20,ncore=10)
print(scMapping)
### spatial location info and expression count of the single cell resolution data
library(SingleCellExperiment)
MapCellCords = as.data.frame(colData(scMapping))
count_SC = assays(scMapping)$counts
library(ggplot2)
df = MapCellCords
colors = c("#8DD3C7","#CFECBB","#F4F4B9","#CFCCCF","#D1A7B9","#E9D3DE","#F4867C","#C0979F",
"#D5CFD6","#86B1CD","#CEB28B","#EDBC63","#C59CC5","#C09CBF","#C2D567","#C9DAC3","#E1EBA0",
"#FFED6F","#CDD796","#F8CDDE")
p9 = ggplot(df, aes(x = x, y = y, colour = CT)) +
geom_point(size = 3.0) +
scale_colour_manual(values = colors) +
#facet_wrap(~Method,ncol = 2,nrow = 3) +
theme(plot.margin = margin(0.1, 0.1, 0.1, 0.1, "cm"),
panel.background = element_rect(colour = "white", fill="white"),
plot.background = element_rect(colour = "white", fill="white"),
legend.position="bottom",
panel.border = element_rect(colour = "grey89", fill=NA, size=0.5),
axis.text =element_blank(),
axis.ticks =element_blank(),
axis.title =element_blank(),
legend.title=element_text(size = 13,face="bold"),
legend.text=element_text(size = 12),
legend.key = element_rect(colour = "transparent", fill = "white"),
legend.key.size = unit(0.45, 'cm'),
strip.text = element_text(size = 15,face="bold"))+
guides(color=guide_legend(title="Cell Type"))
ggsave(p9,file="singlecell_map.pdf",width=15,height=7)