---
title: "Import and QC of Zheng data set"
output: html_document
editor_options: 
  chunk_output_type: console
---

```{r load-packages}
suppressPackageStartupMessages({
  library(MultiAssayExperiment)
  library(SingleCellExperiment)
  library(scater)
  library(scran)
  library(dplyr)
  library(ggplot2)
})
```

## Load raw data

```{r load-dataset}
b.cells <- read10XResults("../../data/data_raw/zheng/b_cells/filtered_matrices_mex/hg19") 
naive.cytotoxic <- read10XResults("../../data/data_raw/zheng/naive_cytotoxic/filtered_matrices_mex/hg19")
cd14.monocytes <- read10XResults("../../data/data_raw/zheng/cd14_monocytes/filtered_matrices_mex/hg19")
regulatory.t <- read10XResults("../../data/data_raw/zheng/regulatory_t/filtered_matrices_mex/hg19")

colnames(b.cells) <-  paste0("b.cells", seq_len(ncol(b.cells)))
colnames(naive.cytotoxic) <- paste0("naive.cytotoxic", seq_len(ncol(naive.cytotoxic)))
colnames(cd14.monocytes) <- paste0("cd14.monocytes", seq_len(ncol(cd14.monocytes)))
colnames(regulatory.t) <- paste0("regulatory.t", seq_len(ncol(regulatory.t)))

colData(b.cells)$phenoid <- "b.cells"
colData(naive.cytotoxic)$phenoid <- "naive.cytotoxic"
colData(cd14.monocytes)$phenoid <- "cd14.monocytes"
colData(regulatory.t)$phenoid <- "regulatory.t"

## Subsample
set.seed(1234)
b.cells <- b.cells[, sample(colnames(b.cells), 1000, replace = FALSE)]
naive.cytotoxic <- naive.cytotoxic[, sample(colnames(naive.cytotoxic), 1000, replace = FALSE)]
cd14.monocytes <- cd14.monocytes[, sample(colnames(cd14.monocytes), 1000, replace = FALSE)]
regulatory.t <- regulatory.t[, sample(colnames(regulatory.t), 1000, replace = FALSE)]
```

## Create a SingleCellExperiment object

```{r create-sce}
sce <- cbind(b.cells, naive.cytotoxic, cd14.monocytes, regulatory.t)
counts(sce) <-  as.matrix(counts(sce))
sce <- normalise(sce, exprs_values = "counts", return_log = TRUE, 
                 return_norm_as_exprs = TRUE) ## generates logcounts(sce)
```

Exclude features that are not expressed

```{r reduce-expression-matrix}
keep_features <- rowSums(counts(sce) > 0) > 0
table(keep_features)
sce <- sce[keep_features, ]
dim(sce)
```

## Identify the remaining ERCC spike-ins.

```{r ercc-mt}
is.spike <- grepl("^ERCC", rowData(sce)$symbol)
table(is.spike)
summary(colSums(counts(sce[is.spike, ])))
```

## Calculate QC metrics

```{r QC}
sce <- calculateQCMetrics(sce)
```

## Quality control using PCA on column data

We create a PCA plot based the quality metrics for each cell, e.g., the total
number of reads, the total number of features and the proportion of spike-in
reads.

```{r qc-pca}
sce <- scater::runPCA(sce, pca_data_input = "coldata")
scater::plotPCA(sce, colour_by = "phenoid")
```

## Filter cells

We remove cells with log-library sizes (or total features) that are more than 3
median absolute deviations (MADs) below the median log-library size (or total
features).

```{r histogram}
colData(sce)$libsize.drop <- isOutlier(sce$total_counts, nmads = 3, type = "lower", log = TRUE)
ggplot(as.data.frame(colData(sce)), aes(x = total_counts)) + 
  geom_histogram(bins = 20, fill = "grey80") + xlab("Total count") + 
  ylab("Number of cells") + 
  geom_vline(xintercept = min(sce$total_counts[!sce$libsize.drop]), 
             color = "red", linetype = "dashed") + 
  theme_bw()

colData(sce)$feature.drop <- isOutlier(sce$total_features, nmads = 3, type = "lower", log = TRUE)
ggplot(as.data.frame(colData(sce)), aes(x = total_features)) + 
  geom_histogram(bins = 20, fill = "grey80") + xlab("Number of detected features") + 
  ylab("Number of cells") + 
  geom_vline(xintercept = min(sce$total_features[!sce$feature.drop]), 
             color = "red", linetype = "dashed") + 
  theme_bw()
```

```{r filter-cells}
table(libsize = sce$libsize.drop, feature = sce$feature.drop)
sce <- sce[, !(sce$libsize.drop | sce$feature.drop)]
dim(sce)
```

## Quality control using highest expressed genes

```{r qc-filt}
plotQC(sce, type = "highest-expression", n = 50)
```

## Data normalization

```{r normalization}
sce <- normalise(sce, exprs_values = "counts", return_log = TRUE, 
                 return_norm_as_exprs = TRUE)
sce <- normalise(sce, exprs_values = "counts", return_log = FALSE, 
                 return_norm_as_exprs = FALSE)
```

## Plot the proportion of explained variances

```{r explained-variance, warning = FALSE} 
expl_vars <- c("phenoid", "log10_total_counts", "log10_total_features", "pct_dropout",
               "pct_counts_top_200_features", "log10_counts_feature_controls",
               "pct_counts_feature_controls")
plotQC(sce, type = "explanatory-variables", variables = expl_vars)
```

## Plot t-SNE representations

```{r tSNE}
set.seed(1234)
sce <- runTSNE(sce, exprs_values = "logcounts", perplexity = 10)
plotTSNE(sce, colour_by = "phenoid")
plotTSNE(sce, colour_by = "total_features", size_by = "total_counts")
```

## Save the normalized and cell filtered dataset

```{r save-data}
sce <- sce[!grepl("^ERCC", rownames(sce)), ]
dim(sce)
table(sce$phenoid)
saveRDS(sce, file = "../../data/sce_full/sce_full_Zhengmix4eq.rds")
```

## Session info

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

