---
title: Processing the Nowakowski cortex dataset
author: Aaron Lun
date: 11 January 2021
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 data

We obtain a single-cell RNA sequencing dataset of the human cotex from @nowakowski2017spatiotemporal.
TPMs for endogenous genes are available from the the UCSC cell browser at http://cells.ucsc.edu/?ds=cortex-dev.
We download and cache them using the `r Biocpkg("BiocFileCache")` package.

```{r}
library(BiocFileCache)
bfc <- BiocFileCache("raw_data", ask = FALSE)
exprs.path <- bfcrpath(bfc, "http://cells.ucsc.edu/cortex-dev/exprMatrix.tsv.gz")

library(scuttle)
mat <- readSparseCounts(exprs.path, row.names=1)
dim(mat)
```

We also read in various pieces of metadata; firstly, the per-cell metadata.

```{r}
meta.path <- bfcrpath(bfc, "http://cells.ucsc.edu/cortex-dev/meta.tsv")
meta <- read.delim(meta.path, row.names=1)
stopifnot(identical(colnames(mat), rownames(meta))) 
meta <- DataFrame(meta)
meta
```

And then all of the various reduced dimensions:

```{r}
red.dim.list <- c(
    "tMinusSNE_on_WGCNA",
    "TriMap",
    "TMinusSNE_scanpy",
    "UMAP_scanpy",
    "PAGAPlusForceAtlas2_scanpy"
)

reddims <- list()
for (i in red.dim.list) { 
    X <- bfcrpath(bfc, sprintf("http://cells.ucsc.edu/cortex-dev/%s.coords.tsv.gz", i))
    Y <- as.matrix(read.table(X, row.names=1, sep="\t"))
    stopifnot(all(rownames(Y) %in% colnames(mat)))

    expanded <- Y[match(colnames(mat), rownames(Y)),,drop=FALSE]
    rownames(expanded) <- colnames(mat)

    stopifnot(identical(colnames(expanded), c("V2", "V3")))
    colnames(expanded) <- NULL
    reddims[[i]] <- expanded
}
```

Making sure we can assemble the final SCE:

```{r}
sce <- SingleCellExperiment(list(tpm=mat), colData=meta, reducedDims=reddims)
sce 
```

# Saving to file 

We now save all of the relevant components to file for upload to `r Biocpkg("ExperimentHub")`.

```{r}
path <- file.path("scRNAseq", "nowakowski-cortex", "2.6.0")
dir.create(path, showWarnings=FALSE, recursive=TRUE)
saveRDS(mat, file=file.path(path, "tpm.rds"))
saveRDS(meta, file=file.path(path, "coldata.rds"))
saveRDS(reddims, file=file.path(path, "reddims.rds"))
```

# Session information

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

# References
