---
title: "Integrating Seurat objects using LIGER"
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
---

NOTE: Please update your `liger` version to 0.5.0 or above before following this tutorial.

This vigettte demonstrates how to run LIGER on Seurat objects. Parameters and commands are based on the [LIGER tutorial](http://htmlpreview.github.io/?https://github.com/MacoskoLab/liger/blob/master/vignettes/Integrating_multi_scRNA_data.html). If you use LIGER, please cite:
  
> *Single-Cell Multi-omic Integration Compares and Contrasts Features of Brain Cell Identity*
>
> Joshua Welch, Velina Kozareva, Ashley Ferreira, Charles Vanderburg, Carly Martin, Evan Z.Macosko
>
> Cell, 2019.
>
> doi: [10.1016/j.cell.2019.05.006](https://doi.org/10.1016/j.cell.2019.05.006)
>
> GitHub: https://github.com/MacoskoLab/liger

```{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) 
* [LIGER](https://github.com/MacoskoLab/liger) 
* [SeuratWrappers](https://github.com/satijalab/seurat-wrappers)
* [SeuratData](https://github.com/satijalab/seurat-data) 

```{r check, include=FALSE}
tryCatch(
  expr = SeuratWrappers:::CheckPackage("rliger", "cran"),
  error = {
    install.packages("rliger")
  }
)
```

```{r packages}
library(rliger)
library(Seurat)
library(SeuratData)
library(SeuratWrappers)
```

In order to replicate LIGER's multi-dataset functionality, we will use the `split.by` parameter to preprocess the Seurat object on subsets of the data belonging to each dataset separately. Also, as LIGER does not center data when scaling, we will skip that step as well.

`RunQuantileNorm` produces joint clusters, but users can also optionally perform Louvain community detection (`FindNeighbors` and `FindClusters`) on the integrated latent space from iNMF.

## {.tabset .tabset-pills}

### Systematic comparative analysis of human PBMC

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

```{r pbmcsca, results='hide', fig.height = 6, fig.width = 18}
InstallData("pbmcsca")
data("pbmcsca")
# Please update your `liger` version to 0.5.0 or above before following this tutorial
pbmcsca <- NormalizeData(pbmcsca)
pbmcsca <- FindVariableFeatures(pbmcsca)
pbmcsca <- ScaleData(pbmcsca, split.by = 'Method', do.center = FALSE)
pbmcsca <- RunOptimizeALS(pbmcsca, k = 20, lambda = 5, split.by = 'Method')
pbmcsca <- RunQuantileNorm(pbmcsca, split.by = 'Method')
# You can optionally perform Louvain clustering (`FindNeighbors` and `FindClusters`) after `RunQuantileNorm` according to your needs
pbmcsca <- FindNeighbors(pbmcsca, reduction = 'iNMF', dims = 1:20)
pbmcsca <- FindClusters(pbmcsca, resolution = 0.3)
# Dimensional reduction and plotting
pbmcsca <- RunUMAP(pbmcsca, dims = 1:ncol(pbmcsca[['iNMF']]), reduction = 'iNMF')
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, results='hide', fig.height = 6, fig.width = 18}
InstallData("ifnb")
data("ifnb")
# Please update your `liger` version to 0.5.0 or above before following this tutorial.
ifnb <- NormalizeData(ifnb)
ifnb <- FindVariableFeatures(ifnb)
ifnb <- ScaleData(ifnb, split.by = 'stim', do.center = FALSE)
ifnb <- RunOptimizeALS(ifnb, k = 20, lambda = 5, split.by = 'stim')
ifnb <- RunQuantileNorm(ifnb, split.by = 'stim')
# You can optionally perform Louvain clustering (`FindNeighbors` and `FindClusters`) after `RunQuantileNorm` according to your needs
ifnb <- FindNeighbors(ifnb, reduction = 'iNMF', dims = 1:20)
ifnb <- FindClusters(ifnb, resolution = 0.55)
# Dimensional reduction and plotting
ifnb <- RunUMAP(ifnb, dims = 1:ncol(ifnb[['iNMF']]), reduction = 'iNMF')
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', fig.height = 6, fig.width = 18}
InstallData("panc8")
data("panc8")
# Please update your `liger` version to 0.5.0 or above before following this tutorial.
panc8 <- NormalizeData(panc8)
panc8 <- FindVariableFeatures(panc8)
panc8 <- ScaleData(panc8, split.by = 'replicate', do.center = FALSE)
panc8 <- RunOptimizeALS(panc8, k = 20, lambda = 5, split.by = 'replicate')
panc8 <- RunQuantileNorm(panc8, split.by = 'replicate')
# You can optionally perform Louvain clustering (`FindNeighbors` and `FindClusters`) after `RunQuantileNorm` according to your needs
panc8 <- FindNeighbors(panc8, reduction = 'iNMF', dims = 1:20)
panc8 <- FindClusters(panc8, resolution = 0.4)
# Dimensional reduction and plotting
panc8 <- RunUMAP(panc8, dims = 1:ncol(panc8[['iNMF']]), reduction = 'iNMF')
DimPlot(panc8, group.by = c('replicate', 'ident', 'celltype'), ncol = 3)
```
