<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="pandoc" />


<meta name="author" content="Briana Mittleman" />


<title>Characterize orthologous peaks</title>

<script src="site_libs/jquery-1.11.3/jquery.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="site_libs/bootstrap-3.3.5/css/cosmo.min.css" rel="stylesheet" />
<script src="site_libs/bootstrap-3.3.5/js/bootstrap.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/html5shiv.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/respond.min.js"></script>
<script src="site_libs/jqueryui-1.11.4/jquery-ui.min.js"></script>
<link href="site_libs/tocify-1.9.1/jquery.tocify.css" rel="stylesheet" />
<script src="site_libs/tocify-1.9.1/jquery.tocify.js"></script>
<script src="site_libs/navigation-1.1/tabsets.js"></script>
<link href="site_libs/highlightjs-9.12.0/textmate.css" rel="stylesheet" />
<script src="site_libs/highlightjs-9.12.0/highlight.js"></script>
<link href="site_libs/font-awesome-5.0.13/css/fa-svg-with-js.css" rel="stylesheet" />
<script src="site_libs/font-awesome-5.0.13/js/fontawesome-all.min.js"></script>
<script src="site_libs/font-awesome-5.0.13/js/fa-v4-shims.min.js"></script>

<style type="text/css">code{white-space: pre;}</style>
<style type="text/css">
  pre:not([class]) {
    background-color: white;
  }
</style>
<script type="text/javascript">
if (window.hljs) {
  hljs.configure({languages: []});
  hljs.initHighlightingOnLoad();
  if (document.readyState && document.readyState === "complete") {
    window.setTimeout(function() { hljs.initHighlighting(); }, 0);
  }
}
</script>



<style type="text/css">
h1 {
  font-size: 34px;
}
h1.title {
  font-size: 38px;
}
h2 {
  font-size: 30px;
}
h3 {
  font-size: 24px;
}
h4 {
  font-size: 18px;
}
h5 {
  font-size: 16px;
}
h6 {
  font-size: 12px;
}
.table th:not([align]) {
  text-align: left;
}
</style>


</head>

<body>

<style type = "text/css">
.main-container {
  max-width: 940px;
  margin-left: auto;
  margin-right: auto;
}
code {
  color: inherit;
  background-color: rgba(0, 0, 0, 0.04);
}
img {
  max-width:100%;
  height: auto;
}
.tabbed-pane {
  padding-top: 12px;
}
.html-widget {
  margin-bottom: 20px;
}
button.code-folding-btn:focus {
  outline: none;
}
</style>


<style type="text/css">
/* padding for bootstrap navbar */
body {
  padding-top: 51px;
  padding-bottom: 40px;
}
/* offset scroll position for anchor links (for fixed navbar)  */
.section h1 {
  padding-top: 56px;
  margin-top: -56px;
}

.section h2 {
  padding-top: 56px;
  margin-top: -56px;
}
.section h3 {
  padding-top: 56px;
  margin-top: -56px;
}
.section h4 {
  padding-top: 56px;
  margin-top: -56px;
}
.section h5 {
  padding-top: 56px;
  margin-top: -56px;
}
.section h6 {
  padding-top: 56px;
  margin-top: -56px;
}
</style>

<script>
// manage active state of menu based on current page
$(document).ready(function () {
  // active menu anchor
  href = window.location.pathname
  href = href.substr(href.lastIndexOf('/') + 1)
  if (href === "")
    href = "index.html";
  var menuAnchor = $('a[href="' + href + '"]');

  // mark it active
  menuAnchor.parent().addClass('active');

  // if it's got a parent navbar menu mark it active as well
  menuAnchor.closest('li.dropdown').addClass('active');
});
</script>


<div class="container-fluid main-container">

<!-- tabsets -->
<script>
$(document).ready(function () {
  window.buildTabsets("TOC");
});
</script>

<!-- code folding -->




<script>
$(document).ready(function ()  {

    // move toc-ignore selectors from section div to header
    $('div.section.toc-ignore')
        .removeClass('toc-ignore')
        .children('h1,h2,h3,h4,h5').addClass('toc-ignore');

    // establish options
    var options = {
      selectors: "h1,h2,h3",
      theme: "bootstrap3",
      context: '.toc-content',
      hashGenerator: function (text) {
        return text.replace(/[.\\/?&!#<>]/g, '').replace(/\s/g, '_').toLowerCase();
      },
      ignoreSelector: ".toc-ignore",
      scrollTo: 0
    };
    options.showAndHide = true;
    options.smoothScroll = true;

    // tocify
    var toc = $("#TOC").tocify(options).data("toc-tocify");
});
</script>

<style type="text/css">

#TOC {
  margin: 25px 0px 20px 0px;
}
@media (max-width: 768px) {
#TOC {
  position: relative;
  width: 100%;
}
}


.toc-content {
  padding-left: 30px;
  padding-right: 40px;
}

div.main-container {
  max-width: 1200px;
}

div.tocify {
  width: 20%;
  max-width: 260px;
  max-height: 85%;
}

@media (min-width: 768px) and (max-width: 991px) {
  div.tocify {
    width: 25%;
  }
}

@media (max-width: 767px) {
  div.tocify {
    width: 100%;
    max-width: none;
  }
}

.tocify ul, .tocify li {
  line-height: 20px;
}

.tocify-subheader .tocify-item {
  font-size: 0.90em;
  padding-left: 25px;
  text-indent: 0;
}

.tocify .list-group-item {
  border-radius: 0px;
}


</style>

<!-- setup 3col/9col grid for toc_float and main content  -->
<div class="row-fluid">
<div class="col-xs-12 col-sm-4 col-md-3">
<div id="TOC" class="tocify">
</div>
</div>

<div class="toc-content col-xs-12 col-sm-8 col-md-9">




<div class="navbar navbar-default  navbar-fixed-top" role="navigation">
  <div class="container">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar">
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="index.html">comparitive_threeprime</a>
    </div>
    <div id="navbar" class="navbar-collapse collapse">
      <ul class="nav navbar-nav">
        <li>
  <a href="index.html">Home</a>
</li>
<li>
  <a href="about.html">About</a>
</li>
<li>
  <a href="license.html">License</a>
</li>
      </ul>
      <ul class="nav navbar-nav navbar-right">
        <li>
  <a href="https://github.com/jdblischak/workflowr">
    <span class="fa fa-github"></span>
     
  </a>
</li>
      </ul>
    </div><!--/.nav-collapse -->
  </div><!--/.container -->
</div><!--/.navbar -->

<!-- Add a small amount of space between sections. -->
<style type="text/css">
div.section {
  padding-top: 12px;
}
</style>

<div class="fluid-row" id="header">



<h1 class="title toc-ignore">Characterize orthologous peaks</h1>
<h4 class="author"><em>Briana Mittleman</em></h4>
<h4 class="date"><em>8/21/2018</em></h4>

</div>


<p><strong>Last updated:</strong> 2018-08-28</p>
<strong>workflowr checks:</strong> <small>(Click a bullet for more information)</small>
<ul>
<li>
<p><details> <summary> <strong style="color:blue;">✔</strong> <strong>R Markdown file:</strong> up-to-date </summary></p>
<p>Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.</p>
</details>
</li>
<li>
<p><details> <summary> <strong style="color:blue;">✔</strong> <strong>Environment:</strong> empty </summary></p>
<p>Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.</p>
</details>
</li>
<li>
<p><details> <summary> <strong style="color:blue;">✔</strong> <strong>Seed:</strong> <code>set.seed(20180801)</code> </summary></p>
<p>The command <code>set.seed(20180801)</code> was run prior to running the code in the R Markdown file. Setting a seed ensures that any results that rely on randomness, e.g. subsampling or permutations, are reproducible.</p>
</details>
</li>
<li>
<p><details> <summary> <strong style="color:blue;">✔</strong> <strong>Session information:</strong> recorded </summary></p>
<p>Great job! Recording the operating system, R version, and package versions is critical for reproducibility.</p>
</details>
</li>
<li>
<p><details> <summary> <strong style="color:blue;">✔</strong> <strong>Repository version:</strong> <a href="https://github.com/brimittleman/comparative_threeprime/tree/8f61cee52fe61dfcffc4e0a2451b13512240efa4" target="_blank">8f61cee</a> </summary></p>
Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility. The version displayed above was the version of the Git repository at the time these results were generated. <br><br> Note that you need to be careful to ensure that all relevant files for the analysis have been committed to Git prior to generating the results (you can use <code>wflow_publish</code> or <code>wflow_git_commit</code>). workflowr only checks the R Markdown file, but you know if there are other scripts or data files that it depends on. Below is the status of the Git repository when the results were generated:
<pre><code>
Ignored files:
    Ignored:    .RData
    Ignored:    .Rhistory
    Ignored:    .Rproj.user/

Untracked files:
    Untracked:  com_threeprime.Rproj
    Untracked:  data/PeakPerExon/
    Untracked:  data/PeakPerGene/
    Untracked:  data/comp.pheno.data.csv
    Untracked:  data/dist_TES/
    Untracked:  data/dist_upexon/
    Untracked:  data/liftover/
    Untracked:  data/map.stats.csv
    Untracked:  data/map.stats.xlsx
    Untracked:  data/orthoPeak_quant/

Unstaged changes:
    Deleted:    comparitive_threeprime.Rproj

</code></pre>
Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes. </details>
</li>
</ul>
<details> <summary> <small><strong>Expand here to see past versions:</strong></small> </summary>
<ul>
<table style="border-collapse:separate; border-spacing:5px;">
<thead>
<tr>
<th style="text-align:left;">
File
</th>
<th style="text-align:left;">
Version
</th>
<th style="text-align:left;">
Author
</th>
<th style="text-align:left;">
Date
</th>
<th style="text-align:left;">
Message
</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left;">
Rmd
</td>
<td style="text-align:left;">
<a href="https://github.com/brimittleman/comparative_threeprime/blob/8f61cee52fe61dfcffc4e0a2451b13512240efa4/analysis/characterize.ortho.peaks.Rmd" target="_blank">8f61cee</a>
</td>
<td style="text-align:left;">
brimittleman
</td>
<td style="text-align:left;">
2018-08-28
</td>
<td style="text-align:left;">
add plot recreating wang et al 2018
</td>
</tr>
<tr>
<td style="text-align:left;">
html
</td>
<td style="text-align:left;">
<a href="https://cdn.rawgit.com/brimittleman/comparative_threeprime/a23786505be6da399bd69014fa9a9e5e19902880/docs/characterize.ortho.peaks.html" target="_blank">a237865</a>
</td>
<td style="text-align:left;">
brimittleman
</td>
<td style="text-align:left;">
2018-08-24
</td>
<td style="text-align:left;">
Build site.
</td>
</tr>
<tr>
<td style="text-align:left;">
Rmd
</td>
<td style="text-align:left;">
<a href="https://github.com/brimittleman/comparative_threeprime/blob/364f3303353ad3cf97203e1b8294d4fa2ed3bbed/analysis/characterize.ortho.peaks.Rmd" target="_blank">364f330</a>
</td>
<td style="text-align:left;">
brimittleman
</td>
<td style="text-align:left;">
2018-08-24
</td>
<td style="text-align:left;">
TES distance
</td>
</tr>
<tr>
<td style="text-align:left;">
html
</td>
<td style="text-align:left;">
<a href="https://cdn.rawgit.com/brimittleman/comparative_threeprime/d11c2cf084c430ac589b3c8b920f9f880de87f8a/docs/characterize.ortho.peaks.html" target="_blank">d11c2cf</a>
</td>
<td style="text-align:left;">
brimittleman
</td>
<td style="text-align:left;">
2018-08-22
</td>
<td style="text-align:left;">
Build site.
</td>
</tr>
<tr>
<td style="text-align:left;">
Rmd
</td>
<td style="text-align:left;">
<a href="https://github.com/brimittleman/comparative_threeprime/blob/3a19b07d0452e0eb9a93fdc63b3ce4f239991190/analysis/characterize.ortho.peaks.Rmd" target="_blank">3a19b07</a>
</td>
<td style="text-align:left;">
brimittleman
</td>
<td style="text-align:left;">
2018-08-22
</td>
<td style="text-align:left;">
add hg19 dist
</td>
</tr>
<tr>
<td style="text-align:left;">
html
</td>
<td style="text-align:left;">
<a href="https://cdn.rawgit.com/brimittleman/comparative_threeprime/d0d459958af52d7fd26c9c046382ccde12956f39/docs/characterize.ortho.peaks.html" target="_blank">d0d4599</a>
</td>
<td style="text-align:left;">
brimittleman
</td>
<td style="text-align:left;">
2018-08-21
</td>
<td style="text-align:left;">
Build site.
</td>
</tr>
</tbody>
</table>
</ul>
<p></details></p>
<hr />
<p>I will use this analysis for QC on the orthologous peaks called in the liftover pipeline analysis.</p>
<div id="distance-to-ortho-exon" class="section level2">
<h2>Distance to ortho exon</h2>
<p>I want to make sure the distances of the orthologous peaks to the nearest exon called in Bryans ortho exon files follow a similar distribution.</p>
<p>The orthologus exon files are in /project2/gilad/briana/genome_anotation_data/ortho_exon and the small version have just chr start end and exon name.</p>
<p>The ortho peak files are in /project2/gilad/briana/comparitive_threeprime/data/ortho_peaks/</p>
<p>I want the closest exon upstream, i will use bedtools closest:</p>
<p>distUpstreamexon.sh</p>
<pre class="bash"><code>#!/bin/bash

#SBATCH --job-name=disUpstreamexon
#SBATCH --account=pi-yangili1
#SBATCH --time=24:00:00
#SBATCH --output=disUpstreamexon.out
#SBATCH --error=disUpstreamexon.err
#SBATCH --partition=broadwl
#SBATCH --mem=12G
#SBATCH --mail-type=END


module load Anaconda3
source activate comp_threeprime_env

bedtools closest -id -D a -a /project2/gilad/briana/comparitive_threeprime/data/ortho_peaks/chimpOrthoPeaks.sort.bed  -b /project2/gilad/briana/genome_anotation_data/ortho_exon/2017_July_ortho_chimp.small.sort.bed &gt; /project2/gilad/briana/comparitive_threeprime/data/dist_upexon/Chimp.distUpstreamexon.txt


bedtools closest -id -D a -a /project2/gilad/briana/comparitive_threeprime/data/ortho_peaks/humanOrthoPeaks.sort.bed -b /project2/gilad/briana/genome_anotation_data/ortho_exon/2017_July_ortho_human.small.sort.bed &gt; /project2/gilad/briana/comparitive_threeprime/data/dist_upexon/Human.distUpstreamexon.txt</code></pre>
<p>Import the files and plot the distances.</p>
<pre class="r"><code>library(tidyverse)</code></pre>
<pre><code>── Attaching packages ─────────────────────────────────────────────────── tidyverse 1.2.1 ──</code></pre>
<pre><code>✔ ggplot2 3.0.0     ✔ purrr   0.2.5
✔ tibble  1.4.2     ✔ dplyr   0.7.6
✔ tidyr   0.8.1     ✔ stringr 1.3.1
✔ readr   1.1.1     ✔ forcats 0.3.0</code></pre>
<pre><code>── Conflicts ────────────────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()</code></pre>
<pre class="r"><code>library(workflowr)</code></pre>
<pre><code>This is workflowr version 1.1.1
Run ?workflowr for help getting started</code></pre>
<pre class="r"><code>library(cowplot)</code></pre>
<pre><code>
Attaching package: &#39;cowplot&#39;</code></pre>
<pre><code>The following object is masked from &#39;package:ggplot2&#39;:

    ggsave</code></pre>
<pre class="r"><code>library(reshape2)</code></pre>
<pre><code>
Attaching package: &#39;reshape2&#39;</code></pre>
<pre><code>The following object is masked from &#39;package:tidyr&#39;:

    smiths</code></pre>
<pre class="r"><code>getwd()</code></pre>
<pre><code>[1] &quot;/Users/bmittleman1/Documents/Gilad_lab/comparitive_threeprime/com_threeprime/analysis&quot;</code></pre>
<pre class="r"><code>file.exists(&quot;../data/dist_upexon/Chimp.distUpstreamexon.txt&quot;)</code></pre>
<pre><code>[1] TRUE</code></pre>
<pre class="r"><code>chimp_dist=read.table(&quot;../data/dist_upexon/Chimp.distUpstreamexon.txt&quot;, col.names = c(&quot;peak_chr&quot;, &quot;peak_start&quot;, &quot;peak_end&quot;, &quot;peak_name&quot;, &quot;exon_chr&quot;, &quot;exon_start&quot;, &quot;exon_end&quot;, &quot;exon_name&quot;, &quot;dist&quot;), stringsAsFactors = F) %&gt;% mutate(logdis=log10(abs(dist) +1 ))

human_dist=read.table(&quot;../data/dist_upexon/Human.distUpstreamexon.txt&quot;, col.names = c(&quot;peak_chr&quot;, &quot;peak_start&quot;, &quot;peak_end&quot;, &quot;peak_name&quot;, &quot;exon_chr&quot;, &quot;exon_start&quot;, &quot;exon_end&quot;, &quot;exon_name&quot;, &quot;dist&quot;),stringsAsFactors = F, skip=1) %&gt;% mutate(logdis=log10(abs(dist) +1 ))</code></pre>
<pre class="r"><code>ch=ggplot(chimp_dist, aes(x=logdis)) + geom_density() + labs(x=&quot;log10 abs.value \n distance +1 &quot;, title=&quot;Chimp distance to ortho exon&quot;)

hu=ggplot(human_dist, aes(x=logdis)) + geom_density()+ labs(x=&quot;log10 abs.value \n distance +1 &quot;, title=&quot;Human distance to ortho exon&quot;)


plot_grid(ch, hu)</code></pre>
<p><img src="figure/characterize.ortho.peaks.Rmd/unnamed-chunk-4-1.png" width="672" style="display: block; margin: auto;" /></p>
<details> <summary><em>Expand here to see past versions of unnamed-chunk-4-1.png:</em></summary>
<table style="border-collapse:separate; border-spacing:5px;">
<thead>
<tr>
<th style="text-align:left;">
Version
</th>
<th style="text-align:left;">
Author
</th>
<th style="text-align:left;">
Date
</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left;">
<a href="https://github.com/brimittleman/comparative_threeprime/blob/d0d459958af52d7fd26c9c046382ccde12956f39/docs/figure/characterize.ortho.peaks.Rmd/unnamed-chunk-4-1.png" target="_blank">d0d4599</a>
</td>
<td style="text-align:left;">
brimittleman
</td>
<td style="text-align:left;">
2018-08-21
</td>
</tr>
</tbody>
</table>
<p></details></p>
<p>This is a good sanity check. The distributions are similar. I want to check this with the peaks from the human APAqtl study. I have the gencode exons and I will run bedtools cloests with this.</p>
<pre class="bash"><code>#!/bin/bash

#SBATCH --job-name=disUpstreamexon_hum
#SBATCH --account=pi-yangili1
#SBATCH --time=24:00:00
#SBATCH --output=disUpstreamexon_hum.out
#SBATCH --error=disUpstreamexon_hum.err
#SBATCH --partition=broadwl
#SBATCH --mem=12G
#SBATCH --mail-type=END


module load Anaconda3
source activate comp_threeprime_env

bedtools closest -id -D a -a /project2/gilad/briana/comparitive_threeprime/data/extra_anno/human_hg19_filteredPeaks.bed -b /project2/gilad/briana/comparitive_threeprime/data/extra_anno/gencode.hg19.v19.exons.bed &gt; /project2/gilad/briana/comparitive_threeprime/data/dist_upexon/hg19.humanpeaks.distUpstreamexon.txt</code></pre>
<p>update these files to remove the tab at the end.</p>
<pre class="r"><code>hg19_dist=read.table(&quot;../data/dist_upexon/hg19.humanpeaks.distUpstreamexon.txt&quot;, col.names = c(&quot;peak_chr&quot;, &quot;peak_start&quot;, &quot;peak_end&quot;, &quot;peak_cov&quot;,&quot;peak_strand&quot;, &quot;peak_score&quot;, &quot;exon_chr&quot;, &quot;exon_start&quot;, &quot;exon_end&quot;, &quot;exon_name&quot;, &quot;exon_score&quot;, &quot;exon_strand&quot;, &quot;dist&quot;),stringsAsFactors = F, skip=1) %&gt;% mutate(logdis=log10(abs(dist) +1 ))

ggplot(hg19_dist, aes(x=logdis)) + geom_density()+ labs(x=&quot;log10 abs.value \n distance +1 &quot;, title=&quot;Hg19 peaks dist to upstream exon&quot;)</code></pre>
<p><img src="figure/characterize.ortho.peaks.Rmd/unnamed-chunk-6-1.png" width="672" style="display: block; margin: auto;" /></p>
<details> <summary><em>Expand here to see past versions of unnamed-chunk-6-1.png:</em></summary>
<table style="border-collapse:separate; border-spacing:5px;">
<thead>
<tr>
<th style="text-align:left;">
Version
</th>
<th style="text-align:left;">
Author
</th>
<th style="text-align:left;">
Date
</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left;">
<a href="https://github.com/brimittleman/comparative_threeprime/blob/d11c2cf084c430ac589b3c8b920f9f880de87f8a/docs/figure/characterize.ortho.peaks.Rmd/unnamed-chunk-6-1.png" target="_blank">d11c2cf</a>
</td>
<td style="text-align:left;">
brimittleman
</td>
<td style="text-align:left;">
2018-08-22
</td>
</tr>
</tbody>
</table>
<p></details></p>
<div id="how-many-per-gene" class="section level3">
<h3>How many per gene:</h3>
<p>This is the code from the human apa qtl study. I used this to count how many peaks map to each gene. I will do this for the human and chimp here.</p>
<pre class="bash"><code>#!/bin/bash

#SBATCH --job-name=mapgene2peak2
#SBATCH --account=pi-yangili1
#SBATCH --time=24:00:00
#SBATCH --output=mapgene2peak2.out
#SBATCH --error=mapgene2peak2.err
#SBATCH --partition=broadwl
#SBATCH --mem=12G
#SBATCH --mail-type=END

module load Anaconda3
source activate three-prime-env


bedtools map -c 4 -o count_distinct -a /project2/gilad/briana/genome_anotation_data/gencode.v19.annotation.proteincodinggene.bed -b /project2/gilad/briana/threeprimeseq/data/clean.peaks_comb/APApeaks_combined_clean_fixed.bed &gt; /project2/gilad/briana/threeprimeseq/data/clean.peaks_comb/APApeaks_combined_clean_countdistgenes.txt</code></pre>
<p>I need to create bed files for the protein coding genes. For the human file the mRNAs are labeled with NM. The gene id is column 2, chr is column 3, strand is 4, start is 5, end is 6.</p>
<p>First I keep only the NM ones with:</p>
<pre class="bash"><code> grep &quot;NM&quot; humanGene_ncbiRefSeq.txt &gt; humanGene_ncbiRefSeq_mRNA.txt
 
 awk &#39;{print $3 &quot;\t&quot; $5 &quot;\t&quot; $6 &quot;\t&quot; $2 &quot;\t&quot; &quot;.&quot; &quot;\t&quot; $4 }&#39; humanGene_ncbiRefSeq_mRNA.txt &gt; humanGene_ncbiRefSeq_mRNA.bed
 
 grep &quot;NM&quot;  chimpGene_refGene.txt  &gt;  chimpGene_refGene_mRNA.txt 
 
 awk &#39;{print $3 &quot;\t&quot; $5 &quot;\t&quot; $6 &quot;\t&quot; $2 &quot;\t&quot; &quot;.&quot; &quot;\t&quot; $4 }&#39;  chimpGene_refGene_mRNA.txt &gt;  chimpGene_refGene_mRNA.bed 
</code></pre>
<pre class="bash"><code>#!/bin/bash

#SBATCH --job-name=PeakPerGene
#SBATCH --account=pi-yangili1
#SBATCH --time=24:00:00
#SBATCH --output=PeakPerGene.out
#SBATCH --error=PeakPerGene.err
#SBATCH --partition=broadwl
#SBATCH --mem=12G
#SBATCH --mail-type=END

module load Anaconda3
source activate comp_threeprime_env


bedtools map -c 4 -o count_distinct -a /project2/gilad/briana/genome_anotation_data/comp_genomes/gene_annos/humanGene_ncbiRefSeq_mRNA_sort.bed -b /project2/gilad/briana/comparitive_threeprime/data/ortho_peaks/humanOrthoPeaks.sort.bed &gt; /project2/gilad/briana/comparitive_threeprime/data/PeakPerGene/humanOrthoPeakPerGene.bed



bedtools map -c 4 -o count_distinct -a /project2/gilad/briana/genome_anotation_data/comp_genomes/gene_annos/chimpGene_refGene_mRNA_sort.bed -b /project2/gilad/briana/comparitive_threeprime/data/ortho_peaks/chimpOrthoPeaks.sort.bed &gt; /project2/gilad/briana/comparitive_threeprime/data/PeakPerGene/chimpOrthoPeakPerGene.bed</code></pre>
<pre class="r"><code>human_peakpergene= read.table(&quot;../data/PeakPerGene/humanOrthoPeakPerGene.bed&quot;, header=F, stringsAsFactors = F,col.names = c(&quot;chr&quot;, &quot;start&quot;, &quot;end&quot;, &quot;gene&quot;, &quot;score&quot;, &quot;strand&quot;, &quot;numPeaks&quot;)) %&gt;% mutate(spec= &quot;H&quot;) 

summary(human_peakpergene$numPeaks)</code></pre>
<pre><code>   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  0.000   0.000   0.000   3.924   3.000 206.000 </code></pre>
<pre class="r"><code>chimp_peakpergene= read.table(&quot;../data/PeakPerGene/chimpOrthoPeakPerGene.bed&quot;, stringsAsFactors = F, header = F, col.names = c(&quot;chr&quot;, &quot;start&quot;, &quot;end&quot;, &quot;gene&quot;, &quot;score&quot;, &quot;strand&quot;, &quot;numPeaks&quot;)) %&gt;% mutate(spec=&quot;C&quot;) 


summary(chimp_peakpergene$numPeaks)</code></pre>
<pre><code>   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  0.000   0.000   0.000   2.684   3.000  95.000 </code></pre>
<pre class="r"><code>humanPPG=ggplot(human_peakpergene, aes(x=log10(numPeaks))) + geom_density(fill=&quot;Red&quot;) + labs(title=&quot;Peaks per Gene \n Human mRNA&quot;)
chimpPPG=ggplot(chimp_peakpergene, aes(x=log10(numPeaks))) + geom_density(fill=&quot;Blue&quot;) + labs(title=&quot;Peaks per Gene \n Chimp mRNA&quot;)
plot_grid(humanPPG, chimpPPG)</code></pre>
<pre><code>Warning: Removed 27137 rows containing non-finite values (stat_density).</code></pre>
<pre><code>Warning: Removed 1218 rows containing non-finite values (stat_density).</code></pre>
<p><img src="figure/characterize.ortho.peaks.Rmd/unnamed-chunk-11-1.png" width="672" style="display: block; margin: auto;" /></p>
<details> <summary><em>Expand here to see past versions of unnamed-chunk-11-1.png:</em></summary>
<table style="border-collapse:separate; border-spacing:5px;">
<thead>
<tr>
<th style="text-align:left;">
Version
</th>
<th style="text-align:left;">
Author
</th>
<th style="text-align:left;">
Date
</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left;">
<a href="https://github.com/brimittleman/comparative_threeprime/blob/a23786505be6da399bd69014fa9a9e5e19902880/docs/figure/characterize.ortho.peaks.Rmd/unnamed-chunk-11-1.png" target="_blank">a237865</a>
</td>
<td style="text-align:left;">
brimittleman
</td>
<td style="text-align:left;">
2018-08-24
</td>
</tr>
</tbody>
</table>
<p></details></p>
<div id="genes-with-conserved-pas" class="section level4">
<h4>Genes with conserved PAS</h4>
<p>I will follow a similar strategy to Wang et al. 2018 to make a plot similar to plot 1d. I want the percent of genes in both species with conserved PAS.</p>
<pre class="r"><code>chimp_peakpergene= chimp_peakpergene %&gt;% mutate(oneConservedPeak=ifelse(numPeaks==1, 1, 0 )) %&gt;% mutate(multConservedPeak= ifelse(numPeaks &gt; 1, 1, 0))


Cgenes1peak=sum(chimp_peakpergene$oneConservedPeak)/nrow(chimp_peakpergene)
CgenesMultpeak=sum(chimp_peakpergene$multConservedPeak)/nrow(chimp_peakpergene)
Cgenes0peak=1- CgenesMultpeak - Cgenes1peak

human_peakpergene = human_peakpergene %&gt;% mutate(oneConservedPeak=ifelse(numPeaks==1, 1,0)) %&gt;% mutate(multConservedPeak=ifelse(numPeaks &gt;1,1,0))


Hgenes1peak=sum(human_peakpergene$oneConservedPeak) / nrow(human_peakpergene) 
HgenesMultpeak=sum(human_peakpergene$multConservedPeak)/ nrow(human_peakpergene)
Hgenes0peak=1- HgenesMultpeak - Hgenes1peak</code></pre>
<p>I want to create a data frame with these numbers to plot it.</p>
<pre class="r"><code>Hgene_peak=c(Hgenes0peak,Hgenes1peak,HgenesMultpeak)
Cgene_peak=c(Cgenes0peak, Cgenes1peak, CgenesMultpeak)
both_gene_peak=as.data.frame(rbind(Hgene_peak, Cgene_peak))
colnames(both_gene_peak)= c(&quot;ZeroConserved&quot;, &quot;OneConserved&quot;, &quot;MultConserved&quot;)
rownames(both_gene_peak)=c(&quot;Human&quot;, &quot;Chimp&quot;)
both_gene_peak= both_gene_peak %&gt;% rownames_to_column(var=&quot;Species&quot;)

both_gene_peak_melt=melt(both_gene_peak, id.vars =&quot;Species&quot;)

#add average number of conserved peak per gene 
avgH=round(mean(human_peakpergene$numPeaks),digits = 3)
avgC=round(mean(chimp_peakpergene$numPeaks),digits = 3)</code></pre>
<p>Plot this:</p>
<pre class="r"><code>genepeakplot= ggplot(both_gene_peak_melt, aes(x=Species, fill=variable, y=value)) + geom_bar(stat=&quot;identity&quot;, position = &quot;fill&quot;) + labs(y=&quot;Prop of Protein Coding Genes&quot;, title=&quot;Conserved peaks \n in protein coding genes&quot;) + scale_fill_discrete(name = &quot;Number of \nConserved Peaks&quot;, labels=c(&quot;Zero&quot;,&quot;One&quot;, &quot;Multiple&quot;)) + annotate(&quot;text&quot;, x=1, y=.8, label= paste(&quot;avg peaks\n per gene \n&quot;, avgH, sep=&quot; &quot;)) + annotate(&quot;text&quot;, x = 2, y=.8, label = paste(&quot;avg peaks\n per gene \n&quot;, avgC, sep=&quot; &quot;))
  
genepeakplot</code></pre>
<p><img src="figure/characterize.ortho.peaks.Rmd/unnamed-chunk-14-1.png" width="672" style="display: block; margin: auto;" /></p>
<details> <summary><em>Expand here to see past versions of unnamed-chunk-14-1.png:</em></summary>
<table style="border-collapse:separate; border-spacing:5px;">
<thead>
<tr>
<th style="text-align:left;">
Version
</th>
<th style="text-align:left;">
Author
</th>
<th style="text-align:left;">
Date
</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left;">
<a href="https://github.com/brimittleman/comparative_threeprime/blob/a23786505be6da399bd69014fa9a9e5e19902880/docs/figure/characterize.ortho.peaks.Rmd/unnamed-chunk-14-1.png" target="_blank">a237865</a>
</td>
<td style="text-align:left;">
brimittleman
</td>
<td style="text-align:left;">
2018-08-24
</td>
</tr>
</tbody>
</table>
<p></details></p>
</div>
<div id="conserved-exons" class="section level4">
<h4>Conserved exons</h4>
<p>I can run similar code on the conserved exons. We expect similar distribution but most of the exons will have 0. This is the primary reason to use conserved peaks in three prime bias data.</p>
<pre class="bash"><code>#!/bin/bash

#SBATCH --job-name=PeakPerExon
#SBATCH --account=pi-yangili1
#SBATCH --time=24:00:00
#SBATCH --output=PeakPerExon.out
#SBATCH --error=PeakPerExon.err
#SBATCH --partition=broadwl
#SBATCH --mem=12G
#SBATCH --mail-type=END

module load Anaconda3
source activate comp_threeprime_env


bedtools map -c 4 -o count_distinct -a /project2/gilad/briana/genome_anotation_data/ortho_exon/2017_July_ortho_human.small.sort.bed -b /project2/gilad/briana/comparitive_threeprime/data/ortho_peaks/humanOrthoPeaks.sort.bed &gt; /project2/gilad/briana/comparitive_threeprime/data/PeakPerGene/humanOrthoPeakPerExon.bed



bedtools map -c 4 -o count_distinct -a /project2/gilad/briana/genome_anotation_data/ortho_exon/2017_July_ortho_chimp.small.sort.bed -b /project2/gilad/briana/comparitive_threeprime/data/ortho_peaks/chimpOrthoPeaks.sort.bed &gt; /project2/gilad/briana/comparitive_threeprime/data/PeakPerGene/chimpOrthoPeakPerExon.bed</code></pre>
<pre class="r"><code>file.exists(&quot;../data/PeakPerExon/humanOrthoPeakPerExon.bed&quot;)</code></pre>
<pre><code>[1] TRUE</code></pre>
<pre class="r"><code>human_peakperexon= read.table(&quot;../data/PeakPerExon/humanOrthoPeakPerExon.bed&quot;, header=F, stringsAsFactors = F,col.names = c(&quot;chr&quot;, &quot;start&quot;, &quot;end&quot;, &quot;exon&quot;, &quot;numPeaks&quot;)) %&gt;% mutate(spec= &quot;H&quot;) 

summary(human_peakperexon$numPeaks)</code></pre>
<pre><code>    Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
 0.00000  0.00000  0.00000  0.06772  0.00000 13.00000 </code></pre>
<pre class="r"><code>chimp_peakperexon= read.table(&quot;../data/PeakPerExon/chimpOrthoPeakPerExon.bed&quot;, stringsAsFactors = F, header = F, col.names = c(&quot;chr&quot;, &quot;start&quot;, &quot;end&quot;, &quot;exon&quot;, &quot;numPeaks&quot;)) %&gt;% mutate(spec=&quot;C&quot;) 


summary(chimp_peakperexon$numPeaks)</code></pre>
<pre><code>    Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
 0.00000  0.00000  0.00000  0.06766  0.00000 13.00000 </code></pre>
<pre class="r"><code>humanPPE=ggplot(human_peakperexon, aes(x=numPeaks)) + geom_density(fill=&quot;Red&quot;) + labs(title=&quot;Peaks per Exon \n Human&quot;)
chimpPPE=ggplot(chimp_peakperexon, aes(x=numPeaks)) + geom_density(fill=&quot;Blue&quot;) + labs(title=&quot;Peaks per Exon \n Chimp &quot;)
plot_grid(humanPPE, chimpPPE)</code></pre>
<p><img src="figure/characterize.ortho.peaks.Rmd/unnamed-chunk-17-1.png" width="672" style="display: block; margin: auto;" /> Most of the exons have 0 peaks. This is expected. I want to look at how many 0s, 1s ect we have in each data set.</p>
<pre class="r"><code>human_exoncounts=human_peakperexon %&gt;% count(numPeaks)

chimp_exoncounts=chimp_peakperexon %&gt;% count(numPeaks)


both_exon=human_exoncounts %&gt;% left_join(chimp_exoncounts, by=&quot;numPeaks&quot;)

colnames(both_exon)=c(&quot;PeakNum&quot;, &quot;Human&quot;, &quot;Chimp&quot;)


both_exon_melt=melt(both_exon, measure.vars =c(&quot;Human&quot;, &quot;Chimp&quot;))


ggplot(both_exon_melt, aes(x=PeakNum, y=value, col=variable)) + geom_point( size=3) + facet_grid(~variable) + labs(y=&quot;Exons&quot;, title=&quot;Number of peaks per conserved exons&quot;)</code></pre>
<p><img src="figure/characterize.ortho.peaks.Rmd/unnamed-chunk-18-1.png" width="672" style="display: block; margin: auto;" /></p>
<details> <summary><em>Expand here to see past versions of unnamed-chunk-18-1.png:</em></summary>
<table style="border-collapse:separate; border-spacing:5px;">
<thead>
<tr>
<th style="text-align:left;">
Version
</th>
<th style="text-align:left;">
Author
</th>
<th style="text-align:left;">
Date
</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left;">
<a href="https://github.com/brimittleman/comparative_threeprime/blob/a23786505be6da399bd69014fa9a9e5e19902880/docs/figure/characterize.ortho.peaks.Rmd/unnamed-chunk-18-1.png" target="_blank">a237865</a>
</td>
<td style="text-align:left;">
brimittleman
</td>
<td style="text-align:left;">
2018-08-24
</td>
</tr>
</tbody>
</table>
<p></details></p>
</div>
</div>
<div id="distance-to-tes" class="section level3">
<h3>Distance to TES</h3>
<p>I want to look at the peaks distance to annotated gene TES in each species. I can make TES files by using the gene file. I need to take into account the strand. For the pos strand I use the end but for the neg strand I need to use the start. The easiest way to do this is in python. The scripts are called human_tes.py and chimp_tes.py</p>
<pre class="bash"><code>#!/bin/bash

#SBATCH --job-name=disTES
#SBATCH --account=pi-yangili1
#SBATCH --time=24:00:00
#SBATCH --output=disTES.out
#SBATCH --error=disTES.err
#SBATCH --partition=broadwl
#SBATCH --mem=12G
#SBATCH --mail-type=END


module load Anaconda3
source activate comp_threeprime_env


bedtools closest -id -D a -a /project2/gilad/briana/comparitive_threeprime/data/ortho_peaks/humanOrthoPeaks.sort.bed -b /project2/gilad/briana/genome_anotation_data/comp_genomes/gene_annos/humanGene_ncbiRefSeq_TES_sort.bed &gt; /project2/gilad/briana/comparitive_threeprime/data/dist_TES/Human.distTES.txt

bedtools closest -id -D a -a /project2/gilad/briana/comparitive_threeprime/data/ortho_peaks/chimpOrthoPeaks.sort.bed  -b /project2/gilad/briana/genome_anotation_data/comp_genomes/gene_annos/chimpGene_refGene_TES_sort.bed &gt; /project2/gilad/briana/comparitive_threeprime/data/dist_TES/Chimp.distTES.txt
</code></pre>
<pre class="r"><code>tes_names=c(&quot;peakchr&quot;, &quot;peakstart&quot;, &quot;peakend&quot;, &quot;peakname&quot;, &quot;genechr&quot;, &quot;geneTES_S&quot;, &quot;geneTES_E&quot;, &quot;gene&quot;, &quot;score&quot;, &quot;strand&quot;, &quot;dist&quot;)
human_TESdis=read.table(&quot;../data/dist_TES/Human.distTES.txt&quot;, stringsAsFactors = F,col.names = tes_names ) %&gt;% mutate(logdis=log10(abs(dist)+1))

chimp_TESdist= read.table(&quot;../data/dist_TES/Chimp.distTES.txt&quot;, stringsAsFactors = F, col.names = tes_names) %&gt;% mutate(logdis=log10(abs(dist) + 1))</code></pre>
<pre class="r"><code>chTES=ggplot(chimp_TESdist, aes(x=logdis)) + geom_density(fill=&quot;blue&quot;) + labs(title=&quot;Distance to TES \n Chimp&quot;,x=&quot;Log10 distance + 1&quot;)

huTES=ggplot(human_TESdis, aes(x=logdis)) + geom_density(fill=&quot;red&quot;) + labs(title=&quot;Distance to TES \n Human&quot;,x=&quot;Log10 distance + 1&quot;)


plot_grid(huTES, chTES)</code></pre>
<p><img src="figure/characterize.ortho.peaks.Rmd/unnamed-chunk-21-1.png" width="672" style="display: block; margin: auto;" /></p>
<details> <summary><em>Expand here to see past versions of unnamed-chunk-21-1.png:</em></summary>
<table style="border-collapse:separate; border-spacing:5px;">
<thead>
<tr>
<th style="text-align:left;">
Version
</th>
<th style="text-align:left;">
Author
</th>
<th style="text-align:left;">
Date
</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left;">
<a href="https://github.com/brimittleman/comparative_threeprime/blob/a23786505be6da399bd69014fa9a9e5e19902880/docs/figure/characterize.ortho.peaks.Rmd/unnamed-chunk-21-1.png" target="_blank">a237865</a>
</td>
<td style="text-align:left;">
brimittleman
</td>
<td style="text-align:left;">
2018-08-24
</td>
</tr>
</tbody>
</table>
<p></details></p>
<p>Flip this and do distance from teh TES to the peak.</p>
<pre class="bash"><code>#!/bin/bash

#SBATCH --job-name=disTES2Peak
#SBATCH --account=pi-yangili1
#SBATCH --time=24:00:00
#SBATCH --output=disTES2Peak.out
#SBATCH --error=disTES2Peak.err
#SBATCH --partition=broadwl
#SBATCH --mem=12G
#SBATCH --mail-type=END


module load Anaconda3
source activate comp_threeprime_env


bedtools closest -iu -D a -b /project2/gilad/briana/comparitive_threeprime/data/ortho_peaks/humanOrthoPeaks.sort.bed -a /project2/gilad/briana/genome_anotation_data/comp_genomes/gene_annos/humanGene_ncbiRefSeq_TES_sort.bed &gt; /project2/gilad/briana/comparitive_threeprime/data/dist_TES/Human.distTES2Peak.txt

bedtools closest -iu -D a -b /project2/gilad/briana/comparitive_threeprime/data/ortho_peaks/chimpOrthoPeaks.sort.bed  -a /project2/gilad/briana/genome_anotation_data/comp_genomes/gene_annos/chimpGene_refGene_TES_sort.bed &gt; /project2/gilad/briana/comparitive_threeprime/data/dist_TES/Chimp.distTES2Peak.txt
</code></pre>
<pre class="r"><code>tes2_names=c(&quot;genechr&quot;, &quot;geneTES_S&quot;, &quot;geneTES_E&quot;, &quot;gene&quot;, &quot;score&quot;, &quot;strand&quot;, &quot;peakchr&quot;, &quot;peakstart&quot;, &quot;peakend&quot;, &quot;peakname&quot;, &quot;dist&quot;)
human_TES2dis=read.table(&quot;../data/dist_TES/Human.distTES2Peak.txt&quot;, stringsAsFactors = F,col.names = tes2_names ) %&gt;% mutate(logdis=log10(abs(dist)+1))

chimp_TES2dist= read.table(&quot;../data/dist_TES/Chimp.distTES2Peak.txt&quot;, stringsAsFactors = F, col.names = tes2_names) %&gt;% mutate(logdis=log10(abs(dist) + 1))</code></pre>
<pre class="r"><code>chTES2=ggplot(chimp_TES2dist, aes(x=logdis)) + geom_density(fill=&quot;blue&quot;) + labs(title=&quot;Distance TES to Peak \n Chimp&quot;,x=&quot;Log10 distance + 1&quot;)

huTES2=ggplot(human_TES2dis, aes(x=logdis)) + geom_density(fill=&quot;red&quot;) + labs(title=&quot;Distance TES to Peak \n Human&quot;,x=&quot;Log10 distance + 1&quot;)


plot_grid(huTES2, chTES2)</code></pre>
<p><img src="figure/characterize.ortho.peaks.Rmd/unnamed-chunk-24-1.png" width="672" style="display: block; margin: auto;" /></p>
</div>
</div>
<div id="session-information" class="section level2">
<h2>Session information</h2>
<pre class="r"><code>sessionInfo()</code></pre>
<pre><code>R version 3.5.1 (2018-07-02)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Sierra 10.12.6

Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] bindrcpp_0.2.2  reshape2_1.4.3  cowplot_0.9.3   workflowr_1.1.1
 [5] forcats_0.3.0   stringr_1.3.1   dplyr_0.7.6     purrr_0.2.5    
 [9] readr_1.1.1     tidyr_0.8.1     tibble_1.4.2    ggplot2_3.0.0  
[13] tidyverse_1.2.1

loaded via a namespace (and not attached):
 [1] tidyselect_0.2.4  haven_1.1.2       lattice_0.20-35  
 [4] colorspace_1.3-2  htmltools_0.3.6   yaml_2.2.0       
 [7] rlang_0.2.2       R.oo_1.22.0       pillar_1.3.0     
[10] glue_1.3.0        withr_2.1.2       R.utils_2.7.0    
[13] modelr_0.1.2      readxl_1.1.0      bindr_0.1.1      
[16] plyr_1.8.4        munsell_0.5.0     gtable_0.2.0     
[19] cellranger_1.1.0  rvest_0.3.2       R.methodsS3_1.7.1
[22] evaluate_0.11     labeling_0.3      knitr_1.20       
[25] broom_0.5.0       Rcpp_0.12.18      scales_1.0.0     
[28] backports_1.1.2   jsonlite_1.5      hms_0.4.2        
[31] digest_0.6.16     stringi_1.2.4     grid_3.5.1       
[34] rprojroot_1.3-2   cli_1.0.0         tools_3.5.1      
[37] magrittr_1.5      lazyeval_0.2.1    crayon_1.3.4     
[40] whisker_0.3-2     pkgconfig_2.0.2   xml2_1.2.0       
[43] lubridate_1.7.4   assertthat_0.2.0  rmarkdown_1.10   
[46] httr_1.3.1        rstudioapi_0.7    R6_2.2.2         
[49] nlme_3.1-137      git2r_0.23.0      compiler_3.5.1   </code></pre>
</div>

<!-- Adjust MathJax settings so that all math formulae are shown using
TeX fonts only; see
http://docs.mathjax.org/en/latest/configuration.html.  This will make
the presentation more consistent at the cost of the webpage sometimes
taking slightly longer to load. Note that this only works because the
footer is added to webpages before the MathJax javascript. -->
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    "HTML-CSS": { availableFonts: ["TeX"] }
  });
</script>

<hr>
<p>
  This reproducible <a href="http://rmarkdown.rstudio.com">R Markdown</a>
  analysis was created with
  <a href="https://github.com/jdblischak/workflowr">workflowr</a> 1.1.1
</p>
<hr>


</div>
</div>

</div>

<script>

// add bootstrap table styles to pandoc tables
function bootstrapStylePandocTables() {
  $('tr.header').parent('thead').parent('table').addClass('table table-condensed');
}
$(document).ready(function () {
  bootstrapStylePandocTables();
});


</script>

<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
  (function () {
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src  = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
    document.getElementsByTagName("head")[0].appendChild(script);
  })();
</script>

</body>
</html>