Overview:

SAMtools is a suite of commands for dealing with databases of mapped reads. You'll be using it quite a bit throughout the course. It includes programs for performing variant calling (mpileup-bcftools).

Learning Objectives

  1. Familiarize yourself with SAMtools.
  2. Use SAMtools to identify variants in the E. coli genomes we mapped in the previous tutorial.

Calling variants in reads mapped by bowtie2

Right now, we'll be using it to call variants (find mutations) in the re-sequenced E. coli genome from the Mapping tutorial. You will need the output SAM files from that tutorial to continue here. If you wish to start this tutorial without completing the Mapping Tutorial, see the bottom section of this page for information about downloading canned data.

We assume that you are still working in the main directory called mapping data that you created on $SCRATCH.

Load SAMtools

Check if SAMtools is loaded and if not Load the SAMtools module.

Remember we use the the base command "module" to list the installed modules, find the available modules, and then load them to access their commands.

click here to check your work, or get the answer
module list samtools
module avail samtools
module load samtools

Can you figure out what version of samtools is loaded on TACC and where it is installed?

This should work:
samtools
which samtools

Prepare your directories

Since the $SCRATCH directory on lonestar is effectively infinite for our purposes, we're going to copy the relevant files from our mapping tutorial into a new directory for this tutorial. This should help you identify what files came from what tutorial if you look back at it in the future. Let's copy over just the read alignment file in the SAM format and the reference genome in FASTA format to a new directory called samtools_tutorial.

Check your work or get the answer here
cds
mkdir samtools_tutorial
cd samtools_tutorial
cp $SCRATCH/bowtie2MappingTutorial/bowtie2/SRR030257.sam .
cp $SCRATCH/bowtie2MappingTutorial/bowtie2/NC_012967.1.fasta .

 

Index the FASTA reference file

First, you need to index the reference file. (This isn't indexing it for read mapping. It's indexing it so that SAMtools can quickly jump to a certain base in the reference.)

Command to index the reference file for SAMtools
samtools faidx NC_012967.1.fasta

Take a look at the new *.fai file that was created by this command see if you have any idea what some of the numbers mean. 

Alternative to head/tail/cat for examining a file without causing programs to crash
less NC_012967.1.fasta.fai  # can exit with "q"

As you can see, the less command also works perfectly well with files that are not in danger of crashing anything without cluttering your terminal  with lines of a file.

Convert mapped reads from SAM to BAM, sort, and index

SAM is a text file, so it is slow to access information about how any given read was mapped. SAMtools and many of the commands that we will run later work on BAM files (essentially GZIP compressed binary forms of the text SAM files). These can be loaded much more quickly. Typically, they also need to be sorted, so that when the program wants to look at all reads overlapping position 4,129,888, it can easily find them all at once without having to search through the entire BAM file.

The following 3 commands are used to convert from SAM to BAM format, sort the BAM file, and index the BAM file. As each command requires the previous command to have been completed it makes more sense to run them on an iDEV node. If you want to submit them to the queue, separate them with a ";" to ensure that they run sequentially on the same node. Under no circumstances should you run this on the head node.

Do not run on head node

Many commands past this point are computationally intensive. You should run them through an idev shell or by qsub. We recommend idev for the tutorial.

Example command to start an idev shell
idev -m 60 -q development -A UT-2015-05-18
Commands to be executed in order...
samtools view -b -S -o SRR030257.bam SRR030257.sam
samtools sort SRR030257.bam SRR030257.sorted
samtools index SRR030257.sorted.bam
This is a really common sequence of commands, so you might want to add it to your personal cheat sheet.

 

Examine the output of the previous commands to get an idea of whats going on. Here are some prompts of how to do that:

  • List the contents of the output directory
    ls
    
    Expected output
    NC_012967.1.fasta      SRR030257.sorted.bam.bai
    NC_012967.1.fasta.fai  SRR030257.sam
    SRR030257.bam          SRR030257.sorted.bam
    
  • Samtools appends an extra .bam to whatever we put here, so it would have created SRR030257.sorted.bam.bam, and then we would have had to make a joke about the Flintstones.

  • Sure enough, it's the index file for the BAM file.

You might be tempted to gzip BAM files when copying them from one computer to another. Don't bother! They are already internally compressed, so you won't be able to shrink the file. On the other hand, compressing SAM files will save a fair bit of space.

Call genome variants

Now we use the mpileup command from samtools to compile information about the bases mapped to each reference position. The output is a BCF file. This is a binary form of the text Variant Call Format (VCF).

This is one command. Put it all on one line.
samtools mpileup -u -f NC_012967.1.fasta SRR030257.sorted.bam > SRR030257.bcf

What are all the options doing? Try calling samtools mpileup without any options to see if you can figure it out before clicking below to  

Optionpurpose
-ugenerates uncompressed BCF output
-f NC_012967.1.fasta.fai

faidx indexed reference sequence file

SRR030257.sorted.bamBAM input file to calcluate pileups from
> SRR030257.bcfDirect output to SRR030257.bcf

 

The samtools mpileup command will take a few minutes to run. As practice for a fairly common occurrence when working with the iDEV environment, once the command is running, you should try putting it in the background by pressing control-z and then typing the command bg so that you can do some other things in this terminal window at the same time. Remember, there are still many other processors available on this node!

Once the mpileup command is complete, convert the BCF file to a "human-readable" VCF file using the bcftools command (the which command will tell you where this command is located and examination of that path should tell you how you have access to it).

This is one command. Put it all on one line.
bcftools view -v -c -g SRR030257.bcf > SRR030257.vcf

What are these options doing?

Optionpurpose
viewspecific command to be executed by bcftools
-v

output potential variant sites only

-cSNP calling
-gcall genotypes at variant sites
SRR030257.bcf
input bcf file
> SRR030257.vcf
output as a vcf file

Take a look at the SRR030257.vcf file using less. It has a nice header explaining what the columns mean. Below this are the rows of data describing potential genetic variants.

Optional Exercises

Calling variants in reads mapped by BWA or Bowtie

Follow the same directions to call variants in the BWA or Bowtie mapped reads. Just be sure you don't write over your old files. Maybe create new directories like samtools_bwa and samtools_bowtie for the output in each case. You could also try running all of the commands from inside of the samtools_bwa directory, just for a change of pace.

Filtering VCF files with grep

VCF format has alternative Allele Frequency tags denoted by AF= Try the following command to see what values we have in our files.

grep AF1 SRR030257.vcf

Try looking at grep --help to see what you can come up with.

Here for answer
grep -v *something*  # The -v flag inverts the match effecitvely showing you everything that does not match your input
cat input.vcf | grep AF1=1 > output.vcf

Is not practical, since we will lose vital VCF formatting and may not be able to use this file in the future.

cat input.vcf | grep -v AF1=0 > output.vcf

Will preserve all lines that don't have a AF=0 value and is one way of doing this.

sed -i '/AF1=0/ d' input.vcf

Is a way of doing it in-line and not requiring you to make another file. (But it writes over your existing file!)

 

Comparing the results of different mappers using bedtools

Often you want to compare the results of variant calling on different samples or using different pipelines. Bedtools is a suite of utility programs that work on a variety of file formats, one of which is conveniently VCF format. It provides many ways of slicing, dicing, and comparing the information in VCF files. For example, we can use it to find out what predictions are the same and which are different from the variant calling on reads mapped with different programs if you generated VCF files for each one. Set up a new output directory and copy the respective VCF files to it, renaming them so that we know where they came from:

If you have done any of the optional other mapping tutorials, consider the following comparisons. Remember the use of cp -i (or cp -n on some newer linux versions) is useful to make sure you don't overwrite any existing files.
mkdir comparison
cp -i samtools_bowtie2/SRR030257.vcf comparison/bowtie2.vcf
cp -i samtools_bwa/SRR030257.vcf comparison/bwa.vcf
cp -i samtools_bowtie/SRR030257.vcf comparison/bowtie.vcf
cd comparison

Use the subcommands bedtools intersect and bedtools subtract we can find equal and different predictions between mappers. Try to figure out how to to do this on your own first. Hint: Remember that adding > output.vcf to the end of a command will pipe the output that is to the terminal into a file, so that you can save it.

load bedtools
module load bedtools
Finding common mutations.
bedtools intersect -a bowtie2.vcf -b bwa.vcf > common_bowtie2_bwa.vcf
Finding mutations that are unique for each mapper.
bedtools subtract -a bowtie2.vcf -b common_bowtie2_bwa.vcf > unique_bowtie2.vcf
bedtools subtract -a bwa.vcf -b common_bowtie2_bwa.vcf > unique_bwa.vcf

Further Optional Exercises

  • Which mapper finds more variants?
  • Can you figure out how to filter the VCF files on various criteria, like coverage, quality, ... ?
  • How many high quality mutations are there in these E. coli samples relative to the reference genome?

From here...

If you do not have the output from the Mapping tutorial, run these commands to copy over the output that would have been produced. Then, you can immediately start this tutorial!

cds
mkdir mapping
cd mapping
cp -r $BI/gva_course/mapping/bowtie .
cp -r $BI/gva_course/mapping/bwa . 
cp -r $BI/gva_course/mapping/bowtie2 .
  • No labels