-
Notifications
You must be signed in to change notification settings - Fork 10
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
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 f936f3a
Remove files now being ignored
jaclyn-taroni 6f6638e
Add PLIER custom function
jaclyn-taroni 474f4bf
Add R notebook for proof-of-concept of PLIER util
jaclyn-taroni 795b5cb
Add wrapper script for conversion of Rmd notebook files to .R
jaclyn-taroni 7a873d7
Add proof-of-concept NB script
jaclyn-taroni 74ee06e
knit to PDF
jaclyn-taroni f161af9
Docker image with ability to knit to PDF
jaclyn-taroni 8a21cce
newline fix
jaclyn-taroni 89f8f5b
Remove script extracted from notebook
jaclyn-taroni 00e590c
Remove converted PDF & associated Dockerfile
jaclyn-taroni 86747d0
Remove knitr::purl wrapper
jaclyn-taroni df6ebc0
Update proof-of-concept notebook
jaclyn-taroni 378ac59
Update plot directory structure
jaclyn-taroni d3228b2
Add recount PLIER EDA notebook
jaclyn-taroni 300bb12
Remove checks for FDR presence
jaclyn-taroni 97e7f57
Add sorted immune cell notebook
jaclyn-taroni 530c542
Update: style & doc in response to PR comments
jaclyn-taroni 1fa6d4b
Simplify reordering
jaclyn-taroni 19a3635
Update: docs, rerun function updates
jaclyn-taroni 83a8466
Correct documentation
jaclyn-taroni e3fe726
Update: recount EDA notebook in response to PR comments
jaclyn-taroni 1134db9
Update heatmap
jaclyn-taroni f7c500a
Add comment to correlation function
jaclyn-taroni fd4ae21
Sub sapply for loop
jaclyn-taroni File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
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") | ||
``` |
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?