---
title: Processing the Fletcher Olfactory Epithelium (OE) dataset
author: Davide Risso
date: 9 February 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 Olfactory Epithelium cells from multiple mice from @fletcher2017deconstructing.
Counts for the endogenous genes, spike-ins, and gene constructs are available from the Gene Expression Omnibus using the accession number [GSE95601](https://www.ncbi.nlm.nih.gov/geo/query/acc.cgi?acc=GSE95601).
We download and cache them using the `r Biocpkg("BiocFileCache")` package.

```{r}
library(BiocFileCache)
bfc <- BiocFileCache("raw_data", ask = FALSE)

base <- "https://www.ncbi.nlm.nih.gov/geo/download/?acc=GSE95601&format=file&file="
fname <- bfcrpath(bfc, 
    paste0(base, "GSE95601%5FoeHBCdiff%5FCufflinks%5FeSet%5Fcounts%5Ftable%2Etxt%2Egz"))

protocol_data_file <- bfcrpath(bfc, 
    paste0(base, "GSE95601%5FoeHBCdiff%5FCufflinks%5FeSet%5FprotocolData%2Etxt%2Egz"))

pheno_data_file <- bfcrpath(bfc, 
    paste0(base, "GSE95601%5FoeHBCdiff%5FphenoData%2Etxt%2Egz"))

rowdata_file <- bfcrpath(bfc, 
    paste0(base, "GSE95601%5FoeHBCdiff%5FCufflinks%5FeSet%5FfeatureData%2Etxt%2Egz"))
```

Reading the counts in a matrix:

```{r}
counts <- read.table(fname)
counts <- as.matrix(counts)
dim(counts)
```

For some reason there are many `NA` values. 
Author response here:

> NA is equivalent to 0 in these matrices. I always produced matrices that had all the annotated genes in the reference transcriptome, and if no reads were detected than it was set to NA. Originally, I thought that distinguishing NAs and 0s for two slightly different cases (no reads in the current sample / no reads in any of the samples) could be useful, but this turned out not to be the case. So in the downstream analyses, I just overwrote the NA's with 0's right after reading the matrix.

So we'll just replace them as well.

```{r}
lost <- is.na(counts)
mean(lost)
counts[lost] <- 0L
```

# Adding metadata

Reading in the column data in dataframes:

```{r}
protocol_data <- read.table(protocol_data_file, header = TRUE)
pheno_data <- read.table(pheno_data_file, header = TRUE)

m1 <- match(colnames(counts), pheno_data$sample_sequencing_id)
m2 <- match(colnames(counts), protocol_data$sample_sequencing_id)
stopifnot(all(!is.na(m1)))
stopifnot(all(!is.na(m2)))

library(S4Vectors)
coldata <- cbind(pheno_data[m1,], protocol_data[m2,])
rownames(coldata) <- colnames(counts)
coldata <- DataFrame(coldata, check.names=FALSE)
colnames(coldata)
```

Repeating the dose for the row data:

```{r}
rowdata <- read.table(rowdata_file, header = TRUE)
rowdata <- DataFrame(rowdata, check.names=FALSE)
stopifnot(
    all(
        rownames(counts) == rowdata$Gene_Symbol |
        sub("\\.[0-9]$", "", rownames(counts)) == rowdata$Gene_Symbol
    )
)
rowdata
```

Attaching the cluster results from the original analysis (cluster labels available [here](https://raw.githubusercontent.com/rufletch/p63-HBC-diff/master/ref/clusterKey.md)):

```{r}
cluster_file <- bfcrpath(bfc, 
    "https://raw.githubusercontent.com/rufletch/p63-HBC-diff/master/ref/oeHBCdiff_clusterLabels.txt")
cluster_id <- read.table(cluster_file, col.names = c("sample_id", "cluster_id"))

cluster_labels <- data.frame(cluster_id = c(1:5, 7:12, 14, 15),
    cluster_label = c("HBC", 
        "INP1", 
        "GBC", 
        "mSUS", 
        "HBC2", 
        "iSUS", 
        "HBC1", 
        "iOSN", 
        "INP3", 
        "MVC1", 
        "mOSN", 
        "INP2", 
        "MVC2"),
    cluster_description = c("Resting Horizontal Basal Cells", 
        "Immediate Neuronal Precursor 1", 
        "Globose Basal Cells", 
        "Mature Sustentacular Cells", 
        "Transitional HBC 2", 
        "Immature Sustentacular Cells", 
        "Transitional HBC 1", 
        "Immature Olfactory Sensory Neurons", 
        "Immediate Neuronal Precursor 3", 
        "Microvillous Cells, type 1", 
        "Mature Olfactory Sensory Neurons", 
        "Immediate Neuronal Precursor 2", 
        "Microvillous Cells, type 2"))

m <- match(colnames(counts), cluster_id$sample_id)
coldata$retained <- !is.na(m)
coldata$cluster_id <- cluster_id$cluster_id[m]

mc <- match(coldata$cluster_id, cluster_labels$cluster_id)
stopifnot(all(!is.na(mc) | !coldata$retained))
coldata <- cbind(coldata, cluster_labels[mc,-1])
coldata
```

Creating a `SingleCellExperiment` object:

```{r}
library(SingleCellExperiment)
sce <- SingleCellExperiment(list(counts=counts),
    colData = coldata, rowData = rowdata)
```

# Saving to file 

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

```{r}
path <- file.path("scRNAseq", "fletcher-olfactory", "2.6.0")
dir.create(path, showWarnings=FALSE, recursive=TRUE)
saveRDS(assay(sce), file=file.path(path, "counts.rds"))
saveRDS(colData(sce), file=file.path(path, "coldata.rds"))
saveRDS(rowData(sce), file=file.path(path, "rowdata.rds"))
```

# Session information {-}

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

# References
