{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# RNASEQ DATA ANALYSIS\n",
    "\n",
    "##### GROUP PROJECT BIOINFORMATICS\n",
    "\n",
    "Aïsha Callebaut, Hava Izci and Xander Spotbeen\n",
    "\n",
    "Location of the original ipynb file: /mnt/nfs/r0386707/jupyternotebooks/RNASeq-2.ipynb"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## DATASET\n",
    "\n",
    "Dataset GSE55688 was used for RNASeq data analysis.\n",
    "\n",
    "Aberrant cell proliferation, a hallmark of most cancers, requires the escape from intrinsic antitumour barriers. Primary among these is the DNA damage response (DDR). In both cell culture-models and in early stages of tumorigenesis in vivo, activated oncogenes induce DNA replication stress and DNA double-strand breaks (DSBs), leading to DDR activation and p53-dependent apoptosis and/or senescence. The means by which tumour-initiating cells, also termed cancer stem cells (CSCs), circumvent this oncosuppressive response is unknown. Here we demonstrate that the ZEB1 transcription factor provides breast CSCs with the ability to withstand an aberrant mitogenic activity. Its forced expression in human mammary epithelial cells is sufficient to alleviate DNA replicative stress and to decrease the production of reactive oxygen species, an important contributor to DDR and oncogene-induced senescence. Consistently, human breast cancer cells with endogenous ZEB1 expression show two characteristic features: low levels of DSBs and DDR markers, reflecting mitigation of the DNA replication stress, and a low p53 mutation frequency, reflecting a weak selective pressure for inactivation. Using high-throughput sequencing analysis of controlled cellular models, we further demonstrate that ZEB1 delays the onset of structural chromosomal instability (CIN), a known consequence of replicative stress and prevents the emergence of chromosome 8p deletions and 8q amplifications, two prevalent abnormalities in high-grade breast cancers. Supporting these findings, ZEB1 expression discriminates human breast tumours by their copy number alterations (CNAs) and chromosome 8 aberrations. We propose that the tumorigenic potential of CSCs relies upon their unique capacity to tolerate oncogenic stimuli through the alleviation of DNA replication stress."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2 different datasets are chosen: the first one is the HMEC hTERT immortalized cell line ectopically expressing HRasG12V and ZEB1, 90 days after transduction (SRR1198284), the second one is the HMEC hTERT immortalized cell line, 90 days after transduction (SRR1197249)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Download and convert data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Make directory to work in:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "mkdir -p ~/data/ncbi\n",
    "vdb-config -s /repository/user/main/public/root=~/data/ncbi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/home/luna.kuleuven.be/r0386707/data/work\r\n"
     ]
    }
   ],
   "source": [
    "mkdir -p ~/data/work\n",
    "cd ~/data/work\n",
    "pwd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Download and convert data from SRA to fastq:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "fastq-dump --split-files SRR1198284"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "fastq-dump --split-files SRR1197249"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Create directory"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/home/luna.kuleuven.be/r0386707/data/RNAseq\r\n"
     ]
    }
   ],
   "source": [
    "mkdir -p ~/data/RNAseq\n",
    "cd ~/data/RNAseq\n",
    "pwd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Create symbolic link to the raw fastq files"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "total 51119264\r\n",
      "lrwxrwxrwx 1 r0386707 domain users          38 jan 22 10:01 *.fastq -> /mnt/nfs/data/RNA-seq/GSE55688/*.fastq\r\n",
      "-rw-r--r-- 1 r0386707 domain users 13850578780 jan 21 18:41 SRR1197249_1.fastq\r\n",
      "-rw-r--r-- 1 r0386707 domain users 13835715356 jan 21 18:41 SRR1197249_2.fastq\r\n",
      "-rw-r--r-- 1 r0386707 domain users 12334840676 jan 21 18:27 SRR1198284_1.fastq\r\n",
      "-rw-r--r-- 1 r0386707 domain users 12324967132 jan 21 18:27 SRR1198284_2.fastq\r\n"
     ]
    }
   ],
   "source": [
    "ln -sf /mnt/nfs/data/RNA-seq/GSE55688/*.fastq ~/data/RNAseq/\n",
    "ls -l"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## QUALITY CONTROL USING FASTQC\n",
    "\n",
    "Produce a quality analysis report for the 4 fastq files with fastQC:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Started analysis of SRR1197249_1.fastq\r\n",
      "Approx 5% complete for SRR1197249_1.fastq\r\n",
      "Approx 10% complete for SRR1197249_1.fastq\r\n",
      "Approx 15% complete for SRR1197249_1.fastq\r\n",
      "Approx 20% complete for SRR1197249_1.fastq\r\n",
      "Approx 25% complete for SRR1197249_1.fastq\r\n",
      "Approx 30% complete for SRR1197249_1.fastq\r\n",
      "Approx 35% complete for SRR1197249_1.fastq\r\n",
      "Approx 40% complete for SRR1197249_1.fastq\r\n",
      "Approx 45% complete for SRR1197249_1.fastq\r\n",
      "Approx 50% complete for SRR1197249_1.fastq\r\n",
      "Approx 55% complete for SRR1197249_1.fastq\r\n",
      "Approx 60% complete for SRR1197249_1.fastq\r\n",
      "Approx 65% complete for SRR1197249_1.fastq\r\n",
      "Approx 70% complete for SRR1197249_1.fastq\r\n",
      "Approx 75% complete for SRR1197249_1.fastq\r\n",
      "Approx 80% complete for SRR1197249_1.fastq\r\n",
      "Approx 85% complete for SRR1197249_1.fastq\r\n",
      "Approx 90% complete for SRR1197249_1.fastq\r\n",
      "Approx 95% complete for SRR1197249_1.fastq\r\n",
      "Analysis complete for SRR1197249_1.fastq\r\n"
     ]
    }
   ],
   "source": [
    "/usr/local/bin/FastQC/fastqc -o . SRR1197249_1.fastq"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Started analysis of SRR1197249_2.fastq\r\n",
      "Approx 5% complete for SRR1197249_2.fastq\r\n",
      "Approx 10% complete for SRR1197249_2.fastq\r\n",
      "Approx 15% complete for SRR1197249_2.fastq\r\n",
      "Approx 20% complete for SRR1197249_2.fastq\r\n",
      "Approx 25% complete for SRR1197249_2.fastq\r\n",
      "Approx 30% complete for SRR1197249_2.fastq\r\n",
      "Approx 35% complete for SRR1197249_2.fastq\r\n",
      "Approx 40% complete for SRR1197249_2.fastq\r\n",
      "Approx 45% complete for SRR1197249_2.fastq\r\n",
      "Approx 50% complete for SRR1197249_2.fastq\r\n",
      "Approx 55% complete for SRR1197249_2.fastq\r\n",
      "Approx 60% complete for SRR1197249_2.fastq\r\n",
      "Approx 65% complete for SRR1197249_2.fastq\r\n",
      "Approx 70% complete for SRR1197249_2.fastq\r\n",
      "Approx 75% complete for SRR1197249_2.fastq\r\n",
      "Approx 80% complete for SRR1197249_2.fastq\r\n",
      "Approx 85% complete for SRR1197249_2.fastq\r\n",
      "Approx 90% complete for SRR1197249_2.fastq\r\n",
      "Approx 95% complete for SRR1197249_2.fastq\r\n",
      "Analysis complete for SRR1197249_2.fastq\r\n"
     ]
    }
   ],
   "source": [
    "/usr/local/bin/FastQC/fastqc -o . SRR1197249_2.fastq"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Started analysis of SRR1198284_1.fastq\r\n",
      "Approx 5% complete for SRR1198284_1.fastq\r\n",
      "Approx 10% complete for SRR1198284_1.fastq\r\n",
      "Approx 15% complete for SRR1198284_1.fastq\r\n",
      "Approx 20% complete for SRR1198284_1.fastq\r\n",
      "Approx 25% complete for SRR1198284_1.fastq\r\n",
      "Approx 30% complete for SRR1198284_1.fastq\r\n",
      "Approx 35% complete for SRR1198284_1.fastq\r\n",
      "Approx 40% complete for SRR1198284_1.fastq\r\n",
      "Approx 45% complete for SRR1198284_1.fastq\r\n",
      "Approx 50% complete for SRR1198284_1.fastq\r\n",
      "Approx 55% complete for SRR1198284_1.fastq\r\n",
      "Approx 60% complete for SRR1198284_1.fastq\r\n",
      "Approx 65% complete for SRR1198284_1.fastq\r\n",
      "Approx 70% complete for SRR1198284_1.fastq\r\n",
      "Approx 75% complete for SRR1198284_1.fastq\r\n",
      "Approx 80% complete for SRR1198284_1.fastq\r\n",
      "Approx 85% complete for SRR1198284_1.fastq\r\n",
      "Approx 90% complete for SRR1198284_1.fastq\r\n",
      "Approx 95% complete for SRR1198284_1.fastq\r\n",
      "Analysis complete for SRR1198284_1.fastq\r\n"
     ]
    }
   ],
   "source": [
    "/usr/local/bin/FastQC/fastqc -o . SRR1198284_1.fastq"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Started analysis of SRR1198284_2.fastq\r\n",
      "Approx 5% complete for SRR1198284_2.fastq\r\n",
      "Approx 10% complete for SRR1198284_2.fastq\r\n",
      "Approx 15% complete for SRR1198284_2.fastq\r\n",
      "Approx 20% complete for SRR1198284_2.fastq\r\n",
      "Approx 25% complete for SRR1198284_2.fastq\r\n",
      "Approx 30% complete for SRR1198284_2.fastq\r\n",
      "Approx 35% complete for SRR1198284_2.fastq\r\n",
      "Approx 40% complete for SRR1198284_2.fastq\r\n",
      "Approx 45% complete for SRR1198284_2.fastq\r\n",
      "Approx 50% complete for SRR1198284_2.fastq\r\n",
      "Approx 55% complete for SRR1198284_2.fastq\r\n",
      "Approx 60% complete for SRR1198284_2.fastq\r\n",
      "Approx 65% complete for SRR1198284_2.fastq\r\n",
      "Approx 70% complete for SRR1198284_2.fastq\r\n",
      "Approx 75% complete for SRR1198284_2.fastq\r\n",
      "Approx 80% complete for SRR1198284_2.fastq\r\n",
      "Approx 85% complete for SRR1198284_2.fastq\r\n",
      "Approx 90% complete for SRR1198284_2.fastq\r\n",
      "Approx 95% complete for SRR1198284_2.fastq\r\n",
      "Analysis complete for SRR1198284_2.fastq\r\n"
     ]
    }
   ],
   "source": [
    "/usr/local/bin/FastQC/fastqc -o . SRR1198284_2.fastq"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The html files were downloaded and inspected. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "Quality control of SRR1197249_1:\n",
    "\n",
    "<img width=\"50%\" src=\"/user/r0386707/files/Schermafbeelding%202017-01-22%20om%2013.03.12.jpg\">\n",
    "<img width=\"50%\" src=\"/user/r0386707/files/Schermafbeelding%202017-01-22%20om%2013.04.55.jpg\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Quality control of SRR1197249_2:\n",
    "\n",
    "<img width=\"50%\" src=\"/user/r0386707/files/Schermafbeelding%202017-01-22%20om%2013.06.34.jpg\">\n",
    "<img width=\"50%\" src=\"/user/r0386707/files/Schermafbeelding%202017-01-22%20om%2013.06.57.jpg\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Quality control of SRR1198284_1:\n",
    "\n",
    "<img width=\"50%\" src=\"/user/r0386707/files/Schermafbeelding%202017-01-22%20om%2013.07.44.jpg\">\n",
    "<img width=\"50%\" src=\"/user/r0386707/files/Schermafbeelding%202017-01-22%20om%2013.08.05.jpg\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Quality control of SRR1198284_2:\n",
    "\n",
    "<img width=\"50%\" src=\"/user/r0386707/files/Schermafbeelding%202017-01-22%20om%2013.08.52.jpg\">\n",
    "<img width=\"50%\" src=\"/user/r0386707/files/Schermafbeelding%202017-01-22%20om%2013.09.15.jpg\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The per base sequence quality and the sequence duplication level are OK for all 4 fastq files and there are no overrepresented sequences. We can continue without pre-processing our data since the quality is already good."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## MAPPING TO THE GENOME"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Database with the human genome and gene annotation we will use to map our reads:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Jan 22 10:49:45 ..... started STAR run\r\n",
      "Jan 22 10:49:45 ..... loading genome\r\n"
     ]
    }
   ],
   "source": [
    "STAR --genomeLoad LoadAndExit --genomeDir /mnt/nfs/mfiers/STAR/hg19_star_db "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Map reads to the genome:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Jan 22 11:01:56 ..... started STAR run\r\n",
      "Jan 22 11:01:56 ..... loading genome\r\n",
      "Jan 22 11:01:56 ..... started mapping\r\n",
      "Jan 22 11:18:21 ..... finished successfully\r\n"
     ]
    }
   ],
   "source": [
    "STAR --genomeDir /mnt/nfs/mfiers/STAR/hg19_star_db \\\n",
    "     --genomeLoad LoadAndKeep \\\n",
    "     --runThreadN 2 \\\n",
    "     --readFilesIn SRR1197249_1.fastq \\\n",
    "     --outFileNamePrefix SRR1197249_1."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Jan 22 11:18:32 ..... started STAR run\r\n",
      "Jan 22 11:18:32 ..... loading genome\r\n",
      "Jan 22 11:18:32 ..... started mapping\r\n",
      "Jan 22 11:37:45 ..... finished successfully\r\n"
     ]
    }
   ],
   "source": [
    "STAR --genomeDir /mnt/nfs/mfiers/STAR/hg19_star_db \\\n",
    "     --genomeLoad LoadAndKeep \\\n",
    "     --runThreadN 2 \\\n",
    "     --readFilesIn SRR1197249_2.fastq \\\n",
    "     --outFileNamePrefix SRR1197249_2."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Jan 22 11:39:02 ..... started STAR run\r\n",
      "Jan 22 11:39:02 ..... loading genome\r\n",
      "Jan 22 11:39:02 ..... started mapping\r\n",
      "Jan 22 11:55:54 ..... finished successfully\r\n"
     ]
    }
   ],
   "source": [
    "STAR --genomeDir /mnt/nfs/mfiers/STAR/hg19_star_db \\\n",
    "     --genomeLoad LoadAndKeep \\\n",
    "     --runThreadN 2 \\\n",
    "     --readFilesIn SRR1198284_1.fastq \\\n",
    "     --outFileNamePrefix SRR1198284_1."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Jan 22 11:56:10 ..... started STAR run\r\n",
      "Jan 22 11:56:10 ..... loading genome\r\n",
      "Jan 22 11:56:10 ..... started mapping\r\n",
      "Jan 22 12:12:47 ..... finished successfully\r\n"
     ]
    }
   ],
   "source": [
    "STAR --genomeDir /mnt/nfs/mfiers/STAR/hg19_star_db \\\n",
    "     --genomeLoad LoadAndKeep \\\n",
    "     --runThreadN 2 \\\n",
    "     --readFilesIn SRR1198284_2.fastq \\\n",
    "     --outFileNamePrefix SRR1198284_2."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We generated sam-files."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## SAM FILE STRUCTURE\n",
    "\n",
    "A SAM file is a text file containing the alignment of all reads."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A SAM files starts with a header:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "@HD\tVN:1.4\r\n",
      "@SQ\tSN:chrM\tLN:16571\r\n",
      "@SQ\tSN:chr1\tLN:249250621\r\n",
      "@SQ\tSN:chr2\tLN:243199373\r\n",
      "@SQ\tSN:chr3\tLN:198022430\r\n",
      "@SQ\tSN:chr4\tLN:191154276\r\n",
      "@SQ\tSN:chr5\tLN:180915260\r\n",
      "@SQ\tSN:chr6\tLN:171115067\r\n",
      "@SQ\tSN:chr7\tLN:159138663\r\n",
      "@SQ\tSN:chr8\tLN:146364022\r\n",
      "@SQ\tSN:chr9\tLN:141213431\r\n",
      "@SQ\tSN:chr10\tLN:135534747\r\n",
      "@SQ\tSN:chr11\tLN:135006516\r\n",
      "@SQ\tSN:chr12\tLN:133851895\r\n",
      "@SQ\tSN:chr13\tLN:115169878\r\n",
      "@SQ\tSN:chr14\tLN:107349540\r\n",
      "@SQ\tSN:chr15\tLN:102531392\r\n",
      "@SQ\tSN:chr16\tLN:90354753\r\n",
      "@SQ\tSN:chr17\tLN:81195210\r\n",
      "@SQ\tSN:chr18\tLN:78077248\r\n",
      "@SQ\tSN:chr19\tLN:59128983\r\n",
      "@SQ\tSN:chr20\tLN:63025520\r\n",
      "@SQ\tSN:chr21\tLN:48129895\r\n",
      "@SQ\tSN:chr22\tLN:51304566\r\n",
      "@SQ\tSN:chrX\tLN:155270560\r\n",
      "@SQ\tSN:chrY\tLN:59373566\r\n",
      "@PG\tID:STAR\tPN:STAR\tVN:STAR_2.5.2b\tCL:STAR   --runThreadN 2   --genomeDir /mnt/nfs/mfiers/STAR/hg19_star_db   --genomeLoad LoadAndKeep   --readFilesIn SRR1197249_1.fastq      --outFileNamePrefix SRR1197249_1.\r\n",
      "@CO\tuser command line: STAR --genomeDir /mnt/nfs/mfiers/STAR/hg19_star_db --genomeLoad LoadAndKeep --runThreadN 2 --readFilesIn SRR1197249_1.fastq --outFileNamePrefix SRR1197249_1.\r\n"
     ]
    }
   ],
   "source": [
    "head -40 SRR1197249_1.Aligned.out.sam | grep '^@'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Followed by the alignments:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SRR1197249.1\t0\tchr18\t15813\t3\t84M16S\t*\t0\t0\tTTTCTGTCTTCATTCATGAGGGATAATAGTCTGCACTTTTATTTTCTTGTGATACTTTTGTCTGATTTGTTATCTGGGTAATACCTGTCTCTTATACACA\tCCCFFFFFHHHHHJJJJJIIJIFHJJJJJHHGHIJJJJJJJJJJJJJJJFIGIJJJJJJJIJJIIIJJJGIJIJJJJIIJHIJJHGHHHGFFFFFFEEEE\tNH:i:2\tHI:i:1\tAS:i:82\tnM:i:0\r\n",
      "SRR1197249.1\t256\tchr10\t61399\t3\t84M16S\t*\t0\t0\tTTTCTGTCTTCATTCATGAGGGATAATAGTCTGCACTTTTATTTTCTTGTGATACTTTTGTCTGATTTGTTATCTGGGTAATACCTGTCTCTTATACACA\tCCCFFFFFHHHHHJJJJJIIJIFHJJJJJHHGHIJJJJJJJJJJJJJJJFIGIJJJJJJJIJJIIIJJJGIJIJJJJIIJHIJJHGHHHGFFFFFFEEEE\tNH:i:2\tHI:i:2\tAS:i:82\tnM:i:0\r\n",
      "SRR1197249.2\t16\tchr10\t61682\t3\t100M\t*\t0\t0\tTTTTACACTTTGTGTATAGCAGGGAATCTGTGTCTAATTTGTAGTATTTCATGCTTCTAGGTTTTCATGGCAGTTGAGATGTAAGAATAACAATAATGTT\tDEDC@A@EEFFFFFFHHGHHIJJJJJIJJJIIGFIJIJJIJJJJJIJJJJJJIIJGGJJJJJJJJJJJJIJJIJJJJJJJJJJJJJJHHHHHFFFFFCCC\tNH:i:2\tHI:i:1\tAS:i:98\tnM:i:0\r\n",
      "SRR1197249.2\t272\tchr18\t16096\t3\t100M\t*\t0\t0\tTTTTACACTTTGTGTATAGCAGGGAATCTGTGTCTAATTTGTAGTATTTCATGCTTCTAGGTTTTCATGGCAGTTGAGATGTAAGAATAACAATAATGTT\tDEDC@A@EEFFFFFFHHGHHIJJJJJIJJJIIGFIJIJJIJJJJJIJJJJJJIIJGGJJJJJJJJJJJJIJJIJJJJJJJJJJJJJJHHHHHFFFFFCCC\tNH:i:2\tHI:i:2\tAS:i:98\tnM:i:0\r\n",
      "SRR1197249.3\t0\tchr10\t62793\t255\t100M\t*\t0\t0\tCTTTGACATATTAATGAAGTAATCACACTCTACACATAACTACTCCATATGGAATACTGGGGAGGAGGTGTTCCAAATAAAGAGACTGAGGATTTCTCAT\t?@@FFEDD<CBHFHIGIIA3A<IIIGGFGHG<CEEEEHIGGIFDH9B@F?BFHIGGHIIIIIDHGFA:-=)5;=CCCCE>DE:>3;>:@3?@?>ACAA@@\tNH:i:1\tHI:i:1\tAS:i:98\tnM:i:0\r\n",
      "SRR1197249.4\t16\tchr18\t17885\t3\t100M\t*\t0\t0\tTGCACCCTTCCCCACCTCCACTGGAACAGGCGCTGGTATCCATGGCTCAGAGACCCAAAGATGGTTCACATCACAGGGCTCTATGCAGACAACCCCCAGT\t:7<BB?@>89&@88<A>::(C>:C@@CBCBCCFCC@CC?>?EEG>GCGGHAC;HGFABABFFEBDA<B?>HEHGDGHF?CBGEFD@BFFF@?DFDDF@@@\tNH:i:2\tHI:i:1\tAS:i:96\tnM:i:1\r\n"
     ]
    }
   ],
   "source": [
    "head -34 SRR1197249_1.Aligned.out.sam | grep -v '^@'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To be able to quickly access the data we tend to sort the SAM file and convert it to a BAM file, which is a compressed (much smaller) version of a SAM file."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[bam_sort_core] merging from 22 files...\r\n",
      "[bam_sort_core] merging from 22 files...\r\n",
      "[bam_sort_core] merging from 22 files...\r\n",
      "[bam_sort_core] merging from 22 files...\r\n"
     ]
    }
   ],
   "source": [
    "samtools sort -o SRR1197249_1.bam SRR1197249_1.Aligned.out.sam\n",
    "samtools sort -o SRR1197249_2.bam SRR1197249_1.Aligned.out.sam\n",
    "samtools sort -o SRR1198284_1.bam SRR1197249_1.Aligned.out.sam\n",
    "samtools sort -o SRR1198284_2.bam SRR1197249_1.Aligned.out.sam"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-rw-r--r-- 1 r0386707 domain users 15209469733 jan 22 11:18 SRR1197249_1.Aligned.out.sam\r\n",
      "-rw-r--r-- 1 r0386707 domain users  3144335116 jan 22 12:27 SRR1197249_1.bam\r\n",
      "-rw-r--r-- 1 r0386707 domain users 15082779761 jan 22 11:37 SRR1197249_2.Aligned.out.sam\r\n",
      "-rw-r--r-- 1 r0386707 domain users  3144335116 jan 22 12:40 SRR1197249_2.bam\r\n",
      "-rw-r--r-- 1 r0386707 domain users 13807062391 jan 22 11:55 SRR1198284_1.Aligned.out.sam\r\n",
      "-rw-r--r-- 1 r0386707 domain users  3144335116 jan 22 12:54 SRR1198284_1.bam\r\n",
      "-rw-r--r-- 1 r0386707 domain users 13715547013 jan 22 12:12 SRR1198284_2.Aligned.out.sam\r\n",
      "-rw-r--r-- 1 r0386707 domain users  3144335116 jan 22 13:07 SRR1198284_2.bam\r\n"
     ]
    }
   ],
   "source": [
    "ls -l SRR1197249_1*[bs]am\n",
    "ls -l SRR1197249_2*[bs]am\n",
    "ls -l SRR1198284_1*[bs]am\n",
    "ls -l SRR1198284_2*[bs]am"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "An index is generated:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "samtools index SRR1197249_1.bam\n",
    "samtools index SRR1197249_2.bam\n",
    "samtools index SRR1198284_1.bam\n",
    "samtools index SRR1198284_2.bam"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-rw-r--r-- 1 r0386707 domain users 15209469733 jan 22 11:18 SRR1197249_1.Aligned.out.sam\r\n",
      "-rw-r--r-- 1 r0386707 domain users  3144335116 jan 22 12:27 SRR1197249_1.bam\r\n",
      "-rw-r--r-- 1 r0386707 domain users     2966728 jan 22 13:09 SRR1197249_1.bam.bai\r\n",
      "-rw-r--r-- 1 r0386707 domain users 15082779761 jan 22 11:37 SRR1197249_2.Aligned.out.sam\r\n",
      "-rw-r--r-- 1 r0386707 domain users  3144335116 jan 22 12:40 SRR1197249_2.bam\r\n",
      "-rw-r--r-- 1 r0386707 domain users     2966728 jan 22 13:11 SRR1197249_2.bam.bai\r\n",
      "-rw-r--r-- 1 r0386707 domain users 13807062391 jan 22 11:55 SRR1198284_1.Aligned.out.sam\r\n",
      "-rw-r--r-- 1 r0386707 domain users  3144335116 jan 22 12:54 SRR1198284_1.bam\r\n",
      "-rw-r--r-- 1 r0386707 domain users     2966728 jan 22 13:13 SRR1198284_1.bam.bai\r\n",
      "-rw-r--r-- 1 r0386707 domain users 13715547013 jan 22 12:12 SRR1198284_2.Aligned.out.sam\r\n",
      "-rw-r--r-- 1 r0386707 domain users  3144335116 jan 22 13:07 SRR1198284_2.bam\r\n",
      "-rw-r--r-- 1 r0386707 domain users     2966728 jan 22 13:13 SRR1198284_2.bam.bai\r\n"
     ]
    }
   ],
   "source": [
    "ls -l SRR1197249_1*[bs]a[mi]\n",
    "ls -l SRR1197249_2*[bs]a[mi]\n",
    "ls -l SRR1198284_1*[bs]a[mi]\n",
    "ls -l SRR1198284_2*[bs]a[mi]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## INSPECT THE BAM FILE"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-rw-r--r-- 1 r0386707 domain users 3144335116 jan 22 12:27 SRR1197249_1.bam\r\n",
      "-rw-r--r-- 1 r0386707 domain users    2966728 jan 22 13:09 SRR1197249_1.bam.bai\r\n",
      "-rw-r--r-- 1 r0386707 domain users 3144335116 jan 22 12:40 SRR1197249_2.bam\r\n",
      "-rw-r--r-- 1 r0386707 domain users    2966728 jan 22 13:11 SRR1197249_2.bam.bai\r\n",
      "-rw-r--r-- 1 r0386707 domain users 3144335116 jan 22 12:54 SRR1198284_1.bam\r\n",
      "-rw-r--r-- 1 r0386707 domain users    2966728 jan 22 13:13 SRR1198284_1.bam.bai\r\n",
      "-rw-r--r-- 1 r0386707 domain users 3144335116 jan 22 13:07 SRR1198284_2.bam\r\n",
      "-rw-r--r-- 1 r0386707 domain users    2966728 jan 22 13:13 SRR1198284_2.bam.bai\r\n"
     ]
    }
   ],
   "source": [
    "ls -l *.ba[mi]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Inspection of 1 of the 4 bam files:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SRR1197249.46676328\t0\tchrM\t1\t255\t3S97M\t*\t0\t0\tATGGATCACAGGTCTATCACCCTATTAACCACTCACGGGAGCTCTCCATGCATTTGGTATTTTCGTCTGGGGGGTGTGCACGCGATAGCATTGCGAGACG\t@@CFFFFFHHFFHIJIEIEFGEIJJJJGIIJIGGCHGDFGGBGHIIGJJHI<BAHIJ@@FHEIJGEHHEFDDDD@BBDDDCDDB@@<@DDCCCCD@@BDD\tNH:i:1\tHI:i:1\tAS:i:95\tnM:i:0\r\n",
      "SRR1197249.46676338\t0\tchrM\t1\t3\t11S89M\t*\t0\t0\tACATCACGATGGATCACAGGTCTATCACCCTATTAACCACTCACGGGAGCTCTCCATGCATTTGGTATTTTCGTCTGGGGGGTGTGCACGCGATAGCATT\t@BCDFFFFHHHHGJJJJJJIHHGIIJJHJJJIIJJJJJIJIJJIJJJJGGIGIJJJJJGGGHHHG?EEHFDFFBDDDCCBBD<BDDDEDDDDDDDDDDED\tNH:i:2\tHI:i:2\tAS:i:87\tnM:i:0\r\n",
      "SRR1197249.46676340\t0\tchrM\t1\t255\t10S90M\t*\t0\t0\tCATCACGATGGATCACAGGTCTATCACCCTATTAACCACTCACGGGAGCTCTCCATGCATTTGGTATTTTCGTCTGGGGGGTGTGCACGCGATAGCATTG\t??@DDDBBFHFHHJJIGFBHHIJGFHHIJHFHIGJGGIGIFGHI?DFGIIIGG@EG@@=EHIGH=CHHH@CD@=?ACDBBB9@<BCDDDDDDBDDACDDD\tNH:i:1\tHI:i:1\tAS:i:88\tnM:i:0\r\n",
      "samtools view: writing to standard output failed: Broken pipe\r\n",
      "samtools view: error closing standard output: -1\r\n"
     ]
    }
   ],
   "source": [
    "samtools view SRR1197249_1.bam | head -3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "See how many reads map to each chromosome:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "chrM\t16571\t32586\t0\r\n",
      "chr1\t249250621\t5824066\t0\r\n",
      "chr2\t243199373\t3491151\t0\r\n",
      "chr3\t198022430\t2488961\t0\r\n",
      "chr4\t191154276\t1970613\t0\r\n",
      "chr5\t180915260\t2757005\t0\r\n",
      "chr6\t171115067\t2192058\t0\r\n",
      "chr7\t159138663\t3471611\t0\r\n",
      "chr8\t146364022\t3165671\t0\r\n",
      "chr9\t141213431\t2534045\t0\r\n",
      "chr10\t135534747\t1913080\t0\r\n",
      "chr11\t135006516\t3066782\t0\r\n",
      "chr12\t133851895\t2120842\t0\r\n",
      "chr13\t115169878\t920180\t0\r\n",
      "chr14\t107349540\t1291553\t0\r\n",
      "chr15\t102531392\t2726577\t0\r\n",
      "chr16\t90354753\t2618082\t0\r\n",
      "chr17\t81195210\t2648329\t0\r\n",
      "chr18\t78077248\t720313\t0\r\n",
      "chr19\t59128983\t2151071\t0\r\n",
      "chr20\t63025520\t1428412\t0\r\n",
      "chr21\t48129895\t565221\t0\r\n",
      "chr22\t51304566\t1291502\t0\r\n",
      "chrX\t155270560\t2191272\t0\r\n",
      "chrY\t59373566\t143632\t0\r\n",
      "*\t0\t0\t0\r\n",
      "chrM\t16571\t32586\t0\r\n",
      "chr1\t249250621\t5824066\t0\r\n",
      "chr2\t243199373\t3491151\t0\r\n",
      "chr3\t198022430\t2488961\t0\r\n",
      "chr4\t191154276\t1970613\t0\r\n",
      "chr5\t180915260\t2757005\t0\r\n",
      "chr6\t171115067\t2192058\t0\r\n",
      "chr7\t159138663\t3471611\t0\r\n",
      "chr8\t146364022\t3165671\t0\r\n",
      "chr9\t141213431\t2534045\t0\r\n",
      "chr10\t135534747\t1913080\t0\r\n",
      "chr11\t135006516\t3066782\t0\r\n",
      "chr12\t133851895\t2120842\t0\r\n",
      "chr13\t115169878\t920180\t0\r\n",
      "chr14\t107349540\t1291553\t0\r\n",
      "chr15\t102531392\t2726577\t0\r\n",
      "chr16\t90354753\t2618082\t0\r\n",
      "chr17\t81195210\t2648329\t0\r\n",
      "chr18\t78077248\t720313\t0\r\n",
      "chr19\t59128983\t2151071\t0\r\n",
      "chr20\t63025520\t1428412\t0\r\n",
      "chr21\t48129895\t565221\t0\r\n",
      "chr22\t51304566\t1291502\t0\r\n",
      "chrX\t155270560\t2191272\t0\r\n",
      "chrY\t59373566\t143632\t0\r\n",
      "*\t0\t0\t0\r\n",
      "chrM\t16571\t32586\t0\r\n",
      "chr1\t249250621\t5824066\t0\r\n",
      "chr2\t243199373\t3491151\t0\r\n",
      "chr3\t198022430\t2488961\t0\r\n",
      "chr4\t191154276\t1970613\t0\r\n",
      "chr5\t180915260\t2757005\t0\r\n",
      "chr6\t171115067\t2192058\t0\r\n",
      "chr7\t159138663\t3471611\t0\r\n",
      "chr8\t146364022\t3165671\t0\r\n",
      "chr9\t141213431\t2534045\t0\r\n",
      "chr10\t135534747\t1913080\t0\r\n",
      "chr11\t135006516\t3066782\t0\r\n",
      "chr12\t133851895\t2120842\t0\r\n",
      "chr13\t115169878\t920180\t0\r\n",
      "chr14\t107349540\t1291553\t0\r\n",
      "chr15\t102531392\t2726577\t0\r\n",
      "chr16\t90354753\t2618082\t0\r\n",
      "chr17\t81195210\t2648329\t0\r\n",
      "chr18\t78077248\t720313\t0\r\n",
      "chr19\t59128983\t2151071\t0\r\n",
      "chr20\t63025520\t1428412\t0\r\n",
      "chr21\t48129895\t565221\t0\r\n",
      "chr22\t51304566\t1291502\t0\r\n",
      "chrX\t155270560\t2191272\t0\r\n",
      "chrY\t59373566\t143632\t0\r\n",
      "*\t0\t0\t0\r\n",
      "chrM\t16571\t32586\t0\r\n",
      "chr1\t249250621\t5824066\t0\r\n",
      "chr2\t243199373\t3491151\t0\r\n",
      "chr3\t198022430\t2488961\t0\r\n",
      "chr4\t191154276\t1970613\t0\r\n",
      "chr5\t180915260\t2757005\t0\r\n",
      "chr6\t171115067\t2192058\t0\r\n",
      "chr7\t159138663\t3471611\t0\r\n",
      "chr8\t146364022\t3165671\t0\r\n",
      "chr9\t141213431\t2534045\t0\r\n",
      "chr10\t135534747\t1913080\t0\r\n",
      "chr11\t135006516\t3066782\t0\r\n",
      "chr12\t133851895\t2120842\t0\r\n",
      "chr13\t115169878\t920180\t0\r\n",
      "chr14\t107349540\t1291553\t0\r\n",
      "chr15\t102531392\t2726577\t0\r\n",
      "chr16\t90354753\t2618082\t0\r\n",
      "chr17\t81195210\t2648329\t0\r\n",
      "chr18\t78077248\t720313\t0\r\n",
      "chr19\t59128983\t2151071\t0\r\n",
      "chr20\t63025520\t1428412\t0\r\n",
      "chr21\t48129895\t565221\t0\r\n",
      "chr22\t51304566\t1291502\t0\r\n",
      "chrX\t155270560\t2191272\t0\r\n",
      "chrY\t59373566\t143632\t0\r\n",
      "*\t0\t0\t0\r\n"
     ]
    }
   ],
   "source": [
    "samtools idxstats SRR1197249_1.bam\n",
    "samtools idxstats SRR1197249_2.bam\n",
    "samtools idxstats SRR1198284_1.bam\n",
    "samtools idxstats SRR1198284_2.bam"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In both datasets there are reads mapped to every chromosome."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Running `samtools flagstat` tells us what the distribution of mapping flags (column 2 in the sam/bam file) is:\n",
    "\n",
    "    0x0001\tp\tthe read is paired in sequencing\n",
    "    0x0002\tP\tthe read is mapped in a proper pair\n",
    "    0x0004\tu\tthe query sequence itself is unmapped\n",
    "    0x0008\tU\tthe mate is unmapped\n",
    "    0x0010\tr\tstrand of the query (1 for reverse)\n",
    "    0x0020\tR\tstrand of the mate\n",
    "    0x0040\t1\tthe read is the first read in a pair\n",
    "    0x0080\t2\tthe read is the second read in a pair\n",
    "    0x0100\ts\tthe alignment is not primary\n",
    "    0x0200\tf\tthe read fails platform/vendor quality checks\n",
    "    0x0400\td\tthe read is either a PCR or an optical duplicate\n",
    "    0x0800\tS\tthe alignment is supplementary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "53724615 + 0 in total (QC-passed reads + QC-failed reads)\r\n",
      "10676732 + 0 secondary\r\n",
      "0 + 0 supplementary\r\n",
      "0 + 0 duplicates\r\n",
      "53724615 + 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",
      "53724615 + 0 in total (QC-passed reads + QC-failed reads)\r\n",
      "10676732 + 0 secondary\r\n",
      "0 + 0 supplementary\r\n",
      "0 + 0 duplicates\r\n",
      "53724615 + 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",
      "53724615 + 0 in total (QC-passed reads + QC-failed reads)\r\n",
      "10676732 + 0 secondary\r\n",
      "0 + 0 supplementary\r\n",
      "0 + 0 duplicates\r\n",
      "53724615 + 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",
      "53724615 + 0 in total (QC-passed reads + QC-failed reads)\r\n",
      "10676732 + 0 secondary\r\n",
      "0 + 0 supplementary\r\n",
      "0 + 0 duplicates\r\n",
      "53724615 + 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 flagstat SRR1197249_1.bam\n",
    "samtools flagstat SRR1197249_2.bam\n",
    "samtools flagstat SRR1198284_1.bam\n",
    "samtools flagstat SRR1198284_2.bam"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## READS TO GENE COUNTS\n",
    "\n",
    "Make a symbolic link to the human annotation:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "lrwxrwxrwx 1 r0386707 domain users 56 jan 22 13:39 gencode.v19.nopseudo.plus.sort.gtf -> /mnt/nfs/data/RNA-seq/gencode.v19.nopseudo.plus.sort.gtf\r\n"
     ]
    }
   ],
   "source": [
    "ln -sf /mnt/nfs/data/RNA-seq/gencode.v19.nopseudo.plus.sort.gtf ~/data/RNAseq/\n",
    "ls -l *gtf"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The amount of reads per gene are counted:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": false
   },
   "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 SRR1197249_1.bam\u001b[0m \u001b[0m                              ||\r\n",
      "||  \u001b[0m                                                                          ||\r\n",
      "||             Output file : \u001b[36mSRR1197249_1.counts\u001b[0m \u001b[0m                             ||\r\n",
      "||             Annotations : \u001b[36mgencode.v19.nopseudo.plus.sort.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 gencode.v19.nopseudo.plus.sort.gtf \u001b[0m... \u001b[0m               ||\r\n",
      "||    Features : \u001b[36m1156385\u001b[0m \u001b[0m                                                     ||\r\n",
      "||    Meta-features : \u001b[36m41863\u001b[0m \u001b[0m                                                  ||\r\n",
      "||    Chromosomes/contigs : \u001b[36m25\u001b[0m \u001b[0m                                               ||\r\n",
      "||  \u001b[0m                                                                          ||\r\n",
      "|| Process BAM file SRR1197249_1.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[36m53724615\u001b[0m \u001b[0m                                                 ||\r\n",
      "||    Successfully assigned reads : \u001b[36m23429445 (43.6%)\u001b[0m \u001b[0m                         ||\r\n",
      "||    Running time : \u001b[36m1.05 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",
      "\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 SRR1197249_2.bam\u001b[0m \u001b[0m                              ||\r\n",
      "||  \u001b[0m                                                                          ||\r\n",
      "||             Output file : \u001b[36mSRR1197249_2.counts\u001b[0m \u001b[0m                             ||\r\n",
      "||             Annotations : \u001b[36mgencode.v19.nopseudo.plus.sort.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 gencode.v19.nopseudo.plus.sort.gtf \u001b[0m... \u001b[0m               ||\r\n",
      "||    Features : \u001b[36m1156385\u001b[0m \u001b[0m                                                     ||\r\n",
      "||    Meta-features : \u001b[36m41863\u001b[0m \u001b[0m                                                  ||\r\n",
      "||    Chromosomes/contigs : \u001b[36m25\u001b[0m \u001b[0m                                               ||\r\n",
      "||  \u001b[0m                                                                          ||\r\n",
      "|| Process BAM file SRR1197249_2.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[36m53724615\u001b[0m \u001b[0m                                                 ||\r\n",
      "||    Successfully assigned reads : \u001b[36m23429445 (43.6%)\u001b[0m \u001b[0m                         ||\r\n",
      "||    Running time : \u001b[36m1.04 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",
      "\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 SRR1198284_1.bam\u001b[0m \u001b[0m                              ||\r\n",
      "||  \u001b[0m                                                                          ||\r\n",
      "||             Output file : \u001b[36mSRR1198284_1.counts\u001b[0m \u001b[0m                             ||\r\n",
      "||             Annotations : \u001b[36mgencode.v19.nopseudo.plus.sort.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 gencode.v19.nopseudo.plus.sort.gtf \u001b[0m... \u001b[0m               ||\r\n",
      "||    Features : \u001b[36m1156385\u001b[0m \u001b[0m                                                     ||\r\n",
      "||    Meta-features : \u001b[36m41863\u001b[0m \u001b[0m                                                  ||\r\n",
      "||    Chromosomes/contigs : \u001b[36m25\u001b[0m \u001b[0m                                               ||\r\n",
      "||  \u001b[0m                                                                          ||\r\n",
      "|| Process BAM file SRR1198284_1.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[36m53724615\u001b[0m \u001b[0m                                                 ||\r\n",
      "||    Successfully assigned reads : \u001b[36m23429445 (43.6%)\u001b[0m \u001b[0m                         ||\r\n",
      "||    Running time : \u001b[36m1.04 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",
      "\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 SRR1198284_2.bam\u001b[0m \u001b[0m                              ||\r\n",
      "||  \u001b[0m                                                                          ||\r\n",
      "||             Output file : \u001b[36mSRR1198284_2.counts\u001b[0m \u001b[0m                             ||\r\n",
      "||             Annotations : \u001b[36mgencode.v19.nopseudo.plus.sort.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 gencode.v19.nopseudo.plus.sort.gtf \u001b[0m... \u001b[0m               ||\r\n",
      "||    Features : \u001b[36m1156385\u001b[0m \u001b[0m                                                     ||\r\n",
      "||    Meta-features : \u001b[36m41863\u001b[0m \u001b[0m                                                  ||\r\n",
      "||    Chromosomes/contigs : \u001b[36m25\u001b[0m \u001b[0m                                               ||\r\n",
      "||  \u001b[0m                                                                          ||\r\n",
      "|| Process BAM file SRR1198284_2.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[36m53724615\u001b[0m \u001b[0m                                                 ||\r\n",
      "||    Successfully assigned reads : \u001b[36m23429445 (43.6%)\u001b[0m \u001b[0m                         ||\r\n",
      "||    Running time : \u001b[36m1.04 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 gencode.v19.nopseudo.plus.sort.gtf -o SRR1197249_1.counts SRR1197249_1.bam\n",
    "featureCounts -Q 10 -g gene_name -a gencode.v19.nopseudo.plus.sort.gtf -o SRR1197249_2.counts SRR1197249_2.bam\n",
    "featureCounts -Q 10 -g gene_name -a gencode.v19.nopseudo.plus.sort.gtf -o SRR1198284_1.counts SRR1198284_1.bam\n",
    "featureCounts -Q 10 -g gene_name -a gencode.v19.nopseudo.plus.sort.gtf -o SRR1198284_2.counts SRR1198284_2.bam"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-rw-r--r-- 1 r0386707 domain users 8371411 jan 22 13:46 SRR1197249_1.counts\r\n",
      "-rw-r--r-- 1 r0386707 domain users     305 jan 22 13:46 SRR1197249_1.counts.summary\r\n",
      "-rw-r--r-- 1 r0386707 domain users 8371411 jan 22 13:48 SRR1197249_2.counts\r\n",
      "-rw-r--r-- 1 r0386707 domain users     305 jan 22 13:48 SRR1197249_2.counts.summary\r\n",
      "-rw-r--r-- 1 r0386707 domain users 8371411 jan 22 13:49 SRR1198284_1.counts\r\n",
      "-rw-r--r-- 1 r0386707 domain users     305 jan 22 13:49 SRR1198284_1.counts.summary\r\n",
      "-rw-r--r-- 1 r0386707 domain users 8371411 jan 22 13:50 SRR1198284_2.counts\r\n",
      "-rw-r--r-- 1 r0386707 domain users     305 jan 22 13:50 SRR1198284_2.counts.summary\r\n"
     ]
    }
   ],
   "source": [
    "ls -l *.counts*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The files are inspected:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Status\tSRR1197249_1.bam\r\n",
      "Assigned\t23429445\r\n",
      "Unassigned_Ambiguity\t1083181\r\n",
      "Unassigned_MultiMapping\t0\r\n",
      "Unassigned_NoFeatures\t14353748\r\n",
      "Unassigned_Unmapped\t0\r\n",
      "Unassigned_MappingQuality\t14858241\r\n",
      "Unassigned_FragmentLength\t0\r\n",
      "Unassigned_Chimera\t0\r\n",
      "Unassigned_Secondary\t0\r\n",
      "Unassigned_Nonjunction\t0\r\n",
      "Unassigned_Duplicate\t0\r\n",
      "Status                     SRR1197249_2.bam\r\n",
      "Assigned                   23429445\r\n",
      "Unassigned_Ambiguity       1083181\r\n",
      "Unassigned_MultiMapping    0\r\n",
      "Unassigned_NoFeatures      14353748\r\n",
      "Unassigned_Unmapped        0\r\n",
      "Unassigned_MappingQuality  14858241\r\n",
      "Unassigned_FragmentLength  0\r\n",
      "Unassigned_Chimera         0\r\n",
      "Unassigned_Secondary       0\r\n",
      "Unassigned_Nonjunction     0\r\n",
      "Unassigned_Duplicate       0\r\n",
      "Status                     SRR1198284_1.bam\r\n",
      "Assigned                   23429445\r\n",
      "Unassigned_Ambiguity       1083181\r\n",
      "Unassigned_MultiMapping    0\r\n",
      "Unassigned_NoFeatures      14353748\r\n",
      "Unassigned_Unmapped        0\r\n",
      "Unassigned_MappingQuality  14858241\r\n",
      "Unassigned_FragmentLength  0\r\n",
      "Unassigned_Chimera         0\r\n",
      "Unassigned_Secondary       0\r\n",
      "Unassigned_Nonjunction     0\r\n",
      "Unassigned_Duplicate       0\r\n",
      "Status                     SRR1198284_2.bam\r\n",
      "Assigned                   23429445\r\n",
      "Unassigned_Ambiguity       1083181\r\n",
      "Unassigned_MultiMapping    0\r\n",
      "Unassigned_NoFeatures      14353748\r\n",
      "Unassigned_Unmapped        0\r\n",
      "Unassigned_MappingQuality  14858241\r\n",
      "Unassigned_FragmentLength  0\r\n",
      "Unassigned_Chimera         0\r\n",
      "Unassigned_Secondary       0\r\n",
      "Unassigned_Nonjunction     0\r\n",
      "Unassigned_Duplicate       0\r\n"
     ]
    }
   ],
   "source": [
    "head -20 SRR1197249_1.counts.summary \n",
    "head -20 SRR1197249_2.counts.summary\n",
    "head -20 SRR1198284_1.counts.summary\n",
    "head -20 SRR1198284_2.counts.summary"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The majority of the reads are assigned to a gene."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "# Program:featureCounts v1.5.0-p1; Command:\"featureCounts\" \"-Q\" \"10\" \"-g\" \"gene_name\" \"-a\" \"gencode.v19.nopseudo.plus.sort.gtf\" \"-o\" \"SRR1197249_1.counts\" \"SRR1197249_1.bam\" \r\n",
      "Geneid\tChr\tStart\tEnd\tStrand\tLength\tSRR1197249_1.bam\r\n",
      "MIR1302-11\tchr1;chr1;chr1\t29554;30267;30976\t30039;30667;31109\t+;+;+\t1021\t0\r\n",
      "FAM138A\tchr1;chr1;chr1\t34554;35245;35721\t35174;35481;36081\t-;-;-\t1219\t0\r\n",
      "OR4F5\tchr1\t69091\t70008\t+\t918\t0\r\n",
      "RP11-34P13.7\tchr1;chr1;chr1;chr1;chr1;chr1;chr1\t89295;92091;110953;112700;120721;129055;133374\t91629;92240;111357;112804;120932;129223;133566\t-;-;-;-;-;-;-\t3569\t82\r\n",
      "RP11-34P13.8\tchr1;chr1\t89551;90287\t90050;91105\t-;-\t1319\t0\r\n",
      "AL627309.1\tchr1;chr1\t134901;137621\t135802;139379\t-;-\t2661\t929\r\n",
      "RP11-34P13.14\tchr1;chr1\t139790;140075\t139847;140339\t-;-\t323\t2\r\n",
      "RP11-34P13.13\tchr1;chr1;chr1;chr1;chr1;chr1;chr1;chr1;chr1;chr1\t141474;146386;155767;164263;165884;168100;168610;169049;172557;173753\t143011;149707;155831;164791;165942;168165;168767;169264;172688;173862\t-;-;-;-;-;-;-;-;-;-\t6195\t3\r\n"
     ]
    }
   ],
   "source": [
    "head SRR1197249_1.counts"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There are a number of columns on the gene structure, and a number with the actual counts. We'll separate these:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "cut -f-6 SRR1197249_1.counts > SRR1197249_1.genedata.tsv\n",
    "cut -f-6 SRR1197249_2.counts > SRR1197249_2.genedata.tsv\n",
    "cut -f-6 SRR1198284_1.counts > SRR1198284_1.genedata.tsv\n",
    "cut -f-6 SRR1198284_2.counts > SRR1198284_2.genedata.tsv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "# Program:featureCounts v1.5.0-p1; Command:\"featureCounts\" \"-Q\" \"10\" \"-g\" \"gene_name\" \"-a\" \"gencode.v19.nopseudo.plus.sort.gtf\" \"-o\" \"SRR1197249_1.counts\" \"SRR1197249_1.bam\" \r\n",
      "Geneid\tChr\tStart\tEnd\tStrand\tLength\r\n",
      "MIR1302-11\tchr1;chr1;chr1\t29554;30267;30976\t30039;30667;31109\t+;+;+\t1021\r\n",
      "FAM138A\tchr1;chr1;chr1\t34554;35245;35721\t35174;35481;36081\t-;-;-\t1219\r\n",
      "OR4F5\tchr1\t69091\t70008\t+\t918\r\n",
      "RP11-34P13.7\tchr1;chr1;chr1;chr1;chr1;chr1;chr1\t89295;92091;110953;112700;120721;129055;133374\t91629;92240;111357;112804;120932;129223;133566\t-;-;-;-;-;-;-\t3569\r\n",
      "RP11-34P13.8\tchr1;chr1\t89551;90287\t90050;91105\t-;-\t1319\r\n",
      "AL627309.1\tchr1;chr1\t134901;137621\t135802;139379\t-;-\t2661\r\n",
      "RP11-34P13.14\tchr1;chr1\t139790;140075\t139847;140339\t-;-\t323\r\n",
      "RP11-34P13.13\tchr1;chr1;chr1;chr1;chr1;chr1;chr1;chr1;chr1;chr1\t141474;146386;155767;164263;165884;168100;168610;169049;172557;173753\t143011;149707;155831;164791;165942;168165;168767;169264;172688;173862\t-;-;-;-;-;-;-;-;-;-\t6195\r\n"
     ]
    }
   ],
   "source": [
    "head SRR1197249_1.genedata.tsv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "cut -f1,7- SRR1197249_1.counts | grep -v '^#' > SRR1197249_1.gene.counts\n",
    "cut -f1,7- SRR1197249_2.counts | grep -v '^#' > SRR1197249_2.gene.counts\n",
    "cut -f1,7- SRR1198284_1.counts | grep -v '^#' > SRR1198284_1.gene.counts\n",
    "cut -f1,7- SRR1198284_2.counts | grep -v '^#' > SRR1198284_2.gene.counts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Geneid\tSRR1197249_1.bam\r\n",
      "MIR1302-11\t0\r\n",
      "FAM138A\t0\r\n",
      "OR4F5\t0\r\n",
      "RP11-34P13.7\t82\r\n",
      "RP11-34P13.8\t0\r\n",
      "AL627309.1\t929\r\n",
      "RP11-34P13.14\t2\r\n",
      "RP11-34P13.13\t3\r\n",
      "RNU6-1100P\t0\r\n"
     ]
    }
   ],
   "source": [
    "head SRR1197249_1.gene.counts"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We have now a list with the counts per gene. With the 'grep' function we can check the counts for an individual gene:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ZEB1-AS1\t364\r\n",
      "ZEB1\t1047\r\n",
      "ZEB1-AS1\t364\r\n",
      "ZEB1\t1047\r\n"
     ]
    }
   ],
   "source": [
    "grep ZEB1 SRR1197249_1.gene.counts\n",
    "grep ZEB1 SRR1198284_1.gene.counts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CDKN1A\t960\r\n"
     ]
    }
   ],
   "source": [
    "grep CDKN1A SRR1197249_1.gene.counts"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## FUNCTIONAL ANALYSIS"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Functional analysis with GORILLA:\n",
    "\n",
    "GOrilla is a tool for identifying and visualizing enriched GO terms in ranked lists of genes. Our top genes are important in many biological processes.\n",
    "\n",
    "<img src=\"/user/r0386707/files/GO2.png\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Functional analysis with PANTHER:\n",
    "\n",
    "In bioinformatics the PANTHER (Protein ANalysis THrough Evolutionary Relationships) classification system is a large curated biological database of gene/protein families and their functionally related subfamilies that can be used to classify and identify the function of gene products. The picture shows only a small part of the biological processes that are statistically significant. Again, many biological processes are important.\n",
    "\n",
    "<img width=\"100%\" src=\"/user/r0386707/files/Schermafbeelding%202017-01-22%20om%2016.43.52.png\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## CONCLUSION\n",
    "\n",
    "2 different datasets are chosen,the HMEC hTERT immortalized cell line ectopically expressing HRasG12V and ZEB1, 90 days after transduction (SRR1198284) and the HMEC hTERT immortalized cell line, 90 days after transduction (SRR1197249). These datasets were chosen because we tought the first one was infected with RAS and ZEB1 and the second one was not so this would be our control condition. Data were downloaded and converted and fastqc was used for quality control. The per base sequence quality and the sequence duplication level were OK for all 4 fastq files and there were no overrepresented sequences. We continued without pre-processing our data since the quality was already good. The reads were mapped to the genome and sam files were converted to compressed bam files as well as indexes were generated. The amount of reads per gene was counted and a count file was generated. With the grep function we could now check the counts per gene of interest. We looked for many genes but did not find a difference between a single gene in the 2 different datasets. What also should be noticed is that even ZEB1 did not have more counts in the dataset of the HMEC hTERT immortalized cell line ectopically expressing HRasG12V and ZEB1, which was not what we expected to see. This showed a discrepancy since we wanted to compare a cell line that was not infected with RAS or ZEB1 with a cell line infected with both RAS and ZEB1.\n",
    "\n",
    "Functional analysis showed that our top genes are important in many biological processes."
   ]
  }
 ],
 "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
}
