CARD反卷積筆記

引自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數(shù)據(jù)

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.
sc_count
#### 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”))
sc_meta

2)空轉(zhuǎn)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.
spatial_count
#### 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.
spatial_location

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.
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
pie_plot
## 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)
selected_cell_types

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)
correlation

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)
newly_grid

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)
enhanced_resolution_proportion

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)
enhanced_resolution_gene_expression
#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)
original_gene_expression

4.不用單細胞作為參考對空轉(zhuǎn)進行反卷積

不用單細胞數(shù)據(jù)鲸匿,則需要一些能指明細胞類型的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")
markerList

image.png
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,])
CARDfree_obj@Proportion_CARD

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)
pie_plot2

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)
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)
singlecell_map
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市譬巫,隨后出現(xiàn)的幾起案子买喧,更是在濱河造成了極大的恐慌糟秘,老刑警劉巖,帶你破解...
    沈念sama閱讀 206,013評論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件趁啸,死亡現(xiàn)場離奇詭異,居然都是意外死亡督惰,警方通過查閱死者的電腦和手機不傅,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,205評論 2 382
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來赏胚,“玉大人访娶,你說我怎么就攤上這事【踉模” “怎么了崖疤?”我有些...
    開封第一講書人閱讀 152,370評論 0 342
  • 文/不壞的土叔 我叫張陵,是天一觀的道長典勇。 經(jīng)常有香客問我劫哼,道長,這世上最難降的妖魔是什么割笙? 我笑而不...
    開封第一講書人閱讀 55,168評論 1 278
  • 正文 為了忘掉前任权烧,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘豪嚎。我一直安慰自己搔驼,他們只是感情好,可當我...
    茶點故事閱讀 64,153評論 5 371
  • 文/花漫 我一把揭開白布侈询。 她就那樣靜靜地躺著舌涨,像睡著了一般。 火紅的嫁衣襯著肌膚如雪扔字。 梳的紋絲不亂的頭發(fā)上囊嘉,一...
    開封第一講書人閱讀 48,954評論 1 283
  • 那天,我揣著相機與錄音革为,去河邊找鬼扭粱。 笑死,一個胖子當著我的面吹牛震檩,可吹牛的內(nèi)容都是我干的琢蛤。 我是一名探鬼主播,決...
    沈念sama閱讀 38,271評論 3 399
  • 文/蒼蘭香墨 我猛地睜開眼抛虏,長吁一口氣:“原來是場噩夢啊……” “哼博其!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起迂猴,我...
    開封第一講書人閱讀 36,916評論 0 259
  • 序言:老撾萬榮一對情侶失蹤慕淡,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后沸毁,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體峰髓,經(jīng)...
    沈念sama閱讀 43,382評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 35,877評論 2 323
  • 正文 我和宋清朗相戀三年息尺,在試婚紗的時候發(fā)現(xiàn)自己被綠了携兵。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 37,989評論 1 333
  • 序言:一個原本活蹦亂跳的男人離奇死亡掷倔,死狀恐怖眉孩,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情勒葱,我是刑警寧澤浪汪,帶...
    沈念sama閱讀 33,624評論 4 322
  • 正文 年R本政府宣布,位于F島的核電站凛虽,受9級特大地震影響死遭,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜凯旋,卻給世界環(huán)境...
    茶點故事閱讀 39,209評論 3 307
  • 文/蒙蒙 一呀潭、第九天 我趴在偏房一處隱蔽的房頂上張望钉迷。 院中可真熱鬧,春花似錦钠署、人聲如沸糠聪。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,199評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽舰蟆。三九已至,卻和暖如春狸棍,著一層夾襖步出監(jiān)牢的瞬間身害,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,418評論 1 260
  • 我被黑心中介騙來泰國打工草戈, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留塌鸯,地道東北人。 一個月前我還...
    沈念sama閱讀 45,401評論 2 352
  • 正文 我出身青樓唐片,卻偏偏與公主長得像丙猬,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子牵触,可洞房花燭夜當晚...
    茶點故事閱讀 42,700評論 2 345