# NOT RUN {
## Seurat Version 2
if (requireNamespace("Seurat", quietly = TRUE)) {
pkg_version <- packageVersion('Seurat')
if (pkg_version >= "2.0" & pkg_version < "3.0") {
data(cell_lines_small_seurat_v2)
seuratObject <- RunHarmony(cell_lines_small_seurat_v2, 'dataset',
lambda = .1, verbose = FALSE)
## Harmony cell embeddings
harmony_embedding <- Seurat::GetCellEmbeddings(
seuratObject, 'harmony'
)
harmony_embedding[seq_len(5), seq_len(5)]
## Harmony gene loadings
harmony_loadings <- Seurat::GetGeneLoadings(
seuratObject, 'harmony'
)
harmony_loadings[seq_len(5), seq_len(5)]
p1 <- Seurat::DimPlot(seuratObject, reduction.use = 'harmony',
group.by = 'dataset', do.return = TRUE)
p2 <- Seurat::VlnPlot(seuratObject, features.plot = 'Harmony1',
group.by = 'dataset', do.return = TRUE)
cowplot::plot_grid(p1,p2)
}
}
## Seurat Version 3
if (requireNamespace("Seurat", quietly = TRUE)) {
pkg_version <- packageVersion('Seurat')
if (pkg_version >= "3.0" & pkg_version < "4.0") {
data(cell_lines_small_seurat_v3)
seuratObject <- RunHarmony(cell_lines_small_seurat_v3, 'dataset',
lambda = .1, verbose = FALSE)
## Harmony cell embeddings
harmony_embedding <- Seurat::Embeddings(seuratObject, 'harmony')
harmony_embedding[seq_len(5), seq_len(5)]
## Harmony gene loadings
harmony_loadings <- Seurat::Loadings(seuratObject, 'harmony')
harmony_loadings[seq_len(5), seq_len(5)]
p1 <- Seurat::DimPlot(seuratObject, reduction = 'harmony',
group.by = 'dataset', do.return = TRUE)
p2 <- Seurat::VlnPlot(seuratObject, features = 'harmony_1',
group.by = 'dataset', do.return = TRUE)
cowplot::plot_grid(p1, p2)
}
}
## SingleCellExperiment
if (requireNamespace("SingleCellExperiment", quietly = TRUE)) {
data(cell_lines_small_sce)
sceObject <- RunHarmony(cell_lines_small_sce, 'dataset',
lambda = .1, verbose = FALSE)
## Harmony cell embeddings
harmony_embedding <- SingleCellExperiment::reducedDim(
sceObject, 'HARMONY'
)
harmony_embedding[seq_len(5), seq_len(5)]
## Plot the Harmonized embeddings
## Colored by batch and cell type
SingleCellExperiment::reducedDim(sceObject, 'HARMONY') %>%
cbind(SingleCellExperiment::colData(sceObject)) %>%
data.frame() %>%
tidyr::gather(key, val, dataset, cell_type) %>%
dplyr::mutate(key = dplyr::case_when(
key == 'dataset' ~ 'Dataset batch',
key == 'cell_type' ~ 'Known cell types'
)) %>%
dplyr::sample_frac(1L, FALSE) %>%
ggplot2::ggplot(ggplot2::aes(x = harmony_1,
y = harmony_2,
color = val)) +
ggplot2::geom_point() +
ggplot2::facet_wrap(~key) +
ggplot2::theme_test(base_size = 12) +
ggplot2::labs(title = 'Cell embeddings after Harmony')
}
# }
Run the code above in your browser using DataLab