{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5. Chipseq analysis\n",
    "\n",
    "We chose NFKB1 as a possible regulator of the upregulated genes in rheumatoid arthritis. Now the next question is: does NFKB1 bind to these upregulated genes? This can be investigated by use of Chipseq. In ChIP-Atlas we searched for a chipseq data set of NFKB1 and found one performed in an Hodgkin Lymphoma Cell Line. Ideally, we would analyze chipseq data of NFKB1 in a rheumatoid arthritis patient, however we did not found one. \n",
    "\n",
    "The chipseq data can be found on the following website:\n",
    "\n",
    "https://www.ncbi.nlm.nih.gov/geo/query/acc.cgi?acc=GSM1556335\n",
    "\n",
    "### 5.1 Convert SRA to fastq\n",
    "\n",
    "First make a directory to store the files in:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "mkdir -p /mnt/nfs/r0368280/jupyternotebooks/NFKB1/\n",
    "cd /mnt/nfs/r0368280/jupyternotebooks/NFKB1/"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Second, we make a new directory where we simulatenously are going to download the Chipseq data and convert SRA to fastq. \n",
    "\n",
    "The original files can be found in /mnt/nfs/r0368280/jupyternotebooks/NCBI/\n",
    "\n",
    "under the account of r0368280"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "refseq\tsra  SRR1686077_1.fastq\r\n"
     ]
    }
   ],
   "source": [
    "mkdir /mnt/nfs/r0368280/jupyternotebooks/NCBI\n",
    "cd /mnt/nfs/r0368280/jupyternotebooks/NCBI/\n",
    "\n",
    "vdb-config -s /repository/user/main/public/root=/mnt/nfs/r0368280/jupyternotebooks/NCBI\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "fastq-dump --split-files SRR1686077"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5.2 Alignment\n",
    "Now we have the chipseq data in fastq format. Subsequently we are going to align this data to the entire genome by using  bowtie2. This can take a long time (several hours)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "39932937 reads; of these:\r\n",
      "  39932937 (100.00%) were unpaired; of these:\r\n",
      "    9435278 (23.63%) aligned 0 times\r\n",
      "    21183429 (53.05%) aligned exactly 1 time\r\n",
      "    9314230 (23.32%) aligned >1 times\r\n",
      "76.37% overall alignment rate\r\n"
     ]
    }
   ],
   "source": [
    "bowtie2-align -x /mnt/nfs/data/resources/bowtie2/hg19 /mnt/storage/r0368280/jupyternotebooks/NCBI/SRR1686077_1.fastq -S ChIP_NFKB1.sam\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We look at the aligment with the head command to see if everything went fine."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SRR1686077.410\t16\tchr1\t569908\t31\t36M\t*\t0\t0\tACTAACCATATACCAATGATGGCGCGATGTAACACG\t@DFGEGIGGIGFIIIIIIIIIIIIIIIIIIIIIIII\tAS:i:0\tXS:i:-6\tXN:i:0\tXM:i:0\tXO:i:0\tXG:i:0\tNM:i:0\tMD:Z:36\tYT:Z:UU\r\n",
      "SRR1686077.411\t16\tchr1\t569908\t31\t36M\t*\t0\t0\tACTAACCATATACCAATGATGGCGCGATGTAACACG\t###BFFDGGGGG@GGGGEGEEGEEGBHFHFHGGHHH\tAS:i:0\tXS:i:-5\tXN:i:0\tXM:i:0\tXO:i:0\tXG:i:0\tNM:i:0\tMD:Z:36\tYT:Z:UU\r\n",
      "SRR1686077.412\t16\tchr1\t569908\t31\t36M\t*\t0\t0\tACTAACCATATACCAATGATGGCGCGATGTAACACG\t:D<FEFBB>@>EGGGGGGGGGDGGD?EBFEF=@==B\tAS:i:0\tXS:i:-5\tXN:i:0\tXM:i:0\tXO:i:0\tXG:i:0\tNM:i:0\tMD:Z:36\tYT:Z:UU\r\n",
      "SRR1686077.413\t16\tchr1\t569908\t31\t36M\t*\t0\t0\tACTAACCATATACCAATGATGGCGCGATGTAACACG\tBECDCFDDBDD?EBDDGH?HHHHHGHHHHHHGGGGF\tAS:i:0\tXS:i:-5\tXN:i:0\tXM:i:0\tXO:i:0\tXG:i:0\tNM:i:0\tMD:Z:36\tYT:Z:UU\r\n",
      "SRR1686077.414\t16\tchr1\t569908\t31\t36M\t*\t0\t0\tACTAACCATATACCAATGATGGCGCGATGTAACACG\tCBCFAFGGBGGDGGGDHBHHHHFHFHHHHHHHHHHH\tAS:i:0\tXS:i:-5\tXN:i:0\tXM:i:0\tXO:i:0\tXG:i:0\tNM:i:0\tMD:Z:36\tYT:Z:UU\r\n"
     ]
    }
   ],
   "source": [
    "head -500 ChIP_NFKB1.sam | tail -5 "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5.3 Convert SAM to BAM file \n",
    "\n",
    "The SAM file is unsorted and uncompressed. To be able to quickly access the data we are going to convert it to a BAM file, which is a compressed (much smaller) version of a SAM file, and sort the file. The last step is to create an index for quick access. We use samtools to do this."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "samtools view -S -b ChIP_NFKB1.sam > ChIP_NFKB1.bam"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[bam_sort_core] merging from 10 files...\r\n"
     ]
    }
   ],
   "source": [
    "samtools sort -O bam -o ChIP_NFKB1.sorted.bam ChIP_NFKB1.bam "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "samtools index ChIP_NFKB1.sorted.bam"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ChIP_NFKB1.bam\tChIP_NFKB1.sorted.bam\t   refseq  SRR1686077_1.fastq\r\n",
      "ChIP_NFKB1.sam\tChIP_NFKB1.sorted.bam.bai  sra\r\n"
     ]
    }
   ],
   "source": [
    "ls"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We now have two sorted bam files and copy these two to our computer and load the BAM file into IGV.\n",
    "If we visualize the BAM file in IGV you see the coverage (how many reads map to a certain position).\n",
    "\n",
    "In the printsceen below you see a lot of coverage for a part of GADD45B. Red is the antisense strand, blue is the sense strand.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img width=\"50%\" src=\"/user/r0368280/files/Examenopdracht/Pictures/Printscreen1.png\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5.4 Call peaks\n",
    "\n",
    "Now we are going to call peaks. With peak calling we try to identify the regions in the genome that are enriched with aligned reads. No control was added because the authors didn't provide one. If you run without control you have less peaks. Why? The program will hesitate more to call something a peak, so you will get less peaks. Also the peaks were called in the entire genome and a q-value of 0.005 was used."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO  @ Fri, 23 Dec 2016 21:08:38: \r\n",
      "# Command line: callpeak -t ChIP_NFKB1.sorted.bam --outdir macs2_NFKB1 -n NFKB1 -g hs -q 0.005\r\n",
      "# ARGUMENTS LIST:\r\n",
      "# name = NFKB1\r\n",
      "# format = AUTO\r\n",
      "# ChIP-seq file = ['ChIP_NFKB1.sorted.bam']\r\n",
      "# control file = None\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-03\r\n",
      "# Larger dataset will be scaled towards smaller dataset.\r\n",
      "# Range for calculating regional lambda is: 10000 bps\r\n",
      "# Broad region calling is off\r\n",
      " \r\n",
      "INFO  @ Fri, 23 Dec 2016 21:08:38: #1 read tag files... \r\n",
      "INFO  @ Fri, 23 Dec 2016 21:08:38: #1 read treatment tags... \r\n",
      "INFO  @ Fri, 23 Dec 2016 21:08:38: Detected format is: BAM \r\n",
      "INFO  @ Fri, 23 Dec 2016 21:08:38: * Input file is gzipped. \r\n",
      "INFO  @ Fri, 23 Dec 2016 21:08:42:  1000000 \r\n",
      "INFO  @ Fri, 23 Dec 2016 21:08:46:  2000000 \r\n",
      "INFO  @ Fri, 23 Dec 2016 21:08:49:  3000000 \r\n",
      "INFO  @ Fri, 23 Dec 2016 21:08:53:  4000000 \r\n",
      "Traceback (most recent call last):\r\n",
      "  File \"/usr/local/bin/macs2\", line 614, in <module>\r\n",
      "    main()\r\n",
      "  File \"/usr/local/bin/macs2\", line 56, in main\r\n",
      "    run( args )\r\n",
      "  File \"/usr/local/lib/python2.7/dist-packages/MACS2/callpeak_cmd.py\", line 70, in run\r\n",
      "    else:       (treat, control) = load_tag_files_options  (options)\r\n",
      "  File \"/usr/local/lib/python2.7/dist-packages/MACS2/callpeak_cmd.py\", line 396, in load_tag_files_options\r\n",
      "    treat = tp.build_fwtrack()\r\n",
      "  File \"MACS2/IO/Parser.pyx\", line 805, in MACS2.IO.Parser.BAMParser.build_fwtrack (MACS2/IO/Parser.c:11368)\r\n",
      "  File \"MACS2/IO/Parser.pyx\", line 810, in MACS2.IO.Parser.BAMParser.build_fwtrack (MACS2/IO/Parser.c:11235)\r\n",
      "  File \"MACS2/IO/Parser.pyx\", line 874, in MACS2.IO.Parser.BAMParser.__build_fwtrack_wo_pysam (MACS2/IO/Parser.c:12256)\r\n",
      "ValueError: read of closed file\r\n"
     ]
    }
   ],
   "source": [
    "mkdir macs2_NFKB1\n",
    "macs2 callpeak -t  ChIP_NFKB1.sorted.bam --outdir macs2_NFKB1 -n NFKB1 -g hs -q 0.005"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It looks like the above command didn't work but this is due to the fact that we redid the command while it actually worked the first time."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ChIP_NFKB1.bam\tChIP_NFKB1.sorted.bam\t   macs2_NFKB1\tsra\r\n",
      "ChIP_NFKB1.sam\tChIP_NFKB1.sorted.bam.bai  refseq\tSRR1686077_1.fastq\r\n"
     ]
    }
   ],
   "source": [
    "ls\n",
    "#you see what is in the macs2_NFKB1 folder"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We are now going to take a look at the file with the peaks to see if everything went fine. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "chr1\t10021\t10142\tNFKB1_peak_1\t95\t.\t6.65150\t12.74406\t9.52473\t50\r\n",
      "chr1\t14481\t14580\tNFKB1_peak_2\t105\t.\t6.33487\t13.84319\t10.58679\t54\r\n",
      "chr1\t29296\t29384\tNFKB1_peak_3\t63\t.\t5.78035\t9.37442\t6.30489\t35\r\n",
      "chr1\t96428\t96567\tNFKB1_peak_4\t428\t.\t18.17081\t46.75889\t42.80931\t76\r\n",
      "chr1\t96631\t96767\tNFKB1_peak_5\t180\t.\t10.38332\t21.51325\t18.03033\t62\r\n",
      "chr1\t150957\t151062\tNFKB1_peak_6\t66\t.\t6.42055\t9.75961\t6.67253\t66\r\n",
      "chr1\t242754\t243105\tNFKB1_peak_7\t492\t.\t18.76245\t53.31419\t49.26944\t84\r\n",
      "chr1\t378088\t378273\tNFKB1_peak_8\t1665\t.\t42.82283\t172.38879\t166.59174\t107\r\n",
      "chr1\t412155\t412449\tNFKB1_peak_9\t345\t.\t15.42760\t38.34253\t34.52510\t230\r\n",
      "chr1\t564664\t564800\tNFKB1_peak_10\t120\t.\t2.81565\t15.37391\t12.06224\t71\r\n"
     ]
    }
   ],
   "source": [
    "head macs2_NFKB1/NFKB1_peaks.narrowPeak"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In the following two commands we sorted on the 5 highest peaks and look at the total number of peaks that were called."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "chr3\t8724705\t8724898\tNFKB1_peak_19016\t2141\t.\t50.75469\t223.63388\t214.14240\t95\r\n",
      "chr20\t42033564\t42033775\tNFKB1_peak_17722\t2095\t.\t44.56903\t218.46510\t209.57567\t111\r\n",
      "chr15\t81068787\t81069028\tNFKB1_peak_9642\t2095\t.\t44.56903\t218.46510\t209.57567\t136\r\n",
      "chr11\t109915401\t109915587\tNFKB1_peak_5586\t2095\t.\t50.62173\t218.42328\t209.57567\t87\r\n",
      "chr4\t3895454\t3895650\tNFKB1_peak_20754\t2080\t.\t48.73769\t216.01567\t208.04269\t100\r\n",
      "sort: write failed: standard output: Broken pipe\r\n",
      "sort: write error\r\n"
     ]
    }
   ],
   "source": [
    "cat macs2_NFKB1/NFKB1_peaks.narrowPeak | sort -k9 -g -r | head -5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "31282 macs2_NFKB1/NFKB1_peaks.narrowPeak\r\n"
     ]
    }
   ],
   "source": [
    "wc -l macs2_NFKB1/NFKB1_peaks.narrowPeak"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There are 31282 peaks called in the entire genome"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5.5 i-cisTarget\n",
    "\n",
    "We want to check if NFKB1 is really binding to the regions in the genome where the peaks are situated, as a quality control to verify if the chipseq worked. This can be done with i-cisTarget, a tool that analyzes the peaks for enriched motifs. We could also use Rsat (Regulatory Sequence Analysis Tools). Here we prefer i-cisTarget because it tests for known motifs in the database, while rsat does the novo motif discovery via statistical analysis which can generate a lot of false positives.\n",
    "\n",
    "We see that the first motifs of the list are NFKB2 and the third one is NFKB1. We can indeed conclude that NFKB1 is binding to the regions in the genome where the peaks are located, so that the chipseq did indeed work. However, NFKB2 also binds to the regions of the peaks so the binding is not that specific. In conclusion, the expected motif (that of NFKB1) is enriched in the ChIP-seq peaks.\n",
    "\n",
    "https://gbiomed.kuleuven.be/apps/lcb/i-cisTarget/reports/d43b8fc8a638b7eb1dfe39b2d81bfde13acfbb64/report.html\n",
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img width=\"50%\" src=\"/user/r0368280/files/Examenopdracht/Pictures/Printscreen2.png\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5.6 IGV and GREAT tool\n",
    "Now we upload the peak file into IGV and look where the peaks are located. Ideally, the genes that were upregulated in RA would now show a peak because our hypothesis was that NKFB1 regulates these genes. \n",
    "\n",
    "For example, if we look at GADD45B, a gene that was upregulated in RA, we see that there are two peaks located in this gene."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img width=\"50%\" src=\"/user/r0368280/files/Examenopdracht/Pictures/Printscreen3.png\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Subsequently we analyze in which genes the 5 highest peaks are located. However these 5 highest peaks aren't located within a gene. With the GREAT Tool you can assign peaks to genes. Association rule: Basal+extension: 5000 bp upstream, 1000 bp downstream, 1000000 bp max extension, curated regulatory domains included. \n",
    "\n",
    "From GREAT we observe that the five highest peaks are associated with the next genes: CAV3, SSUH2, SRSF6, ZC3H12C. These 4 genes are however not known to be associated with immunological processes. Nevertheless, there are still another 31277 peaks (31282-5) called. GREAT generates a list of genes which are assigned to these 31282 peaks but also the associated biological processes. We see that the genes under the peaks are involved in interfon-gamma-mediated signaling pathway and T cell activation involed in immune respone."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img width=\"50%\" src=\"/user/r0368280/files/Examenopdracht/Pictures/Printscreen4.png\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5.7 Integration of expression and Chipseq data\n",
    "\n",
    "Subsequently we upload the list of genes which GREAT linked to peaks as a cluster in MeV and plot expression values to integrate the expression and chipseq data. Now we see the expression values obtained by microarray of the genes that GREAT linked to peaks. \n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img width=\"50%\" src=\"/user/r0368280/files/Examenopdracht/Pictures/Printscreen5.png\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In the picture above we see that IFNG is upregulated in RA. We also see that IL18R1, IL1R2, HLA-DQA1, GADD45B are upregulated (not shown). Now you can analyze this expression data and investigate how many and which genes that were assigned to a NFKB1 peak are upregulated in rheumatoid arthritis. Or more interesting: how many genes that are upregulated in RA are also bound by NKFB1?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 6. Conclusion\n",
    "\n",
    "Microarray analysis provided a list of genes upregulated in rheumatoid arthritis and Chipseq analysis of the proposed regulator of the upregulated genes in rheumatoid arthrits i.e. NFKB1, provided a list of genes that NFKB1 directly binds. Now to investigate if NFKB1 directly binds the upregulated genes in rheumatoid arthritis, we integrated the expression data and chipseq data.\n",
    "\n",
    "The next step would be to look at each gene that is upregulated in RA and check if this gene is bound by NKFB1. We did this for a couple of genes (IFNG, IL18R1, IL1R2,...). However, to conclude that NKFB1 is a possible regulator of the upregulated genes in RA, we should investigate all genes, more specially we should investigate how many and which of the upregulated genes are bound by NFKB1.\n",
    "\n",
    "To conclude, NKFB1 could be a regulator of the upregulated genes in RA but further analysis and research is necessary to conclude this."
   ]
  }
 ],
 "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
}
