Last updated: 2022-12-06

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/figure/
    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/findMarkers_results_v7_ec.rds
    Ignored:    output/findMarkers_results_v7_main.rds
    Ignored:    output/findMarkers_results_v7_mp.rds
    Ignored:    output/findMarkers_results_v7_sf.rds
    Ignored:    output/findMarkers_results_v7_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_cluster_cellid_match_invivo.rds
    Ignored:    output/syn_v7_clustering_lookup_invivo.rds
    Ignored:    output/syn_v7_clustering_lookup_multiple_invivo.rds
    Ignored:    output/syn_v7_sce.rds
    Ignored:    output/syn_v7_sce_ec_invivo.rds
    Ignored:    output/syn_v7_sce_filtered_invivo.rds
    Ignored:    output/syn_v7_sce_hdf5/
    Ignored:    output/syn_v7_sce_hvg_cms_doublet_invivo.rds
    Ignored:    output/syn_v7_sce_hvg_cms_doublet_subcluster_invivo.rds
    Ignored:    output/syn_v7_sce_hvg_invivo.rds
    Ignored:    output/syn_v7_sce_mp_invivo.rds
    Ignored:    output/syn_v7_sce_sf_invivo.rds
    Ignored:    output/syn_v7_sce_tc_invivo.rds
    Ignored:    output/syn_v7_sfig1.rds
    Ignored:    output/syn_v7_vst_out_invivo.rds

Untracked files:
    Untracked:  analysis/scRNAseq_complete_01_preprocessing_comparison.Rmd
    Untracked:  analysis/scRNAseq_complete_04_Annotation_v7.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_04-2_celltype_markers.Rmd
    Modified:   analysis/scRNAseq_complete_04-2_celltype_markers_subcelltypes.Rmd
    Modified:   analysis/scRNAseq_complete_Figures.Rmd
    Modified:   analysis/write_tsv.Rmd
    Modified:   code/create_hdf5.R

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_03-2_Subcelltypes_processing.Rmd) and HTML (public/scRNAseq_complete_03-2_Subcelltypes_processing.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
Rmd 7d99571 Reto Gerber 2022-03-21 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

Set up

suppressPackageStartupMessages({
  library(dplyr)
  library(ggplot2)
  library(purrr)
  library(stringr)
  library(SummarizedExperiment)
  library(SingleCellExperiment)
  library(scater)
  library(scran)
  library(igraph)
  library(scuttle)
  library(tidySingleCellExperiment)
  library(bluster)
  library(CellID)
  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)
tmpfilename <- paste0("syn_v",analysis_version,"_sce_hvg_cms_doublet",dplyr::if_else(remove_low_quality_samples, "_invivo",""),".rds")
syn_sce_tidy_hvg_cms <- readRDS(file = here::here("output",tmpfilename))

tmpfilename <- paste0("syn_v",analysis_version,"_cluster_cellid_match",dplyr::if_else(remove_low_quality_samples, "_invivo",""),".rds")
cluster_assignment <- readRDS(file = here::here("output",tmpfilename))
# list(cluster_names_prop=cluster_names_prop,cluster_names_max=cluster_names_max)
cluster_assignment$cluster_names_max
$tc
[1] "4"  "9"  "13" "21"

$ec
[1] "5"  "12"

$sf
[1] "6"  "11" "15" "20"

$mp
[1] "7"  "8"  "14" "16" "18" "22"
cluster_assignment$cluster_names_prop
$tc
[1] "4"  "9"  "13" "21"

$ec
[1] "5"  "12"

$sf
[1] "6"  "11" "15" "20"

$mp
[1] "8"  "14" "18" "22"

Manually add clusters to main celltypes

# cluster 1 to t-cells
cluster_assignment$cluster_names_max$tc <- c(cluster_assignment$cluster_names_prop$tc,"1")


# cluster 2 to macrophages
cluster_assignment$cluster_names_max$mp <- c(cluster_assignment$cluster_names_prop$mp,"2")
if(remove_low_quality_samples){
  merge_order <- list(
                    list("Syn_Bio_053","Syn_Bio_054A",
                         "Syn_Bio_062","Syn_Bio_064"),#Peripheral_Spondyloarthritis
                    list("Syn_Bio_023","Syn_Bio_079","Syn_Bio_092"),#Psoriatic_Arthritis
                    list("Syn_Bio_083","Syn_Bio_084"),#Rheumatoid_Arthritis
                    list("Syn_Bio_074"),#Seronegative_Polyarthritis
                    list("Syn_Bio_026","Syn_Bio_028",
                         list("Syn_Bio_077b","Syn_Bio_077a"),
                         "Syn_Bio_049","Syn_Bio_050", "Syn_Bio_081",
                         "Syn_Bio_093","Syn_Bio_096",
                         list("Syn_Bio_098a","Syn_Bio_098b")
                         ),#Seronegative_Rheumatoid_Arthritis
                    list("Syn_Bio_091","Syn_Bio_099"),#Spondyloarthritis
                    list("Syn_Bio_087"),#To_be_determined
                    list("Syn_Bio_078")#Undiff._Polyarthritis
                    )

} else{
  merge_order <- list(list("Syn_Bio_086"),#special
                      list("Syn_Bio_053","Syn_Bio_054A",
                           "Syn_Bio_062","Syn_Bio_064"),#Peripheral_Spondyloarthritis
                      list("Syn_Bio_023","Syn_Bio_079","Syn_Bio_092"),#Psoriatic_Arthritis
                      list("Syn_Bio_083","Syn_Bio_084"),#Rheumatoid_Arthritis
                      list("Syn_Bio_074"),#Seronegative_Polyarthritis
                      list("Syn_Bio_026","Syn_Bio_028",
                           list("Syn_Bio_077b","Syn_Bio_077a"),
                           "Syn_Bio_049","Syn_Bio_050", "Syn_Bio_081",
                           "Syn_Bio_093","Syn_Bio_096",
                           list("Syn_Bio_098a","Syn_Bio_098b"),
                           list(
                             list("Syn_Bio_055_DMSO","Syn_Bio_072_DMSO","Syn_Bio_094_DMSO"),
                             list("Syn_Bio_055_Tofa","Syn_Bio_072_Tofa","Syn_Bio_094_Tofa"))
                           ),#Seronegative_Rheumatoid_Arthritis
                      list("Syn_Bio_075","Syn_Bio_091","Syn_Bio_099"),#Spondyloarthritis
                      list("Syn_Bio_059","Syn_Bio_080","Syn_Bio_089","Syn_Bio_095"),#Systemic_Sclerosis
                      list("Syn_Bio_087"),#To_be_determined
                      list("Syn_Bio_031","Syn_Bio_078")#Undiff._Polyarthritis
                      )
}

clusters_lookup <- list()

Indiviual analysis

Repeat analysis for the four main cell types of interest.

Subclustering - SF

celltype_name_pre <- "sf"

HVG selection

set.seed(100)

sce_sub <- syn_sce_tidy_hvg_cms[,syn_sce_tidy_hvg_cms$kgraph_clusters %in% cluster_assignment$cluster_names_max[[celltype_name_pre]]]
assay(sce_sub, "vstresiduals") <- NULL
bpstart(bpparam)
all_gene_var <-  modelGeneVar(sce_sub, block=sce_sub$Sample, BPPARAM=bpparam)
bpstop(bpparam)
hvg <- getTopHVGs(all_gene_var, fdr.threshold=0.05)
UpSetR::upset(UpSetR::fromList(list(subset=hvg, all=rownames(syn_sce_tidy_hvg_cms)[rowData(syn_sce_tidy_hvg_cms)$is_hvg])))

Version Author Date
9133ed1 Reto Gerber 2022-03-04
222b0d1 Reto Gerber 2021-07-29
rowData(sce_sub)$is_hvg <- rownames(sce_sub) %in% hvg

Batch correction

bpstart(bpparam)
temp_sce <- batchelor::multiBatchNorm(sce_sub, 
                                      batch=sce_sub$Sample,
                                      subset.row = rownames(sce_sub)[rowData(sce_sub)[["is_hvg"]]],
                                      normalize.all=TRUE,
                    BPPARAM = bpparam)
bpstop(bpparam)
stopifnot(all(unlist(merge_order) %in% unique(sce_sub$Sample)))
stopifnot(all(unique(sce_sub$Sample) %in% unlist(merge_order)))
bpstart(bpparam)
temp_sce <- batchelor::fastMNN(temp_sce, batch=temp_sce$Sample, prop.k=0.02, merge.order = merge_order,
                               subset.row = rownames(temp_sce)[rowData(temp_sce)[["is_hvg"]]], correct.all=TRUE,
                    BPPARAM = bpparam)
bpstop(bpparam)
assay(sce_sub, "reconstructed") <- assay(temp_sce, "reconstructed")
reducedDim(sce_sub, "corrected") <- reducedDim(temp_sce, "corrected")
set.seed(100)
bpstart(bpparam)
sce_sub <- runUMAP(sce_sub, dimred = "corrected", name = "UMAP_corrected",
                                BPPARAM = bpparam)
bpstop(bpparam)

cms

set.seed(123)
bpstart(bpparam)
sce_sub <- CellMixS::cms(sce_sub, k=300, group = "Sample",
                            dim_red = "PCA", res_name = "unaligned",
                            BPPARAM = bpparam)
bpstop(bpparam)
bpstart(bpparam)
sce_sub <- CellMixS::cms(sce_sub, k=300, group = "Sample",
                            dim_red = "corrected", res_name = "MNN",
                            BPPARAM = bpparam)
bpstop(bpparam)
CellMixS::visHist(sce_sub)

Version Author Date
9133ed1 Reto Gerber 2022-03-04
222b0d1 Reto Gerber 2021-07-29
ggpubr::ggarrange(ncol=1,nrow=3,
  plotReducedDim(sce_sub,"UMAP_corrected",  colour_by="Sample"),
  plotReducedDim(sce_sub,"UMAP_corrected",  colour_by="cms.MNN"),
  plotReducedDim(sce_sub,"UMAP_corrected",  colour_by="cms.unaligned")
)

Version Author Date
7d99571 Reto Gerber 2022-03-21
9133ed1 Reto Gerber 2022-03-04
222b0d1 Reto Gerber 2021-07-29

clustering

set.seed(100)
bpstart(bpparam)
graph_sub <- buildSNNGraph(sce_sub, use.dimred="corrected", k=20, BPPARAM = bpparam)
bpstop(bpparam)
clusters <- igraph::cluster_louvain(graph_sub)$membership

colData(sce_sub)[[paste0(celltype_name_pre,"_clusters")]] <- factor(clusters)
clusters_lookup[[celltype_name_pre]] <- data.frame(cell_id = colnames(sce_sub), 
                                                   cluster = paste0(toupper(celltype_name_pre),"_", as.character(clusters)))
plotReducedDim(sce_sub,"UMAP_corrected", 
               colour_by=paste0(celltype_name_pre,"_clusters"), 
               text_by=paste0(celltype_name_pre,"_clusters"))+
  theme(legend.position = c(1.01,0.7),
        legend.background = element_rect(color="grey",fill = "white"),
        legend.margin = margin(10,10,10,10), plot.margin = margin(t=10,r=250,b=0,l=10))

Version Author Date
9133ed1 Reto Gerber 2022-03-04
222b0d1 Reto Gerber 2021-07-29

save

tmpfilename <- paste0("syn_v",analysis_version,"_sce_",celltype_name_pre,dplyr::if_else(remove_low_quality_samples, "_invivo",""),".rds")
saveRDS(sce_sub, file = here::here("output",tmpfilename))

Subclustering - EC

celltype_name_pre <- "ec"

HVG selection

set.seed(100)

sce_sub <- syn_sce_tidy_hvg_cms[,syn_sce_tidy_hvg_cms$kgraph_clusters %in% cluster_assignment$cluster_names_max[[celltype_name_pre]]]
assay(sce_sub, "vstresiduals") <- NULL
bpstart(bpparam)
all_gene_var <-  modelGeneVar(sce_sub, block=sce_sub$Sample, BPPARAM=bpparam)
Warning in regularize.values(x, y, ties, missing(ties), na.rm = na.rm):
collapsing to unique 'x' values
bpstop(bpparam)
hvg <- getTopHVGs(all_gene_var, fdr.threshold=0.05)
UpSetR::upset(UpSetR::fromList(list(subset=hvg, all=rownames(syn_sce_tidy_hvg_cms)[rowData(syn_sce_tidy_hvg_cms)$is_hvg])))

Version Author Date
9133ed1 Reto Gerber 2022-03-04
222b0d1 Reto Gerber 2021-07-29
rowData(sce_sub)$is_hvg <- rownames(sce_sub) %in% hvg

Batch correction

bpstart(bpparam)
temp_sce <- batchelor::multiBatchNorm(sce_sub, 
                                      batch=sce_sub$Sample,
                                      subset.row = rownames(sce_sub)[rowData(sce_sub)[["is_hvg"]]],
                                      normalize.all=TRUE,
                    BPPARAM = bpparam)
bpstop(bpparam)
stopifnot(all(unlist(merge_order) %in% unique(sce_sub$Sample)))
stopifnot(all(unique(sce_sub$Sample) %in% unlist(merge_order)))
bpstart(bpparam)
temp_sce <- batchelor::fastMNN(temp_sce, batch=temp_sce$Sample, prop.k=0.02, merge.order = merge_order,
                               subset.row = rownames(temp_sce)[rowData(temp_sce)[["is_hvg"]]], correct.all=TRUE,
                    BPPARAM = bpparam)
bpstop(bpparam)
assay(sce_sub, "reconstructed") <- assay(temp_sce, "reconstructed")
reducedDim(sce_sub, "corrected") <- reducedDim(temp_sce, "corrected")
set.seed(100)
bpstart(bpparam)
sce_sub <- runUMAP(sce_sub, dimred = "corrected", name = "UMAP_corrected",
                                BPPARAM = bpparam)
bpstop(bpparam)

cms

set.seed(123)
bpstart(bpparam)
sce_sub <- CellMixS::cms(sce_sub, k=300, group = "Sample",
                            dim_red = "PCA", res_name = "unaligned",
                            BPPARAM = bpparam)
bpstop(bpparam)
bpstart(bpparam)
sce_sub <- CellMixS::cms(sce_sub, k=300, group = "Sample",
                            dim_red = "corrected", res_name = "MNN",
                            BPPARAM = bpparam)
bpstop(bpparam)
CellMixS::visHist(sce_sub)

Version Author Date
9133ed1 Reto Gerber 2022-03-04
222b0d1 Reto Gerber 2021-07-29
ggpubr::ggarrange(ncol=1,nrow=3,
  plotReducedDim(sce_sub,"UMAP_corrected",  colour_by="Sample"),
  plotReducedDim(sce_sub,"UMAP_corrected",  colour_by="cms.MNN"),
  plotReducedDim(sce_sub,"UMAP_corrected",  colour_by="cms.unaligned")
)

Version Author Date
7d99571 Reto Gerber 2022-03-21
9133ed1 Reto Gerber 2022-03-04
222b0d1 Reto Gerber 2021-07-29

clustering

set.seed(100)
bpstart(bpparam)
graph_sub <- buildSNNGraph(sce_sub, use.dimred="corrected", k=20, BPPARAM = bpparam)
bpstop(bpparam)
clusters <- igraph::cluster_louvain(graph_sub)$membership

colData(sce_sub)[[paste0(celltype_name_pre,"_clusters")]] <- factor(clusters)
clusters_lookup[[celltype_name_pre]] <- data.frame(cell_id = colnames(sce_sub), 
                                                   cluster = paste0(toupper(celltype_name_pre),"_", as.character(clusters)))
plotReducedDim(sce_sub,"UMAP_corrected", 
               colour_by=paste0(celltype_name_pre,"_clusters"), 
               text_by=paste0(celltype_name_pre,"_clusters"))+
  theme(legend.position = c(1.01,0.7),
        legend.background = element_rect(color="grey",fill = "white"),
        legend.margin = margin(10,10,10,10), plot.margin = margin(t=10,r=250,b=0,l=10))

Version Author Date
9133ed1 Reto Gerber 2022-03-04
222b0d1 Reto Gerber 2021-07-29

save

tmpfilename <- paste0("syn_v",analysis_version,"_sce_",celltype_name_pre,dplyr::if_else(remove_low_quality_samples, "_invivo",""),".rds")
saveRDS(sce_sub, file = here::here("output",tmpfilename))

Subclustering - Macrophages

celltype_name_pre <- "mp"

HVG selection

set.seed(100)

sce_sub <- syn_sce_tidy_hvg_cms[,syn_sce_tidy_hvg_cms$kgraph_clusters %in% cluster_assignment$cluster_names_max[[celltype_name_pre]]]
assay(sce_sub, "vstresiduals") <- NULL
bpstart(bpparam)
all_gene_var <-  modelGeneVar(sce_sub, block=sce_sub$Sample, BPPARAM=bpparam)
bpstop(bpparam)
hvg <- getTopHVGs(all_gene_var, fdr.threshold=0.05)
UpSetR::upset(UpSetR::fromList(list(subset=hvg, all=rownames(syn_sce_tidy_hvg_cms)[rowData(syn_sce_tidy_hvg_cms)$is_hvg])))

Version Author Date
9133ed1 Reto Gerber 2022-03-04
222b0d1 Reto Gerber 2021-07-29
rowData(sce_sub)$is_hvg <- rownames(sce_sub) %in% hvg

Batch correction

bpstart(bpparam)
temp_sce <- batchelor::multiBatchNorm(sce_sub, 
                                      batch=sce_sub$Sample,
                                      subset.row = rownames(sce_sub)[rowData(sce_sub)[["is_hvg"]]],
                                      normalize.all=TRUE,
                    BPPARAM = bpparam)
bpstop(bpparam)
stopifnot(all(unlist(merge_order) %in% unique(sce_sub$Sample)))
stopifnot(all(unique(sce_sub$Sample) %in% unlist(merge_order)))
bpstart(bpparam)
temp_sce <- batchelor::fastMNN(temp_sce, batch=temp_sce$Sample, prop.k=0.02, merge.order = merge_order,
                               subset.row = rownames(temp_sce)[rowData(temp_sce)[["is_hvg"]]], correct.all=TRUE,
                    BPPARAM = bpparam)
bpstop(bpparam)
assay(sce_sub, "reconstructed") <- assay(temp_sce, "reconstructed")
reducedDim(sce_sub, "corrected") <- reducedDim(temp_sce, "corrected")
set.seed(100)
bpstart(bpparam)
sce_sub <- runUMAP(sce_sub, dimred = "corrected", name = "UMAP_corrected",
                                BPPARAM = bpparam)
bpstop(bpparam)

cms

set.seed(123)
bpstart(bpparam)
sce_sub <- CellMixS::cms(sce_sub, k=300, group = "Sample",
                            dim_red = "PCA", res_name = "unaligned",
                            BPPARAM = bpparam)
bpstop(bpparam)
bpstart(bpparam)
sce_sub <- CellMixS::cms(sce_sub, k=300, group = "Sample",
                            dim_red = "corrected", res_name = "MNN",
                            BPPARAM = bpparam)
bpstop(bpparam)
CellMixS::visHist(sce_sub)

Version Author Date
9133ed1 Reto Gerber 2022-03-04
222b0d1 Reto Gerber 2021-07-29
ggpubr::ggarrange(ncol=1,nrow=3,
  plotReducedDim(sce_sub,"UMAP_corrected",  colour_by="Sample"),
  plotReducedDim(sce_sub,"UMAP_corrected",  colour_by="cms.MNN"),
  plotReducedDim(sce_sub,"UMAP_corrected",  colour_by="cms.unaligned")
)

Version Author Date
7d99571 Reto Gerber 2022-03-21
9133ed1 Reto Gerber 2022-03-04
222b0d1 Reto Gerber 2021-07-29

clustering

set.seed(100)
bpstart(bpparam)
graph_sub <- buildSNNGraph(sce_sub, use.dimred="corrected", k=20, BPPARAM = bpparam)
bpstop(bpparam)
clusters <- igraph::cluster_louvain(graph_sub)$membership

colData(sce_sub)[[paste0(celltype_name_pre,"_clusters")]] <- factor(clusters)
clusters_lookup[[celltype_name_pre]] <- data.frame(cell_id = colnames(sce_sub), 
                                                   cluster = paste0(toupper(celltype_name_pre),"_", as.character(clusters)))
plotReducedDim(sce_sub,"UMAP_corrected", 
               colour_by=paste0(celltype_name_pre,"_clusters"), 
               text_by=paste0(celltype_name_pre,"_clusters"))+
  theme(legend.position = c(1.01,0.7),
        legend.background = element_rect(color="grey",fill = "white"),
        legend.margin = margin(10,10,10,10), plot.margin = margin(t=10,r=250,b=0,l=10))

Version Author Date
9133ed1 Reto Gerber 2022-03-04
222b0d1 Reto Gerber 2021-07-29

save

tmpfilename <- paste0("syn_v",analysis_version,"_sce_",celltype_name_pre,dplyr::if_else(remove_low_quality_samples, "_invivo",""),".rds")
saveRDS(sce_sub, file = here::here("output",tmpfilename))

Subclustering - T-cells

celltype_name_pre <- "tc"

HVG selection

set.seed(100)

sce_sub <- syn_sce_tidy_hvg_cms[,syn_sce_tidy_hvg_cms$kgraph_clusters %in% cluster_assignment$cluster_names_max[[celltype_name_pre]]]
assay(sce_sub, "vstresiduals") <- NULL
bpstart(bpparam)
all_gene_var <-  modelGeneVar(sce_sub, block=sce_sub$Sample, BPPARAM=bpparam)
bpstop(bpparam)
hvg <- getTopHVGs(all_gene_var, fdr.threshold=0.05)
UpSetR::upset(UpSetR::fromList(list(subset=hvg, all=rownames(syn_sce_tidy_hvg_cms)[rowData(syn_sce_tidy_hvg_cms)$is_hvg])))

Version Author Date
9133ed1 Reto Gerber 2022-03-04
222b0d1 Reto Gerber 2021-07-29
rowData(sce_sub)$is_hvg <- rownames(sce_sub) %in% hvg

Batch correction

bpstart(bpparam)
temp_sce <- batchelor::multiBatchNorm(sce_sub, 
                                      batch=sce_sub$Sample,
                                      subset.row = rownames(sce_sub)[rowData(sce_sub)[["is_hvg"]]],
                                      normalize.all=TRUE,
                    BPPARAM = bpparam)
bpstop(bpparam)
stopifnot(all(unlist(merge_order) %in% unique(sce_sub$Sample)))
stopifnot(all(unique(sce_sub$Sample) %in% unlist(merge_order)))
bpstart(bpparam)
temp_sce <- batchelor::fastMNN(temp_sce, batch=temp_sce$Sample, prop.k=0.02, merge.order = merge_order,
                               subset.row = rownames(temp_sce)[rowData(temp_sce)[["is_hvg"]]], correct.all=TRUE,
                    BPPARAM = bpparam)
bpstop(bpparam)
assay(sce_sub, "reconstructed") <- assay(temp_sce, "reconstructed")
reducedDim(sce_sub, "corrected") <- reducedDim(temp_sce, "corrected")
set.seed(100)
bpstart(bpparam)
sce_sub <- runUMAP(sce_sub, dimred = "corrected", name = "UMAP_corrected",
                                BPPARAM = bpparam)
bpstop(bpparam)

cms

set.seed(123)
bpstart(bpparam)
sce_sub <- CellMixS::cms(sce_sub, k=300, group = "Sample",
                            dim_red = "PCA", res_name = "unaligned",
                            BPPARAM = bpparam)
bpstop(bpparam)
bpstart(bpparam)
sce_sub <- CellMixS::cms(sce_sub, k=300, group = "Sample",
                            dim_red = "corrected", res_name = "MNN",
                            BPPARAM = bpparam)
bpstop(bpparam)
CellMixS::visHist(sce_sub)

Version Author Date
9133ed1 Reto Gerber 2022-03-04
222b0d1 Reto Gerber 2021-07-29
ggpubr::ggarrange(ncol=1,nrow=3,
  plotReducedDim(sce_sub,"UMAP_corrected",  colour_by="Sample"),
  plotReducedDim(sce_sub,"UMAP_corrected",  colour_by="cms.MNN"),
  plotReducedDim(sce_sub,"UMAP_corrected",  colour_by="cms.unaligned")
)

Version Author Date
7d99571 Reto Gerber 2022-03-21
9133ed1 Reto Gerber 2022-03-04
222b0d1 Reto Gerber 2021-07-29

clustering

set.seed(100)
bpstart(bpparam)
graph_sub <- buildSNNGraph(sce_sub, use.dimred="corrected", k=20, BPPARAM = bpparam)
bpstop(bpparam)
clusters <- igraph::cluster_louvain(graph_sub)$membership

colData(sce_sub)[[paste0(celltype_name_pre,"_clusters")]] <- factor(clusters)
clusters_lookup[[celltype_name_pre]] <- data.frame(cell_id = colnames(sce_sub), 
                                                   cluster = paste0(toupper(celltype_name_pre),"_", as.character(clusters)))
plotReducedDim(sce_sub,"UMAP_corrected", 
               colour_by=paste0(celltype_name_pre,"_clusters"), 
               text_by=paste0(celltype_name_pre,"_clusters"))+
  theme(legend.position = c(1.01,0.7),
        legend.background = element_rect(color="grey",fill = "white"),
        legend.margin = margin(10,10,10,10), plot.margin = margin(t=10,r=250,b=0,l=10))

Version Author Date
9133ed1 Reto Gerber 2022-03-04
222b0d1 Reto Gerber 2021-07-29

save

tmpfilename <- paste0("syn_v",analysis_version,"_sce_",celltype_name_pre,dplyr::if_else(remove_low_quality_samples, "_invivo",""),".rds")
saveRDS(sce_sub, file = here::here("output",tmpfilename))

Combine clustering results

tmpfilename <- paste0("syn_v",analysis_version,"_clustering_lookup",dplyr::if_else(remove_low_quality_samples, "_invivo",""),".rds")
saveRDS(clusters_lookup, here::here("output",tmpfilename))

colData(syn_sce_tidy_hvg_cms)$combined_clusters <- ""

names(clusters_lookup)
[1] "sf" "ec" "mp" "tc"
for(sub_name in names(clusters_lookup)){
  celllabelmatch <- match(clusters_lookup[[sub_name]]$cell_id,
                        colnames(syn_sce_tidy_hvg_cms))
  celllabelmatch <- celllabelmatch[!is.na(celllabelmatch)]
  colData(syn_sce_tidy_hvg_cms)$combined_clusters[celllabelmatch] <-  clusters_lookup[[sub_name]]$cluster
}
colData(syn_sce_tidy_hvg_cms)$combined_clusters[colData(syn_sce_tidy_hvg_cms)$combined_clusters == ""] <-
  colData(syn_sce_tidy_hvg_cms)$kgraph_clusters[colData(syn_sce_tidy_hvg_cms)$combined_clusters == ""]
tmpfilename <- paste0("syn_v",analysis_version,"_sce_hvg_cms_doublet_subcluster",dplyr::if_else(remove_low_quality_samples, "_invivo",""),".rds")
saveRDS(syn_sce_tidy_hvg_cms, file = here::here("output",tmpfilename))

4th Part: Annotation


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] CellID_0.1.0                   SeuratObject_4.0.0            
 [5] Seurat_4.0.0                   bluster_1.0.0                 
 [7] tidySingleCellExperiment_1.0.0 scuttle_1.0.4                 
 [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] scattermore_0.7           ggthemes_4.2.4           
  [3] tidyr_1.1.2               knitr_1.31               
  [5] irlba_2.3.3               DelayedArray_0.16.3      
  [7] data.table_1.13.6         rpart_4.1-15             
  [9] RCurl_1.98-1.2            generics_0.1.0           
 [11] RhpcBLASctl_0.20-137      cowplot_1.1.1            
 [13] RANN_2.6.1                proxy_0.4-24             
 [15] future_1.21.0             spatstat.data_2.0-0      
 [17] httpuv_1.5.5              assertthat_0.2.1         
 [19] viridis_0.5.1             xfun_0.21                
 [21] hms_1.0.0                 evaluate_0.14            
 [23] promises_1.2.0.1          DEoptimR_1.0-8           
 [25] fansi_0.4.2               readxl_1.3.1             
 [27] DBI_1.1.1                 htmlwidgets_1.5.3        
 [29] kSamples_1.2-9            ellipsis_0.3.1           
 [31] RSpectra_0.16-0           backports_1.2.1          
 [33] ggpubr_0.4.0              deldir_0.2-10            
 [35] sparseMatrixStats_1.2.1   vctrs_0.3.6              
 [37] here_1.0.1                TTR_0.24.2               
 [39] ROCR_1.0-11               abind_1.4-5              
 [41] CellMixS_1.6.1            batchelor_1.6.3          
 [43] RcppEigen_0.3.3.9.1       withr_2.4.1              
 [45] robustbase_0.93-7         vcd_1.4-8                
 [47] sctransform_0.3.2.9008    xts_0.12.1               
 [49] goftest_1.2-2             svglite_1.2.3.2          
 [51] cluster_2.1.1             lazyeval_0.2.2           
 [53] laeken_0.5.1              crayon_1.4.1             
 [55] SuppDists_1.1-9.5         edgeR_3.32.1             
 [57] pkgconfig_2.0.3           labeling_0.4.2           
 [59] nlme_3.1-152              vipor_0.4.5              
 [61] nnet_7.3-15               rlang_0.4.10             
 [63] globals_0.14.0            lifecycle_1.0.0          
 [65] miniUI_0.1.1.1            rsvd_1.0.3               
 [67] cellranger_1.1.0          rprojroot_2.0.2          
 [69] polyclip_1.10-0           RcppHNSW_0.3.0           
 [71] lmtest_0.9-38             Matrix_1.3-2             
 [73] carData_3.0-4             boot_1.3-27              
 [75] zoo_1.8-8                 beeswarm_0.2.3           
 [77] whisker_0.4               ggridges_0.5.3           
 [79] png_0.1-7                 viridisLite_0.3.0        
 [81] bitops_1.0-6              KernSmooth_2.23-18       
 [83] DelayedMatrixStats_1.12.3 parallelly_1.23.0        
 [85] rstatix_0.7.0             ggsignif_0.6.0           
 [87] beachmat_2.6.4            scales_1.1.1             
 [89] magrittr_2.0.1            plyr_1.8.6               
 [91] hexbin_1.28.2             ica_1.0-2                
 [93] zlibbioc_1.36.0           compiler_4.0.3           
 [95] dqrng_0.2.1               RColorBrewer_1.1-2       
 [97] pcaMethods_1.82.0         fitdistrplus_1.1-3       
 [99] cli_2.3.0                 XVector_0.30.0           
[101] listenv_0.8.0             patchwork_1.1.1          
[103] pbapply_1.4-3             ggplot.multistats_1.0.0  
[105] MASS_7.3-53.1             mgcv_1.8-34              
[107] tidyselect_1.1.0          stringi_1.5.3            
[109] forcats_0.5.1             highr_0.8                
[111] yaml_2.2.1                BiocSingular_1.6.0       
[113] askpass_1.1               locfit_1.5-9.4           
[115] ggrepel_0.9.1             grid_4.0.3               
[117] fastmatch_1.1-0           tools_4.0.3              
[119] future.apply_1.7.0        rio_0.5.16               
[121] foreign_0.8-81            git2r_0.28.0             
[123] gridExtra_2.3             smoother_1.1             
[125] scatterplot3d_0.3-41      farver_2.0.3             
[127] Rtsne_0.15                digest_0.6.27            
[129] shiny_1.6.0               Rcpp_1.0.6               
[131] broom_0.7.4               car_3.0-10               
[133] later_1.1.0.1             RcppAnnoy_0.0.18         
[135] httr_1.4.2                colorspace_2.0-0         
[137] fs_1.5.0                  tensor_1.5               
[139] ranger_0.12.1             reticulate_1.18          
[141] umap_0.2.7.0              splines_4.0.3            
[143] uwot_0.1.10               statmod_1.4.35           
[145] spatstat.utils_2.0-0      sp_1.4-5                 
[147] systemfonts_1.0.1         plotly_4.9.3             
[149] xtable_1.8-4              jsonlite_1.7.2           
[151] spatstat_1.64-1           UpSetR_1.4.0             
[153] destiny_3.4.0             R6_2.5.0                 
[155] pillar_1.4.7              htmltools_0.5.1.1        
[157] mime_0.10                 tictoc_1.0               
[159] glue_1.4.2                fastmap_1.1.0            
[161] VIM_6.1.0                 BiocNeighbors_1.8.2      
[163] class_7.3-18              codetools_0.2-18         
[165] fgsea_1.16.0              ResidualMatrix_1.0.0     
[167] lattice_0.20-41           tibble_3.0.6             
[169] curl_4.3                  ggbeeswarm_0.6.0         
[171] leiden_0.3.7              zip_2.1.1                
[173] openxlsx_4.2.3            openssl_1.4.3            
[175] survival_3.2-7            limma_3.46.0             
[177] rmarkdown_2.6             munsell_0.5.0            
[179] e1071_1.7-4               GenomeInfoDbData_1.2.4   
[181] haven_2.3.1               reshape2_1.4.4           
[183] gtable_0.3.0