{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Chip-seq analysis\n",
    "## introduction\n",
    "As our initial paper did not contain any additional data for ChIP-Seq, we attempted to find a transcription factor (TF)\n",
    "that was enriched in our initial paper. We found one named myeloid cell nuclear differentiation antigen (MNDA), however\n",
    "there were no relevant ChIP-Seq data for this TF. Instead we then looked into finding ChIP-Seq data that is associated with myeloid leukemia. In this case we found the following paper that is referenced in the introduction of our main notebook. In this paper they look at GATA expression in Acute Myeloid Leukemia Cells."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mkdir: cannot create directory ‘/home/luna.kuleuven.be/r0425222/data/ncbi2’: File exists\r\n"
     ]
    }
   ],
   "source": [
    "# In order to download the data from NCBI we need to generate a NCBI directory, and use an SRA toolkit.\n",
    "\n",
    "mkdir ~/data/ncbi2\n",
    "vdb-config -s /repository/user/main/public/root=~/data/ncbi2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "# Here we are creating our own seperate directory and redirecting to this directory for the downloads.\n",
    "\n",
    "mkdir ~/data/work2\n",
    "cd ~/data/work2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Read 25504469 spots for SRR772110\r\n",
      "Written 25504469 spots for SRR772110\r\n"
     ]
    }
   ],
   "source": [
    "# Using the SRR identifier that we located on the SRA page of our samples,we download the data using the SRA toolkit.\n",
    "\n",
    "fastq-dump --split-files SRR772110"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Read 24815589 spots for SRR772113\r\n",
      "Written 24815589 spots for SRR772113\r\n"
     ]
    }
   ],
   "source": [
    "fastq-dump --split-files SRR772113"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false,
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mv: cannot stat ‘SRR772110_1.fastq’: No such file or directory\r\n",
      "mv: cannot stat ‘SRR772113_1.fastq’: No such file or directory\r\n"
     ]
    }
   ],
   "source": [
    "# Now we rename the fastq files generated to a more indicative name. \n",
    "\n",
    "mv SRR772110_1.fastq CHIP_GATA_1.fastq\n",
    "mv SRR772113_1.fastq CHIP_IgG.fastq"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "~  CHIP_GATA_1.fastq  CHIP_IgG.fastq  CHIP_IgG.sam\r\n"
     ]
    }
   ],
   "source": [
    "# Next we check the files have been renamed correctly. \n",
    "\n",
    "cd ~/data/work2\n",
    "ls"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "25504469 reads; of these:\r\n",
      "  25504469 (100.00%) were unpaired; of these:\r\n",
      "    640748 (2.51%) aligned 0 times\r\n",
      "    19029921 (74.61%) aligned exactly 1 time\r\n",
      "    5833800 (22.87%) aligned >1 times\r\n",
      "97.49% overall alignment rate\r\n"
     ]
    }
   ],
   "source": [
    "# In the following commands we perform aligning the sequence reads to a reference genome. \n",
    "# Our reference sequence is an IgG control instead of an input control as this is what was used in the paper.\n",
    "\n",
    "bowtie2-align -x /mnt/nfs/data/resources/bowtie2/hg19 CHIP_GATA_1.fastq -S CHIP_GATA2_1.sam"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Warning: Could not open read file \"CHIP_GATA2_1.fastq\" for reading; skipping...\r\n",
      "Error: No input read files were valid\r\n",
      "24815589 reads; of these:\r\n",
      "  24815589 (100.00%) were unpaired; of these:\r\n",
      "    758107 (3.05%) aligned 0 times\r\n",
      "    18766012 (75.62%) aligned exactly 1 time\r\n",
      "    5291470 (21.32%) aligned >1 times\r\n",
      "96.95% overall alignment rate\r\n"
     ]
    }
   ],
   "source": [
    "bowtie2-align -x /mnt/nfs/data/resources/bowtie2/hg19 CHIP_IgG.fastq -S CHIP_IgG.sam"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "# The generated files from bowtie2 require to be saved as SAM format which is the most convenient for further analyses.\n",
    "# For our continued analyses, we convert the SAM file format to BAM files using samtools.\n",
    "\n",
    "samtools view -h -S -b CHIP_GATA2_1.sam > CHIP_GATA2_1.bam\n",
    "samtools view -h -S -b CHIP_IgG.sam > CHIP_IgG.bam"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[bam_sort_core] merging from 11 files...\r\n",
      "[bam_sort_core] merging from 11 files...\r\n"
     ]
    }
   ],
   "source": [
    "# Next we sort our alignments and specify the file output format as a bam file with the -O.\n",
    "\n",
    "samtools sort -O bam -o CHIP_GATA2_1_sorted.bam CHIP_GATA2_1.bam\n",
    "samtools sort -O bam -o CHIP_IgG_sorted.bam CHIP_IgG.bam"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "# Here we index our sorted bam files using samtools. \n",
    "\n",
    "samtools index CHIP_GATA2_1_sorted.bam\n",
    "samtools index CHIP_IgG_sorted.bam"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "# Next we create a new working directory that we will use for Model-based Analysis of ChIP-Seq (MACS).\n",
    "# We use this to identify transcription factor binding sites and later check the peaks using igv.\n",
    "\n",
    "mkdir macs2_GATA2 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO  @ Sat, 28 Jan 2017 22:46:04: \r\n",
      "# Command line: callpeak -t CHIP_GATA2_1_sorted.bam -c CHIP_IgG_sorted.bam --outdir macs2_GATA2 -n GATA2 -g hs -q 0.005\r\n",
      "# ARGUMENTS LIST:\r\n",
      "# name = GATA2\r\n",
      "# format = AUTO\r\n",
      "# ChIP-seq file = ['CHIP_GATA2_1_sorted.bam']\r\n",
      "# control file = ['CHIP_IgG_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-03\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  @ Sat, 28 Jan 2017 22:46:04: #1 read tag files... \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:46:04: #1 read treatment tags... \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:46:04: Detected format is: BAM \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:46:04: * Input file is gzipped. \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:46:08:  1000000 \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:46:13:  2000000 \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:46:17:  3000000 \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:46:22:  4000000 \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:46:26:  5000000 \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:46:31:  6000000 \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:46:35:  7000000 \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:46:40:  8000000 \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:46:44:  9000000 \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:46:48:  10000000 \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:46:53:  11000000 \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:46:57:  12000000 \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:47:02:  13000000 \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:47:06:  14000000 \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:47:11:  15000000 \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:47:16:  16000000 \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:47:20:  17000000 \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:47:25:  18000000 \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:47:29:  19000000 \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:47:34:  20000000 \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:47:38:  21000000 \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:47:43:  22000000 \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:47:47:  23000000 \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:47:52:  24000000 \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:47:56:  25000000 \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:47:58: #1.2 read input tags... \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:47:58: Detected format is: BAM \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:47:58: * Input file is gzipped. \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:48:03:  1000000 \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:48:07:  2000000 \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:48:12:  3000000 \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:48:17:  4000000 \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:48:22:  5000000 \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:48:27:  6000000 \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:48:31:  7000000 \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:48:36:  8000000 \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:48:41:  9000000 \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:48:46:  10000000 \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:48:51:  11000000 \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:48:55:  12000000 \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:49:00:  13000000 \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:49:05:  14000000 \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:49:10:  15000000 \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:49:15:  16000000 \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:49:19:  17000000 \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:49:24:  18000000 \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:49:29:  19000000 \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:49:34:  20000000 \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:49:39:  21000000 \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:49:44:  22000000 \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:49:48:  23000000 \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:49:53:  24000000 \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:49:56: #1 tag size is determined as 49 bps \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:49:56: #1 tag size = 49 \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:49:56: #1  total tags in treatment: 24863721 \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:49:56: #1 user defined the maximum tags... \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:49:56: #1 filter out redundant tags at the same location and the same strand by allowing at most 1 tag(s) \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:49:56: #1  tags after filtering in treatment: 8988903 \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:49:56: #1  Redundant rate of treatment: 0.64 \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:49:56: #1  total tags in control: 24057482 \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:49:56: #1 user defined the maximum tags... \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:49:56: #1 filter out redundant tags at the same location and the same strand by allowing at most 1 tag(s) \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:49:57: #1  tags after filtering in control: 19852820 \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:49:57: #1  Redundant rate of control: 0.17 \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:49:57: #1 finished! \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:49:57: #2 Build Peak Model... \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:49:57: #2 looking for paired plus/minus strand peaks... \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:50:00: #2 number of paired peaks: 21315 \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:50:00: start model_add_line... \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:50:00: start X-correlation... \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:50:00: end of X-cor \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:50:00: #2 finished! \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:50:00: #2 predicted fragment length is 156 bps \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:50:00: #2 alternative fragment length(s) may be 156 bps \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:50:00: #2.2 Generate R script for model : macs2_GATA2/GATA2_model.r \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:50:00: #3 Call peaks... \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:50:00: #3 Pre-compute pvalue-qvalue table... \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:51:46: #3 Call peaks for each chromosome... \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:52:35: #4 Write output xls file... macs2_GATA2/GATA2_peaks.xls \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:52:36: #4 Write peak in narrowPeak format file... macs2_GATA2/GATA2_peaks.narrowPeak \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:52:36: #4 Write summits bed file... macs2_GATA2/GATA2_summits.bed \r\n",
      "INFO  @ Sat, 28 Jan 2017 22:52:36: Done! \r\n"
     ]
    }
   ],
   "source": [
    "# We use our sorted GATA and IgG control bam files for peak-calling from alignment results. The -t indicates the file\n",
    "# that we want to be compared to the -c which is our control file. We name the output file as macs2_GATA2 and the string name as \n",
    "# GATA2. We use -g hs (2.7E9) size for the mappable human genome, and a q-value cut-off of 0.005.\n",
    "\n",
    "macs2 callpeak -t CHIP_GATA2_1_sorted.bam -c CHIP_IgG_sorted.bam --outdir macs2_GATA2 -n GATA2 -g hs -q 0.005\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "chr1\t9937\t10415\tGATA2_peak_1\t54\t.\t4.13794\t8.57740\t5.41466\t153\r\n",
      "chr1\t369462\t369702\tGATA2_peak_2\t97\t.\t7.87173\t13.13085\t9.74953\t108\r\n",
      "chr1\t564386\t566899\tGATA2_peak_3\t1140\t.\t5.85065\t119.30746\t114.09961\t1816\r\n",
      "chr1\t567118\t570377\tGATA2_peak_4\t1316\t.\t7.73811\t137.24675\t131.69553\t278\r\n",
      "chr1\t619982\t620234\tGATA2_peak_5\t152\t.\t10.76500\t18.82044\t15.23603\t135\r\n",
      "chr1\t1079546\t1079702\tGATA2_peak_6\t76\t.\t7.23990\t10.93742\t7.63410\t119\r\n",
      "chr1\t1118178\t1118347\tGATA2_peak_7\t116\t.\t9.21441\t15.13415\t11.66072\t101\r\n",
      "chr1\t1167266\t1167549\tGATA2_peak_8\t423\t.\t21.71969\t46.54713\t42.33672\t136\r\n",
      "chr1\t1565675\t1565882\tGATA2_peak_9\t102\t.\t8.55624\t13.70238\t10.28372\t100\r\n",
      "chr1\t1677837\t1677993\tGATA2_peak_10\t76\t.\t7.23990\t10.93742\t7.63410\t52\r\n"
     ]
    }
   ],
   "source": [
    "# Next we head the first 10 results.\n",
    "\n",
    "head macs2_GATA2/GATA2_peaks.narrowPeak"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "11990\r\n"
     ]
    }
   ],
   "source": [
    "# Here we check the count number of the peaks. \n",
    "\n",
    "cat macs2_GATA2/GATA2_peaks.narrowPeak |wc -l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mkdir: cannot create directory ‘/mnt/storage/r0425222/work2/fuzznuc’: File exists\r\n",
      "chr20.fa  GATA_chr20_1_fuzznuck.gff  GATA_chr20_2_fuzznuck.gff\r\n"
     ]
    }
   ],
   "source": [
    "mkdir /mnt/storage/r0425222/work2/fuzznuc\n",
    "cd /mnt/storage/r0425222/work2/fuzznuc\n",
    "ls\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# GATA2 binding sequence \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "ln -sf /mnt/nfs/data/chr20.fa"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Search for patterns in nucleotide sequences\r\n"
     ]
    }
   ],
   "source": [
    "fuzznuc -sequence chr20.fa -pattern AGATCTTA -rformat gff -outfile GATA_chr20_fuzznuck.gff"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "854 GATA_chr20_fuzznuck.gff\r\n"
     ]
    }
   ],
   "source": [
    "wc -l GATA_chr20_fuzznuck.gff\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "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
}
