<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.10.0">Jekyll</generator><link href="http://www.genviz.org//feed.xml" rel="self" type="application/atom+xml" /><link href="http://www.genviz.org//" rel="alternate" type="text/html" /><updated>2025-11-15T16:10:04+00:00</updated><id>http://www.genviz.org//feed.xml</id><title type="html">Griffith Lab</title><subtitle>This resource is structured in a modular format. Users would benefit from familiarity with topics covered in previous modules however it is not a requirement unless stated otherwise.</subtitle><author><name>Zachary Skidmore</name></author><entry><title type="html">Introduction to R Markdown</title><link href="http://www.genviz.org//module-07-appendix/0007/03/01/introToRmarkdown/" rel="alternate" type="text/html" title="Introduction to R Markdown" /><published>0007-03-01T00:00:00+00:00</published><updated>0007-03-01T00:00:00+00:00</updated><id>http://www.genviz.org//module-07-appendix/0007/03/01/introToRmarkdown</id><content type="html" xml:base="http://www.genviz.org//module-07-appendix/0007/03/01/introToRmarkdown/"><![CDATA[<p>A useful feature within the R ecosystem is R Markdown. R Markdown (or .Rmd) files allow a user to intersperse notes with code providing a useful framework for sharing scientific reports in a transparent and easily reproduceable way. They combine both markdown syntax for styling notes and Rscripts for running code and producing figures. Reports can be output in a variety of file formats including HTML documents, PDF documents, and even presentation slides.</p>

<h4 id="installing-r-markdown">Installing R Markdown</h4>
<ul>
  <li>To start let’s make a simple R Markdown file with Rstudio,  you will need to install the <a href="https://cran.r-project.org/web/packages/rmarkdown/index.html">R Markdown</a> package from cran.
    <div class="language-R highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># install R Markdown</span><span class="w">
</span><span class="n">install.packages</span><span class="p">(</span><span class="s2">"rmarkdown"</span><span class="p">)</span><span class="w">
</span></code></pre></div>    </div>
  </li>
</ul>

<h4 id="r-markdown-basics">R Markdown basics</h4>
<ul>
  <li>Once R Markdown has been installed we can select File -&gt; New File -&gt; R Markdown to create an intial rmarkdown template.</li>
</ul>

<figure class="figure" width="550">
    <img class="image " src="/assets/Rmarkdown/intro_2_rmarkdown_1.png" width="550" />
    
    
</figure>

<ul>
  <li>Rstudio will ask you to choose an output format, and to add a title and author, for now we will just use the default HTML format however this can be changed at any time within the Rmarkdown template.</li>
  <li>Go ahead and select okay when you have added your name and a title.</li>
</ul>

<figure class="figure" width="450">
    <img class="image " src="/assets/Rmarkdown/intro_2_rmarkdown_2.png" width="450" />
    
    
</figure>

<p>Rstudio should now have made a template for us, let’s go over a few introductory topics related to this template. At the top of the file you will see what looks like a YAML header denoted by <code class="language-plaintext highlighter-rouge">---</code>. This is where the defaults for building the file are set.</p>

<p>You will notice that R Markdown has pre-populated some fields based on what we supplied when we initalized the markdown template. You can output the R Markdown (.Rmd) document using the <code class="language-plaintext highlighter-rouge">Knit</code> button in the top left hand corner of RStudio. This is the same as calling the function <code class="language-plaintext highlighter-rouge">render()</code> which takes the path to the R Markdown file as input. This file should end in a .Rmd extension to denote it as an rmarkdown file, though Rstudio will take care of this for you the first time you hit <code class="language-plaintext highlighter-rouge">Knit</code>.</p>

<p>Rstudio also has a convenient way to insert code using the <code class="language-plaintext highlighter-rouge">insert</code> button to the right. You might notice that not only does <a href="https://cran.r-project.org/web/packages/rmarkdown/index.html">rmarkdown</a> support R, but also bash, python and a few other languages as well. Though in order to work, these languages will need to be installed before using <code class="language-plaintext highlighter-rouge">Knit</code>.</p>

<ul>
  <li>Go ahead and hit the <code class="language-plaintext highlighter-rouge">Knit</code> button just to see what an R Markdown output looks like with the default example text. If you are working with the default HTML option the result will load in a new RStudio window with the option to open it in your usual web browser.</li>
</ul>

<figure class="figure" width="650">
    <img class="image " src="/assets/Rmarkdown/intro_2_rmarkdown_3.png" width="650" />
    
    
</figure>

<p>Note: you use “include=FALSE to have the chunk evaluated, but neither the code nor its output displayed.</p>

<h4 id="creating-a-report">Creating a report</h4>
<p>Now that we’ve gone over the basics of <a href="https://cran.r-project.org/web/packages/rmarkdown/index.html">R Markdown</a> let’s create a real (but simple) report. First, you’ll need to download the Folicular Lymphoma data set we used in the previous ggplot2 section. Go ahead and download that dataset from <a href="http://www.genomedata.org/gen-viz-workshop/intro_to_ggplot2/ggplot2ExampleData.tsv">http://www.genomedata.org/gen-viz-workshop/intro_to_ggplot2/ggplot2ExampleData.tsv</a> if you don’t have it.</p>

<p>R Markdown documents combine text and code, the text portion of these documents use a lightweight text markup language known as <a href="https://en.wikipedia.org/wiki/Markdown">markdown</a>. This allows text to be displayed in a stylistic way on the web without having to write HTML or other web code. We won’t go over all of markdowns features however it will be good to familiarize yourself with this style.</p>

<p>A cheatsheet for the markdown flavor that R Markdown uses can be found by going to help -&gt; Cheatsheets -&gt; R Markdown Cheatsheets.</p>

<p>As we have mentioned you can insert a code chunk using the <code class="language-plaintext highlighter-rouge">insert</code> button on the top right. For example as shown below when selecting insert -&gt; R, we get a code chunk formatted for R. However you can also add parameters to this code chunk to alter it’s default behavior. A full list of these parameters is available <a href="https://www.rstudio.com/wp-content/uploads/2015/03/rmarkdown-reference.pdf">here</a>.</p>

<figure class="figure" width="650">
    <img class="image " src="/assets/Rmarkdown/intro_2_rmarkdown_4.png" width="650" />
    
    
</figure>

<h4 id="exercises">Exercises</h4>

<p>We have created a preliminary rmarkdown file you can download <a href="https://raw.githubusercontent.com/griffithlab/gen-viz-workshop/gh-pages/assets/Rmarkdown/rmarkdown_exercise1_question.Rmd">here</a>.</p>

<p>Fill in this document to make it more complete, and then knit it together. The steps you should follow are outlined right in this R Markdown document. You can open this file in RStudio by going to File -&gt; Open File. An R Markdown reference is available <a href="https://www.rstudio.com/wp-content/uploads/2015/03/rmarkdown-reference.pdf">here</a>.</p>

<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>

<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>

<script type="text/javascript" src="/assets/scripts/question.js"></script>

<div class="accordion">
    <p class="q">Get a hint!</p>
    <p class="a">Look at the R Markdown reference guide mentioned above or the cheatsheets in Rstudio.</p>
</div>

<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>

<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>

<script type="text/javascript" src="/assets/scripts/question.js"></script>

<div class="accordion">
    <p class="q">Answer</p>
    <p class="a">Here is a more complete <a href="https://raw.githubusercontent.com/griffithlab/gen-viz-workshop/gh-pages/assets/Rmarkdown/rmarkdown_exercise1_answer.Rmd">.Rmd file</a>.</p>
</div>]]></content><author><name>Zachary Skidmore</name></author><category term="Module-07-Appendix" /><summary type="html"><![CDATA[A useful feature within the R ecosystem is R Markdown. R Markdown (or .Rmd) files allow a user to intersperse notes with code providing a useful framework for sharing scientific reports in a transparent and easily reproduceable way. They combine both markdown syntax for styling notes and Rscripts for running code and producing figures. Reports can be output in a variety of file formats including HTML documents, PDF documents, and even presentation slides. Installing R Markdown To start let’s make a simple R Markdown file with Rstudio, you will need to install the R Markdown package from cran. # install R Markdown install.packages("rmarkdown") R Markdown basics Once R Markdown has been installed we can select File -&gt; New File -&gt; R Markdown to create an intial rmarkdown template. Rstudio will ask you to choose an output format, and to add a title and author, for now we will just use the default HTML format however this can be changed at any time within the Rmarkdown template. Go ahead and select okay when you have added your name and a title. Rstudio should now have made a template for us, let’s go over a few introductory topics related to this template. At the top of the file you will see what looks like a YAML header denoted by ---. This is where the defaults for building the file are set. You will notice that R Markdown has pre-populated some fields based on what we supplied when we initalized the markdown template. You can output the R Markdown (.Rmd) document using the Knit button in the top left hand corner of RStudio. This is the same as calling the function render() which takes the path to the R Markdown file as input. This file should end in a .Rmd extension to denote it as an rmarkdown file, though Rstudio will take care of this for you the first time you hit Knit. Rstudio also has a convenient way to insert code using the insert button to the right. You might notice that not only does rmarkdown support R, but also bash, python and a few other languages as well. Though in order to work, these languages will need to be installed before using Knit. Go ahead and hit the Knit button just to see what an R Markdown output looks like with the default example text. If you are working with the default HTML option the result will load in a new RStudio window with the option to open it in your usual web browser. Note: you use “include=FALSE to have the chunk evaluated, but neither the code nor its output displayed. Creating a report Now that we’ve gone over the basics of R Markdown let’s create a real (but simple) report. First, you’ll need to download the Folicular Lymphoma data set we used in the previous ggplot2 section. Go ahead and download that dataset from http://www.genomedata.org/gen-viz-workshop/intro_to_ggplot2/ggplot2ExampleData.tsv if you don’t have it. R Markdown documents combine text and code, the text portion of these documents use a lightweight text markup language known as markdown. This allows text to be displayed in a stylistic way on the web without having to write HTML or other web code. We won’t go over all of markdowns features however it will be good to familiarize yourself with this style. A cheatsheet for the markdown flavor that R Markdown uses can be found by going to help -&gt; Cheatsheets -&gt; R Markdown Cheatsheets. As we have mentioned you can insert a code chunk using the insert button on the top right. For example as shown below when selecting insert -&gt; R, we get a code chunk formatted for R. However you can also add parameters to this code chunk to alter it’s default behavior. A full list of these parameters is available here. Exercises We have created a preliminary rmarkdown file you can download here. Fill in this document to make it more complete, and then knit it together. The steps you should follow are outlined right in this R Markdown document. You can open this file in RStudio by going to File -&gt; Open File. An R Markdown reference is available here. Get a hint! Look at the R Markdown reference guide mentioned above or the cheatsheets in Rstudio. Answer Here is a more complete .Rmd file.]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://www.genviz.org//assets/DNA.jpg" /><media:content medium="image" url="http://www.genviz.org//assets/DNA.jpg" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Introduction to shiny</title><link href="http://www.genviz.org//module-07-appendix/0007/02/01/introToshiny/" rel="alternate" type="text/html" title="Introduction to shiny" /><published>0007-02-01T00:00:00+00:00</published><updated>0007-02-01T00:00:00+00:00</updated><id>http://www.genviz.org//module-07-appendix/0007/02/01/introToshiny</id><content type="html" xml:base="http://www.genviz.org//module-07-appendix/0007/02/01/introToshiny/"><![CDATA[<p>Interactive graphics is an emerging area within R. There are many libraries available to make interactive visualizations, however most of these libraries are still quite new. In this sub-module we will give a brief overview of <a href="https://shiny.rstudio.com/">shiny</a>, a web application framework within R for building interactive web pages. Using shiny we will build a simple application to display our data using reactive data sets and ggplot.</p>

<h3 id="install-shiny">Install shiny</h3>

<p>The <a href="https://shiny.rstudio.com/">shiny</a> package is available on cran and is fairly easy to install using <a href="https://www.rdocumentation.org/packages/utils/versions/3.4.1/topics/install.packages">install.packages()</a>. Go ahead and install and load the package. The package comes with 11 example apps that can be viewed using the <a href="https://www.rdocumentation.org/packages/shiny/versions/1.0.3/topics/runExample">runExample()</a> function, we will be building our own app from scratch, but feel free to try out a few of these examples to get a feel for what shiny can do. Shiny also provides a nice <a href="https://shiny.rstudio.com/gallery/">gallery of example applications</a> and even a genomics example plotting <a href="https://shiny.rstudio.com/gallery/genome-browser.html">cancer genomics data in a circos-style application</a>.</p>
<div class="language-R highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># install and load shiny</span><span class="w">
</span><span class="n">install.packages</span><span class="p">(</span><span class="s2">"shiny"</span><span class="p">)</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">shiny</span><span class="p">)</span><span class="w">

</span><span class="c1"># list the built in shiny app examples</span><span class="w">
</span><span class="n">runExample</span><span class="p">()</span><span class="w">

</span><span class="c1"># run one of these examples in Rstudio</span><span class="w">
</span><span class="n">runExample</span><span class="p">(</span><span class="s2">"06_tabsets"</span><span class="p">)</span><span class="w">
</span></code></pre></div></div>

<p>What shiny is actually doing here is converting the <a href="https://www.r-project.org/">R</a> code to <a href="https://en.wikipedia.org/wiki/HTML">html pages</a> and serving those on a random port using the ip address 127.0.0.1 which is <a href="https://en.wikipedia.org/wiki/Localhost">localhost</a> on most computers. In simplified terms these <a href="https://en.wikipedia.org/wiki/HTML">html pages</a> are simply being hosted by your own computer. If you are in Rstudio your web application should have been opened automatically, however you can also view these with any modern web browser by going to the web address listed after calling <a href="https://www.rdocumentation.org/packages/shiny/versions/1.0.3/topics/runExample">runExample()</a>. It should look something like this: http://127.0.0.1:4379.</p>

<p>After checking it out, use the escape key to stop the shiny app.</p>

<h3 id="structure-of-a-shiny-app">Structure of a shiny app</h3>

<p>The basic code to run any shiny app is split into two parts: the server (e.g., server.R) and user interface (e.g., ui.R). The server script is the <a href="https://en.wikipedia.org/wiki/Front_and_back_ends">back end</a> of our shiny web app and contains the instructions to build the app. The user interface script is the <a href="https://en.wikipedia.org/wiki/Front_and_back_ends">front end</a> and is essentially what a user views and interacts with. Both of these files should be in the same directory for the app to work properly.</p>

<p>Go ahead and make a folder for our shiny app called “testApp”.</p>

<p>Next create the following two scripts there: <code class="language-plaintext highlighter-rouge">ui.R</code> and <code class="language-plaintext highlighter-rouge">server.R</code>. This is the bare minimum for a shiny app and will generate an empty web application.</p>

<ul>
  <li>Put the following in a file: <code class="language-plaintext highlighter-rouge">ui.R</code></li>
</ul>

<div class="language-R highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># load shiny library</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">shiny</span><span class="p">)</span><span class="w">

</span><span class="c1"># set up front end</span><span class="w">
</span><span class="n">shinyUI</span><span class="p">(</span><span class="n">fluidPage</span><span class="p">(</span><span class="w">
</span><span class="p">))</span><span class="w">
</span></code></pre></div></div>

<ul>
  <li>Put the following in a file: <code class="language-plaintext highlighter-rouge">server.R</code></li>
</ul>

<div class="language-R highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># load shiny library</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">shiny</span><span class="p">)</span><span class="w">

</span><span class="c1"># set up back end</span><span class="w">
</span><span class="n">shinyServer</span><span class="p">(</span><span class="k">function</span><span class="p">(</span><span class="n">input</span><span class="p">,</span><span class="w"> </span><span class="n">output</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w">
</span><span class="p">})</span><span class="w">
</span></code></pre></div></div>

<p>To view/test your app simply type the <code class="language-plaintext highlighter-rouge">runApp(port=7777)</code> command in your R/Rstudio terminal. For convenience in this tutorial, we have selected a specific port instead of letting shiny choose one randomly.</p>

<p>Make sure that your current working directory in R is set to the top level of “testApp” where you put server.R and ui.R.</p>

<p>You can use <a href="https://www.rdocumentation.org/packages/base/versions/3.4.1/topics/getwd">getwd()</a> and <a href="https://www.rdocumentation.org/packages/base/versions/3.4.1/topics/getwd">setwd()</a> to print and set this respectively.</p>

<p>Example:</p>
<div class="language-R highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">getwd</span><span class="p">()</span><span class="w">
</span><span class="n">setwd</span><span class="p">(</span><span class="s2">"/Users/mgriffit/Desktop/testApp"</span><span class="p">)</span><span class="w">
</span><span class="n">getwd</span><span class="p">()</span><span class="w">
</span><span class="n">runApp</span><span class="p">(</span><span class="n">port</span><span class="o">=</span><span class="m">7777</span><span class="p">)</span><span class="w">
</span></code></pre></div></div>

<p>If successful, Rstudio will display a new window with your application running. Alternatively you can view your app in a web browser at <a href="http://127.0.0.1:7777">http://127.0.0.1:7777</a>. So far, all you should see is an empty page.</p>

<h3 id="loading-data-into-the-shiny-back-end-server">Loading data into the shiny back end (server)</h3>
<p>Now that we’ve got a basic frame work up let’s go ahead and load some data and answer a few questions. The data we will use is supplemental table 6 from the paper <a href="https://www.ncbi.nlm.nih.gov/pubmed/27181063">“Comprehensive genomic analysis reveals FLT3 activation and a therapeutic strategy for a patient with relapsed adult B-lymphoblastic leukemia.”</a>. The data contains variant allele frequency (VAF) values from a targeted capture sequencing study of an adult AML patient with 11 samples of various cell populations and timepoints.</p>

<p>You can download the table <a href="http://genomedata.org/gen-viz-workshop/intro_to_shiny/shinyExampleData.tsv">here</a>.
For simplicity, make a “data” directory in your app and place the data file there.</p>

<p>We can load this data into shiny as you would any other data in R. Just be sure to do this in the server.R script and place the code within the unamed function. Add the following to your server.R script to make the data available within the shiny server.</p>
<ul>
  <li>server.R</li>
</ul>

<div class="language-R highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># load shiny library</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">shiny</span><span class="p">)</span><span class="w">

</span><span class="c1"># set up back end</span><span class="w">
</span><span class="n">shinyServer</span><span class="p">(</span><span class="k">function</span><span class="p">(</span><span class="n">input</span><span class="p">,</span><span class="w"> </span><span class="n">output</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w">
    </span><span class="c1"># load the data</span><span class="w">
    </span><span class="n">amlData</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">read.delim</span><span class="p">(</span><span class="s2">"data/shinyExampleData.tsv"</span><span class="p">)</span><span class="w">
</span><span class="p">})</span><span class="w">
</span></code></pre></div></div>

<h3 id="sending-output-to-the-shiny-front-end-ui">Sending output to the shiny front end (UI)</h3>
<p>Now that we have data let’s make a quick plot showing the distribution of VAF for the normal skin sample (Skin_d42_I_vaf) in comparison to the initial tumor marrow core sample (MC_d0_clot_A_vaf) and send it to the app’s user interface.</p>

<p>We’ll need to first create the plot on the back end (i.e. server.R). We can use any graphics library for this, but here we use <a href="http://ggplot2.tidyverse.org/reference/">ggplot2</a>.</p>

<p>In order to be compatible with the shiny UI we call a <em>Render</em> function, in this case <a href="https://www.rdocumentation.org/packages/shiny/versions/1.0.3/topics/renderPlot">renderPlot()</a> which takes an expression (i.e. set of instructions) and produces a plot. The curly braces in <a href="https://www.rdocumentation.org/packages/shiny/versions/1.0.3/topics/renderPlot">renderPlot()</a> just contain the expression used to create the plot and are useful if the expression takes up more than one line. The <a href="https://www.rdocumentation.org/packages/shiny/versions/1.0.3/topics/renderPlot">renderPlot()</a> will do some minimal pre-processing of the object returned in the expression and store it to the list-like “output” object.</p>

<p>Notice that in the ui.R file we have added a <a href="https://www.rdocumentation.org/packages/shiny/versions/1.0.3/topics/mainPanel">mainPanel()</a> which, as it sounds, is instructing the app to create a main panel on the user interface. Now that we have somewhere to display our plot we can link what was created on the back end to the front end. This is done with the <em>Output</em> family of functions, in this case our output is a plot generated by <a href="https://www.rdocumentation.org/packages/shiny/versions/1.0.3/topics/renderPlot">renderPlot()</a> and is stored in the list like output object as “scatterplot” created in the server.R file.</p>

<p>We use <a href="https://www.rdocumentation.org/packages/shiny/versions/1.0.3/topics/plotOutput">plotOutput()</a> to provide this link to the front end and give the output ID, which is just the name of the object stored in the output-like list.</p>

<p>Note that when providing this link the type of object created with a <em>Render</em> function must correspond to the <em>Output</em> function, in this example we use <a href="https://www.rdocumentation.org/packages/shiny/versions/1.0.3/topics/renderPlot">renderPlot()</a> and <a href="https://www.rdocumentation.org/packages/shiny/versions/1.0.3/topics/plotOutput">plotOutput()</a> but other functions exist for other data types such as <a href="https://www.rdocumentation.org/packages/shiny/versions/1.0.3/topics/renderText">renderText()</a> and <a href="https://www.rdocumentation.org/packages/shiny/versions/1.0.3/topics/textOutput">textOuput()</a>.</p>

<ul>
  <li>ui.R</li>
</ul>

<div class="language-R highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># load shiny library</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">shiny</span><span class="p">)</span><span class="w">

</span><span class="c1"># set up front end</span><span class="w">
</span><span class="n">shinyUI</span><span class="p">(</span><span class="n">fluidPage</span><span class="p">(</span><span class="w">
    </span><span class="n">mainPanel</span><span class="p">(</span><span class="n">plotOutput</span><span class="p">(</span><span class="s2">"scatterPlot"</span><span class="p">))</span><span class="w">
</span><span class="p">))</span><span class="w">
</span></code></pre></div></div>

<ul>
  <li>server.R</li>
</ul>

<div class="language-R highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># load shiny library</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">shiny</span><span class="p">)</span><span class="w">

</span><span class="c1"># set up back end</span><span class="w">
</span><span class="n">shinyServer</span><span class="p">(</span><span class="k">function</span><span class="p">(</span><span class="n">input</span><span class="p">,</span><span class="w"> </span><span class="n">output</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w">
    </span><span class="c1"># load the data</span><span class="w">
    </span><span class="n">amlData</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">read.delim</span><span class="p">(</span><span class="s2">"data/shinyExampleData.tsv"</span><span class="p">)</span><span class="w">

    </span><span class="c1"># construct a plot to show the data</span><span class="w">
    </span><span class="n">library</span><span class="p">(</span><span class="n">ggplot2</span><span class="p">)</span><span class="w">
    </span><span class="n">output</span><span class="o">$</span><span class="n">scatterPlot</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">renderPlot</span><span class="p">({</span><span class="w">
        </span><span class="n">p1</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">ggplot</span><span class="p">(</span><span class="n">amlData</span><span class="p">,</span><span class="w"> </span><span class="n">aes</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="n">Skin_d42_I_vaf</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="o">=</span><span class="n">MC_d0_clot_A_vaf</span><span class="p">))</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">geom_point</span><span class="p">()</span><span class="w">
        </span><span class="n">p1</span><span class="w">
    </span><span class="p">})</span><span class="w">
</span><span class="p">})</span><span class="w">
</span></code></pre></div></div>

<p>Once again, to view/test your app simply type the <code class="language-plaintext highlighter-rouge">runApp(port=7777)</code> command in your R/Rstudio terminal and go to <a href="http://127.0.0.1:7777">http://127.0.0.1:7777</a>.</p>

<p>This should happen automatically from Rstudio. If your previous app is still running you may need to stop and restart it and/or refresh your browser. You should now see a ggplot graphic in your browser (see below). But, so far, nothing is interactive about this plot. We will allow some basic user input and interactivity in the next section.</p>

<figure class="figure" width="1000">
    <img class="image " src="/assets/shiny/shiny_simple_output.png" width="1000" />
    
    
</figure>

<p>Now using what we’ve learned so far try to add some text to are web app by passing it from the back end to the front end.</p>

<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>

<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>

<script type="text/javascript" src="/assets/scripts/question.js"></script>

<div class="accordion">
    <p class="q">Get a hint!</p>
    <p class="a">Look at the help for textInput <a href="https://www.rdocumentation.org/packages/shiny/versions/1.2.0/topics/textOutput">textOutput()</a> and renderText <a href="https://www.rdocumentation.org/packages/shiny/versions/1.2.0/topics/renderText">renderText()</a></p>
</div>

<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>

<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>

<script type="text/javascript" src="/assets/scripts/question.js"></script>

<div class="accordion">
    <p class="q">Solution</p>
    <p class="a">These files contain a correct answer: <a href="http://genviz.org/assets/shiny/exercise0/ui.R">ui.R</a>, <a href="http://genviz.org/assets/shiny/exercise0/server.R">server.R</a></p>
</div>

<p>When you’ve completed the above exercise try and answer a few of the questions below.</p>

<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>

<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>

<script type="text/javascript" src="/assets/scripts/question.js"></script>

<div class="accordion">
    <p class="q">Why would you want to pass text from the backend to the frontend as opposed to just rendering it in the front end</p>
    <p class="a">By passing the text from the backend we have the ability to make the text reactive, i.e. it could change based on what the web app is displaying.</p>
</div>

<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>

<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>

<script type="text/javascript" src="/assets/scripts/question.js"></script>

<div class="accordion">
    <p class="q">If you did not care if the text was reactive what could you do?, try adding some text by only modifying the ui.R file.</p>
    <p class="a">You could simply use any of the html builder functions present in the shiny package, one that would work is <a href="https://www.rdocumentation.org/packages/shiny/versions/1.2.0/topics/builder">p()</a></p>
</div>

<h3 id="sending-input-from-the-front-end">Sending input from the front end</h3>
<p>Now that we know how to link output from the back end to the front end, let’s do the opposite and link user input from the front end to the back end. Essentially this is giving the user control to manipulate user interface objects. Specifically let’s allow the user to choose which sample Variant Allele Fraction (VAF) columns in the data set to plot on the x and y axis of our scatter plot.</p>

<p>Let’s start with the ui.R file. Below, we have added the <a href="https://www.rdocumentation.org/packages/shiny/versions/1.0.3/topics/sidebarLayout">sidebarLayout()</a> schema which will create a layout with a side bar and a main panel. Within this layout we define a <a href="https://www.rdocumentation.org/packages/shiny/versions/1.0.3/topics/sidebarPanel">sidebarPanel()</a> and a <a href="https://www.rdocumentation.org/packages/shiny/versions/1.0.3/topics/mainPanel">mainPanel()</a>. Within the <a href="https://www.rdocumentation.org/packages/shiny/versions/1.0.3/topics/sidebarPanel">sidebarPanel()</a> we define two drop down selectors with <a href="https://www.rdocumentation.org/packages/shiny/versions/1.0.3/topics/selectInput">selectInput()</a>.</p>

<p>Importantly, within these functions we assign an <em>inputId</em> which is what will be passed to the back end. On the back end side (server.R) we’ve already talked about output within the unnamed function, a second argument exists called “input”. This is the argument used to communicate from the front end to the back end and in our case it holds the information passed from each <a href="[selectInput()](https://www.rdocumentation.org/packages/shiny/versions/1.0.3/topics/selectInput)">selectInput()</a> call with the id’s “x_axis” and “y_axis”.</p>

<p>To make our plot reactively change based on this input we simply call up this information within the ggplot call.</p>

<p>You might have noticed that we are using <a href="http://ggplot2.tidyverse.org/reference/aes_.html">aes_string()</a> instead of <a href="http://ggplot2.tidyverse.org/reference/aes_.html">aes()</a>. This is only necessary because “input$x_axis” and “input$y_axis” are passed as strings and as such we need to let ggplot know this so the non-standard evalutation typically used with <a href="http://ggplot2.tidyverse.org/reference/aes_.html">aes()</a> is not performed.</p>

<ul>
  <li>ui.R</li>
</ul>

<div class="language-R highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">#load shiny library</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">shiny</span><span class="p">)</span><span class="w">

</span><span class="c1"># define the vaf column names</span><span class="w">
</span><span class="n">axis_options</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="s2">"Skin_d42_I_vaf"</span><span class="p">,</span><span class="w"> </span><span class="s2">"MC_d0_clot_A_vaf"</span><span class="p">,</span><span class="w"> </span><span class="s2">"MC_d0_slide_A_vaf"</span><span class="p">,</span><span class="w"> </span><span class="s2">"BM_d42_I_vaf"</span><span class="p">,</span><span class="w">
                  </span><span class="s2">"M_d1893_A_vaf"</span><span class="p">,</span><span class="w"> </span><span class="s2">"M_d3068_A_vaf"</span><span class="p">,</span><span class="w"> </span><span class="s2">"SB_d3072_A_rna_vaf"</span><span class="p">,</span><span class="w"> </span><span class="s2">"SB_d3072_A_vaf"</span><span class="p">,</span><span class="w">
                  </span><span class="s2">"BM_d3072_A_vaf"</span><span class="p">,</span><span class="w"> </span><span class="s2">"SL_d3072_I_vaf"</span><span class="p">,</span><span class="w"> </span><span class="s2">"MC_d3107_A_vaf"</span><span class="p">,</span><span class="w"> </span><span class="s2">"BM_d3137_I_vaf"</span><span class="p">,</span><span class="w">
                  </span><span class="s2">"M_d3219_I_vaf"</span><span class="p">,</span><span class="w"> </span><span class="s2">"BM_d4024_I_vaf"</span><span class="p">)</span><span class="w">

</span><span class="c1"># set up front end</span><span class="w">
</span><span class="n">shinyUI</span><span class="p">(</span><span class="n">fluidPage</span><span class="p">(</span><span class="w">

  </span><span class="c1"># set up the UI layout with a side and main panel</span><span class="w">
  </span><span class="n">sidebarLayout</span><span class="p">(</span><span class="w">

    </span><span class="c1"># set the side panel to allow for user input</span><span class="w">
    </span><span class="n">sidebarPanel</span><span class="p">(</span><span class="w">
      </span><span class="n">selectInput</span><span class="p">(</span><span class="n">inputId</span><span class="o">=</span><span class="s2">"x_axis"</span><span class="p">,</span><span class="w"> </span><span class="n">label</span><span class="o">=</span><span class="s2">"x axis"</span><span class="p">,</span><span class="w"> </span><span class="n">choices</span><span class="o">=</span><span class="n">axis_options</span><span class="p">,</span><span class="w"> </span><span class="n">selected</span><span class="o">=</span><span class="s2">"Skin_d42_I_vaf"</span><span class="p">),</span><span class="w">
      </span><span class="n">selectInput</span><span class="p">(</span><span class="n">inputId</span><span class="o">=</span><span class="s2">"y_axis"</span><span class="p">,</span><span class="w"> </span><span class="n">label</span><span class="o">=</span><span class="s2">"y axis"</span><span class="p">,</span><span class="w"> </span><span class="n">choices</span><span class="o">=</span><span class="n">axis_options</span><span class="p">,</span><span class="w"> </span><span class="n">selected</span><span class="o">=</span><span class="s2">"MC_d0_clot_A_vaf"</span><span class="p">)</span><span class="w">
    </span><span class="p">),</span><span class="w">

    </span><span class="c1"># set the plot panel</span><span class="w">
    </span><span class="n">mainPanel</span><span class="p">(</span><span class="w">
      </span><span class="n">plotOutput</span><span class="p">(</span><span class="s2">"scatterPlot"</span><span class="p">)</span><span class="w">
    </span><span class="p">)</span><span class="w">
  </span><span class="p">)</span><span class="w">
</span><span class="p">))</span><span class="w">
</span></code></pre></div></div>

<ul>
  <li>server.R</li>
</ul>

<div class="language-R highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># load shiny library</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">shiny</span><span class="p">)</span><span class="w">

</span><span class="c1"># set up back end</span><span class="w">
</span><span class="n">shinyServer</span><span class="p">(</span><span class="k">function</span><span class="p">(</span><span class="n">input</span><span class="p">,</span><span class="w"> </span><span class="n">output</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w">
  </span><span class="c1"># load the data</span><span class="w">
  </span><span class="n">amlData</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">read.delim</span><span class="p">(</span><span class="s2">"data/shinyExampleData.tsv"</span><span class="p">)</span><span class="w">

  </span><span class="c1"># construct a plot to show the data</span><span class="w">
  </span><span class="n">library</span><span class="p">(</span><span class="n">ggplot2</span><span class="p">)</span><span class="w">
  </span><span class="n">output</span><span class="o">$</span><span class="n">scatterPlot</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">renderPlot</span><span class="p">({</span><span class="w">
    </span><span class="n">p1</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">ggplot</span><span class="p">(</span><span class="n">amlData</span><span class="p">,</span><span class="w"> </span><span class="n">aes_string</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="n">input</span><span class="o">$</span><span class="n">x_axis</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="o">=</span><span class="n">input</span><span class="o">$</span><span class="n">y_axis</span><span class="p">))</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">geom_point</span><span class="p">()</span><span class="w">
    </span><span class="n">p1</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">p1</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">xlab</span><span class="p">(</span><span class="s2">"Variant Allele Fraction"</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">ylab</span><span class="p">(</span><span class="s2">"Variant Allele Fraction"</span><span class="p">)</span><span class="w">
    </span><span class="n">p1</span><span class="w">
  </span><span class="p">})</span><span class="w">
</span><span class="p">})</span><span class="w">
</span></code></pre></div></div>

<p>Once again, to view/test your app simply type the <code class="language-plaintext highlighter-rouge">runApp(port=7777)</code> command in your R/Rstudio terminal and go to <a href="http://127.0.0.1:7777">http://127.0.0.1:7777</a>. This should happen automatically from Rstudio. If your previous app is still running you may need to stop and restart it and/or simply refresh your browser. You should now see a ggplot scatterplot graphic in your browser (see below) as before. But, now you should also see user-activated drop-down menus that allow you to select which data to plot and visualize. You have created your first interative shiny application!</p>

<figure class="figure" width="1000">
    <img class="image " src="/assets/shiny/shiny_interactive_output.png" width="1000" />
    
    
</figure>

<h3 id="exercises">Exercises</h3>
<p>We have given a very quick overview of <a href="https://shiny.rstudio.com/">shiny</a>, and have really only scraped the surface of what <a href="https://shiny.rstudio.com/">shiny</a> can be used for. Using the knowledge we have already learned however let’s try modifying our existing shiny app.</p>

<p>Right now the plot looks fairly bland. Try adding the ability for the user to enter a column name as text to color points by. For example, try coloring by the column names “Class” or “Clonal.Assignment”. Use your existing ui.R and server.R files as a starting point. If successful, you should be able to restart/refresh your shiny app and see something like the following:</p>

<figure class="figure" width="1000">
    <img class="image " src="/assets/shiny/shiny_exercise_output.png" width="1000" />
    
    
</figure>

<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>

<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>

<script type="text/javascript" src="/assets/scripts/question.js"></script>

<div class="accordion">
    <p class="q">Get a hint!</p>
    <p class="a">You will want to use <a href="https://www.rdocumentation.org/packages/shinybootstrap2/versions/0.2.1/topics/textInput">textInput()</a> within the ui.R file for this and then link the input to the ggplot call.</p>
</div>

<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>

<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>

<script type="text/javascript" src="/assets/scripts/question.js"></script>

<div class="accordion">
    <p class="q">Solution</p>
    <p class="a">These files contain the correct answer: <a href="http://genviz.org/assets/shiny/exercise1/ui.R">ui.R</a>, <a href="http://genviz.org/assets/shiny/exercise1/server.R">server.R</a></p>
</div>

<h3 id="hosting-your-shiny-app-on-the-web">Hosting your shiny app on the web</h3>
<p>To make your new shiny app accessible on the web you have several options. The simplest is to just sign up for an account at <a href="http://www.shinyapps.io/">www.shinyapps.io</a>. Once you sign up shinyapps.io will walk you through the process of installing (STEP 1) and authorizing (STEP 2) the rsconnect library (see below).</p>

<figure class="figure" width="1000">
    <img class="image " src="/assets/shiny/shinyapps_io_setup.png" width="1000" />
    
    
</figure>

<p>If set up correctly you will be able to deploy your app (STEP3) with:</p>

<div class="language-R highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">library</span><span class="p">(</span><span class="n">rsconnect</span><span class="p">)</span><span class="w">
</span><span class="n">rsconnect</span><span class="o">::</span><span class="n">deployApp</span><span class="p">(</span><span class="s1">'path/to/your/app'</span><span class="p">)</span><span class="w">
</span></code></pre></div></div>

<p>Alternatively, simply select the ‘Publish’ button in the top-right of a running Shiny App from Rstudio (see below).</p>

<figure class="figure" width="1000">
    <img class="image " src="/assets/shiny/shiny_exercise_output_rstudio.png" width="1000" />
    
    
</figure>

<p>Either process should create an app at https://[your_account].shinyapps.io/[yourApp]/ using the name for the account you created at shinyapps.io and the name you set for your App during the publication process. However, the free shinyapps.io account is limited to 5 applications and 25 active hours of runtime (any time your application is not idle). Upgrading to a pay account will increase the allowed numbers of applications, active hours, and add options for authentication.</p>

<p>For a longer-term, do-it-yourself, possibly cheaper solution, you will need a web server with the separate <a href="https://www.rstudio.com/products/shiny/download-server/">Shiny Server Open Source</a> software running on it, along with with your Shiny App. There are many ways you could set this up. One option would be to do something like the following: (1) Start an Ubuntu linux Amazon AWS instance; (2) Login to your AWS linux box; (3) Install R, the shiny R library, and any other R libraries that your shiny app needs (e.g., ggplot2, rmarkdown, etc); (4) Install and start the shiny-server; (5) Copy your shiny application files (R and Rda) files to the shiny-server folder on your linux server. (6) In a browser, navigate to the public IP address of the linux server. Detailed instructions are available on this <a href="http://www.kimberlycoffey.com/blog/2016/2/13/mlz90wjw0k76446xkg262prvjp0l8u">blog post</a>. Unfortunately, for authentication (password protection support) you will need to upgrade to the pay version - Shiny Server Pro.</p>]]></content><author><name>Zachary Skidmore</name></author><category term="Module-07-Appendix" /><summary type="html"><![CDATA[Interactive graphics is an emerging area within R. There are many libraries available to make interactive visualizations, however most of these libraries are still quite new. In this sub-module we will give a brief overview of shiny, a web application framework within R for building interactive web pages. Using shiny we will build a simple application to display our data using reactive data sets and ggplot. Install shiny The shiny package is available on cran and is fairly easy to install using install.packages(). Go ahead and install and load the package. The package comes with 11 example apps that can be viewed using the runExample() function, we will be building our own app from scratch, but feel free to try out a few of these examples to get a feel for what shiny can do. Shiny also provides a nice gallery of example applications and even a genomics example plotting cancer genomics data in a circos-style application. # install and load shiny install.packages("shiny") library(shiny) # list the built in shiny app examples runExample() # run one of these examples in Rstudio runExample("06_tabsets") What shiny is actually doing here is converting the R code to html pages and serving those on a random port using the ip address 127.0.0.1 which is localhost on most computers. In simplified terms these html pages are simply being hosted by your own computer. If you are in Rstudio your web application should have been opened automatically, however you can also view these with any modern web browser by going to the web address listed after calling runExample(). It should look something like this: http://127.0.0.1:4379. After checking it out, use the escape key to stop the shiny app. Structure of a shiny app The basic code to run any shiny app is split into two parts: the server (e.g., server.R) and user interface (e.g., ui.R). The server script is the back end of our shiny web app and contains the instructions to build the app. The user interface script is the front end and is essentially what a user views and interacts with. Both of these files should be in the same directory for the app to work properly. Go ahead and make a folder for our shiny app called “testApp”. Next create the following two scripts there: ui.R and server.R. This is the bare minimum for a shiny app and will generate an empty web application. Put the following in a file: ui.R # load shiny library library(shiny) # set up front end shinyUI(fluidPage( )) Put the following in a file: server.R # load shiny library library(shiny) # set up back end shinyServer(function(input, output) { }) To view/test your app simply type the runApp(port=7777) command in your R/Rstudio terminal. For convenience in this tutorial, we have selected a specific port instead of letting shiny choose one randomly. Make sure that your current working directory in R is set to the top level of “testApp” where you put server.R and ui.R. You can use getwd() and setwd() to print and set this respectively. Example: getwd() setwd("/Users/mgriffit/Desktop/testApp") getwd() runApp(port=7777) If successful, Rstudio will display a new window with your application running. Alternatively you can view your app in a web browser at http://127.0.0.1:7777. So far, all you should see is an empty page. Loading data into the shiny back end (server) Now that we’ve got a basic frame work up let’s go ahead and load some data and answer a few questions. The data we will use is supplemental table 6 from the paper “Comprehensive genomic analysis reveals FLT3 activation and a therapeutic strategy for a patient with relapsed adult B-lymphoblastic leukemia.”. The data contains variant allele frequency (VAF) values from a targeted capture sequencing study of an adult AML patient with 11 samples of various cell populations and timepoints. You can download the table here. For simplicity, make a “data” directory in your app and place the data file there. We can load this data into shiny as you would any other data in R. Just be sure to do this in the server.R script and place the code within the unamed function. Add the following to your server.R script to make the data available within the shiny server. server.R # load shiny library library(shiny) # set up back end shinyServer(function(input, output) { # load the data amlData &lt;- read.delim("data/shinyExampleData.tsv") }) Sending output to the shiny front end (UI) Now that we have data let’s make a quick plot showing the distribution of VAF for the normal skin sample (Skin_d42_I_vaf) in comparison to the initial tumor marrow core sample (MC_d0_clot_A_vaf) and send it to the app’s user interface. We’ll need to first create the plot on the back end (i.e. server.R). We can use any graphics library for this, but here we use ggplot2. In order to be compatible with the shiny UI we call a Render function, in this case renderPlot() which takes an expression (i.e. set of instructions) and produces a plot. The curly braces in renderPlot() just contain the expression used to create the plot and are useful if the expression takes up more than one line. The renderPlot() will do some minimal pre-processing of the object returned in the expression and store it to the list-like “output” object. Notice that in the ui.R file we have added a mainPanel() which, as it sounds, is instructing the app to create a main panel on the user interface. Now that we have somewhere to display our plot we can link what was created on the back end to the front end. This is done with the Output family of functions, in this case our output is a plot generated by renderPlot() and is stored in the list like output object as “scatterplot” created in the server.R file. We use plotOutput() to provide this link to the front end and give the output ID, which is just the name of the object stored in the output-like list. Note that when providing this link the type of object created with a Render function must correspond to the Output function, in this example we use renderPlot() and plotOutput() but other functions exist for other data types such as renderText() and textOuput(). ui.R # load shiny library library(shiny) # set up front end shinyUI(fluidPage( mainPanel(plotOutput("scatterPlot")) )) server.R # load shiny library library(shiny) # set up back end shinyServer(function(input, output) { # load the data amlData &lt;- read.delim("data/shinyExampleData.tsv") # construct a plot to show the data library(ggplot2) output$scatterPlot &lt;- renderPlot({ p1 &lt;- ggplot(amlData, aes(x=Skin_d42_I_vaf, y=MC_d0_clot_A_vaf)) + geom_point() p1 }) }) Once again, to view/test your app simply type the runApp(port=7777) command in your R/Rstudio terminal and go to http://127.0.0.1:7777. This should happen automatically from Rstudio. If your previous app is still running you may need to stop and restart it and/or refresh your browser. You should now see a ggplot graphic in your browser (see below). But, so far, nothing is interactive about this plot. We will allow some basic user input and interactivity in the next section. Now using what we’ve learned so far try to add some text to are web app by passing it from the back end to the front end. Get a hint! Look at the help for textInput textOutput() and renderText renderText() Solution These files contain a correct answer: ui.R, server.R When you’ve completed the above exercise try and answer a few of the questions below. Why would you want to pass text from the backend to the frontend as opposed to just rendering it in the front end By passing the text from the backend we have the ability to make the text reactive, i.e. it could change based on what the web app is displaying. If you did not care if the text was reactive what could you do?, try adding some text by only modifying the ui.R file. You could simply use any of the html builder functions present in the shiny package, one that would work is p() Sending input from the front end Now that we know how to link output from the back end to the front end, let’s do the opposite and link user input from the front end to the back end. Essentially this is giving the user control to manipulate user interface objects. Specifically let’s allow the user to choose which sample Variant Allele Fraction (VAF) columns in the data set to plot on the x and y axis of our scatter plot. Let’s start with the ui.R file. Below, we have added the sidebarLayout() schema which will create a layout with a side bar and a main panel. Within this layout we define a sidebarPanel() and a mainPanel(). Within the sidebarPanel() we define two drop down selectors with selectInput(). Importantly, within these functions we assign an inputId which is what will be passed to the back end. On the back end side (server.R) we’ve already talked about output within the unnamed function, a second argument exists called “input”. This is the argument used to communicate from the front end to the back end and in our case it holds the information passed from each selectInput() call with the id’s “x_axis” and “y_axis”. To make our plot reactively change based on this input we simply call up this information within the ggplot call. You might have noticed that we are using aes_string() instead of aes(). This is only necessary because “input$x_axis” and “input$y_axis” are passed as strings and as such we need to let ggplot know this so the non-standard evalutation typically used with aes() is not performed. ui.R #load shiny library library(shiny) # define the vaf column names axis_options &lt;- c("Skin_d42_I_vaf", "MC_d0_clot_A_vaf", "MC_d0_slide_A_vaf", "BM_d42_I_vaf", "M_d1893_A_vaf", "M_d3068_A_vaf", "SB_d3072_A_rna_vaf", "SB_d3072_A_vaf", "BM_d3072_A_vaf", "SL_d3072_I_vaf", "MC_d3107_A_vaf", "BM_d3137_I_vaf", "M_d3219_I_vaf", "BM_d4024_I_vaf") # set up front end shinyUI(fluidPage( # set up the UI layout with a side and main panel sidebarLayout( # set the side panel to allow for user input sidebarPanel( selectInput(inputId="x_axis", label="x axis", choices=axis_options, selected="Skin_d42_I_vaf"), selectInput(inputId="y_axis", label="y axis", choices=axis_options, selected="MC_d0_clot_A_vaf") ), # set the plot panel mainPanel( plotOutput("scatterPlot") ) ) )) server.R # load shiny library library(shiny) # set up back end shinyServer(function(input, output) { # load the data amlData &lt;- read.delim("data/shinyExampleData.tsv") # construct a plot to show the data library(ggplot2) output$scatterPlot &lt;- renderPlot({ p1 &lt;- ggplot(amlData, aes_string(x=input$x_axis, y=input$y_axis)) + geom_point() p1 &lt;- p1 + xlab("Variant Allele Fraction") + ylab("Variant Allele Fraction") p1 }) }) Once again, to view/test your app simply type the runApp(port=7777) command in your R/Rstudio terminal and go to http://127.0.0.1:7777. This should happen automatically from Rstudio. If your previous app is still running you may need to stop and restart it and/or simply refresh your browser. You should now see a ggplot scatterplot graphic in your browser (see below) as before. But, now you should also see user-activated drop-down menus that allow you to select which data to plot and visualize. You have created your first interative shiny application! Exercises We have given a very quick overview of shiny, and have really only scraped the surface of what shiny can be used for. Using the knowledge we have already learned however let’s try modifying our existing shiny app. Right now the plot looks fairly bland. Try adding the ability for the user to enter a column name as text to color points by. For example, try coloring by the column names “Class” or “Clonal.Assignment”. Use your existing ui.R and server.R files as a starting point. If successful, you should be able to restart/refresh your shiny app and see something like the following: Get a hint! You will want to use textInput() within the ui.R file for this and then link the input to the ggplot call. Solution These files contain the correct answer: ui.R, server.R Hosting your shiny app on the web To make your new shiny app accessible on the web you have several options. The simplest is to just sign up for an account at www.shinyapps.io. Once you sign up shinyapps.io will walk you through the process of installing (STEP 1) and authorizing (STEP 2) the rsconnect library (see below). If set up correctly you will be able to deploy your app (STEP3) with: library(rsconnect) rsconnect::deployApp('path/to/your/app') Alternatively, simply select the ‘Publish’ button in the top-right of a running Shiny App from Rstudio (see below). Either process should create an app at https://[your_account].shinyapps.io/[yourApp]/ using the name for the account you created at shinyapps.io and the name you set for your App during the publication process. However, the free shinyapps.io account is limited to 5 applications and 25 active hours of runtime (any time your application is not idle). Upgrading to a pay account will increase the allowed numbers of applications, active hours, and add options for authentication. For a longer-term, do-it-yourself, possibly cheaper solution, you will need a web server with the separate Shiny Server Open Source software running on it, along with with your Shiny App. There are many ways you could set this up. One option would be to do something like the following: (1) Start an Ubuntu linux Amazon AWS instance; (2) Login to your AWS linux box; (3) Install R, the shiny R library, and any other R libraries that your shiny app needs (e.g., ggplot2, rmarkdown, etc); (4) Install and start the shiny-server; (5) Copy your shiny application files (R and Rda) files to the shiny-server folder on your linux server. (6) In a browser, navigate to the public IP address of the linux server. Detailed instructions are available on this blog post. Unfortunately, for authentication (password protection support) you will need to upgrade to the pay version - Shiny Server Pro.]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://www.genviz.org//assets/DNA.jpg" /><media:content medium="image" url="http://www.genviz.org//assets/DNA.jpg" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Advanced ggplot2</title><link href="http://www.genviz.org//module-07-appendix/0007/01/01/advancedggplot2/" rel="alternate" type="text/html" title="Advanced ggplot2" /><published>0007-01-01T00:00:00+00:00</published><updated>0007-01-01T00:00:00+00:00</updated><id>http://www.genviz.org//module-07-appendix/0007/01/01/advancedggplot2</id><content type="html" xml:base="http://www.genviz.org//module-07-appendix/0007/01/01/advancedggplot2/"><![CDATA[<p>This appendix is a continuation of the <a href="https://genviz.org/module-02-r/0002/03/02/arrangingPlots/">arrangingPlots</a>, here we go over some advanced concepts in terms of aligning plot eements and manipulating grob objects. Some of the objects we’ll be working with come from the previously mentioned section so <strong>make sure you have that code run!</strong></p>

<h4 id="aligning-plots-part-1">Aligning plots part 1</h4>

<p>Our plot from the arrangingPlots section is looking pretty good, you might notice an unfortunate issue however in that the boxplots don’t align with their respective barcharts. Don’t worry it’s fairly easy to fix in this case, however before we start we need to go down a rabbit hole and obtain a basic understanding of grobs, tableGrobs and viewports.</p>

<p>First off a grob is just short for “grid graphical object” from the low-level graphics package grid; Think of it as a set of instructions for create a graphical object (i.e. a plot). The graphics library underneath all of ggplot2’s graphical elements are really composed of grob’s because ggplot2 uses grid underneath. A TableGrob is a class from the gtable package and provides an easier way to view and manipulate groups of grobs, it is actually the intermediary between ggplot2 and grid. A “viewport” is a graphics region for which describes where a grob or group of grobs is assigned on a graphics device. When we have been calling grid.arrange in our previous examples what we are really doing is arranging viewports which contain groups of grobs.</p>

<p>To Illustrate grobs and viewports a bit further let’s convert our arranged plot to a grob and take a look at it.</p>

<div class="language-R highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">grob</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">arrangeGrob</span><span class="p">(</span><span class="n">p1</span><span class="p">,</span><span class="w"> </span><span class="n">p4</span><span class="p">,</span><span class="w"> </span><span class="n">p5</span><span class="p">,</span><span class="w"> </span><span class="n">p2</span><span class="p">,</span><span class="w"> </span><span class="n">p3</span><span class="p">,</span><span class="w"> </span><span class="n">layout_matrix</span><span class="o">=</span><span class="n">layout</span><span class="p">)</span><span class="w">
</span><span class="n">grob</span><span class="w">
</span></code></pre></div></div>

<p>you’ll notice a couple things right away, the table grob is composed of 5 individual grobs and are arranged in a 3 row, 2 column layout. The z column denotes the order in which grobs are plotted. the cells column is telling us where the grob is located within the viewport. For example the first element has a value of (1-1,1-2). This is telling us that that grob spans from from rows 1 to 1 (1-1) on the viewport and columns 1 to 2 (1-2) on the viewport. This is a bit easier to illustrate by viewing the actual layout with gtable_show_layout().</p>

<div class="language-R highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">gtable_show_layout</span><span class="p">(</span><span class="n">grob</span><span class="p">)</span><span class="w">
</span></code></pre></div></div>

<p>After running the command above you should see something like the figure below, (note that i’ve taken the liberty of overalying the output ontop of the original plot). Notice how the first grob is spanning rows 1-1 and columns 1-2.</p>

<figure class="figure" width="750">
    <img class="image " src="/assets/advanced_ggplot/arrangedPlot.2.png" width="750" />
    
    
</figure>

<p>We can verify that this is correct by drawing just the first grob.</p>

<div class="language-R highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">grid.draw</span><span class="p">(</span><span class="n">grob</span><span class="o">$</span><span class="n">grobs</span><span class="p">[[</span><span class="m">1</span><span class="p">]])</span><span class="w">
</span><span class="n">dev.off</span><span class="p">()</span><span class="w">
</span></code></pre></div></div>

<p>Okay our trip down the rabbit hole is coming to an end, I’ll just mention one last thing. As eluded to already the tableGrob we looked at is just a collection of viewports and those viewports contain grobs. In the grob we looked at we were at the top level and so by default the viewport takes up the entire page. Inside this top level we saw 5 grobs each of which have their own viewports. In the above command we go a layer deeper and draw one grob which itself has viewports it’s own associated viewports for the elements of the plot (legend, axis, etc.).</p>

<p>We glossed over quite a bit of detail in our discussion of grobs, tableGrobs and viewports however I think we know enough to get our plots to align. To start we need to convert all of the plots we made in ggplot to grobs, we can do this with the ggplotGrob() function. Next each viewport in the grob at this level has an associated width, for example the axis title has a width, the axis text etc. We can access these widths within the table grob using <code class="language-plaintext highlighter-rouge">tableGrob$widths</code> which will output a vector of these widths. We can then use the <code class="language-plaintext highlighter-rouge">unit.pmax()</code> function to find the maximum width for each viewport among all of our plots. From there it’s a simple matter of manually modifying and reassinging the widths for each grob and plotting the results as before.</p>

<div class="language-R highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># convert to grobs</span><span class="w">
</span><span class="n">p2_grob</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">ggplotGrob</span><span class="p">(</span><span class="n">p2</span><span class="p">)</span><span class="w">
</span><span class="n">p3_grob</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">ggplotGrob</span><span class="p">(</span><span class="n">p3</span><span class="p">)</span><span class="w">
</span><span class="n">p4_grob</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">ggplotGrob</span><span class="p">(</span><span class="n">p4</span><span class="p">)</span><span class="w">
</span><span class="n">p5_grob</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">ggplotGrob</span><span class="p">(</span><span class="n">p5</span><span class="p">)</span><span class="w">

</span><span class="c1"># align plots</span><span class="w">
</span><span class="n">p4_grob_widths</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">p4_grob</span><span class="o">$</span><span class="n">widths</span><span class="w">
</span><span class="n">p5_grob_widths</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">p5_grob</span><span class="o">$</span><span class="n">widths</span><span class="w">
</span><span class="n">p2_grob_widths</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">p2_grob</span><span class="o">$</span><span class="n">widths</span><span class="w">
</span><span class="n">p3_grob_widths</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">p3_grob</span><span class="o">$</span><span class="n">widths</span><span class="w">

</span><span class="n">maxWidth</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">unit.pmax</span><span class="p">(</span><span class="n">p4_grob_widths</span><span class="p">,</span><span class="w"> </span><span class="n">p5_grob_widths</span><span class="p">,</span><span class="w"> </span><span class="n">p2_grob_widths</span><span class="p">,</span><span class="w"> </span><span class="n">p3_grob_widths</span><span class="p">)</span><span class="w">

</span><span class="n">p4_grob</span><span class="o">$</span><span class="n">widths</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">maxWidth</span><span class="w">
</span><span class="n">p5_grob</span><span class="o">$</span><span class="n">widths</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">maxWidth</span><span class="w">
</span><span class="n">p2_grob</span><span class="o">$</span><span class="n">widths</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">maxWidth</span><span class="w">
</span><span class="n">p3_grob</span><span class="o">$</span><span class="n">widths</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">maxWidth</span><span class="w">

</span><span class="n">layout</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">rbind</span><span class="p">(</span><span class="nf">c</span><span class="p">(</span><span class="m">1</span><span class="p">,</span><span class="w"> </span><span class="m">1</span><span class="p">),</span><span class="w">
                </span><span class="nf">c</span><span class="p">(</span><span class="m">2</span><span class="p">,</span><span class="w"> </span><span class="m">3</span><span class="p">),</span><span class="w">
                </span><span class="nf">c</span><span class="p">(</span><span class="m">4</span><span class="p">,</span><span class="w"> </span><span class="m">5</span><span class="p">))</span><span class="w">
</span><span class="n">grid.arrange</span><span class="p">(</span><span class="n">p1_grob</span><span class="p">,</span><span class="w"> </span><span class="n">p4_grob</span><span class="p">,</span><span class="w"> </span><span class="n">p5_grob</span><span class="p">,</span><span class="w"> </span><span class="n">p2_grob</span><span class="p">,</span><span class="w"> </span><span class="n">p3_grob</span><span class="p">,</span><span class="w"> </span><span class="n">layout_matrix</span><span class="o">=</span><span class="n">layout</span><span class="p">)</span><span class="w">
</span></code></pre></div></div>

<p>At the end you should see something like the figure below.</p>

<figure class="figure" width="750">
    <img class="image " src="/assets/advanced_ggplot/arrangedPlot.3.png" width="750" />
    
    
</figure>

<h4 id="aligning-plots-part-2">Aligning plots part 2</h4>

<p>If you poked around the grob a bit you might have noticed that this only works because each plot has an equal number of viewports/grobs all of which have an associated width. What would you do then in a situation where your plots don’t have the same number of viewports. For example what if our boxplots didn’t have a legend. Fortunately there is a simple way around this, let’s start by first removing the legend from our boxplots and converting the resulting plots to grobs. If you take a look at the barchart (p4) and boxplot (p2) table grob you’ll notice that they are now different sizes as expected. The barchart is 12 x 11 and the boxplot is 12 x 9 further we see the boxplot is missing the grob named “guide-box” which corresponds to the legend. We don’t care that the grob is missing neccessarily, in fact it’s what we want, but we do need to add columns to the tableGrob for the boxplot to match the barchart. Examining the grobs we can see that the “guide-box” of the barchart spans columns 9-9 so we should add a place holder column before that at position 8. Further we can see we will actually need to add 2 placeholders, as the barchart has 11 columns and our boxplot has 9. This is because we need a placeholder not only for the legend but the whitespace between the legend and the main plot as well. Fortunately the gTable package has a function to add columns <code class="language-plaintext highlighter-rouge">gtable_add_cols</code>, it takes the gTable ojbect to modify, the width of the column to be added, and the position to add the column as arguments. For our purposes we need to specify a width but the actual width doesn’t matter, it just needs to be a valid width as we will be reassigning that width in a minute anyway.</p>

<div class="language-R highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># remove legend from the boxplots</span><span class="w">
</span><span class="n">p2</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">p2</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">theme</span><span class="p">(</span><span class="n">legend.position</span><span class="o">=</span><span class="s2">"none"</span><span class="p">)</span><span class="w">
</span><span class="n">p3</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">p3</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">theme</span><span class="p">(</span><span class="n">legend.position</span><span class="o">=</span><span class="s2">"none"</span><span class="p">)</span><span class="w">

</span><span class="c1"># and then convert these to grob objects</span><span class="w">
</span><span class="n">p2_grob</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">ggplotGrob</span><span class="p">(</span><span class="n">p2</span><span class="p">)</span><span class="w">
</span><span class="n">p3_grob</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">ggplotGrob</span><span class="p">(</span><span class="n">p3</span><span class="p">)</span><span class="w">

</span><span class="c1"># look at on of the boxplot/barchart grob sets</span><span class="w">
</span><span class="n">p2_grob</span><span class="w">
</span><span class="n">p4_grob</span><span class="w">

</span><span class="n">p2_grob</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">gtable_add_cols</span><span class="p">(</span><span class="n">p2_grob</span><span class="p">,</span><span class="w"> </span><span class="n">widths</span><span class="o">=</span><span class="n">unit</span><span class="p">(</span><span class="m">1</span><span class="p">,</span><span class="w"> </span><span class="s2">"null"</span><span class="p">),</span><span class="w"> </span><span class="n">pos</span><span class="o">=</span><span class="m">8</span><span class="p">)</span><span class="w">
</span><span class="n">p2_grob</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">gtable_add_cols</span><span class="p">(</span><span class="n">p2_grob</span><span class="p">,</span><span class="w"> </span><span class="n">widths</span><span class="o">=</span><span class="n">unit</span><span class="p">(</span><span class="m">1</span><span class="p">,</span><span class="w"> </span><span class="s2">"null"</span><span class="p">),</span><span class="w"> </span><span class="n">pos</span><span class="o">=</span><span class="m">8</span><span class="p">)</span><span class="w">

</span><span class="n">p3_grob</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">gtable_add_cols</span><span class="p">(</span><span class="n">p3_grob</span><span class="p">,</span><span class="w"> </span><span class="n">widths</span><span class="o">=</span><span class="n">unit</span><span class="p">(</span><span class="m">1</span><span class="p">,</span><span class="w"> </span><span class="s2">"null"</span><span class="p">),</span><span class="w"> </span><span class="n">pos</span><span class="o">=</span><span class="m">8</span><span class="p">)</span><span class="w">
</span><span class="n">p3_grob</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">gtable_add_cols</span><span class="p">(</span><span class="n">p3_grob</span><span class="p">,</span><span class="w"> </span><span class="n">widths</span><span class="o">=</span><span class="n">unit</span><span class="p">(</span><span class="m">1</span><span class="p">,</span><span class="w"> </span><span class="s2">"null"</span><span class="p">),</span><span class="w"> </span><span class="n">pos</span><span class="o">=</span><span class="m">8</span><span class="p">)</span><span class="w">
</span></code></pre></div></div>

<p>From here we can use the same methodology as we employed before to align the plots. You should see something like the figure below</p>

<div class="language-R highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># get the grob width for the new boxplots</span><span class="w">
</span><span class="n">p2_grob_widths</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">p2_grob</span><span class="o">$</span><span class="n">widths</span><span class="w">
</span><span class="n">p3_grob_widths</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">p3_grob</span><span class="o">$</span><span class="n">widths</span><span class="w">

</span><span class="c1"># find the max width of all elements</span><span class="w">
</span><span class="n">maxWidth</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">unit.pmax</span><span class="p">(</span><span class="n">p4_grob_widths</span><span class="p">,</span><span class="w"> </span><span class="n">p5_grob_widths</span><span class="p">,</span><span class="w"> </span><span class="n">p2_grob_widths</span><span class="p">,</span><span class="w"> </span><span class="n">p3_grob_widths</span><span class="p">)</span><span class="w">

</span><span class="c1"># assign this max width to all elements</span><span class="w">
</span><span class="n">p4_grob</span><span class="o">$</span><span class="n">widths</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">maxWidth</span><span class="w">
</span><span class="n">p5_grob</span><span class="o">$</span><span class="n">widths</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">maxWidth</span><span class="w">
</span><span class="n">p2_grob</span><span class="o">$</span><span class="n">widths</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">maxWidth</span><span class="w">
</span><span class="n">p3_grob</span><span class="o">$</span><span class="n">widths</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">maxWidth</span><span class="w">

</span><span class="c1"># create a layout and plot the result</span><span class="w">
</span><span class="n">layout</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">rbind</span><span class="p">(</span><span class="nf">c</span><span class="p">(</span><span class="m">1</span><span class="p">,</span><span class="w"> </span><span class="m">1</span><span class="p">),</span><span class="w">
                </span><span class="nf">c</span><span class="p">(</span><span class="m">2</span><span class="p">,</span><span class="w"> </span><span class="m">3</span><span class="p">),</span><span class="w">
                </span><span class="nf">c</span><span class="p">(</span><span class="m">4</span><span class="p">,</span><span class="w"> </span><span class="m">5</span><span class="p">))</span><span class="w">
</span><span class="n">finalGrob</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">grid.arrange</span><span class="p">(</span><span class="n">p1_grob</span><span class="p">,</span><span class="w"> </span><span class="n">p4_grob</span><span class="p">,</span><span class="w"> </span><span class="n">p5_grob</span><span class="p">,</span><span class="w"> </span><span class="n">p2_grob</span><span class="p">,</span><span class="w"> </span><span class="n">p3_grob</span><span class="p">,</span><span class="w"> </span><span class="n">layout_matrix</span><span class="o">=</span><span class="n">layout</span><span class="p">)</span><span class="w">
</span><span class="n">grid.draw</span><span class="p">(</span><span class="n">finalGrob</span><span class="p">)</span><span class="w">
</span></code></pre></div></div>

<figure class="figure" width="750">
    <img class="image " src="/assets/advanced_ggplot/arrangedPlot.4.png" width="750" />
    
    
</figure>

<h4 id="gtable-grob-modification">gTable grob modification</h4>

<p>Were almost done with our final plot, there’s just one more thing we’re going to cover. It might have occurred to you that if we can view grobs we can manipulate them and you would be right. Let’s suppose that we want to color the labels in our final plot in a specific way, in particular we want to highlight the genes in the top most plot in red for which we have boxplots. The good new is that we can do this, the trick is to know which grobs and viewports to dig into. As a side note, it is hugely beneficial to use Rstudio when doing this sort of thing to take advantage of the autocompletion feature. To start digging in we need to look at the various grobs and their viewports. We first go into <code class="language-plaintext highlighter-rouge">finalGrob$grobs</code> which will print out all grobs at this level as a list. There are 5 one for each of our plots we used with grid.arrange and the first one in the list corresponds to the top plot which we can access with <code class="language-plaintext highlighter-rouge">[[]]</code> and draw with <code class="language-plaintext highlighter-rouge">grid.draw()</code> to verify. Digging in further through lists of grobs we can finally get to the x axis with  <code class="language-plaintext highlighter-rouge">grid.draw(finalGrob$grobs[[1]]$grobs[[7]]$children$axis$grobs[[2]])</code>. Going just a bit further we can see that the x-axis has a color of “grey30” and we simply give it a new vector of colors to change the color for each label. At the end you should see something like the plot below:</p>

<div class="language-R highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># figure out the base grob we want to dig into</span><span class="w">
</span><span class="n">grid.draw</span><span class="p">(</span><span class="n">finalGrob</span><span class="o">$</span><span class="n">grobs</span><span class="p">[[</span><span class="m">1</span><span class="p">]])</span><span class="w">
</span><span class="n">dev.off</span><span class="p">()</span><span class="w">

</span><span class="c1"># access x-axis</span><span class="w">
</span><span class="n">grid.draw</span><span class="p">(</span><span class="n">finalGrob</span><span class="o">$</span><span class="n">grobs</span><span class="p">[[</span><span class="m">1</span><span class="p">]]</span><span class="o">$</span><span class="n">grobs</span><span class="p">[[</span><span class="m">7</span><span class="p">]]</span><span class="o">$</span><span class="n">children</span><span class="o">$</span><span class="n">axis</span><span class="o">$</span><span class="n">grobs</span><span class="p">[[</span><span class="m">2</span><span class="p">]])</span><span class="w">
</span><span class="n">dev.off</span><span class="p">()</span><span class="w">

</span><span class="c1"># access x-axis color</span><span class="w">
</span><span class="n">finalGrob</span><span class="o">$</span><span class="n">grobs</span><span class="p">[[</span><span class="m">1</span><span class="p">]]</span><span class="o">$</span><span class="n">grobs</span><span class="p">[[</span><span class="m">7</span><span class="p">]]</span><span class="o">$</span><span class="n">children</span><span class="o">$</span><span class="n">axis</span><span class="o">$</span><span class="n">grobs</span><span class="p">[[</span><span class="m">2</span><span class="p">]]</span><span class="o">$</span><span class="n">children</span><span class="o">$</span><span class="n">GRID.text.6880</span><span class="o">$</span><span class="n">gp</span><span class="o">$</span><span class="n">col</span><span class="w">

</span><span class="c1"># change the color of the x-axis text</span><span class="w">
</span><span class="n">finalGrob</span><span class="o">$</span><span class="n">grobs</span><span class="p">[[</span><span class="m">1</span><span class="p">]]</span><span class="o">$</span><span class="n">grobs</span><span class="p">[[</span><span class="m">7</span><span class="p">]]</span><span class="o">$</span><span class="n">children</span><span class="o">$</span><span class="n">axis</span><span class="o">$</span><span class="n">grobs</span><span class="p">[[</span><span class="m">2</span><span class="p">]]</span><span class="o">$</span><span class="n">children</span><span class="o">$</span><span class="n">GRID.text.6880</span><span class="o">$</span><span class="n">gp</span><span class="o">$</span><span class="n">col</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="s2">"blue"</span><span class="p">,</span><span class="w"> </span><span class="s2">"blue"</span><span class="p">,</span><span class="w"> </span><span class="s2">"blue"</span><span class="p">,</span><span class="w"> </span><span class="s2">"blue"</span><span class="p">,</span><span class="w"> </span><span class="s2">"blue"</span><span class="p">,</span><span class="w"> </span><span class="s2">"red"</span><span class="p">,</span><span class="w"> </span><span class="s2">"red"</span><span class="p">,</span><span class="w"> </span><span class="s2">"blue"</span><span class="p">,</span><span class="w"> </span><span class="s2">"red"</span><span class="p">)</span><span class="w">

</span><span class="c1"># plot the result</span><span class="w">
</span><span class="n">finalGrob</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">grid.arrange</span><span class="p">(</span><span class="n">p1_grob</span><span class="p">,</span><span class="w"> </span><span class="n">p4_grob</span><span class="p">,</span><span class="w"> </span><span class="n">p5_grob</span><span class="p">,</span><span class="w"> </span><span class="n">p2_grob</span><span class="p">,</span><span class="w"> </span><span class="n">p3_grob</span><span class="p">,</span><span class="w"> </span><span class="n">layout_matrix</span><span class="o">=</span><span class="n">layout</span><span class="p">)</span><span class="w">
</span><span class="n">grid.draw</span><span class="p">(</span><span class="n">finalGrob</span><span class="p">)</span><span class="w">
</span></code></pre></div></div>

<figure class="figure" width="750">
    <img class="image " src="/assets/advanced_ggplot/arrangedPlot.5.png" width="750" />
    
    
</figure>

<p>Most of the material in here, specifically the modification of gTable objects is advanced and in most cases will probably be uneccessary. But hopefully if you need to modify these types of objects you’ll have a basic understanding of how to go about doing it. We’ve really only scratched the surface of gTable objects as these are low level functions. The thing to remember is that you can modify these objects with some patience and trail and error.</p>

<h4 id="exercise">Exercise</h4>

<p>Someone has decided they want a purple border around all the legends for our final plot (don’t ask me why). We could of course do this within ggplot but let’s imagine we’ve lost the code for creating the plot and only have the grob object to work with. Follow the instructions below and modify the grob to have this purple border.</p>

<ol>
  <li>Save our <code class="language-plaintext highlighter-rouge">finalGrob</code> as <code class="language-plaintext highlighter-rouge">exercise1</code> so we don’t overwrite anything</li>
  <li>dig into the newly saved <code class="language-plaintext highlighter-rouge">exercise1</code> and attempt to find where to change the legend border (hint your looking for something called col)</li>
  <li>Replace the value currently in <code class="language-plaintext highlighter-rouge">col</code> to purple</li>
  <li>use <code class="language-plaintext highlighter-rouge">grid.draw()</code> to plot the result</li>
</ol>

<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>

<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>

<script type="text/javascript" src="/assets/scripts/question.js"></script>

<div class="accordion">
    <p class="q">solution</p>
    <p class="a">The solution is in <a href="http://genviz.org/assets/advanced_ggplot/exercise1/solution.2.R">solution.2.R</a></p>
</div>]]></content><author><name>Zachary Skidmore</name></author><category term="Module-07-Appendix" /><summary type="html"><![CDATA[This appendix is a continuation of the arrangingPlots, here we go over some advanced concepts in terms of aligning plot eements and manipulating grob objects. Some of the objects we’ll be working with come from the previously mentioned section so make sure you have that code run! Aligning plots part 1 Our plot from the arrangingPlots section is looking pretty good, you might notice an unfortunate issue however in that the boxplots don’t align with their respective barcharts. Don’t worry it’s fairly easy to fix in this case, however before we start we need to go down a rabbit hole and obtain a basic understanding of grobs, tableGrobs and viewports. First off a grob is just short for “grid graphical object” from the low-level graphics package grid; Think of it as a set of instructions for create a graphical object (i.e. a plot). The graphics library underneath all of ggplot2’s graphical elements are really composed of grob’s because ggplot2 uses grid underneath. A TableGrob is a class from the gtable package and provides an easier way to view and manipulate groups of grobs, it is actually the intermediary between ggplot2 and grid. A “viewport” is a graphics region for which describes where a grob or group of grobs is assigned on a graphics device. When we have been calling grid.arrange in our previous examples what we are really doing is arranging viewports which contain groups of grobs. To Illustrate grobs and viewports a bit further let’s convert our arranged plot to a grob and take a look at it. grob &lt;- arrangeGrob(p1, p4, p5, p2, p3, layout_matrix=layout) grob you’ll notice a couple things right away, the table grob is composed of 5 individual grobs and are arranged in a 3 row, 2 column layout. The z column denotes the order in which grobs are plotted. the cells column is telling us where the grob is located within the viewport. For example the first element has a value of (1-1,1-2). This is telling us that that grob spans from from rows 1 to 1 (1-1) on the viewport and columns 1 to 2 (1-2) on the viewport. This is a bit easier to illustrate by viewing the actual layout with gtable_show_layout(). gtable_show_layout(grob) After running the command above you should see something like the figure below, (note that i’ve taken the liberty of overalying the output ontop of the original plot). Notice how the first grob is spanning rows 1-1 and columns 1-2. We can verify that this is correct by drawing just the first grob. grid.draw(grob$grobs[[1]]) dev.off() Okay our trip down the rabbit hole is coming to an end, I’ll just mention one last thing. As eluded to already the tableGrob we looked at is just a collection of viewports and those viewports contain grobs. In the grob we looked at we were at the top level and so by default the viewport takes up the entire page. Inside this top level we saw 5 grobs each of which have their own viewports. In the above command we go a layer deeper and draw one grob which itself has viewports it’s own associated viewports for the elements of the plot (legend, axis, etc.). We glossed over quite a bit of detail in our discussion of grobs, tableGrobs and viewports however I think we know enough to get our plots to align. To start we need to convert all of the plots we made in ggplot to grobs, we can do this with the ggplotGrob() function. Next each viewport in the grob at this level has an associated width, for example the axis title has a width, the axis text etc. We can access these widths within the table grob using tableGrob$widths which will output a vector of these widths. We can then use the unit.pmax() function to find the maximum width for each viewport among all of our plots. From there it’s a simple matter of manually modifying and reassinging the widths for each grob and plotting the results as before. # convert to grobs p2_grob &lt;- ggplotGrob(p2) p3_grob &lt;- ggplotGrob(p3) p4_grob &lt;- ggplotGrob(p4) p5_grob &lt;- ggplotGrob(p5) # align plots p4_grob_widths &lt;- p4_grob$widths p5_grob_widths &lt;- p5_grob$widths p2_grob_widths &lt;- p2_grob$widths p3_grob_widths &lt;- p3_grob$widths maxWidth &lt;- unit.pmax(p4_grob_widths, p5_grob_widths, p2_grob_widths, p3_grob_widths) p4_grob$widths &lt;- maxWidth p5_grob$widths &lt;- maxWidth p2_grob$widths &lt;- maxWidth p3_grob$widths &lt;- maxWidth layout &lt;- rbind(c(1, 1), c(2, 3), c(4, 5)) grid.arrange(p1_grob, p4_grob, p5_grob, p2_grob, p3_grob, layout_matrix=layout) At the end you should see something like the figure below. Aligning plots part 2 If you poked around the grob a bit you might have noticed that this only works because each plot has an equal number of viewports/grobs all of which have an associated width. What would you do then in a situation where your plots don’t have the same number of viewports. For example what if our boxplots didn’t have a legend. Fortunately there is a simple way around this, let’s start by first removing the legend from our boxplots and converting the resulting plots to grobs. If you take a look at the barchart (p4) and boxplot (p2) table grob you’ll notice that they are now different sizes as expected. The barchart is 12 x 11 and the boxplot is 12 x 9 further we see the boxplot is missing the grob named “guide-box” which corresponds to the legend. We don’t care that the grob is missing neccessarily, in fact it’s what we want, but we do need to add columns to the tableGrob for the boxplot to match the barchart. Examining the grobs we can see that the “guide-box” of the barchart spans columns 9-9 so we should add a place holder column before that at position 8. Further we can see we will actually need to add 2 placeholders, as the barchart has 11 columns and our boxplot has 9. This is because we need a placeholder not only for the legend but the whitespace between the legend and the main plot as well. Fortunately the gTable package has a function to add columns gtable_add_cols, it takes the gTable ojbect to modify, the width of the column to be added, and the position to add the column as arguments. For our purposes we need to specify a width but the actual width doesn’t matter, it just needs to be a valid width as we will be reassigning that width in a minute anyway. # remove legend from the boxplots p2 &lt;- p2 + theme(legend.position="none") p3 &lt;- p3 + theme(legend.position="none") # and then convert these to grob objects p2_grob &lt;- ggplotGrob(p2) p3_grob &lt;- ggplotGrob(p3) # look at on of the boxplot/barchart grob sets p2_grob p4_grob p2_grob &lt;- gtable_add_cols(p2_grob, widths=unit(1, "null"), pos=8) p2_grob &lt;- gtable_add_cols(p2_grob, widths=unit(1, "null"), pos=8) p3_grob &lt;- gtable_add_cols(p3_grob, widths=unit(1, "null"), pos=8) p3_grob &lt;- gtable_add_cols(p3_grob, widths=unit(1, "null"), pos=8) From here we can use the same methodology as we employed before to align the plots. You should see something like the figure below # get the grob width for the new boxplots p2_grob_widths &lt;- p2_grob$widths p3_grob_widths &lt;- p3_grob$widths # find the max width of all elements maxWidth &lt;- unit.pmax(p4_grob_widths, p5_grob_widths, p2_grob_widths, p3_grob_widths) # assign this max width to all elements p4_grob$widths &lt;- maxWidth p5_grob$widths &lt;- maxWidth p2_grob$widths &lt;- maxWidth p3_grob$widths &lt;- maxWidth # create a layout and plot the result layout &lt;- rbind(c(1, 1), c(2, 3), c(4, 5)) finalGrob &lt;- grid.arrange(p1_grob, p4_grob, p5_grob, p2_grob, p3_grob, layout_matrix=layout) grid.draw(finalGrob) gTable grob modification Were almost done with our final plot, there’s just one more thing we’re going to cover. It might have occurred to you that if we can view grobs we can manipulate them and you would be right. Let’s suppose that we want to color the labels in our final plot in a specific way, in particular we want to highlight the genes in the top most plot in red for which we have boxplots. The good new is that we can do this, the trick is to know which grobs and viewports to dig into. As a side note, it is hugely beneficial to use Rstudio when doing this sort of thing to take advantage of the autocompletion feature. To start digging in we need to look at the various grobs and their viewports. We first go into finalGrob$grobs which will print out all grobs at this level as a list. There are 5 one for each of our plots we used with grid.arrange and the first one in the list corresponds to the top plot which we can access with [[]] and draw with grid.draw() to verify. Digging in further through lists of grobs we can finally get to the x axis with grid.draw(finalGrob$grobs[[1]]$grobs[[7]]$children$axis$grobs[[2]]). Going just a bit further we can see that the x-axis has a color of “grey30” and we simply give it a new vector of colors to change the color for each label. At the end you should see something like the plot below: # figure out the base grob we want to dig into grid.draw(finalGrob$grobs[[1]]) dev.off() # access x-axis grid.draw(finalGrob$grobs[[1]]$grobs[[7]]$children$axis$grobs[[2]]) dev.off() # access x-axis color finalGrob$grobs[[1]]$grobs[[7]]$children$axis$grobs[[2]]$children$GRID.text.6880$gp$col # change the color of the x-axis text finalGrob$grobs[[1]]$grobs[[7]]$children$axis$grobs[[2]]$children$GRID.text.6880$gp$col &lt;- c("blue", "blue", "blue", "blue", "blue", "red", "red", "blue", "red") # plot the result finalGrob &lt;- grid.arrange(p1_grob, p4_grob, p5_grob, p2_grob, p3_grob, layout_matrix=layout) grid.draw(finalGrob) Most of the material in here, specifically the modification of gTable objects is advanced and in most cases will probably be uneccessary. But hopefully if you need to modify these types of objects you’ll have a basic understanding of how to go about doing it. We’ve really only scratched the surface of gTable objects as these are low level functions. The thing to remember is that you can modify these objects with some patience and trail and error. Exercise Someone has decided they want a purple border around all the legends for our final plot (don’t ask me why). We could of course do this within ggplot but let’s imagine we’ve lost the code for creating the plot and only have the grob object to work with. Follow the instructions below and modify the grob to have this purple border. Save our finalGrob as exercise1 so we don’t overwrite anything dig into the newly saved exercise1 and attempt to find where to change the legend border (hint your looking for something called col) Replace the value currently in col to purple use grid.draw() to plot the result solution The solution is in solution.2.R]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://www.genviz.org//assets/DNA.jpg" /><media:content medium="image" url="http://www.genviz.org//assets/DNA.jpg" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Q &amp;amp; A, Discussion, Integrated Assignments, and Working with Your Own Data</title><link href="http://www.genviz.org//module-06-review/0006/01/01/Review/" rel="alternate" type="text/html" title="Q &amp;amp; A, Discussion, Integrated Assignments, and Working with Your Own Data" /><published>0006-01-01T00:00:00+00:00</published><updated>0006-01-01T00:00:00+00:00</updated><id>http://www.genviz.org//module-06-review/0006/01/01/Review</id><content type="html" xml:base="http://www.genviz.org//module-06-review/0006/01/01/Review/"><![CDATA[<p>In this section we provide some additional exercises covering a range of topics to reinforce concepts and topics throughout this course series. We encourage students to attempt to do these exercises on their own. We have provided hints and an answer for each exercise however these should be used only as a last resort, students should first try searching for solutions throughout this course and other available resources throughout the web.</p>

<h4 id="additional-exercises">Additional Exercises</h4>
<p>In 1854 there was cholera epedemic in the Soho district of London kown as the <a href="https://en.wikipedia.org/wiki/1854_Broad_Street_cholera_outbreak">Golden square outbreak</a>. Ultimately a particularly virulent strain of the disease caused the deaths of 616 individuals. At this time there were two competing theories as to the cause of the outbreak. The commonly held miasma theory postulated that foul air from decaying organic matter was the cause of the disease. A physician by the name of <a href="https://en.wikipedia.org/wiki/John_Snow">John Snow</a> had published years earlier the competing germ theory, specifically postulating that cholera was caused by the presence of as yet unknown germ cells which contaminated water. The <a href="https://en.wikipedia.org/wiki/1854_Broad_Street_cholera_outbreak">Golden square outbreak</a> allowed <a href="https://en.wikipedia.org/wiki/John_Snow">John Snow</a> with the help of <a href="https://en.wikipedia.org/wiki/Henry_Whitehead_(priest)">Henry Whitehead</a> to map the deaths of the outbreak in relation to public water pumps around the area. Eventually this work led to the debunking of miasma theory. In this exercise try and recreate the famous map originally created by <a href="https://en.wikipedia.org/wiki/John_Snow">John Snow</a> to support his theory, an example of which is shown below. You’ll need to install the package <a href="https://cran.r-project.org/web/packages/cholera/index.html">cholera</a> and use the data frames specified below.</p>

<ul>
  <li><strong>topics covered:</strong> ggplot2, basic R</li>
  <li><strong>difficulty:</strong> 3/5</li>
</ul>

<figure class="figure" width="700">
    <img class="image " src="/assets/Review/snowMap.png" width="700" />
    
    
</figure>

<div class="language-R highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">install.packages</span><span class="p">(</span><span class="n">cholera</span><span class="p">)</span><span class="w">
</span><span class="n">data</span><span class="p">(</span><span class="n">roads</span><span class="p">)</span><span class="w">
</span><span class="n">data</span><span class="p">(</span><span class="n">pumps</span><span class="p">)</span><span class="w">
</span><span class="n">data</span><span class="p">(</span><span class="n">fatalities.address</span><span class="p">)</span><span class="w">
</span><span class="n">data</span><span class="p">(</span><span class="n">pump.case</span><span class="p">)</span><span class="w">
</span></code></pre></div></div>

<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>

<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>

<script type="text/javascript" src="/assets/scripts/question.js"></script>

<div class="accordion">
    <p class="q">Hint!</p>
    <p class="a">You shouldn't need to alter the roads dataframe to plot it with ggplot, take a look at the group aesthetic!</p>
</div>

<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>

<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>

<script type="text/javascript" src="/assets/scripts/question.js"></script>

<div class="accordion">
    <p class="q">Hint</p>
    <p class="a">you need to merge the fatalities.address and pump.case data frames but first you'll need to convert pump.case to a data frame, look at the stack() function!</p>
</div>

<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>

<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>

<script type="text/javascript" src="/assets/scripts/question.js"></script>

<div class="accordion">
    <p class="q">Answer</p>
    <p class="a">Download an Rscript with the answer <a href="http://genomedata.org/gen-viz-workshop/additional_exercises/snowMapAnswer.R">Here</a>.</p>
</div>

<ul>
  <li><strong>roads:</strong> Data frame providing the x/y coordinates for road start and end points grouped by street.</li>
  <li><strong>pumps:</strong> Data frame providing coordinates and names for water pumps.</li>
  <li><strong>fatalities.address:</strong> Data frame providing coordinates for each anchor case address for a case of cholera (i.e. address of the first cholera case at an address)</li>
  <li><strong>pump.case:</strong> list of vectors associating each anchor case with a water pump id.</li>
</ul>

<h4 id="lecture">Lecture</h4>
<p><a href="https://github.com/griffithlab/gen-viz-lectures/raw/master/GenViz_Module6_Lecture.pdf">Module 6 Lecture</a></p>]]></content><author><name>Zachary Skidmore</name></author><category term="Module-06-Review" /><summary type="html"><![CDATA[In this section we provide some additional exercises covering a range of topics to reinforce concepts and topics throughout this course series. We encourage students to attempt to do these exercises on their own. We have provided hints and an answer for each exercise however these should be used only as a last resort, students should first try searching for solutions throughout this course and other available resources throughout the web. Additional Exercises In 1854 there was cholera epedemic in the Soho district of London kown as the Golden square outbreak. Ultimately a particularly virulent strain of the disease caused the deaths of 616 individuals. At this time there were two competing theories as to the cause of the outbreak. The commonly held miasma theory postulated that foul air from decaying organic matter was the cause of the disease. A physician by the name of John Snow had published years earlier the competing germ theory, specifically postulating that cholera was caused by the presence of as yet unknown germ cells which contaminated water. The Golden square outbreak allowed John Snow with the help of Henry Whitehead to map the deaths of the outbreak in relation to public water pumps around the area. Eventually this work led to the debunking of miasma theory. In this exercise try and recreate the famous map originally created by John Snow to support his theory, an example of which is shown below. You’ll need to install the package cholera and use the data frames specified below. topics covered: ggplot2, basic R difficulty: 3/5 install.packages(cholera) data(roads) data(pumps) data(fatalities.address) data(pump.case) Hint! You shouldn't need to alter the roads dataframe to plot it with ggplot, take a look at the group aesthetic! Hint you need to merge the fatalities.address and pump.case data frames but first you'll need to convert pump.case to a data frame, look at the stack() function! Answer Download an Rscript with the answer Here. roads: Data frame providing the x/y coordinates for road start and end points grouped by street. pumps: Data frame providing coordinates and names for water pumps. fatalities.address: Data frame providing coordinates for each anchor case address for a case of cholera (i.e. address of the first cholera case at an address) pump.case: list of vectors associating each anchor case with a water pump id. Lecture Module 6 Lecture]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://www.genviz.org//assets/DNA.jpg" /><media:content medium="image" url="http://www.genviz.org//assets/DNA.jpg" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">CIViC</title><link href="http://www.genviz.org//module-05-annotation/0005/04/01/CIViC/" rel="alternate" type="text/html" title="CIViC" /><published>0005-04-01T00:00:00+00:00</published><updated>0005-04-01T00:00:00+00:00</updated><id>http://www.genviz.org//module-05-annotation/0005/04/01/CIViC</id><content type="html" xml:base="http://www.genviz.org//module-05-annotation/0005/04/01/CIViC/"><![CDATA[<p>Precision medicine refers to the use of prevention and treatment strategies that are tailored to the unique features of each individual and their disease. In the context of cancer this might involve the identification of specific mutations shown to predict response to a targeted therapy. The biomedical literature describing these associations is large and growing rapidly. Currently these interpretations exist largely in private or encumbered databases resulting in extensive repetition of effort. Realizing precision medicine will require this information to be centralized, debated and interpreted for application in the clinic. <a href="https://civic.genome.wustl.edu/home">CIViC</a> is an open access, open source, community-driven web resource for Clinical Interpretation of Variants in Cancer. Its goal is to enable precision medicine by providing an educational forum for dissemination of knowledge and active discussion of the clinical significance of cancer genome alterations. For more details refer to the 2017 <a href="http://www.nature.com/ng/journal/v49/n2/full/ng.3774.html">CIViC publication</a> in Nature Genetics.</p>]]></content><author><name>Zachary Skidmore</name></author><category term="Module-05-Annotation" /><summary type="html"><![CDATA[Precision medicine refers to the use of prevention and treatment strategies that are tailored to the unique features of each individual and their disease. In the context of cancer this might involve the identification of specific mutations shown to predict response to a targeted therapy. The biomedical literature describing these associations is large and growing rapidly. Currently these interpretations exist largely in private or encumbered databases resulting in extensive repetition of effort. Realizing precision medicine will require this information to be centralized, debated and interpreted for application in the clinic. CIViC is an open access, open source, community-driven web resource for Clinical Interpretation of Variants in Cancer. Its goal is to enable precision medicine by providing an educational forum for dissemination of knowledge and active discussion of the clinical significance of cancer genome alterations. For more details refer to the 2017 CIViC publication in Nature Genetics.]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://www.genviz.org//assets/DNA.jpg" /><media:content medium="image" url="http://www.genviz.org//assets/DNA.jpg" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">ClinVar</title><link href="http://www.genviz.org//module-05-annotation/0005/03/01/clinvar/" rel="alternate" type="text/html" title="ClinVar" /><published>0005-03-01T00:00:00+00:00</published><updated>0005-03-01T00:00:00+00:00</updated><id>http://www.genviz.org//module-05-annotation/0005/03/01/clinvar</id><content type="html" xml:base="http://www.genviz.org//module-05-annotation/0005/03/01/clinvar/"><![CDATA[<p><a href="https://www.ncbi.nlm.nih.gov/clinvar/">ClinVar</a> aggregates information about genomic variation and its relationship to human health. To date, thousands of variants and associated phenotypes have been deposited in ClinVar. The main use case for ClinVar is when you have one or more variants observed in a human sample and you would like to know if these variants have established clinical relevance. The resource is fairly heavily biased towards germline inherited variants that predispose an individual for disease. Many diseases and phenotypes have been reported. Some of the variant observations in ClinVar come from sources that mine published literature (e.g. <a href="http://docm.info">DoCM</a>, <a href="https://www.omim.org/">OMiM</a>, etc.). However, the majority of submissions to ClinVar come from clinical sequencing labs that identify variants in patients and report that variant along with the phenotype of the patient(see <a href="https://www.ncbi.nlm.nih.gov/clinvar/docs/datasources/">Data Sources</a> for more details). Several types of variants are supported in ClinVar. Many are single nucleotide variants (SNVs) or small insertions and deletions, but large structural variants are also supported. In general, all variants in ClinVar are designated using the HGVS nomenclature. ClinVar strongly recommends following the ACMG guidelines in deciding on the clinical significance (pathogenicity) of variants. The following tutorial explores these concepts in greater detail.</p>

<h3 id="basic-intro-to-the-clinvar-interface">Basic intro to the ClinVar interface</h3>
<p>ClinVar offers a few video tutorials to get you started: <a href="https://youtu.be/A8G3ej83ZgU">Intro to ClinVar</a> and <a href="https://youtu.be/H09-0pP48Us">Find All Variants with ClinVar</a>. The ClinVar website is quite simple. The <a href="https://www.ncbi.nlm.nih.gov/clinvar/">home page</a> offers various links to frequently used sections. The tool bar at the top of each page includes the simple search bar and various menus to navigate to additional information.</p>

<figure class="figure" width="1200">
    <img class="image " src="/assets/ClinVar/clinvar1.png" width="1200" />
    
    
</figure>

<ul>
  <li>To get a sense of where the data in ClinVar is coming from, visit the complete <a href="https://www.ncbi.nlm.nih.gov/clinvar/docs/submitter_list/">submitter list</a> (Statistics -&gt; List of Submitters). This list gives you an idea of some of the major players in the genetic diagnostics and variant interpretation.</li>
</ul>

<figure class="figure" width="1200">
    <img class="image " src="/assets/ClinVar/clinvar2.png" width="1200" />
    
    
</figure>

<ul>
  <li>To get a sense of the current content of ClinVar, visit the <a href="https://www.ncbi.nlm.nih.gov/clinvar/submitters/">Statistics page</a> (Statistics -&gt; Statistics).</li>
</ul>

<figure class="figure" width="1000">
    <img class="image " src="/assets/ClinVar/clinvar3.png" width="1000" />
    
    
</figure>

<ul>
  <li>If you would like to download raw ClinVar data, you can do so from their <a href="ftp://ftp.ncbi.nlm.nih.gov/pub/clinvar/">FTP site</a>. For example, you can download the complete set of ClinVar variants in VCF format (<a href="ftp://ftp.ncbi.nlm.nih.gov/pub/clinvar/vcf_GRCh37/">human genome build 37 VCF</a> or <a href="ftp://ftp.ncbi.nlm.nih.gov/pub/clinvar/vcf_GRCh38/">human genome build 38 VCF</a>).</li>
</ul>

<p>ClinVar has several key entities used to organize variant data. (1) Each submission of an interpretation for a variant is assigned a <strong>submission accession</strong> of the format <em>SCV000000000.0</em>. Multiple labs could report independent interpretations for the same variant. Each would be entered as a distinct record. If a lab wishes to update that submission, they can do so, and the version number will be updated to reflect this change. (2) When there are multiple submissions about the same variation/condition relationship, they are aggregated under a <strong>reference accession</strong> of the format <em>RCV000000000.0</em>. If the same variant is associated with multiple conditions, there will be multiple reference accessions. (3) All information about a single distinct variant are organized under one Variation ID and a corresponding Allele ID. Both are simple integers. Each allele corresponds to a distinct genomic variation that must be described using one or more HGVS expressions.</p>

<p>Consider the following example: <a href="https://www.ncbi.nlm.nih.gov/clinvar/variation/55628/">NM_007294.3(BRCA1):c.5558dupA (p.Tyr1853Terfs)</a></p>

<figure class="figure" width="1000">
    <img class="image " src="/assets/ClinVar/clinvar4.png" width="1000" />
    
    
</figure>

<ul>
  <li>Variation ID: 55628</li>
  <li>Allele ID: 70295</li>
  <li>Submission accessions: SCV000300281.2, SCV000329130.4, SCV000488476.1, …</li>
  <li>Reference accessions: RCV000074357 (Breast-ovarian cancer, familial 1), …, RCV000163976 (Hereditary cancer-predisposing syndrome)</li>
</ul>

<p>A more detailed explanation of these identifiers can be found in the <a href="https://www.ncbi.nlm.nih.gov/clinvar/docs/identifiers/">Identifiers</a> section of the ClinVar documentation.</p>

<h3 id="searching-clinvar">Searching ClinVar</h3>
<p>ClinVar several search modes. (1) you can simply type free form text in the search box near the top of every page, (2) if you know the neccessary <a href="https://www.ncbi.nlm.nih.gov/clinvar/docs/help/">field codes</a>, you can construct complex queries in this same search box, (3) you can use the <a href="https://www.ncbi.nlm.nih.gov/clinvar/advanced/">Advanced Search Builder</a>.</p>

<ul>
  <li>Use the basic search box to find all variants for the gene <em>AKT1</em>. Note that when ClinVar detects a match to a proper gene name in your search, it assumes you want to search against only the gene name field. It indicates this by adding [gene] to your search text. If you wish to search all fields for this term, you can can chose the option to “Search instead for all ClinVar records that mention AKT1”.</li>
</ul>

<figure class="figure" width="1000">
    <img class="image " src="/assets/ClinVar/clinvar5.png" width="1000" />
    
    
</figure>

<ul>
  <li>Now try searching for a specific disease: proteus syndrome. What is the popular significance of this disease? Note that this search appears to be returning partial matches to various conditions. To get a more refined search, trying placing the condition name in quotes and adding the disease tag: “proteus syndrome”[dis]. Remember that we can find possible tags to use here on the <a href="https://www.ncbi.nlm.nih.gov/clinvar/docs/help/">help page</a>. Note the variant <em>NM_005163.2(AKT1):c.49G&gt;A (p.Glu17Lys)</em>. This variant has significance in both the germline and somatic contexts. In the germline context, it appears pathogenic for Proteus Syndrome (actually in this case, this involves somatic mosaicism).</li>
</ul>

<figure class="figure" width="1000">
    <img class="image " src="/assets/ClinVar/clinvar6.png" width="1000" />
    
    
</figure>

<ul>
  <li>
    <p>Search for variants within a narrow window on a single chromosome: “5[chr] AND 1264532:1264552[chrpos]”. What gene does this region correspond to? What is the significance of this gene?</p>
  </li>
  <li>
    <p>Use the <a href="https://www.ncbi.nlm.nih.gov/clinvar/advanced">Advanced Search</a> page to construct the same query. Note that you can use the <em>show index list</em> option to see valid search options/examples for each field.</p>
  </li>
</ul>

<figure class="figure" width="1200">
    <img class="image " src="/assets/ClinVar/clinvar7.png" width="1200" />
    
    
</figure>

<ul>
  <li>Use the Advanced Search page to construct a complex query. For example: Variants submitted by “Counsyl, that are “duplication variants”, for the disease “Renal carnitine transport defect”, and with review status “criteria provided, single submitter”.</li>
</ul>

<figure class="figure" width="1200">
    <img class="image " src="/assets/ClinVar/clinvar8.png" width="1200" />
    
    
</figure>

<h3 id="obtaining-a-list-of-all-high-quality-predisposing-variants-in-clinvar">Obtaining a list of all high quality predisposing variants in ClinVar</h3>
<p>If you just want the complete list of high quality variants that you might consider returning results for in a clinical setting. You might decide to peform a query that requires the variant to have been reviewed by an expert panel, belong to a practice guideline, or have been submitted by multiple entities that agree on the interpretation. This list could be further limited to those that are Pathogenic and where the variant origin is stated as “germline”.  If you put all of this together the query looks like this:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>((("reviewed by expert panel"[Review status]) OR "criteria provided, multiple submitters, no conflicts"[Review status]) OR "practice guideline"[Review status]) AND "clinsig pathogenic"[Properties] AND "germline"[Origin]
</code></pre></div></div>

<ul>
  <li>Download this complete data set to a tabular (TSV) file and open it is a spreadsheet editor such as Excel.</li>
</ul>

<h3 id="a-brief-primer-on-hgvs">A brief primer on HGVS</h3>
<p>In completing the exercises above in ClinVar you will have noticed that variants have rather complex looking names. This is a deliberate choice on the part of ClinVar. It is common in the literature and in day to day conversations to refer to variants with shorthand names. For example, someone refers to <em>BRAF V600E</em> you may have a good idea what they are referring to. However, such names can be ambigiguous. There are often multiple correct variations in the genome that could lead to a V600E effect in the protein. Furthermore, if you want to design an assay to detect this variant in RNA, cDNA, or genomic DNA, then <em>V600E</em> is not very useful. For this reason, the community has adopted a set of standards for describing variants in a much more precise way that is unambiguous and leads to less confusion. The <a href="http://www.hgvs.org/">Human Genome Variation Society (HGVS)</a> acts as a steward for these standards. They have also released detailed guidelines (the rulebook) on the correct way to create HGVS expressions for almost any variant. These guidelines are maintained, updated, and versioned. They are available at: <a href="http://varnomen.hgvs.org/">varnomen.hgvs.org</a>. ClinVar also provides a simple overview of the <a href="https://www.ncbi.nlm.nih.gov/clinvar/docs/hgvs_types/">HGVS types</a> they use. Here is an example of the same variant (AKT G17K) represented as HGVS expressions describing it at the:</p>

<p>ClinVar variant name: NM_005163.2(AKT1):c.49G&gt;A (p.Glu17Lys)
HGVS for protein: NP_005154.2:p.Glu17Lys
HGVS for cDNA: NM_005163.2:c.49G&gt;A
HGVS for genome: NC_000014.9:g.104780214C&gt;T (GRCh38)</p>

<p>Each HGVS has several required elements. First you have a sequence accession with a version number (e.g. <em>NM_005163.2</em>). This tells you unambiguously what the reference sequence is (a published protein, cDNA, or genome sequence). Then you have a delimiter “:”. Next you have a letter that indicates the general type of HGVS expression to follow: “p” for protein, “c” for cDNA, “g” for genomic DNA. Finally, you have the expression that describes the sequence variation itself. The rules for these expressions depend on the type of variation (substitution, duplication, deletion, etc.) and the level of HGVS (protein, cDNA, gDNA, RNA, …).</p>

<ul>
  <li>As an exercise. Try manually creating valid HGVS expressions at the genome, cDNA, and protein level for: <em>BRAF V600E</em>. What fundamental tools would we need to do this (assuming we can not just look it up in a database)?</li>
</ul>

<p>Here are some excellent resources for working with HGVS:</p>
<ul>
  <li><a href="http://varnomen.hgvs.org/">HGVS guidelines</a></li>
</ul>

<figure class="figure" width="1000">
    <img class="image " src="/assets/ClinVar/clinvar9.png" width="1000" />
    
    
</figure>

<ul>
  <li><a href="https://mutalyzer.nl/">Mutalyzer</a> (tools for validating and converting HGVS expressions)</li>
</ul>

<figure class="figure" width="1000">
    <img class="image " src="/assets/ClinVar/clinvar10.png" width="1000" />
    
    
</figure>

<ul>
  <li><a href="http://bioinformatics.mdanderson.org/transvarweb/">TransVar</a> (tool for converting HGVS expressions)</li>
</ul>

<figure class="figure" width="1000">
    <img class="image " src="/assets/ClinVar/clinvar11.png" width="1000" />
    
    
</figure>

<ul>
  <li><a href="http://myvariant.info/">MyVariantInfo</a> (aggregates variant info including HGVS expressions from many sources)</li>
</ul>

<figure class="figure" width="1000">
    <img class="image " src="/assets/ClinVar/clinvar12.png" width="1000" />
    
    
</figure>

<h3 id="a-brief-primer-on-the-acmg-guidelines-for-germline-variant-interpretation">A brief primer on the ACMG guidelines for germline variant interpretation</h3>
<p>You may have noticed in the exercises above various references to the <em>clinical significance</em> or <em>pathogenicity</em> of variants. In some queries of ClinVar we limited variants to only those that are thought to be Pathogenic (i.e. if inherited, the predispose an individual to a particular disease). How does one establish whether a variant is Pathogenic, or Benign?  To help establish how this should be done, the ACMG has released detailed <a href="http://dx.doi.org/10.1038/gim.2015.30">Standards and guidelines for the interpretation of sequence variants</a>. For the full details, one should definitely read that paper and refer back to it extensively as you try to interepret specific variants. Briefly, the ACMG established a variant classification approach that is meant to be applicable to variants in all <em>Mendelian genes</em>. They categorize the types of evidence that one should use to support the pathogenicity of a variant. Each type of evidence is indicated by an evidence code.  Evidence codes are broken down into two main categories, those that support a pathogenic interpretation, and those that support a benign interpretation. Within these two broad groups, sub-categories are defined for different types of evidence (e.g. functional data, population frequency, inheritance patterns, algorithmic predictions, etc.). The guidelines describe how to document this evidence and how to use that evidence to make a final conclusion for the pathogenicity of <em>each variant</em> for <em>each disease/phenotype</em>. The ACMG defines five tiers for the pathogenicity of a variant: “pathogenic,” “likely pathogenic,” “uncertain significance,” “likely benign,” and “benign”.</p>

<p>We strongly encourage you to read the ACMG guidelines carefully. The critical summaries are provided in several tables of this paper.</p>

<figure class="figure" width="1000">
    <img class="image " src="/assets/ClinVar/clinvar13.png" width="1000" />
    
    
</figure>

<ul>
  <li>Evidence and codes for classifying pathogenic variants: <a href="http://www.nature.com/gim/journal/v17/n5/fig_tab/gim201530t3.html">Table 3</a></li>
  <li>Evidence and codes for classifying benign variants: <a href="http://www.nature.com/gim/journal/v17/n5/fig_tab/gim201530t4.html">Table 4</a></li>
  <li>Rules for combining evidence to classify sequence variants: <a href="http://www.nature.com/gim/journal/v17/n5/fig_tab/gim201530t5.html">Table 5</a></li>
</ul>

<p>Final comment:
If used properly, the ACMG guidelines require a lot of evidence to define a variant as pathogenic or even likely pathgenic. Similarly strong evidence is needed to conclude a variant is benign or even likely benign. Many variants are thus initially classified as “uncertain significance”. Note that this does not mean <em>unknown</em> it means <em>uncertain</em>. i.e. lacking certainty. Since variant classifications are meant to be used in a clinical setting the goal is to avoid misinforming patients that may take life changing actions upon hearing these results.</p>

<h3 id="clinvar-practice-exercises">ClinVar practice exercises</h3>

<p>How many ClinVar variants are there for BRCA2 that are Germline, Pathogenic and have Expert Panel review status?
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script type="text/javascript" src="/assets/scripts/question.js"></script></p>

<div class="accordion">
    <p class="q">Get a hint!</p>
    <p class="a">Simply enter BRCA2 in the ClinVar search box, then apply the three filters requested.</p>
</div>

<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>

<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>

<script type="text/javascript" src="/assets/scripts/question.js"></script>

<div class="accordion">
    <p class="q">Answer</p>
    <p class="a">At the time of writing this post, there were 2069 germline, pathogenic, expert panel reviewed BRCA2 variants.</p>
</div>

<p>Use ClinVar to find valid HGVS expressions at the genome, cDNA, and protein level for the following variant: <em>PIK3CA</em> H1047R.
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script type="text/javascript" src="/assets/scripts/question.js"></script></p>

<div class="accordion">
    <p class="q">Get a hint!</p>
    <p class="a">Simply enter PIK3CA H1047R in the ClinVar search box, then review the HGVS section.</p>
</div>

<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>

<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>

<script type="text/javascript" src="/assets/scripts/question.js"></script>

<div class="accordion">
    <p class="q">Answer</p>
    <p class="a">NG_012113.2:g.90775A&gt;G (genome), NM_006218.3:c.3140A&gt;G (cDNA), NP_006209.2:p.His1047Arg (protein).</p>
</div>

<p>Find all of the variants in ClinVar that correspond to exon 2 of VHL. How many are there? Export these to a spreadsheet.
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script type="text/javascript" src="/assets/scripts/question.js"></script></p>

<div class="accordion">
    <p class="q">Get a hint!</p>
    <p class="a">Use a genome browser such as IGV to obtain the build38 coordinates for exon 2 of VHL. Then use the advanced ClinVar search box to search for variants in VHL and within the exon 2 coordinates.</p>
</div>

<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>

<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>

<script type="text/javascript" src="/assets/scripts/question.js"></script>

<div class="accordion">
    <p class="q">Answer</p>
    <p class="a">To get VHL exon 2 variants use this query. (VHL[Gene Name]) AND 10146514:10146636[Base Position]. At the time this question was created there were 517 variants in VHL and 63 of these were within exon 2.</p>
</div>]]></content><author><name>Zachary Skidmore</name></author><category term="Module-05-Annotation" /><summary type="html"><![CDATA[ClinVar aggregates information about genomic variation and its relationship to human health. To date, thousands of variants and associated phenotypes have been deposited in ClinVar. The main use case for ClinVar is when you have one or more variants observed in a human sample and you would like to know if these variants have established clinical relevance. The resource is fairly heavily biased towards germline inherited variants that predispose an individual for disease. Many diseases and phenotypes have been reported. Some of the variant observations in ClinVar come from sources that mine published literature (e.g. DoCM, OMiM, etc.). However, the majority of submissions to ClinVar come from clinical sequencing labs that identify variants in patients and report that variant along with the phenotype of the patient(see Data Sources for more details). Several types of variants are supported in ClinVar. Many are single nucleotide variants (SNVs) or small insertions and deletions, but large structural variants are also supported. In general, all variants in ClinVar are designated using the HGVS nomenclature. ClinVar strongly recommends following the ACMG guidelines in deciding on the clinical significance (pathogenicity) of variants. The following tutorial explores these concepts in greater detail. Basic intro to the ClinVar interface ClinVar offers a few video tutorials to get you started: Intro to ClinVar and Find All Variants with ClinVar. The ClinVar website is quite simple. The home page offers various links to frequently used sections. The tool bar at the top of each page includes the simple search bar and various menus to navigate to additional information. To get a sense of where the data in ClinVar is coming from, visit the complete submitter list (Statistics -&gt; List of Submitters). This list gives you an idea of some of the major players in the genetic diagnostics and variant interpretation. To get a sense of the current content of ClinVar, visit the Statistics page (Statistics -&gt; Statistics). If you would like to download raw ClinVar data, you can do so from their FTP site. For example, you can download the complete set of ClinVar variants in VCF format (human genome build 37 VCF or human genome build 38 VCF). ClinVar has several key entities used to organize variant data. (1) Each submission of an interpretation for a variant is assigned a submission accession of the format SCV000000000.0. Multiple labs could report independent interpretations for the same variant. Each would be entered as a distinct record. If a lab wishes to update that submission, they can do so, and the version number will be updated to reflect this change. (2) When there are multiple submissions about the same variation/condition relationship, they are aggregated under a reference accession of the format RCV000000000.0. If the same variant is associated with multiple conditions, there will be multiple reference accessions. (3) All information about a single distinct variant are organized under one Variation ID and a corresponding Allele ID. Both are simple integers. Each allele corresponds to a distinct genomic variation that must be described using one or more HGVS expressions. Consider the following example: NM_007294.3(BRCA1):c.5558dupA (p.Tyr1853Terfs) Variation ID: 55628 Allele ID: 70295 Submission accessions: SCV000300281.2, SCV000329130.4, SCV000488476.1, … Reference accessions: RCV000074357 (Breast-ovarian cancer, familial 1), …, RCV000163976 (Hereditary cancer-predisposing syndrome) A more detailed explanation of these identifiers can be found in the Identifiers section of the ClinVar documentation. Searching ClinVar ClinVar several search modes. (1) you can simply type free form text in the search box near the top of every page, (2) if you know the neccessary field codes, you can construct complex queries in this same search box, (3) you can use the Advanced Search Builder. Use the basic search box to find all variants for the gene AKT1. Note that when ClinVar detects a match to a proper gene name in your search, it assumes you want to search against only the gene name field. It indicates this by adding [gene] to your search text. If you wish to search all fields for this term, you can can chose the option to “Search instead for all ClinVar records that mention AKT1”. Now try searching for a specific disease: proteus syndrome. What is the popular significance of this disease? Note that this search appears to be returning partial matches to various conditions. To get a more refined search, trying placing the condition name in quotes and adding the disease tag: “proteus syndrome”[dis]. Remember that we can find possible tags to use here on the help page. Note the variant NM_005163.2(AKT1):c.49G&gt;A (p.Glu17Lys). This variant has significance in both the germline and somatic contexts. In the germline context, it appears pathogenic for Proteus Syndrome (actually in this case, this involves somatic mosaicism). Search for variants within a narrow window on a single chromosome: “5[chr] AND 1264532:1264552[chrpos]”. What gene does this region correspond to? What is the significance of this gene? Use the Advanced Search page to construct the same query. Note that you can use the show index list option to see valid search options/examples for each field. Use the Advanced Search page to construct a complex query. For example: Variants submitted by “Counsyl, that are “duplication variants”, for the disease “Renal carnitine transport defect”, and with review status “criteria provided, single submitter”. Obtaining a list of all high quality predisposing variants in ClinVar If you just want the complete list of high quality variants that you might consider returning results for in a clinical setting. You might decide to peform a query that requires the variant to have been reviewed by an expert panel, belong to a practice guideline, or have been submitted by multiple entities that agree on the interpretation. This list could be further limited to those that are Pathogenic and where the variant origin is stated as “germline”. If you put all of this together the query looks like this: ((("reviewed by expert panel"[Review status]) OR "criteria provided, multiple submitters, no conflicts"[Review status]) OR "practice guideline"[Review status]) AND "clinsig pathogenic"[Properties] AND "germline"[Origin] Download this complete data set to a tabular (TSV) file and open it is a spreadsheet editor such as Excel. A brief primer on HGVS In completing the exercises above in ClinVar you will have noticed that variants have rather complex looking names. This is a deliberate choice on the part of ClinVar. It is common in the literature and in day to day conversations to refer to variants with shorthand names. For example, someone refers to BRAF V600E you may have a good idea what they are referring to. However, such names can be ambigiguous. There are often multiple correct variations in the genome that could lead to a V600E effect in the protein. Furthermore, if you want to design an assay to detect this variant in RNA, cDNA, or genomic DNA, then V600E is not very useful. For this reason, the community has adopted a set of standards for describing variants in a much more precise way that is unambiguous and leads to less confusion. The Human Genome Variation Society (HGVS) acts as a steward for these standards. They have also released detailed guidelines (the rulebook) on the correct way to create HGVS expressions for almost any variant. These guidelines are maintained, updated, and versioned. They are available at: varnomen.hgvs.org. ClinVar also provides a simple overview of the HGVS types they use. Here is an example of the same variant (AKT G17K) represented as HGVS expressions describing it at the: ClinVar variant name: NM_005163.2(AKT1):c.49G&gt;A (p.Glu17Lys) HGVS for protein: NP_005154.2:p.Glu17Lys HGVS for cDNA: NM_005163.2:c.49G&gt;A HGVS for genome: NC_000014.9:g.104780214C&gt;T (GRCh38) Each HGVS has several required elements. First you have a sequence accession with a version number (e.g. NM_005163.2). This tells you unambiguously what the reference sequence is (a published protein, cDNA, or genome sequence). Then you have a delimiter “:”. Next you have a letter that indicates the general type of HGVS expression to follow: “p” for protein, “c” for cDNA, “g” for genomic DNA. Finally, you have the expression that describes the sequence variation itself. The rules for these expressions depend on the type of variation (substitution, duplication, deletion, etc.) and the level of HGVS (protein, cDNA, gDNA, RNA, …). As an exercise. Try manually creating valid HGVS expressions at the genome, cDNA, and protein level for: BRAF V600E. What fundamental tools would we need to do this (assuming we can not just look it up in a database)? Here are some excellent resources for working with HGVS: HGVS guidelines Mutalyzer (tools for validating and converting HGVS expressions) TransVar (tool for converting HGVS expressions) MyVariantInfo (aggregates variant info including HGVS expressions from many sources) A brief primer on the ACMG guidelines for germline variant interpretation You may have noticed in the exercises above various references to the clinical significance or pathogenicity of variants. In some queries of ClinVar we limited variants to only those that are thought to be Pathogenic (i.e. if inherited, the predispose an individual to a particular disease). How does one establish whether a variant is Pathogenic, or Benign? To help establish how this should be done, the ACMG has released detailed Standards and guidelines for the interpretation of sequence variants. For the full details, one should definitely read that paper and refer back to it extensively as you try to interepret specific variants. Briefly, the ACMG established a variant classification approach that is meant to be applicable to variants in all Mendelian genes. They categorize the types of evidence that one should use to support the pathogenicity of a variant. Each type of evidence is indicated by an evidence code. Evidence codes are broken down into two main categories, those that support a pathogenic interpretation, and those that support a benign interpretation. Within these two broad groups, sub-categories are defined for different types of evidence (e.g. functional data, population frequency, inheritance patterns, algorithmic predictions, etc.). The guidelines describe how to document this evidence and how to use that evidence to make a final conclusion for the pathogenicity of each variant for each disease/phenotype. The ACMG defines five tiers for the pathogenicity of a variant: “pathogenic,” “likely pathogenic,” “uncertain significance,” “likely benign,” and “benign”. We strongly encourage you to read the ACMG guidelines carefully. The critical summaries are provided in several tables of this paper. Evidence and codes for classifying pathogenic variants: Table 3 Evidence and codes for classifying benign variants: Table 4 Rules for combining evidence to classify sequence variants: Table 5 Final comment: If used properly, the ACMG guidelines require a lot of evidence to define a variant as pathogenic or even likely pathgenic. Similarly strong evidence is needed to conclude a variant is benign or even likely benign. Many variants are thus initially classified as “uncertain significance”. Note that this does not mean unknown it means uncertain. i.e. lacking certainty. Since variant classifications are meant to be used in a clinical setting the goal is to avoid misinforming patients that may take life changing actions upon hearing these results. ClinVar practice exercises How many ClinVar variants are there for BRCA2 that are Germline, Pathogenic and have Expert Panel review status? Get a hint! Simply enter BRCA2 in the ClinVar search box, then apply the three filters requested. Answer At the time of writing this post, there were 2069 germline, pathogenic, expert panel reviewed BRCA2 variants. Use ClinVar to find valid HGVS expressions at the genome, cDNA, and protein level for the following variant: PIK3CA H1047R. Get a hint! Simply enter PIK3CA H1047R in the ClinVar search box, then review the HGVS section. Answer NG_012113.2:g.90775A&gt;G (genome), NM_006218.3:c.3140A&gt;G (cDNA), NP_006209.2:p.His1047Arg (protein). Find all of the variants in ClinVar that correspond to exon 2 of VHL. How many are there? Export these to a spreadsheet. Get a hint! Use a genome browser such as IGV to obtain the build38 coordinates for exon 2 of VHL. Then use the advanced ClinVar search box to search for variants in VHL and within the exon 2 coordinates. Answer To get VHL exon 2 variants use this query. (VHL[Gene Name]) AND 10146514:10146636[Base Position]. At the time this question was created there were 517 variants in VHL and 63 of these were within exon 2.]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://www.genviz.org//assets/DNA.jpg" /><media:content medium="image" url="http://www.genviz.org//assets/DNA.jpg" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Variant annotation with VEP</title><link href="http://www.genviz.org//module-05-annotation/0005/02/01/VEP/" rel="alternate" type="text/html" title="Variant annotation with VEP" /><published>0005-02-01T00:00:00+00:00</published><updated>0005-02-01T00:00:00+00:00</updated><id>http://www.genviz.org//module-05-annotation/0005/02/01/VEP</id><content type="html" xml:base="http://www.genviz.org//module-05-annotation/0005/02/01/VEP/"><![CDATA[<p>Often it will be informative to annotate variants with additional information in order to get a sense of a variants impact on a phenotype. One tool that makes this process quick and straightforward is the ensembl <a href="http://www.ensembl.org/info/docs/tools/vep/index.html">Variant Effect Predictor</a> (VEP). This program is available both as a stand alone software program based in <a href="https://www.perl.org/">perl</a> and as a web based GUI. In this module we will learn how to use <a href="http://www.ensembl.org/info/docs/tools/vep/index.html">VEP</a> in both forms.</p>

<h3 id="installing-perl">Installing perl</h3>
<h5 id="mac-and-unix">MAC and UNIX</h5>
<p>In order to use stand alone <a href="http://www.ensembl.org/info/docs/tools/vep/index.html">VEP</a> we will first need to download and install <a href="https://www.perl.org/">perl</a>, a high level scripting language. First let’s check if you have perl ≥ 5.10 already installed, open a command prompt “terminal” on your local machine and run the code below.</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c"># check perl version</span>
perl <span class="nt">-v</span>
</code></pre></div></div>
<p>If you see a message to the effect of “This is perl 5” then you can ignore the next bit, otherwise you will need to download and install perl. To do this navigate to the perl downloads page at <a href="https://www.perl.org/get.html">https://www.perl.org/get.html</a> and select <strong>get started</strong> for your specific operating system.</p>

<figure class="figure" width="650">
    <img class="image " src="/assets/VEP/downloadPerl.png" width="650" />
    
    
</figure>

<p>Then select activeState perl, once the installer is downloaded follow the on screen instructions and check your install with <code class="language-plaintext highlighter-rouge">perl -v</code> from a terminal window.</p>

<figure class="figure" width="750">
    <img class="image " src="/assets/VEP/DownloadActivestatePerl.png" width="750" />
    
    
</figure>

<h5 id="windows">Windows</h5>
<p>In order to use <a href="http://www.ensembl.org/info/docs/tools/vep/index.html">VEP</a> on widnows we will first need to download and install a special flavor of perl called DWIMperl. Navigate to <a href="http://dwimperl.com/windows.html">http://dwimperl.com/windows.html</a> and download the “Dwimperl-5.14.2.1-v7-32.exe” executable at the bottom of the page. Then run the executable and follow the on screen instructions. Once finished search and open <strong>Command Prompt</strong> from the “Start Menu” and type <code class="language-plaintext highlighter-rouge">perl -v</code> to check that the installation was successful.</p>

<figure class="figure" width="650">
    <img class="image " src="/assets/VEP/downloadDWIMperl.png" width="650" />
    
    
</figure>

<h3 id="installing-vep">Installing VEP</h3>
<p>With <a href="https://www.perl.org/">perl</a> now installed we can go ahead and download and install <a href="http://www.ensembl.org/info/docs/tools/vep/index.html">VEP</a> itself. The recommended way to do this is by cloning the github repo available <a href="https://github.com/Ensembl/ensembl-vep">here</a>. We won’t actually be cloning the repo, though you certainly could. Instead select the green <strong>Clone or download</strong> button to the right and then select <strong>Download Zip</strong>. Once the file is downloaded go ahead and unzip it.</p>

<figure class="figure" width="750">
    <img class="image " src="/assets/VEP/downloadVep.png" width="750" />
    
    
</figure>

<p>Once that is complete you will need to navigate to the directory where you unziped the <a href="http://www.ensembl.org/info/docs/tools/vep/index.html">VEP</a> repo and run the code below.</p>

<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c"># change to directory where vep is</span>
<span class="nb">cd</span> ./ensembl-vep-release-90

<span class="c"># initate VEP installations</span>
perl INSTALL.pl <span class="nt">--NO_HTSLIB</span> <span class="nt">--NO_TEST</span>
</code></pre></div></div>

<p>This will start the <a href="http://www.ensembl.org/info/docs/tools/vep/index.html">VEP</a> installation process. You will be asked if you want to “download local cache files”, “download fasta files”, and “install plugins”. Because of their size we will answer “no” to all these however you can always change this by re-running <code class="language-plaintext highlighter-rouge">perl INSTALL.pl</code>.</p>

<figure class="figure" width="550">
    <img class="image " src="/assets/VEP/vepInstall.png" width="550" />
    
    
</figure>

<p>Finally let’s check our <a href="http://www.ensembl.org/info/docs/tools/vep/index.html">VEP</a> installation, from the same directory you ran <code class="language-plaintext highlighter-rouge">perl INSTALL.pl</code> run <code class="language-plaintext highlighter-rouge">./vep --help</code>. If everything went okay you should see some <a href="http://www.ensembl.org/info/docs/tools/vep/index.html">VEP</a> version numbers appear.</p>

<figure class="figure" width="550">
    <img class="image " src="/assets/VEP/vepVersion.png" width="550" />
    
    
</figure>

<h3 id="running-vep">Running VEP</h3>
<p>As we have mentioned previously <a href="http://www.ensembl.org/info/docs/tools/vep/index.html">VEP</a> can either be run via the command line or through a web GUI. For the remainder of this section we will be doing both side by side however we should note that using <a href="http://www.ensembl.org/info/docs/tools/vep/index.html">VEP</a> from the command line is more flexible and there is a greater range of features.</p>

<p>To start let’s go ahead and annotate the germline variant <a href="https://www.ncbi.nlm.nih.gov/SNP/snp_ref.cgi?type=rs&amp;rs=rs1799966">rs1799966</a> using the default <a href="https://www.ensembl.org/info/docs/tools/vep/vep_formats.html">ensembl input format</a>. Essentially we need to create a file with 5 columns corresponding to “chromosome”, “start” (1-based), “stop” (1-based), “reference/variant”, and “strand” (corresponding to reference/variant). You can optionally have a sixth column to add a unique identifier for this row. After creating the file <code class="language-plaintext highlighter-rouge">variant.txt</code> we run <a href="http://www.ensembl.org/info/docs/tools/vep/index.html">VEP</a> with the following options.</p>

<p>NOTE: A common source of confusion is regarding the strand specified as input for VEP. This is not the strand of any specific gene/transcript. Rather it represents the strand of the reference genome and should match the reference/variant alleles specified for each variant. By convention, variant callers typically report all variants relative to the positive strand. However there may be some cases where you are starting with already annotated variants and therefore have c. notation and/or have gene- or transcript-specific strand available. Be careful not to input genomic positions with variant alleles and strand mismatched (e.g, using the positive strand reference and variant allele basesfrom the reference genome together with a negative transcript).</p>

<ol>
  <li><code class="language-plaintext highlighter-rouge">-i</code> input file, the file format is automatically detected</li>
  <li><code class="language-plaintext highlighter-rouge">-o</code> output file to write results</li>
  <li><code class="language-plaintext highlighter-rouge">--database</code> make queries to public ensembl databases instead of looking for local copies</li>
  <li><code class="language-plaintext highlighter-rouge">--species</code> species for which annotations should be obtained</li>
  <li><code class="language-plaintext highlighter-rouge">--everything</code> flag to output additional annotations</li>
</ol>

<div class="language-R highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># make a file with a single variant in</span><span class="w">
</span><span class="n">echo</span><span class="w"> </span><span class="s2">"17 43071077 43071077 T/C + variant_1"</span><span class="w"> </span><span class="o">&gt;</span><span class="w"> </span><span class="n">variant.txt</span><span class="w">

</span><span class="c1"># run VEP</span><span class="w">
</span><span class="n">.</span><span class="o">/</span><span class="n">vep</span><span class="w"> </span><span class="o">-</span><span class="n">i</span><span class="w"> </span><span class="n">variant.txt</span><span class="w"> </span><span class="o">-</span><span class="n">o</span><span class="w"> </span><span class="n">variant.anno.txt</span><span class="w"> </span><span class="o">--</span><span class="n">database</span><span class="w"> </span><span class="o">--</span><span class="n">species</span><span class="w"> </span><span class="s2">"human"</span><span class="w"> </span><span class="o">--</span><span class="n">everything</span><span class="w">
</span></code></pre></div></div>

<p>We can do the same thing through the web interface, navigate to the vep homepage at <a href="http://www.ensembl.org/info/docs/tools/vep/index.html">http://www.ensembl.org/info/docs/tools/vep/index.html</a>, and click on <strong>Launch VEP</strong>.</p>

<figure class="figure" width="550">
    <img class="image " src="/assets/VEP/runVep_v0.png" width="550" />
    
    
</figure>

<p>Then input your variant and click on <strong>Run</strong>.</p>

<figure class="figure" width="650">
    <img class="image " src="/assets/VEP/runVep_v1.png" width="650" />
    
    
</figure>

<p>This will submit the job to ensembl servers, the page will refresh every few seconds. When the job completes click on <strong>view results</strong>.</p>

<figure class="figure" width="750">
    <img class="image " src="/assets/VEP/runVep_v2.png" width="750" />
    
    
</figure>

<p>Doing so will take you to a web page where you will be able to view summary statistics, the results, and options to filter or export a file.</p>

<figure class="figure" width="750">
    <img class="image " src="/assets/VEP/runVep_v3.png" width="750" />
    
    
</figure>

<p>You have probably noticed from your exploration of the results that even though only one variant was supplied multiple rows were output. By default <a href="http://www.ensembl.org/info/docs/tools/vep/index.html">VEP</a> returns annotations for each “transcript” and each “variant consequence”. While this is informative it is often desireable to only have one annotation per gene. We can achieve this on our command line by adding the <code class="language-plaintext highlighter-rouge">--per_gene</code> parameter. The same thing can be achieved through the web interface by expanding the <strong>filtering options</strong> tab and setting <strong>Restrict results</strong> to <strong>Show one selected consequence per gene</strong>. Go ahead and do that now through either the command line or web GUI.</p>

<figure class="figure" width="750">
    <img class="image " src="/assets/VEP/runVep_v4.png" width="750" />
    
    
</figure>

<p>There are many features through both forms of <a href="http://www.ensembl.org/info/docs/tools/vep/index.html">VEP</a>, to many to cover in it’s entirety for this course. However extensive documentation for the web based version is available <a href="http://www.ensembl.org/info/docs/tools/vep/online/index.html">here</a> and the stand alone  perl script available <a href="http://www.ensembl.org/info/docs/tools/vep/script/index.html">here</a>.</p>

<h3 id="exercises">Exercises</h3>

<p>Now that you have an inital <a href="http://www.ensembl.org/info/docs/tools/vep/index.html">VEP</a> result try and answer a few questions about your data. If you need help in understanding a certain column hover over it with your cursor or look at the <a href="http://www.ensembl.org/info/docs/tools/vep/index.html">VEP</a> documentation available <a href="http://www.ensembl.org/info/docs/tools/vep/vep_formats.html#output">here</a>.</p>

<p>What is the maximum allele frequency observed for this vairant in the 1000 genomes european population?
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script type="text/javascript" src="/assets/scripts/question.js"></script></p>

<div class="accordion">
    <p class="q">Answer</p>
    <p class="a">0.3598</p>
</div>

<p>In what gene is this variant located?
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script type="text/javascript" src="/assets/scripts/question.js"></script></p>

<div class="accordion">
    <p class="q">Answer</p>
    <p class="a">BRCA1</p>
</div>

<p>What is the amino acid change for this variant?
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script type="text/javascript" src="/assets/scripts/question.js"></script></p>

<div class="accordion">
    <p class="q">Answer</p>
    <p class="a">S/G</p>
</div>]]></content><author><name>Zachary Skidmore</name></author><category term="Module-05-Annotation" /><summary type="html"><![CDATA[Often it will be informative to annotate variants with additional information in order to get a sense of a variants impact on a phenotype. One tool that makes this process quick and straightforward is the ensembl Variant Effect Predictor (VEP). This program is available both as a stand alone software program based in perl and as a web based GUI. In this module we will learn how to use VEP in both forms. Installing perl MAC and UNIX In order to use stand alone VEP we will first need to download and install perl, a high level scripting language. First let’s check if you have perl ≥ 5.10 already installed, open a command prompt “terminal” on your local machine and run the code below. # check perl version perl -v If you see a message to the effect of “This is perl 5” then you can ignore the next bit, otherwise you will need to download and install perl. To do this navigate to the perl downloads page at https://www.perl.org/get.html and select get started for your specific operating system. Then select activeState perl, once the installer is downloaded follow the on screen instructions and check your install with perl -v from a terminal window. Windows In order to use VEP on widnows we will first need to download and install a special flavor of perl called DWIMperl. Navigate to http://dwimperl.com/windows.html and download the “Dwimperl-5.14.2.1-v7-32.exe” executable at the bottom of the page. Then run the executable and follow the on screen instructions. Once finished search and open Command Prompt from the “Start Menu” and type perl -v to check that the installation was successful. Installing VEP With perl now installed we can go ahead and download and install VEP itself. The recommended way to do this is by cloning the github repo available here. We won’t actually be cloning the repo, though you certainly could. Instead select the green Clone or download button to the right and then select Download Zip. Once the file is downloaded go ahead and unzip it. Once that is complete you will need to navigate to the directory where you unziped the VEP repo and run the code below. # change to directory where vep is cd ./ensembl-vep-release-90 # initate VEP installations perl INSTALL.pl --NO_HTSLIB --NO_TEST This will start the VEP installation process. You will be asked if you want to “download local cache files”, “download fasta files”, and “install plugins”. Because of their size we will answer “no” to all these however you can always change this by re-running perl INSTALL.pl. Finally let’s check our VEP installation, from the same directory you ran perl INSTALL.pl run ./vep --help. If everything went okay you should see some VEP version numbers appear. Running VEP As we have mentioned previously VEP can either be run via the command line or through a web GUI. For the remainder of this section we will be doing both side by side however we should note that using VEP from the command line is more flexible and there is a greater range of features. To start let’s go ahead and annotate the germline variant rs1799966 using the default ensembl input format. Essentially we need to create a file with 5 columns corresponding to “chromosome”, “start” (1-based), “stop” (1-based), “reference/variant”, and “strand” (corresponding to reference/variant). You can optionally have a sixth column to add a unique identifier for this row. After creating the file variant.txt we run VEP with the following options. NOTE: A common source of confusion is regarding the strand specified as input for VEP. This is not the strand of any specific gene/transcript. Rather it represents the strand of the reference genome and should match the reference/variant alleles specified for each variant. By convention, variant callers typically report all variants relative to the positive strand. However there may be some cases where you are starting with already annotated variants and therefore have c. notation and/or have gene- or transcript-specific strand available. Be careful not to input genomic positions with variant alleles and strand mismatched (e.g, using the positive strand reference and variant allele basesfrom the reference genome together with a negative transcript). -i input file, the file format is automatically detected -o output file to write results --database make queries to public ensembl databases instead of looking for local copies --species species for which annotations should be obtained --everything flag to output additional annotations # make a file with a single variant in echo "17 43071077 43071077 T/C + variant_1" &gt; variant.txt # run VEP ./vep -i variant.txt -o variant.anno.txt --database --species "human" --everything We can do the same thing through the web interface, navigate to the vep homepage at http://www.ensembl.org/info/docs/tools/vep/index.html, and click on Launch VEP. Then input your variant and click on Run. This will submit the job to ensembl servers, the page will refresh every few seconds. When the job completes click on view results. Doing so will take you to a web page where you will be able to view summary statistics, the results, and options to filter or export a file. You have probably noticed from your exploration of the results that even though only one variant was supplied multiple rows were output. By default VEP returns annotations for each “transcript” and each “variant consequence”. While this is informative it is often desireable to only have one annotation per gene. We can achieve this on our command line by adding the --per_gene parameter. The same thing can be achieved through the web interface by expanding the filtering options tab and setting Restrict results to Show one selected consequence per gene. Go ahead and do that now through either the command line or web GUI. There are many features through both forms of VEP, to many to cover in it’s entirety for this course. However extensive documentation for the web based version is available here and the stand alone perl script available here. Exercises Now that you have an inital VEP result try and answer a few questions about your data. If you need help in understanding a certain column hover over it with your cursor or look at the VEP documentation available here. What is the maximum allele frequency observed for this vairant in the 1000 genomes european population? Answer 0.3598 In what gene is this variant located? Answer BRCA1 What is the amino acid change for this variant? Answer S/G]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://www.genviz.org//assets/DNA.jpg" /><media:content medium="image" url="http://www.genviz.org//assets/DNA.jpg" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Variant annotation and interpretation</title><link href="http://www.genviz.org//module-05-annotation/0005/01/01/Variant_Annotation_and_Interpretation/" rel="alternate" type="text/html" title="Variant annotation and interpretation" /><published>0005-01-01T00:00:00+00:00</published><updated>0005-01-01T00:00:00+00:00</updated><id>http://www.genviz.org//module-05-annotation/0005/01/01/Variant_Annotation_and_Interpretation</id><content type="html" xml:base="http://www.genviz.org//module-05-annotation/0005/01/01/Variant_Annotation_and_Interpretation/"><![CDATA[<p>When variants are identified in the genome (or transcriptome) some kind of annotation and need for interpretation invariably follows. There are many, many tools for annotation and interpretation in different contexts and for different purposes. In this section we explore just a few of these many options. First we will learn to use Ensembl’s Variant Effect Predictor (VEP), a popular and widely used variant transcript annotator. VEP has many functions, but it is first used to annotate variants in the context of set of known transcripts. The other resources we will use, ClinVar and CIViC attempt to summarize evidence for the clinical relevance of variants in inherited human diseases and cancer respectively.</p>

<p>Some here are some examples of variant annotation and interpretation contexts:</p>
<ul>
  <li>Population frequency/recurrence (is the variant common, rare, rare?)</li>
  <li>Transcripts (does the variant occur within a transcribed region of a gene? Does is affect the predicted translation of that transcript?)</li>
  <li>Function (is the variant likely to disrupt the normal function of a gene?). There are many, many approaches to this.
    <ul>
      <li>Conservation of the affected region</li>
      <li>Predicted biochemical significance of amino acid alterations</li>
      <li>Occurence in know functional domains (e.g. the binding pocket of a kinase)</li>
      <li>Hot spots of variantion (some patterns can suggest gain-of-function)
        <ul>
          <li>2D hotspots</li>
          <li>3D hotspots</li>
        </ul>
      </li>
      <li>Patterns that suggest loss of function</li>
      <li>Actual experimental evidence for the specific variant or one very similar</li>
    </ul>
  </li>
  <li>What other approaches can you think of?</li>
</ul>

<p><a href="https://github.com/griffithlab/gen-viz-lectures/raw/master/GenViz_Module5_Lecture.pdf">Module 5 Lecture</a></p>]]></content><author><name>Zachary Skidmore</name></author><category term="Module-05-Annotation" /><summary type="html"><![CDATA[When variants are identified in the genome (or transcriptome) some kind of annotation and need for interpretation invariably follows. There are many, many tools for annotation and interpretation in different contexts and for different purposes. In this section we explore just a few of these many options. First we will learn to use Ensembl’s Variant Effect Predictor (VEP), a popular and widely used variant transcript annotator. VEP has many functions, but it is first used to annotate variants in the context of set of known transcripts. The other resources we will use, ClinVar and CIViC attempt to summarize evidence for the clinical relevance of variants in inherited human diseases and cancer respectively. Some here are some examples of variant annotation and interpretation contexts: Population frequency/recurrence (is the variant common, rare, rare?) Transcripts (does the variant occur within a transcribed region of a gene? Does is affect the predicted translation of that transcript?) Function (is the variant likely to disrupt the normal function of a gene?). There are many, many approaches to this. Conservation of the affected region Predicted biochemical significance of amino acid alterations Occurence in know functional domains (e.g. the binding pocket of a kinase) Hot spots of variantion (some patterns can suggest gain-of-function) 2D hotspots 3D hotspots Patterns that suggest loss of function Actual experimental evidence for the specific variant or one very similar What other approaches can you think of? Module 5 Lecture]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://www.genviz.org//assets/DNA.jpg" /><media:content medium="image" url="http://www.genviz.org//assets/DNA.jpg" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Pathway visualization</title><link href="http://www.genviz.org//module-04-expression/0004/04/01/PathwayVisualization/" rel="alternate" type="text/html" title="Pathway visualization" /><published>0004-04-01T00:00:00+00:00</published><updated>0004-04-01T00:00:00+00:00</updated><id>http://www.genviz.org//module-04-expression/0004/04/01/PathwayVisualization</id><content type="html" xml:base="http://www.genviz.org//module-04-expression/0004/04/01/PathwayVisualization/"><![CDATA[<p>A common task after pathway analysis is contructing visualizations to represent experimental data for pathways of interest. There are many tools for this. We will focus on the bioconductor <a href="https://bioconductor.org/packages/release/bioc/html/pathview.html">pathview</a> package for this task.</p>

<h3 id="pathview">Pathview</h3>
<p><a href="https://bioconductor.org/packages/release/bioc/html/pathview.html">Pathview</a> is used to integrate and display data on KEGG pathway maps that it retrieves through API queries to the <a href="http://www.genome.jp/kegg/">KEGG</a> database. Please refer to the pathview <a href="https://bioconductor.org/packages/release/bioc/vignettes/pathview/inst/doc/pathview.pdf">vignette</a> and <a href="http://www.genome.jp/kegg/">KEGG</a> website for license information as there may be restrictions for commercial use due for these API queries. <a href="https://bioconductor.org/packages/release/bioc/html/pathview.html">Pathview</a> itself is open source and is able to map a wide variety of biological data relevant to pathway views. In this section we will be mapping the overall expression results for a few pathways from the <a href="http://genviz.org/module%203/0008/01/01/pathwayAnalysis/">pathway analysis</a> section of this course. Let’s start by installing <a href="https://bioconductor.org/packages/release/bioc/html/pathview.html">pathview</a> from bioconductor and loading the data we created in the previous section.</p>

<div class="language-R highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Install pathview from bioconductor</span><span class="w">
</span><span class="n">source</span><span class="p">(</span><span class="s2">"https://bioconductor.org/biocLite.R"</span><span class="p">)</span><span class="w">
</span><span class="n">biocLite</span><span class="p">(</span><span class="s2">"pathview"</span><span class="p">)</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">pathview</span><span class="p">)</span><span class="w">

</span><span class="n">load</span><span class="p">(</span><span class="n">url</span><span class="p">(</span><span class="s2">"http://genomedata.org/gen-viz-workshop/pathway_visualization/pathview_Data.RData"</span><span class="p">))</span><span class="w">
</span></code></pre></div></div>

<h3 id="visualizing-kegg-pathways">Visualizing KEGG pathways</h3>
<p>Now that we have our initial data loaded let’s choose a few pathways to visualize. The “Mismatch repair” repair pathway is significantly perturbed by up regulated genes, and corresponds to the following kegg id: “hsa03430”. We can view this using the row names of the pathway dataset <code class="language-plaintext highlighter-rouge">fc.kegg.sigmet.p.up</code>. Let’s use our experiment’s expression in the data frame <code class="language-plaintext highlighter-rouge">tumor_v_normal_DE.fc</code> and view it in the context of this pathway. Two graphs will be written to your current working directory by the <a href="https://www.rdocumentation.org/packages/pathview/versions/1.12.0/topics/pathview">pathview()</a> function, one will be the original kegg pathway view and the second one will have expression values overlayed (see below). You can find your current working directory with the function <a href="https://www.rdocumentation.org/packages/base/versions/3.4.1/topics/getwd">getwd()</a>.</p>

<div class="language-R highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># View the hsa03430 pathway from the pathway analysis</span><span class="w">
</span><span class="n">fc.kegg.sigmet.p.up</span><span class="p">[</span><span class="n">grepl</span><span class="p">(</span><span class="s2">"hsa03430"</span><span class="p">,</span><span class="w"> </span><span class="n">rownames</span><span class="p">(</span><span class="n">fc.kegg.sigmet.p.up</span><span class="p">),</span><span class="w"> </span><span class="n">fixed</span><span class="o">=</span><span class="kc">TRUE</span><span class="p">),]</span><span class="w">

</span><span class="c1"># Overlay the expression data onto this pathway</span><span class="w">
</span><span class="n">pathview</span><span class="p">(</span><span class="n">gene.data</span><span class="o">=</span><span class="n">tumor_v_normal_DE.fc</span><span class="p">,</span><span class="w"> </span><span class="n">species</span><span class="o">=</span><span class="s2">"hsa"</span><span class="p">,</span><span class="w"> </span><span class="n">pathway.id</span><span class="o">=</span><span class="s2">"hsa03430"</span><span class="p">)</span><span class="w">
</span></code></pre></div></div>

<figure class="figure" width="800">
    <img class="image " src="/assets/pathway_visualization/hsa03430_pathview_expression.png" width="800" />
    
    
</figure>

<p>It is often nice to see the relationship between genes in the kegg pathview diagrams, this can be achieved by setting the parameter <code class="language-plaintext highlighter-rouge">kegg.native=FALSE</code>. Below we show an example for the Fanconi anemia pathway.</p>

<div class="language-R highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># View the hsa03430 pathway from the pathway analysis</span><span class="w">
</span><span class="n">fc.kegg.sigmet.p.up</span><span class="p">[</span><span class="n">grepl</span><span class="p">(</span><span class="s2">"hsa03460"</span><span class="p">,</span><span class="w"> </span><span class="n">rownames</span><span class="p">(</span><span class="n">fc.kegg.sigmet.p.up</span><span class="p">),</span><span class="w"> </span><span class="n">fixed</span><span class="o">=</span><span class="kc">TRUE</span><span class="p">),]</span><span class="w">

</span><span class="c1"># Overlay the expression data onto this pathway</span><span class="w">
</span><span class="n">pathview</span><span class="p">(</span><span class="n">gene.data</span><span class="o">=</span><span class="n">tumor_v_normal_DE.fc</span><span class="p">,</span><span class="w"> </span><span class="n">species</span><span class="o">=</span><span class="s2">"hsa"</span><span class="p">,</span><span class="w"> </span><span class="n">pathway.id</span><span class="o">=</span><span class="s2">"hsa03460"</span><span class="p">,</span><span class="w"> </span><span class="n">kegg.native</span><span class="o">=</span><span class="kc">FALSE</span><span class="p">)</span><span class="w">
</span></code></pre></div></div>

<figure class="figure" width="800">
    <img class="image " src="/assets/pathway_visualization/hsa03460_pathview_relationships.png" width="800" />
    
    
</figure>]]></content><author><name>Zachary Skidmore</name></author><category term="Module-04-Expression" /><summary type="html"><![CDATA[A common task after pathway analysis is contructing visualizations to represent experimental data for pathways of interest. There are many tools for this. We will focus on the bioconductor pathview package for this task. Pathview Pathview is used to integrate and display data on KEGG pathway maps that it retrieves through API queries to the KEGG database. Please refer to the pathview vignette and KEGG website for license information as there may be restrictions for commercial use due for these API queries. Pathview itself is open source and is able to map a wide variety of biological data relevant to pathway views. In this section we will be mapping the overall expression results for a few pathways from the pathway analysis section of this course. Let’s start by installing pathview from bioconductor and loading the data we created in the previous section. # Install pathview from bioconductor source("https://bioconductor.org/biocLite.R") biocLite("pathview") library(pathview) load(url("http://genomedata.org/gen-viz-workshop/pathway_visualization/pathview_Data.RData")) Visualizing KEGG pathways Now that we have our initial data loaded let’s choose a few pathways to visualize. The “Mismatch repair” repair pathway is significantly perturbed by up regulated genes, and corresponds to the following kegg id: “hsa03430”. We can view this using the row names of the pathway dataset fc.kegg.sigmet.p.up. Let’s use our experiment’s expression in the data frame tumor_v_normal_DE.fc and view it in the context of this pathway. Two graphs will be written to your current working directory by the pathview() function, one will be the original kegg pathway view and the second one will have expression values overlayed (see below). You can find your current working directory with the function getwd(). # View the hsa03430 pathway from the pathway analysis fc.kegg.sigmet.p.up[grepl("hsa03430", rownames(fc.kegg.sigmet.p.up), fixed=TRUE),] # Overlay the expression data onto this pathway pathview(gene.data=tumor_v_normal_DE.fc, species="hsa", pathway.id="hsa03430") It is often nice to see the relationship between genes in the kegg pathview diagrams, this can be achieved by setting the parameter kegg.native=FALSE. Below we show an example for the Fanconi anemia pathway. # View the hsa03430 pathway from the pathway analysis fc.kegg.sigmet.p.up[grepl("hsa03460", rownames(fc.kegg.sigmet.p.up), fixed=TRUE),] # Overlay the expression data onto this pathway pathview(gene.data=tumor_v_normal_DE.fc, species="hsa", pathway.id="hsa03460", kegg.native=FALSE)]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://www.genviz.org//assets/DNA.jpg" /><media:content medium="image" url="http://www.genviz.org//assets/DNA.jpg" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Pathway analysis</title><link href="http://www.genviz.org//module-04-expression/0004/03/01/pathwayAnalysis/" rel="alternate" type="text/html" title="Pathway analysis" /><published>0004-03-01T00:00:00+00:00</published><updated>0004-03-01T00:00:00+00:00</updated><id>http://www.genviz.org//module-04-expression/0004/03/01/pathwayAnalysis</id><content type="html" xml:base="http://www.genviz.org//module-04-expression/0004/03/01/pathwayAnalysis/"><![CDATA[<p>In the previous section we examined differential expression of genes from the <a href="https://www.ncbi.nlm.nih.gov/pubmed/25049118">E-GEOD-50760</a> data set. In this section we will use the <a href="https://bioconductor.org/packages/release/bioc/html/gage.html">gage</a> package to determine if there are any coordinated differential expression patterns in the data set we used for differential expression,  <a href="https://www.ncbi.nlm.nih.gov/pubmed/25049118">E-GEOD-50760</a>.</p>

<h3 id="what-is-gage">What is gage?</h3>
<p>generally applicable gene-set enrichment (<a href="https://bioconductor.org/packages/release/bioc/html/gage.html">gage</a>) is a popular bioconductor package for performing gene-set and pathway analysis. The package works independent of sample sizes, experimental designs, assay platforms, and is applicable to both microarray and rnaseq data sets. In this section we will use <a href="https://bioconductor.org/packages/release/bioc/html/gage.html">gage</a> and gene sets from the “Kyoto Encyclopedia of Genes and Genomes” (<a href="http://www.kegg.jp/">KEGG</a>) and “Gene Ontology” (<a href="http://www.geneontology.org/">GO</a>) databases to perform pathway analysis. Let’s go ahead and install <a href="https://bioconductor.org/packages/release/bioc/html/gage.html">gage</a> and load the differential expression results from the previous section.</p>

<div class="language-R highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># install gage</span><span class="w">
</span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="o">!</span><span class="n">requireNamespace</span><span class="p">(</span><span class="s2">"BiocManager"</span><span class="p">,</span><span class="w"> </span><span class="n">quietly</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">TRUE</span><span class="p">))</span><span class="w">
    </span><span class="n">install.packages</span><span class="p">(</span><span class="s2">"BiocManager"</span><span class="p">)</span><span class="w">
</span><span class="n">BiocManager</span><span class="o">::</span><span class="n">install</span><span class="p">(</span><span class="nf">c</span><span class="p">(</span><span class="s2">"gage"</span><span class="p">,</span><span class="s2">"GO.db"</span><span class="p">,</span><span class="s2">"AnnotationDbi"</span><span class="p">,</span><span class="s2">"org.Hs.eg.db"</span><span class="p">),</span><span class="w"> </span><span class="n">version</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"3.8"</span><span class="p">)</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">gage</span><span class="p">)</span><span class="w">

</span><span class="c1"># load the differential expression results fro the previous section</span><span class="w">
</span><span class="n">load</span><span class="p">(</span><span class="n">url</span><span class="p">(</span><span class="s2">"http://genomedata.org/gen-viz-workshop/intro_to_deseq2/tutorial/deseq2Data_v1.RData"</span><span class="p">))</span><span class="w">

</span><span class="c1"># extract the results from the deseq2 data</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">DESeq2</span><span class="p">)</span><span class="w">
</span><span class="n">tumor_v_normal_DE</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">results</span><span class="p">(</span><span class="n">deseq2Data</span><span class="p">,</span><span class="w"> </span><span class="n">contrast</span><span class="o">=</span><span class="nf">c</span><span class="p">(</span><span class="s2">"tissueType"</span><span class="p">,</span><span class="w"> </span><span class="s2">"primary colorectal cancer"</span><span class="p">,</span><span class="w"> </span><span class="s2">"normal-looking surrounding colonic epithelium"</span><span class="p">))</span><span class="w">
</span></code></pre></div></div>

<h3 id="setting-up-gene-set-databases">setting up gene set databases</h3>
<p>In order to perform our pathway analysis we need a list of pathways and their respective genes. The most common databases for this type of data are <a href="http://www.kegg.jp/">KEGG</a> and <a href="http://www.geneontology.org/">GO</a>. The <a href="https://bioconductor.org/packages/release/bioc/html/gage.html">gage</a> package has two functions for querying this information in real time, <a href="https://www.rdocumentation.org/packages/gage/versions/2.22.0/topics/kegg.gsets">kegg.gsets()</a> and <a href="https://www.rdocumentation.org/packages/gage/versions/2.22.0/topics/go.gsets">go.gsets()</a>, both of which take a species as an argument and will return a list of gene sets and some helpful meta information for subsetting these list. For the <a href="http://www.kegg.jp/">KEGG</a> database object <code class="language-plaintext highlighter-rouge">kg.hsa$kg.sets</code> stores all gene sets for the queried species; <code class="language-plaintext highlighter-rouge">kg.hsa$sigmet.idx</code> and <code class="language-plaintext highlighter-rouge">kg.hsa$dise.idx</code> store the indices for those gene sets which are classified as signaling and metabolism and disease respectively. We use this information to extract a list of gene sets for the signaling and metabolism and disease subsets. A similar process is used for the <a href="http://www.geneontology.org/">GO</a> gene sets splitting the master gene set into the three gene ontologies: “Biological Process”, “Molecular Function”, and “Cellular Component”.</p>

<div class="language-R highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># set up kegg database</span><span class="w">
</span><span class="n">kg.hsa</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">kegg.gsets</span><span class="p">(</span><span class="n">species</span><span class="o">=</span><span class="s2">"hsa"</span><span class="p">)</span><span class="w">
</span><span class="n">kegg.sigmet.gs</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">kg.hsa</span><span class="o">$</span><span class="n">kg.sets</span><span class="p">[</span><span class="n">kg.hsa</span><span class="o">$</span><span class="n">sigmet.idx</span><span class="p">]</span><span class="w">
</span><span class="n">kegg.dise.gs</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">kg.hsa</span><span class="o">$</span><span class="n">kg.sets</span><span class="p">[</span><span class="n">kg.hsa</span><span class="o">$</span><span class="n">dise.idx</span><span class="p">]</span><span class="w">

</span><span class="c1"># set up go database</span><span class="w">
</span><span class="n">go.hs</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">go.gsets</span><span class="p">(</span><span class="n">species</span><span class="o">=</span><span class="s2">"human"</span><span class="p">)</span><span class="w">
</span><span class="n">go.bp.gs</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">go.hs</span><span class="o">$</span><span class="n">go.sets</span><span class="p">[</span><span class="n">go.hs</span><span class="o">$</span><span class="n">go.subs</span><span class="o">$</span><span class="n">BP</span><span class="p">]</span><span class="w">
</span><span class="n">go.mf.gs</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">go.hs</span><span class="o">$</span><span class="n">go.sets</span><span class="p">[</span><span class="n">go.hs</span><span class="o">$</span><span class="n">go.subs</span><span class="o">$</span><span class="n">MF</span><span class="p">]</span><span class="w">
</span><span class="n">go.cc.gs</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">go.hs</span><span class="o">$</span><span class="n">go.sets</span><span class="p">[</span><span class="n">go.hs</span><span class="o">$</span><span class="n">go.subs</span><span class="o">$</span><span class="n">CC</span><span class="p">]</span><span class="w">
</span></code></pre></div></div>

<h3 id="annotating-genes">annotating genes</h3>
<p>We have our gene sets now however if you look at one of these objects containing the gene sets you’ll notice that each gene set contains a series of integers. These integers are actually <a href="https://www.ncbi.nlm.nih.gov/gquery/">entrez</a> gene identifiers which presents a problem as our DESeq2 results use ensemble ID’s as gene identifiers. We will need to convert our gene identifiers to the same format before we perform the pathway analysis. Fortunately bioconductor maintains genome wide annotation data for many species, you can view these species with the <a href="https://bioconductor.org/packages/release/BiocViews.html#___OrgDb">OrgDb bioc view</a>. This makes converting the gene identifiers relatively straight forward, below we use the <a href="https://www.rdocumentation.org/packages/OrganismDbi/versions/1.14.1/topics/MultiDb-class">mapIds()</a> function to query the OrganismDb object for the gene symbol, entrez id, and gene name based on the ensembl id. Because there might not be a one to one relationship with these identifiers we also use <code class="language-plaintext highlighter-rouge">multiVals="first"</code> to specify that only the first identifier should be returned in such cases.</p>

<div class="language-R highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># load in libraries to annotate data</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">AnnotationDbi</span><span class="p">)</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">org.Hs.eg.db</span><span class="p">)</span><span class="w">

</span><span class="c1"># annotate the deseq2 results with additional gene identifiers</span><span class="w">
</span><span class="n">tumor_v_normal_DE</span><span class="o">$</span><span class="n">symbol</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">mapIds</span><span class="p">(</span><span class="n">org.Hs.eg.db</span><span class="p">,</span><span class="w"> </span><span class="n">keys</span><span class="o">=</span><span class="n">row.names</span><span class="p">(</span><span class="n">tumor_v_normal_DE</span><span class="p">),</span><span class="w"> </span><span class="n">column</span><span class="o">=</span><span class="s2">"SYMBOL"</span><span class="p">,</span><span class="w"> </span><span class="n">keytype</span><span class="o">=</span><span class="s2">"ENSEMBL"</span><span class="p">,</span><span class="w"> </span><span class="n">multiVals</span><span class="o">=</span><span class="s2">"first"</span><span class="p">)</span><span class="w">
</span><span class="n">tumor_v_normal_DE</span><span class="o">$</span><span class="n">entrez</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">mapIds</span><span class="p">(</span><span class="n">org.Hs.eg.db</span><span class="p">,</span><span class="w"> </span><span class="n">keys</span><span class="o">=</span><span class="n">row.names</span><span class="p">(</span><span class="n">tumor_v_normal_DE</span><span class="p">),</span><span class="w"> </span><span class="n">column</span><span class="o">=</span><span class="s2">"ENTREZID"</span><span class="p">,</span><span class="w"> </span><span class="n">keytype</span><span class="o">=</span><span class="s2">"ENSEMBL"</span><span class="p">,</span><span class="w"> </span><span class="n">multiVals</span><span class="o">=</span><span class="s2">"first"</span><span class="p">)</span><span class="w">
</span><span class="n">tumor_v_normal_DE</span><span class="o">$</span><span class="n">name</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">mapIds</span><span class="p">(</span><span class="n">org.Hs.eg.db</span><span class="p">,</span><span class="w"> </span><span class="n">keys</span><span class="o">=</span><span class="n">row.names</span><span class="p">(</span><span class="n">tumor_v_normal_DE</span><span class="p">),</span><span class="w"> </span><span class="n">column</span><span class="o">=</span><span class="s2">"GENENAME"</span><span class="p">,</span><span class="w"> </span><span class="n">keytype</span><span class="o">=</span><span class="s2">"ENSEMBL"</span><span class="p">,</span><span class="w"> </span><span class="n">multiVals</span><span class="o">=</span><span class="s2">"first"</span><span class="p">)</span><span class="w">
</span></code></pre></div></div>

<h3 id="preparing-deseq2-results-for-gage">Preparing DESeq2 results for gage</h3>
<p>Before we perform the actuall pathway analysis we need to format our differential expression results into a format suitable for the <a href="">gage</a> package. Basically this means obtaining the normalized log2 expression values and assigning entrez gene identifiers to these values.</p>

<div class="language-R highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># grab the log fold changes for everything</span><span class="w">
</span><span class="n">tumor_v_normal_DE.fc</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">tumor_v_normal_DE</span><span class="o">$</span><span class="n">log2FoldChange</span><span class="w">
</span><span class="nf">names</span><span class="p">(</span><span class="n">tumor_v_normal_DE.fc</span><span class="p">)</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">tumor_v_normal_DE</span><span class="o">$</span><span class="n">entrez</span><span class="w">
</span></code></pre></div></div>

<h3 id="running-pathway-analysis">Running pathway analysis</h3>
<p>We can now use the <a href="https://www.rdocumentation.org/packages/gage/versions/2.22.0/topics/gage">gage()</a> function to obtain the significantly perturbed pathways from our differential expression experiment. By default the <a href="https://bioconductor.org/packages/release/bioc/html/gage.html">gage</a> package performs this analysis while taking into account up and down regulation. Setting <code class="language-plaintext highlighter-rouge">same.dir=FALSE</code> will capture pathways perturbed without taking into account direction. This is generally not recommended for the GO groups as most genes within these gene sets are regulated in the same direction, however the same is not true for KEGG pathways and using this parameter may produce informative results in such cases.</p>

<p>Note on the abbreviations below: “bp” refers to biological process, “mf” refers to molecular function, and “cc” refers to cellular process. These are the three main categories of gene ontology terms/annotations.</p>

<div class="language-R highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Run enrichment analysis on all log fc</span><span class="w">
</span><span class="n">fc.kegg.sigmet.p</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">gage</span><span class="p">(</span><span class="n">tumor_v_normal_DE.fc</span><span class="p">,</span><span class="w"> </span><span class="n">gsets</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">kegg.sigmet.gs</span><span class="p">)</span><span class="w">
</span><span class="n">fc.kegg.dise.p</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">gage</span><span class="p">(</span><span class="n">tumor_v_normal_DE.fc</span><span class="p">,</span><span class="w"> </span><span class="n">gsets</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">kegg.dise.gs</span><span class="p">)</span><span class="w">
</span><span class="n">fc.go.bp.p</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">gage</span><span class="p">(</span><span class="n">tumor_v_normal_DE.fc</span><span class="p">,</span><span class="w"> </span><span class="n">gsets</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">go.bp.gs</span><span class="p">)</span><span class="w">
</span><span class="n">fc.go.mf.p</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">gage</span><span class="p">(</span><span class="n">tumor_v_normal_DE.fc</span><span class="p">,</span><span class="w"> </span><span class="n">gsets</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">go.mf.gs</span><span class="p">)</span><span class="w">
</span><span class="n">fc.go.cc.p</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">gage</span><span class="p">(</span><span class="n">tumor_v_normal_DE.fc</span><span class="p">,</span><span class="w"> </span><span class="n">gsets</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">go.cc.gs</span><span class="p">)</span><span class="w">

</span><span class="c1"># covert the kegg results to data frames</span><span class="w">
</span><span class="n">fc.kegg.sigmet.p.up</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">as.data.frame</span><span class="p">(</span><span class="n">fc.kegg.sigmet.p</span><span class="o">$</span><span class="n">greater</span><span class="p">)</span><span class="w">
</span><span class="n">fc.kegg.dise.p.up</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">as.data.frame</span><span class="p">(</span><span class="n">fc.kegg.dise.p</span><span class="o">$</span><span class="n">greater</span><span class="p">)</span><span class="w">

</span><span class="n">fc.kegg.sigmet.p.down</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">as.data.frame</span><span class="p">(</span><span class="n">fc.kegg.sigmet.p</span><span class="o">$</span><span class="n">less</span><span class="p">)</span><span class="w">
</span><span class="n">fc.kegg.dise.p.down</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">as.data.frame</span><span class="p">(</span><span class="n">fc.kegg.dise.p</span><span class="o">$</span><span class="n">less</span><span class="p">)</span><span class="w">

</span><span class="c1"># convert the go results to data frames</span><span class="w">
</span><span class="n">fc.go.bp.p.up</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">as.data.frame</span><span class="p">(</span><span class="n">fc.go.bp.p</span><span class="o">$</span><span class="n">greater</span><span class="p">)</span><span class="w">
</span><span class="n">fc.go.mf.p.up</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">as.data.frame</span><span class="p">(</span><span class="n">fc.go.mf.p</span><span class="o">$</span><span class="n">greater</span><span class="p">)</span><span class="w">
</span><span class="n">fc.go.cc.p.up</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">as.data.frame</span><span class="p">(</span><span class="n">fc.go.cc.p</span><span class="o">$</span><span class="n">greater</span><span class="p">)</span><span class="w">

</span><span class="n">fc.go.bp.p.down</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">as.data.frame</span><span class="p">(</span><span class="n">fc.go.bp.p</span><span class="o">$</span><span class="n">less</span><span class="p">)</span><span class="w">
</span><span class="n">fc.go.mf.p.down</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">as.data.frame</span><span class="p">(</span><span class="n">fc.go.mf.p</span><span class="o">$</span><span class="n">less</span><span class="p">)</span><span class="w">
</span><span class="n">fc.go.cc.p.down</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">as.data.frame</span><span class="p">(</span><span class="n">fc.go.cc.p</span><span class="o">$</span><span class="n">less</span><span class="p">)</span><span class="w">
</span></code></pre></div></div>

<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>

<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>

<script type="text/javascript" src="/assets/scripts/question.js"></script>

<div class="accordion">
    <p class="q">Which genes are in &gt; 30% of significant pathways in the upregulated GO biological process results (q &lt;= .05)</p>
    <p class="a">Two genes are, ATM, CCNB1. Here is an <a href="http://genviz.org/assets/pathway_analysis/exercise1/exercise1_pathway_analysis.R">Rscript</a> to get the correct answer.</p>
</div>]]></content><author><name>Zachary Skidmore</name></author><category term="Module-04-Expression" /><summary type="html"><![CDATA[In the previous section we examined differential expression of genes from the E-GEOD-50760 data set. In this section we will use the gage package to determine if there are any coordinated differential expression patterns in the data set we used for differential expression, E-GEOD-50760. What is gage? generally applicable gene-set enrichment (gage) is a popular bioconductor package for performing gene-set and pathway analysis. The package works independent of sample sizes, experimental designs, assay platforms, and is applicable to both microarray and rnaseq data sets. In this section we will use gage and gene sets from the “Kyoto Encyclopedia of Genes and Genomes” (KEGG) and “Gene Ontology” (GO) databases to perform pathway analysis. Let’s go ahead and install gage and load the differential expression results from the previous section. # install gage if (!requireNamespace("BiocManager", quietly = TRUE)) install.packages("BiocManager") BiocManager::install(c("gage","GO.db","AnnotationDbi","org.Hs.eg.db"), version = "3.8") library(gage) # load the differential expression results fro the previous section load(url("http://genomedata.org/gen-viz-workshop/intro_to_deseq2/tutorial/deseq2Data_v1.RData")) # extract the results from the deseq2 data library(DESeq2) tumor_v_normal_DE &lt;- results(deseq2Data, contrast=c("tissueType", "primary colorectal cancer", "normal-looking surrounding colonic epithelium")) setting up gene set databases In order to perform our pathway analysis we need a list of pathways and their respective genes. The most common databases for this type of data are KEGG and GO. The gage package has two functions for querying this information in real time, kegg.gsets() and go.gsets(), both of which take a species as an argument and will return a list of gene sets and some helpful meta information for subsetting these list. For the KEGG database object kg.hsa$kg.sets stores all gene sets for the queried species; kg.hsa$sigmet.idx and kg.hsa$dise.idx store the indices for those gene sets which are classified as signaling and metabolism and disease respectively. We use this information to extract a list of gene sets for the signaling and metabolism and disease subsets. A similar process is used for the GO gene sets splitting the master gene set into the three gene ontologies: “Biological Process”, “Molecular Function”, and “Cellular Component”. # set up kegg database kg.hsa &lt;- kegg.gsets(species="hsa") kegg.sigmet.gs &lt;- kg.hsa$kg.sets[kg.hsa$sigmet.idx] kegg.dise.gs &lt;- kg.hsa$kg.sets[kg.hsa$dise.idx] # set up go database go.hs &lt;- go.gsets(species="human") go.bp.gs &lt;- go.hs$go.sets[go.hs$go.subs$BP] go.mf.gs &lt;- go.hs$go.sets[go.hs$go.subs$MF] go.cc.gs &lt;- go.hs$go.sets[go.hs$go.subs$CC] annotating genes We have our gene sets now however if you look at one of these objects containing the gene sets you’ll notice that each gene set contains a series of integers. These integers are actually entrez gene identifiers which presents a problem as our DESeq2 results use ensemble ID’s as gene identifiers. We will need to convert our gene identifiers to the same format before we perform the pathway analysis. Fortunately bioconductor maintains genome wide annotation data for many species, you can view these species with the OrgDb bioc view. This makes converting the gene identifiers relatively straight forward, below we use the mapIds() function to query the OrganismDb object for the gene symbol, entrez id, and gene name based on the ensembl id. Because there might not be a one to one relationship with these identifiers we also use multiVals="first" to specify that only the first identifier should be returned in such cases. # load in libraries to annotate data library(AnnotationDbi) library(org.Hs.eg.db) # annotate the deseq2 results with additional gene identifiers tumor_v_normal_DE$symbol &lt;- mapIds(org.Hs.eg.db, keys=row.names(tumor_v_normal_DE), column="SYMBOL", keytype="ENSEMBL", multiVals="first") tumor_v_normal_DE$entrez &lt;- mapIds(org.Hs.eg.db, keys=row.names(tumor_v_normal_DE), column="ENTREZID", keytype="ENSEMBL", multiVals="first") tumor_v_normal_DE$name &lt;- mapIds(org.Hs.eg.db, keys=row.names(tumor_v_normal_DE), column="GENENAME", keytype="ENSEMBL", multiVals="first") Preparing DESeq2 results for gage Before we perform the actuall pathway analysis we need to format our differential expression results into a format suitable for the gage package. Basically this means obtaining the normalized log2 expression values and assigning entrez gene identifiers to these values. # grab the log fold changes for everything tumor_v_normal_DE.fc &lt;- tumor_v_normal_DE$log2FoldChange names(tumor_v_normal_DE.fc) &lt;- tumor_v_normal_DE$entrez Running pathway analysis We can now use the gage() function to obtain the significantly perturbed pathways from our differential expression experiment. By default the gage package performs this analysis while taking into account up and down regulation. Setting same.dir=FALSE will capture pathways perturbed without taking into account direction. This is generally not recommended for the GO groups as most genes within these gene sets are regulated in the same direction, however the same is not true for KEGG pathways and using this parameter may produce informative results in such cases. Note on the abbreviations below: “bp” refers to biological process, “mf” refers to molecular function, and “cc” refers to cellular process. These are the three main categories of gene ontology terms/annotations. # Run enrichment analysis on all log fc fc.kegg.sigmet.p &lt;- gage(tumor_v_normal_DE.fc, gsets = kegg.sigmet.gs) fc.kegg.dise.p &lt;- gage(tumor_v_normal_DE.fc, gsets = kegg.dise.gs) fc.go.bp.p &lt;- gage(tumor_v_normal_DE.fc, gsets = go.bp.gs) fc.go.mf.p &lt;- gage(tumor_v_normal_DE.fc, gsets = go.mf.gs) fc.go.cc.p &lt;- gage(tumor_v_normal_DE.fc, gsets = go.cc.gs) # covert the kegg results to data frames fc.kegg.sigmet.p.up &lt;- as.data.frame(fc.kegg.sigmet.p$greater) fc.kegg.dise.p.up &lt;- as.data.frame(fc.kegg.dise.p$greater) fc.kegg.sigmet.p.down &lt;- as.data.frame(fc.kegg.sigmet.p$less) fc.kegg.dise.p.down &lt;- as.data.frame(fc.kegg.dise.p$less) # convert the go results to data frames fc.go.bp.p.up &lt;- as.data.frame(fc.go.bp.p$greater) fc.go.mf.p.up &lt;- as.data.frame(fc.go.mf.p$greater) fc.go.cc.p.up &lt;- as.data.frame(fc.go.cc.p$greater) fc.go.bp.p.down &lt;- as.data.frame(fc.go.bp.p$less) fc.go.mf.p.down &lt;- as.data.frame(fc.go.mf.p$less) fc.go.cc.p.down &lt;- as.data.frame(fc.go.cc.p$less) Which genes are in &gt; 30% of significant pathways in the upregulated GO biological process results (q &lt;= .05) Two genes are, ATM, CCNB1. Here is an Rscript to get the correct answer.]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://www.genviz.org//assets/DNA.jpg" /><media:content medium="image" url="http://www.genviz.org//assets/DNA.jpg" xmlns:media="http://search.yahoo.com/mrss/" /></entry></feed>