Skip to content

Commit

Permalink
Add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mfoos committed Apr 14, 2019
1 parent 6386137 commit 2ad0c7e
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 27 deletions.
27 changes: 17 additions & 10 deletions 01_advanced.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ library(dplyr)
library(tidyr)
library(ggplot2)

# Read in the expression table, and identify the non-gene columns (metadata)
expr_table <- read.table("expression_matrix_w_metadata.txt", sep = ",", header = TRUE, stringsAsFactors = FALSE)
metacols <- grep("^ENSG", colnames(expr_table), invert = TRUE, value = TRUE)

# Read in the list of genes, discard genes with no expression data,
# and turn the gene name/gene id table into a lookup list
allgenes <- read.table("mart_export.txt", sep = "\t", header = TRUE, stringsAsFactors = FALSE)
allgenes <- allgenes[allgenes$ensId %in% colnames(expr_table),]
lookuptable <- unstack(allgenes)
Expand All @@ -14,9 +17,11 @@ ui <- fluidPage(

titlePanel("Immune Cell Gene Expression Data"),

sidebarLayout(
sidebarLayout( # Determines that this app will use the sidebar layout method
sidebarPanel(
# Populate the dropdown from the allgenes vector values
selectizeInput("ingene", "Choose a gene:", choices = sort(allgenes$symbol), selected = "HBB"),
# Don't be that guy who doesn't give others credit
helpText(h4("Data source:")),
helpText("Linsley PS, Speake C, Whalen E, Chaussabel D.", em("Copy number loss of the interferon gene cluster in melanomas is linked to reduced T cell infiltrate and poor patient prognosis."),
"PLoS One 2014 Oct 14;9(10):e109760."),
Expand All @@ -36,27 +41,29 @@ ui <- fluidPage(
server <- function(input, output) {

output$theplot <- renderPlot({
# Check that the input gene is not null before plotting
req(input$ingene)

# Take the human-friend user input (gene name) and convert
# it to Ensembl Id
ens <- lookuptable[[input$ingene]]
gene <- input$ingene

# Pull out the chosen gene and metadata only
tinyframe <- expr_table[,c(ens, metacols)]
tinyframe <- tinyframe %>% gather(variable, value, one_of(ens))
p <- ggplot(tinyframe, aes(x = variable, y = value, color = cellType)) +

# Tidy the dataframe for plotting
tinyframe <- tinyframe %>%
gather(variable, value, one_of(ens))

ggplot(tinyframe, aes(x = variable, y = value, color = cellType)) +
ggtitle(paste0(gene, " expression by cell type across conditions")) +
geom_boxplot() +
facet_grid(~ diseaseStatus) +
ylab("Normalized Counts") +
theme(axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
axis.title.x = element_blank())

if (length(ens) > 1) {
p <- p + facet_grid(variable ~ diseaseStatus, scales = "free_y")
} else {
p <- p + facet_grid(~ diseaseStatus)
}
return(p)
})

}
Expand Down
8 changes: 6 additions & 2 deletions 01_simple.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
library(shiny)


ui <- fluidPage(
ui <- fluidPage(

fluidRow(
fluidRow( # Determines that this app will use the "fluid page" (grid) layout method
column(width = 12,
# Creates the input widget
textInput("textinputlabel", "What's up?"),
# Designates the place-holder for the output generated
textOutput("newtext")

)
Expand All @@ -14,6 +16,8 @@ ui <- fluidPage(

server <- function(input, output) {

# Transforms the input to uppercase and writes it where it can be read
# into the user interface
output$newtext <- renderText({
toupper(input$textinputlabel)
})
Expand Down
2 changes: 1 addition & 1 deletion 02_advanced.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
library(profvis)

# Run the application
# Surprise! We're just going to look at the last one!
profvis(runApp("01_advanced.R"))

5 changes: 5 additions & 0 deletions 02_simple.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ ui <- fluidPage(

fluidRow(
column(width = 12,
# Create input widget and place-hold outputs
sliderInput("sample_count", "How many times should we sample each population?",
value = 10,
min = 1,
Expand All @@ -20,15 +21,19 @@ ui <- fluidPage(
server <- function(input, output) {

sample_dat <- reactive({
# Sample from two normal distributions with slightly different means
# The rowid is just a unique identifier to allow tidying
data.frame("rowid" = paste0("row", seq(1,input$sample_count)),
"thicc bois" = rnorm(input$sample_count, 0, 1),
"chonkers" = rnorm(input$sample_count, 0.2, 1),
stringsAsFactors = FALSE,
check.names = FALSE) %>%
# "Tidy" the dataframe into "long" format for plotting
gather(population, measurement, -rowid)
})

output$sample_plot <- renderPlot({
# Plot the two populations
ggplot(sample_dat(), aes(x = population, y = measurement, fill = population)) +
geom_boxplot() +
geom_point(position = position_jitterdodge(0.5)) +
Expand Down
27 changes: 18 additions & 9 deletions 03_advanced.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ library(tidyr)
library(ggplot2)
library(RSQLite)

# Pull gene names from the database when they occur in both the
# gene list and the expression table
conn <- dbConnect(RSQLite::SQLite(), "GSE60424.db")
choosenames <- dbGetQuery(conn, "SELECT symbol
FROM identifiers i
Expand All @@ -18,7 +20,9 @@ ui <- fluidPage(

sidebarLayout(
sidebarPanel(
# Create input widget, but do not populate it with any genes
selectizeInput("ingene", "Choose a gene:", choices = NULL, selected = NULL),

helpText(h4("Data source:")),
helpText("Linsley PS, Speake C, Whalen E, Chaussabel D.", em("Copy number loss of the interferon gene cluster in melanomas is linked to reduced T cell infiltrate and poor patient prognosis."),
"PLoS One 2014 Oct 14;9(10):e109760."),
Expand All @@ -36,12 +40,17 @@ ui <- fluidPage(
)

server <- function(input, output, session) {


# Populate the selection widget after the rest of the page loads.
# Using server = TRUE makes it even faster
updateSelectizeInput(session, "ingene", choices = choosenames, selected = "HBB", server = TRUE)

# Function to pull selected gene from SQLite database rather
# than reading in a full text ("flat") file
makeTinyframe <- function(gene, dbpath){
conn <- dbConnect(RSQLite::SQLite(), dbpath)
meta <- dbGetQuery(conn, "SELECT * FROM meta;")
# This text is SQL code:
exprquery <- sprintf("SELECT i.symbol, e.*
FROM identifiers i
JOIN expr e
Expand All @@ -64,31 +73,31 @@ server <- function(input, output, session) {

}

# Function to create the plot
makePlot <- function(tinyframe){

# This is just because the whole dataframe is passed to the function,
# but we want the gene name itself for the title text
printgene <- unique(tinyframe$genename)

p <- ggplot(tinyframe, aes(x = genename, y = TPM, color = celltype)) +
ggplot(tinyframe, aes(x = genename, y = TPM, color = celltype)) +
ggtitle(paste0(printgene, " expression by cell type across conditions")) +
geom_boxplot() +
facet_grid(~ disease_status) +
theme(axis.text.x = element_blank(), axis.ticks.x = element_blank(), axis.title.x = element_blank())

if (length(unique(tinyframe$gene)) > 1) {
p <- p + facet_grid(variable ~ disease_status, scales = "free_y")
} else {
p <- p + facet_grid(~ disease_status)
}
return(p)
}

inputgene <- reactive({
# This allows us to return custom text if there is something wrong
# with the input. The app will not continue until the "need" is met.
validate(
need(input$ingene != "", "Please select a gene")
)
input$ingene
})

output$theplot <- renderPlot({
# Call the querying and plotting functions
tf <- makeTinyframe(inputgene(), "GSE60424.db")
makePlot(tf)
})
Expand Down
8 changes: 8 additions & 0 deletions 03_simple.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ ui <- fluidPage(
server <- function(input, output) {

sample_dat <- reactive({
# Perform the random sampling and assign it to a reactive object
# so when we plot and perform a t-test, they are not getting
# different random samples
data.frame("rowid" = paste0("row", seq(1,input$sample_count)),
"thicc bois" = rnorm(input$sample_count, 0, 1),
"chonkers" = rnorm(input$sample_count, 0.2, 1),
Expand All @@ -28,6 +31,8 @@ server <- function(input, output) {
})

output$sample_plot <- renderPlot({
# In this example the tidying is done as part of plotting
# because the t-test uses the original data format
sample_dat() %>%
gather(population, measurement, -rowid) %>%
ggplot(aes(x = population, y = measurement, fill = population)) +
Expand All @@ -40,9 +45,12 @@ server <- function(input, output) {
})

output$sample_result <- renderText({
# As in the plot, the reactive object must be called
# with parentheses (because it's actually a function)
p <- t.test(sample_dat()[["thicc bois"]],
sample_dat()[["chonkers"]],
paired = FALSE)[["p.value"]]
# Add an asterisk to "significant" results
if(p >= 0.05){
paste("p-value = ", p)
} else {
Expand Down
20 changes: 15 additions & 5 deletions 04_simple.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ ui <- fluidPage(

fluidRow(
column(width = 12,
# Clicking the button increases the value of input$do_sample by 1
actionButton("do_sample", "Moar!"),
plotOutput("sample_plot"),
textOutput("sample_result")
Expand All @@ -15,19 +16,28 @@ ui <- fluidPage(
)

server <- function(input, output) {


# Initialize the reactive value at 10.
# The reactive value can store intermediate values
# while keeping them in "reactive context"
sample_count <- reactiveVal(10)

# When the button is clicked, input$do_sample changes
# and triggers the contents of the "observeEvent" expression
# to run. The observeEvent does not return a value like "render"
# expressions do.
observeEvent(input$do_sample,{
x <- sample_count() + 10
sample_count(x)
})

sample_dat <- reactive({
tmp <- sample_count()
data.frame("rowid" = paste0("row", seq(1, tmp)),
"thicc bois" = rnorm(tmp, 0, 1),
"chonkers" = rnorm(tmp, 0.2, 1),
# When observeEvent is triggers, it changes the value of
# sample_count(), so everything depending on sample_count()
# recalculates. Here it regenerates the data frame.
data.frame("rowid" = paste0("row", seq(1, sample_count())),
"thicc bois" = rnorm(sample_count(), 0, 1),
"chonkers" = rnorm(sample_count(), 0.2, 1),
stringsAsFactors = FALSE,
check.names = FALSE)
})
Expand Down

0 comments on commit 2ad0c7e

Please sign in to comment.