🔸 Code Along for F-IE-2: PPI Visualization APP

Task: Get Started with R Shiny for Bioinformatics

Welcome to our tutorial on creating your own Bioinformatics tool using R Shiny! This task is designed to familiarize you with the basic workflow of R Shiny and serves as an alternative pathway to qualifying for the AI Evaluator, especially for those working on the PPI Visualization Project. We’ll start by exploring existing Bioinformatics tools built with Shiny to spark your creativity and demonstrate potential achievements. Then, you’ll build your own simple application. Once your app is up and running, we encourage you to enhance its features.

Please share your work by replying to this post with a screenshot of your enhanced app. Your active participation and the enhancements you implement will serve as the second stage of evaluation for the Virtual-Internships. Alternatively, you may choose to participate in the AI Evaluator session, which will focus on discussing your progress and findings from tinkering with the PPI Visualization Project.

Let’s begin by setting a strong foundation in R Shiny that will carry you through to creating more complex projects like a PPI visualization app.


Step 1: Explore Existing Bioinformatics Tools Built with Shiny

Discover a variety of Bioinformatics tools developed with Shiny to get an idea of what’s possible. This exploration will help inspire your own projects by showing you different functionalities and applications.

For more Shiny tools in different Bioinformatics applications, explore PubMed.


Step 2: Experiment with Shiny Gallery Apps

Familiarize yourself with Shiny’s capabilities by running examples from the Shiny Gallery. This practical experience will help you understand the framework and how to manipulate its components.

Task:

  • Visit the Shiny Gallery.
  • Choose and run at least two examples in your RStudio.

Resources for Learning Shiny


Step 3: Build Your Own App

Project: Gene Expression Explorer Based on Airway Data

In this project, we’ll create a Shiny app that allows users to explore gene expression data from the airway dataset. This app demonstrates how to load biological data, process it, and create interactive visualizations.

Data Source:

  • We’ll use the ‘airway’ dataset, which is available through the Bioconductor project. This dataset contains RNA-Seq data from airway smooth muscle cells.

Features:

  1. Load and process gene expression data from the airway dataset.
  2. Allow users to select specific genes for analysis.
  3. Visualize gene expression across different treatments using boxplots or violin plots.
  4. Display detailed data in an interactive table.

This is what your final app will look like:

image


Step-by-Step Instructions

  1. Start RStudio and create a new project.
  2. Create a new file and save it as app.R.
  3. Copy the code provided below into app.R.
  4. Click ‘Run App’ to launch your Shiny application.

# Install necessary packages if not already installed
if (!requireNamespace("BiocManager", quietly = TRUE))
  install.packages("BiocManager")
packages_needed <- c("shiny", "ggplot2", "plotly", "DT", "dplyr", "airway", "SummarizedExperiment", "tidyr")
packages_to_install <- packages_needed[!packages_needed %in% installed.packages()[,"Package"]]
if (length(packages_to_install) > 0) {
  BiocManager::install(packages_to_install)
}

# Load libraries
library(shiny)
library(ggplot2)
library(plotly)
library(DT)
library(dplyr)
library(airway)
library(SummarizedExperiment)
library(tidyr)

# Load the airway dataset
data("airway")
airway_data <- as.data.frame(assay(airway))
coldata <- as.data.frame(colData(airway))

# Select a few genes for demonstration
selected_genes <- c("ENSG00000000003", "ENSG00000000005", "ENSG00000000419", "ENSG00000000457", "ENSG00000000460")

# Prepare the data
airway_data <- airway_data[selected_genes, ]
airway_data$Gene <- rownames(airway_data)
airway_long <- airway_data %>%
  pivot_longer(cols = -Gene, names_to = "Sample", values_to = "Expression")

# Add sample names to coldata
coldata$Sample <- rownames(coldata)

# Join the metadata
airway_long <- airway_long %>%
  left_join(coldata, by = "Sample")

ui <- fluidPage(
  titlePanel("Gene Expression Explorer Based on Airway Data (Demo)"),
  
  sidebarLayout(
    sidebarPanel(
      selectInput("gene", "Select Gene:", choices = selected_genes),
      radioButtons("plot_type", "Select Plot Type:",
                   choices = c("Boxplot", "Violin Plot"),
                   selected = "Boxplot")
    ),
    mainPanel(
      plotlyOutput("gene_plot"),
      DTOutput("table")
    )
  )
)

server <- function(input, output) {
  
  output$table <- renderDT({
    airway_long %>%
      filter(Gene == input$gene) %>%
      select(Sample, Expression, dex, cell)
  })
  
  output$gene_plot <- renderPlotly({
    filtered_data <- airway_long %>%
      filter(Gene == input$gene)
    
    if (input$plot_type == "Boxplot") {
      p <- ggplot(filtered_data, aes(x = dex, y = Expression, fill = dex)) +
        geom_boxplot() +
        theme_minimal() +
        labs(title = paste("Expression of", input$gene), x = "Treatment", y = "Expression Level")
    } else {
      p <- ggplot(filtered_data, aes(x = dex, y = Expression, fill = dex)) +
        geom_violin(trim = FALSE) +
        geom_boxplot(width = 0.1, fill = "white") +
        theme_minimal() +
        labs(title = paste("Expression of", input$gene), x = "Treatment", y = "Expression Level")
    }
    
    ggplotly(p)
  })
}

shinyApp(ui = ui, server = server)


Understanding the Code

  1. Package Installation and Loading: The code begins by installing and loading necessary packages, including Shiny, ggplot2, and Bioconductor packages.
  2. Data Preparation: We load the airway dataset and process it into a format suitable for our app. We select a few genes for demonstration purposes to improve performance.
  3. User Interface (UI): The UI is defined with a title, a sidebar for user inputs (gene selection and plot type), and a main panel for displaying the plot and data table.
  4. Server Logic: The server function contains the logic for creating the plot and table based on user inputs. It filters the data for the selected gene and creates either a boxplot or violin plot as specified by the user.
  5. Interactive Elements: The app uses plotly to create interactive plots and DataTables for an interactive data display.

Customizing the App

Customizing your Shiny app allows you to tailor it to specific research needs or personal preferences. Here are some ways you can extend and enhance your app:

  • Adding New Features:
    • Multiple Analysis Options: Allow users to select not just genes, but also the type of analysis they wish to perform. This could include descriptive statistics, correlation analysis, or clustering techniques.
    • Visualization Types: Expand the range of visualization options available. Besides boxplots and violin plots, consider adding scatter plots, heatmaps, or interactive 3D plots to enhance data presentation.
  • Interactive Elements:
    • Enhance user interaction by adding sliders for numeric inputs, dropdown menus for categorical choices, or checkboxes for options like normalization or logarithmic transformation.

Troubleshooting Common Issues

  • Package Installation Errors: Make sure you have the correct versions of R and RStudio. Some packages might require newer versions of R. If a package fails to install, try updating R and retry the installation.
  • Code Errors: Double-check the code for typos, especially in variable names and function calls. R is case-sensitive, which can lead to errors if not careful.


Additional Resources

Hi @Moh_Saiger, great job getting the app up and running! Can you take a shot at one of the customizations? The goal is to get you (all members of this project) confident on the coding side so that you can work with the more experienced bioformaticians from the advanced project.

1 Like

1 Like