Contents

1 Introduction

scFeatures is a tool for generating multi-view representations of samples in a single-cell dataset. This vignette provides an overview of scFeatures. It uses the main function to generate features and then illustrates case studies of using the generated features for classification, survival analysis and association study.

library(scFeatures)

2 Running scFeatures

scFeatures can be run using one line of code scfeatures_result <- scFeatures(data), which generates a list of dataframes containing all feature types in the form of samples x features.

data("example_scrnaseq" , package = "scFeatures")
data <- example_scrnaseq
data <- process_data(data, normalise = TRUE) #  perform normalisation

scfeatures_result <- scFeatures(data, feature_types = "gene_mean_celltype")

By default, the above function generates all feature types. To reduce the computational time for the demonstrate, here we generate only the selected feature type “gene mean celltype”. More information on the function customisation can be obtained by typing ?scFeatures()

2.1 Classification of conditions using the generated features

To build disease prediction model from the generated features we utilise ClassifyR.

The output from scFeatures is a matrix of sample x feature, ie, the row corresponds to each sample, the column corresponds to the feature, and can be directly used as the X. The order of the rows is in the order of unique(data$sample).

Here we use the feature type gene mean celltype as an example to build classification model on the disease condition.

 
feature_gene_mean_celltype <- scfeatures_result$gene_mean_celltype

# inspect the first 5 rows and first 5 columns
feature_gene_mean_celltype[1:5, 1:5]
#>         Naive T Cells--NOSIP Naive T Cells--LUC7L3 Naive T Cells--ARL6IP4
#> Pre_P8             1.3403199             1.3253486              1.1861664
#> Pre_P27            0.9504078             0.9565850              0.0000000
#> Pre_P7             0.9230278             0.7445286              0.2737271
#> Pre_P20            0.9472109             0.9359819              0.4789126
#> Pre_P3             0.7099657             1.3188926              0.6994015
#>         Naive T Cells--IL2RG Naive T Cells--SNRPD2
#> Pre_P8              1.304897             1.1722461
#> Pre_P27             1.018693             0.0000000
#> Pre_P7              1.462253             0.8139368
#> Pre_P20             1.944960             0.4753097
#> Pre_P3              1.607346             0.4793805

# inspect the dimension of the matrix
dim(feature_gene_mean_celltype)
#> [1]   16 3642

We recommend using ClassifyR::crossValidate to do cross-validated classification with the extracted feaures.

library(ClassifyR)

# X is the feature type generated
# y is the condition for classification
X <- feature_gene_mean_celltype
y <- data@meta.data[!duplicated(data$sample), ]
y <- y[match(rownames(X), y$sample), ]$condition

# run the classification model using random forest
result <- ClassifyR::crossValidate(
    X, y,
    classifier = "randomForest", nCores = 2,
    nFolds = 3, nRepeats = 5
)

ClassifyR::performancePlot(results = result)

It is expected that the classification accuracy is low. This is because we are using a small subset of data containing only 3523 genes and 519 cells. The dataset is unlikely to contain enough information to distinguish responders and non-responders.

2.2 Survival analysis using the generated features

Suppose we want to use the features to perform survival analysis. In here, since the patient outcomes are responder and non-responder, and do not contain survival information, we randomly “generate” the survival outcome for the purpose of demonstration.

We use a standard hierarchical clustering to split the patients into 2 groups based on the generated features.

library(survival)
library(survminer)
 

X <- feature_gene_mean_celltype
X <- t(X)

# run hierarchical clustering
hclust_res <- hclust(
    as.dist(1 - cor(X, method = "pearson")),
    method = "ward.D2"
)

set.seed(1)
# generate some survival outcome, including the survival days and the censoring outcome
survival_day <- sample(1:100, ncol(X))
censoring <- sample(0:1, ncol(X), replace = TRUE)

cluster_res <- cutree(hclust_res, k = 2)
metadata <- data.frame( cluster = factor(cluster_res),
                        survival_day = survival_day,
                        censoring = censoring)

# plot survival curve
fit <- survfit(
    Surv(survival_day, censoring) ~ cluster,
    data = metadata
)
ggsurv <- ggsurvplot(fit,
    conf.int = FALSE, risk.table = TRUE,
    risk.table.col = "strata", pval = TRUE
)
ggsurv

The p-value is very high, indicating there is not enough evidence to claim there is a survival difference between the two groups. This is as expected, because we randomly assigned survival status to each of the patient.

3 Association study of the features with the conditions

scFeatures provides a function that automatically run association study of the features with the conditions and produce an HTML file with the visualisation of the features and the association result.

For this, we would first need to generate the features using scFeatures and then store the result in a named list format.

For demonstration purpose, we provide an example of this features list. The code below show the steps of generating the HTML output from the features list.

# here we use the demo data from the package 
data("scfeatures_result" , package = "scFeatures")

# here we use the current working directory to save the html output
# modify this to save the html file to other directory
output_folder <-  tempdir()

run_association_study_report(scfeatures_result, output_folder )
#> /usr/bin/pandoc +RTS -K512m -RTS output_report.knit.md --to html4 --from markdown+autolink_bare_uris+tex_math_single_backslash --output /tmp/Rtmpwe0FZW/output_report.html --lua-filter /home/biocbuild/bbs-3.17-bioc/R/site-library/rmarkdown/rmarkdown/lua/pagebreak.lua --lua-filter /home/biocbuild/bbs-3.17-bioc/R/site-library/rmarkdown/rmarkdown/lua/latex-div.lua --self-contained --variable bs3=TRUE --section-divs --table-of-contents --toc-depth 3 --variable toc_float=1 --variable toc_selectors=h1,h2,h3 --variable toc_collapsed=1 --variable toc_print=1 --template /home/biocbuild/bbs-3.17-bioc/R/site-library/rmarkdown/rmd/h/default.html --no-highlight --variable highlightjs=1 --number-sections --variable theme=bootstrap --mathjax --variable 'mathjax-url=https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML' --include-in-header /tmp/Rtmpwe0FZW/rmarkdown-str1ec17044bc4273.html --variable code_folding=hide --variable code_menu=1

Inside the directory defined in the output_folder, you will see the html report output with the name output_report.html.

4 sessionInfo()

sessionInfo()
#> R version 4.3.0 RC (2023-04-13 r84269)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Ubuntu 22.04.2 LTS
#> 
#> Matrix products: default
#> BLAS:   /home/biocbuild/bbs-3.17-bioc/R/lib/libRblas.so 
#> LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0
#> 
#> locale:
#>  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
#>  [3] LC_TIME=en_GB              LC_COLLATE=C              
#>  [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] grid      stats4    stats     graphics  grDevices utils     datasets 
#> [8] methods   base     
#> 
#> other attached packages:
#>  [1] data.table_1.14.8           enrichplot_1.20.0          
#>  [3] DOSE_3.26.0                 clusterProfiler_4.8.0      
#>  [5] msigdbr_7.5.1               EnsDb.Hsapiens.v79_2.99.0  
#>  [7] ensembldb_2.24.0            AnnotationFilter_1.24.0    
#>  [9] GenomicFeatures_1.52.0      org.Hs.eg.db_3.17.0        
#> [11] AnnotationDbi_1.62.0        plotly_4.10.1              
#> [13] igraph_1.4.2                tidyr_1.3.0                
#> [15] DT_0.27                     limma_3.56.0               
#> [17] pheatmap_1.0.12             dplyr_1.1.2                
#> [19] reshape2_1.4.4              survminer_0.4.9            
#> [21] ggpubr_0.6.0                ggplot2_3.4.2              
#> [23] ClassifyR_3.4.0             survival_3.5-5             
#> [25] BiocParallel_1.34.0         MultiAssayExperiment_1.26.0
#> [27] SummarizedExperiment_1.30.0 Biobase_2.60.0             
#> [29] GenomicRanges_1.52.0        GenomeInfoDb_1.36.0        
#> [31] IRanges_2.34.0              MatrixGenerics_1.12.0      
#> [33] matrixStats_0.63.0          generics_0.1.3             
#> [35] scFeatures_1.0.0            S4Vectors_0.38.0           
#> [37] BiocGenerics_0.46.0         BiocStyle_2.28.0           
#> 
#> loaded via a namespace (and not attached):
#>   [1] SpatialExperiment_1.10.0    R.methodsS3_1.8.2          
#>   [3] GSEABase_1.62.0             progress_1.2.2             
#>   [5] EnsDb.Mmusculus.v79_2.99.0  goftest_1.2-3              
#>   [7] Biostrings_2.68.0           HDF5Array_1.28.0           
#>   [9] vctrs_0.6.2                 spatstat.random_3.1-4      
#>  [11] digest_0.6.31               png_0.1-8                  
#>  [13] shape_1.4.6                 ggrepel_0.9.3              
#>  [15] deldir_1.0-6                parallelly_1.35.0          
#>  [17] magick_2.7.4                MASS_7.3-59                
#>  [19] qvalue_2.32.0               httpuv_1.6.9               
#>  [21] foreach_1.5.2               withr_2.5.0                
#>  [23] ggfun_0.0.9                 xfun_0.39                  
#>  [25] ellipsis_0.3.2              memoise_2.0.1              
#>  [27] proxyC_0.3.3                commonmark_1.9.0           
#>  [29] gson_0.1.0                  tidytree_0.4.2             
#>  [31] zoo_1.8-12                  GlobalOptions_0.1.2        
#>  [33] gtools_3.9.4                SingleCellSignalR_1.12.0   
#>  [35] pbapply_1.7-0               R.oo_1.25.0                
#>  [37] prettyunits_1.1.1           KEGGREST_1.40.0            
#>  [39] promises_1.2.0.1            httr_1.4.5                 
#>  [41] downloader_0.4              rstatix_0.7.2              
#>  [43] restfulr_0.0.15             globals_0.16.2             
#>  [45] fitdistrplus_1.1-11         rhdf5filters_1.12.0        
#>  [47] rhdf5_2.44.0                miniUI_0.1.1.1             
#>  [49] babelgene_22.9              curl_5.0.0                 
#>  [51] zlibbioc_1.46.0             ggraph_2.1.0               
#>  [53] ScaledMatrix_1.8.0          polyclip_1.10-4            
#>  [55] GenomeInfoDbData_1.2.10     xtable_1.8-4               
#>  [57] stringr_1.5.0               evaluate_0.20              
#>  [59] BiocFileCache_2.8.0         hms_1.1.3                  
#>  [61] bookdown_0.33               irlba_2.3.5.1              
#>  [63] colorspace_2.1-0            filelock_1.0.2             
#>  [65] ROCR_1.0-11                 reticulate_1.28            
#>  [67] spatstat.data_3.0-1         magrittr_2.0.3             
#>  [69] lmtest_0.9-40               ggtree_3.8.0               
#>  [71] viridis_0.6.2               later_1.3.0                
#>  [73] lattice_0.21-8              spatstat.geom_3.1-0        
#>  [75] future.apply_1.10.0         genefilter_1.82.0          
#>  [77] shadowtext_0.1.2            scattermore_0.8            
#>  [79] XML_3.99-0.14               scuttle_1.10.0             
#>  [81] cowplot_1.1.1               RcppAnnoy_0.0.20           
#>  [83] pillar_1.9.0                nlme_3.1-162               
#>  [85] iterators_1.0.14            caTools_1.18.2             
#>  [87] compiler_4.3.0              beachmat_2.16.0            
#>  [89] stringi_1.7.12              tensor_1.5                 
#>  [91] GenomicAlignments_1.36.0    plyr_1.8.8                 
#>  [93] crayon_1.5.2                abind_1.4-5                
#>  [95] BiocIO_1.10.0               gridGraphics_0.5-1         
#>  [97] ggtext_0.1.2                locfit_1.5-9.7             
#>  [99] sp_1.6-0                    graphlayouts_0.8.4         
#> [101] bit_4.0.5                   fastmatch_1.1-3            
#> [103] codetools_0.2-19            BiocSingular_1.16.0        
#> [105] crosstalk_1.2.0             bslib_0.4.2                
#> [107] multtest_2.56.0             mime_0.12                  
#> [109] splines_4.3.0               markdown_1.6               
#> [111] circlize_0.4.15             Rcpp_1.0.10                
#> [113] dbplyr_2.3.2                sparseMatrixStats_1.12.0   
#> [115] HDO.db_0.99.1               gridtext_0.1.5             
#> [117] knitr_1.42                  blob_1.2.4                 
#> [119] utf8_1.2.3                  listenv_0.9.0              
#> [121] DelayedMatrixStats_1.22.0   GSVA_1.48.0                
#> [123] ggplotify_0.1.0             ggsignif_0.6.4             
#> [125] tibble_3.2.1                Matrix_1.5-4               
#> [127] statmod_1.5.0               tweenr_2.0.2               
#> [129] pkgconfig_2.0.3             tools_4.3.0                
#> [131] cachem_1.0.7                RSQLite_2.3.1              
#> [133] viridisLite_0.4.1           DBI_1.1.3                  
#> [135] fastmap_1.1.1               rmarkdown_2.21             
#> [137] scales_1.2.1                ica_1.0-3                  
#> [139] Seurat_4.3.0                Rsamtools_2.16.0           
#> [141] broom_1.0.4                 sass_0.4.5                 
#> [143] patchwork_1.1.2             BiocManager_1.30.20        
#> [145] graph_1.78.0                carData_3.0-5              
#> [147] RANN_2.6.1                  farver_2.1.1               
#> [149] scatterpie_0.1.9            tidygraph_1.2.3            
#> [151] yaml_2.3.7                  rtracklayer_1.60.0         
#> [153] cli_3.6.1                   purrr_1.0.1                
#> [155] leiden_0.4.3                lifecycle_1.0.3            
#> [157] uwot_0.1.14                 bluster_1.10.0             
#> [159] backports_1.4.1             DropletUtils_1.20.0        
#> [161] annotate_1.78.0             gtable_0.3.3               
#> [163] rjson_0.2.21                ggridges_0.5.4             
#> [165] progressr_0.13.0            parallel_4.3.0             
#> [167] ape_5.7-1                   jsonlite_1.8.4             
#> [169] edgeR_3.42.0                bitops_1.0-7               
#> [171] bit64_4.0.5                 Rtsne_0.16                 
#> [173] yulab.utils_0.0.6           spatstat.utils_3.0-2       
#> [175] BiocNeighbors_1.18.0        SeuratObject_4.1.3         
#> [177] ranger_0.15.1               RcppParallel_5.1.7         
#> [179] jquerylib_0.1.4             highr_0.10                 
#> [181] metapod_1.8.0               GOSemSim_2.26.0            
#> [183] dqrng_0.3.0                 survMisc_0.5.6             
#> [185] R.utils_2.12.2              lazyeval_0.2.2             
#> [187] shiny_1.7.4                 htmltools_0.5.5            
#> [189] GO.db_3.17.0                KMsurv_0.1-5               
#> [191] sctransform_0.3.5           rappdirs_0.3.3             
#> [193] glue_1.6.2                  XVector_0.40.0             
#> [195] RCurl_1.98-1.12             treeio_1.24.0              
#> [197] scran_1.28.0                gridExtra_2.3              
#> [199] AUCell_1.22.0               R6_2.5.1                   
#> [201] SingleCellExperiment_1.22.0 gplots_3.1.3               
#> [203] km.ci_0.5-6                 labeling_0.4.2             
#> [205] cluster_2.1.4               Rhdf5lib_1.22.0            
#> [207] aplot_0.1.10                DelayedArray_0.26.0        
#> [209] tidyselect_1.2.0            ProtGenerics_1.32.0        
#> [211] ggforce_0.4.1               xml2_1.3.3                 
#> [213] car_3.1-2                   future_1.32.0              
#> [215] rsvd_1.0.5                  munsell_0.5.0              
#> [217] KernSmooth_2.23-20          fgsea_1.26.0               
#> [219] htmlwidgets_1.6.2           RColorBrewer_1.1-3         
#> [221] biomaRt_2.56.0              rlang_1.1.0                
#> [223] spatstat.sparse_3.0-1       spatstat.explore_3.1-0     
#> [225] fansi_1.0.4