---
title: "How to import Cytobank into a GatingSet"
author: "Mike Jiang"
output: 
  html_document: 
    number_sections: yes
    toc: yes
    toc_float: true
vignette: >    
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteIndexEntry{How to import Cytobank into a GatingSet}
---


```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, results = "markup", message = FALSE, warning = FALSE)
```

This vignette demonstrates how the gatingML files exported from Cytobank can be imported into R as a GatingSet object.

```{r}
library(flowWorkspace)
library(CytoML)
acs <- system.file("extdata/cytobank_experiment.acs", package = "CytoML")
```

Create `cytobank_experiment` object from the ACS bundle exported from Cytobank
```{r}
ce <- open_cytobank_experiment(acs)
ce
```

**cytobank_experiment** is a wrapper around the `ACS` file, which can be inspected by various accessors.
```{r}
sampleNames(ce)
colnames(ce)
markernames(ce)
pData(ce)
```
Then import `cytobank_experiment` into **GatingSet**
```{r}
gs <- cytobank_to_gatingset(ce)
```

Alternatively, the import can be done by `gatingML` and `fcs` files that are downloaded separately form Cytobank without `ACS`.
```{r, eval=FALSE}
xmlfile <- ce$gatingML
fcsFiles <- list.files(ce$fcsdir, full.names = TRUE)
gs <- cytobank_to_gatingset(xmlfile, fcsFiles)
```
However, it doesn't have the information from `yaml` file (part of `ACS`). E.g. sample tags (i.e. `pData`) and customized markernames. So it is recommended to import `ACS`.

Inspect the results
```{r}
library(ggcyto)
## Plot the gates
autoplot(gs[[1]])
# Extract the population statistics
gs_pop_get_count_fast(gs, statType = "count")
```


