Skip to content

Instantly share code, notes, and snippets.

@trestletech
Last active March 12, 2020 12:27
Show Gist options
  • Save trestletech/5924147 to your computer and use it in GitHub Desktop.
Save trestletech/5924147 to your computer and use it in GitHub Desktop.
Shiny Example #1 for Bioconductor

Bioconductor Shiny Example #1

Simple app comparing the expression of genes between Tumor and Normal tissue.

This is an example Shiny app featuring some basic analysis of Ovarian Cancer gene expression data collected on the Affymetrix U133A platform. We filter the available genes and samples down to a much smaller matrix to make reproducibility simpler for a broader audience. The R code involved in sampling the data is available in this Gist as an R-Markdown file, and the sampled data are available in this Gist as Rds files.

To run the application, install shiny (install.packages("shiny")) then run the following command:

library(shiny)
runGist(5924147)

The relevant citation for the original data is available below:

Benjamin Frederick Ganzfried, Markus Riester, Benjamin Haibe-Kains, Thomas Risch, Svitlana Tyekucheva, Ina Jazic, Xin Victoria Wang, Mahnaz Ahmadifar, Michael Birrer, Giovanni Parmigiani, Curtis Huttenhower, Levi Waldron. curatedOvarianData: Clinically Annotated Data for the Ovarian Cancer Transcriptome, Database 2013: bat013 doi:10.1093/database/bat013 published online April 2, 2013.

library(shiny)
library(Biobase)
# Load in the sampled matrices we've generated ahead of time.
tumor <- readRDS("tumorExpr.Rds")
# Distill the expression data away from the ExpressionSet object.
tumorExp <- exprs(tumor)
# And now the normal sample.
normal <- readRDS("normalExpr.Rds")
normalExp <- exprs(normal)
shinyServer(function(input, output) {
# Expression that generates a boxplot of gene expression. The
# expression is wrapped in a call to renderPlot to indicate that:
#
# 1) It is "reactive" and therefore should be automatically
# re-executed when inputs change
# 2) Its output type is a plot
#
output$genePlot <- renderPlot({
#Extract the relevant tumor and normal expression values.
tumorGene <- tumorExp[input$gene, ]
normalGene <- normalExp[input$gene, ]
#Format as a list and plot as a boxplot
expr <- list(Tumor=tumorGene, Normal=normalGene)
boxplot(expr, main=input$gene)
})
})

Extract and Store Some Data

We'll first want to install the curatedOvarianData package from Bioconductor. Note that this package is a few hundred MB.

source("http://bioconductor.org/biocLite.R")
biocLite("curatedOvarianData")

Now we'll load the package and load in one of the datasets.

library(curatedOvarianData)
data(TCGA_eset)

Let's write out the clinical data so we have it for later. We'll trim out the "uncurated" metadata column to save some space and make the printing a bit easier.

clinical <- phenoData(TCGA_eset)
pData(clinical) <- pData(clinical)[, colnames(pData(clinical)) != "uncurated_author_metadata"]

We won't want to store all of the tumor samples available in the dataset, so let's grab 20 tumor samples at random and keep all of the normal samples.

# Get all normal phenotypic data
normalClinical <- clinical
pData(normalClinical) <- pData(clinical)[pData(clinical)$sample_type == "adjacentnormal", 
    ]

# Sample 20 samples from the tumor phenotypic data
tumorIndices <- sample(which(pData(clinical)$sample_type == "tumor"), 20)
tumorClinical <- clinical
pData(tumorClinical) <- pData(clinical)[tumorIndices, ]

Filtering

For the sake of demonstration, we'll want to keep the dataset fairly small. So let's filter the data to the few dozen samples we selected above and only 8 genes.

# Provide the HGNC symbols of 8 genes
geneList <- c("EGFR", "KLF6", "FOXO1", "KRAS", "JAK2", "BRCA1", "BRCA2", "PPM1D")

# Extract the relevant probeset to gene mappings
featureList <- fData(TCGA_eset)
featureList <- featureList[featureList$gene %in% geneList, ]
featureList <- featureList[match(geneList, rownames(featureList)), ]  #order
featureList <- AnnotatedDataFrame(featureList)

# Filter the tumor expression data to only relevant genes and samples
tumor <- exprs(TCGA_eset[, tumorIndices])
tumor <- tumor[match(geneList, rownames(tumor)), ]
tumor <- ExpressionSet(tumor, tumorClinical, featureList, experimentData(TCGA_eset))
saveRDS(tumor, "tumorExpr.Rds")

And grab all of the normal tissue samples:

# Filter the normal expression data to only relevant genes and samples
normal <- exprs(TCGA_eset[, TCGA_eset@phenoData@data$sample_type == "adjacentnormal"])
normal <- normal[match(geneList, rownames(normal)), ]
normal <- ExpressionSet(normal, normalClinical, featureList, experimentData(TCGA_eset))
saveRDS(normal, "normalExpr.Rds")

Now we should be able to use this sampled data from our apps!

Extract and Store Some Data
========================================================
We'll first want to install the `curatedOvarianData` package from Bioconductor. Note that this package is a few hundred MB.
```{r, cache=TRUE, results='hide', message=FALSE}
source("http://bioconductor.org/biocLite.R")
biocLite("curatedOvarianData")
```
Now we'll load the package and load in one of the datasets.
```{r, results='hide', message=FALSE}
library(curatedOvarianData)
data(TCGA_eset)
```
Let's write out the clinical data so we have it for later. We'll trim out the "uncurated" metadata column to save some space and make the printing a bit easier.
```{r}
clinical <- phenoData(TCGA_eset)
pData(clinical) <- pData(clinical)[,colnames(pData(clinical))!="uncurated_author_metadata"]
```
We won't want to store all of the tumor samples available in the dataset, so let's grab 20 tumor samples at random and keep all of the normal samples.
```{r}
# Get all normal phenotypic data
normalClinical <- clinical
pData(normalClinical) <- pData(clinical)[pData(clinical)$sample_type == "adjacentnormal",]
# Sample 20 samples from the tumor phenotypic data
tumorIndices <- sample(which(pData(clinical)$sample_type == "tumor"), 20)
tumorClinical <- clinical
pData(tumorClinical) <- pData(clinical)[tumorIndices,]
```
Filtering
---------
For the sake of demonstration, we'll want to keep the dataset fairly small. So let's filter the data to the few dozen samples we selected above and only 8 genes.
```{r}
# Provide the HGNC symbols of 8 genes
geneList <- c("EGFR", "KLF6", "FOXO1", "KRAS", "JAK2", "BRCA1", "BRCA2", "PPM1D")
# Extract the relevant probeset to gene mappings
featureList <- fData(TCGA_eset)
featureList <- featureList[featureList$gene %in% geneList, ]
featureList <- featureList[match(geneList, rownames(featureList)), ] #order
featureList <- AnnotatedDataFrame(featureList)
# Filter the tumor expression data to only relevant genes and samples
tumor <- exprs(TCGA_eset[,tumorIndices])
tumor <- tumor[match(geneList, rownames(tumor)),]
tumor <- ExpressionSet(tumor, tumorClinical, featureList, experimentData(TCGA_eset))
saveRDS(tumor, "tumorExpr.Rds")
```
And grab all of the normal tissue samples:
```{r}
# Filter the normal expression data to only relevant genes and samples
normal <- exprs(TCGA_eset[,TCGA_eset@phenoData@data$sample_type == "adjacentnormal"])
normal <- normal[match(geneList, rownames(normal)),]
normal <- ExpressionSet(normal, normalClinical, featureList, experimentData(TCGA_eset))
saveRDS(normal, "normalExpr.Rds")
```
Now we should be able to use this sampled data from our apps!
library(shiny)
geneList <- c( "KRAS", "EGFR", "KLF6", "FOXO1", "JAK2", "BRCA1", "BRCA2", "PPM1D")
shinyUI(pageWithSidebar(
# Application title
headerPanel("Hello Shiny Bioconductor!"),
# Sidebar with a selector to choose a gene
sidebarPanel(
selectInput("gene", "Gene", geneList)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("genePlot")
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment