{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Using `bash` in bioinformatics\n", "\n", "The Unix commands shown in this notebook are quite intimidating at first. There's a lot of material becaue I try to cover all the stuff you willl need to construct a typicla bioinformatics pipeline. \n", "\n", "Try to understand the expressions and play aound to see what varying them will do. You do not need to memorize the commands now - you can always come back to this notebook for reference. You will also get more practice when you do the Bioiinformatics sessions to construct Unix pipelines for processing your RNA-seq data sets." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Hack to handle broken pipes." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "cleanup () { \n", " :\n", "}\n", "\n", "trap \"cleanup\" SIGPIPE" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Treat unset variables as an error, and immediately exit." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "set -u" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 0.1. Files, directories and working with text\n", "\n", "Quickly reveiw what the following navigation and file/directory manipulation commands do:\n", "\n", "```bash\n", ">, >>, <, |, ls, cd, pwd, cp, mv, rm, mkdir, rmdir\n", "```\n", "\n", "This session dives quite deep into Unix shell scripting and invovles quite a bit of text processing. We review some basic commands for working with text in Unix." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### This is the file we will warm up with\n", "\n", "We create it using a HEREDOC that allows us to construct multi-line files and redirrect it to the file `junk.txt`" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "cat > junk.txt <> $HOME/.bashrc\n", "\n", "or manually edit the PATH variable assignment in your .bash_profile file.\n", "\n" ] } ], "source": [ "### setup efetch command\n", "cd ~\n", "#/bin/bash\n", "perl -MNet::FTP -e \\\n", " '$ftp = new Net::FTP(\"ftp.ncbi.nlm.nih.gov\", Passive => 1);\n", " $ftp->login; $ftp->binary;\n", " $ftp->get(\"/entrez/entrezdirect/edirect.tar.gz\");'\n", "gunzip -c edirect.tar.gz | tar xf -\n", "rm edirect.tar.gz\n", "#builtin exit\n", "export PATH=${PATH}:$HOME/edirect >& /dev/null || setenv PATH \"${PATH}:$HOME/edirect\"\n", "./edirect/setup.sh" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "mkdir -p seqs\n", "efetch -db=nuccore -format=fasta -id=CM000046.1 > seqs/CM000046.1.fa\n", "efetch -db=nuccore -format=fasta -id=CM000047.1 > seqs/CM000047.1.fa" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CM000046.1.fa \u001b[0m\u001b[01;31mcneo040623.scaffs.fa.gz\u001b[0m\n", "CM000047.1.fa \u001b[01;31mCryptococcus_neoformans.ASM9104v1.43.gtf.gz\u001b[0m\n", "CM000047.1.fa.bak\n" ] } ], "source": [ "ls seqs" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ ">CM000046.1 Cryptococcus neoformans var. neoformans B-3501A chromosome 7, whole genome shotgun sequence\n", "TGGTCTTATGAGGAAGAGGAGTTTGGATTATTTTTTCTTTTCTTTAAAAAGTTGTTTATTTAAGTAGTTT\n", "CTTTAATTCGGGTAACACACACGACAACCCAATAAATTAAACAACGAAAAAATGCAACCTCTATAACCCC\n", "CTAACCCTTTTATTCGGGTAACACACACGACAACCCAATAAATTAAACAACGAAAAATGCAACCTCTATA\n", "ACCCCCGAAAAGGATTGGTGTCGAGTTAGTAAAGGCGAGGGCTAGGACGCTGGTCTTGTGAGGAAGAGGA\n", "GGTTGGATTATTTTTTCTTTTCTTTAATAAGTTGTTTATTTAAGTAGTTTCTTTTATTCGGGTAACACAC\n", "ACGACAACCCAATAAATTAAACAACGAAAAATGCAACCTCTATAACCCCCATCCAAGGTGCCACGGTTGA\n", "GATTGAGCCCCACACACTGTCGGGACAAAGGAGAAACGACCTTCGGGTCAGAGGTTCCAGCGCTCTGGCC\n", "TTCACTGACTACGACCTGAAGGTTTACTCCCTCGGGGACCGAGACGCGAGGAGCACAGCCACCCCCAGCA\n", "CCCCCAACAGCAAACTGGCCGACTTCTGCTTGGACCGGTGCGTGAACTGGCTCGACAAGGTGGGTCAGGT\n" ] } ], "source": [ "head seqs/CM000046.1.fa" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ ">CM000047.1 Cryptococcus neoformans var. neoformans B-3501A chromosome 8, whole genome shotgun sequence\n", "ATGGCCTTCCTGACTACGACCTGAGGTTTACTCCCTCGGCGACCGAGACGCGAGGAGCACAGCCACCCCA\n", "AGCACCCCCAACAGCAAGCTGGCCGAATTCTGCTTGGACCGGTGCGTGAACTGGCTCGACAAGGTGGGTC\n", "AGGTCGTCTCGAAGAACGCTCCGAAAGTCACTGGTGGGGTCTTTAAACCGATCATCCTTTCCACTGGTGG\n", "CCTGATGAGCAGGAGCACAGCAGACGAATGGAAGGAGTGGAGGGAGGCGATGCCGGTGGGGGGGTTCGAG\n", "AAAATGGAGAAACGGATTGGTGTCGAGTTAGTAAAGGCGAGGGCTAGGACGCTGGTCTTGTGAGGAAGAG\n", "GAGGTTGGATTATTTTTTCTTTTCTTTAAAAAGTTGTTTATTTAAGTAGTTTCTTTAATTCGGGCAACCC\n", "ACACGACAACCCAATAAATTAAACAACGAAAAATGCAACCTCTATAACCCTCAATAGCTCTGGCTGGTAC\n", "AGATGGGCAGAAACTCACCTTGCTCCCACGGCATACATCCCTCCCAATAAGTATCTGCAGATGGCGAATA\n", "AACCCCATGCTCTGCGCAAATATATTGAAGGCTGACGACTTTGATGTTATGATCCACCCCATAAACGCTC\n" ] } ], "source": [ "head seqs/CM000047.1.fa" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 0.3 Compressing ard archiving files" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Using `zip`, `gzip`, `bzip2`, `bgzip`\n", "\n", "In Bioinformatics the most common formats are `gzip` and `bgzip`." ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CM000046.1.fa \u001b[0m\u001b[01;31mcneo040623.scaffs.fa.gz\u001b[0m\n", "CM000047.1.fa \u001b[01;31mCryptococcus_neoformans.ASM9104v1.43.gtf.gz\u001b[0m\n", "CM000047.1.fa.bak\n" ] } ], "source": [ "cd ~/seqs\n", "ls" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Compressing with `gzip`" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[0m\u001b[01;31mCM000046.1.fa.gz\u001b[0m \u001b[01;31mcneo040623.scaffs.fa.gz\u001b[0m\n", "CM000047.1.fa.bak \u001b[01;31mCryptococcus_neoformans.ASM9104v1.43.gtf.gz\u001b[0m\n", "\u001b[01;31mCM000047.1.fa.gz\u001b[0m\n" ] } ], "source": [ "gzip *fa\n", "ls" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Uncompressing with `gzip`" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CM000046.1.fa CM000047.1.fa.bak Cryptococcus_neoformans.ASM9104v1.43.gtf\n", "CM000047.1.fa cneo040623.scaffs.fa\n" ] } ], "source": [ "gunzip *gz\n", "ls " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Compressing with `bgzip`\n", "\n", "`bgzip` (Blocked GNU Zip Format) is commonly used in Bioinformatics, and allows random access of the content. BAM, BCF and VCF file formats are typically `bgzip` compressed.\n", "\n", "Unfortunately `bgzip` does not take multiple files, and we have to use the `xargs` command to pipe in one file at a time." ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " -n, --max-args=MAX-ARGS use at most MAX-ARGS arguments per command line\n" ] } ], "source": [ "xargs --help | grep '\\-n,'" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[0m\u001b[01;31mCM000046.1.fa.gz\u001b[0m \u001b[01;31mcneo040623.scaffs.fa.gz\u001b[0m\n", "CM000047.1.fa.bak Cryptococcus_neoformans.ASM9104v1.43.gtf\n", "\u001b[01;31mCM000047.1.fa.gz\u001b[0m\n" ] } ], "source": [ "ls *fa | xargs -n1 bgzip\n", "ls" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Note that `bgzip` files can be uncompressed with `gzip`" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CM000046.1.fa CM000047.1.fa.bak Cryptococcus_neoformans.ASM9104v1.43.gtf\n", "CM000047.1.fa cneo040623.scaffs.fa\n" ] } ], "source": [ "gunzip *gz\n", "ls" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Viewing compressed files\n", "\n", "Some utilities can work directly with gzipped files. One that we will be using extensively is `zcat`." ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[0m\u001b[01;31mCM000046.1.fa.gz\u001b[0m \u001b[01;31mcneo040623.scaffs.fa.gz\u001b[0m\n", "CM000047.1.fa.bak Cryptococcus_neoformans.ASM9104v1.43.gtf\n", "\u001b[01;31mCM000047.1.fa.gz\u001b[0m\n" ] } ], "source": [ "ls *fa | xargs -n1 bgzip\n", "ls" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ ">CM000046.1 Cryptococcus neoformans var. neoformans B-3501A chromosome 7, whole genome shotgun sequence\n", "TGGTCTTATGAGGAAGAGGAGTTTGGATTATTTTTTCTTTTCTTTAAAAAGTTGTTTATTTAAGTAGTTT\n", "CTTTAATTCGGGTAACACACACGACAACCCAATAAATTAAACAACGAAAAAATGCAACCTCTATAACCCC\n", "CTAACCCTTTTATTCGGGTAACACACACGACAACCCAATAAATTAAACAACGAAAAATGCAACCTCTATA\n", "ACCCCCGAAAAGGATTGGTGTCGAGTTAGTAAAGGCGAGGGCTAGGACGCTGGTCTTGTGAGGAAGAGGA\n", "GGTTGGATTATTTTTTCTTTTCTTTAATAAGTTGTTTATTTAAGTAGTTTCTTTTATTCGGGTAACACAC\n", "ACGACAACCCAATAAATTAAACAACGAAAAATGCAACCTCTATAACCCCCATCCAAGGTGCCACGGTTGA\n", "GATTGAGCCCCACACACTGTCGGGACAAAGGAGAAACGACCTTCGGGTCAGAGGTTCCAGCGCTCTGGCC\n", "TTCACTGACTACGACCTGAAGGTTTACTCCCTCGGGGACCGAGACGCGAGGAGCACAGCCACCCCCAGCA\n", "CCCCCAACAGCAAACTGGCCGACTTCTGCTTGGACCGGTGCGTGAACTGGCTCGACAAGGTGGGTCAGGT\n" ] } ], "source": [ "zcat CM000046.1.fa.gz | head" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CM000046.1.fa CM000047.1.fa.bak Cryptococcus_neoformans.ASM9104v1.43.gtf\n", "CM000047.1.fa cneo040623.scaffs.fa\n" ] } ], "source": [ "gunzip *gz\n", "ls" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Archival with `tar`\n", "\n", "Often we want to compress an entire direcotry (possibly with subdirectories). In Unix, this is most commonly done with `tar`. Flags to note\n", "\n", "- `c` compress\n", "- `x` extract \n", "- `z` gzip or gunzip\n", "- `f` files\n", "- `v` verbose" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Usage: tar [OPTION...] [FILE]...\n", "GNU 'tar' saves many files together into a single tape or disk archive, and can\n", "restore individual files from the archive.\n", "\n", "Examples:\n", " tar -cf archive.tar foo bar # Create archive.tar from files foo and bar.\n", " tar -tvf archive.tar # List all files in archive.tar verbosely.\n", " tar -xf archive.tar # Extract all files from archive.tar.\n" ] } ], "source": [ "tar --help | head -8" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CM000046.1.fa CM000047.1.fa.bak Cryptococcus_neoformans.ASM9104v1.43.gtf\n", "CM000047.1.fa cneo040623.scaffs.fa\n" ] } ], "source": [ "cd \n", "ls seqs" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "seqs/\n", "seqs/CM000047.1.fa.bak\n", "seqs/CM000046.1.fa\n", "seqs/Cryptococcus_neoformans.ASM9104v1.43.gtf\n", "seqs/CM000047.1.fa\n", "seqs/cneo040623.scaffs.fa\n", "\u001b[0m\u001b[01;31mseqs.tar.gz\u001b[0m\n", "\n", "seqs:\n", "CM000046.1.fa CM000047.1.fa.bak Cryptococcus_neoformans.ASM9104v1.43.gtf\n", "CM000047.1.fa cneo040623.scaffs.fa\n" ] } ], "source": [ "tar czvf seqs.tar.gz seqs\n", "ls seqs*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### View contents of archive" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "drwxr-xr-x jovyan/users 0 2019-06-26 09:39 seqs/\n", "-rw-r--r-- jovyan/users 1194398 2019-06-26 09:30 seqs/CM000047.1.fa.bak\n", "-rw-r--r-- jovyan/users 1434513 2019-06-26 09:38 seqs/CM000046.1.fa\n", "-rw-r--r-- jovyan/users 27773163 2019-06-26 09:30 seqs/Cryptococcus_neoformans.ASM9104v1.43.gtf\n", "-rw-r--r-- jovyan/users 1194378 2019-06-26 09:38 seqs/CM000047.1.fa\n", "-rw-r--r-- jovyan/users 18907235 2019-06-26 09:39 seqs/cneo040623.scaffs.fa\n" ] } ], "source": [ "tar tzvf seqs.tar.gz" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Remove direcotry and regenerate from archive" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[0m\u001b[01;31mseqs.tar.gz\u001b[0m\n" ] } ], "source": [ "rm -rf seqs\n", "ls seqs*" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "seqs/\n", "seqs/CM000047.1.fa.bak\n", "seqs/CM000046.1.fa\n", "seqs/Cryptococcus_neoformans.ASM9104v1.43.gtf\n", "seqs/CM000047.1.fa\n", "seqs/cneo040623.scaffs.fa\n", "\u001b[0m\u001b[01;31mseqs.tar.gz\u001b[0m\n", "\n", "seqs:\n", "CM000046.1.fa CM000047.1.fa.bak Cryptococcus_neoformans.ASM9104v1.43.gtf\n", "CM000047.1.fa cneo040623.scaffs.fa\n" ] } ], "source": [ "tar xzvf seqs.tar.gz\n", "ls seqs*" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [], "source": [ "rm seqs.tar.gz" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 0.4 Checksums" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When working with genomic data, we deal with very large files. There is a small risk that these files will be corrupted over time or during data transfer. To ensure that files are not changed, we use a “checksum” function. This is a function that generates an long, essentially random number called a checksum that represents the contents of the file. When the file contents change, so will the checksum. In theory, there is a very small probability that two different files generate the same checksum, but in practice the probability is too small to worry about.\n", "\n", "There are several different algorithms for generating the checksums, and at least 3 Unix commands to do so, but they all work very similarly for our purposes.\n", "\n", "The strategy is:\n", "\n", "- Generate and store a checksum together with a data file whose integrity you care about\n", "- When you use or download the data, re-generate the checksum (using the same algorithm e.g. MD5) and compare with the checksum" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CM000046.1.fa CM000047.1.fa.bak Cryptococcus_neoformans.ASM9104v1.43.gtf\n", "CM000047.1.fa cneo040623.scaffs.fa\n" ] } ], "source": [ "ls ~/seqs" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [], "source": [ "md5sum ~/seqs/*fa > MD5_CHECKSUM" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### We check that the files are unchanged wth the `-c` flag" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/home/jovyan/seqs/CM000046.1.fa: OK\n", "/home/jovyan/seqs/CM000047.1.fa: OK\n", "/home/jovyan/seqs/cneo040623.scaffs.fa: OK\n" ] } ], "source": [ "md5sum -c MD5_CHECKSUM" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Suppsoe one of the files is corrupted" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [], "source": [ "echo 'Oops I did it again' >> seqs/CM000047.1.fa" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/home/jovyan/seqs/CM000046.1.fa: OK\n", "/home/jovyan/seqs/CM000047.1.fa: FAILED\n", "/home/jovyan/seqs/cneo040623.scaffs.fa: OK\n", "md5sum: WARNING: 1 computed checksum did NOT match\n" ] } ], "source": [ "md5sum -c MD5_CHECKSUM \\\n", "| cat # this is jusst so we can Run All" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Using `diff` to find changes\n", "\n", "Let's rename the altered file, re-download the orginal and see what was changed." ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [], "source": [ "mv seqs/CM000047.1.fa seqs/CM000047.1.fa.bak" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [], "source": [ "efetch -db=nuccore -format=fasta -id=CM000047.1 > seqs/CM000047.1.fa" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "--- seqs/CM000047.1.fa\t2019-06-26 09:39:10.615210391 +0000\n", "+++ seqs/CM000047.1.fa.bak\t2019-06-26 09:39:09.187204139 +0000\n", "@@ -16820,3 +16820,4 @@\n", " CCGTGTCGGCACGCATCTGATTTTTTTCTTATTATTCATAACAACGGCTACAGATCACGCAAGCGCATCG\n", " ACTCCGCTGCTGCGAAACACCACGAGTACTGCAACCATTGCTGCGGATGAGCTTAGGGGGTTAGGGGGTT\n", " AGGGGGTTAGGGGGTTAGGGGGTTAGGGGGTTAGGGGGTTAGGGGGTTAGGGG\n", "+Oops I did it again\n" ] } ], "source": [ "diff -u seqs/CM000047.1.fa seqs/CM000047.1.fa.bak \\\n", "| cat # this is jusst so we can Run All" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Explanaiton from [official documentation]\n", "\n", "Header\n", "\n", "```\n", "--- seqs/CM000047.1.fa\t2018-06-21 09:58:18.246185758 -0400\n", "+++ seqs/CM000047.1.fa.bak\t2018-06-21 09:55:55.967707764 -0400\n", "```\n", "\n", "Explanation\n", "\n", "```\n", "The unified output format starts with a two-line header, which looks like this:\n", "\n", "--- from-file from-file-modification-time\n", "+++ to-file to-file-modification-time\n", "The timestamp looks like ‘2002-02-21 23:30:39.942229878 -0800’ to indicate the date, time with fractional seconds, and time zone. The fractional seconds are omitted on hosts that do not support fractional timestamps.\n", "\n", "You can change the header’s content with the --label=label option. See Alternate Names.\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Hunk \n", "\n", "```\n", "@@ -16820,3 +16820,4 @@\n", " CCGTGTCGGCACGCATCTGATTTTTTTCTTATTATTCATAACAACGGCTACAGATCACGCAAGCGCATCG\n", " ACTCCGCTGCTGCGAAACACCACGAGTACTGCAACCATTGCTGCGGATGAGCTTAGGGGGTTAGGGGGTT\n", " AGGGGGTTAGGGGGTTAGGGGGTTAGGGGGTTAGGGGGTTAGGGGGTTAGGGG\n", "+Oops I did it again\n", "```\n", "\n", "Explanation\n", "\n", "```\n", "@@ from-file-line-numbers to-file-line-numbers @@\n", " line-from-either-file\n", " line-from-either-file…\n", " \n", " If a hunk contains just one line, only its start line number appears. Otherwise its line numbers look like ‘start,count’. An empty hunk is considered to start at the line that follows the hunk.\n", "\n", "If a hunk and its context contain two or more lines, its line numbers look like ‘start,count’. Otherwise only its end line number appears. An empty hunk is considered to end at the line that precedes the hunk.\n", "\n", "The lines common to both files begin with a space character. The lines that actually differ between the two files have one of the following indicator characters in the left print column:\n", "\n", "‘+’\n", "A line was added here to the first file.\n", "\n", "‘-’\n", "A line was removed here from the first file.\n", " ```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Verify\n", "\n", "We can confirm that a line was added at the position given by `diff`" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CCGTGTCGGCACGCATCTGATTTTTTTCTTATTATTCATAACAACGGCTACAGATCACGCAAGCGCATCG\n", "ACTCCGCTGCTGCGAAACACCACGAGTACTGCAACCATTGCTGCGGATGAGCTTAGGGGGTTAGGGGGTT\n", "AGGGGGTTAGGGGGTTAGGGGGTTAGGGGGTTAGGGGGTTAGGGGGTTAGGGG\n" ] } ], "source": [ "cat seqs/CM000047.1.fa | tail +16820 | head -3" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CCGTGTCGGCACGCATCTGATTTTTTTCTTATTATTCATAACAACGGCTACAGATCACGCAAGCGCATCG\n", "ACTCCGCTGCTGCGAAACACCACGAGTACTGCAACCATTGCTGCGGATGAGCTTAGGGGGTTAGGGGGTT\n", "AGGGGGTTAGGGGGTTAGGGGGTTAGGGGGTTAGGGGGTTAGGGGGTTAGGGG\n", "Oops I did it again\n" ] } ], "source": [ "cat seqs/CM000047.1.fa.bak | tail +16820 | head -4" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1. Working with FASTA files" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Strucrure of a FASTA file\n", "\n", "From [NCBI FASTA specification](https://blast.ncbi.nlm.nih.gov/Blast.cgi?CMD=Web&PAGE_TYPE=BlastDocs&DOC_TYPE=BlastHelp)\n", "\n", "```\n", "A sequence in FASTA format begins with a single-line description, followed by lines of sequence data. The description line (defline) is distinguished from the sequence data by a greater-than (\">\") symbol at the beginning. It is recommended that all lines of text be shorter than 80 characters in length. An example sequence in FASTA format is:\n", "\n", "\t\t>P01013 GENE X PROTEIN (OVALBUMIN-RELATED)\n", "\t\tQIKDLLVSSSTDLDTTLVLVNAIYFKGMWKTAFNAEDTREMPFHVTKQESKPVQMMCMNNSFNVATLPAE\n", "\t\tKMKILELPFASGDLSMLVLLPDEVSDLERIEKTINFEKLTEWTNPNTMEKRRVKVYLPQMKIEEKYNLTS\n", "\t\tVLMALGMTDLFIPSANLTGISSAESLKISQAVHGAFMELSEDGIEMAGSTGVIEDIKHSPESEQFRADHP\n", "\t\tFLFLIKHNPTNTIVYFGRYWSP\n", "```\n", "\n", "Note that a FASTA file may contain mulitple sequences, each with its own description line." ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "--2019-06-26 09:39:11-- ftp://ftp.ensemblgenomes.org/pub/release-43/fungi/gtf/cryptococcus_neoformans/Cryptococcus_neoformans.ASM9104v1.43.gtf.gz\n", " => ‘Cryptococcus_neoformans.ASM9104v1.43.gtf.gz’\n", "Resolving ftp.ensemblgenomes.org (ftp.ensemblgenomes.org)... 193.62.197.94\n", "Connecting to ftp.ensemblgenomes.org (ftp.ensemblgenomes.org)|193.62.197.94|:21... connected.\n", "Logging in as anonymous ... Logged in!\n", "==> SYST ... done. ==> PWD ... done.\n", "==> TYPE I ... done. ==> CWD (1) /pub/release-43/fungi/gtf/cryptococcus_neoformans ... done.\n", "==> SIZE Cryptococcus_neoformans.ASM9104v1.43.gtf.gz ... 1546239\n", "==> PASV ... done. ==> RETR Cryptococcus_neoformans.ASM9104v1.43.gtf.gz ... done.\n", "Length: 1546239 (1.5M) (unauthoritative)\n", "\n", "Cryptococcus_neofor 100%[===================>] 1.47M 2.04MB/s in 0.7s \n", "\n", "2019-06-26 09:39:13 (2.04 MB/s) - ‘Cryptococcus_neoformans.ASM9104v1.43.gtf.gz’ saved [1546239]\n", "\n", "--2019-06-26 09:39:13-- http://www-sequence.stanford.edu/group/C.neoformans/files/cneo040623.scaffs.fa.gz\n", "Resolving www-sequence.stanford.edu (www-sequence.stanford.edu)... 171.66.102.5\n", "Connecting to www-sequence.stanford.edu (www-sequence.stanford.edu)|171.66.102.5|:80... connected.\n", "HTTP request sent, awaiting response... 200 OK\n", "Length: 5856251 (5.6M) [text/plain]\n", "Saving to: ‘cneo040623.scaffs.fa.gz’\n", "\n", "cneo040623.scaffs.f 100%[===================>] 5.58M 1.65MB/s in 3.4s \n", "\n", "2019-06-26 09:39:16 (1.65 MB/s) - ‘cneo040623.scaffs.fa.gz’ saved [5856251/5856251]\n", "\n", "/home/jovyan\n" ] } ], "source": [ "cd seqs\n", "wget ftp://ftp.ensemblgenomes.org/pub/release-43/fungi/gtf/cryptococcus_neoformans/Cryptococcus_neoformans.ASM9104v1.43.gtf.gz\n", "wget http://www-sequence.stanford.edu/group/C.neoformans/files/cneo040623.scaffs.fa.gz\n", "cd -" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Set up variables for files we will be using" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [], "source": [ "#DATA_DIR='/data/hts/2018/Granek_4837_180427A5'\n", "DATA_DIR='./seqs'\n", "FASTA='cneo040623.scaffs.fa.gz'\n", "FASTQ='10_MA_C_S3_L003_R1_001.fastq.gz'\n", "GTF='Cryptococcus_neoformans.ASM9104v1.43.gtf.gz'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### This is how you get the value of a shell variable" ] }, { "cell_type": "code", "execution_count": 51, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "./seqs\n" ] } ], "source": [ "echo $DATA_DIR" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### List files in data direcotry" ] }, { "cell_type": "code", "execution_count": 52, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CM000046.1.fa\n", "CM000047.1.fa\n", "CM000047.1.fa.bak\n" ] } ], "source": [ "ls $DATA_DIR | head -3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Inspect the contents of a zipped file.\n", "\n", "`zcat` is like `cat` but works on gzipped files." ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ ">chr01.b3501.040506 5 contigs (2263073 bp) \n", "CCCTAACCCCCTAACCCCCTAACCCTCAGTCGGGGAGGCGACAATAGCTG\n", "GTCATATGTTCTGTACGTTATTGGCCAACTGATCATACCTGAATCGAGCC\n", "GAACCGGGATTATCAAAGACGAACATGGTCGGGTCCTTGAACCAAACGAA\n", "TCTCCGTCCGCTGGCGTGTTTTTCTTTTCTCAAGTGGGCAAGTTACCCGG\n", "GGATTTATACAACGATAAGAGGCTATCCGCTGACTATAAATTGTGTTAGC\n", "TGATCCAGATCCGCAGTTGGCACAAACCGATGTTTCCTTTTCCTCAGGTC\n", "CTGAATATTTCTCTTCCATCAAGGCTCCCAACCCCGAGGGGAGTATTTCC\n", "ACCCGATCTGATTCCAAGAGGTCCAGCGTCAACCAGGTACGTGAATGTTT\n", "TCTTCTTTAGTGGTGGGGACCCTATGCTTGTCCCACAGAACTGCACTGAA\n" ] } ], "source": [ "zcat $DATA_DIR/$FASTA | head" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Show headers only" ] }, { "cell_type": "code", "execution_count": 54, "metadata": { "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ ">chr01.b3501.040506 5 contigs (2263073 bp) \n", ">chr02.b3501.040506 6 contigs (1607892 bp) \n", ">chr03.b3501.040506 6 contigs (2025976 bp) \n", ">chr04.b3501.040623 5 contigs (1762351 bp) \n", ">chr05.b3501.040506 7 contigs (1432964 bp) \n", ">chr06.b3501.040616 4 contigs (1411173 bp) \n", ">chr07.b3501.040506 6 contigs (1356306 bp) \n", ">chr08.b3501.040506 3 contigs (1153453 bp) \n", ">chr09.b3501.040506 5 contigs (1072068 bp) \n", ">chr10.b3501.040506 3 contigs (1017008 bp) \n" ] } ], "source": [ "zcat $DATA_DIR/$FASTA | grep '^>' | head" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Show headers with line numbers" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1:>chr01.b3501.040506 5 contigs (2263073 bp) \n", "45264:>chr02.b3501.040506 6 contigs (1607892 bp) \n", "77423:>chr03.b3501.040506 6 contigs (2025976 bp) \n", "117944:>chr04.b3501.040623 5 contigs (1762351 bp) \n", "153193:>chr05.b3501.040506 7 contigs (1432964 bp) \n", "181854:>chr06.b3501.040616 4 contigs (1411173 bp) \n", "210079:>chr07.b3501.040506 6 contigs (1356306 bp) \n", "237207:>chr08.b3501.040506 3 contigs (1153453 bp) \n", "260278:>chr09.b3501.040506 5 contigs (1072068 bp) \n", "281721:>chr10.b3501.040506 3 contigs (1017008 bp) \n" ] } ], "source": [ "zcat $DATA_DIR/$FASTA| grep -n '^>' | head" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Count number of headers (one for each crhomosome)" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "14\n" ] } ], "source": [ "zcat $DATA_DIR/$FASTA | grep -c '^>'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Extract first part of the sequence from chromosome 7" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ ">chr07.b3501.040506 6 contigs (1356306 bp) \n", "TGGTCTTATGAGGAAGAGGAGTTTGGATTATTTTTTCTTTTCTTTAAAAA\n", "GTTGTTTATTTAAGTAGTTTCTTTAATTCGGGTAACACACACGACAACCC\n", "AATAAATTAAACAACGAAAAAATGCAACCTCTATAACCCCCTAACCCTTT\n" ] } ], "source": [ "zcat $DATA_DIR/$FASTA | sed -n '210079,210082p'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Extract first line of chromosome 7 and save to file" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [], "source": [ "zcat $DATA_DIR/$FASTA | sed -n '210080p' > dna.txt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Inspect the file we just saved" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "TGGTCTTATGAGGAAGAGGAGTTTGGATTATTTTTTCTTTTCTTTAAAAA\n" ] } ], "source": [ "cat dna.txt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Find complement" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ACCAGAATACTCCTTCTCCTCAAACCTAATAAAAAAGAAAAGAAATTTTT\n" ] } ], "source": [ "cat dna.txt | tr ACGT TGCA" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Find reverse complement" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "TTTTTAAAGAAAAGAAAAAATAATCCAAACTCCTCTTCCTCATAAGACCA\n" ] } ], "source": [ "cat dna.txt | tr ACGT TGCA | rev" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Delete A and T" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "GGCGGGGGGGGGCC\n" ] } ], "source": [ "cat dna.txt | tr -d 'AT' " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Only count non A and T" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "14\n" ] } ], "source": [ "cat dna.txt | tr -d 'AT' | tr -d '\\n' | wc -c" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "bash: man: command not found\n" ] }, { "ename": "", "evalue": "1", "output_type": "error", "traceback": [] } ], "source": [ "man tr | grep '\\-d,' -A2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Count C and G" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "14\n" ] } ], "source": [ "grep '[CG]' -o dna.txt | wc -l" ] }, { "cell_type": "code", "execution_count": 66, "metadata": { "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "bash: man: command not found\n" ] }, { "ename": "", "evalue": "1", "output_type": "error", "traceback": [] } ], "source": [ "man grep | grep '\\-o,' -A2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Specify data directory as a shell variable" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2. Working with FASTQ files" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Structure of a FASTQ file\n", "\n", "From [Wikipedia](https://en.wikipedia.org/wiki/FASTQ_format)\n", "\n", "```\n", "A FASTQ file normally uses four lines per sequence.\n", "\n", "Line 1 begins with a '@' character and is followed by a sequence identifier and an optional description (like a FASTA title line).\n", "Line 2 is the raw sequence letters.\n", "Line 3 begins with a '+' character and is optionally followed by the same sequence identifier (and any description) again.\n", "Line 4 encodes the quality values for the sequence in Line 2, and must contain the same number of symbols as letters in the sequence.\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Specify target file as a shell variable" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [], "source": [ "DATA_DIR='/data/hts2018_pilot/Granek_4837_180427A5'" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[0m\u001b[01;31m10_MA_C_S3_L001_R1_001.fastq.gz\u001b[0m \u001b[01;31m26_RZ_C_S16_L004_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m10_MA_C_S3_L002_R1_001.fastq.gz\u001b[0m \u001b[01;31m27_MA_P_S38_L001_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m10_MA_C_S3_L003_R1_001.fastq.gz\u001b[0m \u001b[01;31m27_MA_P_S38_L002_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m10_MA_C_S3_L004_R1_001.fastq.gz\u001b[0m \u001b[01;31m27_MA_P_S38_L003_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m10_RZ_C_S11_L001_R1_001.fastq.gz\u001b[0m \u001b[01;31m27_MA_P_S38_L004_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m10_RZ_C_S11_L002_R1_001.fastq.gz\u001b[0m \u001b[01;31m27_RZ_P_S46_L001_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m10_RZ_C_S11_L003_R1_001.fastq.gz\u001b[0m \u001b[01;31m27_RZ_P_S46_L002_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m10_RZ_C_S11_L004_R1_001.fastq.gz\u001b[0m \u001b[01;31m27_RZ_P_S46_L003_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m11_MA_J_S20_L001_R1_001.fastq.gz\u001b[0m \u001b[01;31m27_RZ_P_S46_L004_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m11_MA_J_S20_L002_R1_001.fastq.gz\u001b[0m \u001b[01;31m2_MA_C_S1_L001_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m11_MA_J_S20_L003_R1_001.fastq.gz\u001b[0m \u001b[01;31m2_MA_C_S1_L002_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m11_MA_J_S20_L004_R1_001.fastq.gz\u001b[0m \u001b[01;31m2_MA_C_S1_L003_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m11_RZ_J_S28_L001_R1_001.fastq.gz\u001b[0m \u001b[01;31m2_MA_C_S1_L004_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m11_RZ_J_S28_L002_R1_001.fastq.gz\u001b[0m \u001b[01;31m2_RZ_C_S9_L001_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m11_RZ_J_S28_L003_R1_001.fastq.gz\u001b[0m \u001b[01;31m2_RZ_C_S9_L002_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m11_RZ_J_S28_L004_R1_001.fastq.gz\u001b[0m \u001b[01;31m2_RZ_C_S9_L003_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m12_MA_P_S36_L001_R1_001.fastq.gz\u001b[0m \u001b[01;31m2_RZ_C_S9_L004_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m12_MA_P_S36_L002_R1_001.fastq.gz\u001b[0m \u001b[01;31m2_TOT_C_S17_L001_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m12_MA_P_S36_L003_R1_001.fastq.gz\u001b[0m \u001b[01;31m2_TOT_C_S17_L002_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m12_MA_P_S36_L004_R1_001.fastq.gz\u001b[0m \u001b[01;31m2_TOT_C_S17_L003_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m12_RZ_P_S44_L001_R1_001.fastq.gz\u001b[0m \u001b[01;31m2_TOT_C_S17_L004_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m12_RZ_P_S44_L002_R1_001.fastq.gz\u001b[0m \u001b[01;31m35_MA_P_S39_L001_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m12_RZ_P_S44_L003_R1_001.fastq.gz\u001b[0m \u001b[01;31m35_MA_P_S39_L002_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m12_RZ_P_S44_L004_R1_001.fastq.gz\u001b[0m \u001b[01;31m35_MA_P_S39_L003_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m13_MA_J_S21_L001_R1_001.fastq.gz\u001b[0m \u001b[01;31m35_MA_P_S39_L004_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m13_MA_J_S21_L002_R1_001.fastq.gz\u001b[0m \u001b[01;31m35_RZ_P_S47_L001_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m13_MA_J_S21_L003_R1_001.fastq.gz\u001b[0m \u001b[01;31m35_RZ_P_S47_L002_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m13_MA_J_S21_L004_R1_001.fastq.gz\u001b[0m \u001b[01;31m35_RZ_P_S47_L003_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m13_RZ_J_S29_L001_R1_001.fastq.gz\u001b[0m \u001b[01;31m35_RZ_P_S47_L004_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m13_RZ_J_S29_L002_R1_001.fastq.gz\u001b[0m \u001b[01;31m36_MA_J_S24_L001_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m13_RZ_J_S29_L003_R1_001.fastq.gz\u001b[0m \u001b[01;31m36_MA_J_S24_L002_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m13_RZ_J_S29_L004_R1_001.fastq.gz\u001b[0m \u001b[01;31m36_MA_J_S24_L003_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m14_MA_C_S4_L001_R1_001.fastq.gz\u001b[0m \u001b[01;31m36_MA_J_S24_L004_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m14_MA_C_S4_L002_R1_001.fastq.gz\u001b[0m \u001b[01;31m36_RZ_J_S32_L001_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m14_MA_C_S4_L003_R1_001.fastq.gz\u001b[0m \u001b[01;31m36_RZ_J_S32_L002_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m14_MA_C_S4_L004_R1_001.fastq.gz\u001b[0m \u001b[01;31m36_RZ_J_S32_L003_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m14_RZ_C_S12_L001_R1_001.fastq.gz\u001b[0m \u001b[01;31m36_RZ_J_S32_L004_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m14_RZ_C_S12_L002_R1_001.fastq.gz\u001b[0m \u001b[01;31m38_MA_P_S40_L001_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m14_RZ_C_S12_L003_R1_001.fastq.gz\u001b[0m \u001b[01;31m38_MA_P_S40_L002_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m14_RZ_C_S12_L004_R1_001.fastq.gz\u001b[0m \u001b[01;31m38_MA_P_S40_L003_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m15_MA_C_S5_L001_R1_001.fastq.gz\u001b[0m \u001b[01;31m38_MA_P_S40_L004_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m15_MA_C_S5_L002_R1_001.fastq.gz\u001b[0m \u001b[01;31m38_RZ_P_S48_L001_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m15_MA_C_S5_L003_R1_001.fastq.gz\u001b[0m \u001b[01;31m38_RZ_P_S48_L002_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m15_MA_C_S5_L004_R1_001.fastq.gz\u001b[0m \u001b[01;31m38_RZ_P_S48_L003_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m15_RZ_C_S13_L001_R1_001.fastq.gz\u001b[0m \u001b[01;31m38_RZ_P_S48_L004_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m15_RZ_C_S13_L002_R1_001.fastq.gz\u001b[0m \u001b[01;31m3_MA_J_S19_L001_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m15_RZ_C_S13_L003_R1_001.fastq.gz\u001b[0m \u001b[01;31m3_MA_J_S19_L002_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m15_RZ_C_S13_L004_R1_001.fastq.gz\u001b[0m \u001b[01;31m3_MA_J_S19_L003_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m16_MA_P_S37_L001_R1_001.fastq.gz\u001b[0m \u001b[01;31m3_MA_J_S19_L004_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m16_MA_P_S37_L002_R1_001.fastq.gz\u001b[0m \u001b[01;31m3_RZ_J_S27_L001_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m16_MA_P_S37_L003_R1_001.fastq.gz\u001b[0m \u001b[01;31m3_RZ_J_S27_L002_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m16_MA_P_S37_L004_R1_001.fastq.gz\u001b[0m \u001b[01;31m3_RZ_J_S27_L003_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m16_RZ_P_S45_L001_R1_001.fastq.gz\u001b[0m \u001b[01;31m3_RZ_J_S27_L004_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m16_RZ_P_S45_L002_R1_001.fastq.gz\u001b[0m \u001b[01;31m3_TOT_J_S34_L001_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m16_RZ_P_S45_L003_R1_001.fastq.gz\u001b[0m \u001b[01;31m3_TOT_J_S34_L002_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m16_RZ_P_S45_L004_R1_001.fastq.gz\u001b[0m \u001b[01;31m3_TOT_J_S34_L003_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m1_MA_J_S18_L001_R1_001.fastq.gz\u001b[0m \u001b[01;31m3_TOT_J_S34_L004_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m1_MA_J_S18_L002_R1_001.fastq.gz\u001b[0m \u001b[01;31m40_MA_J_S25_L001_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m1_MA_J_S18_L003_R1_001.fastq.gz\u001b[0m \u001b[01;31m40_MA_J_S25_L002_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m1_MA_J_S18_L004_R1_001.fastq.gz\u001b[0m \u001b[01;31m40_MA_J_S25_L003_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m1_RZ_J_S26_L001_R1_001.fastq.gz\u001b[0m \u001b[01;31m40_MA_J_S25_L004_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m1_RZ_J_S26_L002_R1_001.fastq.gz\u001b[0m \u001b[01;31m40_RZ_J_S33_L001_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m1_RZ_J_S26_L003_R1_001.fastq.gz\u001b[0m \u001b[01;31m40_RZ_J_S33_L002_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m1_RZ_J_S26_L004_R1_001.fastq.gz\u001b[0m \u001b[01;31m40_RZ_J_S33_L003_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m21_MA_C_S6_L001_R1_001.fastq.gz\u001b[0m \u001b[01;31m40_RZ_J_S33_L004_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m21_MA_C_S6_L002_R1_001.fastq.gz\u001b[0m \u001b[01;31m45_MA_P_S41_L001_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m21_MA_C_S6_L003_R1_001.fastq.gz\u001b[0m \u001b[01;31m45_MA_P_S41_L002_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m21_MA_C_S6_L004_R1_001.fastq.gz\u001b[0m \u001b[01;31m45_MA_P_S41_L003_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m21_RZ_C_S14_L001_R1_001.fastq.gz\u001b[0m \u001b[01;31m45_MA_P_S41_L004_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m21_RZ_C_S14_L002_R1_001.fastq.gz\u001b[0m \u001b[01;31m45_RZ_P_S49_L001_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m21_RZ_C_S14_L003_R1_001.fastq.gz\u001b[0m \u001b[01;31m45_RZ_P_S49_L002_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m21_RZ_C_S14_L004_R1_001.fastq.gz\u001b[0m \u001b[01;31m45_RZ_P_S49_L003_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m22_MA_C_S7_L001_R1_001.fastq.gz\u001b[0m \u001b[01;31m45_RZ_P_S49_L004_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m22_MA_C_S7_L002_R1_001.fastq.gz\u001b[0m \u001b[01;31m47_MA_P_S42_L001_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m22_MA_C_S7_L003_R1_001.fastq.gz\u001b[0m \u001b[01;31m47_MA_P_S42_L002_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m22_MA_C_S7_L004_R1_001.fastq.gz\u001b[0m \u001b[01;31m47_MA_P_S42_L003_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m22_RZ_C_S15_L001_R1_001.fastq.gz\u001b[0m \u001b[01;31m47_MA_P_S42_L004_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m22_RZ_C_S15_L002_R1_001.fastq.gz\u001b[0m \u001b[01;31m47_RZ_P_S50_L001_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m22_RZ_C_S15_L003_R1_001.fastq.gz\u001b[0m \u001b[01;31m47_RZ_P_S50_L002_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m22_RZ_C_S15_L004_R1_001.fastq.gz\u001b[0m \u001b[01;31m47_RZ_P_S50_L003_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m23_MA_J_S22_L001_R1_001.fastq.gz\u001b[0m \u001b[01;31m47_RZ_P_S50_L004_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m23_MA_J_S22_L002_R1_001.fastq.gz\u001b[0m \u001b[01;31m4_MA_P_S35_L001_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m23_MA_J_S22_L003_R1_001.fastq.gz\u001b[0m \u001b[01;31m4_MA_P_S35_L002_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m23_MA_J_S22_L004_R1_001.fastq.gz\u001b[0m \u001b[01;31m4_MA_P_S35_L003_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m23_RZ_J_S30_L001_R1_001.fastq.gz\u001b[0m \u001b[01;31m4_MA_P_S35_L004_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m23_RZ_J_S30_L002_R1_001.fastq.gz\u001b[0m \u001b[01;31m4_RZ_P_S43_L001_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m23_RZ_J_S30_L003_R1_001.fastq.gz\u001b[0m \u001b[01;31m4_RZ_P_S43_L002_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m23_RZ_J_S30_L004_R1_001.fastq.gz\u001b[0m \u001b[01;31m4_RZ_P_S43_L003_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m24_MA_J_S23_L001_R1_001.fastq.gz\u001b[0m \u001b[01;31m4_RZ_P_S43_L004_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m24_MA_J_S23_L002_R1_001.fastq.gz\u001b[0m \u001b[01;31m4_TOT_P_S51_L001_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m24_MA_J_S23_L003_R1_001.fastq.gz\u001b[0m \u001b[01;31m4_TOT_P_S51_L002_R1_001.fastq.gz\u001b[0m\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\u001b[01;31m24_MA_J_S23_L004_R1_001.fastq.gz\u001b[0m \u001b[01;31m4_TOT_P_S51_L003_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m24_RZ_J_S31_L001_R1_001.fastq.gz\u001b[0m \u001b[01;31m4_TOT_P_S51_L004_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m24_RZ_J_S31_L002_R1_001.fastq.gz\u001b[0m \u001b[01;31m9_MA_C_S2_L001_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m24_RZ_J_S31_L003_R1_001.fastq.gz\u001b[0m \u001b[01;31m9_MA_C_S2_L002_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m24_RZ_J_S31_L004_R1_001.fastq.gz\u001b[0m \u001b[01;31m9_MA_C_S2_L003_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m26_MA_C_S8_L001_R1_001.fastq.gz\u001b[0m \u001b[01;31m9_MA_C_S2_L004_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m26_MA_C_S8_L002_R1_001.fastq.gz\u001b[0m \u001b[01;31m9_RZ_C_S10_L001_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m26_MA_C_S8_L003_R1_001.fastq.gz\u001b[0m \u001b[01;31m9_RZ_C_S10_L002_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m26_MA_C_S8_L004_R1_001.fastq.gz\u001b[0m \u001b[01;31m9_RZ_C_S10_L003_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m26_RZ_C_S16_L001_R1_001.fastq.gz\u001b[0m \u001b[01;31m9_RZ_C_S10_L004_R1_001.fastq.gz\u001b[0m\n", "\u001b[01;31m26_RZ_C_S16_L002_R1_001.fastq.gz\u001b[0m Granek_4837_180427A5.checksum\n", "\u001b[01;31m26_RZ_C_S16_L003_R1_001.fastq.gz\u001b[0m README.rtf\n" ] } ], "source": [ "ls $DATA_DIR" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Show the contents of a zipped file" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "@NB501800:128:HTYKFBGX5:3:11401:19195:1019 1:N:0:ATTACTCG+AGGATAGG\n", "NGTAAGGGTCTGGGGGACGTTGTTGTGGATGGAAACAACCCTGGCCTCTCGGGGCTGAGCAGGGAGTCGCTTGAGC\n", "+\n", "#AAAAEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEAEEEEEEEEEEEEEEEEEEAEEEEEEEEEEEEE/<<\n", "@NB501800:128:HTYKFBGX5:3:11401:24339:1020 1:N:0:ATTACTCG+AGGATAGG\n", "NCTCGCCTGGGGTGCTTGCGTGTCCCTGTCCTTGGCCCTGGTCACCGGGGAGATGCACATCATCTTCTTCTGGCTC\n", "+\n", "#AAAAEAAEEEEEEEEEEEEEEEE/EEE/EEEEEEEEEEEEEEEE genes.txt" ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "gene_id \"CNA00010\"; gene_source \"ena\"; gene_biotype \"protein_coding\";\n", "gene_id \"CNA00020\"; gene_source \"ena\"; gene_biotype \"protein_coding\";\n", "gene_id \"CNA00030\"; gene_source \"ena\"; gene_biotype \"protein_coding\";\n", "gene_id \"CNA00040\"; gene_source \"ena\"; gene_biotype \"protein_coding\";\n", "gene_id \"CNA00050\"; gene_source \"ena\"; gene_biotype \"protein_coding\";\n", "gene_id \"CNA00060\"; gene_source \"ena\"; gene_biotype \"protein_coding\";\n" ] } ], "source": [ "head -6 genes.txt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 4. Looping" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Loop throgh first 10 lines\n", "\n", "- Creae a counter variable `LINE_NUM` and initialize it to 0\n", "- Redirect `genes.txt` to a while loop\n", "- Whlie loop reads each line into the variable `LINE`\n", "- The `[[ CONDITION ]]` tests if CONDITION is true\n", "- If LINE_NUM is less than `-lt` 10 print LINE and increment LINE_NUM\n", "- Note that `(( EXPR ))` is used to evaluate numeric expressions" ] }, { "cell_type": "code", "execution_count": 82, "metadata": { "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "gene_id \"CNA00010\"; gene_source \"ena\"; gene_biotype \"protein_coding\";\n", "gene_id \"CNA00020\"; gene_source \"ena\"; gene_biotype \"protein_coding\";\n", "gene_id \"CNA00030\"; gene_source \"ena\"; gene_biotype \"protein_coding\";\n", "gene_id \"CNA00040\"; gene_source \"ena\"; gene_biotype \"protein_coding\";\n", "gene_id \"CNA00050\"; gene_source \"ena\"; gene_biotype \"protein_coding\";\n", "gene_id \"CNA00060\"; gene_source \"ena\"; gene_biotype \"protein_coding\";\n", "gene_id \"CNA00070\"; gene_source \"ena\"; gene_biotype \"protein_coding\";\n", "gene_id \"CNA00075\"; gene_source \"ena\"; gene_biotype \"protein_coding\";\n", "gene_id \"CNA00080\"; gene_source \"ena\"; gene_biotype \"protein_coding\";\n", "gene_id \"CNA00090\"; gene_source \"ena\"; gene_biotype \"protein_coding\";\n" ] } ], "source": [ "LINE_NUM=0\n", "while read LINE\n", "do\n", " if [[ $LINE_NUM -lt 10 ]]; then\n", " echo $LINE\n", " (( LINE_NUM++ ))\n", " fi\n", "done < genes.txt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Loop through first 10 tRNAs\n", "\n", "Almost the same as previous program but we add a new condition to check tha the gene_biotype is tRNA using a REGULAr EXPRESSION via the match operator `=~`\n", "\n", "`.*gene_biotype[[:space:]]\\\"tRNA\\\".*` means\n", "\n", "- `.*`: Match any number of any character \n", "- `gene_biotype`: Match the string gene_biotype\n", "- `[[:space:]]`: Match a single space cahracter\n", "- `\\\"tRNA\\\"`: Math the string \"tRNA\" - we escape the double quotes to match linteral double quote characters\n", "- `.*`: Match any number of any character " ] }, { "cell_type": "code", "execution_count": 83, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "gene_id \"CNA02110\"; gene_name \"CNA02110\"; gene_source \"ena\"; gene_biotype \"tRNA\";\n", "gene_id \"EBG00005237496\"; gene_name \"tRNA\"; gene_source \"Rfam\"; gene_biotype \"tRNA\";\n", "gene_id \"CNA02190\"; gene_name \"CNA02190\"; gene_source \"ena\"; gene_biotype \"tRNA\";\n", "gene_id \"CNA02200\"; gene_name \"CNA02200\"; gene_source \"ena\"; gene_biotype \"tRNA\";\n", "gene_id \"CNA02280\"; gene_name \"CNA02280\"; gene_source \"ena\"; gene_biotype \"tRNA\";\n", "gene_id \"EBG00005237502\"; gene_name \"tRNA\"; gene_source \"Rfam\"; gene_biotype \"tRNA\";\n", "gene_id \"CNA02320\"; gene_name \"CNA02320\"; gene_source \"ena\"; gene_biotype \"tRNA\";\n", "gene_id \"CNA03230\"; gene_name \"CNA03230\"; gene_source \"ena\"; gene_biotype \"tRNA\";\n", "gene_id \"EBG00005237494\"; gene_name \"tRNA\"; gene_source \"Rfam\"; gene_biotype \"tRNA\";\n", "gene_id \"CNA03260\"; gene_name \"CNA03260\"; gene_source \"ena\"; gene_biotype \"tRNA\";\n" ] } ], "source": [ "LINE_NUM=0\n", "while read LINE\n", "do\n", " if [[ $LINE_NUM -lt 10 ]] && [[ $LINE =~ .*gene_biotype[[:space:]]\\\"tRNA\\\".* ]] ; then\n", " echo $LINE\n", " ((LINE_NUM++))\n", " fi\n", "done < genes.txt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Loop through files in a directory and print the filename followed by the first line\n", "\n", "- Use a COMMAND EXPRESSION `$(EXPR)` to list files matching the glob pattern\n", "- We loop through each file using a `for` statement\n", "- For each file, extract the first line with `head -1` and save in variable `FIRST`\n", "- The `2>/dev/null` ignores output from the `stderr` stream\n", "- We use another COMMAND EXPRESSION to print the filename withou the directory (using `basename`)\n", "- We then print the first line storred in `FIRST`" ] }, { "cell_type": "code", "execution_count": 84, "metadata": {}, "outputs": [], "source": [ "DATA_DIR='/data/hts2018_pilot/Granek_4837_180427A5'" ] }, { "cell_type": "code", "execution_count": 85, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "11_MA_J_S20_L001_R1_001.fastq.gz\n", "@NB501800:128:HTYKFBGX5:1:11101:26760:1056 1:N:0:CGCTCATT+NGGATAGG\n", "\n", "11_MA_J_S20_L002_R1_001.fastq.gz\n", "@NB501800:128:HTYKFBGX5:2:11101:20327:1041 1:N:0:CGCTCATT+AGGATAGN\n", "\n", "11_MA_J_S20_L003_R1_001.fastq.gz\n", "@NB501800:128:HTYKFBGX5:3:11401:10896:1024 1:N:0:CGCTCATT+AGGATAGG\n", "\n", "11_MA_J_S20_L004_R1_001.fastq.gz\n", "@NB501800:128:HTYKFBGX5:4:11401:8259:1032 1:N:0:CGCTCATT+AGGATAGG\n", "\n" ] } ], "source": [ "for FILE in $(ls $DATA_DIR/*_MA_J_S20_*gz)\n", "do\n", " FIRST=$(zcat $FILE 2>/dev/null | head -1)\n", " echo $(basename $FILE)\n", " echo $FIRST\n", " echo\n", "done" ] } ], "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": 2 }