Last updated: 2022-01-06

Checks: 7 0

Knit directory: cacoaAnalysis/

This reproducible R Markdown analysis was created with workflowr (version 1.6.2). 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(20211123) 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 13491ea. 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:    .Rhistory
    Ignored:    .Rproj.user/
    Ignored:    analysis/.Rhistory
    Ignored:    analysis/figure_cluster_based_de_bio.nb.html
    Ignored:    analysis/figure_cluster_free_expression.nb.html
    Ignored:    analysis/figure_compositional.nb.html
    Ignored:    analysis/figure_compositional_cf.nb.html
    Ignored:    analysis/figure_expression_shifts.nb.html
    Ignored:    analysis/figure_heterogeneity.nb.html
    Ignored:    analysis/figure_interpretation.nb.html
    Ignored:    analysis/prepare_cacoa_results.nb.html
    Ignored:    analysis/preprocess.nb.html
    Ignored:    analysis/report_asd.nb.html
    Ignored:    analysis/report_az.nb.html
    Ignored:    analysis/report_ep.nb.html
    Ignored:    analysis/report_ms.nb.html
    Ignored:    analysis/report_pf.nb.html
    Ignored:    analysis/report_scc.nb.html
    Ignored:    analysis/simulation_distances.nb.html
    Ignored:    analysis/simulation_variance.nb.html
    Ignored:    cache/
    Ignored:    data/ASD/
    Ignored:    data/AZ/
    Ignored:    data/EP/
    Ignored:    data/MS/
    Ignored:    data/PF/
    Ignored:    data/SCC/
    Ignored:    man/
    Ignored:    output/figures/

Unstaged changes:
    Modified:   analysis/figure_interpretation.Rmd

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/figure_compositional_cf.Rmd) and HTML (docs/figure_compositional_cf.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 96b366c viktor_petukhov 2021-12-13 Updated cluster-free compositional figure
Rmd d864029 viktor_petukhov 2021-12-10 Compositional figure notebooks

PF

cao_pf <- DataPath("PF/cao.rds") %>% readr::read_rds() %>% Cacoa$new()
cao_pf$plot.theme %<>% {. + p_theme}
leg_theme <- theme(legend.key.height=unit(10, "pt"), legend.text=element_text(size=9), 
                   legend.background=element_blank())
leg_theme_small <- leg_theme + theme(legend.key.width=unit(6, "pt"), legend.key.height=unit(6, "pt"))
gene_lab_size <- 12

p_lims <- list("Endothelial Cells", c("Macrophages", "cDCs", "Monocytes")) %>% lapply(function(ct) {
  getCellTypeEmbeddingLimits(cao_pf$embedding, cell.groups=cao_pf$cell.groups, groups.to.plot=ct, quant=0.004)
})

p_genes <- c("SPP1", "DKK2", "COL15A1", "CPE")

gg_markers <- mapply(function(g, pl) {
  p.cols <- cao_pf$cache$joint.count.matrix.norm[,g] %>% pmin(., quantile(., 0.995))
  cao_pf$data.object$plotGraph(colors=p.cols, size=0.2, alpha=0.1, show.legend=TRUE, 
                               legend.position=c(1, 0), legend.title="Expr.") +
    leg_theme_small + lims(x=pl$x, y=pl$y) +
    scale_color_gradientn(colors=cacoa:::dark.red.palette(100), 
                          breaks=seq(0, signif(max(p.cols) / 3, 1) * 3, length.out=4)) +
    theme(panel.grid=element_blank())
}, p_genes, list(p_lims[[2]], p_lims[[1]], p_lims[[1]], p_lims[[1]]), SIMPLIFY=FALSE)

gg_markers %>%
  plot_grid(plotlist=., labels=p_genes, nrow=2, label_fontface="italic", label_x=0.05,
            hjust=0, label_size=gene_lab_size)

cao_pf$estimateDiffCellDensity(type='permutation', n.permutations=1000, verbose=TRUE, 
                               n.cores=45, name='cell.density.kde')
gg_pf_emb <- cao_pf$plotEmbedding(color.by='cell.groups', font.size=c(2, 3.5), size=0.25, alpha=0.1) +
  theme(panel.grid=element_blank())

ggs_pf_dens <- cao_pf$plotCellDensity(
  add.points=FALSE, show.grid=TRUE, show.cell.groups=FALSE, legend.title="Density",
  name='cell.density.kde'
) %>% lapply(`+`, theme(plot.title=element_blank()))

c_pal <- c("#FFFFFF", brewerPalette("YlOrRd", rev=FALSE)(9)) %>% colorRampPalette()
pf_dens_col <- sccore::val2ggcol(ggs_pf_dens[[1]]$data$z, palette=c_pal, return.fill=TRUE, 
                                 color.range=c(0, 1e-4), breaks=seq(0, 1.2e-4, length.out=4))

ggs_pf_dens %<>% lapply(`+`, pf_dens_col)
ggs_pf_dens[[1]] %<>% {. + theme_legend_position(c(0, 0)) + leg_theme_small + 
    theme(legend.text=element_text(size=8))}

gg_pf_diff <- cao_pf$plotDiffCellDensity(adjust.pvalues=FALSE, legend.position=c(0, 1), 
                                         color.range=c("1%", "99%"), name='cell.density.kde',
                                         type='permutation')
gg_pf_diff_adj <- cao_pf$plotDiffCellDensity(adjust.pvalues=TRUE, legend.position=c(0, 1), min.z=0.5,
                                             name='cell.density.kde', type='permutation')

gg_prop_change <- cao_pf$plotDiffCellDensity(type="subtract", legend.position=c(0, 1), 
                                             color.range=c("1%", "99%"), name='cell.density.kde') +
  leg_theme

plot_grid(plotlist=rast(ggs_pf_dens), ncol=2)

MS

cao_ms <- DataPath("MS/cao.rds") %>% readr::read_rds() %>% Cacoa$new()
cao_ms$plot.theme %<>% {. + p_theme}
gg_ms_emb <- cao_ms$plotEmbedding(color.by='cell.groups', font.size=c(2.5, 4.5), size=0.25, alpha=0.1) +
  theme(panel.grid=element_blank())

gg_ms_diff_adj <- cao_ms$plotDiffCellDensity(
  adjust.pvalues=TRUE, legend.position=c(0, 0), min.z=0.5, size=0.1, alpha=0.1,
  name="cell.density.graph"
) + leg_theme
p_lims_ms <- getCellTypeEmbeddingLimits(
  cao_ms$embedding, cell.groups=cao_ms$cell.groups, groups.to.plot="Astrocytes", quant=0.01
)

p_genes_ms <- c("GFAP", "CD44", "TNC")
astro_lims <- lims(x=p_lims_ms$x, y=p_lims_ms$y)
col_range <- c(0, 0.8)

gg_ms_markers <- lapply(p_genes_ms, function(g) {
  p.cols <- cao_ms$cache$joint.count.matrix.norm[,g]# %>% pmin(., quantile(., 0.995))
  cao_ms$plotEmbedding(colors=p.cols, size=0.2, alpha=0.1, show.legend=TRUE, 
                       legend.position=c(1, 0), legend.title="Expr.", color.range=col_range,
                       palette=cacoa:::dark.red.palette) + astro_lims
})

gg_ms_markers[2:3] %<>% lapply(`+`, theme(legend.position="none"))

adj_list <- list(astro_lims, theme(plot.title=element_blank()))
gg_shisa_markers <- cao_ms$plotGeneExpressionComparison(
  "SHISA9", plots="expression", build.panel=FALSE, size=0.2, alpha=0.5, 
  legend.position="none", adj.list=adj_list, max.expr=col_range[2]
)

cd44_ast <- cao_ms$cell.groups %>% {names(.)[. == "Astrocytes"]} %>% 
  .[cao_ms$test.results$cell.density.graph$diff$wilcox$adj[.] > 1]

cm_agg <- cao_ms$cache$joint.count.matrix[cd44_ast,] %>% 
  sccore::colSumByFactor(cao_ms$sample.per.cell[cd44_ast])

p_df <- (cm_agg[,"SHISA9"] / rowSums(cm_agg)) %>% 
  .[!is.na(.)] %>% {tibble(expr=1000 * ., sg=cao_ms$sample.groups[names(.)], type=names(.))}

gg_shisa_box <- ggplot(p_df, aes(x=sg, y=expr, fill=sg)) +
  geom_boxplot(outlier.size=0) +
  ggbeeswarm::geom_quasirandom(size=1) +
  ylab("SHISA9 expr.") +
  scale_fill_manual(values=cao_ms$sample.groups.palette) +
  theme(legend.position="none", axis.title.x=element_blank(), panel.grid.major.x=element_blank(),
        panel.grid.minor=element_blank())

gg_mark_list <- c(
  gg_ms_markers, 
  gg_shisa_markers[c(2, 1)], 
  list(gg_shisa_box)
  # list(gg_shisa_box + theme(plot.margin=margin(l=3)))
) %>% lapply(rast) %>% lapply(`+`, theme(panel.grid=element_blank()))

gg_mark_list[[1]] %<>% {. + leg_theme_small}

Compile figure

theme_nm <- theme(
  plot.margin=margin(), panel.border=element_rect(size=0.25)
)

scale_row <- 0.97
scale_inner <- 0.99
plot_grid(
  plot_grid(
    ggplot() + theme_nothing(),
    rast(gg_pf_emb, dpi=75) + theme_nm,
    plot_grid(
      plotlist=rast(lapply(ggs_pf_dens, `+`, theme_nm)), labels=names(ggs_pf_dens),
      label_fontface="italic", label_x=0.05, hjust=0, label_size=gene_lab_size,
      ncol=1, scale=scale_inner
    ),
    ncol=3, rel_widths=c(1.5, 2, 1), scale=scale_row
  ),
  plot_grid(
    rast(gg_prop_change) + leg_theme + theme_nm + theme(plot.margin=margin(l=2)),
    rast(gg_pf_diff) + leg_theme + theme_nm,
    rast(gg_pf_diff_adj) + leg_theme + theme_nm,
    plot_grid(
      plotlist=rast(lapply(gg_markers, `+`, theme_nm)), labels=p_genes,
      label_fontface="italic", label_x=0.05, hjust=0, label_size=gene_lab_size, ncol=2,
      scale=scale_inner
    ),
    nrow=1, scale=scale_row
  ),
  plot_grid(
    rast(gg_ms_emb) + theme_nm,
    rast(gg_ms_diff_adj) + theme_nm,
    plot_grid(
      plotlist=rast(lapply(gg_mark_list, `+`, theme_nm)),
      labels=c(p_genes_ms, "SHISA9, CNT", "SHISA9, MS"),
      label_fontface="italic", label_x=0.05, hjust=0, label_size=gene_lab_size,
      nrow=2, scale=scale_inner
    ),
    nrow=1, rel_widths=c(1, 1, 1.5), scale=scale_row
  ),
  nrow=3, rel_heights=c(1.7, 1, 1.1), scale=0.98
)

ggsave(figurePath("2_composition_cf.pdf"))

sessionInfo()
R version 4.0.3 (2020-10-10)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.6 LTS

Matrix products: default
BLAS:   /usr/local/R/R-4.0.3/lib/R/lib/libRblas.so
LAPACK: /usr/local/R/R-4.0.3/lib/R/lib/libRlapack.so

locale:
[1] C

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

other attached packages:
 [1] cacoaAnalysis_0.1.0 dataorganizer_0.1.0 cowplot_1.1.1      
 [4] reshape2_1.4.4      ggrastr_1.0.1       ggpubr_0.4.0       
 [7] cacoa_0.2.0         Matrix_1.2-18       magrittr_2.0.1     
[10] forcats_0.5.1       stringr_1.4.0       dplyr_1.0.7        
[13] purrr_0.3.4         readr_1.4.0         tidyr_1.1.4        
[16] tibble_3.1.5        ggplot2_3.3.5       tidyverse_1.3.0    
[19] workflowr_1.6.2    

loaded via a namespace (and not attached):
  [1] N2R_0.1.1            readxl_1.3.1         backports_1.2.1     
  [4] circlize_0.4.13      systemfonts_1.0.0    plyr_1.8.6          
  [7] igraph_1.2.6         splines_4.0.3        usethis_1.6.3       
 [10] urltools_1.7.3       digest_0.6.28        foreach_1.5.1       
 [13] htmltools_0.5.2      fansi_0.5.0          RMTstat_0.3         
 [16] memoise_2.0.0        cluster_2.1.0        doParallel_1.0.16   
 [19] openxlsx_4.2.3       remotes_2.2.0        ComplexHeatmap_2.9.4
 [22] modelr_0.1.8         matrixStats_0.61.0   R.utils_2.10.1      
 [25] sccore_1.0.1         prettyunits_1.1.1    colorspace_2.0-2    
 [28] rvest_0.3.6          ggrepel_0.9.1        pagoda2_1.0.7       
 [31] textshaping_0.2.1    haven_2.4.1          xfun_0.26           
 [34] callr_3.5.1          crayon_1.4.1         jsonlite_1.7.2      
 [37] brew_1.0-6           iterators_1.0.13     ape_5.5             
 [40] glue_1.4.2           gtable_0.3.0         GetoptLong_1.0.5    
 [43] car_3.0-10           leidenAlg_0.1.0      pkgbuild_1.1.0      
 [46] Rook_1.1-1           shape_1.4.6          BiocGenerics_0.36.1 
 [49] abind_1.4-5          scales_1.1.1         DBI_1.1.1           
 [52] rstatix_0.7.0        Rcpp_1.0.7           clue_0.3-59         
 [55] reticulate_1.22      foreign_0.8-80       stats4_4.0.3        
 [58] httr_1.4.2           RColorBrewer_1.1-2   ellipsis_0.3.2      
 [61] farver_2.1.0         R.methodsS3_1.8.1    pkgconfig_2.0.3     
 [64] dbplyr_2.0.0         utf8_1.2.2           labeling_0.4.2      
 [67] tidyselect_1.1.1     rlang_0.4.11         later_1.1.0.1       
 [70] pbmcapply_1.5.0      munsell_0.5.0        cellranger_1.1.0    
 [73] tools_4.0.3          cachem_1.0.6         cli_3.0.1           
 [76] generics_0.1.0       devtools_2.3.2       broom_0.7.9         
 [79] evaluate_0.14        fastmap_1.1.0        ragg_0.4.1          
 [82] yaml_2.2.1           processx_3.4.5       knitr_1.36          
 [85] fs_1.5.0             zip_2.2.0            nlme_3.1-149        
 [88] whisker_0.4          R.oo_1.24.0          grr_0.9.5           
 [91] xml2_1.3.2           compiler_4.0.3       rstudioapi_0.13     
 [94] beeswarm_0.4.0       curl_4.3.2           png_0.1-7           
 [97] testthat_3.0.0       ggsignif_0.6.1       reprex_0.3.0        
[100] stringi_1.7.5        highr_0.9            drat_0.1.8          
[103] ps_1.4.0             desc_1.3.0           lattice_0.20-41     
[106] vctrs_0.3.8          pillar_1.6.3         lifecycle_1.0.1     
[109] triebeard_0.3.0      jquerylib_0.1.4      GlobalOptions_0.1.2 
[112] irlba_2.3.3          data.table_1.14.2    Matrix.utils_0.9.8  
[115] httpuv_1.5.4         conos_1.4.4          R6_2.5.1            
[118] promises_1.1.1       gridExtra_2.3        rio_0.5.26          
[121] vipor_0.4.5          IRanges_2.24.1       sessioninfo_1.1.1   
[124] codetools_0.2-16     MASS_7.3-53          assertthat_0.2.1    
[127] pkgload_1.2.1        rprojroot_2.0.2      rjson_0.2.20        
[130] withr_2.4.2          S4Vectors_0.28.1     mgcv_1.8-33         
[133] parallel_4.0.3       hms_1.1.1            grid_4.0.3          
[136] rmarkdown_2.11       dendsort_0.3.3       carData_3.0-4       
[139] Rtsne_0.15           git2r_0.27.1         lubridate_1.7.9.2   
[142] ggbeeswarm_0.6.0