Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom functions for working with PLIER models and initial exploratory analyses #3

Merged
merged 25 commits into from
Apr 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
02fe738
Merge branch 'master' into origin/initial-util
jaclyn-taroni Apr 3, 2018
f936f3a
Remove files now being ignored
jaclyn-taroni Apr 4, 2018
6f6638e
Add PLIER custom function
jaclyn-taroni Apr 4, 2018
474f4bf
Add R notebook for proof-of-concept of PLIER util
jaclyn-taroni Apr 4, 2018
795b5cb
Add wrapper script for conversion of Rmd notebook files to .R
jaclyn-taroni Apr 4, 2018
7a873d7
Add proof-of-concept NB script
jaclyn-taroni Apr 4, 2018
74ee06e
knit to PDF
jaclyn-taroni Apr 4, 2018
f161af9
Docker image with ability to knit to PDF
jaclyn-taroni Apr 4, 2018
8a21cce
newline fix
jaclyn-taroni Apr 4, 2018
89f8f5b
Remove script extracted from notebook
jaclyn-taroni Apr 4, 2018
00e590c
Remove converted PDF & associated Dockerfile
jaclyn-taroni Apr 4, 2018
86747d0
Remove knitr::purl wrapper
jaclyn-taroni Apr 4, 2018
df6ebc0
Update proof-of-concept notebook
jaclyn-taroni Apr 4, 2018
378ac59
Update plot directory structure
jaclyn-taroni Apr 4, 2018
d3228b2
Add recount PLIER EDA notebook
jaclyn-taroni Apr 4, 2018
300bb12
Remove checks for FDR presence
jaclyn-taroni Apr 5, 2018
97e7f57
Add sorted immune cell notebook
jaclyn-taroni Apr 5, 2018
530c542
Update: style & doc in response to PR comments
jaclyn-taroni Apr 6, 2018
1fa6d4b
Simplify reordering
jaclyn-taroni Apr 6, 2018
19a3635
Update: docs, rerun function updates
jaclyn-taroni Apr 6, 2018
83a8466
Correct documentation
jaclyn-taroni Apr 6, 2018
e3fe726
Update: recount EDA notebook in response to PR comments
jaclyn-taroni Apr 6, 2018
1134db9
Update heatmap
jaclyn-taroni Apr 6, 2018
f7c500a
Add comment to correlation function
jaclyn-taroni Apr 6, 2018
fd4ae21
Sub sapply for loop
jaclyn-taroni Apr 6, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
153 changes: 153 additions & 0 deletions 01-PLIER_util_proof-of-concept_notebook.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
---
title: "PLIER Utils proof-of-concept"
output:
html_notebook: default
pdf_document: default
---
**J. Taroni 2018**

In this notebook, I aim to demonstrate the validity of implementation for a
subset of PLIER custom functions. Specifically, checking the operations for:

* Training a PLIER model on a new dataset `PLIERNewData()`
* Applying a previously computed PLIER to a new dataset to get the LV x sample
matrix (B) `GetNewDataB()`
* Reconstruction of input gene expression data with a PLIER model
`GetReconstructedExprs()` and the evaluation function
`GetReconstructionCorrelation()`

Here, I use the **NARES dataset** for convenience due to its relatively small
size (n = 77).

#### Load libraries and custom functions
```{r}
library(AnnotationDbi)
library(PLIER)

source(file.path("util", "plier_util.R"))
```

```{r}
# plots directory specifically for this notebook
dir.create(file.path("plots", "01"), recursive = TRUE,
showWarnings = FALSE)
```


#### NARES expression data

```{r}
# Read in PCL
nares.data <- readr::read_tsv(file.path("data", "expression_data",
"NARES_SCANfast_ComBat.pcl"),
progress = FALSE)
```
Building a PLIER model requires HGNC symbol annotation, as this is what is
included in the prior information (e.g., pathways, genesets) that is included
in the package.

```{r}
symbol.obj <- org.Hs.eg.db::org.Hs.egSYMBOL
mapped.genes <- AnnotationDbi::mappedkeys(symbol.obj)
symbol.list <- as.list(symbol.obj[mapped.genes])
symbol.df <- as.data.frame(cbind(names(symbol.list), unlist(symbol.list)))
colnames(symbol.df) <- c("EntrezID", "GeneSymbol")

# get gene column name to match to facilitate use with dplyr
colnames(nares.data)[1] <- "EntrezID"

# matching types
symbol.df$EntrezID <- as.integer(as.character(symbol.df$EntrezID))

# inner join
annot.nares.data <- dplyr::inner_join(symbol.df, nares.data, by = "EntrezID")

# only leave the matrix with gene symbol as rownames
exprs.mat <- dplyr::select(annot.nares.data, -EntrezID)
rownames(exprs.mat) <- exprs.mat$GeneSymbol
exprs.mat <- as.matrix(dplyr::select(exprs.mat, -GeneSymbol))
```

#### Train PLIER model

`PLIERNewData` is a wrapper function for applying PLIER to a new dataset.
Expression data is row-normalized for use with PLIER.
We use the following genesets that come with PLIER: `bloodCellMarkersIRISDMAP`,
`svmMarkers`, and `canonicalPathways`.
See also the [PLIER vignette](https://github.com/wgmao/PLIER/blob/a2d4a2aa343f9ed4b9b945c04326bebd31533d4d/vignettes/vignette.pdf).

```{r}
nares.plier <- PLIERNewData(exprs.mat)
```

#### Apply PLIER model to "new" expression dataset

The `GetNewDataB` function will first row-normalize and reorder the "new" input
gene expression data (`exprs.mat`), and then using a previously computed PLIER
model (`plier.model`, specifically the gene loadings and the L2 constant), get
the new data into the PLIER model LV space. Here, we supply the NARES data as
the expression data and the PLIER model that has already been trained on the
same gene expression matrix.

```{r}
new.b.mat <- GetNewDataB(exprs.mat = exprs.mat,
plier.model = nares.plier)
# NARES B matrix from PLIERNewData
nares.b.mat <- nares.plier$B
```
We want to ensure that the two B matrices are the same.

```{r}
all.equal(nares.b.mat, new.b.mat)
```

#### Reconstruction of gene expression data

We reconstruct gene expression data from the gene loadings and LVs.

```{r}
nares.recon <- GetReconstructedExprs(z.matrix = as.matrix(nares.plier$Z),
b.matrix = as.matrix(nares.plier$B))
```

Let's evaluate the reconstruction.
We'll need the row-normalized NARES expression data (input) for comparison.

```{r}
nares.rownorm <- PLIER::rowNorm(exprs.mat)
nares.rownorm <- nares.rownorm[rownames(nares.recon), ]
```

Spearman correlation between input, row-normalized expression data and the
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it worth adding a bit of interpretation of what the plot represents here?

reconstructed data.
If correlation between the input and the reconstructed data is high, that
suggests that reconstruction is "successful."
Given the different constraints in PLIER, we would not expect to perfectly
(`rho = 1`) reconstruct the input data.
This particular evaluation will be _most useful_ when we look at applying a
trained PLIER model to a test dataset.
```{r}
recon.correlation <- GetReconstructionCorrelation(true.mat = nares.rownorm,
recon.mat = nares.recon)
```

Plot density
```{r}
# density plot
p <- ggplot2::ggplot(as.data.frame(recon.correlation),
ggplot2::aes(x = recon.correlation)) +
ggplot2::geom_density() +
ggplot2::theme_bw() +
ggplot2::labs(x = "Spearman Correlation",
title = "Input vs. PLIER reconstructed NARES data") +
ggplot2::theme(plot.title = ggplot2::element_text(hjust = 0.5,
face = "bold"))
p
```

```{r}
png.file <- file.path("plots", "01",
"NARES_reconstructed_data_correlation_density.png")
ggplot2::ggsave(filename = png.file, plot = p, width = 7, height = 5,
units = "in")
```
659 changes: 659 additions & 0 deletions 01-PLIER_util_proof-of-concept_notebook.nb.html

Large diffs are not rendered by default.