Overview

This session will be more challenging if you are still new to Unix. However, it’s main aim is to demonstrate that Unix is much more than an environment in which you can execute big programmes like bwa or samtools. Rather, with it’s suite of text searching, extraction and manipulation programmes, it is itself a powerful tool for the many small bioinformatic tasks that you will encounter during everyday work. This module expects that you have done the steps in the Basic Unix module, i. e. it expects that you have already created certain folders and have copied the example data into your account. There are still small (hopefully not large) bugs lurking in this protocol. Please help improve it by correcting those mistakes or adding comments to the talk page. Many thanks.

TASK 3: I have downloaded my illumina read data. Now I want to know how many reads my sequence run has yielded.

gives you the number of lines in your sequence file, which is the number of reads times four for fastq format. Note, you can usually avoid uncompressing data files, which saves disk space.

TASK 4: I have a .fastq file with raw sequences from a RAD library. How can I find out how many of the reads contain the correct sequence of the remainder of the restriction site?

Let's assume you had 5bp long barcode sequences incorporated into the single-end adapters, which should show up at the beginning of each sequence read. Let's also assume that you have used the restriction enzyme SbfI for the creation of the library, which has the following recognition sequence: CCTGCAGG . So the correct remainder of the restriction site that you expect to see after the 5bp barcode is TGCAGG. First have a look at your fastq file again:

$ zless -N Unix_tut_sequences.fastq.gz

Each sequence record contains four lines. The actual sequences are on the 2nd, 6th, 10th, 14th, 18th line and so on. The following can give you the count:

This is a pipeline which first uncompresses your sequence read file and pipes it into the perl command, which extracts only the DNA sequence part of each fastq record. $. in perl stands for the current line number, %4 returns modulo 4 of the current line number minus 2 (the modulo operator returns 0 if the result of the division is an integer (i. e. if the line number is a multiple of 4) and 1, 2, 3 etc. otherwise. Zero is equivalent to FALSE and any integer other than zero is equivalent to TRUE). Get it?

Grep searches each line from the output of the perl command for the regular expression given in quotation marks. ^ stands for the beginning of a line. A dot stands for any single character. There are five dots, because the barcodes are all 5 base pairs long. The -c switch makes grep return the number of lines in which it has found the search pattern at least once.

TASK 5: I have split my reads by barcode and I have quality filtered them. Now I want to know how many reads I have left from each (barcoded) individual. How can I find that out?

This bash for loop goes sequentially over each file in the current directory which has the file ending .fq. It prints that file name to the output file retained. The >> redirection character makes sure that all output is appended to the file retained. Otherwise only the output from the last command in the loop and from the last file in the list of files would be stored in the file retained. The ampersand & at the end of the command line sends it into the background, which means you get your command line prompt back immediately while the process is running in the background.

TASK 6: I have 30 output files from a programme, but their names are not informative. I want to insert the keyword cleaned into their names? How can I do this?

Renaming files is a very common and important task. I’ll show you a simple and fast way to do that. There are as always many ways to solve a task. First, you could type 30 mv commands → that’s the fastest way to rename a single file but doing that 30 times is very tedious and error prone. Second, you could use a bash for loop as above in combination with echo and the substitution command sed, like this:

but that's a bit tricky if you are not using sed every day. Note the two backticks characters: one before echo, the other right before the last semicolon. Everything in backticks will be evaluated by bash and then replaced by it’s output. So in this case it’s the modified file name that is provided as second argument to the mv command. If you want to find out what the sed command does, take a look at the beginning of this sed tutorial.

The best way, however, to solve the task is by downloading the perl rename script (note, this is not the system rename command that comes with Unix, which has very limited capabilities), put it into a directory that is in your PATH (e. g. ~/prog) and make that text file executable with chmod.
You can download Larry Wall's rename.pl script from here with wget:

You already know that bash expands *.out at the end of the command line into a list of all the file names in the current directory that end with .out. So the command does the renaming on all the 30 files we created. Let’s look at the stuff in single quotes. s turns on substitution, since we are substituting old file names with new file names. The forward slashes / separate the search and the replacement patterns. The search pattern begins with ^, which stands for the beginning of the file name. The dot stands for any single character. The + means one or more of the preceding character. So .+ will capture everything from the beginning of the file name until .out. In the second pair of brackets we needed to escape the dot with a backslash to indicate that we mean dot in its literal sense. Otherwise, it would have been interpreted as representing any single character. Anything that matches the regular expressions in the brackets will be automatically saved, so that it can be called in the replacement pattern. Everything that matches the pattern in the first pair of brackets is saved to $1, everything that matches the pattern in the second pair of brackets is saved to $2. Fore more on regular expressions, look up:

$ man grep
$ man perlrequick

TASK 7: I have got a text file originating from a Windows or an old Mac text editor. But when I open it in less, all lines are concatenated into one line and with repeatedly the strange ^M character in it. How can I fix this?

Unix has only linefeeds, the old Mac’s had only carriage returns and Windows uses both characters together to represent one return character. The following will remove the carriage return from the end of lines, leaving only linefeeds:

Note, most Unix programmes print their output to STDOUT, which is the screen. If you want to save the output, it needs to be redirected into a new file. Never redirect output back into the input file, like this:

The last two programmes are available on iceberg, but are not by default included in a Unix system. You would have to install them yourself. Note also, that dos2unix and mac2unix do in place editing, i. e. the old version will be overwritten by the new version.

TASK 8: I have just mapped my reads against the reference sequences and I got a BAM file for each individual. How can I find out the proportion of reads from each individual that got mapped successfully?

Note: it seems that the samtools view command should be able to do that with the -f and -c switches. However, in my trials it only returned the total number of reads in the BAM file, i .e including those that did not get mapped. Fortunately, this is a really easy Unix task. First let’s have a look at the .bam file:

If that fits on your screen without line wrapping ... lucky bastard! If not, just turn off line wrapping in less:

$ samtools view alignment.bam | less -S

The second column contains a numerical flag which indicates the result of the mapping for that read. The flag 0 stands for a successful mapping of the read. So in order to get the number of reads that got mapped successfully we need to count the number of lines with zeros in their second column. Let’s cut out the second column of the tab delimited .bam file:

$ samtools view alignment.bam | cut -f 2 | less
$ man cut

Next, we have to sort this column numerically in order to collapse it into unique numbers:

With the -c switch to uniq the output is a contingency table of the flags from column 2 that can be found in your alignment file. However, some reads with a 0 flag in the second column still have a mapping quality of 0 (don’t ask me why), which is in the 5th column. So, in order to get a count of the number of reads that got mapped successfully with a mapping quality above 0, use gawk:

$ samtools view alignment.bam | awk '$2==0 && $5>0' | wc -l
$ man awk

Gawk is the GNU version of awk. Macs get only shipped with awk. Both are almost identical except that awk can’t handle large files. $2 stands for the content in the second column, $5 for the fifth and && means logical AND. Awk prints only those lines to output which match our conditions. We then simply count those lines.

TASK 9: I have got a big multi-fasta file with thousands of sequences. How can I extract only those fasta records whose sequences contain at least 3 consecutive repeats of the dinucleotide AG?

Believe it or not, this task can be solved with a combination of Unix commands, i. e. no programming is required. Let’s approach the task step by step. First have a look at the input file:

$ cd ~/NGS_workshop/Unix_module/TASK_9
$ less multi_fasta.fa

We could just use the programme grep to search each line of the fasta file for our pattern (i. e. 3 consecutive AG), but since grep is line based we would lose the fasta headers and some part of the sequence in the process. So, we somehow have to get the fasta headers and their corresponding sequences on the same line.

$ tr '\n' '@' < multi_fasta.fa | less

Everything is on one line now (but don’t do this with really large files as this causes the whole file to be read into memory).

$ tr '\n' '@' < multi_fasta.fa | sed 's/>/#>/g' | less

Note the g at the end of the sed command, which stands for global. With the global option sed does the replacement for every occurrence of a search pattern in a line, not just for the first.

$ tr '\n' '@' < multi_fasta.fa | sed 's/>/#>/g' | tr '#' '\n' | less

Ok, we are almost ready to search, but we first need to get rid of the @ sign in the middle of the sequences.

In the sed command the search and the replacement patterns are enclosed in forward slashes /.
Anything that matches the pattern between \( and \) will be stored by sed and can be called again in the replacement pattern. The square brackets [ ] mean match any one character that is enclosed by them. In the replacement pattern \1 stands for the base before the @ sign, \2 stands for the base after the @ sign. Finally, let’s search for the microsats:

Note the use of egrep instead of just grep for extended regular expressions. The search pattern is enclosed in quotations marks. Our sequences are delimited by @’s on each side. The dot stands for any single character. The asterisk * means zero or more of the preceding character. So .* could match exactly nothing or anything else. The {3,} means 3 or more times the preceding character. Without the brackets around AG this would only refer to the the G in AG.
Now that we have all sequences with ≥3x AG microsats, let’s get them back into fasta format again:

The grep search pattern contains the regular expression symbols for the beginning and the end of the line with nothing in between → an empty line. The -v switch inverts the matching.

Open AGAGAG.fa in less, and type:

/(AG){3,}

and hit Enter. Each sequence should have a highlighted match.
The syntax of regular expressions for grep, sed and less (which are very similar) is one of the most useful things you can learn. The most flexible regular expressions, however, are provided by Perl. Finally, let’s count how many sequences are in the output file:

$ grep -c “^>” AGAGAG.fa

TASK 10: I have a large table from the output of the programme stacks containing the condensed information for each reference tag. Each column is tab delimited. The first column contains the tag ids. Another column the reference sequence for the tag. How can I create a multi-fasta file from this table with the tag ids as fasta headers?

In the sed command \(.*\) captures a whole line, which we call in the replacement pattern with \1. Finally we have to replace the tab that separates each header from its sequence by a return character in order to bring each sequence on the line below its header.

With the sort command we turn on numerical sort with -n and the -k switch lets us specify on which column the sorting should be done (by default, sort would use the whole line for sorting).

TASK 11: I want to map the reads from 96 individuals against a reference sequence (e. g. partial or full reference genome or transcriptome, etc.). A mapping programme takes one of the 96 input files and tries to find locations for each read in the reference sequence. How can I parallelise this task and thus get the job done in a fraction of the time?

Note, this task makes use of a computer cluster running the job scheduler SGE. It also requires that you have the mapping programme stampy installed in a location that is in your PATH. Check this with:

… that’s 96 × 4 + 31 × 8 + 96 × 12 = 1,784 CPU’s !!! Let’s try to use up to 96 of them at the same time.

$ cd ~/Genomics_workshop/Unix_module/TASK_11
$ ll ind_seqs

There are 96 fastq files with the reads from 96 individuals in this folder. All input files contain a number from 1 to 96 in their names. Otherwise, their names are identical. We now want to map the reads from those individuals against the reference_seq. We have already prepared a so-called array job submission script for you. Let’s have a look at it.

$ nano array-job.sh

The lines starting with #$ are specific to the SGE. The first requests 2 Gigabytes of memory. the second asks for slightly less than 1 hour to complete each task of the job. Any job or task taking less than 8 hours will be submitted to the short queue by the SGE and there is almost no waiting time for this queue. The next two lines specify that you get an email when each task has been begun and ended. -j y saves STDERR and STDOUT from each task in one file. Change those two lines appropriately:

This is the actual command line that executes stampy, our mapping programme. The -M switch to stampy takes the input file, the -o switch the output file name. Submitting this array job script to the SGE scheduler is equivalent to submitting 96 different job scripts, each with an explicit number instead of $SGE_TASK_ID. After exiting nano let’s submit the job.

$ qsub array_job.sh
$ Qstat

This is the end of the advanced Unix session. If you’ve made it until here CONGRATULATIONS !!!

Notes

Please feel free to post comments, questions, or improvements to this protocol. Happy to have your input!

Please sign your name to your note by adding '''*~~~~''': to the beginning of your tip.