Last updated: 2021-11-11

Checks: 7 0

Knit directory: Multispectral HCC/

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(20210728) 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 e98088d. 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:    .DS_Store
    Ignored:    .Rhistory
    Ignored:    .Rproj.user/
    Ignored:    NMF_26ce5dfd0cab/
    Ignored:    analysis/.DS_Store
    Ignored:    code/
    Ignored:    data/
    Ignored:    old/
    Ignored:    output/.DS_Store
    Ignored:    output/tumor.hc.nmf.rank.1.rds
    Ignored:    output/tumor.hc.nmf.rank.10.rds
    Ignored:    output/tumor.hc.umap.rds

Untracked files:
    Untracked:  core_cluster_fractions.pdf
    Untracked:  output/cores_3/11_18529_T_HCC1_Core[1,11,M]_[61473,19738].im3.png
    Untracked:  output/cores_3/12_291_T_HCC1_Core[1,5,K]_[58929,10236].im3.png
    Untracked:  output/cores_3/13_372_T_HCC1_Core[1,3,A]_[45443,7020].im3.png

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/cores_explore.Rmd) and HTML (docs/cores_explore.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 e98088d Jovan Tanevski 2021-11-11 core analysis of all hepatocytes for one core per patient

Setup

library(tidyverse)
library(skimr)
library(uwot)
library(limma)
library(NMF)
library(factoextra)
library(cowplot)
library(pheatmap)
library(RColorBrewer)
data <- read_csv("data/tumor_hepatocytes.csv", col_types = cols())
tumor.hc <- data %>%
  select(
    `Cytoplasm AGS (Opal 690) Mean (Normalized Counts, Total Weighting)`,
    `Cytoplasm BerEP4 (Opal 650) Mean (Normalized Counts, Total Weighting)`,
    `Cytoplasm CRP (Opal 540) Mean (Normalized Counts, Total Weighting)`,
    `Nucleus p-S6 (Opal 570) Mean (Normalized Counts, Total Weighting)`,
#    `Nucleus beta-cat. (Opal 520) Mean (Normalized Counts, Total Weighting)`
  ) %>%
  `colnames<-`(str_split(colnames(.), " ") %>% map_chr(~ .x[2]) %>% make.names())

skim(tumor.hc)
Data summary
Name tumor.hc
Number of rows 223846
Number of columns 4
_______________________
Column type frequency:
numeric 4
________________________
Group variables None

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
AGS 0 1 0.27 0.41 0 0.07 0.12 0.25 5.23 ▇▁▁▁▁
BerEP4 0 1 1.09 2.19 0 0.21 0.32 0.58 24.08 ▇▁▁▁▁
CRP 0 1 2.51 3.08 0 0.55 1.00 3.50 35.80 ▇▁▁▁▁
p.S6 0 1 1.31 1.25 0 0.58 0.94 1.61 35.24 ▇▁▁▁▁

Detect outliers based on Tukey’s interquartile approach and winsorize. Follow by quantile normalization and ranking to get rid of the effect of abundance

quartiles <- apply(tumor.hc, 2, \(x) quantile(x, c(.25, .75)))
lower <- quartiles[1, ] - 1.5 * (quartiles[2, ] - quartiles[1, ])
upper <- quartiles[2, ] + 1.5 * (quartiles[2, ] - quartiles[1, ])


tumor.hc.winsorized <- tumor.hc %>% imap_dfc(\(x, y){
  x[x < lower[y]] <- x[which.min(abs(x - lower[y]))]
  x[x > upper[y]] <- x[which.min(abs(x - upper[y]))]
  x
})

skim(tumor.hc.winsorized)
Data summary
Name tumor.hc.winsorized
Number of rows 223846
Number of columns 4
_______________________
Column type frequency:
numeric 4
________________________
Group variables None

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
AGS 0 1 0.19 0.16 0 0.07 0.12 0.25 0.52 ▇▅▂▁▃
BerEP4 0 1 0.46 0.35 0 0.21 0.32 0.58 1.15 ▆▇▂▁▃
CRP 0 1 2.31 2.50 0 0.55 1.00 3.50 7.93 ▇▂▁▁▂
p.S6 0 1 1.21 0.83 0 0.58 0.94 1.61 3.16 ▆▇▃▂▂
tumor.hc.norm <- normalizeQuantiles(data.frame(tumor.hc.winsorized))

skim(tumor.hc.norm)
Data summary
Name tumor.hc.norm
Number of rows 223846
Number of columns 4
_______________________
Column type frequency:
numeric 4
________________________
Group variables None

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
AGS 0 1 1.06 0.99 0 0.35 0.6 1.49 3.19 ▇▃▁▁▂
BerEP4 0 1 1.07 1.00 0 0.35 0.6 1.49 3.15 ▇▃▂▁▂
CRP 0 1 1.04 0.96 0 0.35 0.6 1.49 3.19 ▇▃▁▁▂
p.S6 0 1 1.04 0.96 0 0.35 0.6 1.49 3.19 ▇▃▁▁▂
tumor.hc.rank <- mutate_all(tumor.hc.winsorized, ~ rank(., ties.method = "min"))

Pilot run. Subsample 10% of the original data.

set.seed(42)
percent <- 10
subsamp <- sample(nrow(tumor.hc.rank), (percent / 100) * nrow(tumor.hc.rank))

Dimensionality reduction

cache <- "output/tumor.hc.umap.rds"
if (file.exists(cache)) {
  tumor.hc.umap <- read_rds(cache)
} else {
  tumor.hc.umap <- umap(tumor.hc.norm, n_neighbors = 100, 
                        min_dist = 0.2, n_threads = 7)
  write_rds(tumor.hc.umap, cache, "gz")
}

Check if sample is representative in UMAP space

tumor.hc.umap.sample <-
  tumor.hc.umap %>%
  `colnames<-`(c("U1", "U2")) %>%
  as_tibble()

all <- ggplot(tumor.hc.umap.sample, aes(x = U1, y = U2)) +
  geom_point(size = 0.5) +
  theme_classic()

sampled <- ggplot(tumor.hc.umap.sample %>% slice(subsamp), aes(x = U1, y = U2)) +
  geom_point(color = "darkgreen", size = 0.5) +
  theme_classic()

unsampled <- ggplot(tumor.hc.umap.sample %>% slice(-subsamp), aes(x = U1, y = U2)) +
  geom_point(color = "darkred", size = 0.5) +
  theme_classic()

plot_grid(all, sampled, unsampled)

Consensus NMF

We use an efficient implementation of alternating non negative least-squares with regularized to favor sparse coefficient matrices snmf/r. In this way we aim for cleaner clustering.

cache <- paste0("output/tumor.hc.nmf.rank.", percent, ".rds")

if (file.exists(cache)) {
  tumor.hc.nmf.rank <- read_rds(cache)
} else {
  tumor.hc.nmf.rank <- nmfEstimateRank(as.matrix(t(tumor.hc.rank[subsamp, ])),
    range = seq(2, 5), method = "snmf/r",
    nrun = 10, seed = 42, verbose = TRUE,
    .options = "mp5"
  )
  write_rds(tumor.hc.nmf.rank, cache, "gz")
}

plot(tumor.hc.nmf.rank)
Warning: Removed 2 rows containing missing values (geom_point).


sessionInfo()
R version 4.1.1 (2021-08-10)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Big Sur 10.16

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

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

other attached packages:
 [1] RColorBrewer_1.1-2  pheatmap_1.0.12     cowplot_1.1.1      
 [4] factoextra_1.0.7    NMF_0.23.0          synchronicity_1.3.5
 [7] bigmemory_4.5.36    Biobase_2.54.0      BiocGenerics_0.40.0
[10] cluster_2.1.2       rngtools_1.5.2      pkgmaker_0.32.2    
[13] registry_0.5-1      limma_3.50.0        uwot_0.1.10        
[16] Matrix_1.3-4        skimr_2.1.3         forcats_0.5.1      
[19] stringr_1.4.0       dplyr_1.0.7         purrr_0.3.4        
[22] readr_2.0.2         tidyr_1.1.4         tibble_3.1.6       
[25] ggplot2_3.3.5       tidyverse_1.3.1     workflowr_1.6.2    

loaded via a namespace (and not attached):
 [1] fs_1.5.0            bit64_4.0.5         lubridate_1.8.0    
 [4] doParallel_1.0.16   httr_1.4.2          rprojroot_2.0.2    
 [7] repr_1.1.3          tools_4.1.1         backports_1.3.0    
[10] bslib_0.3.1         utf8_1.2.2          R6_2.5.1           
[13] DBI_1.1.1           colorspace_2.0-2    withr_2.4.2        
[16] tidyselect_1.1.1    bit_4.0.4           compiler_4.1.1     
[19] git2r_0.28.0        cli_3.1.0           rvest_1.0.2        
[22] xml2_1.3.2          labeling_0.4.2      sass_0.4.0         
[25] scales_1.1.1        digest_0.6.28       rmarkdown_2.11     
[28] base64enc_0.1-3     pkgconfig_2.0.3     htmltools_0.5.2    
[31] highr_0.9           dbplyr_2.1.1        fastmap_1.1.0      
[34] rlang_0.4.12        readxl_1.3.1        rstudioapi_0.13    
[37] farver_2.1.0        jquerylib_0.1.4     generics_0.1.1     
[40] jsonlite_1.7.2      vroom_1.5.6         magrittr_2.0.1     
[43] Rcpp_1.0.7          munsell_0.5.0       fansi_0.5.0        
[46] lifecycle_1.0.1     stringi_1.7.5       whisker_0.4        
[49] yaml_2.2.1          plyr_1.8.6          grid_4.1.1         
[52] ggrepel_0.9.1       parallel_4.1.1      promises_1.2.0.1   
[55] bigmemory.sri_0.1.3 crayon_1.4.2        lattice_0.20-45    
[58] haven_2.4.3         hms_1.1.1           knitr_1.36         
[61] pillar_1.6.4        uuid_1.0-3          reshape2_1.4.4     
[64] codetools_0.2-18    reprex_2.0.1        glue_1.5.0         
[67] evaluate_0.14       modelr_0.1.8        vctrs_0.3.8        
[70] tzdb_0.2.0          httpuv_1.6.3        foreach_1.5.1      
[73] cellranger_1.1.0    gtable_0.3.0        assertthat_0.2.1   
[76] xfun_0.28           gridBase_0.4-7      xtable_1.8-4       
[79] broom_0.7.10        later_1.3.0         iterators_1.0.13   
[82] ellipsis_0.3.2