Skip to content

Instantly share code, notes, and snippets.

@trestletech
Last active September 25, 2019 17:43
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trestletech/5929468 to your computer and use it in GitHub Desktop.
Save trestletech/5929468 to your computer and use it in GitHub Desktop.
Shiny Example #2 for Bioconductor

Bioconductor Shiny Example #2

A demonstration of Shiny's text widget support.

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(5929468)

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 ExpressionSets we've generated ahead of time.
tumor <- readRDS("tumorExpr.Rds")
normal <- readRDS("normalExpr.Rds")
shinyServer(function(input, output) {
# Return the requested dataset
datasetInput <- reactive({
t(switch(input$tissue,
"Tumor" = exprs(tumor),
"Normal" = exprs(normal)))
})
# Generate a summary of the dataset
output$summary <- renderPrint({
dataset <- datasetInput()[,1:input$numCols]
summary(dataset)
})
# Show the first "n" observations
output$view <- renderTable({
head(datasetInput()[,1:input$numCols], n = input$numRows)
})
})
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)
shinyUI(pageWithSidebar(
# Application title
headerPanel("Hello Shiny Bioconductor Text!"),
# Sidebar with a selector to choose a gene
sidebarPanel(
selectInput("tissue", "Tissue Type:", c("Tumor", "Normal")),
numericInput("numCols", "Max Genes to Display:", value=5),
numericInput("numRows", "Max Samples to Display:", value=5)
),
# Show a plot of the generated distribution
mainPanel(
verbatimTextOutput("summary"),
tableOutput("view")
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment