---
title: Processing the Xin pancreas dataset
author: Aaron Lun
date: 28 June 2019
output:
  BiocStyle::html_document:
    titlecaps: false
    toc_float: true
bibliography: ../ref.bib
---

```{r style, echo=FALSE, results='hide', message=FALSE}
library(BiocStyle)
knitr::opts_chunk$set(error=FALSE, message=FALSE, warning=FALSE)
```

# Downloading the count data

We obtain a single-cell RNA sequencing dataset of human pancreas from @xin2016rna. 
A matrix of RPKMs is provided in the Gene Expression Omnibus
under the accession [GSE81608](https://www.ncbi.nlm.nih.gov/geo/query/acc.cgi?acc=GSE81608).
We download it using `r Biocpkg("BiocFileCache")` to cache the results:

```{r}
library(BiocFileCache)
bfc <- BiocFileCache("raw_data", ask=FALSE)    
rpkm.txt <- bfcrpath(bfc, file.path("ftp://ftp.ncbi.nlm.nih.gov/geo/series",
    "GSE81nnn/GSE81608/suppl/GSE81608_human_islets_rpkm.txt.gz"))
```

We read the RPKMs into memory as a sparse matrix.

```{r}
library(scater)
mat <- readSparseCounts(rpkm.txt)
dim(mat)
```

# Preparing the column metadata

We download the metadata, which was supplied by the authors to Vladimir Kiselev, Tallulah Andrews and Martin Hemberg.

```{r}
col.path <- bfcrpath(bfc, file.path("https://s3.amazonaws.com/",
    "scrnaseq-public-datasets/manual-data/xin",
    "human_islet_cell_identity.txt"))
coldata <- read.delim(col.path, stringsAsFactors=FALSE, check.names=FALSE)
colnames(coldata)
```

We check that this is consistent:

```{r}
refnames <- sub(" ", "_", coldata[,1])
stopifnot(identical(colnames(mat), refnames))
```

We clean out uninteresting columns that have either all unique values or only one value.
We hold onto the sample ID for verification purposes later.

```{r}
keep <- vapply(coldata, function(x) {
    !length(unique(x)) %in% c(1L, length(x))
}, TRUE)
coldata <- coldata[,c(1, which(keep))]
```

Finally, we coerce it into a `DataFrame` for storage.

```{r}
library(S4Vectors)
coldata <- as(coldata, "DataFrame")
coldata
```

# Preparing the row metadata

We do the same for the row metadata, which is pretty straightforward.

```{r}
row.path <- bfcrpath(bfc, file.path("https://s3.amazonaws.com/",
    "scrnaseq-public-datasets/manual-data/xin",
    "human_gene_annotation.csv"))
rowdata <- read.csv(row.path, stringsAsFactors=FALSE, check.names=FALSE)
rowdata <- as(rowdata, "DataFrame")
stopifnot(identical(rownames(mat), as.character(rowdata[,1])))
rowdata
```

# Saving to file

We now save all of the components to file for upload to `r Biocpkg("ExperimentHub")`.
These will be used to construct a `SingleCellExperiment` on the client side when the dataset is requested.

```{r}
path <- file.path("scRNAseq", "xin-pancreas", "2.0.0")
dir.create(path, showWarnings=FALSE, recursive=TRUE)
saveRDS(mat, file=file.path(path, "rpkm.rds"))
saveRDS(rowdata, file=file.path(path, "rowdata.rds"))
saveRDS(coldata, file=file.path(path, "coldata.rds"))
```

# Session information

```{r}
sessionInfo()
```

# References
