---
title: "Abundance of plasmablasts measured by multiparameter flow cytometry in SDY180"
author: "Renan Sauteraud"
date: "`r Sys.Date()`"
output: rmarkdown::html_document
vignette: >
  %\VignetteIndexEntry{Reproducing an online report using ImmuneSpaceR: Plasmablast abundance in SDY180}
  %\VignetteEngine{knitr::rmarkdown}
---


ImmuneSpaceR code produces consistent results, regardless of whether it is being
executed from a module or a UI based report on the server or on a local machine.
This vignette reproduces a report available on <a href="https://www.immunespace.org/reports/Studies/SDY180/runReport.view?reportId=module
%3ASDY180%2Freports%2Fschemas%2Fstudy%2Fdemographics%2Fplasmablast_abundance.Rmd">www.immunespace.org</a> using the same code.

### Summary
This report investigate the abundance of plasmablast (and other B cell subsets) over time after vaccination with Pneumovax, Fluzone, or no vaccination (saline control group). 
It can be seen on the three figures below that the plasmablast subset peaks at day 7 in both vaccine groups, with a stronger and more durable response with Pneumovax. 
As expected, there is no clear peak in the saline group. These results are similar to those reported in Figure 6 B of <a href="http://www.ncbi.nlm.nih.gov/pmc/articles/PMC3681204/" target="_BLANK"> Obermoser et al. (2013)</a> published as part of the original study.


```{r knitr-opts, echo = FALSE, message = FALSE, cache = FALSE}
library(knitr)
opts_chunk$set(cache=FALSE, echo=TRUE, message=FALSE, warning=FALSE,
               fig.width=10, fig.height=14, dpi=100, fig.align="center")
```
```{r netrc_req, echo = FALSE}
# This chunk is only useful for BioConductor checks and shouldn't affect any other setup
ISR_login <- Sys.getenv("ISR_login")
ISR_pwd <- Sys.getenv("ISR_pwd")
if(ISR_login != ""  &  ISR_pwd != ""){
  netrc_file <- tempfile("ImmuneSpaceR_tmp_netrc")
  netrc_string <- paste("machine www.immunespace.org login", ISR_login, "password", ISR_pwd)
  write(x = netrc_string, file = netrc_file)
  labkey.netrc.file <- netrc_file
}
```

#### Load ImmuneSpaceR and other libraries
```{r libraries, cache=FALSE}
library(ImmuneSpaceR)
library(ggplot2)
library(data.table)
```


#### Connect to the study and get the flow cytometry results
```{r connection}
study <- CreateConnection(c("SDY180"))
dt_fcs <- study$getDataset("fcs_analyzed_result", reload=TRUE)
```

#### Subset the population of interest
```{r data-subset}
dt_fcs19 <- dt_fcs[population_name_reported%like%"Plasma"]
dt_fcs19 <- dt_fcs19[,cohort:=gsub("Study g", "G", cohort),]
```

#### Compute the median
```{r data-summary}
dt_fcs19_median <- dt_fcs19[, .(median_cell_reported = median(as.double(population_cell_number) + 1,
                   na.rm=TRUE)), by = .(cohort,study_time_collected,population_name_reported)]
```

#### Flow cytometry vs. elispot
```{r, dev='png'}
ggplot(dt_fcs19, aes(x = as.factor(study_time_collected), y = as.double(population_cell_number) + 1)) +
  geom_boxplot() + geom_jitter() + scale_y_log10() + facet_grid(cohort~population_name_reported, 
  scale = "free") + xlab("Time") + ylab(expression(paste("Number of cells/", mu, "l"))) + 
  geom_line(data = dt_fcs19_median, aes(x = as.factor(study_time_collected), y = as.double(median_cell_reported),
  group = 1), color = "black", size = 1.2) + labs(title = "Plasma cell abundance after vaccination") + theme_IS()
```

