---
title: Processing the Ledergor Myeloma data
author: Milan Malfait
date: 6 July 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 MARS-seq single-cell RNA sequencing dataset of human bone marrow
plasma cells and circulating plasma cells from @ledergor2018single.
Counts for endogenous genes are available from the Gene Expression Omnibus
using the accession number [GSE117156](https://www.ncbi.nlm.nih.gov/geo/query/acc.cgi?acc=GSE117156).
We download and cache them using the `r Biocpkg("BiocFileCache")` package.

```{r}
library(BiocFileCache)
bfc <- BiocFileCache("raw_data", ask = FALSE)
mat.path <- bfcrpath(bfc,
    "https://www.ncbi.nlm.nih.gov/geo/download/?acc=GSE117156&format=file"
)
```

We read this into memory as a sparse matrix.

Each matrix corresponds to a 384-well plate from the MARS-seq sequencing.
Rows correspond to the features (identical across all matrices), columns
correspond to the wells. To obtain the full count matrix, we `cbind` the
matrices together.

```{r}
tmp <- tempfile()
untar(mat.path, exdir=tmp)
all.files <- list.files(tmp)

library(BiocParallel)
library(scuttle)
all.counts <- bplapply(file.path(tmp, all.files), readSparseCounts)
names(all.counts) <- sub(".txt.gz$", "", all.files)
do.call(rbind, lapply(all.counts, dim))

counts <- do.call(cbind, all.counts)
dim(counts)
```

Creating a `SingleCellExperiment` object:

```{r}
library(SingleCellExperiment)
sce <- SingleCellExperiment(list(counts=counts))
```

# Adding the metadata

```{r}
meta.path <- bfcrpath(bfc,
    "https://www.ncbi.nlm.nih.gov/geo/download/?acc=GSE117156&format=file&file=GSE117156%5Fmetadata%2Etxt%2Egz"
)
meta <- read.delim(meta.path)
meta <- DataFrame(meta, check.names=FALSE)
meta

meta$X <- NULL
rownames(meta) <- meta$well
```

We check that the cell names match up with the matrix.

```{r}
m <- match(colnames(counts), rownames(meta))
stopifnot(all(!is.na(m)))
meta <- meta[m,]
```

The `Experiment_ID` column encodes information on the subject, tissue and
treatment status. We'll extract this into separate columns for easier access.

```{r}
splt <- strsplit(meta$Experiment_ID, split = "_")

meta$Subject_ID <- vapply(splt, `[[`, character(1), 2)
meta$Condition <- sub("\\d+$", "", meta$Subject_ID)
meta$Condition[which(meta$Condition == "hip")] <- "Control"

## Treated IDs have 4 components, with `postRx` between subject ID and tissue
meta$Tissue <- vapply(splt, function(x) x[[length(x)]], character(1))
meta$Tissue <- sub("#\\d$", "", meta$Tissue)

meta$Treated <- grepl("postRx", meta$Experiment_ID)

meta
```

```{r}
colData(sce) <- meta
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", "ledergor-myeloma", "2.8.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"))
```

# Session information

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

# References
