Quick start to Harmony

Introduction

Harmony is an algorithm for performing integration of single cell genomics datasets. Please check out our latest manuscript on Nature Methods.

Installation

Install Harmony from CRAN with standard commands.

install.packages('harmony')

Once Harmony is installed, load it up!

library(harmony)

Integrating cell line datasets from 10X

The example below follows Figure 2 in the manuscript.

We downloaded 3 cell line datasets from the 10X website. The first two (jurkat and 293t) come from pure cell lines while the half dataset is a 50:50 mixture of Jurkat and HEK293T cells. We inferred cell type with the canonical marker XIST, since the two cell lines come from 1 male and 1 female donor.

  • support.10xgenomics.com/single-cell-gene-expression/datasets/1.1.0/jurkat
  • support.10xgenomics.com/single-cell-gene-expression/datasets/1.1.0/293t
  • support.10xgenomics.com/single-cell-gene-expression/datasets/1.1.0/jurkat:293t_50:50

We library normalized the cells, log transformed the counts, and scaled the genes. Then we performed PCA and kept the top 20 PCs. The PCA embeddings and meta data are available as part of this package.

data(cell_lines)
V <- cell_lines$scaled_pcs
meta_data <- cell_lines$meta_data

Initially, the cells cluster by both dataset (left) and cell type (right).

library(ggplot2)

do_scatter <- function(xy, meta_data, label_name, base_size = 12) {    
    palette_use <- c(`jurkat` = '#810F7C', `t293` = '#D09E2D',`half` = '#006D2C')
    xy <- xy[, 1:2]
    colnames(xy) <- c('X1', 'X2')
    plt_df <- xy %>% data.frame() %>% cbind(meta_data)
    plt <- ggplot(plt_df, aes(X1, X2, col = !!rlang::sym(label_name), fill = !!rlang::sym(label_name))) + 
        theme_test(base_size = base_size) +
        guides(color = guide_legend(override.aes = list(stroke = 1, alpha = 1,
                                                        shape = 16, size = 4))) +
        scale_color_manual(values = palette_use) +
        scale_fill_manual(values = palette_use) +
        theme(plot.title = element_text(hjust = .5)) +
        labs(x = "PC 1", y = "PC 2") +
        theme(legend.position = "none") +
        geom_point(shape = '.')
    
    ## Add labels
    data_labels <- plt_df %>%
        dplyr::group_by(!!rlang::sym(label_name)) %>%
        dplyr::summarise(X1 = mean(X1), X2 = mean(X2)) %>%
        dplyr::ungroup()
    plt + geom_label(data = data_labels, aes(label = !!rlang::sym(label_name)), 
                            color = "white", size = 4)
}
p1 <- do_scatter(V, meta_data, 'dataset') + 
    labs(title = 'Colored by dataset')
p2 <- do_scatter(V, meta_data, 'cell_type') + 
    labs(title = 'Colored by cell type')

cowplot::plot_grid(p1, p2)

Let’s run Harmony to remove the influence of dataset-of-origin from the cell embeddings.

harmony_embeddings <- harmony::RunHarmony(
    V, meta_data, 'dataset', verbose=FALSE
)

After Harmony, the datasets are now mixed (left) and the cell types are still separate (right).

p1 <- do_scatter(harmony_embeddings, meta_data, 'dataset') + 
    labs(title = 'Colored by dataset')
p2 <- do_scatter(harmony_embeddings, meta_data, 'cell_type') + 
    labs(title = 'Colored by cell type')
cowplot::plot_grid(p1, p2, nrow = 1)

Next Steps

Interfacing to software packages

You can also run Harmony as part of an established pipeline in several packages, such as Seurat. For these vignettes, please visit our github page.

Detailed breakdown of the Harmony algorithm

For more details on how each part of Harmony works, consult our more detailed vignette “Detailed Walkthrough of Harmony Algorithm”.

Session Info

sessionInfo()
## R version 4.4.2 (2024-10-31)
## Platform: x86_64-pc-linux-gnu
## Running under: Ubuntu 24.04.1 LTS
## 
## Matrix products: default
## BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3 
## LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.26.so;  LAPACK version 3.12.0
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=C              
##  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
##  [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       
## 
## time zone: Etc/UTC
## tzcode source: system (glibc)
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
##  [1] patchwork_1.3.0    ggrepel_0.9.6      ggthemes_5.1.0     lubridate_1.9.3   
##  [5] forcats_1.0.0      stringr_1.5.1      purrr_1.0.2        readr_2.1.5       
##  [9] tidyr_1.3.1        tibble_3.2.1       ggplot2_3.5.1      tidyverse_2.0.0   
## [13] data.table_1.16.2  cowplot_1.1.3      dplyr_1.1.4        Seurat_5.1.0      
## [17] SeuratObject_5.0.2 sp_2.1-4           harmony_1.2.3      Rcpp_1.0.13-1     
## [21] rmarkdown_2.29    
## 
## loaded via a namespace (and not attached):
##   [1] RColorBrewer_1.1-3     sys_3.4.3              jsonlite_1.8.9        
##   [4] magrittr_2.0.3         spatstat.utils_3.1-1   farver_2.1.2          
##   [7] vctrs_0.6.5            ROCR_1.0-11            spatstat.explore_3.3-3
##  [10] htmltools_0.5.8.1      sass_0.4.9             sctransform_0.4.1     
##  [13] parallelly_1.39.0      KernSmooth_2.23-24     bslib_0.8.0           
##  [16] htmlwidgets_1.6.4      ica_1.0-3              plyr_1.8.9            
##  [19] plotly_4.10.4          zoo_1.8-12             cachem_1.1.0          
##  [22] buildtools_1.0.0       igraph_2.1.1           mime_0.12             
##  [25] lifecycle_1.0.4        pkgconfig_2.0.3        Matrix_1.7-1          
##  [28] R6_2.5.1               fastmap_1.2.0          fitdistrplus_1.2-1    
##  [31] future_1.34.0          shiny_1.9.1            digest_0.6.37         
##  [34] colorspace_2.1-1       tensor_1.5             RSpectra_0.16-2       
##  [37] irlba_2.3.5.1          labeling_0.4.3         progressr_0.15.1      
##  [40] timechange_0.3.0       fansi_1.0.6            spatstat.sparse_3.1-0 
##  [43] httr_1.4.7             polyclip_1.10-7        abind_1.4-8           
##  [46] compiler_4.4.2         withr_3.0.2            fastDummies_1.7.4     
##  [49] MASS_7.3-61            tools_4.4.2            lmtest_0.9-40         
##  [52] httpuv_1.6.15          future.apply_1.11.3    goftest_1.2-3         
##  [55] glue_1.8.0             nlme_3.1-166           promises_1.3.2        
##  [58] grid_4.4.2             Rtsne_0.17             cluster_2.1.6         
##  [61] reshape2_1.4.4         generics_0.1.3         gtable_0.3.6          
##  [64] spatstat.data_3.1-4    tzdb_0.4.0             hms_1.1.3             
##  [67] utf8_1.2.4             spatstat.geom_3.3-4    RcppAnnoy_0.0.22      
##  [70] RANN_2.6.2             pillar_1.9.0           spam_2.11-0           
##  [73] RcppHNSW_0.6.0         later_1.4.1            splines_4.4.2         
##  [76] lattice_0.22-6         survival_3.7-0         deldir_2.0-4          
##  [79] tidyselect_1.2.1       maketools_1.3.1        miniUI_0.1.1.1        
##  [82] pbapply_1.7-2          knitr_1.49             gridExtra_2.3         
##  [85] scattermore_1.2        RhpcBLASctl_0.23-42    xfun_0.49             
##  [88] matrixStats_1.4.1      stringi_1.8.4          lazyeval_0.2.2        
##  [91] yaml_2.3.10            evaluate_1.0.1         codetools_0.2-20      
##  [94] cli_3.6.3              uwot_0.2.2             xtable_1.8-4          
##  [97] reticulate_1.40.0      munsell_0.5.1          jquerylib_0.1.4       
## [100] globals_0.16.3         spatstat.random_3.3-2  png_0.1-8             
## [103] spatstat.univar_3.1-1  parallel_4.4.2         dotCall64_1.2         
## [106] listenv_0.9.1          viridisLite_0.4.2      scales_1.3.0          
## [109] ggridges_0.5.6         leiden_0.4.3.1         rlang_1.1.4