<!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" />

<meta name="date" content="2018-11-26" />

<title>Modify Leafcutter and Chose Extra Filter Cuttoff</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/journal.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">Three Prime Sequencing in Human LCLs</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/brimittleman/threeprimeseq">
    <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">Modify Leafcutter and Chose Extra Filter Cuttoff</h1>
<h4 class="author"><em>Briana Mittleman</em></h4>
<h4 class="date"><em>11/26/2018</em></h4>

</div>


<p><strong>Last updated:</strong> 2018-12-05</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(12345)</code> </summary></p>
<p>The command <code>set.seed(12345)</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/threeprimeseq/tree/e230640e18e2c841d3525567d36e24e94d21bdbb" target="_blank">e230640</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:    .DS_Store
    Ignored:    .Rhistory
    Ignored:    .Rproj.user/
    Ignored:    data/.DS_Store
    Ignored:    output/.DS_Store

Untracked files:
    Untracked:  KalistoAbundance18486.txt
    Untracked:  analysis/DirectionapaQTL.Rmd
    Untracked:  analysis/ncbiRefSeq_sm.sort.mRNA.bed
    Untracked:  analysis/snake.config.notes.Rmd
    Untracked:  analysis/verifyBAM.Rmd
    Untracked:  data/18486.genecov.txt
    Untracked:  data/APApeaksYL.total.inbrain.bed
    Untracked:  data/ChromHmmOverlap/
    Untracked:  data/GM12878.chromHMM.bed
    Untracked:  data/GM12878.chromHMM.txt
    Untracked:  data/LocusZoom/
    Untracked:  data/NuclearApaQTLs.txt
    Untracked:  data/PeakCounts/
    Untracked:  data/PeaksUsed/
    Untracked:  data/RNAkalisto/
    Untracked:  data/TotalApaQTLs.txt
    Untracked:  data/Totalpeaks_filtered_clean.bed
    Untracked:  data/YL-SP-18486-T-combined-genecov.txt
    Untracked:  data/YL-SP-18486-T_S9_R1_001-genecov.txt
    Untracked:  data/apaExamp/
    Untracked:  data/bedgraph_peaks/
    Untracked:  data/bin200.5.T.nuccov.bed
    Untracked:  data/bin200.Anuccov.bed
    Untracked:  data/bin200.nuccov.bed
    Untracked:  data/clean_peaks/
    Untracked:  data/comb_map_stats.csv
    Untracked:  data/comb_map_stats.xlsx
    Untracked:  data/comb_map_stats_39ind.csv
    Untracked:  data/combined_reads_mapped_three_prime_seq.csv
    Untracked:  data/diff_iso_trans/
    Untracked:  data/ensemble_to_genename.txt
    Untracked:  data/example_gene_peakQuant/
    Untracked:  data/explainProtVar/
    Untracked:  data/filtered_APApeaks_merged_allchrom_refseqTrans.closest2End.bed
    Untracked:  data/filtered_APApeaks_merged_allchrom_refseqTrans.closest2End.noties.bed
    Untracked:  data/first50lines_closest.txt
    Untracked:  data/gencov.test.csv
    Untracked:  data/gencov.test.txt
    Untracked:  data/gencov_zero.test.csv
    Untracked:  data/gencov_zero.test.txt
    Untracked:  data/gene_cov/
    Untracked:  data/joined
    Untracked:  data/leafcutter/
    Untracked:  data/merged_combined_YL-SP-threeprimeseq.bg
    Untracked:  data/mol_overlap/
    Untracked:  data/mol_pheno/
    Untracked:  data/nom_QTL/
    Untracked:  data/nom_QTL_opp/
    Untracked:  data/nom_QTL_trans/
    Untracked:  data/nuc6up/
    Untracked:  data/other_qtls/
    Untracked:  data/pQTL_otherphen/
    Untracked:  data/peakPerRefSeqGene/
    Untracked:  data/perm_QTL/
    Untracked:  data/perm_QTL_opp/
    Untracked:  data/perm_QTL_trans/
    Untracked:  data/perm_QTL_trans_filt/
    Untracked:  data/reads_mapped_three_prime_seq.csv
    Untracked:  data/smash.cov.results.bed
    Untracked:  data/smash.cov.results.csv
    Untracked:  data/smash.cov.results.txt
    Untracked:  data/smash_testregion/
    Untracked:  data/ssFC200.cov.bed
    Untracked:  data/temp.file1
    Untracked:  data/temp.file2
    Untracked:  data/temp.gencov.test.txt
    Untracked:  data/temp.gencov_zero.test.txt
    Untracked:  output/picard/
    Untracked:  output/plots/
    Untracked:  output/qual.fig2.pdf

Unstaged changes:
    Modified:   analysis/28ind.peak.explore.Rmd
    Modified:   analysis/apaQTLoverlapGWAS.Rmd
    Modified:   analysis/cleanupdtseq.internalpriming.Rmd
    Modified:   analysis/coloc_apaQTLs_protQTLs.Rmd
    Modified:   analysis/dif.iso.usage.leafcutter.Rmd
    Modified:   analysis/diff_iso_pipeline.Rmd
    Modified:   analysis/explainpQTLs.Rmd
    Modified:   analysis/explore.filters.Rmd
    Modified:   analysis/flash2mash.Rmd
    Modified:   analysis/overlapMolQTL.Rmd
    Modified:   analysis/overlap_qtls.Rmd
    Modified:   analysis/peakOverlap_oppstrand.Rmd
    Modified:   analysis/pheno.leaf.comb.Rmd
    Modified:   analysis/swarmPlots_QTLs.Rmd
    Modified:   analysis/test.max2.Rmd
    Modified:   code/Snakefile

</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/threeprimeseq/blob/e230640e18e2c841d3525567d36e24e94d21bdbb/analysis/modifyLeafcutter.Rmd" target="_blank">e230640</a>
</td>
<td style="text-align:left;">
Briana Mittleman
</td>
<td style="text-align:left;">
2018-12-05
</td>
<td style="text-align:left;">
add code to save relevant figures
</td>
</tr>
<tr>
<td style="text-align:left;">
html
</td>
<td style="text-align:left;">
<a href="https://cdn.rawgit.com/brimittleman/threeprimeseq/210b58b2a0f472cefb26b16fad513ea3ca49d9d7/docs/modifyLeafcutter.html" target="_blank">210b58b</a>
</td>
<td style="text-align:left;">
Briana Mittleman
</td>
<td style="text-align:left;">
2018-11-29
</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/threeprimeseq/blob/413c8fddd492447ac0d51a7b928c56a3c941f41f/analysis/modifyLeafcutter.Rmd" target="_blank">413c8fd</a>
</td>
<td style="text-align:left;">
Briana Mittleman
</td>
<td style="text-align:left;">
2018-11-29
</td>
<td style="text-align:left;">
add filter QTL analysis and start explain pqtl
</td>
</tr>
</tbody>
</table>
</ul>
<p></details></p>
<hr />
<p>I want to use this analysis to understand how many genes are not included in the apaQTL analysis due to low coverage and how many are not included due to low variance between individuals. I can do this by modifying the leafcutter prepare_phenotype_table.py to include output this information.</p>
<p>This will take 2 arguments, the ratio file and the outfile with the information for missing info ##Modify Scripts MissingandNoVarGenes.py</p>
<pre class="bash"><code>#!/usr/bin/env python

import sys
import gzip
import numpy as np
import scipy as sc
import pickle

from optparse import OptionParser
    
from sklearn.decomposition import PCA
from sklearn import preprocessing
from sklearn import linear_model

from scipy.stats import rankdata
from scipy.stats import norm

def qqnorm(x):
    n=len(x)
    a=3.0/8.0 if n&lt;=10 else 0.5
    return(norm.ppf( (rankdata(x)-a)/(n+1.0-2.0*a) ))

def stream_table(f, ss = &#39;&#39;):
    fc = &#39;#&#39;
    while fc[0] == &quot;#&quot;:
        fc = f.readline().strip()
        head = fc.split(ss)

    for ln in f:
        ln = ln.strip().split(ss)
        attr = {}

        for i in range(len(head)):
            try: attr[head[i]] = ln[i]
            except: break
        yield attr

def main(ratio_file,out_File, pcs=50):
    outF=open(out_File, &quot;w&quot;)
    n_Missing=0
    n_lowVar=0
    dic_pop, fout = {}, {}
    try: open(ratio_file)
    except: 
        sys.stderr.write(&quot;Can&#39;t find %s..exiting\n&quot;%(ratio_file))
        return

    sys.stderr.write(&quot;Starting...\n&quot;)
    for i in range(1,23):
        fout[i] = file(ratio_file+&quot;.phen_chr%d&quot;%i,&#39;w&#39;)
        fout_ave = file(ratio_file+&quot;.ave&quot;,&#39;w&#39;)
    valRows, valRowsnn, geneRows = [], [], []
    finished = False
    header = gzip.open(ratio_file).readline().split()[1:]

    for i in fout:
        fout[i].write(&quot;\t&quot;.join([&quot;#Chr&quot;,&quot;start&quot;, &quot;end&quot;, &quot;ID&quot;]+header)+&#39;\n&#39;)

    for dic in stream_table(gzip.open(ratio_file),&#39; &#39;):

        chrom = dic[&#39;chrom&#39;].replace(&quot;chr&quot;,&#39;&#39;)
        chr_ = chrom.split(&quot;:&quot;)[0]
        if chr_ in &#39;XY&#39;: continue
        NA_indices, valRow, aveReads = [], [], []
        tmpvalRow = []

        i = 0
        for sample in header:

            try: count = dic[sample]
            except: print chrom, len(dic)
            num, denom = count.split(&#39;/&#39;)
            if float(denom) &lt; 1:
                count = &quot;NA&quot;
                tmpvalRow.append(&quot;NA&quot;)
                NA_indices.append(i)
            else:
                # add a 0.5 pseudocount
                count = (float(num)+0.5)/((float(denom))+0.5)
                tmpvalRow.append(count) 
                aveReads.append(count)

        # If ratio is missing for over 40% of the samples, skip
        if tmpvalRow.count(&quot;NA&quot;) &gt; len(tmpvalRow)*0.4:
            #this is missing data (i want to write this row to skipped genes)
            n_Missing += 1
            continue

        ave = np.mean(aveReads)

        # Set missing values as the mean of all values
        for c in tmpvalRow:
            if c == &quot;NA&quot;: valRow.append(ave)
            else: valRow.append(c)

        # If there is too little variation, skip (there is a bug in fastqtl which doesn&#39;t handle cases with no variation)
        if np.std(valRow) &lt; 0.005:
            #this is little variation (i want to write this row to little variation genes)
            n_lowVar +=1 
            continue
    outF.write(&quot;MissingData:%d: NoVariation:%d&quot;%(n_Missing,n_lowVar))
    outF.close()

if __name__ == &quot;__main__&quot;:

    parser = OptionParser(usage=&quot;usage: %prog [-p num_PCs] input_perind.counts.gz&quot;)
    parser.add_option(&quot;-p&quot;, &quot;--pcs&quot;, dest=&quot;npcs&quot;, default = 50, help=&quot;number of PCs output&quot;)
    (options, args) = parser.parse_args()
    if len(args)==0:
        sys.stderr.write(&quot;Error: no ratio file provided... (e.g. python leafcutter/scripts/prepare_phenotype_table.py input_perind.counts.gz\n&quot;)
        exit(0)
    main(args[0], args[1], int(options.npcs))</code></pre>
<p>Try this in /project2/gilad/briana/threeprimeseq/data/PASstats</p>
<pre class="bash"><code>module load python 

python /project2/gilad/briana/threeprimeseq/code/MissingandNoVarGenes.py /project2/gilad/briana/threeprimeseq/data/phenotypes_filtPeakTranscript/filtered_APApeaks_merged_allchrom_refseqGenes.Transcript_sm_quant.Nuclear.pheno_fixed.txt.gz /project2/gilad/briana/threeprimeseq/data/PASstats/Nuclear_missing_and_noVar.txt

python /project2/gilad/briana/threeprimeseq/code/MissingandNoVarGenes.py /project2/gilad/briana/threeprimeseq/data/phenotypes_filtPeakTranscript/filtered_APApeaks_merged_allchrom_refseqGenes.Transcript_sm_quant.Total.pheno_fixed.txt.gz /project2/gilad/briana/threeprimeseq/data/PASstats/Total_missing_and_noVar.txt
</code></pre>
<p>total (338142): MissingData:1692: NoVariation:112641<br />
Nuclear (338142):<br />
MissingData:472: NoVariation:134521</p>
<p>This is 33% in total and 40% in nuclear with low variation.</p>
<p>It may be interesting to look at how many peaks have counts &lt;5 in at least 40% of the individuals. This could tell me if we need to be filtering more.</p>
<p>LowExpPeaks.py</p>
<p>Input the ratio matrix and a file to output the number of NAs due to low expression. I am looking at the number of peaks with &lt;5 reads in at least 40% of the individuals.</p>
<pre class="bash"><code>#!/usr/bin/env python

import sys
import gzip
import numpy as np
import scipy as sc
import pickle

from optparse import OptionParser
    
from sklearn.decomposition import PCA
from sklearn import preprocessing
from sklearn import linear_model

from scipy.stats import rankdata
from scipy.stats import norm

def qqnorm(x):
    n=len(x)
    a=3.0/8.0 if n&lt;=10 else 0.5
    return(norm.ppf( (rankdata(x)-a)/(n+1.0-2.0*a) ))

def stream_table(f, ss = &#39;&#39;):
    fc = &#39;#&#39;
    while fc[0] == &quot;#&quot;:
        fc = f.readline().strip()
        head = fc.split(ss)

    for ln in f:
        ln = ln.strip().split(ss)
        attr = {}

        for i in range(len(head)):
            try: attr[head[i]] = ln[i]
            except: break
        yield attr

def main(ratio_file,out_File, pcs=50):
    outF=open(out_File, &quot;w&quot;)
    n_Missing=0
    n_lowVar=0
    dic_pop, fout = {}, {}
    try: open(ratio_file)
    except: 
        sys.stderr.write(&quot;Can&#39;t find %s..exiting\n&quot;%(ratio_file))
        return

    sys.stderr.write(&quot;Starting...\n&quot;)
    for i in range(1,23):
        fout[i] = file(ratio_file+&quot;.phen_chr%d&quot;%i,&#39;w&#39;)
        fout_ave = file(ratio_file+&quot;.ave&quot;,&#39;w&#39;)
    valRows, valRowsnn, geneRows = [], [], []
    finished = False
    header = gzip.open(ratio_file).readline().split()[1:]

    for i in fout:
        fout[i].write(&quot;\t&quot;.join([&quot;#Chr&quot;,&quot;start&quot;, &quot;end&quot;, &quot;ID&quot;]+header)+&#39;\n&#39;)

    for dic in stream_table(gzip.open(ratio_file),&#39; &#39;):

        chrom = dic[&#39;chrom&#39;].replace(&quot;chr&quot;,&#39;&#39;)
        chr_ = chrom.split(&quot;:&quot;)[0]
        if chr_ in &#39;XY&#39;: continue
        NA_indices, valRow, aveReads = [], [], []
        tmpvalRow = []

        i = 0
        for sample in header:

            try: count = dic[sample]
            except: print chrom, len(dic)
            num, denom = count.split(&#39;/&#39;)
            if float(denom) &lt; 1:
                count = &quot;NA&quot;
                tmpvalRow.append(&quot;NA&quot;)
                NA_indices.append(i)
            #add condition to make the count NA if the numerator is less than 5 
            if float(num) &lt; 5:
                count = &quot;NA&quot;
                tmpvalRow.append(&quot;NA&quot;)
                NA_indices.append(i)
            else:
                # add a 0.5 pseudocount
                count = (float(num)+0.5)/((float(denom))+0.5)
                tmpvalRow.append(count) 
                aveReads.append(count)

        # If ratio is missing for over 40% of the samples, skip
        if tmpvalRow.count(&quot;NA&quot;) &gt; len(tmpvalRow)*0.4:
            #this is missing data (i want to write this row to skipped genes)
            n_Missing += 1
            continue

        ave = np.mean(aveReads)

        # Set missing values as the mean of all values
        for c in tmpvalRow:
            if c == &quot;NA&quot;: valRow.append(ave)
            else: valRow.append(c)

        # If there is too little variation, skip (there is a bug in fastqtl which doesn&#39;t handle cases with no variation)
        if np.std(valRow) &lt; 0.005:
            #this is little variation (i want to write this row to little variation genes)
            n_lowVar +=1 
            continue
    outF.write(&quot;TooLow:%d\n&quot;%(n_Missing))
    outF.close()

if __name__ == &quot;__main__&quot;:

    parser = OptionParser(usage=&quot;usage: %prog [-p num_PCs] input_perind.counts.gz&quot;)
    parser.add_option(&quot;-p&quot;, &quot;--pcs&quot;, dest=&quot;npcs&quot;, default = 50, help=&quot;number of PCs output&quot;)
    (options, args) = parser.parse_args()
    if len(args)==0:
        sys.stderr.write(&quot;Error: no ratio file provided... (e.g. python leafcutter/scripts/prepare_phenotype_table.py input_perind.counts.gz\n&quot;)
        exit(0)
    main(args[0], args[1], int(options.npcs))</code></pre>
<pre class="bash"><code>module load python 

python /project2/gilad/briana/threeprimeseq/code/LowExpPeaks.py /project2/gilad/briana/threeprimeseq/data/phenotypes_filtPeakTranscript/filtered_APApeaks_merged_allchrom_refseqGenes.Transcript_sm_quant.Nuclear.pheno_fixed.txt.gz /project2/gilad/briana/threeprimeseq/data/PASstats/Nuclear_LowExp.txt

python /project2/gilad/briana/threeprimeseq/code/LowExpPeaks.py /project2/gilad/briana/threeprimeseq/data/phenotypes_filtPeakTranscript/filtered_APApeaks_merged_allchrom_refseqGenes.Transcript_sm_quant.Total.pheno_fixed.txt.gz /project2/gilad/briana/threeprimeseq/data/PASstats/Total_LowExp.txt
</code></pre>
<p>Total: 258090/338142 Nuclear: 185530/338142</p>
<div id="plot-results" class="section level2">
<h2>Plot results</h2>
<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(edgeR)</code></pre>
<pre><code>Loading required package: limma</code></pre>
<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(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>fraction=c(&quot;Total&quot;, &quot;Nuclear&quot;)
NoVariation=c(0.33,0.40)
LowExpression=c(0.76,0.55)
PASstats=data.frame(fraction, NoVariation, LowExpression)
PASstats_melt=melt(PASstats)</code></pre>
<pre><code>Using fraction as id variables</code></pre>
<p>plot thid</p>
<pre class="r"><code>prefilter_pas_stat=ggplot(PASstats_melt, aes(x=variable, y=value,fill=fraction))+ geom_bar(stat=&quot;identity&quot;,position=&quot;dodge&quot;) + labs(y=&quot;Proportion of PAS&quot;, title=&quot;PAS with low variation or low expression&quot;, x=&quot;Category&quot;) + scale_fill_manual(values=c(&quot;deepskyblue3&quot;,&quot;darkviolet&quot;))
prefilter_pas_stat</code></pre>
<p><img src="figure/modifyLeafcutter.Rmd/unnamed-chunk-7-1.png" width="672" style="display: block; margin: auto;" /></p>
<details> <summary><em>Expand here to see past versions of unnamed-chunk-7-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/threeprimeseq/blob/210b58b2a0f472cefb26b16fad513ea3ca49d9d7/docs/figure/modifyLeafcutter.Rmd/unnamed-chunk-7-1.png" target="_blank">210b58b</a>
</td>
<td style="text-align:left;">
Briana Mittleman
</td>
<td style="text-align:left;">
2018-11-29
</td>
</tr>
</tbody>
</table>
<p></details></p>
<pre class="r"><code>ggsave(&quot;../output/plots/QC_plots/prefilter_PASstats.png&quot;, prefilter_pas_stat)</code></pre>
<pre><code>Saving 7 x 5 in image</code></pre>
</div>
<div id="do-this-de-novo" class="section level2">
<h2>Do this de novo</h2>
<div id="cpm" class="section level3">
<h3>CPM</h3>
<p>It may be better to look at mean distribution of reads per peak to look for a cuttoff. I can take counts matrix</p>
<ul>
<li><p>/project2/gilad/briana/threeprimeseq/data/filtPeakOppstrand_cov/filtered_APApeaks_merged_allchrom_refseqGenes.Transcript_sm_quant.Nuclear_fixed.fc</p></li>
<li><p>/project2/gilad/briana/threeprimeseq/data/filtPeakOppstrand_cov/filtered_APApeaks_merged_allchrom_refseqGenes.Transcript_sm_quant.Total_fixed.fc</p></li>
</ul>
<p>It would be good to do this in CPM and look at the densities like you do for RNA seq. In order to do this. I need a vector with the peak lengths.</p>
<p><strong>total</strong>:</p>
<p>Load data and convert to CPM with peak length.</p>
<pre class="r"><code>totalPeakCov=read.table(&quot;../data/PeakCounts/filtered_APApeaks_merged_allchrom_refseqGenes.Transcript_sm_quant.Total_fixed.fc&quot;, header = T, stringsAsFactors = F)

peakLength=totalPeakCov[,6]


totalPeakCov_cpm= cpm(as.matrix(totalPeakCov[,7:ncol(totalPeakCov)]),log=T, gene.length=peakLength)</code></pre>
<p>Plot prefiltered density</p>
<pre class="r"><code>plotDensities(totalPeakCov_cpm, legend = &quot;bottomright&quot;, main=&quot;Pre-filtering&quot;)
abline(v = -1, lty = 3)</code></pre>
<p><img src="figure/modifyLeafcutter.Rmd/unnamed-chunk-9-1.png" width="672" style="display: block; margin: auto;" /></p>
<details> <summary><em>Expand here to see past versions of unnamed-chunk-9-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/threeprimeseq/blob/210b58b2a0f472cefb26b16fad513ea3ca49d9d7/docs/figure/modifyLeafcutter.Rmd/unnamed-chunk-9-1.png" target="_blank">210b58b</a>
</td>
<td style="text-align:left;">
Briana Mittleman
</td>
<td style="text-align:left;">
2018-11-29
</td>
</tr>
</tbody>
</table>
<p></details></p>
<p>Looks like filtering around -1 works. I can keep peaks with greater than -1 cpm in 2/3 of the samples (26)</p>
<pre class="r"><code>keep.exprs_T=rowSums(totalPeakCov_cpm&gt;-1) &gt;= 26
totalPeakCov_cpm_filt= totalPeakCov_cpm[keep.exprs_T,]

plotDensities(totalPeakCov_cpm_filt, legend = &quot;bottomright&quot;, main=&quot;Post-filtering&quot;)</code></pre>
<p><img src="figure/modifyLeafcutter.Rmd/unnamed-chunk-10-1.png" width="672" style="display: block; margin: auto;" /></p>
<details> <summary><em>Expand here to see past versions of unnamed-chunk-10-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/threeprimeseq/blob/210b58b2a0f472cefb26b16fad513ea3ca49d9d7/docs/figure/modifyLeafcutter.Rmd/unnamed-chunk-10-1.png" target="_blank">210b58b</a>
</td>
<td style="text-align:left;">
Briana Mittleman
</td>
<td style="text-align:left;">
2018-11-29
</td>
</tr>
</tbody>
</table>
<p></details></p>
<p>I can assess this with boxplots. It is interesting to see how different these data are from the RNA seq.</p>
<pre class="r"><code>boxplot(totalPeakCov_cpm, main=&quot;Log CPM Counts by library prefilter&quot;)</code></pre>
<p><img src="figure/modifyLeafcutter.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/threeprimeseq/blob/210b58b2a0f472cefb26b16fad513ea3ca49d9d7/docs/figure/modifyLeafcutter.Rmd/unnamed-chunk-11-1.png" target="_blank">210b58b</a>
</td>
<td style="text-align:left;">
Briana Mittleman
</td>
<td style="text-align:left;">
2018-11-29
</td>
</tr>
</tbody>
</table>
<p></details></p>
<pre class="r"><code>boxplot(totalPeakCov_cpm_filt, main=&quot;Log CPM Counts by library filtered&quot;)</code></pre>
<p><img src="figure/modifyLeafcutter.Rmd/unnamed-chunk-11-2.png" width="672" style="display: block; margin: auto;" /></p>
<details> <summary><em>Expand here to see past versions of unnamed-chunk-11-2.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/threeprimeseq/blob/210b58b2a0f472cefb26b16fad513ea3ca49d9d7/docs/figure/modifyLeafcutter.Rmd/unnamed-chunk-11-2.png" target="_blank">210b58b</a>
</td>
<td style="text-align:left;">
Briana Mittleman
</td>
<td style="text-align:left;">
2018-11-29
</td>
</tr>
</tbody>
</table>
<p></details></p>
<p><strong>Nuclear</strong></p>
<p>Load data and convert to CPM with peak length.</p>
<pre class="r"><code>nuclearPeakCov=read.table(&quot;../data/PeakCounts/filtered_APApeaks_merged_allchrom_refseqGenes.Transcript_sm_quant.Nuclear_fixed.fc&quot;, header = T, stringsAsFactors = F)

peakLengthN=nuclearPeakCov[,6]


nuclearPeakCov_cpm= cpm(as.matrix(nuclearPeakCov[,7:ncol(nuclearPeakCov)]),log=T, gene.length=peakLengthN)</code></pre>
<p>Plot prefiltered density</p>
<pre class="r"><code>plotDensities(nuclearPeakCov_cpm, legend = &quot;bottomright&quot;, main=&quot;Pre-filtering&quot;)
abline(v = 0, lty = 3)</code></pre>
<p><img src="figure/modifyLeafcutter.Rmd/unnamed-chunk-13-1.png" width="672" style="display: block; margin: auto;" /></p>
<details> <summary><em>Expand here to see past versions of unnamed-chunk-13-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/threeprimeseq/blob/210b58b2a0f472cefb26b16fad513ea3ca49d9d7/docs/figure/modifyLeafcutter.Rmd/unnamed-chunk-13-1.png" target="_blank">210b58b</a>
</td>
<td style="text-align:left;">
Briana Mittleman
</td>
<td style="text-align:left;">
2018-11-29
</td>
</tr>
</tbody>
</table>
<p></details></p>
<p>Looks like filtering around -4.5 works. I can keep peaks with greater than -4.5 cpm in 2/3 of the samples (26)</p>
<pre class="r"><code>keep.exprs_N=rowSums(nuclearPeakCov_cpm&gt;-4.5) &gt;= 26
nuclearPeakCov_cpm_filt= nuclearPeakCov_cpm[keep.exprs_T,]

plotDensities(nuclearPeakCov_cpm_filt, legend = &quot;bottomright&quot;, main=&quot;Post-filtering&quot;)</code></pre>
<p><img src="figure/modifyLeafcutter.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/threeprimeseq/blob/210b58b2a0f472cefb26b16fad513ea3ca49d9d7/docs/figure/modifyLeafcutter.Rmd/unnamed-chunk-14-1.png" target="_blank">210b58b</a>
</td>
<td style="text-align:left;">
Briana Mittleman
</td>
<td style="text-align:left;">
2018-11-29
</td>
</tr>
</tbody>
</table>
<p></details></p>
<pre class="r"><code>boxplot(nuclearPeakCov_cpm, main=&quot;Log CPM Counts by library prefilter&quot;)</code></pre>
<p><img src="figure/modifyLeafcutter.Rmd/unnamed-chunk-15-1.png" width="672" style="display: block; margin: auto;" /></p>
<details> <summary><em>Expand here to see past versions of unnamed-chunk-15-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/threeprimeseq/blob/210b58b2a0f472cefb26b16fad513ea3ca49d9d7/docs/figure/modifyLeafcutter.Rmd/unnamed-chunk-15-1.png" target="_blank">210b58b</a>
</td>
<td style="text-align:left;">
Briana Mittleman
</td>
<td style="text-align:left;">
2018-11-29
</td>
</tr>
</tbody>
</table>
<p></details></p>
<pre class="r"><code>boxplot(nuclearPeakCov_cpm_filt, main=&quot;Log CPM Counts by library filtered&quot;)</code></pre>
<p><img src="figure/modifyLeafcutter.Rmd/unnamed-chunk-15-2.png" width="672" style="display: block; margin: auto;" /></p>
<details> <summary><em>Expand here to see past versions of unnamed-chunk-15-2.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/threeprimeseq/blob/210b58b2a0f472cefb26b16fad513ea3ca49d9d7/docs/figure/modifyLeafcutter.Rmd/unnamed-chunk-15-2.png" target="_blank">210b58b</a>
</td>
<td style="text-align:left;">
Briana Mittleman
</td>
<td style="text-align:left;">
2018-11-29
</td>
</tr>
</tbody>
</table>
<p></details></p>
<p>I had to filter stronger in the total fraction to get similar boxplot distributions. I can compare this method to using raw counts rather than CPM.</p>
<p>With this scheme I have the following number of peaks remaining:<br />
Total: 66,782 Nuclear: 124,196</p>
</div>
<div id="raw-counts" class="section level3">
<h3>Raw counts</h3>
<p><strong>Total</strong></p>
<pre class="r"><code>png(&quot;../output/plots/QC_plots/log10TotalPeakCov_prefilter.png&quot;)
totalPeakCov_logM=log10(as.matrix(totalPeakCov[,7:ncol(totalPeakCov)]))
plotDensities(totalPeakCov_logM, legend = &quot;bottomright&quot;, main=&quot;Raw Pre-filtering&quot;)
abline(v = .55, lty = 3)
dev.off()</code></pre>
<pre><code>quartz_off_screen 
                2 </code></pre>
<p>I can filter 2/3 (26) individuals at .55.</p>
<pre class="r"><code>keep.exprs_T_raw=rowSums(totalPeakCov_logM&gt;.55) &gt;= 26
totalPeakCov_raw_filt= totalPeakCov_logM[keep.exprs_T_raw,]

png(&quot;../output/plots/QC_plots/log10TotalPeakCov_postfilter.png&quot;)
plotDensities(totalPeakCov_raw_filt, legend = &quot;bottomright&quot;, main=&quot;Post-filtering&quot;)
dev.off()</code></pre>
<pre><code>quartz_off_screen 
                2 </code></pre>
<pre class="r"><code>boxplot(totalPeakCov_logM, main=&quot;log Counts by library prefilter&quot;)</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 9 is not drawn</code></pre>
<p><img src="figure/modifyLeafcutter.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/threeprimeseq/blob/210b58b2a0f472cefb26b16fad513ea3ca49d9d7/docs/figure/modifyLeafcutter.Rmd/unnamed-chunk-18-1.png" target="_blank">210b58b</a>
</td>
<td style="text-align:left;">
Briana Mittleman
</td>
<td style="text-align:left;">
2018-11-29
</td>
</tr>
</tbody>
</table>
<p></details></p>
<pre class="r"><code>boxplot(totalPeakCov_raw_filt, main=&quot;Log Counts by library filtered&quot;)</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 2 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 3 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 4 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 5 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 6 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 7 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 8 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 9 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 10 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 11 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 12 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 13 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 14 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 15 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 16 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 17 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 18 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 19 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 20 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 21 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 22 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 23 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 24 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 25 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 26 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 27 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 28 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 29 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 30 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 31 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 32 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 33 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 34 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 35 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 36 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 37 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 38 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 39 is not drawn</code></pre>
<p><img src="figure/modifyLeafcutter.Rmd/unnamed-chunk-18-2.png" width="672" style="display: block; margin: auto;" /></p>
<details> <summary><em>Expand here to see past versions of unnamed-chunk-18-2.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/threeprimeseq/blob/210b58b2a0f472cefb26b16fad513ea3ca49d9d7/docs/figure/modifyLeafcutter.Rmd/unnamed-chunk-18-2.png" target="_blank">210b58b</a>
</td>
<td style="text-align:left;">
Briana Mittleman
</td>
<td style="text-align:left;">
2018-11-29
</td>
</tr>
</tbody>
</table>
<p></details></p>
<p>This leaves 80554 rows.</p>
<p><strong>Nuclear</strong></p>
<pre class="r"><code>png(&quot;../output/plots/QC_plots/log10NuclearPeakCov_prefilter.png&quot;)

nuclearPeakCov_logM=log10(as.matrix(nuclearPeakCov[,7:ncol(nuclearPeakCov)]))
plotDensities(nuclearPeakCov_logM, legend = &quot;bottomright&quot;, main=&quot;Raw Pre-filtering&quot;)
abline(v = .55, lty = 3)
dev.off()</code></pre>
<pre><code>quartz_off_screen 
                2 </code></pre>
<p>I can filter 2/3 (26) individuals at .55.</p>
<pre class="r"><code>keep.exprs_N_raw=rowSums(nuclearPeakCov_logM&gt;.55) &gt;= 26
nuclearPeakCov_raw_filt= nuclearPeakCov_logM[keep.exprs_N_raw,]
png(&quot;../output/plots/QC_plots/log10NuclearPeakCov_postfilter.png&quot;)
plotDensities(nuclearPeakCov_raw_filt, legend = &quot;bottomright&quot;, main=&quot;Post-filtering&quot;)
dev.off()</code></pre>
<pre><code>quartz_off_screen 
                2 </code></pre>
<pre class="r"><code>boxplot(nuclearPeakCov_logM, main=&quot;log Counts by library prefilter&quot;)</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 2 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 4 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 5 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 6 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 7 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 8 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 9 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 10 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 11 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 12 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 14 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 15 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 16 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 17 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 18 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 19 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 20 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 21 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 22 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 23 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 24 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 25 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 26 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 27 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 28 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 29 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 31 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 32 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 33 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 34 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 35 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 37 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 38 is not drawn</code></pre>
<p><img src="figure/modifyLeafcutter.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/threeprimeseq/blob/210b58b2a0f472cefb26b16fad513ea3ca49d9d7/docs/figure/modifyLeafcutter.Rmd/unnamed-chunk-21-1.png" target="_blank">210b58b</a>
</td>
<td style="text-align:left;">
Briana Mittleman
</td>
<td style="text-align:left;">
2018-11-29
</td>
</tr>
</tbody>
</table>
<p></details></p>
<pre class="r"><code>boxplot(nuclearPeakCov_raw_filt, main=&quot;Log Counts by library filtered&quot;)</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 1 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 2 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 3 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 4 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 5 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 6 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 7 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 8 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 9 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 10 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 11 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 12 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 13 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 14 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 15 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 16 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 17 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 18 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 19 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 20 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 21 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 22 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 23 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 24 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 25 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 26 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 27 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 28 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 29 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 30 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 31 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 32 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 33 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 34 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 35 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 36 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 37 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 38 is not drawn</code></pre>
<pre><code>Warning in bplt(at[i], wid = width[i], stats = z$stats[, i], out =
z$out[z$group == : Outlier (-Inf) in boxplot 39 is not drawn</code></pre>
<p><img src="figure/modifyLeafcutter.Rmd/unnamed-chunk-21-2.png" width="672" style="display: block; margin: auto;" /></p>
<details> <summary><em>Expand here to see past versions of unnamed-chunk-21-2.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/threeprimeseq/blob/210b58b2a0f472cefb26b16fad513ea3ca49d9d7/docs/figure/modifyLeafcutter.Rmd/unnamed-chunk-21-2.png" target="_blank">210b58b</a>
</td>
<td style="text-align:left;">
Briana Mittleman
</td>
<td style="text-align:left;">
2018-11-29
</td>
</tr>
</tbody>
</table>
<p></details></p>
<p>This leaves 159308 peaks with coverage.</p>
</div>
<div id="filter-based-on-raw-coverage" class="section level3">
<h3>Filter based on raw coverage</h3>
<p>I am going to write a script to filter based on this cutoff so I can see if there is increased power to detect QTLs. I will need to change this when I add individuals.</p>
<p>extraFilterPeaks_39ind.R</p>
<pre class="r"><code>#this script will filter the peaks based on peaks with coverage greater than log10(cov)==.55 in 2/3 of the individuals, I will have to fix headers after 

#nonfilter dir:  /project2/gilad/briana/threeprimeseq/data/filtPeakOppstrand_cov/  
#filter dir: /project2/gilad/briana/threeprimeseq/data/ExtrafiltPeakOppstrand_cov/


#total
totalPeak=read.table(&quot;/project2/gilad/briana/threeprimeseq/data/filtPeakOppstrand_cov/filtered_APApeaks_merged_allchrom_refseqGenes.Transcript_sm_quant.Total_fixed.fc&quot;, stringsAsFactors = F, header=T)

totalPeakCov_logM=log10(as.matrix(totalPeak[,7:ncol(totalPeak)]))
keep.exprs_T_raw=rowSums(totalPeakCov_logM&gt;.55) &gt;= 26
totalPeakCov_raw_filt= totalPeak[keep.exprs_T_raw,]

write.table(totalPeakCov_raw_filt,file=&quot;/project2/gilad/briana/threeprimeseq/data/ExtrafiltPeakOppstrand_cov/Extrafiltered_APApeaks_merged_allchrom_refseqGenes.Transcript_sm_quant.Total_fixed.fc&quot;, quote=F, col.names = T ,row.names = F)

#nuclear 

nuclearPeak=read.table(&quot;/project2/gilad/briana/threeprimeseq/data/filtPeakOppstrand_cov/filtered_APApeaks_merged_allchrom_refseqGenes.Transcript_sm_quant.Nuclear_fixed.fc&quot;, header = T)

nuclearPeakCov_logM=log10(as.matrix(nuclearPeak[,7:ncol(nuclearPeak)]))
keep.exprs_N_raw=rowSums(nuclearPeakCov_logM&gt;.55) &gt;= 26
nuclearPeakCov_raw_filt= nuclearPeak[keep.exprs_N_raw,]

write.table(nuclearPeakCov_raw_filt,file=&quot;/project2/gilad/briana/threeprimeseq/data/ExtrafiltPeakOppstrand_cov/Extrafiltered_APApeaks_merged_allchrom_refseqGenes.Transcript_sm_quant.Nuclear_fixed.fc&quot;, quote=F, col.names = T,row.names = F )</code></pre>
<p>I can use fileIDs /project2/gilad/briana/threeprimeseq/data/filtPeakOppstrand_cov/file_id_mapping_total_Transcript_head.txt and /project2/gilad/briana/threeprimeseq/data/filtPeakOppstrand_cov/file_id_mapping_nuclear_Transcript_head.txt</p>
<p>makePhenoRefSeqPeaks_Transcript_Total_extraFilt.py</p>
<pre class="bash"><code>#PYTHON 3

dic_IND = {}
dic_BAM = {}

for ln in open(&quot;/project2/gilad/briana/threeprimeseq/data/filtPeakOppstrand_cov/file_id_mapping_total_Transcript.txt&quot;):
    bam, IND = ln.split(&quot;\t&quot;)
    IND = IND.strip()
    dic_IND[bam] = IND
    if IND not in dic_BAM:
        dic_BAM[IND] = []
    dic_BAM[IND].append(bam)


#now I have ind dic with keys as the bam and ind as the values
#I also have a bam dic with ind as the keys and bam as the values  
    
inds=list(dic_BAM.keys()) #list of ind libraries  

#gene start and end dictionaries: 
dic_geneS = {}
dic_geneE = {}
for ln in open(&quot;/project2/gilad/briana/genome_anotation_data/ncbiRefSeq_endProtCodGenes_sort.txt&quot;):
    chrom, start, end, geneID, score, strand = ln.split(&#39;\t&#39;)
    gene= geneID.split(&quot;:&quot;)[1]
    if &quot;-&quot; in gene:
        gene=gene.split(&quot;-&quot;)[0]
    if gene not in dic_geneS:
        dic_geneS[gene]=int(start)
        dic_geneE[gene]=int(end)
        


#list of genes   

count_file=open(&quot;/project2/gilad/briana/threeprimeseq/data/ExtrafiltPeakOppstrand_cov/Extrafiltered_APApeaks_merged_allchrom_refseqGenes.Transcript_sm_quant.Total_fixed.fc&quot;, &quot;r&quot;)
genes=[]
for line , i in enumerate(count_file):
    if line &gt; 1:
        i_list=i.split()
        id=i_list[0]
        id_list=id.split(&quot;:&quot;)
        gene=id_list[5]
        if gene not in genes:
            genes.append(gene)
            
#make the ind and gene dic  
dic_dub={}
for g in genes:
    dic_dub[g]={}
    for i in inds:
        dic_dub[g][i]=0


#populate the dictionary  
count_file=open(&quot;/project2/gilad/briana/threeprimeseq/data/ExtrafiltPeakOppstrand_cov/Extrafiltered_APApeaks_merged_allchrom_refseqGenes.Transcript_sm_quant.Total_fixed.fc&quot;, &quot;r&quot;)
for line, i in enumerate(count_file):
    if line &gt; 1:
        i_list=i.split()
        id=i_list[0]
        id_list=id.split(&quot;:&quot;)
        g= id_list[5]
        values=list(i_list[6:])
        list_list=[]
        for ind,val in zip(inds, values):
            list_list.append([ind, val])
        for num, name in enumerate(list_list):
            dic_dub[g][list_list[num][0]] += int(list_list[num][1])
        

#write the file by acessing the dictionary and putting values in the table ver the value in the dic 
        

fout=open(&quot;/project2/gilad/briana/threeprimeseq/data/ExtrafiltPeakOppstrand_cov/Extrafiltered_APApeaks_merged_allchrom_refseqGenes.Transcript_sm_quant.Total.pheno_fixed.txt&quot;,&quot;w&quot;)
peak=[&quot;chrom&quot;]
inds_noL=[]
for each in inds:
    indsNA= &quot;NA&quot; + each[:-2]
    inds_noL.append(indsNA) 
fout.write(&quot; &quot;.join(peak + inds_noL) + &#39;\n&#39; )


count_file=open(&quot;/project2/gilad/briana/threeprimeseq/data/ExtrafiltPeakOppstrand_cov/Extrafiltered_APApeaks_merged_allchrom_refseqGenes.Transcript_sm_quant.Total_fixed.fc&quot;, &quot;r&quot;)
for line , i in enumerate(count_file):
    if line &gt; 1:
        i_list=i.split()
        id=i_list[0]
        id_list=id.split(&quot;:&quot;)
        gene=id_list[5]
        start=dic_geneS[id_list[5]]
        end=dic_geneE[id_list[5]]
        buff=[]
        buff.append(&quot;chr%s:%d:%d:%s_%s_%s&quot;%(id_list[1], start, end, id_list[5], id_list[4], id_list[0]))
        for x,y in zip(i_list[6:], inds):
            b=int(dic_dub[gene][y])
            t=int(x)
            buff.append(&quot;%d/%d&quot;%(t,b))
        fout.write(&quot; &quot;.join(buff)+ &#39;\n&#39;)
        
fout.close()</code></pre>
<p>makePhenoRefSeqPeaks_Transcript_Nuclear_extraFilt.py</p>
<pre class="bash"><code>#PYTHON 3

dic_IND = {}
dic_BAM = {}

for ln in open(&quot;/project2/gilad/briana/threeprimeseq/data/filtPeakOppstrand_cov/file_id_mapping_nuclear_Transcript.txt&quot;):
    bam, IND = ln.split(&quot;\t&quot;)
    IND = IND.strip()
    dic_IND[bam] = IND
    if IND not in dic_BAM:
        dic_BAM[IND] = []
    dic_BAM[IND].append(bam)


#now I have ind dic with keys as the bam and ind as the values
#I also have a bam dic with ind as the keys and bam as the values  
    
inds=list(dic_BAM.keys()) #list of ind libraries  

#gene start and end dictionaries: 
dic_geneS = {}
dic_geneE = {}
for ln in open(&quot;/project2/gilad/briana/genome_anotation_data/ncbiRefSeq_endProtCodGenes_sort.txt&quot;):
    chrom, start, end, geneID, score, strand = ln.split(&#39;\t&#39;)
    gene= geneID.split(&quot;:&quot;)[1]
    if &quot;-&quot; in gene:
        gene=gene.split(&quot;-&quot;)[0]
    if gene not in dic_geneS:
        dic_geneS[gene]=int(start)
        dic_geneE[gene]=int(end)
        


#list of genes   

count_file=open(&quot;/project2/gilad/briana/threeprimeseq/data/ExtrafiltPeakOppstrand_cov/Extrafiltered_APApeaks_merged_allchrom_refseqGenes.Transcript_sm_quant.Nuclear_fixed.fc&quot;, &quot;r&quot;)
genes=[]
for line , i in enumerate(count_file):
    if line &gt; 1:
        i_list=i.split()
        id=i_list[0]
        id_list=id.split(&quot;:&quot;)
        gene=id_list[5]
        if gene not in genes:
            genes.append(gene)
            
#make the ind and gene dic  
dic_dub={}
for g in genes:
    dic_dub[g]={}
    for i in inds:
        dic_dub[g][i]=0


#populate the dictionary  
count_file=open(&quot;/project2/gilad/briana/threeprimeseq/data/ExtrafiltPeakOppstrand_cov/Extrafiltered_APApeaks_merged_allchrom_refseqGenes.Transcript_sm_quant.Nuclear_fixed.fc&quot;, &quot;r&quot;)
for line, i in enumerate(count_file):
    if line &gt; 1:
        i_list=i.split()
        id=i_list[0]
        id_list=id.split(&quot;:&quot;)
        g= id_list[5]
        values=list(i_list[6:])
        list_list=[]
        for ind,val in zip(inds, values):
            list_list.append([ind, val])
        for num, name in enumerate(list_list):
            dic_dub[g][list_list[num][0]] += int(list_list[num][1])
        

#write the file by acessing the dictionary and putting values in the table ver the value in the dic 
        

fout=open(&quot;/project2/gilad/briana/threeprimeseq/data/ExtrafiltPeakOppstrand_cov/Extrafiltered_APApeaks_merged_allchrom_refseqGenes.Transcript_sm_quant.Nuclear.pheno_fixed.txt&quot;,&quot;w&quot;)
peak=[&quot;chrom&quot;]
inds_noL=[]
for each in inds:
    indsNA= &quot;NA&quot; + each[:-2]
    inds_noL.append(indsNA) 
fout.write(&quot; &quot;.join(peak + inds_noL) + &#39;\n&#39; )


count_file=open(&quot;/project2/gilad/briana/threeprimeseq/data/ExtrafiltPeakOppstrand_cov/Extrafiltered_APApeaks_merged_allchrom_refseqGenes.Transcript_sm_quant.Nuclear_fixed.fc&quot;, &quot;r&quot;)
for line , i in enumerate(count_file):
    if line &gt; 1:
        i_list=i.split()
        id=i_list[0]
        id_list=id.split(&quot;:&quot;)
        gene=id_list[5]
        start=dic_geneS[id_list[5]]
        end=dic_geneE[id_list[5]]
        buff=[]
        buff.append(&quot;chr%s:%d:%d:%s_%s_%s&quot;%(id_list[1], start, end, id_list[5], id_list[4], id_list[0]))
        for x,y in zip(i_list[6:], inds):
            b=int(dic_dub[gene][y])
            t=int(x)
            buff.append(&quot;%d/%d&quot;%(t,b))
        fout.write(&quot; &quot;.join(buff)+ &#39;\n&#39;)
        
fout.close()</code></pre>
<p>run_makePhen_sep_Transcript_extraFilt.sh</p>
<pre class="bash"><code>#!/bin/bash

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

module load Anaconda3
source activate three-prime-env

python makePhenoRefSeqPeaks_Transcript_Total_extraFilt.py  

python makePhenoRefSeqPeaks_Transcript_Nuclear_extraFilt.py  
</code></pre>
<p>Prepare for fastQTL</p>
<p>/project2/gilad/briana/threeprimeseq/data/ExtrafiltPeakOppstrand_cov</p>
<pre class="bash"><code>module load samtools
#zip file 
gzip Extrafiltered_APApeaks_merged_allchrom_refseqGenes.Transcript_sm_quant.Total.pheno_fixed.txt

module load python
#leafcutter script
python /project2/gilad/briana/threeprimeseq/code/prepare_phenotype_table.py  Extrafiltered_APApeaks_merged_allchrom_refseqGenes.Transcript_sm_quant.Total.pheno_fixed.txt.gz 

#source activate three-prime-env
sh  Extrafiltered_APApeaks_merged_allchrom_refseqGenes.Transcript_sm_quant.Total.pheno_fixed.txt.gz_prepare.sh

#run for nuclear as well 
gzip  Extrafiltered_APApeaks_merged_allchrom_refseqGenes.Transcript_sm_quant.Nuclear.pheno_fixed.txt
#unload anaconda, load python
python /project2/gilad/briana/threeprimeseq/code/prepare_phenotype_table.py  Extrafiltered_APApeaks_merged_allchrom_refseqGenes.Transcript_sm_quant.Nuclear.pheno_fixed.txt.gz
#load anaconda and env. 
sh  Extrafiltered_APApeaks_merged_allchrom_refseqGenes.Transcript_sm_quant.Nuclear.pheno_fixed.txt.gz_prepare.sh





#keep only 2 PCs
head -n 3 Extrafiltered_APApeaks_merged_allchrom_refseqGenes.Transcript_sm_quant.Nuclear.pheno_fixed.txt.gz.PCs &gt; Extrafiltered_APApeaks_merged_allchrom_refseqGenes.Transcript_sm_quant.Nuclear.pheno_fixed.txt.gz.2PCs
head -n 3 Extrafiltered_APApeaks_merged_allchrom_refseqGenes.Transcript_sm_quant.Total.pheno_fixed.txt.gz.PCs &gt; Extrafiltered_APApeaks_merged_allchrom_refseqGenes.Transcript_sm_quant.Total.pheno_fixed.txt.gz.2PCs</code></pre>
<p>Now I can rerun the modified leafcutter and see if this improves the drop proportion.</p>
<pre class="bash"><code>module load python 

python /project2/gilad/briana/threeprimeseq/code/MissingandNoVarGenes.py /project2/gilad/briana/threeprimeseq/data/ExtrafiltPeakOppstrand_cov/Extrafiltered_APApeaks_merged_allchrom_refseqGenes.Transcript_sm_quant.Total.pheno_fixed.txt.gz /project2/gilad/briana/threeprimeseq/data/PASstats/Total_missing_and_noVarFilter.txt

python /project2/gilad/briana/threeprimeseq/code/MissingandNoVarGenes.py /project2/gilad/briana/threeprimeseq/data/ExtrafiltPeakOppstrand_cov/Extrafiltered_APApeaks_merged_allchrom_refseqGenes.Transcript_sm_quant.Nuclear.pheno_fixed.txt.gz /project2/gilad/briana/threeprimeseq/data/PASstats/Nuclear_missing_and_noVarFilter.txt




python /project2/gilad/briana/threeprimeseq/code/LowExpPeaks.py /project2/gilad/briana/threeprimeseq/data/ExtrafiltPeakOppstrand_cov/Extrafiltered_APApeaks_merged_allchrom_refseqGenes.Transcript_sm_quant.Nuclear.pheno_fixed.txt.gz /project2/gilad/briana/threeprimeseq/data/PASstats/Nuclear_LowExpFilter.txt

python /project2/gilad/briana/threeprimeseq/code/LowExpPeaks.py /project2/gilad/briana/threeprimeseq/data/ExtrafiltPeakOppstrand_cov/Extrafiltered_APApeaks_merged_allchrom_refseqGenes.Transcript_sm_quant.Total.pheno_fixed.txt.gz /project2/gilad/briana/threeprimeseq/data/PASstats/Total_LowExpFilter.txt
</code></pre>
<p>No variation<br />
* Nuclear: 37738/159308<br />
* Total: 8289/80554<br />
Low exp<br />
* Nuclear 8221/159308<br />
* Total: 2808/80554</p>
<pre class="r"><code>#total, nuclear
NoVariationFilt=c(0.10,0.23)
LowExpressionFilt=c(0.035,0.052)
PASstatsFilt=data.frame(fraction, NoVariationFilt, LowExpressionFilt)
PASstatsFilt_melt=melt(PASstatsFilt)</code></pre>
<pre><code>Using fraction as id variables</code></pre>
<p>plot thid</p>
<pre class="r"><code>postfilter_pas_stat=ggplot(PASstatsFilt_melt, aes(x=variable, y=value,fill=fraction))+ geom_bar(stat=&quot;identity&quot;,position=&quot;dodge&quot;) + labs(y=&quot;Proportion of Filtered PAS&quot;, title=&quot;PAS with low variation or low expression after filtering&quot;, x=&quot;Category&quot;) + scale_fill_manual(values=c(&quot;deepskyblue3&quot;,&quot;darkviolet&quot;))
postfilter_pas_stat</code></pre>
<p><img src="figure/modifyLeafcutter.Rmd/unnamed-chunk-29-1.png" width="672" style="display: block; margin: auto;" /></p>
<details> <summary><em>Expand here to see past versions of unnamed-chunk-29-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/threeprimeseq/blob/210b58b2a0f472cefb26b16fad513ea3ca49d9d7/docs/figure/modifyLeafcutter.Rmd/unnamed-chunk-29-1.png" target="_blank">210b58b</a>
</td>
<td style="text-align:left;">
Briana Mittleman
</td>
<td style="text-align:left;">
2018-11-29
</td>
</tr>
</tbody>
</table>
<p></details></p>
<pre class="r"><code>ggsave(&quot;../output/plots/QC_plots/postfilter_PASstats.png&quot;, postfilter_pas_stat)</code></pre>
<pre><code>Saving 7 x 5 in image</code></pre>
<p>merge the pre and post filter into the same dataframe (did this in excel and saved it)</p>
<pre class="r"><code>PasStats_all=read.csv(&quot;../data/PeakCounts/pasStats.csv&quot;, header=T, stringsAsFactors = F)

preandpostfilter_pas_stat=ggplot(PasStats_all, aes(x=Category, y=Value,fill=Fraction))+ geom_bar(stat=&quot;identity&quot;,position=&quot;dodge&quot;)  +labs(y=&quot;Proportion of PAS&quot;, title=&quot;PAS with low variation or low expression before and after filtering&quot;, x=&quot;Category&quot;) + scale_fill_manual(values=c(&quot;deepskyblue3&quot;,&quot;darkviolet&quot;)) + facet_grid(~Filter)
preandpostfilter_pas_stat</code></pre>
<p><img src="figure/modifyLeafcutter.Rmd/unnamed-chunk-30-1.png" width="672" style="display: block; margin: auto;" /></p>
<details> <summary><em>Expand here to see past versions of unnamed-chunk-30-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/threeprimeseq/blob/210b58b2a0f472cefb26b16fad513ea3ca49d9d7/docs/figure/modifyLeafcutter.Rmd/unnamed-chunk-30-1.png" target="_blank">210b58b</a>
</td>
<td style="text-align:left;">
Briana Mittleman
</td>
<td style="text-align:left;">
2018-11-29
</td>
</tr>
</tbody>
</table>
<p></details></p>
<pre class="r"><code>ggsave(&quot;../output/plots/QC_plots/preandpostfilter_PASstats.png&quot;, preandpostfilter_pas_stat)</code></pre>
<pre><code>Saving 7 x 5 in image</code></pre>
<p>Rerun QTL analysis.</p>
<p>I can use the same sample list. /project2/gilad/briana/threeprimeseq/data/phenotypes_filtPeakTranscript/SAMPLE.txt&quot;<br />
APAqtl_nominal_transcript_filt.sh</p>
<pre class="bash"><code>#!/bin/bash


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

for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 
do
/home/brimittleman/software/bin/FastQTL/bin/fastQTL.static --vcf /project2/gilad/briana/YRI_geno_hg19/chr$i.dose.filt.vcf.gz --cov /project2/gilad/briana/threeprimeseq/data/ExtrafiltPeakOppstrand_cov/Extrafiltered_APApeaks_merged_allchrom_refseqGenes.Transcript_sm_quant.Nuclear.pheno_fixed.txt.gz.2PCs --bed /project2/gilad/briana/threeprimeseq/data/ExtrafiltPeakOppstrand_cov/Extrafiltered_APApeaks_merged_allchrom_refseqGenes.Transcript_sm_quant.Nuclear.pheno_fixed.txt.gz.qqnorm_chr$i.gz --out /project2/gilad/briana/threeprimeseq/data/nominal_APAqtl_trans_filt/Extrafiltered_APApeaks_merged_allchrom_refseqGenes.Transcript_sm_quant.Nuclear.pheno_fixed.txt.gz.qqnorm_chr$i.nominal.out --chunk 1 1  --window 5e5 --include-samples /project2/gilad/briana/threeprimeseq/data/phenotypes_filtPeakTranscript/SAMPLE.txt
done


for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 
do
/home/brimittleman/software/bin/FastQTL/bin/fastQTL.static --vcf /project2/gilad/briana/YRI_geno_hg19/chr$i.dose.filt.vcf.gz --cov /project2/gilad/briana/threeprimeseq/data/ExtrafiltPeakOppstrand_cov/Extrafiltered_APApeaks_merged_allchrom_refseqGenes.Transcript_sm_quant.Total.pheno_fixed.txt.gz.2PCs --bed /project2/gilad/briana/threeprimeseq/data/ExtrafiltPeakOppstrand_cov/Extrafiltered_APApeaks_merged_allchrom_refseqGenes.Transcript_sm_quant.Total.pheno_fixed.txt.gz.qqnorm_chr$i.gz --out /project2/gilad/briana/threeprimeseq/data/nominal_APAqtl_trans_filt/Extrafiltered_APApeaks_merged_allchrom_refseqGenes.Transcript_sm_quant.Total.pheno_fixed.txt.gz.qqnorm_chr$i.nominal.out --chunk 1 1  --window 5e5 --include-samples /project2/gilad/briana/threeprimeseq/data/phenotypes_filtPeakTranscript/SAMPLE.txt
done
</code></pre>
<p>permuted:</p>
<p>APAqtl_permuted_transcript_filt.sh</p>
<pre class="bash"><code>
#!/bin/bash


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

for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 
do
/home/brimittleman/software/bin/FastQTL/bin/fastQTL.static --permute 1000  --vcf /project2/gilad/briana/YRI_geno_hg19/chr$i.dose.filt.vcf.gz --cov /project2/gilad/briana/threeprimeseq/data/ExtrafiltPeakOppstrand_cov/Extrafiltered_APApeaks_merged_allchrom_refseqGenes.Transcript_sm_quant.Nuclear.pheno_fixed.txt.gz.2PCs --bed /project2/gilad/briana/threeprimeseq/data/ExtrafiltPeakOppstrand_cov/Extrafiltered_APApeaks_merged_allchrom_refseqGenes.Transcript_sm_quant.Nuclear.pheno_fixed.txt.gz.qqnorm_chr$i.gz --out /project2/gilad/briana/threeprimeseq/data/perm_APAqtl_trans_filt/Extrafiltered_APApeaks_merged_allchrom_refseqGenes.Transcript_sm_quant.Nuclear.pheno_fixed.txt.gz.qqnorm_chr$i.perm.out --chunk 1 1  --window 5e5 --include-samples /project2/gilad/briana/threeprimeseq/data/phenotypes_filtPeakTranscript/SAMPLE.txt
done


for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 
do
/home/brimittleman/software/bin/FastQTL/bin/fastQTL.static --permute 1000  --vcf /project2/gilad/briana/YRI_geno_hg19/chr$i.dose.filt.vcf.gz --cov /project2/gilad/briana/threeprimeseq/data/ExtrafiltPeakOppstrand_cov/Extrafiltered_APApeaks_merged_allchrom_refseqGenes.Transcript_sm_quant.Total.pheno_fixed.txt.gz.2PCs --bed /project2/gilad/briana/threeprimeseq/data/ExtrafiltPeakOppstrand_cov/Extrafiltered_APApeaks_merged_allchrom_refseqGenes.Transcript_sm_quant.Total.pheno_fixed.txt.gz.qqnorm_chr$i.gz --out /project2/gilad/briana/threeprimeseq/data/perm_APAqtl_trans_filt/Extrafiltered_APApeaks_merged_allchrom_refseqGenes.Transcript_sm_quant.Total.pheno_fixed.txt.gz.qqnorm_chr$i.perm.out --chunk 1 1  --window 5e5 --include-samples /project2/gilad/briana/threeprimeseq/data/phenotypes_filtPeakTranscript/SAMPLE.txt
done</code></pre>
<p>APAqtlpermCorrectQQplot_trans_filt.R</p>
<pre class="r"><code>library(dplyr)


##total results
tot.perm= read.table(&quot;/project2/gilad/briana/threeprimeseq/data/perm_APAqtl_trans_filt/Extrafiltered_APApeaks_merged_allchrom_refseqGenes.Transcript_sm_quant.Total.pheno_fixed.txt.gz.qqnorm_permRes.out&quot;,head=F, stringsAsFactors=F, col.names = c(&quot;pid&quot;, &quot;nvar&quot;, &quot;shape1&quot;, &quot;shape2&quot;, &quot;dummy&quot;, &quot;sid&quot;, &quot;dist&quot;, &quot;npval&quot;, &quot;slope&quot;, &quot;ppval&quot;, &quot;bpval&quot;))

#BH correction
tot.perm$bh=p.adjust(tot.perm$bpval, method=&quot;fdr&quot;)

#plot qqplot
png(&quot;/project2/gilad/briana/threeprimeseq/output/plots/qqplot_total_APAperm_transcript_filt.png&quot;) 
qqplot_total= qqplot(-log10(runif(nrow(tot.perm))), -log10(tot.perm$bpval),ylab=&quot;-log10 Total permuted pvalue&quot;, xlab=&quot;Uniform expectation&quot;, main=&quot;Total permuted pvalues for all snps&quot;)
abline(0,1)
dev.off()

#write df with BH  

write.table(tot.perm, file = &quot;/project2/gilad/briana/threeprimeseq/data/perm_APAqtl_trans_filt/Extrafiltered_APApeaks_merged_allchrom_refseqGenes.Transcript_sm_quant.Total.pheno_fixed.txt.gz.qqnorm_permResBH.out&quot;, col.names = T, row.names = F, quote = F)

##nuclear results  


nuc.perm= read.table(&quot;/project2/gilad/briana/threeprimeseq/data/perm_APAqtl_trans_filt/Extrafiltered_APApeaks_merged_allchrom_refseqGenes.Transcript_sm_quant.Nuclear.pheno_fixed.txt.gz.qqnorm_permRes.out&quot;,head=F, stringsAsFactors=F, col.names = c(&quot;pid&quot;, &quot;nvar&quot;, &quot;shape1&quot;, &quot;shape2&quot;, &quot;dummy&quot;, &quot;sid&quot;, &quot;dist&quot;, &quot;npval&quot;, &quot;slope&quot;, &quot;ppval&quot;, &quot;bpval&quot;))
nuc.perm$bh=p.adjust(nuc.perm$bpval, method=&quot;fdr&quot;)


#plot qqplot
png(&quot;/project2/gilad/briana/threeprimeseq/output/plots/qqplot_nuclear_APAperm_transcript_filt.png&quot;) 
qqplot(-log10(runif(nrow(nuc.perm))), -log10(nuc.perm$bpval),ylab=&quot;-log10 Nuclear permuted pvalue&quot;, xlab=&quot;Uniform expectation&quot;, main=&quot;Nuclear permuted pvalues for all snps&quot;)
abline(0,1)
dev.off()

# write df with BH
write.table(nuc.perm, file = &quot;/project2/gilad/briana/threeprimeseq/data/perm_APAqtl_trans_filt/Extrafiltered_APApeaks_merged_allchrom_refseqGenes.Transcript_sm_quant.Nuclear.pheno_fixed.txt.gz.qqnorm_permResBH.out&quot;, col.names = T, row.names = F, quote = F)</code></pre>
<p>run_APAqtlpermCorrectQQplot_trans_filt.sh</p>
<pre class="bash"><code>#!/bin/bash


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

module load Anaconda3
source activate three-prime-env


Rscript APAqtlpermCorrectQQplot_trans_filt.R </code></pre>
<p>Evaluate results:</p>
<p>Total:</p>
<pre class="r"><code>tot.perm= read.table(&quot;../data/perm_QTL_trans_filt/Extrafiltered_APApeaks_merged_allchrom_refseqGenes.Transcript_sm_quant.Total.pheno_fixed.txt.gz.qqnorm_permResBH.out&quot;,head=T, stringsAsFactors=F)

tot.perm %&gt;% filter(-log10(bh) &gt; 1) %&gt;% nrow()</code></pre>
<pre><code>[1] 183</code></pre>
<p>Nuclear:</p>
<pre class="r"><code>nuc.perm= read.table(&quot;../data/perm_QTL_trans_filt/Extrafiltered_APApeaks_merged_allchrom_refseqGenes.Transcript_sm_quant.Nuclear.pheno_fixed.txt.gz.qqnorm_permResBH.out&quot;,head=T, stringsAsFactors=F)

nuc.perm %&gt;% filter(-log10(bh) &gt; 1) %&gt;% nrow()</code></pre>
<pre><code>[1] 1031</code></pre>
<p>This analysis gives 183 total and 1031 nuclear. (65 more total and 151 more in nuclear)</p>
<pre class="r"><code>plot(tot.perm$ppval, tot.perm$bpval, xlab=&quot;Direct method&quot;, ylab=&quot;Beta approximation&quot;, main=&quot;Total Check plot&quot;)
abline(0, 1, col=&quot;red&quot;)</code></pre>
<p><img src="figure/modifyLeafcutter.Rmd/unnamed-chunk-37-1.png" width="672" style="display: block; margin: auto;" /></p>
<details> <summary><em>Expand here to see past versions of unnamed-chunk-37-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/threeprimeseq/blob/210b58b2a0f472cefb26b16fad513ea3ca49d9d7/docs/figure/modifyLeafcutter.Rmd/unnamed-chunk-37-1.png" target="_blank">210b58b</a>
</td>
<td style="text-align:left;">
Briana Mittleman
</td>
<td style="text-align:left;">
2018-11-29
</td>
</tr>
</tbody>
</table>
<p></details></p>
<pre class="r"><code>plot(nuc.perm$ppval, nuc.perm$bpval, xlab=&quot;Direct method&quot;, ylab=&quot;Beta approximation&quot;, main=&quot;Nuclear Check plot&quot;)
abline(0, 1, col=&quot;red&quot;)</code></pre>
<p><img src="figure/modifyLeafcutter.Rmd/unnamed-chunk-37-2.png" width="672" style="display: block; margin: auto;" /></p>
<details> <summary><em>Expand here to see past versions of unnamed-chunk-37-2.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/threeprimeseq/blob/210b58b2a0f472cefb26b16fad513ea3ca49d9d7/docs/figure/modifyLeafcutter.Rmd/unnamed-chunk-37-2.png" target="_blank">210b58b</a>
</td>
<td style="text-align:left;">
Briana Mittleman
</td>
<td style="text-align:left;">
2018-11-29
</td>
</tr>
</tbody>
</table>
<p></details></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  10.14.1

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  forcats_0.3.0   stringr_1.3.1  
 [5] dplyr_0.7.6     purrr_0.2.5     readr_1.1.1     tidyr_0.8.1    
 [9] tibble_1.4.2    ggplot2_3.0.0   tidyverse_1.2.1 edgeR_3.22.5   
[13] limma_3.36.5    workflowr_1.1.1

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

<hr>
<p>
    
</p>
<hr>

<!-- To enable disqus, uncomment the section below and provide your disqus_shortname -->

<!-- disqus
  <div id="disqus_thread"></div>
    <script type="text/javascript">
        /* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
        var disqus_shortname = 'rmarkdown'; // required: replace example with your forum shortname

        /* * * DON'T EDIT BELOW THIS LINE * * */
        (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
        })();
    </script>
    <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
    <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
-->
<!-- 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>