{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Aim of the study\n",
    "\n",
    "RUNX is a family of mammalian transcription factors consisting of three members, RUNX1, RUNX2 and RUNX3. They each have tissue-specific expression patterns and cell-context dependent functions through interactions with their binding factor and each have another physiological function. The RUNX factors play a key role in different types of cancer.\n",
    "\n",
    "The article focuses on the contributions of RUNX1, which is involved in hematopoiesis, in the development in breast cancer. It functions both as a tumor suppressor and an oncogene, and is thus shown to be both down-regulated and upregulated in breast cancer. Whole genome sequencing has shown deletions and point mutations in the RUNX1 gene in human breast tumors.\n",
    "\n",
    "RUNX1 activates and repsresses target genes expression depending on its target partners. \n",
    "\n",
    "Cancer is characterized with changes in the nucleus, mostly alterations in the folding of the genome. In breast cancer, alterations in the nuclear architecture are seen such as  alterations in the chromatin and at telomeres compared to epithelial cells. Through the importance of the higher-order genome folding and the involvement of RUNX1 in breast cancer, it is necessary to understand the relation between the function of RUNX1 in chromatin organization and its regulatory role in breast cancer.\n",
    "\n",
    "Hypothesis of the study: to understand the relationship between the RUNX1-dependent alterations in higher-order chromatin structure and RUNX1-altered gene expression in breast cancer cells.\n",
    "\n",
    "- RNAseq: they suppressed RUNX1 in a human MCF-7 breast cancer cell line and evaluated the gene expression\n",
    "- ChIP-seq: performed to gain insight into RUNX1-mediated regulation of chromatin organization. They probed RUNX1 binding by performing ChIP-seq"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ChIP-sequencing\n",
    "\n",
    "Chromatin immunoprecipitation followed by high-throughput sequencing is the standard method\n",
    "to predict DNA binding sites from ChIP-seq read count data and thereby perform peak calling methods."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Aim of ChIP-seq\n",
    "\n",
    "ChIP-seq is performed to determine where a protein binds the genome, which can be transcription factors, DNA-binding enzymes, histones, chaperones, or nucleosomes.\n",
    "\n",
    "ChIP-seq first cross-links bound proteins to chromatin, fragments the chromatin, captures the DNA fragments bound to one protein using an antibody specific to it, and sequences the ends of the captured fragments using next-generation sequencing. Mapping of the sequenced DNA identifies the genomic locations of bound DNA-enzymes, modified histones, chaperones, nucleosomes and transcription factors, thereby illuminating the role of these protein-DNA interactions in gene expression and other cellular proceses. The use of NGS provides relatively high solution, low noise, and high genomic coverage compared with ChIP-seq assays (chIP followed by microarray hybridisation). ChIP-seq is the most widely used procedure for DNA-protein interaction."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### STEP 1: downloading the ChIP-seq data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "Download the ChIP-seq data from the GEO site. They performed chip-seq on 2 replicates, namely MCF-7 wildtype RUNX1 replicate 1 and 2. We downloaded the input and pulldown data of both replicates.\n",
    "\n",
    "SRA numbers:\n",
    "\n",
    "- SRR2919474, replicate 1 input named S1\n",
    "- SRR2919475, replicate 1 pulldown named S2\n",
    "- SRR2919476, replicate 2 input named S3 \n",
    "- SRR2919477, replicate 2 pulldown named S4\n",
    "\n",
    "The input files are used as control."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "cd ~/data/work"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "~\t\tS1_fastqc.zip  S2_fastqc.html  S3_fastqc.html  S4_fastqc.html\r\n",
      "S1.fastq\tS1.sam\t       S2_fastqc.zip   S3_fastqc.zip   S4_fastqc.zip\r\n",
      "S1_fastqc.html\tS2.fastq       S3.fastq        S4.fastq\r\n"
     ]
    }
   ],
   "source": [
    "ls"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Before we map the reads to the reference genome, we should apply quality control to asses possible sequencing errors or biases."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "We performed quality control in bitvise with fastqc."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Overview of the data quality after fastqc\n",
    "\n",
    "The phred quality scores are analysed for both input and pulldown data."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Figure 1: S1 fastqc data (replicate 1 input)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img width=\"50%\" src=\"/user/r0618772/files/S1 fastqc.JPG\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Figure 2: S2 fastqc data (replicate 1 pulldown)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img width=\"50%\" src=\"/user/r0618772/files/S2 fastqc.JPG\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Figure 3: S3 fastqc data (replicate 2 input)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img width=\"50%\" src=\"/user/r0618772/files/S3 fastqc.JPG\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Figure 4: S4 fastqc data (repliate 2 pulldown)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img width=\"50%\" src=\"/user/r0618772/files/S4 fastqc.JPG\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We checked the reads of our data to check the quality (figure 1-4). We concluded that the overall quality is good for the input and pulldown data of replicate 1 and 2. therefore we worked further with the files without trimming the data first."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## STEP 2: alignment with BOWTIE\n",
    "\n",
    "The reads should be mapped using Bowtie, which allows gapped alignment, but detection of indels is not necessary for most chip-seq experiments. We use bowtie for mapping, because it is ultra fast. Bowtie needs a reference genome to align each read and the genome has to be in an index for bowtie to be able to use is. All the samples of our genome are human, so we choose the human reference genome hg19, which is not the latest but a stable version of human genome assembly."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Alignment with bowtie2 is performed whereafter the samfiles are converted to a bam file and they are sorted."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "~\t\tS2.bam\t\t   S3.bam\t      S4.bam\r\n",
      "S1.bam\t\tS2.fastq\t   S3.fastq\t      S4.fastq\r\n",
      "S1.fastq\tS2_fastqc.html\t   S3_fastqc.html     S4_fastqc.html\r\n",
      "S1_fastqc.html\tS2_fastqc.zip\t   S3_fastqc.zip      S4_fastqc.zip\r\n",
      "S1_fastqc.zip\tS2.sam\t\t   S3.sam\t      S4.sam\r\n",
      "S1.sam\t\tS2.sorted.bam\t   S3.sorted.bam      S4.sorted.bam\r\n",
      "S1.sorted.bam\tS2.sorted.bam.bai  S3.sorted.bam.bai\r\n"
     ]
    }
   ],
   "source": [
    "cd ~/data/work\n",
    "ls"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\r\n"
     ]
    }
   ],
   "source": [
    "bowtie2-align -x /mnt/nfs/data/resources/bowtie2/hg19 S1.fastq -S S1.sam\n",
    "bowtie2-align -x /mnt/nfs/data/resources/bowtie2/hg19 S2.fastq -S S2.sam\n",
    "bowtie2-align -x /mnt/nfs/data/resources/bowtie2/hg19 S3.fastq -S S3.sam\n",
    "bowtie2-align -x /mnt/nfs/data/resources/bowtie2/hg19 S4.fastq -S S4.sam"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\r\n"
     ]
    }
   ],
   "source": [
    "bowtie2-align -x /mnt/nfs/data/resources/bowtie2/hg19 S2.fastq -S S2.sam"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "30075142 reads; of these:\r\n",
      "  30075142 (100.00%) were unpaired; of these:\r\n",
      "    2226261 (7.40%) aligned 0 times\r\n",
      "    18605200 (61.86%) aligned exactly 1 time\r\n",
      "    9243681 (30.74%) aligned >1 times\r\n",
      "92.60% overall alignment rate\r\n",
      "29988988 reads; of these:\r\n",
      "  29988988 (100.00%) were unpaired; of these:\r\n",
      "    1801619 (6.01%) aligned 0 times\r\n",
      "    21932161 (73.13%) aligned exactly 1 time\r\n",
      "    6255208 (20.86%) aligned >1 times\r\n",
      "93.99% overall alignment rate\r\n"
     ]
    }
   ],
   "source": [
    "bowtie2-align -x /mnt/nfs/data/resources/bowtie2/hg19 S1.fastq -S S1.sam\n",
    "bowtie2-align -x /mnt/nfs/data/resources/bowtie2/hg19 S4.fastq -S S4.sam"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "bowtie2-align -x /mnt/nfs/data/resources/bowtie2/hg19 S4.fastq -S S4.sam"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "~\t\tS1.sam\t\tS2.sam\t\tS3.sam\t\tS4.sam\r\n",
      "S1.fastq\tS2.fastq\tS3.fastq\tS4.fastq\r\n",
      "S1_fastqc.html\tS2_fastqc.html\tS3_fastqc.html\tS4_fastqc.html\r\n",
      "S1_fastqc.zip\tS2_fastqc.zip\tS3_fastqc.zip\tS4_fastqc.zip\r\n"
     ]
    }
   ],
   "source": [
    "cd ~/data/work\n",
    "ls"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[W::sam_read1] parse error at line 22324899\r\n",
      "[main_samview] truncated file.\r\n"
     ]
    }
   ],
   "source": [
    "#evaluation of the mapped reads: view, sort and index files\n",
    "samtools view -S -b S1.sam > S1.bam\n",
    "samtools view -S -b S2.sam > S2.bam\n",
    "samtools view -S -b S3.sam > S3.bam\n",
    "samtools view -S -b S4.sam > S4.bam"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "samtools view -S -b S1.sam > S1.bam\n",
    "samtools view -S -b S4.sam > S4.bam"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SRR2919477.1\t16\tchr2\t201828614\t42\t101M\t*\t0\t0\tTTGTTTGGATCTGAGGAAGTTTGCACAGTAGCTGGCTGAGTGGTAGCTGTTTGTCTTGTAGCAATCTTTAAAAAGTGTTCTTTGGGGTCGGGCGCAGTGTN\tBBBBBBBBBBBBB<BBBBBBBFBBBBBBBB<<B7BBBFBFFFFBB0FFFFFIFFFFFIFFFBFBFFFFFBBFFFBFBFFFBBFFFBIFF<B7F7FFF<00#\tAS:i:-1\tXN:i:0\tXM:i:1\tXO:i:0\tXG:i:0\tNM:i:1\tMD:Z:100C0\tYT:Z:UU\r\n",
      "SRR2919477.2\t0\tchr3\t61375604\t42\t101M\t*\t0\t0\tNTATGTCATAGATATTTTACTACAATAGAAAAGGCAAATATAGAAAGAGAAAGAGATGACTAGTTTTGTTCACCACTGTATGCGCAAGGGCTACAGCAATA\t#0<FFFFFFFFFFFFFFFFBIIIIIIFIFIIIIIIFFIFFFIIFFIIIFFIIIIIFBFFFFIIIIIIIIFIIIFIIIFFFFFFFBFBBBBBBBBBBBBBBB\tAS:i:-1\tXN:i:0\tXM:i:1\tXO:i:0\tXG:i:0\tNM:i:1\tMD:Z:0T100\tYT:Z:UU\r\n",
      "SRR2919477.3\t16\tchr14\t73491894\t33\t101M\t*\t0\t0\tAGCCTCCCAAGGAGCTGGGATTACACGCGCTAGCCACAATGCCCAGCTAATTTTTGTATTTTTAGTAGAGACGGGGTTTCACCATGTTGGCCAGGCTGGTN\t7FB<BBB<BBFFBBBBFFFFFB<B7BBBFBFFFFFFFFFIIIIIIIIIIIIIIFIIIFFIIIIIIIIFIIFIIIIIIIIIFIIIIIIIFFFFFFFFFF<0#\tAS:i:-1\tXS:i:-60\tXN:i:0\tXM:i:1\tXO:i:0\tXG:i:0\tNM:i:1\tMD:Z:100C0\tYT:Z:UU\r\n",
      "SRR2919477.4\t16\tchr20\t49356059\t42\t101M\t*\t0\t0\tTAAGCATTAGACCACTCCCAGGGCTCTGGAATTCCACTGATTATGTATCTTTGCTTTCCTGGGTTTGGAGGAACTAAAGTCCACCAACAAAATAAACCTCC\tFFBBBBBFBBB7<BFFFFFBFFFFFFFFFFFFIIFFIIIIIFIF<IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIFIIIIIIFFFFFFFFFFBBB\tAS:i:0\tXN:i:0\tXM:i:0\tXO:i:0\tXG:i:0\tNM:i:0\tMD:Z:101\tYT:Z:UU\r\n",
      "SRR2919477.5\t0\tchr1\t149832555\t1\t101M\t*\t0\t0\tNAGATCCGCTTAACGCCGCCACGCCGAGCTAGACGCCGAATGGCAGGCTTGGTGATGCCCTGAATGTTGTCTCTCAAGACCTTGCGGTGGCGCTTAGCGCC\t#0<FFFFFFFFFFIIIIIIIIIIIIIIIIIIIIIIFFFFFFFFFFFFFFFFFBBBFFFFFFFBFBFFFFFFFFBFFFFFBBFFFFFFBFFFFBFFBFFBFF\tAS:i:-1\tXS:i:-1\tXN:i:0\tXM:i:1\tXO:i:0\tXG:i:0\tNM:i:1\tMD:Z:0G100\tYT:Z:UU\r\n",
      "SRR2919477.6\t0\tchr11\t66273531\t37\t101M\t*\t0\t0\tTGCCCCAGTCTCCCGAGTAGCTGGTATAACAGGCGCCCGCCACCACACCCAGCTAATTTTTGTATTTTTTTTAGTAGAGACGGGGTTTTACTATGTTGGCT\tBB<BBFB<B0BBBFF<B<F<FBFB<BFFBFF<BBF7BFBBBFFFFF<FBF<B<<B'<BBBFB<7BBFBBBBBBBBBFBB<<<<BBB77<BBBFFB0<00B0\tAS:i:0\tXS:i:-48\tXN:i:0\tXM:i:0\tXO:i:0\tXG:i:0\tNM:i:0\tMD:Z:101\tYT:Z:UU\r\n",
      "SRR2919477.7\t4\t*\t0\t0\t*\t*\t0\t0\tGATCGGAAGAGCACACGTCTGAACTCCAGTCACTCGAAGATCTCGTATGCCGTCTTCTGCTTGAAAAAAAAAAAAGAAAACACGGAGGGATGCCGCAGGCG\tBBBFFFBFFFFFFFIIIFFFFFFIIFFFFFFFBFFFFFIFFFI<FBFFFFFFFFFFFFIFFFF<<7<777B70<0'00'0'07707'070'0''''''700\tYT:Z:UU\r\n",
      "SRR2919477.8\t16\tchr2\t187296576\t27\t101M\t*\t0\t0\tGGTAGAAACCTGGGTGTTGGTAGCTGGGTCCCTCATGAGCATGGAGAAGGCACTGAAGCAGCTGGAAGTGCAGAGCACCAAGAAGCAGTGAGCTTTTGCTN\tBBFBBBBB77B7BBBBBBBFFBFFFFFFB<07<<BFB<FFFFIFFFFBB<FBFFFFFFFBFFFFFBFFIIIFBBFBFBFIFFF<FFFFFFBFFBBFFF<0#\tAS:i:-1\tXS:i:-55\tXN:i:0\tXM:i:1\tXO:i:0\tXG:i:0\tNM:i:1\tMD:Z:100G0\tYT:Z:UU\r\n",
      "SRR2919477.9\t0\tchr5\t108632692\t42\t101M\t*\t0\t0\tNTGGAAGCCATTATTCTAAGTGAAGTAACTAAGGAATGGAACATCAAATACTGCACATTCTCACTTACAAGGGGGAGCTAAGCTATGGGTACACAAAGCCT\t#0<FFFFFFFFFFIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIFIIIIIIIIIIIIIIIIIIIIIIIIIIIFFFFFFFFFFFFFFFFFFFFFFFFFFB\tAS:i:-1\tXN:i:0\tXM:i:1\tXO:i:0\tXG:i:0\tNM:i:1\tMD:Z:0C100\tYT:Z:UU\r\n",
      "SRR2919477.10\t16\tchr4\t116809655\t42\t101M\t*\t0\t0\tACAAATTATTGAAGTGACTATGAGACATTAGTGGTCTTTTAAAATAATACAACATTCAGGAACTAATCAACTTACCCCAAAATTATTTTTAGCATTGAAAN\t<FFFFFFFFFFFFFFFFFFFFFFFFFIIIFIIIIIIFFIIIIIIIIIIIIIFIIIIIFIIFFIIIIIIFFFFFBIIIIIIIIIIFFFFFFFFFFFFFF<0#\tAS:i:-1\tXN:i:0\tXM:i:1\tXO:i:0\tXG:i:0\tNM:i:1\tMD:Z:100A0\tYT:Z:UU\r\n",
      "SRR2919477.11\t0\tchr8\t135362563\t38\t101M\t*\t0\t0\tTCTTGTGCCTGTTTTCAAATGGAATGCTTCCAGCTTTTGCCCATTCAGTATGATATTGGCTGTGGATTTGTCATAAATGGCTATTATTTTGAGTATGTTCC\tBBBFFFFFFFFFFIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIFIIFIIIIIIIIIFFFBFIIIFFIIIIIIIIIFFFFFFFFFFBFBFBFFFFB\tAS:i:0\tXS:i:-49\tXN:i:0\tXM:i:0\tXO:i:0\tXG:i:0\tNM:i:0\tMD:Z:101\tYT:Z:UU\r\n",
      "SRR2919477.12\t0\tchr4\t151332999\t42\t101M\t*\t0\t0\tNCAATCATTGCATAAAATCTGTGGAATTGAAGTGGTAAACCAAAAAGAGGAGGGGGATGAAACTCTTATGTGACAATATGCTAACCAATACATAAGATACA\t#0<FFFFFFFFFFFFIIIIIIIFFFFIIIIIIIIIFFIIIIIIIIIIFFIBFIIIIFFFFFFFFFFFFFFFFFFFFBFFFFFFFFFFFFFFFFFFFFFFFB\tAS:i:-1\tXN:i:0\tXM:i:1\tXO:i:0\tXG:i:0\tNM:i:1\tMD:Z:0G100\tYT:Z:UU\r\n",
      "SRR2919477.13\t0\tchr3\t132101967\t42\t101M\t*\t0\t0\tNCCTCCTATGCTCATCAGAGTCCAAAGTCCAGAGGGGGCCAAAGCAGCAGGGGCTGGCATGTCAGCACTGCTCCAAGTGTTTATACTCCCAGCCGGGCTGC\t#0<FFFFFFFFFFIIIIIIIIIIIIIIFIIIIIIIIIIIIIIIIIIIIIIIIFFFFFFFFFFBFFFFFFBBBFFBFBBBFFFFFFFBFFFFBFFFFFFFFB\tAS:i:-1\tXN:i:0\tXM:i:1\tXO:i:0\tXG:i:0\tNM:i:1\tMD:Z:0T100\tYT:Z:UU\r\n",
      "SRR2919477.14\t0\tchrX\t125452454\t42\t101M\t*\t0\t0\tNCTGTTCAGTTATTCGACCCAACACTGCATATGATTCTCTCTTATCTTTGCTAATTTAACAGGTAAAAATGTATTCCTATTTGTGTTTGTTTGACTATTGG\t#0<FFFFFFFFFFIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIFIIIFIIIFFIIIIIFFFFFFFFFFFFFFFFFFFFFFF\tAS:i:-1\tXN:i:0\tXM:i:1\tXO:i:0\tXG:i:0\tNM:i:1\tMD:Z:0A100\tYT:Z:UU\r\n",
      "SRR2919477.15\t0\tchr2\t89867398\t3\t101M\t*\t0\t0\tCCATTCCATTTGGGTTGAATCCATTCCATTCCATTCCATTCCATTCCATTCCATTCTATTCCATTGCACTAGGTTTGATTCCATTCCACTGCATTCCATTC\tBBBFFFFFFFFFFIIIIB<BBFFIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIFFFFFFFFFFF\tAS:i:-42\tXN:i:0\tXM:i:8\tXO:i:0\tXG:i:0\tNM:i:8\tMD:Z:10C0C0A5T37C8C22T1C10\tYT:Z:UU\r\n",
      "SRR2919477.16\t0\tchr3\t197010131\t42\t101M\t*\t0\t0\tNATTCTACAGATAGGAGTTATTACCGTTCACCACCAATATCTAAGCTGGGAATGTAAATCCCTCAAGTGGTTTTAATTGCCCTTCTAGCCAAAGCCAAAGC\t#0BFFFFFFFFFFIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIFIFFFFFFFFFFFFFFFFFFFFFFFFFFFFF\tAS:i:-1\tXN:i:0\tXM:i:1\tXO:i:0\tXG:i:0\tNM:i:1\tMD:Z:0A100\tYT:Z:UU\r\n",
      "SRR2919477.17\t0\tchr14\t37684811\t42\t101M\t*\t0\t0\tAATAAAGTAGGTGGAGTAAGTTTTTACTCATCAATAACAATGTTGAATATAAATGGACTAAACTCTCCAGTCAGAAGACATAGCATGGCTGAATTGATGAA\tBBBFFFFFFFFFFIIIFFIIIIIIIIIIIIIIIIIIIIIIIIFIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIFFFFFFFFFFFFFFFFFFFFFF\tAS:i:0\tXN:i:0\tXM:i:0\tXO:i:0\tXG:i:0\tNM:i:0\tMD:Z:101\tYT:Z:UU\r\n",
      "SRR2919477.18\t16\tchr8\t38020518\t0\t101M\t*\t0\t0\tAGTAGAGACGGGTTTTCCCCACGTTGGCTGGGCTGGTCCTGACCCCCCACCCCCGGGTGACCCGCCCGCCTCGGCATCCCAAAGTGCTGGGATTACAGGCA\t'''00'0''070'B0'7'700''70B70''<0''70'70''0'7'77000B7'''B70'''70'7IIIIIIIIIIIIIIIIIIIIIIIFFFFFFFFFFBBB\tAS:i:-44\tXS:i:-53\tXN:i:0\tXM:i:19\tXO:i:0\tXG:i:0\tNM:i:19\tMD:Z:9A2G4T3T5T0C0A2A5T1C1A1T3G0A2T1A5T1T0A37\tYT:Z:UU\r\n",
      "SRR2919477.19\t0\tchr4\t38737420\t42\t101M\t*\t0\t0\tNAGGACAGCAAAAGCCCAGCTCAGCCCTCCACTAGGGTATAGTAGCCCTGTAGGTAAGCTTGGAGGAAGTCAAAATTTGAAGCTCTATCTTGGGCTGCACA\t#0<FFFFFFFFFFIIIIIIIIIIIIIIFIIIIIIIIIBFFIIIIIIIIIIFFIIFFFFIIIIIIFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF<\tAS:i:-7\tXN:i:0\tXM:i:2\tXO:i:0\tXG:i:0\tNM:i:2\tMD:Z:0C40A59\tYT:Z:UU\r\n",
      "SRR2919477.20\t0\tchr5\t26480420\t1\t101M\t*\t0\t0\tNGAATAAAATACCTAGGAATCCAACTTACAAGGGATGTGAAGGACCTCTTCAAGGAGAACTACAAACCACTGCTCAAGGAAATAAAAGAGGATACAAACAA\t#0<FFFFFFFFFFIIIIIIFIIIIIIIIIIIIFIBFFFIIIIIIFFIIIIIIIIIIFFIIIIIIIIIIIFFFFFFFFFFFFFFBFFFFBFFFFFFFFFFFB\tAS:i:-1\tXS:i:-1\tXN:i:0\tXM:i:1\tXO:i:0\tXG:i:0\tNM:i:1\tMD:Z:0C100\tYT:Z:UU\r\n",
      "samtools view: writing to standard output failed: Broken pipe\r\n",
      "samtools view: error closing standard output: -1\r\n"
     ]
    }
   ],
   "source": [
    "samtools view S4.bam | head -20"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[bam_sort_core] merging from 13 files...\r\n",
      "[W::bam_hdr_read] EOF marker is absent. The input is probably truncated.\r\n",
      "[W::bam_hdr_read] EOF marker is absent. The input is probably truncated.\r\n",
      "[bam_merge_core] 'S1.bam.tmp.0005.bam' is truncated. Continue anyway.\r\n",
      "[bam_merge_core] 'S1.bam.tmp.0004.bam' is truncated. Continue anyway.\r\n",
      "[bam_sort_core] merging from 13 files...\r\n",
      "[W::bam_hdr_read] EOF marker is absent. The input is probably truncated.\r\n",
      "[bam_merge_core] 'S2.bam.tmp.0004.bam' is truncated. Continue anyway.\r\n",
      "[bam_sort_core] merging from 5 files...\r\n",
      "[bam_sort_core] merging from 10 files...\r\n"
     ]
    }
   ],
   "source": [
    "samtools sort -O bam -o S1.sorted.bam S1.bam\n",
    "samtools sort -O bam -o S2.sorted.bam S2.bam\n",
    "samtools sort -O bam -o S3.sorted.bam S3.bam\n",
    "samtools sort -O bam -o S4.sorted.bam S4.bam"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[bam_sort_core] merging from 13 files...\r\n",
      "[bam_sort_core] merging from 13 files...\r\n"
     ]
    }
   ],
   "source": [
    "samtools sort -O bam -o S1.sorted.bam S1.bam\n",
    "samtools sort -O bam -o S4.sorted.bam S4.bam"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "~\t\tS1.sam\t\tS2_fastqc.zip  S3_fastqc.html  S4.fastq\r\n",
      "S1.bam\t\tS1.sorted.bam\tS2.sam\t       S3_fastqc.zip   S4_fastqc.html\r\n",
      "S1.fastq\tS2.bam\t\tS2.sorted.bam  S3.sam\t       S4_fastqc.zip\r\n",
      "S1_fastqc.html\tS2.fastq\tS3.bam\t       S3.sorted.bam   S4.sam\r\n",
      "S1_fastqc.zip\tS2_fastqc.html\tS3.fastq       S4.bam\t       S4.sorted.bam\r\n"
     ]
    }
   ],
   "source": [
    "ls"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[W::bam_hdr_read] EOF marker is absent. The input is probably truncated.\r\n",
      "samtools index: \"S1.sorted.bam\" is corrupted or unsorted\r\n",
      "samtools index: \"S4.sorted.bam\" is corrupted or unsorted\r\n"
     ]
    }
   ],
   "source": [
    "samtools index S1.sorted.bam\n",
    "samtools index S2.sorted.bam\n",
    "samtools index S3.sorted.bam\n",
    "samtools index S4.sorted.bam"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "samtools index S1.sorted.bam\n",
    "samtools index S4.sorted.bam"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "~\t\t   S2.bam\t      S3.fastq\t\t S4_fastqc.html\r\n",
      "S1.bam\t\t   S2.fastq\t      S3_fastqc.html\t S4_fastqc.zip\r\n",
      "S1.fastq\t   S2_fastqc.html     S3_fastqc.zip\t S4.sam\r\n",
      "S1_fastqc.html\t   S2_fastqc.zip      S3.sam\t\t S4.sorted.bam\r\n",
      "S1_fastqc.zip\t   S2.sam\t      S3.sorted.bam\t S4.sorted.bam.bai\r\n",
      "S1.sam\t\t   S2.sorted.bam      S3.sorted.bam.bai\r\n",
      "S1.sorted.bam\t   S2.sorted.bam.bai  S4.bam\r\n",
      "S1.sorted.bam.bai  S3.bam\t      S4.fastq\r\n"
     ]
    }
   ],
   "source": [
    "ls"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# look at bam files in IGV"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Then the regions of the genome where the chiped protein is bound has to be predicted by finding regions with significant numbers of mapped reads (peaks). The input DNA can be used as a control sample for the use of the mapped reads."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## STEP 3: Peak calling with Macs2\n",
    "\n",
    "Macs (model-based analysis of chip-seq) is a peak caller which supports both single and paired-end sequencing. It uses cross-correlation to find the lag between the reads mapped to the minus and the plus strans as the size of actual protein-DNA interacting regions.\n",
    "\n",
    "- Remove duplicate tags\n",
    "- Find candidate peaks with a significant tag enrichment\n",
    "- Merge overlapping peaks \n",
    "- Look at background levels and eliminates peaks that are not significant with respect to local background \n",
    "- Uses the control sample to eliminate peaks that are also present there\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "usage: macs2 [-h] [--version]\r\n",
      "             {callpeak,bdgpeakcall,bdgbroadcall,bdgcmp,bdgopt,cmbreps,bdgdiff,filterdup,predictd,pileup,randsample,refinepeak}\r\n",
      "             ...\r\n",
      "macs2: error: too few arguments\r\n"
     ]
    }
   ],
   "source": [
    "macs2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO  @ Tue, 24 Jan 2017 12:16:35: \r\n",
      "# Command line: callpeak -t S2.sorted.bam -c S1.sorted.bam -f BAM -g hs -n RUNX\r\n",
      "# ARGUMENTS LIST:\r\n",
      "# name = RUNX\r\n",
      "# format = BAM\r\n",
      "# ChIP-seq file = ['S2.sorted.bam']\r\n",
      "# control file = ['S1.sorted.bam']\r\n",
      "# effective genome size = 2.70e+09\r\n",
      "# band width = 300\r\n",
      "# model fold = [5, 50]\r\n",
      "# qvalue cutoff = 5.00e-02\r\n",
      "# Larger dataset will be scaled towards smaller dataset.\r\n",
      "# Range for calculating regional lambda is: 1000 bps and 10000 bps\r\n",
      "# Broad region calling is off\r\n",
      " \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:16:35: #1 read tag files... \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:16:35: #1 read treatment tags... \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:16:41:  1000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:16:45:  2000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:16:51:  3000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:16:56:  4000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:17:01:  5000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:17:06:  6000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:17:12:  7000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:17:17:  8000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:17:23:  9000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:17:28:  10000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:17:32:  11000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:17:38:  12000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:17:43:  13000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:17:47:  14000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:17:52:  15000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:17:57:  16000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:18:02:  17000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:18:07:  18000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:18:11:  19000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:18:17:  20000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:18:21:  21000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:18:26:  22000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:18:29:  23000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:18:32: #1.2 read input tags... \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:18:39:  1000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:18:45:  2000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:18:51:  3000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:18:58:  4000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:19:06:  5000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:19:12:  6000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:19:17:  7000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:19:23:  8000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:19:29:  9000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:19:35:  10000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:19:41:  11000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:19:46:  12000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:19:53:  13000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:20:00:  14000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:20:06:  15000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:20:12:  16000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:20:18:  17000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:20:25:  18000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:20:31:  19000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:20:37:  20000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:20:42:  21000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:20:47:  22000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:20:53:  23000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:20:59:  24000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:21:06:  25000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:21:11:  26000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:21:17:  27000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:21:21:  28000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:21:24:  29000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:21:28:  30000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:21:28: #1 tag size is determined as 101 bps \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:21:28: #1 tag size = 101 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:21:28: #1  total tags in treatment: 22007755 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:21:28: #1 user defined the maximum tags... \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:21:28: #1 filter out redundant tags at the same location and the same strand by allowing at most 1 tag(s) \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:21:29: #1  tags after filtering in treatment: 2550992 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:21:29: #1  Redundant rate of treatment: 0.88 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:21:29: #1  total tags in control: 27848881 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:21:29: #1 user defined the maximum tags... \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:21:29: #1 filter out redundant tags at the same location and the same strand by allowing at most 1 tag(s) \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:21:29: #1  tags after filtering in control: 25809686 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:21:29: #1  Redundant rate of control: 0.07 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:21:29: #1 finished! \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:21:29: #2 Build Peak Model... \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:21:29: #2 looking for paired plus/minus strand peaks... \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:21:38: #2 number of paired peaks: 298943 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:21:38: start model_add_line... \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:21:38: start X-correlation... \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:21:38: end of X-cor \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:21:38: #2 finished! \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:21:38: #2 predicted fragment length is 177 bps \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:21:38: #2 alternative fragment length(s) may be 177 bps \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:21:38: #2.2 Generate R script for model : RUNX_model.r \r\n",
      "WARNING @ Tue, 24 Jan 2017 12:21:38: #2 Since the d (177) calculated from paired-peaks are smaller than 2*tag length, it may be influenced by unknown sequencing problem! \r\n",
      "WARNING @ Tue, 24 Jan 2017 12:21:38: #2 You may need to consider one of the other alternative d(s): 177 \r\n",
      "WARNING @ Tue, 24 Jan 2017 12:21:38: #2 You can restart the process with --nomodel --extsize XXX with your choice or an arbitrary number. Nontheless, MACS will continute computing. \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:21:38: #3 Call peaks... \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:21:38: #3 Pre-compute pvalue-qvalue table... \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:23:15: #3 Call peaks for each chromosome... \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:24:06: #4 Write output xls file... RUNX_peaks.xls \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:24:06: #4 Write peak in narrowPeak format file... RUNX_peaks.narrowPeak \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:24:06: #4 Write summits bed file... RUNX_summits.bed \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:24:06: Done! \r\n"
     ]
    }
   ],
   "source": [
    "#macs2 gebruikt en de sorted files ingevoegd met telkens -c S1 en S3 als de controle inputfiles. dan hs gebruikt voor het hele humaan genoom\n",
    "mkdir macs2_RUNX\n",
    "macs2 callpeak -t  S2.sorted.bam -c S1.sorted.bam -f BAM -g hs -n RUNX"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO  @ Tue, 24 Jan 2017 12:25:21: \r\n",
      "# Command line: callpeak -t S4.sorted.bam -c S3.sorted.bam -f BAM -g hs -n RUNX1\r\n",
      "# ARGUMENTS LIST:\r\n",
      "# name = RUNX1\r\n",
      "# format = BAM\r\n",
      "# ChIP-seq file = ['S4.sorted.bam']\r\n",
      "# control file = ['S3.sorted.bam']\r\n",
      "# effective genome size = 2.70e+09\r\n",
      "# band width = 300\r\n",
      "# model fold = [5, 50]\r\n",
      "# qvalue cutoff = 5.00e-02\r\n",
      "# Larger dataset will be scaled towards smaller dataset.\r\n",
      "# Range for calculating regional lambda is: 1000 bps and 10000 bps\r\n",
      "# Broad region calling is off\r\n",
      " \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:25:21: #1 read tag files... \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:25:21: #1 read treatment tags... \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:25:25:  1000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:25:29:  2000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:25:33:  3000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:25:37:  4000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:25:42:  5000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:25:46:  6000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:25:50:  7000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:25:54:  8000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:25:58:  9000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:26:02:  10000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:26:06:  11000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:26:10:  12000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:26:15:  13000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:26:19:  14000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:26:23:  15000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:26:27:  16000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:26:31:  17000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:26:35:  18000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:26:39:  19000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:26:43:  20000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:26:47:  21000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:26:51:  22000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:26:56:  23000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:27:00:  24000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:27:04:  25000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:27:08:  26000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:27:12:  27000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:27:16:  28000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:27:19:  29000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:27:22: #1.2 read input tags... \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:27:27:  1000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:27:32:  2000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:27:37:  3000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:27:42:  4000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:27:47:  5000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:27:52:  6000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:27:58:  7000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:28:03:  8000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:28:08:  9000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:28:13:  10000000 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:28:16: #1 tag size is determined as 101 bps \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:28:16: #1 tag size = 101 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:28:16: #1  total tags in treatment: 28187369 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:28:16: #1 user defined the maximum tags... \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:28:16: #1 filter out redundant tags at the same location and the same strand by allowing at most 1 tag(s) \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:28:17: #1  tags after filtering in treatment: 7203889 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:28:17: #1  Redundant rate of treatment: 0.74 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:28:17: #1  total tags in control: 9872550 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:28:17: #1 user defined the maximum tags... \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:28:17: #1 filter out redundant tags at the same location and the same strand by allowing at most 1 tag(s) \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:28:18: #1  tags after filtering in control: 8946186 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:28:18: #1  Redundant rate of control: 0.09 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:28:18: #1 finished! \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:28:18: #2 Build Peak Model... \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:28:18: #2 looking for paired plus/minus strand peaks... \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:28:21: #2 number of paired peaks: 67064 \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:28:21: start model_add_line... \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:28:21: start X-correlation... \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:28:21: end of X-cor \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:28:21: #2 finished! \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:28:21: #2 predicted fragment length is 173 bps \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:28:21: #2 alternative fragment length(s) may be 173 bps \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:28:21: #2.2 Generate R script for model : RUNX1_model.r \r\n",
      "WARNING @ Tue, 24 Jan 2017 12:28:21: #2 Since the d (173) calculated from paired-peaks are smaller than 2*tag length, it may be influenced by unknown sequencing problem! \r\n",
      "WARNING @ Tue, 24 Jan 2017 12:28:21: #2 You may need to consider one of the other alternative d(s): 173 \r\n",
      "WARNING @ Tue, 24 Jan 2017 12:28:21: #2 You can restart the process with --nomodel --extsize XXX with your choice or an arbitrary number. Nontheless, MACS will continute computing. \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:28:21: #3 Call peaks... \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:28:21: #3 Pre-compute pvalue-qvalue table... \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:29:00: #3 Call peaks for each chromosome... \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:29:20: #4 Write output xls file... RUNX1_peaks.xls \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:29:20: #4 Write peak in narrowPeak format file... RUNX1_peaks.narrowPeak \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:29:20: #4 Write summits bed file... RUNX1_summits.bed \r\n",
      "INFO  @ Tue, 24 Jan 2017 12:29:20: Done! \r\n"
     ]
    }
   ],
   "source": [
    "macs2 callpeak -t  S4.sorted.bam -c S3.sorted.bam -f BAM -g hs -n RUNX1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "cd macs2_RUNX\n",
    "ls"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10914\r\n"
     ]
    }
   ],
   "source": [
    "# to count the total of peaks\n",
    "cat RUNX_summits.bed | wc -l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "chr1\t145208751\t145209328\tRUNX_peak_528\t2841\t.\t59.26764\t292.91473\t284.12222\t282\r\n",
      "chr1\t121483988\t121485495\tRUNX_peak_515\t2094\t.\t9.78081\t216.87074\t209.49983\t1269\r\n",
      "chr15\t70755033\t70755782\tRUNX_peak_3148\t1937\t.\t73.76989\t200.79581\t193.73245\t394\r\n",
      "chr16\t12009687\t12010180\tRUNX_peak_3456\t1641\t.\t63.54353\t170.92520\t164.10767\t246\r\n",
      "chr2\t11638662\t11639087\tRUNX_peak_5169\t1491\t.\t60.41454\t155.78967\t149.14822\t194\r\n",
      "sort: write failed: standard output: Broken pipe\r\n",
      "sort: write error\r\n"
     ]
    }
   ],
   "source": [
    "cat RUNX_peaks.narrowPeak | sort -k9 -g -r | head -5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "12812\r\n"
     ]
    }
   ],
   "source": [
    "cat RUNX1_summits.bed | wc -l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "chr16\t12009727\t12010253\tRUNX1_peak_4271\t3975\t.\t109.13633\t407.07169\t397.58020\t242\r\n",
      "chr2\t11638620\t11639103\tRUNX1_peak_6318\t3414\t.\t100.26329\t348.93518\t341.46072\t250\r\n",
      "chr1\t145208737\t145209479\tRUNX1_peak_542\t3389\t.\t38.02754\t346.41064\t338.97223\t305\r\n",
      "chr1\t205180321\t205181031\tRUNX1_peak_974\t3062\t.\t72.78316\t313.35925\t306.24615\t521\r\n",
      "chr15\t50646618\t50647924\tRUNX1_peak_3640\t2878\t.\t69.33554\t294.79651\t287.85281\t980\r\n",
      "sort: write failed: standard output: Broken pipe\r\n",
      "sort: write error\r\n"
     ]
    }
   ],
   "source": [
    "cat RUNX1_peaks.narrowPeak | sort -k9 -g -r | head -5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# load bed files to igv"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Figure 5: loading of bed files to IGV"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img width=\"50%\" src=\"/user/r0618772/files/IGV.JPG\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## STEP 4 : motif analysis\n",
    "\n",
    "\n",
    "Motif analysis is useful for much more than just identifying the causal DNA-binding motif in TF ChIP-seq peaks. When the motif of the ChIPed protein is already known, motif analysis provides validation of the success of the experiment. Even when the motif is not known beforehand, identifying a centrally located motif in a large fraction of the peaks by motif analysis is indicative of a successful experiment. Motif analysis can also identify the DNA-binding motifs of other proteins that bind in complex or in conjunction with the ChIPed protein, illuminating the mechanisms of transcriptional regulation. Motif analysis is also useful with histone modification ChIP-seq because it can discover unanticipated sequence signals associated with such marks. \n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We loaded the bed files from replicate 1 and 2 (RUNX and RUNX1 are the names of the files respectively) in IGV. We found that RUNX1 binds to the promotor regions of NEAT1 and MALAT1. (invoegen foto's van IGV)\n",
    "\n",
    "We perfomed icis-target, you can find the links for respectively replicate 1 and 2 [Here](https://gbiomed.kuleuven.be/apps/lcb/i-cisTarget/reports/4823c681009bb3eae6bb6189fab55b96426ab95d/report.html) and [Here](https://gbiomed.kuleuven.be/apps/lcb/i-cisTarget/reports/e62ba80c6d20e39357d621d3aec666bd06e7be79/report.html)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## STEP 5: enhancer-to-gene association\n",
    "\n",
    "We have linked the peaks to genes with UCSC, you can find the link to the report [Here](https://genome.ucsc.edu/cgi-bin/hgTracks?db=hg19&lastVirtModeType=default&lastVirtModeExtraState=&virtModeType=default&virtMode=0&nonVirtPosition=&position=chr11%3A64073044%2D64084210&hgsid=576694635_ZuucNUI0Otw93sy1QbNEoDMiFaSK) .\n",
    "\n",
    "Although we haven't found the genes from the article via icis-target, we searched the set of genes from the article. Here we found that our gene RUNX1 has an influence on the expressionlevel of this set of genes."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## STEP 6: link CHIP-seq data to RNA-seq data\n",
    "\n",
    "overall conclusion of the experiment (chip seq data en rna-seq data beiden in IGV --> neat en malat)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Bash",
   "language": "bash",
   "name": "bash"
  },
  "language_info": {
   "codemirror_mode": "shell",
   "file_extension": ".sh",
   "mimetype": "text/x-sh",
   "name": "bash"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
