Last updated: 2022-10-28

Checks: 6 1

Knit directory: synovialscrnaseq/

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.


The R Markdown file has unstaged changes. To know which version of the R Markdown file created these results, you’ll want to first commit it to the Git repo. If you’re still working on the analysis, you can ignore this warning. When you’re finished, you can run wflow_publish to commit the R Markdown file and build the HTML.

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(20210105) 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 816d5c9. 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:    '/
    Ignored:    .Rhistory
    Ignored:    .Rproj.user/
    Ignored:    .empty/
    Ignored:    analysis/.Rhistory
    Ignored:    analysis/iSEE_interactive_document.html
    Ignored:    code/test_files/
    Ignored:    data/Culemann/
    Ignored:    data/E-MTAB-8322/
    Ignored:    data/Synovial scRNA-seq samples - Sheet1.csv
    Ignored:    data/Zhang_top20_singlecell_cluster_markers_fromGithub.csv
    Ignored:    data/findMarkers_results.rds
    Ignored:    data/findMarkers_results_v2.rds
    Ignored:    data/info/
    Ignored:    data/syn_sce_tidy_filtered.rds
    Ignored:    data/syn_sce_tidy_hvg.rds
    Ignored:    data/syn_sce_tidy_hvg_cms.rds
    Ignored:    docs/
    Ignored:    output/Figures_Paper/
    Ignored:    output/Sample_summaries_RA_comparisons.rds
    Ignored:    output/Sample_summaries_direct_dissociation.rds
    Ignored:    output/Sample_summaries_exvivo_treatment.rds
    Ignored:    output/Suppl_Figure_4d.rds
    Ignored:    output/barcodes.txt
    Ignored:    output/count_matrix_unfiltered.mtx
    Ignored:    output/emptyDrops_result_v4.rds
    Ignored:    output/emptyDrops_result_v4_tmp.rds
    Ignored:    output/emptyDrops_result_v4tmptmp.rds
    Ignored:    output/entropies_fstat_v5_ec.rds
    Ignored:    output/entropies_fstat_v5_main.rds
    Ignored:    output/entropies_fstat_v5_mp.rds
    Ignored:    output/entropies_fstat_v5_sf.rds
    Ignored:    output/entropies_fstat_v5_tc.rds
    Ignored:    output/findMarkers_results_v5_ec.rds
    Ignored:    output/findMarkers_results_v5_main.rds
    Ignored:    output/findMarkers_results_v5_mp.rds
    Ignored:    output/findMarkers_results_v5_sf.rds
    Ignored:    output/findMarkers_results_v5_tc.rds
    Ignored:    output/findMarkers_results_v6.rds
    Ignored:    output/findMarkers_results_v6_ec.rds
    Ignored:    output/findMarkers_results_v6_main.rds
    Ignored:    output/findMarkers_results_v6_mp.rds
    Ignored:    output/findMarkers_results_v6_sf.rds
    Ignored:    output/findMarkers_results_v6_tc.rds
    Ignored:    output/genes.txt
    Ignored:    output/goana_results_v6_ec.rds
    Ignored:    output/goana_results_v6_mp.rds
    Ignored:    output/preprocessing_number_of_cells.rds
    Ignored:    output/syn_v4_sce_emptyDrops_invivo.rds
    Ignored:    output/syn_v4_swappedDrops_24300_after.rds
    Ignored:    output/syn_v4_swappedDrops_24300_before.rds
    Ignored:    output/syn_v4_swappedDrops_24793_after.rds
    Ignored:    output/syn_v4_swappedDrops_24793_before.rds
    Ignored:    output/syn_v5_annot_df_manual.rds
    Ignored:    output/syn_v5_cluster_cellid_match_invivo.rds
    Ignored:    output/syn_v5_clustering_lookup_invivo.rds
    Ignored:    output/syn_v5_clustering_lookup_multiple_invivo.rds
    Ignored:    output/syn_v5_res_da_Accute_inflammation_invivo.rds
    Ignored:    output/syn_v5_res_da_Diagnosis_invivo.rds
    Ignored:    output/syn_v5_res_da_Diagnosis_main_invivo.rds
    Ignored:    output/syn_v5_res_da_Lymphoid_folicles_invivo.rds
    Ignored:    output/syn_v5_res_da_Pathotype_invivo.rds
    Ignored:    output/syn_v5_res_da_Therapy_invivo.rds
    Ignored:    output/syn_v5_res_da_Vascularisation_bin_invivo.rds
    Ignored:    output/syn_v5_res_ds_Accute_inflammation_invivo.rds
    Ignored:    output/syn_v5_res_ds_Diagnosis_invivo.rds
    Ignored:    output/syn_v5_res_ds_Diagnosis_main_invivo.rds
    Ignored:    output/syn_v5_res_ds_Lymphoid_folicles_invivo.rds
    Ignored:    output/syn_v5_res_ds_Pathotype_invivo.rds
    Ignored:    output/syn_v5_res_ds_Therapy_invivo.rds
    Ignored:    output/syn_v5_res_ds_Vascularisation_bin_invivo.rds
    Ignored:    output/syn_v5_sce.rds
    Ignored:    output/syn_v5_sce_ec_invivo.rds
    Ignored:    output/syn_v5_sce_filtered_invivo.rds
    Ignored:    output/syn_v5_sce_hvg_cms_doublet_annot_manual_invivo.rds
    Ignored:    output/syn_v5_sce_hvg_cms_doublet_cmstest_invivo.rds
    Ignored:    output/syn_v5_sce_hvg_cms_doublet_invivo.rds
    Ignored:    output/syn_v5_sce_hvg_cms_doublet_subcluster_invivo.rds
    Ignored:    output/syn_v5_sce_hvg_invivo.rds
    Ignored:    output/syn_v5_sce_mp_invivo.rds
    Ignored:    output/syn_v5_sce_sf_invivo.rds
    Ignored:    output/syn_v5_sce_tc_invivo.rds
    Ignored:    output/syn_v5_vst_out_invivo.rds
    Ignored:    output/syn_v6_cluster_cellid_match_invivo.rds
    Ignored:    output/syn_v6_clustering_lookup_invivo.rds
    Ignored:    output/syn_v6_clustering_lookup_multiple_invivo.rds
    Ignored:    output/syn_v6_sce.rds
    Ignored:    output/syn_v6_sce_Figure8.rds
    Ignored:    output/syn_v6_sce_Figure8_dic_ls.rds
    Ignored:    output/syn_v6_sce_ec_invivo.rds
    Ignored:    output/syn_v6_sce_filtered_invivo.rds
    Ignored:    output/syn_v6_sce_hdf5/
    Ignored:    output/syn_v6_sce_hvg_cms_doublet_invivo.rds
    Ignored:    output/syn_v6_sce_hvg_cms_doublet_subcluster_invivo.rds
    Ignored:    output/syn_v6_sce_hvg_invivo.rds
    Ignored:    output/syn_v6_sce_hvg_marker_genes.rds
    Ignored:    output/syn_v6_sce_mp_invivo.rds
    Ignored:    output/syn_v6_sce_sf_invivo.rds
    Ignored:    output/syn_v6_sce_tc_invivo.rds
    Ignored:    output/syn_v6_sfig1.rds
    Ignored:    output/syn_v6_vst_out_invivo.rds
    Ignored:    output/syn_v7_sce.rds
    Ignored:    output/syn_v7_sce_filtered_invivo.rds
    Ignored:    output/syn_v7_sfig1.rds

Untracked files:
    Untracked:  analysis/scRNAseq_complete_01_preprocessing_comparison.Rmd
    Untracked:  analysis/test.Rmd
    Untracked:  code/rebuild_ezRun.R
    Untracked:  nonhosted_public/
    Untracked:  singRstudio.sh.bak

Unstaged changes:
    Modified:   analysis/scRNAseq_complete_01_preprocessing.Rmd
    Modified:   analysis/scRNAseq_complete_02_HVG_Dimred.Rmd
    Modified:   analysis/scRNAseq_complete_03-2_Subcelltypes_processing.Rmd
    Modified:   analysis/scRNAseq_complete_03-3_Subcelltypes_clustering.Rmd
    Modified:   analysis/scRNAseq_complete_03-4_Subcelltypes_clustering_walktrap.Rmd
    Modified:   analysis/scRNAseq_complete_03_Batch_Clustering_Doublets.Rmd
    Modified:   analysis/scRNAseq_complete_Figures.Rmd
    Modified:   analysis/write_tsv.Rmd

Staged changes:
    Modified:   analysis/scRNAseq_complete_00_ambient_RNA.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/scRNAseq_complete_02_HVG_Dimred.Rmd) and HTML (public/scRNAseq_complete_02_HVG_Dimred.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
html 3443cc6 Reto Gerber 2022-04-25 Update
html b5b139f Reto Gerber 2022-03-29 Update analysis
html 7d99571 Reto Gerber 2022-03-21 update analysis
Rmd 9133ed1 Reto Gerber 2022-03-04 update to v6
html 9133ed1 Reto Gerber 2022-03-04 update to v6
html f2e34e1 Reto Gerber 2021-07-29 Update navbar
Rmd ee1face Reto Gerber 2021-07-29 Add missing scripts
html 222b0d1 Reto Gerber 2021-07-29 Update analysis to v5
Rmd a18fb61 retogerber 2021-05-26 workflow with no cultured and no low quality samples
html a18fb61 retogerber 2021-05-26 workflow with no cultured and no low quality samples
Rmd a301681 retogerber 2021-05-19 update complete analysis, new samples added
html a301681 retogerber 2021-05-19 update complete analysis, new samples added
Rmd 1d92bf1 retogerber 2021-05-03 update nearly complete data workflow
html 1d92bf1 retogerber 2021-05-03 update nearly complete data workflow

Set up

suppressPackageStartupMessages({
  library(dplyr)
  library(ggplot2)
  library(purrr)
  library(stringr)
  library(SummarizedExperiment)
  library(SingleCellExperiment)
  library(scater)
  library(scran)
  library(igraph)
  library(SingleR)
  library(scuttle)
  library(celldex)
  library(ggbeeswarm)
  library(tidySingleCellExperiment)
  library(bluster)
  library(BiocParallel)
})
n_workers <- 20
RhpcBLASctl::blas_set_num_threads(n_workers)
bpparam <- BiocParallel::MulticoreParam(workers=n_workers, RNGseed = 123)

here::here()
[1] "/home/retger/Synovial/synovialscrnaseq"
remove_low_quality_samples <- TRUE
analysis_version <- 7

set.seed(100)

load data from preprocessing

tmpfilename <- paste0("syn_v",analysis_version,"_sce_filtered",dplyr::if_else(remove_low_quality_samples, "_invivo",""),".rds")
syn_sce_tidy_filtered <- readRDS(file = here::here("output",tmpfilename))

SCtransform

transform

set.seed(123)
vst_out <-   sctransform::vst(counts(syn_sce_tidy_filtered), method = "glmGamPoi_offset", n_genes=NULL, 
                              exclude_poisson = TRUE,  return_cell_attr = TRUE, 
                              return_corrected_umi = TRUE,verbosity=1)
Calculating cell attributes from input UMI matrix: log_umi
Total Step 1 genes: 17057
Total overdispersed genes: 16744
Excluding 313 genes from Step 1 because they are not overdispersed.
Variance stabilizing transformation of count matrix of size 17057 by 102758
Model formula is y ~ log_umi
Get Negative Binomial regression parameters per gene
Using 16744 genes, 102758 cells
Setting estimate of  2 genes to inf as theta_mm/theta_mle < 1e-3
# of step1 poisson genes (variance < mean): 0
# of low mean genes (mean < 0.001): 840
Total # of Step1 poisson genes (theta=Inf; variance < mean): 602
Total # of poisson genes (theta=Inf; variance < mean): 910
Calling offset model for all 910 poisson genes
Found 631 outliers - those will be ignored in fitting/regularization step
Ignoring theta inf genes
Replacing fit params for 910 poisson genes by theta=Inf
Second step: Get residuals using fitted parameters for 17057 genes
Computing corrected count matrix for 17057 genes
Calculating gene attributes
Wall clock passed: Time difference of 1.071576 hours
# discard genes that could not be transformed
genes_not_in_vst <- which(!(rownames(syn_sce_tidy_filtered) %in% rownames(vst_out$y)))
if(length(genes_not_in_vst) != 0){
  warning(paste(length(genes_not_in_vst), "genes removed!"))
  syn_sce_tidy_filtered <- syn_sce_tidy_filtered[-genes_not_in_vst, ]
}
assay(syn_sce_tidy_filtered, "vstresiduals") <- vst_out$y

sctransform::plot_model_pars(vst_out, show_theta = TRUE)
Warning: Removed 66976 rows containing missing values (geom_point).
Warning: Removed 68228 rows containing missing values (geom_point).

Version Author Date
9133ed1 Reto Gerber 2022-03-04
222b0d1 Reto Gerber 2021-07-29
# hist(assay(syn_sce_tidy_filtered, "logcounts"),breaks=300, main="All")
par(mfrow=c(1,2))
cat("### Data characteristics {.tabset}\n\n")

Data characteristics

cat("#### RowMax\n\n")

RowMax

cmaxes <- rowMaxs(assay(syn_sce_tidy_filtered, "vstresiduals"))
hist(cmaxes,breaks=100, main="RowMax - vst", xlab="RowMax vstresiduals")
cmaxes <- rowMaxs(assay(syn_sce_tidy_filtered, "logcounts"))
hist(cmaxes,breaks=100, main="RowMax - logcounts", xlab="RowMax logcounts")

Version Author Date
9133ed1 Reto Gerber 2022-03-04
222b0d1 Reto Gerber 2021-07-29
cat("\n\n#### RowMean \n\n")

RowMean

cmean <- rowMeans(assay(syn_sce_tidy_filtered, "vstresiduals"))
hist(cmean,breaks=100, main="RowMean -vst", xlab="RowMean vstresiduals")
cmean <- rowMeans(assay(syn_sce_tidy_filtered, "logcounts"))
hist(cmean,breaks=100, main="RowMean - logcounts", xlab="RowMean logcounts")

Version Author Date
9133ed1 Reto Gerber 2022-03-04
222b0d1 Reto Gerber 2021-07-29
cat("\n\n#### RowVar \n\n")

RowVar

cvar <- rowVars(assay(syn_sce_tidy_filtered, "vstresiduals"))
hist(cvar,breaks=100, main="RowVar - vst", xlab="RowVar vstresiduals")
cvar <- rowVars(assay(syn_sce_tidy_filtered, "logcounts"))
hist(cvar,breaks=100, main="RowVar - logcounts", xlab="RowVar logcounts")

Version Author Date
9133ed1 Reto Gerber 2022-03-04
222b0d1 Reto Gerber 2021-07-29
cat("\n\n#### RowMin \n\n")

RowMin

cvar <- rowMins(assay(syn_sce_tidy_filtered, "vstresiduals"))
hist(cvar,breaks=100, main="RowMin - vst", xlab="RowMin vstresiduals")
cvar <- rowMins(assay(syn_sce_tidy_filtered, "logcounts"))
hist(cvar,breaks=100, main="RowMin - logcounts", xlab="RowMin logcounts")

Version Author Date
9133ed1 Reto Gerber 2022-03-04
222b0d1 Reto Gerber 2021-07-29
cat("\n\n#### COL1A2 \n\n")

COL1A2

hist(assay(syn_sce_tidy_filtered, "vstresiduals")[which(rowData(syn_sce_tidy_filtered)$Symbol == "COL1A2"), ], xlab="vstresiduals", main="COL1A2 - vst", breaks=100)
hist(assay(syn_sce_tidy_filtered, "logcounts")[which(rowData(syn_sce_tidy_filtered)$Symbol == "COL1A2"), ], xlab="logcounts", main="COL1A2 - logcounts", breaks=100)

Version Author Date
9133ed1 Reto Gerber 2022-03-04
222b0d1 Reto Gerber 2021-07-29
cat("\n\n### {-}")

dev.off()

null device 1

HVG subsetting

# only keep highly variable genes
res_var <- sctransform::get_residual_var(vst_out, vst_out$umi_corrected,verbosity=1)
Calculating variance for residuals of type pearson for 17057 genes
hvg_vst <- names(res_var)[order(res_var,decreasing = TRUE)[1:3000]]
rowData(syn_sce_tidy_filtered)$is_hvg_vst <- rownames(syn_sce_tidy_filtered) %in% hvg_vst
# syn_sce_tidy_hvg <- syn_sce_tidy_filtered
# tmpfilename <- paste0("syn_v",analysis_version,"_sce_hvg",dplyr::if_else(remove_low_quality_samples, "_invivo",""),".rds")
# saveRDS(syn_sce_tidy_hvg, file = here::here("output",tmpfilename))

tmpfilename <- paste0("syn_v",analysis_version,"_vst_out",dplyr::if_else(remove_low_quality_samples, "_invivo",""),".rds")
saveRDS(vst_out, file = here::here("output",tmpfilename))
tmpfilename <- paste0("syn_v",analysis_version,"_sce_hvg",dplyr::if_else(remove_low_quality_samples, "_invivo",""),".rds")
syn_sce_tidy_filtered <- readRDS(file = here::here("output",tmpfilename))
cat("### Upsetplot detected HVG {.tabset}\n\n")

Upsetplot detected HVG

cat("#### Unique Sample \n\n")

Unique Sample

  suppressMessages(suppressWarnings(capture.output({tmpsce_nest_sample_unique <- syn_sce_tidy_filtered[rowData(syn_sce_tidy_filtered)$is_hvg_vst,] %>% 
      nest(data=-Sample) 
  })))

character(0)

upsetdat_sample_unique <- purrr::map(seq_along(tmpsce_nest_sample_unique$data), ~{
    ind <- rowSums(counts(tmpsce_nest_sample_unique$data[[.x]]) >0 ) >0
    rownames(rowData(tmpsce_nest_sample_unique$data[[.x]]))[ind]
  }) 
names(upsetdat_sample_unique) <- tmpsce_nest_sample_unique$Sample
UpSetR::upset(UpSetR::fromList(upsetdat_sample_unique),nsets = length(upsetdat_sample_unique), nintersects = 20, order.by = "freq", mb.ratio = c(0.3,0.7))

Version Author Date
7d99571 Reto Gerber 2022-03-21
9133ed1 Reto Gerber 2022-03-04
222b0d1 Reto Gerber 2021-07-29
cat("\n\n")
cat("#### main Diagnosis \n\n")

main Diagnosis

  suppressMessages(suppressWarnings(capture.output({tmpsce_nest_Diagnosis <- syn_sce_tidy_filtered[rowData(syn_sce_tidy_filtered)$is_hvg_vst,] %>% 
    nest(data=-Diagnosis) 
  })))

character(0)

upsetdat_Diagnosis <- purrr::map(seq_along(tmpsce_nest_Diagnosis$data), ~{
    ind <- rowSums(counts(tmpsce_nest_Diagnosis$data[[.x]]) >0 ) >0
    rownames(rowData(tmpsce_nest_Diagnosis$data[[.x]]))[ind]
  }) 
names(upsetdat_Diagnosis) <- tmpsce_nest_Diagnosis$Diagnosis
UpSetR::upset(UpSetR::fromList(upsetdat_Diagnosis),nsets = length(upsetdat_Diagnosis), nintersects = 20, order.by = "freq", mb.ratio = c(0.3,0.7))

Version Author Date
9133ed1 Reto Gerber 2022-03-04
222b0d1 Reto Gerber 2021-07-29
cat("\n\n")
cat("### {-}")

Dimensionality reduction

sce_tmp <- syn_sce_tidy_filtered[rowData(syn_sce_tidy_filtered)$is_hvg_vst,] %>% 
  runPCA(name = "PCA",exprs_values = "vstresiduals") 
ndims <- intrinsicDimension::maxLikGlobalDimEst(as.matrix(reducedDim(sce_tmp, "PCA")), k=20)
reducedDim(sce_tmp,"PCA_reduced") <- reducedDim(sce_tmp,"PCA")[,seq_len(ceiling(ndims$dim.est))]
reducedDimNames(sce_tmp)
[1] "PCA"         "PCA_reduced"
ncol(reducedDim(sce_tmp,"PCA_reduced"))
[1] 8
set.seed(100)
sce_tmp <- sce_tmp %>% 
  runUMAP(name = "UMAP", dimred = "PCA")
set.seed(100)
sce_tmp <- sce_tmp %>%  
  runUMAP(name = "UMAP_reduced", dimred = "PCA_reduced")


reducedDim(syn_sce_tidy_filtered,"PCA_vst") <- reducedDim(sce_tmp,"PCA")
reducedDim(syn_sce_tidy_filtered,"PCA_vst_reduced") <- reducedDim(sce_tmp,"PCA_reduced")
reducedDim(syn_sce_tidy_filtered,"UMAP_vst") <- reducedDim(sce_tmp,"UMAP")
reducedDim(syn_sce_tidy_filtered,"UMAP_vst_reduced") <- reducedDim(sce_tmp,"UMAP_reduced")
n_sam <- length(unique(syn_sce_tidy_filtered$Sample))
splitind <- split(seq_len(n_sam),ceiling(seq(0.01,2.99,length.out = n_sam)))
colind <- unlist(purrr::map(seq_len(ceiling(n_sam/3)), ~ c(splitind[[1]][.x],splitind[[2]][.x],splitind[[3]][.x])))
colind <- colind[!is.na(colind)]
colors_used <- viridis::viridis(n_sam)[colind]

cat("### Dimred plots {.tabset}\n\n")

Dimred plots

cat("#### PCA vst\n\n")

PCA vst

plotReducedDim(syn_sce_tidy_filtered, "PCA_vst", colour_by = "Sample") +
  scale_color_manual(values=colors_used)
Scale for 'colour' is already present. Adding another scale for 'colour',
which will replace the existing scale.

Version Author Date
7d99571 Reto Gerber 2022-03-21
9133ed1 Reto Gerber 2022-03-04
222b0d1 Reto Gerber 2021-07-29
cat("#### UMAP vst\n\n")

UMAP vst

plotReducedDim(syn_sce_tidy_filtered, "UMAP_vst", colour_by = "Sample") +
  scale_color_manual(values=colors_used)
Scale for 'colour' is already present. Adding another scale for 'colour',
which will replace the existing scale.

Version Author Date
7d99571 Reto Gerber 2022-03-21
9133ed1 Reto Gerber 2022-03-04
222b0d1 Reto Gerber 2021-07-29
cat("#### UMAP vst reduced\n\n")

UMAP vst reduced

plotReducedDim(syn_sce_tidy_filtered, "UMAP_vst_reduced", colour_by = "Sample") +
  scale_color_manual(values=colors_used)
Scale for 'colour' is already present. Adding another scale for 'colour',
which will replace the existing scale.

Version Author Date
7d99571 Reto Gerber 2022-03-21
9133ed1 Reto Gerber 2022-03-04
222b0d1 Reto Gerber 2021-07-29
cat("\n\n### {-}")

Scran

HVG subsetting

model per gene variance, get highly variable genes. Plot Mean vs. Variance of normalized log expression values.

bpstart(bpparam)
all_gene_var <-  modelGeneVar(syn_sce_tidy_filtered, block=syn_sce_tidy_filtered$Sample,
                              BPPARAM = bpparam)
bpstop(bpparam)
hvg <- getTopHVGs(all_gene_var, fdr.threshold=0.05)
length(hvg)
[1] 3290
mean_var_comb <- map(unique(syn_sce_tidy_filtered$Sample), ~ {
 all_gene_var$per.block[[.x]] %>% 
  as_tibble %>% 
  mutate(row_names = rownames(all_gene_var$per.block[[.x]]),
         is_hvg = row_names %in% hvg,
         Sample = .x)
  }) %>% 
  purrr::reduce(rbind)

mean_var_comb %>% 
  ggplot() +
    geom_point(aes(x = mean, y = total, color= is_hvg)) +
    geom_line(aes(x=mean, y= tech)) +
    labs(y="Variance",x="Mean expression") +
    facet_wrap(~Sample)

Version Author Date
7d99571 Reto Gerber 2022-03-21
9133ed1 Reto Gerber 2022-03-04
222b0d1 Reto Gerber 2021-07-29
a18fb61 retogerber 2021-05-26
a301681 retogerber 2021-05-19
1d92bf1 retogerber 2021-05-03
syn_sce_tidy_hvg <- syn_sce_tidy_filtered
rowData(syn_sce_tidy_hvg)$is_hvg <- rownames(syn_sce_tidy_hvg) %in% hvg

Plot set of detected genes (of HVG) for different conditions.

cat("### Upsetplot detected HVG {.tabset}\n\n")

Upsetplot detected HVG

cat("#### Unique Sample \n\n")

Unique Sample

  suppressMessages(suppressWarnings(capture.output({ tmpsce_nest_sample_unique <- syn_sce_tidy_hvg[rowData(syn_sce_tidy_hvg)$is_hvg,] %>% 
    nest(data=-Sample) 
  })))

character(0)

upsetdat_sample_unique <- purrr::map(seq_along(tmpsce_nest_sample_unique$data), ~{
    ind <- rowSums(counts(tmpsce_nest_sample_unique$data[[.x]]) >0 ) >0
    rownames(rowData(tmpsce_nest_sample_unique$data[[.x]]))[ind]
  }) 
names(upsetdat_sample_unique) <- tmpsce_nest_sample_unique$Sample
UpSetR::upset(UpSetR::fromList(upsetdat_sample_unique),nsets = length(upsetdat_sample_unique), nintersects = 20, order.by = "freq", mb.ratio = c(0.3,0.7))

Version Author Date
7d99571 Reto Gerber 2022-03-21
9133ed1 Reto Gerber 2022-03-04
222b0d1 Reto Gerber 2021-07-29
a18fb61 retogerber 2021-05-26
a301681 retogerber 2021-05-19
1d92bf1 retogerber 2021-05-03
cat("\n\n")
cat("#### main Diagnosis \n\n")

main Diagnosis

  suppressMessages(suppressWarnings(capture.output({ tmpsce_nest_Diagnosis <- syn_sce_tidy_hvg[rowData(syn_sce_tidy_hvg)$is_hvg,] %>% 
    nest(data=-Diagnosis) 
  })))

character(0)

upsetdat_Diagnosis <- purrr::map(seq_along(tmpsce_nest_Diagnosis$data), ~{
    ind <- rowSums(counts(tmpsce_nest_Diagnosis$data[[.x]]) >0 ) >0
    rownames(rowData(tmpsce_nest_Diagnosis$data[[.x]]))[ind]
  }) 
names(upsetdat_Diagnosis) <- tmpsce_nest_Diagnosis$Diagnosis
UpSetR::upset(UpSetR::fromList(upsetdat_Diagnosis),nsets = length(upsetdat_Diagnosis), nintersects = 20, order.by = "freq", mb.ratio = c(0.3,0.7))

Version Author Date
9133ed1 Reto Gerber 2022-03-04
222b0d1 Reto Gerber 2021-07-29
a18fb61 retogerber 2021-05-26
a301681 retogerber 2021-05-19
1d92bf1 retogerber 2021-05-03
cat("\n\n")
cat("### {-}")

Dimensionality reduction

use intrinsicDimension to get number of PC’s to keep. Run UMAP on reduced PCA.

sce_tmp <- syn_sce_tidy_hvg[rowData(syn_sce_tidy_hvg)$is_hvg,] %>% 
  runPCA(name = "PCA") 
ndims <- intrinsicDimension::maxLikGlobalDimEst(as.matrix(reducedDim(sce_tmp, "PCA")), k=20)
reducedDim(sce_tmp,"PCA_reduced") <- reducedDim(sce_tmp,"PCA")[,seq_len(ceiling(ndims$dim.est))]
reducedDimNames(sce_tmp)
[1] "PCA"              "PCA_vst"          "PCA_vst_reduced"  "UMAP_vst"        
[5] "UMAP_vst_reduced" "PCA_reduced"     
ncol(reducedDim(sce_tmp,"PCA_reduced"))
[1] 16
set.seed(100)
sce_tmp <- sce_tmp %>% 
  runUMAP(name = "UMAP", dimred = "PCA_reduced")


reducedDim(syn_sce_tidy_hvg,"PCA") <- reducedDim(sce_tmp,"PCA")
reducedDim(syn_sce_tidy_hvg,"PCA_reduced") <- reducedDim(sce_tmp,"PCA_reduced")
reducedDim(syn_sce_tidy_hvg,"UMAP") <- reducedDim(sce_tmp,"UMAP")

Plot UMAP per sample.

n_sam <- length(unique(syn_sce_tidy_hvg$Sample))
splitind <- split(seq_len(n_sam),ceiling(seq(0.01,2.99,length.out = n_sam)))
colind <- unlist(purrr::map(seq_len(ceiling(n_sam/3)), ~ c(splitind[[1]][.x],splitind[[2]][.x],splitind[[3]][.x])))
colind <- colind[!is.na(colind)]
colors_used <- viridis::viridis(n_sam)[colind]
cat("### Dimred plots {.tabset}\n\n")

Dimred plots

cat("#### PCA\n\n")

PCA

plotReducedDim(syn_sce_tidy_hvg, "PCA_reduced", colour_by = "Sample") +
  scale_color_manual(values=colors_used)
Scale for 'colour' is already present. Adding another scale for 'colour',
which will replace the existing scale.

Version Author Date
7d99571 Reto Gerber 2022-03-21
9133ed1 Reto Gerber 2022-03-04
222b0d1 Reto Gerber 2021-07-29
a18fb61 retogerber 2021-05-26
a301681 retogerber 2021-05-19
1d92bf1 retogerber 2021-05-03
cat("#### UMAP\n\n")

UMAP

plotReducedDim(syn_sce_tidy_hvg, "UMAP", colour_by = "Sample") +
  scale_color_manual(values=colors_used)
Scale for 'colour' is already present. Adding another scale for 'colour',
which will replace the existing scale.

Version Author Date
7d99571 Reto Gerber 2022-03-21
9133ed1 Reto Gerber 2022-03-04
222b0d1 Reto Gerber 2021-07-29
cat("\n\n### {-}")

Upset comparison sctransform vs. scran

tryCatch({
  hvg_list <- list(vst=rownames(syn_sce_tidy_hvg)[rowData(syn_sce_tidy_hvg)$is_hvg_vst], 
                   scran=rownames(syn_sce_tidy_hvg)[rowData(syn_sce_tidy_hvg)$is_hvg])
  UpSetR::upset(UpSetR::fromList(hvg_list), nintersects = 20, order.by = "freq")
},
error=function(e) e)

Version Author Date
9133ed1 Reto Gerber 2022-03-04
222b0d1 Reto Gerber 2021-07-29
tmpfilename <- paste0("syn_v",analysis_version,"_sce_hvg",dplyr::if_else(remove_low_quality_samples, "_invivo",""),".rds")
saveRDS(syn_sce_tidy_hvg, file = here::here("output",tmpfilename))

3rd Part: Batch removal, Doublets removal, Clustering


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

Matrix products: default
BLAS/LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.8.so

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=C             
 [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       

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

other attached packages:
 [1] gdtools_0.2.3                  BiocParallel_1.24.1           
 [3] bluster_1.0.0                  tidySingleCellExperiment_1.0.0
 [5] ggbeeswarm_0.6.0               celldex_1.0.0                 
 [7] scuttle_1.0.4                  SingleR_1.4.1                 
 [9] igraph_1.2.6                   scran_1.18.7                  
[11] scater_1.18.6                  SingleCellExperiment_1.12.0   
[13] SummarizedExperiment_1.20.0    Biobase_2.50.0                
[15] GenomicRanges_1.42.0           GenomeInfoDb_1.26.7           
[17] IRanges_2.24.1                 S4Vectors_0.28.1              
[19] BiocGenerics_0.36.1            MatrixGenerics_1.2.1          
[21] matrixStats_0.58.0             stringr_1.4.0                 
[23] purrr_0.3.4                    ggplot2_3.3.3                 
[25] dplyr_1.0.4                    workflowr_1.6.2               

loaded via a namespace (and not attached):
  [1] AnnotationHub_2.22.1          systemfonts_1.0.1            
  [3] BiocFileCache_1.14.0          plyr_1.8.6                   
  [5] lazyeval_0.2.2                splines_4.0.3                
  [7] listenv_0.8.0                 digest_0.6.27                
  [9] htmltools_0.5.1.1             viridis_0.5.1                
 [11] fansi_0.4.2                   magrittr_2.0.1               
 [13] memoise_2.0.0                 limma_3.46.0                 
 [15] globals_0.14.0                svglite_1.2.3.2              
 [17] colorspace_2.0-0              blob_1.2.1                   
 [19] rappdirs_0.3.3                xfun_0.21                    
 [21] crayon_1.4.1                  RCurl_1.98-1.2               
 [23] jsonlite_1.7.2                glue_1.4.2                   
 [25] gtable_0.3.0                  zlibbioc_1.36.0              
 [27] XVector_0.30.0                UpSetR_1.4.0                 
 [29] DelayedArray_0.16.3           BiocSingular_1.6.0           
 [31] future.apply_1.7.0            scales_1.1.1                 
 [33] DBI_1.1.1                     edgeR_3.32.1                 
 [35] Rcpp_1.0.6                    viridisLite_0.3.0            
 [37] xtable_1.8-4                  dqrng_0.2.1                  
 [39] bit_4.0.4                     rsvd_1.0.3                   
 [41] yaImpute_1.0-32               htmlwidgets_1.5.3            
 [43] httr_1.4.2                    ellipsis_0.3.1               
 [45] pkgconfig_2.0.3               farver_2.0.3                 
 [47] uwot_0.1.10                   dbplyr_2.1.0                 
 [49] locfit_1.5-9.4                here_1.0.1                   
 [51] tidyselect_1.1.0              labeling_0.4.2               
 [53] rlang_0.4.10                  reshape2_1.4.4               
 [55] later_1.1.0.1                 AnnotationDbi_1.52.0         
 [57] munsell_0.5.0                 BiocVersion_3.12.0           
 [59] tools_4.0.3                   cachem_1.0.4                 
 [61] cli_2.3.0                     generics_0.1.0               
 [63] RSQLite_2.2.3                 ExperimentHub_1.16.1         
 [65] evaluate_0.14                 fastmap_1.1.0                
 [67] yaml_2.2.1                    RhpcBLASctl_0.20-137         
 [69] knitr_1.31                    bit64_4.0.5                  
 [71] fs_1.5.0                      future_1.21.0                
 [73] sparseMatrixStats_1.2.1       whisker_0.4                  
 [75] mime_0.10                     compiler_4.0.3               
 [77] beeswarm_0.2.3                plotly_4.9.3                 
 [79] curl_4.3                      interactiveDisplayBase_1.28.0
 [81] tibble_3.0.6                  statmod_1.4.35               
 [83] glmGamPoi_1.2.0               stringi_1.5.3                
 [85] highr_0.8                     RSpectra_0.16-0              
 [87] lattice_0.20-41               Matrix_1.3-2                 
 [89] vctrs_0.3.6                   pillar_1.4.7                 
 [91] lifecycle_1.0.0               BiocManager_1.30.12          
 [93] RcppAnnoy_0.0.18              BiocNeighbors_1.8.2          
 [95] cowplot_1.1.1                 data.table_1.13.6            
 [97] bitops_1.0-6                  irlba_2.3.3                  
 [99] httpuv_1.5.5                  R6_2.5.0                     
[101] promises_1.2.0.1              gridExtra_2.3                
[103] vipor_0.4.5                   parallelly_1.23.0            
[105] codetools_0.2-18              MASS_7.3-53.1                
[107] assertthat_0.2.1              intrinsicDimension_1.2.0     
[109] rprojroot_2.0.2               withr_2.4.1                  
[111] sctransform_0.3.2.9008        GenomeInfoDbData_1.2.4       
[113] grid_4.0.3                    beachmat_2.6.4               
[115] tidyr_1.1.2                   rmarkdown_2.6                
[117] DelayedMatrixStats_1.12.3     git2r_0.28.0                 
[119] shiny_1.6.0