---
title: "Load a FlowJo workspace"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Load a FlowJo workspace}
  %\VignetteEncoding{UTF-8}
  %\VignetteEngine{knitr::rmarkdown}
editor_options: 
  markdown: 
    wrap: 72
---

```{r setup, warning=FALSE, message=FALSE}
library(cyCONDOR)
library(flowWorkspace)
library(CytoML)
library(Biobase)
library(ggplot2)
library(dplyr)
```

Within the `cyCONDOR` ecosystem it is possible to load a FlowJo work space (.wsp file). This gives the advantage of having the gating hierarchy in the `fcd` object. We would still reccomend using files preprocessed as described in vignette("Prepare_data_for_cyCONDOR_analysis") to be fully compatible with all downstream analysis (e.g. cytonorm). Exported compensared files can be loaded in FlowJo and used for gating analysis.

## Read the .wsp file

First we use the [CytoML](https://doi.org/10.18129/B9.bioc.CytoML) package to load the `.wsp` file.

```{r}
ws <- CytoML::open_flowjo_xml("../.test_files/flowjo_workspace.wsp")

ws
```

## Transformation into **GatingSet** object

We then transform the object to a `GatingSet` object.

```{r}
gs <- CytoML::flowjo_to_gatingset(ws, name = "samples", path = "../.test_files/fcs_flowjo_ws/")

gs
```

### Visualization of the FlowJo gating strategy

We visualize the gating hierarchy and look at the cell count per gated population.

```{r}
flowWorkspace::plot(gs, bool = TRUE)
```

```{r}
flowWorkspace::gs_pop_get_stats(gs)
```


## Prepare the data for **cyCONDOR**

From the `GatingSet` object we prepare the `fcd` object using `prep_fjw()`. Here, we provide the `GatingSet` object as input, reverse the FlowJo tranformation, perform an autologicle transformation and remove the parameter "Time". Optionally, we can read in an annotation table to add further sample information (not shown).

```{r}
condor <- prep_fjw(data_gs = gs, 
                   inverse.transform = TRUE,
                   transformation = "auto_logi",
                   remove_param = c("Time")
                   )

class(condor)
```

The performed FlowJo gating with the assigned cell populations is saved in `condor$anno$cell_anno`.
```{r}
condor$anno$cell_anno[110:115,]
```


## Filter for a population

We can filter our data set for a population of interest e.g. the CD45+ cells.

```{r}
#select cell IDs of all CD45+ cells
keep <- condor$anno$cell_anno[condor$anno$cell_anno$`CD45+` == 1,] %>% rownames()

#check the number of CD45+ cells
length(keep)

#filter data for CD45+ cells
condor_CD45 <- filter_fcd(condor, cell_ids = keep)
```


### Simple visualization of the dataset with FlowJo gating

We can visualize the gated cell populations for example in a simple dot plot. In the plot below, we plot the fluorescence intensity of CD3 against CD19 and color the cells by the 'CD3+' population from the FlowJo workspace.

```{r}
# subset condor to 10000 cells for visualization
condor_sub <- subset_fcd(condor_CD45, size= 10000)

#prepare data frame for plotting
tmp <- cbind(condor_sub$expr$orig, condor_sub$anno$cell_anno)

ggplot(tmp, aes(x= `CD19`, y = `CD3`, color = as.factor(`CD3+`))) +
  geom_point() + 
  theme_bw() + 
  theme(aspect.ratio = 1) + 
  scale_color_viridis_d()
```

We can now continue with the usual `cyCONDOR` workflow (see e.g. `vignette("Dimensionality_Reduction")`, `vignette("Clustering_and_cell_annotation")` and `vignette("Data_Visualization")`.


# Session Info
```{r}
info <- sessionInfo()

info
```

