Last updated: 2024-03-21

Checks: 7 0

Knit directory: Celina/

This reproducible R Markdown analysis was created with workflowr (version 1.7.1). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.


Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.

Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.

The command set.seed(20231207) was run prior to running the code in the R Markdown file. Setting a seed ensures that any results that rely on randomness, e.g. subsampling or permutations, are reproducible.

Great job! Recording the operating system, R version, and package versions is critical for reproducibility.

Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.

Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.

Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.

Note that you need to be careful to ensure that all relevant files for the analysis have been committed to Git prior to generating the results (you can use wflow_publish or wflow_git_commit). workflowr only checks the R Markdown file, but you know if there are other scripts or data files that it depends on. Below is the status of the Git repository when the results were generated:


Untracked files:
    Untracked:  .Rprofile
    Untracked:  .gitattributes
    Untracked:  .gitignore
    Untracked:  Celina.Rproj
    Untracked:  README.md
    Untracked:  _workflowr.yml
    Untracked:  analysis/
    Untracked:  code/
    Untracked:  data/
    Untracked:  output/

Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.


There are no past versions. Publish this analysis with wflow_publish() to start tracking its development.


This is a step by step guide for performing cell type deconvolution of your non-single cell spatial transcriptomic data using CARD. If your spatial transcriptomic data is already at single cell level, you may skip this section.

Load in data

The processed example data can be downloaded from here.

# load in data
load(paste0("./data/scRNA_data_RCC_PD47171in_tumor_interface.RData"))
# single cell reference data
load(paste0("./data/data_RCC_PD47171in_tumor_interface.RData"))
# 10x visium spatial transcriptomics data
names(sc_section)
names(data_section)

Prepare datasets

For spot level spatial transcriptomics data, we will do cell type deconvolution analysis first to infer the cell type proportions at each spot. The datasets needed are:

  1. single cell reference dataset, we need gene expression count matrix (sc_section$count), cell type annotation for each cell (sc_section$meta$broad_type);

  2. spatial transcriptomics dataset, we need gene expression count matrix (data_section$count), spatial coordinate matrix (data_section$location).

Perform deconvolution

library(CARD)
sc_meta_in = data.frame("cellType" = sc_section$meta$broad_type)
rownames(sc_meta_in) = colnames(sc_section$count)
sc_meta_in$cellID = rownames(sc_meta_in)
sc_meta_in$sampleInfo=1
location=data_section$location
colnames(location) = c("x","y")
sc_meta_in_subset = sc_meta_in
scRNA_count_subset = sc_section$count
CARD_obj = createCARDObject(
    sc_count = scRNA_count_subset,
    sc_meta = sc_meta_in_subset,
    spatial_count = data_section$count,
    spatial_location = location,
    ct.varname = "cellType",
    ct.select = unique(sc_meta_in$cellType),
    sample.varname = "sampleInfo",
    minCountGene = 100,
    minCountSpot = 20) 
CARD_obj = CARD_deconvolution(CARD_object = CARD_obj)
CARD_obj@Proportion_CARD[1:2,]

# match results with spatial locations, save results to data_section
celltype_proportion = CARD_obj@Proportion_CARD
data_section$celltype_proportion = celltype_proportion
id = match(rownames(celltype_proportion), rownames(location))
data_section$location_coord = data_section$location[id,]
data_section$count_use = data_section$count[,id]

Visualize deconvolution result

library(viridis)
library(ggplot2)
loc_x = data_section$location[,1]
loc_y = data_section$location[,2]
prop = data_section$celltype_proportion[,"RCC"]
datt = data.frame(loc_x,loc_y,prop)
title_in = "RCC cell type proportion"
p1 = ggplot() +
        scale_fill_brewer()+
        scale_color_viridis_c()+
        scale_color_viridis(discrete = TRUE)+
        geom_point(data=datt, aes(x = loc_x, y = loc_y, color = prop),alpha = 0.9, size = prop*5,shape=16) + 
        ggtitle(paste0(title_in)) + 
        scale_color_viridis(option = "H")+
        theme_void() + 
        theme(plot.title = element_text(size = 30), text = element_text(size = 30), legend.position = "right")+ labs(color=NULL)

print(p1)


sessionInfo()
R version 4.3.3 (2024-02-29)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.6 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3 
LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/liblapack.so.3;  LAPACK version 3.9.0

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

time zone: America/New_York
tzcode source: system (glibc)

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] vctrs_0.6.5       cli_3.6.2         knitr_1.45        rlang_1.1.2      
 [5] xfun_0.41         stringi_1.8.3     promises_1.2.1    jsonlite_1.8.8   
 [9] workflowr_1.7.1   glue_1.7.0        rprojroot_2.0.4   git2r_0.33.0     
[13] htmltools_0.5.7   httpuv_1.6.13     sass_0.4.8        fansi_1.0.5      
[17] rmarkdown_2.25    jquerylib_0.1.4   evaluate_0.23     tibble_3.2.1     
[21] fastmap_1.1.1     yaml_2.3.8        lifecycle_1.0.4   stringr_1.5.1    
[25] compiler_4.3.3    fs_1.6.3          Rcpp_1.0.12       pkgconfig_2.0.3  
[29] rstudioapi_0.15.0 later_1.3.2       digest_0.6.33     R6_2.5.1         
[33] utf8_1.2.4        pillar_1.9.0      magrittr_2.0.3    bslib_0.6.1      
[37] tools_4.3.3       cachem_1.0.8