---
title: "Integration of datasets using Harmony"
date: 'Compiled: `r format(Sys.Date(), "%B %d, %Y")`'
output:
  github_document:
    html_preview: true
    toc: true
    toc_depth: 3
    fig_width: 16
  html_document:
    df_print: kable
    theme: united
    fig_height: 5
    fig_width: 16
    out_height: 4
---

This vigettte demonstrates the use of the Harmony package in Seurat. Commands and parameters are based off of the [Harmony use page](https://github.com/immunogenomics/harmony). If you use Harmony in your work, please cite:

> *Fast, sensitive, and flexible integration of single cell data with Harmony*
>
> Ilya Korsunsky, Jean Fan, Kamil Slowikowski, Fan Zhang, Kevin Wei, Yuriy Baglaenko, Michael Brenner, Po-Ru Loh, Soumya Raychaudhuri
>
> bioRxiv, 2019
>
> doi: [10.1101/461954v2](https://www.biorxiv.org/content/10.1101/461954v2)
>
> GitHub: https://github.com/immunogenomics/harmony

```{r setup, include=FALSE}
knitr::opts_chunk$set(
  tidy = TRUE,
  tidy.opts = list(width.cutoff = 95),
  message = FALSE,
  warning = FALSE
)
```

Prerequisites to install:

* [Seurat](https://satijalab.org/seurat/install)
* [Harmony](https://github.com/immunogenomics/harmony)
* [SeuratData](https://github.com/satijalab/seurat-data)

Note that SeuratWrappers is not necessary, as the wrapper functions were generously provided by the Harmony authors, and are included when installing Harmony.

```{r packages}
library(harmony)
library(Seurat)
library(SeuratData)
```

## {.tabset .tabset-pills}

### Systematic comparative analysis of human PBMC

To learn more about this dataset, type `?pbmcsca`

```{r pbmcsca, results='hide', cache=TRUE}
InstallData("pbmcsca")
data("pbmcsca")
pbmcsca <- NormalizeData(pbmcsca) %>% FindVariableFeatures() %>% ScaleData() %>% RunPCA(verbose = FALSE)
pbmcsca <- RunHarmony(pbmcsca, group.by.vars = 'Method')
pbmcsca <- RunUMAP(pbmcsca, reduction = 'harmony', dims = 1:30)
pbmcsca <- FindNeighbors(pbmcsca, reduction = 'harmony', dims = 1:30) %>% FindClusters()
DimPlot(pbmcsca, group.by = c('Method', 'ident',"CellType"), ncol = 3)
```

### Interferon-stimulated and control PBMC

To learn more about this dataset, type `?ifnb`

```{r ifnb_stim, results='hide', cache=TRUE}
InstallData("ifnb")
data("ifnb")
ifnb <- NormalizeData(ifnb) %>% FindVariableFeatures() %>% ScaleData() %>% RunPCA(verbose = FALSE)
ifnb <- RunHarmony(ifnb, group.by.vars = 'stim')
ifnb <- RunUMAP(ifnb, reduction = 'harmony', dims = 1:30)
ifnb <- FindNeighbors(ifnb, reduction = 'harmony', dims = 1:30) %>% FindClusters()
DimPlot(ifnb, group.by = c("stim", "ident", "seurat_annotations"), ncol = 3)
```

### Eight human pancreatic islet datasets

To learn more about this dataset, type `?panc8`

```{r pancreas, results='hide', cache=TRUE}
InstallData("panc8")
data("panc8")
panc8 <- NormalizeData(panc8) %>% FindVariableFeatures() %>% ScaleData() %>% RunPCA(verbose = FALSE)
panc8 <- RunHarmony(panc8, group.by.vars = 'replicate')
panc8 <- RunUMAP(panc8, reduction = 'harmony', dims = 1:30)
panc8 <- FindNeighbors(panc8, reduction = 'harmony', dims = 1:30) %>% FindClusters()
DimPlot(panc8, group.by = c("replicate", "ident","celltype"), ncol = 3)
```
