Last updated: 2024-04-08

Checks: 7 0

Knit directory: muse/

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(20200712) 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.

The results in this page were generated with repository version b3568c0. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.

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:


Ignored files:
    Ignored:    .Rproj.user/
    Ignored:    r_packages_4.3.0/
    Ignored:    r_packages_4.3.2/
    Ignored:    r_packages_4.3.3/

Untracked files:
    Untracked:  analysis/breast_cancer.Rmd
    Untracked:  code/multiz100way/
    Untracked:  data/lung_bcell.rds
    Untracked:  data/pbmc3k.csv
    Untracked:  data/pbmc3k.csv.gz
    Untracked:  data/pbmc3k/

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.


These are the previous versions of the repository in which changes were made to the R Markdown (analysis/cellxgene_census.Rmd) and HTML (docs/cellxgene_census.html) files. If you’ve configured a remote Git repository (see ?wflow_git_remote), click on the hyperlinks in the table below to view the files as they were in that past version.

File Version Author Date Message
Rmd b3568c0 Dave Tang 2024-04-08 CELLxGENE Census

Preparation

Following the documentation for the R package {cellxgene.census} which is part of CZ CELLxGENE Discover Census.

{cellxgene.census} provides an API to efficiently access the cloud-hosted Census single-cell data from R. In just a few seconds users can access any slice of Census data using cell or gene filters across hundreds of single-cell datasets.

Census data can be fetched in an iterative fashion for bigger-than-memory slices of data, or quickly exported to basic R structures, as well as {Seurat} or {SingleCellExperiment} objects for downstream analysis.

Install from the R-universe platform. If installing using Ubuntu/Debian, you may need to install the following libraries via APT:

  • libxml2-dev
  • libssl-dev
  • libcurl4-openssl-dev

In addition you must have at least cmake v3.21. Install the {tiledbsoma} dependency first, since this takes some time to compile and install, it’s better to fail first.

install.packages('tiledbsoma', repos = c('https://tiledb-inc.r-universe.dev',
                                         'https://cloud.r-project.org'))

Now install {cellxgene.census}, which should be a breeze now.

install.packages(
  "cellxgene.census",
  repos=c('https://chanzuckerberg.r-universe.dev', 'https://cloud.r-project.org')
)

Now install {Seurat}.

install.packages("Seurat")

Querying the metadata

Querying and fetching the single-cell data and cell/gene metadata

library("cellxgene.census")
packageVersion("cellxgene.census")
[1] '1.13.0'

The human gene metadata of the Census, for RNA assays, is located at census$get("census_data")$get("homo_sapiens")$obs. The mouse cell metadata is at census$get("census_data")$get("mus_musculus").obs.

To learn what metadata columns are available for fetching and filtering we can directly look at the keys of the cell metadata.

census <- open_soma()
The stable Census release is currently 2023-12-15. Specify census_version = "2023-12-15" in future calls to open_soma() to ensure data consistency.
my_keys <- census$get("census_data")$get("homo_sapiens")$obs$colnames()
my_keys
 [1] "soma_joinid"                             
 [2] "dataset_id"                              
 [3] "assay"                                   
 [4] "assay_ontology_term_id"                  
 [5] "cell_type"                               
 [6] "cell_type_ontology_term_id"              
 [7] "development_stage"                       
 [8] "development_stage_ontology_term_id"      
 [9] "disease"                                 
[10] "disease_ontology_term_id"                
[11] "donor_id"                                
[12] "is_primary_data"                         
[13] "self_reported_ethnicity"                 
[14] "self_reported_ethnicity_ontology_term_id"
[15] "sex"                                     
[16] "sex_ontology_term_id"                    
[17] "suspension_type"                         
[18] "tissue"                                  
[19] "tissue_ontology_term_id"                 
[20] "tissue_general"                          
[21] "tissue_general_ontology_term_id"         
[22] "raw_sum"                                 
[23] "nnz"                                     
[24] "raw_mean_nnz"                            
[25] "raw_variance_nnz"                        
[26] "n_measured_vars"                         

soma_joinid is a special SOMADataFrame column that is used for join operations. All of the keys can be used to fetch specific columns or specific rows matching a condition. For the latter we need to know the values we are looking for a priori.

For example let’s see what are the possible values available for sex. To this we can load all cell metadata but fetching only for the column sex; column_names are character vector indicating what metadata columns to fetch.

census$get("census_data")$get("homo_sapiens")$obs$read(column_names = "sex")$concat() |>
  as.data.frame() |>
  unique()
            sex
1          male
224      female
3747640 unknown

With this information we can fetch all cell metadata for a specific sex value, for example “unknown”; the value_filter is an R expression with selection conditions to fetch rows.

census$get("census_data")$get("homo_sapiens")$obs$read(value_filter = "sex == 'unknown'")$concat() |>
  as.data.frame() -> sex_unknown

head(sex_unknown)
  soma_joinid                           dataset_id     assay
1     3747639 9fcb0b73-c734-40a5-be9c-ace7eea401c9 10x 3' v2
2     3747640 9fcb0b73-c734-40a5-be9c-ace7eea401c9 10x 3' v2
3     3747641 9fcb0b73-c734-40a5-be9c-ace7eea401c9 10x 3' v2
4     3747642 9fcb0b73-c734-40a5-be9c-ace7eea401c9 10x 3' v2
5     3747643 9fcb0b73-c734-40a5-be9c-ace7eea401c9 10x 3' v2
6     3747644 9fcb0b73-c734-40a5-be9c-ace7eea401c9 10x 3' v2
  assay_ontology_term_id  cell_type cell_type_ontology_term_id
1            EFO:0009899 fibroblast                 CL:0000057
2            EFO:0009899 fibroblast                 CL:0000057
3            EFO:0009899 fibroblast                 CL:0000057
4            EFO:0009899 fibroblast                 CL:0000057
5            EFO:0009899 fibroblast                 CL:0000057
6            EFO:0009899 fibroblast                 CL:0000057
  development_stage development_stage_ontology_term_id disease
1 human adult stage                     HsapDv:0000087  normal
2 human adult stage                     HsapDv:0000087  normal
3 human adult stage                     HsapDv:0000087  normal
4 human adult stage                     HsapDv:0000087  normal
5 human adult stage                     HsapDv:0000087  normal
6 human adult stage                     HsapDv:0000087  normal
  disease_ontology_term_id                     donor_id is_primary_data
1             PATO:0000461 Pagella_GSE161267_GSM4904134            TRUE
2             PATO:0000461 Pagella_GSE161267_GSM4904134            TRUE
3             PATO:0000461 Pagella_GSE161267_GSM4904134            TRUE
4             PATO:0000461 Pagella_GSE161267_GSM4904134            TRUE
5             PATO:0000461 Pagella_GSE161267_GSM4904134            TRUE
6             PATO:0000461 Pagella_GSE161267_GSM4904134            TRUE
  self_reported_ethnicity self_reported_ethnicity_ontology_term_id     sex
1                 unknown                                  unknown unknown
2                 unknown                                  unknown unknown
3                 unknown                                  unknown unknown
4                 unknown                                  unknown unknown
5                 unknown                                  unknown unknown
6                 unknown                                  unknown unknown
  sex_ontology_term_id suspension_type  tissue tissue_ontology_term_id
1              unknown            cell gingiva          UBERON:0001828
2              unknown            cell gingiva          UBERON:0001828
3              unknown            cell gingiva          UBERON:0001828
4              unknown            cell gingiva          UBERON:0001828
5              unknown            cell gingiva          UBERON:0001828
6              unknown            cell gingiva          UBERON:0001828
  tissue_general tissue_general_ontology_term_id raw_sum  nnz raw_mean_nnz
1         mucosa                  UBERON:0000344     547  329     1.662614
2         mucosa                  UBERON:0000344     982  563     1.744227
3         mucosa                  UBERON:0000344   12467 3809     3.273038
4         mucosa                  UBERON:0000344    1053  566     1.860424
5         mucosa                  UBERON:0000344     548  363     1.509642
6         mucosa                  UBERON:0000344     678  429     1.580420
  raw_variance_nnz n_measured_vars
1        14.559604           31602
2         5.315247           31602
3       109.305683           31602
4         7.430042           31602
5         2.410818           31602
6        11.379616           31602

We can use both column_names and value_filter to perform specific queries. For example fetching the disease column for the cell_type “B cell” in the tissue_general “lung”.

cell_metadata_b_cell <- census$get("census_data")$get("homo_sapiens")$obs$read(
  value_filter = "cell_type == 'B cell' & tissue_general == 'lung'",
  column_names = "disease"
)

cell_metadata_b_cell <- as.data.frame(cell_metadata_b_cell$concat())

table(cell_metadata_b_cell)
disease
chronic obstructive pulmonary disease                              COVID-19 
                                 6369                                  2729 
         hypersensitivity pneumonitis             interstitial lung disease 
                                   52                                   376 
                  lung adenocarcinoma             lung large cell carcinoma 
                                62351                                  1534 
             lymphangioleiomyomatosis         non-small cell lung carcinoma 
                                  133                                 17484 
  non-specific interstitial pneumonia                                normal 
                                  231                                 25461 
                pleomorphic carcinoma                             pneumonia 
                                 1210                                    50 
                  pulmonary emphysema                    pulmonary fibrosis 
                                 1512                                  6798 
                pulmonary sarcoidosis             small cell lung carcinoma 
                                    6                                   583 
         squamous cell lung carcinoma 
                                11920 

Querying expression data as Seurat

Use get_seurat() to perform the same type of filtering but returning a Seurat object.

  • obs_column_names — character vector indicating the columns to select for cell metadata.
  • obs_value_filter — expression with selection conditions to fetch cells meeting a criteria.
  • var_column_names — character vector indicating the columns to select for gene metadata.
  • var_value_filter — expression with selection conditions to fetch genes meeting a criteria.
library("Seurat")

seurat_obj <- get_seurat(
  census, "Homo sapiens",
  obs_column_names = c("cell_type", "tissue_general", "disease", "sex"),
  var_value_filter = "feature_id %in% c('ENSG00000161798', 'ENSG00000188229')",
  obs_value_filter = "cell_type == 'B cell' & tissue_general == 'lung' & disease == 'COVID-19'"
)

saveRDS(object = seurat_obj, file = "data/lung_bcell.rds")

Seurat object.

seurat_obj <- readRDS(file = "data/lung_bcell.rds")
seurat_obj
Loading required package: SeuratObject
Loading required package: sp
'SeuratObject' was built under R 4.3.0 but the current version is
4.3.3; it is recomended that you reinstall 'SeuratObject' as the ABI
for R may have changed

Attaching package: 'SeuratObject'
The following object is masked from 'package:base':

    intersect
An object of class Seurat 
2 features across 2729 samples within 1 assay 
Active assay: RNA (2 features, 0 variable features)
 2 layers present: counts, data

Close the census

After use, the census object should be closed to release memory and other resources. This also closes all SOMA objects accessed via the top-level census. Closing can be automated using on.exit(census$close(), add = TRUE) immediately after census <- open_soma().

census$close()

sessionInfo()
R version 4.3.3 (2024-02-29)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 22.04.4 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/libopenblasp-r0.3.20.so;  LAPACK version 3.10.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: Etc/UTC
tzcode source: system (glibc)

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

other attached packages:
[1] SeuratObject_5.0.1      sp_2.1-3                RcppSpdlog_0.0.16      
[4] cellxgene.census_1.13.0 workflowr_1.7.1        

loaded via a namespace (and not attached):
 [1] dotCall64_1.1-1     spam_2.10-0         xfun_0.43          
 [4] bslib_0.7.0         processx_3.8.4      lattice_0.22-5     
 [7] callr_3.7.6         vctrs_0.6.5         tools_4.3.3        
[10] ps_1.7.6            generics_0.1.3      parallel_4.3.3     
[13] curl_5.2.1          tibble_3.2.1        fansi_1.0.6        
[16] pkgconfig_2.0.3     Matrix_1.6-5        data.table_1.15.4  
[19] assertthat_0.2.1    lifecycle_1.0.4     compiler_4.3.3     
[22] stringr_1.5.1       git2r_0.33.0        codetools_0.2-19   
[25] getPass_0.2-4       tiledbsoma_1.9.4    httpuv_1.6.15      
[28] htmltools_0.5.8.1   nanoarrow_0.4.0.1   sass_0.4.9         
[31] yaml_2.3.8          later_1.3.2         pillar_1.9.0       
[34] jquerylib_0.1.4     whisker_0.4.1       aws.s3_0.3.21      
[37] cachem_1.0.8        RcppCCTZ_0.2.12     tiledb_0.25.0      
[40] parallelly_1.37.1   tidyselect_1.2.1    digest_0.6.35      
[43] future_1.33.2       stringi_1.8.3       listenv_0.9.1      
[46] dplyr_1.1.4         purrr_1.0.2         arrow_15.0.1       
[49] rprojroot_2.0.4     fastmap_1.1.1       grid_4.3.3         
[52] cli_3.6.2           magrittr_2.0.3      base64enc_0.1-3    
[55] triebeard_0.4.1     spdl_0.0.5          utf8_1.2.4         
[58] future.apply_1.11.2 aws.signature_0.6.0 promises_1.3.0     
[61] bit64_4.0.5         nanotime_0.3.7      rmarkdown_2.26     
[64] httr_1.4.7          globals_0.16.3      bit_4.0.5          
[67] progressr_0.14.0    zoo_1.8-12          evaluate_0.23      
[70] knitr_1.46          rlang_1.1.3         urltools_1.7.3     
[73] Rcpp_1.0.12         glue_1.7.0          xml2_1.3.6         
[76] rstudioapi_0.16.0   jsonlite_1.8.8      R6_2.5.1           
[79] fs_1.6.3