---
title: Processing the Campbell brain dataset
author: Aaron Lun
date: "June 24, 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 the mouse brain from @campbell2017molecular.
Counts for endogenous genes are available from the Gene Expression Omnibus
using the accession number [GSE93374](https://www.ncbi.nlm.nih.gov/geo/query/acc.cgi?acc=GSE93374).
We download and cache them using the `r Biocpkg("BiocFileCache")` package.

```{r}
library(BiocFileCache)
bfc <- BiocFileCache("raw_data", ask = FALSE)
base.url <- file.path("ftp://ftp.ncbi.nlm.nih.gov/geo/series",
    "GSE93nnn/GSE93374/suppl")
count.file <- bfcrpath(bfc, file.path(base.url,
    "GSE93374_Merged_all_020816_DGE.txt.gz"))
```

Reading them in as a sparse matrix.

```{r}
library(scater)
counts <- readSparseCounts(count.file)
dim(counts)
```

# Downloading the metadata

We also download the cluster identities.

```{r}
cluster.file <- bfcrpath(bfc, file.path(base.url,
    "GSE93374_cell_metadata.txt.gz"))
colClasses <- vector("list", 15)
colClasses[1:11] <- "character"
coldata <- read.delim(cluster.file, stringsAsFactors=FALSE, 
    colClasses=colClasses)
coldata <- as(coldata, "DataFrame")
colnames(coldata) <- sub("X[0-9]+\\.", "", colnames(coldata))
coldata
```

We check that the columns are in the same order.

```{r}
m <- match(colnames(counts), coldata$ID)
coldata <- coldata[m,]
stopifnot(identical(colnames(counts), coldata$ID))
```

# 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", "campbell-brain", "2.0.0")
dir.create(path, showWarnings=FALSE, recursive=TRUE)
saveRDS(counts, file=file.path(path, "counts.rds"))
saveRDS(coldata, file=file.path(path, "coldata.rds"))
```

# Session information

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

# References
