Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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 (if not already loaded).

Expand
titleClick here for a hint without the answer

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.

Code Block
languagebash
titleclick here to check your work, or get the answer
collapsetrue
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?

code
Code Block
Expand
No, give me the commands...
No, give me the commands...languagebash
titleThis should work:
collapsetrue
samtools
which samtools

Prepare your directories

From inside your main mapping directory, create a new output directory called samtools_bowtie2 or whatever makes sense to you.

LetSince 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 this new directory, so that we don't have so many files cluttering our space.

...

a new directory called samtools_tutorial.

Code Block
languagebash
titleCheck your work or get the answer here
collapsetrue
cds
mkdir samtools_tutorial
cd samtools_tutorial
cp $SCRATCH/bowtie2MappingTutorial/bowtie2/SRR030257.sam 
samtools_bowtie2
.
cp $SCRATCH/bowtie2MappingTutorial/bowtie2/NC_012967.1.fasta 
samtools_bowtie2
.

 

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.)Then run this command

Code Block
languagebash
titleCommand to index the reference file

...

for SAMtools
Code Block
samtools faidx samtools_bowtie2/NC_012967.1.fasta

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

...

Code Block
languagebash
titleAlternative to head/tail/cat for examining a file without causing programs to crash
collapsetrue
less NC_012967.1.fasta.fai 
Hint: Type q to exit less.
 # can exit with "q"

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.

Warning
titleDo 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.

Code Block
titleExample command to start an idev shell
idev -m 60 -q development -A CCBB

 

Convert from SAM to BAM format.

code
Code Block
Code Block
languagebash
titleCommands to be executed in order...
samtools view -b -S -o 
samtools_bowtie2/
SRR030257.bam 
samtools_bowtie2/
SRR030257.sam

Sort and index the BAM file.


samtools sort 
samtools_bowtie2/
SRR030257.bam 
samtools_bowtie2/
SRR030257.sorted
samtools index 
samtools_bowtie2/
SRR030257.sorted.bam
Tip
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:

  • expandCheck that Check that
    Expand
    titleWhat new files were created by these commands?
    Code Block
    language
    bash
    Code Block
    titleList the contents of the output directory
    ls samtools_bowtie2
    
    Code Block
    titleExpected output
    NC_012967.1.fasta      SRR030257.sorted.bam.bai
    NC_012967.1.fasta.fai  SRR030257.sam
    SRR030257.bam          SRR030257.sorted.bam
    
  • Expand
    titleWhy didn't we name the
    output 
    output SRR030257.sorted.bam
     in the 
    in the samtools sort
     command
    command?
    Expand
    Answer...Answer...

    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.

  • Expand
    titleCan you guess what a *.bai file is?
    expand
    Answer...Answer...

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

...

Tip
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. Output BCF The output is a BCF file. This is a binary form of the text Variant Call Format (VCF).

Code Block
titleThis is *one* command. Put it all on one line.
samtools mpileup -u -f samtools_bowtie2/NC_012967.1.fasta samtools_bowtie2/SRR030257.sorted.bam > samtools_bowtie2/SRR030257.bcf

What are all the options doing? 

The samtools mpileup command will take a few minutes to run. You might consider 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 – like starting to run variant calling on the BWA or bowtie mapping results. Remember, there are still many other processors available on this node!

...

Code Block
titleThis is *one* command. Put it all on one line.
bcftools view -v -c -g samtools_bowtie2/SRR030257.bcf > samtools_bowtie2/SRR030257.vcf

What are these options doing?

...