{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Assignment Bioinformatics:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# The role of Sox2 in gastric epithelium and the consequences of its loss\n",
    "\n",
    "    Maarten Eggerickx (r0380465), Martijn Broux (r0372900), Wietse Decraene (r0368164)\n",
    "    29 January 2017"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Introduction"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Sox 2 is a gene that is expressed highly in developping organisms and stem cells. In the case of stem cells, it is known to be important for sustaining the stem cell populations because it activates renewal genes and supresses differentiation. It would seem therefore, that activation of Sox2 would be a leading cause of cancer. But the role of Sox2 is rather controversial, with some reports of Sox2 being tumor-inducing while others reporting it to be tumor-suppressing. A known fact, is that the Sox 2 pathway is dysregulated in many cancers.\n",
    "\n",
    "Sox2 is higly expressed in gastric cancers, probably because the gastric mucosa contains one of the few stem cell populations that uses Sox2 inherently for their renewal. Gastic cancers cause the 3rd most deaths of any cancers (after lung and intestinal). This makes the gastic mucosa an interesting tissue to study the role of Sox2. Because of both its indigenous expression and its importance for cancer research.\n",
    "\n",
    "We studied an RNA sequencing data-set of Sox 2 WT vs Sox 2 KO mice. The KO-group was knocked out through Tamoxifen-induced Cre-recombination. Attached to this dataset, was a ChIPseq dataset of Sox2 immunopreciptation of both the WT and the KO. The ChIPseq WT data is useful for identifying all the binding sites that Sox2 has in the genome of gastric tissue. The knock-out data is useful for assessing the quality of our KO (because Cre-induced KO is never 100%).\n",
    "\n",
    "    GEO accession number: GSE83966\n",
    "\n",
    "    pathway notebook: /mnt/storage/r0372900/jupyternotebooks/Assignment_Bioinformatics2/Bash-Notebook-Assignment\n",
    "    \n",
    "    pathway attachment: /mnt/storage/r0372900/jupyternotebooks/Assignment_Bioinformatics2/R-Notebook-Attachment"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Part 1: Differential Expression: RNA sequencing"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For the RNAseq, we used the following subseries: GSE83964\tSox2 suppresses gastric tumorigenesis in mice [RNA-seq]\n",
    "\n",
    "Organism: Mus musculus\n",
    "\n",
    "Platform: Illumina GPL17021\n",
    "\n",
    "    GSM2224570\tSox2 WT stomach 1 [RNA-seq]\n",
    "    GSM2224571\tSox2 WT stomach 2 [RNA-seq]\n",
    "    GSM2224572\tSox2 WT stomach 3 [RNA-seq]\n",
    "    GSM2224573\tSox2 KO stomach 1 [RNA-seq]\n",
    "    GSM2224574\tSox2 KO stomach 2 [RNA-seq]\n",
    "    GSM2224575\tSox2 KO stomach 3 [RNA-seq]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/mnt/storage/r0372900/jupyternotebooks/Assignment_Bioinformatics2\r\n"
     ]
    }
   ],
   "source": [
    "pwd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Step 1.1: making the directory"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/mnt/storage/r0372900/Assignment_bioinformatics2/RNAseq\r\n"
     ]
    }
   ],
   "source": [
    "# mkdir -p /mnt/storage/r0372900/Assignment_bioinformatics2/RNAseq\n",
    "cd /mnt/storage/r0372900/Assignment_bioinformatics2/RNAseq\n",
    "pwd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Step 1.2: downloading the fastq files"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "(Be sure to select the correct output folder. To not achieve limit.)\n",
    "(It is possible an extra folder appears in your home directory called NCBI and that the sra files are in there. \n",
    "This should not happen and these files should be removed.)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\r\n",
      "Usage:\r\n",
      "  fastq-dump [options] <path> [<path>...]\r\n",
      "  fastq-dump [options] <accession>\r\n",
      "\r\n",
      "INPUT\r\n",
      "  -A|--accession <accession>       Replaces accession derived from <path> in \r\n",
      "                                   filename(s) and deflines (only for single \r\n",
      "                                   table dump) \r\n",
      "  --table <table-name>             Table name within cSRA object, default is \r\n",
      "                                   \"SEQUENCE\" \r\n",
      "\r\n",
      "PROCESSING\r\n",
      "\r\n",
      "Read Splitting                     Sequence data may be used in raw form or\r\n",
      "                                     split into individual reads\r\n",
      "  --split-spot                     Split spots into individual reads \r\n",
      "\r\n",
      "Full Spot Filters                  Applied to the full spot independently\r\n",
      "                                     of --split-spot\r\n",
      "  -N|--minSpotId <rowid>           Minimum spot id \r\n",
      "  -X|--maxSpotId <rowid>           Maximum spot id \r\n",
      "  --spot-groups <[list]>           Filter by SPOT_GROUP (member): name[,...] \r\n",
      "  -W|--clip                        Apply left and right clips \r\n",
      "\r\n",
      "Common Filters                     Applied to spots when --split-spot is not\r\n",
      "                                     set, otherwise - to individual reads\r\n",
      "  -M|--minReadLen <len>            Filter by sequence length >= <len> \r\n",
      "  -R|--read-filter <[filter]>      Split into files by READ_FILTER value \r\n",
      "                                   optionally filter by value: \r\n",
      "                                   pass|reject|criteria|redacted \r\n",
      "  -E|--qual-filter                 Filter used in early 1000 Genomes data: no \r\n",
      "                                   sequences starting or ending with >= 10N \r\n",
      "  --qual-filter-1                  Filter used in current 1000 Genomes data \r\n",
      "\r\n",
      "Filters based on alignments        Filters are active when alignment\r\n",
      "                                     data are present\r\n",
      "  --aligned                        Dump only aligned sequences \r\n",
      "  --unaligned                      Dump only unaligned sequences \r\n",
      "  --aligned-region <name[:from-to]>  Filter by position on genome. Name can \r\n",
      "                                   either be accession.version (ex: \r\n",
      "                                   NC_000001.10) or file specific name (ex: \r\n",
      "                                   \"chr1\" or \"1\"). \"from\" and \"to\" are 1-based \r\n",
      "                                   coordinates \r\n",
      "  --matepair-distance <from-to|unknown>  Filter by distance beiween matepairs. \r\n",
      "                                   Use \"unknown\" to find matepairs split \r\n",
      "                                   between the references. Use from-to to limit \r\n",
      "                                   matepair distance on the same reference \r\n",
      "\r\n",
      "Filters for individual reads       Applied only with --split-spot set\r\n",
      "  --skip-technical                 Dump only biological reads \r\n",
      "\r\n",
      "OUTPUT\r\n",
      "  -O|--outdir <path>               Output directory, default is working \r\n",
      "                                   directory '.' ) \r\n",
      "  -Z|--stdout                      Output to stdout, all split data become \r\n",
      "                                   joined into single stream \r\n",
      "  --gzip                           Compress output using gzip \r\n",
      "  --bzip2                          Compress output using bzip2 \r\n",
      "\r\n",
      "Multiple File Options              Setting these options will produce more\r\n",
      "                                     than 1 file, each of which will be suffixed\r\n",
      "                                     according to splitting criteria.\r\n",
      "  --split-files                    Dump each read into separate file.Files \r\n",
      "                                   will receive suffix corresponding to read \r\n",
      "                                   number \r\n",
      "  --split-3                        Legacy 3-file splitting for mate-pairs: \r\n",
      "                                   First biological reads satisfying dumping \r\n",
      "                                   conditions are placed in files *_1.fastq and \r\n",
      "                                   *_2.fastq If only one biological read is \r\n",
      "                                   present it is placed in *.fastq Biological \r\n",
      "                                   reads and above are ignored. \r\n",
      "  -G|--spot-group                  Split into files by SPOT_GROUP (member name) \r\n",
      "  -R|--read-filter <[filter]>      Split into files by READ_FILTER value \r\n",
      "                                   optionally filter by value: \r\n",
      "                                   pass|reject|criteria|redacted \r\n",
      "  -T|--group-in-dirs               Split into subdirectories instead of files \r\n",
      "  -K|--keep-empty-files            Do not delete empty files \r\n",
      "\r\n",
      "FORMATTING\r\n",
      "\r\n",
      "Sequence\r\n",
      "  -C|--dumpcs <[cskey]>            Formats sequence using color space (default \r\n",
      "                                   for SOLiD),\"cskey\" may be specified for \r\n",
      "                                   translation \r\n",
      "  -B|--dumpbase                    Formats sequence using base space (default \r\n",
      "                                   for other than SOLiD). \r\n",
      "\r\n",
      "Quality\r\n",
      "  -Q|--offset <integer>            Offset to use for quality conversion, \r\n",
      "                                   default is 33 \r\n",
      "  --fasta <[line width]>           FASTA only, no qualities, optional line \r\n",
      "                                   wrap width (set to zero for no wrapping) \r\n",
      "  --suppress-qual-for-cskey        supress quality-value for cskey \r\n",
      "\r\n",
      "Defline\r\n",
      "  -F|--origfmt                     Defline contains only original sequence name \r\n",
      "  -I|--readids                     Append read id after spot id as \r\n",
      "                                   'accession.spot.readid' on defline \r\n",
      "  --helicos                        Helicos style defline \r\n",
      "  --defline-seq <fmt>              Defline format specification for sequence. \r\n",
      "  --defline-qual <fmt>             Defline format specification for quality. \r\n",
      "                                   <fmt> is string of characters and/or \r\n",
      "                                   variables. The variables can be one of: $ac \r\n",
      "                                   - accession, $si spot id, $sn spot \r\n",
      "                                   name, $sg spot group (barcode), $sl spot \r\n",
      "                                   length in bases, $ri read number, $rn \r\n",
      "                                   read name, $rl read length in bases. '[]' \r\n",
      "                                   could be used for an optional output: if \r\n",
      "                                   all vars in [] yield empty values whole \r\n",
      "                                   group is not printed. Empty value is empty \r\n",
      "                                   string or for numeric variables. Ex: \r\n",
      "                                   @$sn[_$rn]/$ri '_$rn' is omitted if name \r\n",
      "                                   is empty\r\n",
      " \r\n",
      "OTHER:\r\n",
      "  --disable-multithreading         disable multithreading \r\n",
      "  -h|--help                        Output brief explanation of program usage \r\n",
      "  -V|--version                     Display the version of the program \r\n",
      "  -L|--log-level <level>           Logging level as number or enum string One \r\n",
      "                                   of (fatal|sys|int|err|warn|info) or (0-5) \r\n",
      "                                   Current/default is warn \r\n",
      "  -v|--verbose                     Increase the verbosity level of the program \r\n",
      "                                   Use multiple times for more verbosity \r\n",
      "  --ncbi_error_report              Control program execution environment \r\n",
      "                                   report generation (if implemented). One of \r\n",
      "                                   (never|error|always). Default is error \r\n",
      "  --legacy-report                  use legacy style 'Written spots' for tool \r\n",
      "\r\n",
      "fastq-dump : 2.8.0\r\n",
      "\r\n"
     ]
    }
   ],
   "source": [
    "# fastq-dump -h"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "We downloaded the files manually and put them in the desired folder. Then, we executed the fastq-dump command to convert the .sra files to .fastq files."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#first, download sra-files and then upload it via bitvise.\n",
    "fastq-dump ./SRR3737439.sra .\n",
    "fastq-dump ./SRR3737440.sra .\n",
    "fastq-dump ./SRR3737441.sra .\n",
    "fastq-dump ./SRR3737442.sra .\n",
    "fastq-dump ./SRR3737443.sra .\n",
    "fastq-dump ./SRR3737444.sra ."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/mnt/storage/r0372900/Assignment_bioinformatics2/RNAseq\r\n",
      "SRR3737439.fastq  SRR3737441.fastq  SRR3737443.fastq\tSRR3737444.sra\r\n",
      "SRR3737439.sra\t  SRR3737441.sra    SRR3737443.sra\r\n",
      "SRR3737440.fastq  SRR3737442.fastq  SRR3737444_2.fastq\r\n",
      "SRR3737440.sra\t  SRR3737442.sra    SRR3737444.fastq\r\n"
     ]
    }
   ],
   "source": [
    "cd /mnt/storage/r0372900/Assignment_bioinformatics2/RNAseq\n",
    "pwd\n",
    "ls"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "@SRR3737439.1 HWI-D00449:143:HWLFCADXX:1:1101:1313:1987 length=51\r\n",
      "NCCTCCCACTTATTCTACACCTCTCATGTCTCTTCACAGTGCCAGACTAGA\r\n",
      "+SRR3737439.1 HWI-D00449:143:HWLFCADXX:1:1101:1313:1987 length=51\r\n",
      "#1:DDDDFFFFFFGHGIIIIIHGEBHIIGHEIIIIIIIGDGCHIADCEHII\r\n",
      "@SRR3737439.2 HWI-D00449:143:HWLFCADXX:1:1101:1635:1951 length=51\r\n",
      "NACTGATGCCGAAAGCTTCCGCGATGGTGAGCTGGACCTTCTGCCGAATGT\r\n",
      "+SRR3737439.2 HWI-D00449:143:HWLFCADXX:1:1101:1635:1951 length=51\r\n",
      "#1=DFFFFHHHHHJJJJJJJJJJJJJJGHIJJJJJJJJJJJJJJJJJJHHH\r\n",
      "@SRR3737439.3 HWI-D00449:143:HWLFCADXX:1:1101:1514:1952 length=51\r\n",
      "NACCAGAAGCAGTTCAACCCTTGTAGTAACAAGAGCAGCAGGTTAAAAACC\r\n"
     ]
    }
   ],
   "source": [
    "head SRR3737439.fastq"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now that the sequences are downloaded, we can test their quality using fastqc with the following command:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "fastqc SRR*.fastq -o . "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We show the results of the fastqc of the first WT dataset (SRR3737439) as an example for the whole set: (see figure below)\n",
    "\n",
    "The per base sequence content, sequence quality score, QC content and sequence length are excellent.\n",
    "\n",
    "The per base sequence content, per base GC content and Kmer content have a slight deviation at the start of all the reads. This can be attributed in part to the genetic adapter used for the RNA sequencing. It is of no real concern for the interpretation of our data.\n",
    "\n",
    "there were a lot of sequence duplications above 10. This could point to PCR artifacts. It was not entirely clear though, whether this was the case, as we think sequence duplications of 10 are a rather low number. We desided to use the data as 'as provided' and not exclude sequences that were duplicated 10 times or more.  \n",
    "\n",
    "<img src=\"/user/r0372900/files/Assignment_Bioinformatics2/fastqc-SRR3737439.jpg\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Step 1.3: mapping to the genome"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "-We used STAR to map to our genome\n",
    "\n",
    "-mouse reference genome (mm10/GRCm38), positioned at:\n",
    "\n",
    "    /mnt/nfs/data/reference_genomes/mm10/STAR"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "STAR --genomeDir /mnt/nfs/data/reference_genomes/mm10/STAR \\\n",
    "     --runThreadN 4 \\\n",
    "     --readFilesIn SRR3737439.fastq \\\n",
    "     --outFileNamePrefix STAR.SRR3737439.\n",
    "\n",
    "STAR --genomeDir /mnt/nfs/data/reference_genomes/mm10/STAR \\\n",
    "     --runThreadN 4 \\\n",
    "     --readFilesIn SRR3737440.fastq \\\n",
    "     --outFileNamePrefix STAR.SRR3737440.\n",
    "\t \n",
    "STAR --genomeDir /mnt/nfs/data/reference_genomes/mm10/STAR \\\n",
    "     --runThreadN 4 \\\n",
    "     --readFilesIn SRR3737441.fastq \\\n",
    "     --outFileNamePrefix STAR.SRR3737441.\n",
    "\t \n",
    "STAR --genomeDir /mnt/nfs/data/reference_genomes/mm10/STAR \\\n",
    "     --runThreadN 4 \\\n",
    "     --readFilesIn SRR3737442.fastq \\\n",
    "     --outFileNamePrefix STAR.SRR3737442.\n",
    "\t \n",
    "STAR --genomeDir /mnt/nfs/data/reference_genomes/mm10/STAR \\\n",
    "     --runThreadN 4 \\\n",
    "     --readFilesIn SRR3737443.fastq \\\n",
    "     --outFileNamePrefix STAR.SRR3737443.\n",
    "\t \n",
    "STAR --genomeDir /mnt/nfs/data/reference_genomes/mm10/STAR \\\n",
    "     --runThreadN 4 \\\n",
    "     --readFilesIn SRR3737444.fastq \\\n",
    "     --outFileNamePrefix STAR.SRR3737444.\n",
    "\t \n",
    "ls"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#Check if header is present and looks the way it should look\n",
    "head -30 STAR.SRR3737439.Aligned.out.sam | grep '^@'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#Check if the rest of the file looks the way it should look (never use cat for such big files)\n",
    "head -80 STAR.SRR3737439.Aligned.out.sam | grep -v '^@'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Sam files are too big to work with. So we will compress them to bam files.\n",
    "These are easier to work with"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Firstly: compressing to bam\n",
    "samtools sort -o SRR3737439.bam STAR.SRR3737439.Aligned.out.sam\n",
    "samtools sort -o SRR3737440.bam STAR.SRR3737440.Aligned.out.sam\n",
    "samtools sort -o SRR3737441.bam STAR.SRR3737441.Aligned.out.sam\n",
    "samtools sort -o SRR3737442.bam STAR.SRR3737442.Aligned.out.sam\n",
    "samtools sort -o SRR3737443.bam STAR.SRR3737443.Aligned.out.sam\n",
    "samtools sort -o SRR3737444.bam STAR.SRR3737444.Aligned.out.sam\n",
    "ls -l *[bs]am"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Secondly: we create an index for the bam files so that we can visualise them in IGV.\n",
    "samtools index SRR3737439.bam\n",
    "samtools index SRR3737440.bam\n",
    "samtools index SRR3737441.bam\n",
    "samtools index SRR3737442.bam\n",
    "samtools index SRR3737443.bam\n",
    "samtools index SRR3737444.bam\n",
    "ls -l *[bs]a[mi]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# check the size of our directory. We delete the fastq and the sam files because they are too big.\n",
    "ls -lh /mnt/storage/r0372900/Assignment_bioinformatics2/RNAseq"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# We can take a look at the mapping\n",
    "\n",
    "samtools idxstats SRR3737439.bam\n",
    "samtools flagstat SRR3737439.bam #remember total amount of reads and compare to what got mapped () with certainty and with less certainty ()\n",
    "\n",
    "samtools idxstats SRR3737440.bam\n",
    "samtools flagstat SRR3737440.bam\n",
    "\n",
    "samtools idxstats SRR3737441.bam\n",
    "samtools flagstat SRR3737441.bam\n",
    "\n",
    "samtools idxstats SRR3737442.bam\n",
    "samtools flagstat SRR3737442.bam\n",
    "\n",
    "samtools idxstats SRR3737443.bam\n",
    "samtools flagstat SRR3737443.bam\n",
    "\n",
    "samtools idxstats SRR3737444.bam\n",
    "samtools flagstat SRR3737444.bam"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "chr1\t195471971\t3031530\t0\r\n",
      "chr2\t182113224\t2509773\t0\r\n",
      "chr3\t160039680\t2257331\t0\r\n",
      "chr4\t156508116\t1880546\t0\r\n",
      "chr5\t151834684\t1912161\t0\r\n",
      "chr6\t149736546\t2719948\t0\r\n",
      "chr7\t145441459\t2535882\t0\r\n",
      "chr8\t129401213\t1648420\t0\r\n",
      "chr9\t124595110\t2302198\t0\r\n",
      "chr10\t130694993\t1441217\t0\r\n",
      "chr11\t122082543\t2719082\t0\r\n",
      "chr12\t120129022\t991728\t0\r\n",
      "chr13\t120421639\t1233925\t0\r\n",
      "chr14\t124902244\t1092196\t0\r\n",
      "chr15\t104043685\t1470097\t0\r\n",
      "chr16\t98207768\t987607\t0\r\n",
      "chr17\t94987271\t2735196\t0\r\n",
      "chr18\t90702639\t819678\t0\r\n",
      "chr19\t61431566\t1171338\t0\r\n",
      "chrX\t171031299\t1027703\t0\r\n",
      "chrY\t91744698\t34172\t0\r\n",
      "chrM\t16299\t1915281\t0\r\n",
      "chr1_GL456210_random\t169725\t328\t0\r\n",
      "chr1_GL456211_random\t241735\t225\t0\r\n",
      "chr1_GL456212_random\t153618\t363\t0\r\n",
      "chr1_GL456213_random\t39340\t78\t0\r\n",
      "chr1_GL456221_random\t206961\t352\t0\r\n",
      "chr4_GL456216_random\t66673\t683\t0\r\n",
      "chr4_GL456350_random\t227966\t177\t0\r\n",
      "chr4_JH584292_random\t14945\t0\t0\r\n",
      "chr4_JH584293_random\t207968\t587\t0\r\n",
      "chr4_JH584294_random\t191905\t598\t0\r\n",
      "chr4_JH584295_random\t1976\t272\t0\r\n",
      "chr5_GL456354_random\t195993\t13\t0\r\n",
      "chr5_JH584296_random\t199368\t131\t0\r\n",
      "chr5_JH584297_random\t205776\t128\t0\r\n",
      "chr5_JH584298_random\t184189\t11\t0\r\n",
      "chr5_JH584299_random\t953012\t41\t0\r\n",
      "chr7_GL456219_random\t175968\t8\t0\r\n",
      "chrX_GL456233_random\t336933\t2030\t0\r\n",
      "chrY_JH584300_random\t182347\t3\t0\r\n",
      "chrY_JH584301_random\t259875\t0\t0\r\n",
      "chrY_JH584302_random\t155838\t1\t0\r\n",
      "chrY_JH584303_random\t158099\t1\t0\r\n",
      "chrUn_GL456239\t40056\t1\t0\r\n",
      "chrUn_GL456359\t22974\t0\t0\r\n",
      "chrUn_GL456360\t31704\t3\t0\r\n",
      "chrUn_GL456366\t47073\t79\t0\r\n",
      "chrUn_GL456367\t42057\t6\t0\r\n",
      "chrUn_GL456368\t20208\t10\t0\r\n",
      "chrUn_GL456370\t26764\t27\t0\r\n",
      "chrUn_GL456372\t28664\t28\t0\r\n",
      "chrUn_GL456378\t31602\t578\t0\r\n",
      "chrUn_GL456379\t72385\t847\t0\r\n",
      "chrUn_GL456381\t25871\t0\t0\r\n",
      "chrUn_GL456382\t23158\t9585\t0\r\n",
      "chrUn_GL456383\t38659\t5\t0\r\n",
      "chrUn_GL456385\t35240\t4\t0\r\n",
      "chrUn_GL456387\t24685\t0\t0\r\n",
      "chrUn_GL456389\t28772\t3\t0\r\n",
      "chrUn_GL456390\t24668\t23\t0\r\n",
      "chrUn_GL456392\t23629\t20\t0\r\n",
      "chrUn_GL456393\t55711\t5\t0\r\n",
      "chrUn_GL456394\t24323\t0\t0\r\n",
      "chrUn_GL456396\t21240\t2\t0\r\n",
      "chrUn_JH584304\t114452\t10220\t0\r\n",
      "*\t0\t0\t0\r\n",
      "38464485 + 0 in total (QC-passed reads + QC-failed reads)\r\n",
      "9361125 + 0 secondary\r\n",
      "0 + 0 supplementary\r\n",
      "0 + 0 duplicates\r\n",
      "38464485 + 0 mapped (100.00% : N/A)\r\n",
      "0 + 0 paired in sequencing\r\n",
      "0 + 0 read1\r\n",
      "0 + 0 read2\r\n",
      "0 + 0 properly paired (N/A : N/A)\r\n",
      "0 + 0 with itself and mate mapped\r\n",
      "0 + 0 singletons (N/A : N/A)\r\n",
      "0 + 0 with mate mapped to a different chr\r\n",
      "0 + 0 with mate mapped to a different chr (mapQ>=5)\r\n"
     ]
    }
   ],
   "source": [
    "samtools idxstats SRR3737439.bam\n",
    "samtools flagstat SRR3737439.bam"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Each .bam and accompanying .bai file can be downloaded and visualised in IGV."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's visualise the expression of Sox2 for example: Then we see a clear absence of the expression of Sox2 in the KO (bottom).\n",
    "\n",
    "<img src=\"/user/r0372900/files/Assignment_Bioinformatics2/RNAseq-bam-IGV-figure1.JPG\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Step 1.4: calculate reads to gene counts (~annotation)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "-We can now look at the reads per gene. But first we have to annotate our file. \n",
    "\n",
    "-To annotate we need a .gtf file of the mouse genome.\n",
    "  This can be found in: /mnt/storage/data/reference_genomes/mm10/Mus_musculus.GRCm38.76.chr.gtf \n",
    "  \n",
    "  Let's make a symbolic link to this file."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "lrwxrwxrwx 1 r0372900 domain users 71 jan  4 20:01 Mus_musculus.GRCm38.76.chr.gtf -> /mnt/storage/data/reference_genomes/mm10/Mus_musculus.GRCm38.76.chr.gtf\r\n"
     ]
    }
   ],
   "source": [
    "cd /mnt/storage/r0372900/Assignment_bioinformatics2/RNAseq\n",
    "ln -sf /mnt/storage/data/reference_genomes/mm10/Mus_musculus.GRCm38.76.chr.gtf /mnt/storage/r0372900/Assignment_bioinformatics2/RNAseq\n",
    "ls -l *gtf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "chr1\tpseudogene\tgene\t3054233\t3054733\t.\t+\t.\tgene_id \"ENSMUSG00000090025\"; gene_name \"Gm16088\"; gene_source \"havana\"; gene_biotype \"pseudogene\";\r\n",
      "chr1\tunprocessed_pseudogene\ttranscript\t3054233\t3054733\t.\t+\t.\tgene_id \"ENSMUSG00000090025\"; transcript_id \"ENSMUST00000160944\"; gene_name \"Gm16088\"; gene_source \"havana\"; gene_biotype \"pseudogene\"; transcript_name \"Gm16088-001\"; transcript_source \"havana\";\r\n",
      "chr1\tunprocessed_pseudogene\texon\t3054233\t3054733\t.\t+\t.\tgene_id \"ENSMUSG00000090025\"; transcript_id \"ENSMUST00000160944\"; exon_number \"1\"; gene_name \"Gm16088\"; gene_source \"havana\"; gene_biotype \"pseudogene\"; transcript_name \"Gm16088-001\"; transcript_source \"havana\"; exon_id \"ENSMUSE00000848981\";\r\n"
     ]
    }
   ],
   "source": [
    "# We can take a look at how the .gtf file looks\n",
    "head -3 Mus_musculus.GRCm38.76.chr.gtf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\r\n",
      "       \u001b[44;37m ========== \u001b[0m\u001b[36m    _____ _    _ ____  _____  ______          _____  \r\n",
      "       \u001b[44;37m =====      \u001b[0m\u001b[36m   / ____| |  | |  _ \\|  __ \\|  ____|   /\\   |  __ \\ \r\n",
      "       \u001b[44;37m   =====    \u001b[0m\u001b[36m  | (___ | |  | | |_) | |__) | |__     /  \\  | |  | |\r\n",
      "       \u001b[44;37m     ====   \u001b[0m\u001b[36m   \\___ \\| |  | |  _ <|  _  /|  __|   / /\\ \\ | |  | |\r\n",
      "       \u001b[44;37m       ==== \u001b[0m\u001b[36m   ____) | |__| | |_) | | \\ \\| |____ / ____ \\| |__| |\r\n",
      "       \u001b[44;37m ========== \u001b[0m\u001b[36m  |_____/ \\____/|____/|_|  \\_\\______/_/    \\_\\_____/\u001b[0m\r\n",
      "\tv1.5.0-p1\r\n",
      "\r\n",
      "//==========================\u001b[36m featureCounts setting \u001b[0m===========================\\\\\r\n",
      "||  \u001b[0m                                                                          ||\r\n",
      "||             Input files : \u001b[36m1 BAM file  \u001b[0m \u001b[0m                                    ||\r\n",
      "||                           \u001b[32mS\u001b[36m SRR3737439.bam\u001b[0m \u001b[0m                                ||\r\n",
      "||  \u001b[0m                                                                          ||\r\n",
      "||             Output file : \u001b[36mSRR3737439.counts\u001b[0m \u001b[0m                               ||\r\n",
      "||             Annotations : \u001b[36mMus_musculus.GRCm38.76.chr.gtf (GTF)\u001b[0m \u001b[0m            ||\r\n",
      "||  \u001b[0m                                                                          ||\r\n",
      "||                 Threads : \u001b[36m1\u001b[0m \u001b[0m                                               ||\r\n",
      "||                   Level : \u001b[36mmeta-feature level\u001b[0m \u001b[0m                              ||\r\n",
      "||              Paired-end : \u001b[36mno\u001b[0m \u001b[0m                                              ||\r\n",
      "||         Strand specific : \u001b[36mno\u001b[0m \u001b[0m                                              ||\r\n",
      "||      Multimapping reads : \u001b[36mnot counted\u001b[0m \u001b[0m                                     ||\r\n",
      "|| Multi-overlapping reads : \u001b[36mnot counted\u001b[0m \u001b[0m                                     ||\r\n",
      "||       Read orientations : \u001b[36mfr\u001b[0m \u001b[0m                                              ||\r\n",
      "||  \u001b[0m                                                                          ||\r\n",
      "\\\\=====================\u001b[36m http://subread.sourceforge.net/ \u001b[0m======================//\r\n",
      "\r\n",
      "//=================================\u001b[36m Running \u001b[0m==================================\\\\\r\n",
      "||  \u001b[0m                                                                          ||\r\n",
      "|| Load annotation file Mus_musculus.GRCm38.76.chr.gtf \u001b[0m... \u001b[0m                   ||\r\n",
      "||    Features : \u001b[36m653441\u001b[0m \u001b[0m                                                      ||\r\n",
      "||    Meta-features : \u001b[36m41255\u001b[0m \u001b[0m                                                  ||\r\n",
      "||    Chromosomes/contigs : \u001b[36m58\u001b[0m \u001b[0m                                               ||\r\n",
      "||  \u001b[0m                                                                          ||\r\n",
      "|| Process BAM file SRR3737439.bam... \u001b[0m                                        ||\r\n",
      "||    Single-end reads are included. \u001b[0m                                         ||\r\n",
      "||    Assign reads to features... \u001b[0m                                            ||\r\n",
      "||    Total reads : \u001b[36m38464485\u001b[0m \u001b[0m                                                 ||\r\n",
      "||    Successfully assigned reads : \u001b[36m21596493 (56.1%)\u001b[0m \u001b[0m                         ||\r\n",
      "||    Running time : \u001b[36m0.62 minutes\u001b[0m \u001b[0m                                            ||\r\n",
      "||  \u001b[0m                                                                          ||\r\n",
      "||                         \u001b[36mRead assignment finished.\u001b[0m \u001b[0m                         ||\r\n",
      "||  \u001b[0m                                                                          ||\r\n",
      "\\\\=====================\u001b[36m http://subread.sourceforge.net/ \u001b[0m======================//\r\n",
      "\r\n"
     ]
    }
   ],
   "source": [
    "# First try one\n",
    "# -Q stands for minimum quality score of the mapping that the read should have before we count is as a read (default=0).\n",
    "featureCounts -Q 10 -g gene_name -a Mus_musculus.GRCm38.76.chr.gtf -o SRR3737439.counts SRR3737439.bam"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"/user/r0372900/files/Assignment_Bioinformatics2/Genecount_SRR3737439-with-Q10.JPG\">\n",
    "\n",
    "The -Q 10 gave us a number of successfully assigned reads of 56.1%. Lowering it did not give any effect on the number of assigned reads.\n",
    "\n",
    "56,1% is an acceptable level however, and the other reads gave similar percentages. So this step is succesfull.\n",
    "\n",
    "    SRR3737439 (56.1%)     (GSM2224570)\tSox2 WT stomach 1 [RNA-seq]\n",
    "\n",
    "    SRR3737440 (55.0%)     (GSM2224571)\tSox2 WT stomach 2 [RNA-seq]\n",
    "\n",
    "    SRR3737441 (58.2%)     (GSM2224572)\tSox2 WT stomach 3 [RNA-seq]\n",
    "\n",
    "    SRR3737442 (59.7%)     (GSM2224573)\tSox2 KO stomach 1 [RNA-seq]\n",
    "\n",
    "    SRR3737443 (56.6%)     (GSM2224574)\tSox2 KO stomach 2 [RNA-seq]\n",
    "\n",
    "    SRR3737444 (52.2%)     (GSM2224575)\tSox2 KO stomach 3 [RNA-seq]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\r\n",
      "       \u001b[44;37m ========== \u001b[0m\u001b[36m    _____ _    _ ____  _____  ______          _____  \r\n",
      "       \u001b[44;37m =====      \u001b[0m\u001b[36m   / ____| |  | |  _ \\|  __ \\|  ____|   /\\   |  __ \\ \r\n",
      "       \u001b[44;37m   =====    \u001b[0m\u001b[36m  | (___ | |  | | |_) | |__) | |__     /  \\  | |  | |\r\n",
      "       \u001b[44;37m     ====   \u001b[0m\u001b[36m   \\___ \\| |  | |  _ <|  _  /|  __|   / /\\ \\ | |  | |\r\n",
      "       \u001b[44;37m       ==== \u001b[0m\u001b[36m   ____) | |__| | |_) | | \\ \\| |____ / ____ \\| |__| |\r\n",
      "       \u001b[44;37m ========== \u001b[0m\u001b[36m  |_____/ \\____/|____/|_|  \\_\\______/_/    \\_\\_____/\u001b[0m\r\n",
      "\tv1.5.0-p1\r\n",
      "\r\n",
      "//==========================\u001b[36m featureCounts setting \u001b[0m===========================\\\\\r\n",
      "||  \u001b[0m                                                                          ||\r\n",
      "||             Input files : \u001b[36m6 BAM files  \u001b[0m \u001b[0m                                   ||\r\n",
      "||                           \u001b[32mS\u001b[36m SRR3737439.bam\u001b[0m \u001b[0m                                ||\r\n",
      "||                           \u001b[32mS\u001b[36m SRR3737440.bam\u001b[0m \u001b[0m                                ||\r\n",
      "||                           \u001b[32mS\u001b[36m SRR3737441.bam\u001b[0m \u001b[0m                                ||\r\n",
      "||                           \u001b[32mS\u001b[36m SRR3737442.bam\u001b[0m \u001b[0m                                ||\r\n",
      "||                           \u001b[32mS\u001b[36m SRR3737443.bam\u001b[0m \u001b[0m                                ||\r\n",
      "||                           \u001b[32mS\u001b[36m SRR3737444.bam\u001b[0m \u001b[0m                                ||\r\n",
      "||  \u001b[0m                                                                          ||\r\n",
      "||             Output file : \u001b[36mall.counts\u001b[0m \u001b[0m                                      ||\r\n",
      "||             Annotations : \u001b[36mMus_musculus.GRCm38.76.chr.gtf (GTF)\u001b[0m \u001b[0m            ||\r\n",
      "||  \u001b[0m                                                                          ||\r\n",
      "||                 Threads : \u001b[36m1\u001b[0m \u001b[0m                                               ||\r\n",
      "||                   Level : \u001b[36mmeta-feature level\u001b[0m \u001b[0m                              ||\r\n",
      "||              Paired-end : \u001b[36mno\u001b[0m \u001b[0m                                              ||\r\n",
      "||         Strand specific : \u001b[36mno\u001b[0m \u001b[0m                                              ||\r\n",
      "||      Multimapping reads : \u001b[36mnot counted\u001b[0m \u001b[0m                                     ||\r\n",
      "|| Multi-overlapping reads : \u001b[36mnot counted\u001b[0m \u001b[0m                                     ||\r\n",
      "||       Read orientations : \u001b[36mfr\u001b[0m \u001b[0m                                              ||\r\n",
      "||  \u001b[0m                                                                          ||\r\n",
      "\\\\=====================\u001b[36m http://subread.sourceforge.net/ \u001b[0m======================//\r\n",
      "\r\n",
      "//=================================\u001b[36m Running \u001b[0m==================================\\\\\r\n",
      "||  \u001b[0m                                                                          ||\r\n",
      "|| Load annotation file Mus_musculus.GRCm38.76.chr.gtf \u001b[0m... \u001b[0m                   ||\r\n",
      "||    Features : \u001b[36m653441\u001b[0m \u001b[0m                                                      ||\r\n",
      "||    Meta-features : \u001b[36m41255\u001b[0m \u001b[0m                                                  ||\r\n",
      "||    Chromosomes/contigs : \u001b[36m58\u001b[0m \u001b[0m                                               ||\r\n",
      "||  \u001b[0m                                                                          ||\r\n",
      "|| Process BAM file SRR3737439.bam... \u001b[0m                                        ||\r\n",
      "||    Single-end reads are included. \u001b[0m                                         ||\r\n",
      "||    Assign reads to features... \u001b[0m                                            ||\r\n",
      "||    Total reads : \u001b[36m38464485\u001b[0m \u001b[0m                                                 ||\r\n",
      "||    Successfully assigned reads : \u001b[36m21596493 (56.1%)\u001b[0m \u001b[0m                         ||\r\n",
      "||    Running time : \u001b[36m0.52 minutes\u001b[0m \u001b[0m                                            ||\r\n",
      "||  \u001b[0m                                                                          ||\r\n",
      "|| Process BAM file SRR3737440.bam... \u001b[0m                                        ||\r\n",
      "||    Single-end reads are included. \u001b[0m                                         ||\r\n",
      "||    Assign reads to features... \u001b[0m                                            ||\r\n",
      "||    Total reads : \u001b[36m49256550\u001b[0m \u001b[0m                                                 ||\r\n",
      "||    Successfully assigned reads : \u001b[36m27084059 (55.0%)\u001b[0m \u001b[0m                         ||\r\n",
      "||    Running time : \u001b[36m0.64 minutes\u001b[0m \u001b[0m                                            ||\r\n",
      "||  \u001b[0m                                                                          ||\r\n",
      "|| Process BAM file SRR3737441.bam... \u001b[0m                                        ||\r\n",
      "||    Single-end reads are included. \u001b[0m                                         ||\r\n",
      "||    Assign reads to features... \u001b[0m                                            ||\r\n",
      "||    Total reads : \u001b[36m43882868\u001b[0m \u001b[0m                                                 ||\r\n",
      "||    Successfully assigned reads : \u001b[36m25553601 (58.2%)\u001b[0m \u001b[0m                         ||\r\n",
      "||    Running time : \u001b[36m0.58 minutes\u001b[0m \u001b[0m                                            ||\r\n",
      "||  \u001b[0m                                                                          ||\r\n",
      "|| Process BAM file SRR3737442.bam... \u001b[0m                                        ||\r\n",
      "||    Single-end reads are included. \u001b[0m                                         ||\r\n",
      "||    Assign reads to features... \u001b[0m                                            ||\r\n",
      "||    Total reads : \u001b[36m53280404\u001b[0m \u001b[0m                                                 ||\r\n",
      "||    Successfully assigned reads : \u001b[36m31804920 (59.7%)\u001b[0m \u001b[0m                         ||\r\n",
      "||    Running time : \u001b[36m0.71 minutes\u001b[0m \u001b[0m                                            ||\r\n",
      "||  \u001b[0m                                                                          ||\r\n",
      "|| Process BAM file SRR3737443.bam... \u001b[0m                                        ||\r\n",
      "||    Single-end reads are included. \u001b[0m                                         ||\r\n",
      "||    Assign reads to features... \u001b[0m                                            ||\r\n",
      "||    Total reads : \u001b[36m51625512\u001b[0m \u001b[0m                                                 ||\r\n",
      "||    Successfully assigned reads : \u001b[36m29211671 (56.6%)\u001b[0m \u001b[0m                         ||\r\n",
      "||    Running time : \u001b[36m0.71 minutes\u001b[0m \u001b[0m                                            ||\r\n",
      "||  \u001b[0m                                                                          ||\r\n",
      "|| Process BAM file SRR3737444.bam... \u001b[0m                                        ||\r\n",
      "||    Single-end reads are included. \u001b[0m                                         ||\r\n",
      "||    Assign reads to features... \u001b[0m                                            ||\r\n",
      "||    Total reads : \u001b[36m60530777\u001b[0m \u001b[0m                                                 ||\r\n",
      "||    Successfully assigned reads : \u001b[36m31582119 (52.2%)\u001b[0m \u001b[0m                         ||\r\n",
      "||    Running time : \u001b[36m0.78 minutes\u001b[0m \u001b[0m                                            ||\r\n",
      "||  \u001b[0m                                                                          ||\r\n",
      "||                         \u001b[36mRead assignment finished.\u001b[0m \u001b[0m                         ||\r\n",
      "||  \u001b[0m                                                                          ||\r\n",
      "\\\\=====================\u001b[36m http://subread.sourceforge.net/ \u001b[0m======================//\r\n",
      "\r\n"
     ]
    }
   ],
   "source": [
    "featureCounts -Q 10 -g gene_name -a Mus_musculus.GRCm38.76.chr.gtf -o all.counts    SRR3737439.bam \\\n",
    "                                                                                    SRR3737440.bam \\\n",
    "                                                                                    SRR3737441.bam \\\n",
    "                                                                                    SRR3737442.bam \\\n",
    "                                                                                    SRR3737443.bam \\\n",
    "                                                                                    SRR3737444.bam"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-rw-r--r-- 1 r0372900 domain users 7847644 jan  4 20:58 all.counts\r\n",
      "-rw-r--r-- 1 r0372900 domain users     616 jan  4 20:58 all.counts.summary\r\n",
      "-rw-r--r-- 1 r0372900 domain users 1016903 jan  4 21:03 all.gene.counts\r\n",
      "-rw-r--r-- 1 r0372900 domain users 1016903 jan  4 21:18 all.gene.counts.tsv\r\n"
     ]
    }
   ],
   "source": [
    "ls -l *.counts*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "-All the samples have normal results in their counts.summary (see below)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Status                     SRR3737439.bam  SRR3737440.bam  SRR3737441.bam  SRR3737442.bam  SRR3737443.bam  SRR3737444.bam\r\n",
      "Assigned                   21596493        27084059        25553601        31804920        29211671        31582119\r\n",
      "Unassigned_Ambiguity       860300          1026942         1176656         1360371         1192068         1351493\r\n",
      "Unassigned_MultiMapping    0               0               0               0               0               0\r\n",
      "Unassigned_NoFeatures      1786860         1769268         1664550         2002508         1868439         1993549\r\n",
      "Unassigned_Unmapped        0               0               0               0               0               0\r\n",
      "Unassigned_MappingQuality  14220832        19376281        15488061        18112605        19353334        25603616\r\n",
      "Unassigned_FragmentLength  0               0               0               0               0               0\r\n",
      "Unassigned_Chimera         0               0               0               0               0               0\r\n",
      "Unassigned_Secondary       0               0               0               0               0               0\r\n",
      "Unassigned_Nonjunction     0               0               0               0               0               0\r\n",
      "Unassigned_Duplicate       0               0               0               0               0               0\r\n"
     ]
    }
   ],
   "source": [
    "head -20 all.counts.summary | column -t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "# Program:featureCounts v1.5.0-p1; Command:\"featureCounts\" \"-Q\" \"10\" \"-g\" \"gene_name\" \"-a\" \"Mus_musculus.GRCm38.76.chr.gtf\" \"-o\" \"all.counts\" \"SRR3737439.bam\" \"SRR3737440.bam\" \"SRR3737441.bam\" \"SRR3737442.bam\" \"SRR3737443.bam\" \"SRR3737444.bam\" \r\n",
      "Geneid\tChr\tStart\tEnd\tStrand\tLength\r\n",
      "Gm16088\tchr1\t3054233\t3054733\t+\t501\r\n",
      "Gm26206\tchr1\t3102016\t3102125\t+\t110\r\n",
      "Xkr4\tchr1;chr1;chr1;chr1\t3205901;3213439;3421702;3670552\t3207317;3216968;3421901;3671498\t-;-;-;-\t6094\r\n",
      "Gm1992\tchr1;chr1\t3466587;3513405\t3466687;3513553\t+;+\t250\r\n",
      "Gm27396\tchr1\t3783876\t3783933\t-\t58\r\n",
      "Rp1\tchr1;chr1;chr1;chr1\t4343507;4351910;4352202;4360200\t4350091;4352081;4352837;4360314\t-;-;-;-\t7508\r\n",
      "Sox17\tchr1;chr1;chr1;chr1;chr1\t4490928;4493100;4493772;4495136;4496291\t4492668;4493490;4493863;4495942;4496413\t-;-;-;-;-\t3154\r\n",
      "Gm22307\tchr1\t4529017\t4529123\t+\t107\r\n"
     ]
    }
   ],
   "source": [
    "cut -f-6 all.counts > all.genedata.tsv\n",
    "head all.genedata.tsv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Geneid\tSRR3737439.bam\tSRR3737440.bam\tSRR3737441.bam\tSRR3737442.bam\tSRR3737443.bam\tSRR3737444.bam\r\n",
      "Gm16088\t0\t0\t0\t0\t0\t0\r\n",
      "Gm26206\t0\t0\t0\t0\t0\t0\r\n",
      "Xkr4\t1\t2\t1\t3\t0\t2\r\n",
      "Gm1992\t0\t2\t0\t0\t0\t0\r\n",
      "Gm27396\t0\t0\t0\t0\t0\t0\r\n",
      "Rp1\t0\t0\t0\t0\t0\t0\r\n",
      "Sox17\t72\t122\t40\t87\t105\t126\r\n",
      "Gm22307\t0\t0\t0\t0\t0\t0\r\n",
      "Gm6085\t10\t14\t13\t13\t20\t31\r\n"
     ]
    }
   ],
   "source": [
    "cut -f1,7- all.counts | grep -v '^#' > all.gene.counts\n",
    "head all.gene.counts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Geneid\tSRR3737439.bam\tSRR3737440.bam\tSRR3737441.bam\tSRR3737442.bam\tSRR3737443.bam\tSRR3737444.bam\r\n",
      "Gm16088\t0\t0\t0\t0\t0\t0\r\n",
      "Gm26206\t0\t0\t0\t0\t0\t0\r\n",
      "Xkr4\t1\t2\t1\t3\t0\t2\r\n",
      "Gm1992\t0\t2\t0\t0\t0\t0\r\n",
      "Gm27396\t0\t0\t0\t0\t0\t0\r\n",
      "Rp1\t0\t0\t0\t0\t0\t0\r\n",
      "Sox17\t72\t122\t40\t87\t105\t126\r\n",
      "Gm22307\t0\t0\t0\t0\t0\t0\r\n",
      "Gm6085\t10\t14\t13\t13\t20\t31\r\n"
     ]
    }
   ],
   "source": [
    "#let's also make a .tsv file of the counts so that we can open it in Excel.\n",
    "cut -f1,7- all.counts | grep -v '^#' > all.gene.counts.tsv\n",
    "head all.gene.counts.tsv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "collapsed": false,
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Sox2ot\t18\t15\t14\t58\t57\t78\r\n",
      "Sox2\t585\t573\t608\t24\t14\t34\r\n",
      "Sox21\t3489\t3616\t5960\t4693\t3391\t3667\r\n"
     ]
    }
   ],
   "source": [
    "grep Sox2 all.gene.counts"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Step 1.5: Differential expression"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We had 2 options of doing differential expression: MS Excel in the GUI or DESeq2 in R. We did both.\n",
    "\n",
    "The R-file of DESeq2 can be found in the pdf in attachment under title: \"DESeq\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### See attachment: \"1. Calculation of differential expressino between WT and KO samples\" for DESeq2 script in R-notebook."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Part 2: ChIP sequencing"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Before we go into detail about the further tests we did with the differential expression data, we analysed the ChIP sequencing data. This gave us more information, which allowed us to choose our tests for the differential expression more carefully."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Step 2.1: Make directory + download fastq files"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "mkdir -p /mnt/storage/r0372900/Assignment_bioinformatics2/ChIPseq/\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/mnt/storage/r0372900/Assignment_bioinformatics2/ChIPseq\r\n"
     ]
    }
   ],
   "source": [
    "cd /mnt/storage/r0372900/Assignment_bioinformatics2/ChIPseq/\n",
    "pwd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Because the ChIPseq files are not too large for the home directory. We could download the sra files with the normal fastq-dump command. After the download, we deleted the cache version of the .sra in the home directory and worked further on the fastq data in our working directory."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "fastq-dump SRR3737445 -O /mnt/storage/r0372900/Assignment_bioinformatics2/ChIPseq\n",
    "rm -r /home/luna.kuleuven.be/r0372900/ncbi/\n",
    "fastq-dump SRR3737446 -O /mnt/storage/r0372900/Assignment_bioinformatics2/ChIPseq\n",
    "rm -r /home/luna.kuleuven.be/r0372900/ncbi/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "mv SRR3737445.fastq SRR3737445_WT.fastq\n",
    "mv SRR3737446.fastq SRR3737446_KO.fastq\n",
    "ls"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Step 2.2: mapping to the genome"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For mapping of the ChIPseq reads, we used the bowtie2 package."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "bowtie2-align -x /mnt/nfs/data/reference_genomes/mm10/mm10 ./SRR3737445_WT.fastq -S SRR3737445_WT.sam\n",
    "bowtie2-align -x /mnt/nfs/data/reference_genomes/mm10/mm10 ./SRR3737446_KO.fastq -S SRR3737446_KO.sam"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"/user/r0372900/files/Assignment_Bioinformatics2/ChIPseq-Figure1-bowtie.JPG\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "after mapping, we once more compress the big .sam file to a smaller .bam file and generate an index to visualize in IGV"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "samtools view -S -b SRR3737445_WT.sam > SRR3737445_WT.bam\n",
    "samtools view -S -b SRR3737446_KO.sam > SRR3737446_KO.bam"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "samtools sort -O bam -o SRR3737445_WT.sorted.bam SRR3737445_WT.bam\n",
    "samtools sort -O bam -o SRR3737446_KO.sorted.bam SRR3737446_KO.bam "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "samtools index SRR3737445_WT.sorted.bam\n",
    "samtools index SRR3737446_KO.sorted.bam"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The Bam files can now be visualized in IGV. (See step 2.3 for visualization)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Step 2.3: calling ChIPseq peaks"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "macs2 callpeak -t  SRR3737445_WT.sorted.bam --outdir macs2_SRR3737445_WT -n SRR3737445_WT -g mm -q 0.005\n",
    "macs2 callpeak -t  SRR3737446_KO.sorted.bam --outdir macs2_SRR3737446_KO -n SRR3737446_KO -g mm -q 0.005"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This command gives us peaks, narrow peaks, and summits. We will work with the narrow peaks in our further tests.\n",
    "\n",
    "Let's head the narrowpeak file to see how it looks."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "chr1\t4722724\t4722904\tSRR3737445_WT_peak_1\t100\t.\t7.73861\t14.14513\t10.04187\t110\r\n",
      "chr1\t6973751\t6973934\tSRR3737445_WT_peak_2\t69\t.\t6.70971\t10.87522\t6.99946\t87\r\n",
      "chr1\t10699869\t10700186\tSRR3737445_WT_peak_3\t84\t.\t6.98095\t12.47094\t8.47891\t103\r\n",
      "chr1\t13266870\t13267031\tSRR3737445_WT_peak_4\t66\t.\t6.49833\t10.53417\t6.69065\t85\r\n",
      "chr1\t16228429\t16228746\tSRR3737445_WT_peak_5\t207\t.\t10.67768\t25.45601\t20.78275\t128\r\n",
      "chr1\t20820242\t20820430\tSRR3737445_WT_peak_6\t98\t.\t7.89083\t13.96368\t9.87352\t113\r\n",
      "chr1\t20857021\t20857287\tSRR3737445_WT_peak_7\t71\t.\t6.32711\t10.99427\t7.10964\t146\r\n",
      "chr1\t20863850\t20864016\tSRR3737445_WT_peak_8\t137\t.\t8.97666\t18.09726\t13.76226\t62\r\n",
      "chr1\t21231745\t21231929\tSRR3737445_WT_peak_9\t71\t.\t6.58126\t11.03197\t7.14467\t70\r\n",
      "chr1\t21324355\t21324514\tSRR3737445_WT_peak_10\t96\t.\t7.72306\t13.68940\t9.61458\t91\r\n"
     ]
    }
   ],
   "source": [
    "head macs2_SRR3737445_WT/SRR3737445_WT_peaks.narrowPeak"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can see that the files look good. Now, we can count the rows to see the amount of peaks that were found. We expect the WT to have a lot of peaks (because Sox2 binds a lot of TFBS), while we expect our Cre-induced Sox2 KO to have only very few peaks."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3431 macs2_SRR3737445_WT/SRR3737445_WT_peaks.narrowPeak\r\n",
      "268 macs2_SRR3737446_KO/SRR3737446_KO_peaks.narrowPeak\r\n"
     ]
    }
   ],
   "source": [
    "wc -l macs2_SRR3737445_WT/SRR3737445_WT_peaks.narrowPeak\n",
    "wc -l macs2_SRR3737446_KO/SRR3737446_KO_peaks.narrowPeak"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can see that this is indeed the case, and that the KO has only very few peaks.\n",
    "\n",
    "SRR3737445 = GSM2224576 Wild type Stomach [ChIP-seq]  : 3431\n",
    "\n",
    "SRR3737446 = GSM2224577 Sox2 KO Stomach [ChIP-seq]    : 268"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now, let's download the narrowPeak files, save them as .bed files, and perform some gene annotations, enrichment testing, and other tests to them."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Part 3: Functional Analysis Study and Gene Enrichment testing"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As a third and most important part of the project. We tested what those Sox2 binding sites in the genome and the differences in differential expression, now actually meant for the function of Sox2 in the tissue, and the information we can substract from it."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Tests on the Differential Expression data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1) R             -> checking for compensation by other Sox-family members\n",
    "\n",
    "2) Iregulon      -> identify possible TF for these genes\n",
    "\n",
    "3) GSEA          -> motif enrichment \n",
    "\n",
    "4) Gene Ontology -> identify biological function of the higher and lower expressed genes respectively and make conclusions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1) R"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### See attachment: \"2. Checking possible compensation for loss of Sox2 activity by Sox family members\" in R-notebook"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2) iRegulon"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We inserted our list of differentially expressed genes in iRegulon and looked at the motifs and TF's that it returned.\n",
    "\n",
    "We found the following TF's to be possible candidates for causing our expression levels:\n",
    "\n",
    "<img src=\"/user/r0372900/files/Assignment_Bioinformatics2/RNAseq-figure2-iRegulon-Transcriptiefactoren.JPG\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Of these genes, mainly Pou2f1, Pou5f1, Spib, Tcf7, and Gata1 were interesting:\n",
    "\n",
    "    -Spib was differentially expressed with a factor log2(x)=1.5, and is now found to be able to influence this expression as a TF.\n",
    "    \n",
    "    -Pou2f1 and Pou5f1 are Oct1 and Oct3/4 respectively. Known to be important growth hormones and dysregulated in some carcinoma's.\n",
    "    \n",
    "    -Tcf7 is part of the Wnt-pathway, just like Sox2, that it is upregulated might point out to some kind of compensation mechanism, or otherwise to a dysequilibrium because of the loss of Sox2. \n",
    "    \n",
    "    -Gata1: activates genes for erythrocid differentiation and is known to be present in erythroid leukemia."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 3) GSEA"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "GSEA is able to show us the enriched motifs and the pathways in which they are included."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "(GSEA is mainly meant for human genes. We changed the gene names to capital letters to be able to run against human genes. Altough less precise, it still gives us valuable information about the processes involved)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    3.1) Generation of the Ranked file (.rnk) --> see R-script"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### See attachment: \"3. Generation of the rank list file\" in R-notbook"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    3.2) Running GSEA\n",
    "\n",
    "We searched for the involvement of Hallmarks in cell-differentation and found the following interesting results:\n",
    "\n",
    "The graphs should be interpreted with a correlation to the presence of Sox2. When there is positive correleation, it means more Sox2 leads to more of that genetic pathway. When there is negative correlation, it means more Sox2 leads to less of that genetic pathway.\n",
    "\n",
    "<img src=\"/user/r0372900/files/Assignment_Bioinformatics2/GSEA-HALLMARK-figure1.png\">\n",
    "We can see that the presence of Sox2 is slightly correlated with an enrichment of genes in the Angiogenesis pathway.\n",
    "\n",
    "<img src=\"/user/r0372900/files/Assignment_Bioinformatics2/GSEA-HALLMARK-figure2-ESTROGEN_RESPONSE_LATE_19.png\"> <img src=\"/user/r0372900/files/Assignment_Bioinformatics2/GSEA-HALLMARK-figure3-ESTROGEN_RESPONSE_EARLY_17.png\">\n",
    "We can see that the presence of Sox2 is slightly correlated with an enrichment of genes in the Estrogen pathway.\n",
    "\n",
    "<img src=\"/user/r0372900/files/Assignment_Bioinformatics2/GSEA-HALLMARK-figure4-APICAL_SURFACE_11.png\">\n",
    "Sox2 presence leads to enrichment in apical suface proteins. This can be seen as a hint to a link to cancer, as metastasis needs certain apical surface proteins while it needs to lose others.\n",
    "\n",
    "<img src=\"/user/r0372900/files/Assignment_Bioinformatics2/GSEA-HALLMARK-figure5-HEDGEHOG_SIGNALING_1.png\">\n",
    "Sox2 absence clearly leads to diminished Hedgehog signalling pathway.\n",
    "\n",
    "<img src=\"/user/r0372900/files/Assignment_Bioinformatics2/GSEA-HALLMARK-figure6-HALLMARK_HYPOXIA_15.png\">\n",
    "Sox2 absence leads to less hypoxia.\n",
    "\n",
    "<img src=\"/user/r0372900/files/Assignment_Bioinformatics2/GSEA-HALLMARK-figure7-IL6_JAK_STAT3_SIGNALING_47.png\">\n",
    "Sox2 absence is strongly correlated with an increase in IL6, JAK, STAT3 signalling pathway.\n",
    "\n",
    "<img src=\"/user/r0372900/files/Assignment_Bioinformatics2/GSEA-HALLMARK-figure8-INFLAMMATORY_RESPONSE_51.png\">\n",
    "Sox2 absence is correlated to an increase in inflammatory response.\n",
    "\n",
    "<img src=\"/user/r0372900/files/Assignment_Bioinformatics2/GSEA-HALLMARK-figure9-INTERFERON_ALPHA_RESPONSE_41.png\">\n",
    "Sox2 absence is correlated to an increase in interferon-alpha-response.\n",
    "\n",
    "<img src=\"/user/r0372900/files/Assignment_Bioinformatics2/GSEA-HALLMARK-figure10-INTERFERON_GAMMA_RESPONSE_43.png\">\n",
    "Sox2 absence is correlated to an increase in interferon-gamma-response.\n",
    "\n",
    "<img src=\"/user/r0372900/files/Assignment_Bioinformatics2/GSEA-HALLMARK-figure11-HALLMARK_KRAS_SIGNALING_UP.png\">\n",
    "Sox2 absence leads to a diminishment of the KRAS-singalling.\n",
    "\n",
    "<img src=\"/user/r0372900/files/Assignment_Bioinformatics2/GSEA-HALLMARK-figure12-WNT_BETA_CATENIN_SIGNALING_23.png\">\n",
    "Sox2 absence leads to a diminishment of WNT-B-catenin-pathway.\n",
    "\n",
    "<img src=\"/user/r0372900/files/Assignment_Bioinformatics2/GSEA-HALLMARK-figure13-XENOBIOTIC_METABOLISM.png\">\n",
    "Sox2 absence leads to an increase in Xenobiotic metabolism.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 4) Gene Ontology (Gorilla)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For the Gene ontology we used Gorilla. The gene lists were obtained via ranking them in R the same way we did for GSEA. Gorilla gives us the cellular processes and the pathway that the differentially expressed genes are involved in. The results are described below: \n",
    "\n",
    "Note: there were many genes involved in the 10^-3 range. We filtered these out by setting the p-value threshold to 10^-4"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 4.1) Gorilla of upregulated genes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The upregulated genes are important for several processes in lipid production, included those of the inflammatory response. This can point to a possible link with tumorigenesis. Furthermore, it has a role in the negative regulation of the immune respons, which also can point to a possible link with tumorigenesis.\n",
    "In terms of function, the upregulated genes are important for arachidonate 5-lipoxygenase activity.\n",
    "\n",
    "Processes:\n",
    "<img src=\"/user/r0372900/files/Assignment_Bioinformatics2/RNAseq-figure3-Gorilla.JPG\">\n",
    "\n",
    "Function:\n",
    "<img src=\"/user/r0372900/files/Assignment_Bioinformatics2/RNAseq-figure4-Gorilla.JPG\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 4.2) Gorilla of downregulated genes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The downregulated genes are important for several glucuronidation processes, including those for xenobiotics, flavonoids and cellular proteins.\n",
    "In terms of function, the downregulated genes ara important for glucuronidation and interleukin receptor activity. There is no direct relevance in their downregulation in cancer.\n",
    "\n",
    "Processes:\n",
    "<img src=\"/user/r0372900/files/Assignment_Bioinformatics2/RNAseq-figure5-Gorilla.JPG\">\n",
    "\n",
    "Function:\n",
    "<img src=\"/user/r0372900/files/Assignment_Bioinformatics2/RNAseq-figure6-Gorilla.JPG\">\n",
    "<img src=\"/user/r0372900/files/Assignment_Bioinformatics2/RNAseq-figure7-Gorilla.JPG\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note: When performing Gorilla analysis on the whole gene list and setting the p-value at 10^-3, we got more results that can link Sox2 to tumorigenesis. These were all in the fields of morphology, inflammation and immune response. (Data not shown)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Tests on the ChIPseq data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1) RSAT"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Using UCSC-table browser, we got the sequences of our narrowpeaks. From these sequences, we identified the most common Motifs that were present in regions that Sox2 bound. The top 3 of motifs are the following:\n",
    "\n",
    "    1) AACAAAG  = Sox2 binding motif\n",
    "    \n",
    "    2) AACAATG  = Other Sox proteins (is logical because same family of TF.)\n",
    "    \n",
    "    3) TGACTCA = FOSL1, FOSL2 and JUNB binding motif (these TF's are involved in MAPK signalling and oxidative stress.)\n",
    "\n",
    "<img src=\"/user/r0372900/files/Assignment_Bioinformatics2/RSAT-figure1.JPG\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "When all the motifs were grouped together, we found this list of TF's that are able to bind:\n",
    "\n",
    "FOSL1, \n",
    "FOSL2, \n",
    "FOXP1, \n",
    "FOXP3, \n",
    "GATA1, \n",
    "GATA3, \n",
    "GATA4, \n",
    "JUNB, \n",
    "JUND, \n",
    "Klf1, \n",
    "Klf5, \n",
    "LEF1, \n",
    "Sox10, \n",
    "Sox2, \n",
    "Sox21, \n",
    "Sox3, \n",
    "Sox5, \n",
    "Sox6, \n",
    "Sox9, \n",
    "SP1, \n",
    "SP2, \n",
    "SP3, \n",
    "SRY, \n",
    "TCF7L2.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2) GREAT"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Then, we used GREAT, a tool that could show us the neighouring genes. We searched for all the genes that were within 10kb up- or downstream of our peaks. We uploaded the peaks to the website in a .bed file. \n",
    "\n",
    "The results of GREAT was a file with all the neigbouring genes for each ChIPseq-peak."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### See attachment: \"4. Analysis of location of Sox2 binding peaks in the WT sample\" in the R-notebook."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "-Using R, we tested how many of the ChIPseq binding sites were located within the 500bp upstream region of the genes. We found that is was 13.2%. The same could be done for binding sites within 500bp of promotor regions, these were 2.22%.\n",
    "(See R-script subtitle 4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "-Interestingly, our data also contained the Sox2 gene 500kb upstream of one of the peaks. This has been described previously in literature and has been assigned as the cause of Sox2's self-induction. \n",
    "\n",
    "<img src=\"/user/r0372900/files/Assignment_Bioinformatics2/GREAT-figure1.JPG\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 3) Gene Ontology"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Cellular component:\n",
    "\n",
    "-Mainly for intestinal tissue\n",
    "\n",
    "<img src=\"/user/r0372900/files/Assignment_Bioinformatics2/GO-figure1-cellular-component.png\">\n",
    "\n",
    "Mouse phenotype:\n",
    "\n",
    "-A lot of associated cancers and abnormalities\n",
    "\n",
    "<img src=\"/user/r0372900/files/Assignment_Bioinformatics2/GO-figure2-mouse-phenotype.png\">\n",
    "\n",
    "Molecular function:\n",
    "\n",
    "<img src=\"/user/r0372900/files/Assignment_Bioinformatics2/GO-figure3-molecular-function.png\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Cross-referencing of RNAseq with ChIPseq data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can conclude that only 2 significantly differentially expressed genes have a Sox2 binding peak in their upstream region, while none of the significantly differentially expressed genes have a Sox2 binding peak in their promotor region.\n",
    "\n",
    "Cxcl17 shows singificant downregulation upon knock down, albeit the padj value (0.048695841) is on the verge of non-significance and its log2FoldChange is also low. Acsm3 is significantly upregulated on knock down, though the log2FoldChange value is again on the low side. Here the results provide evidence for both activation and repression of transcription by Sox2."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### See attachment: \"5. Exploration of the effect of Sox2 binding on differential expression\" "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Conclusion"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "Our data shows that the Sox2 KO was succesful. The affected genes give some hints towards a possible role in cancer, identified through a very general gene ontology. In our specific case however, the KO of Sox2 in gastric stem cells lead to no significant noticable effect; We identified no compensation mechanisms and the morphology of the gatric tissue of the mice was not affected. \n",
    "\n",
    "The literature that reports Sox2 being involved in cancer is ambiguous. Different phenotypes can be the cause of these reports. In that case, our data show no direct involvement in cancer of Sox2 in gastric stem cell populations.\n",
    "\n",
    "In conclusion, our data analysis suggests that Sox2 has a different role in gastric tissue compared to other tissues and no significant role in tumorigenesis has been found."
   ]
  }
 ],
 "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
}
