You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 13 Next »

As Nathan showed you yesterday, the main type of output from aligning reads to a databases is a binary alignment file, or BAM file.  These files are compressed, so they can't be viewed using standard unix file viewers such as more, less and head. Samtools allows you to manipulate the bam files - they can be converted into a non-binary format (SAM format specification here) and can also be ordered and sorted based on the quality of the alignment.  This is a good way to remove low quality reads, or make a bam file restricted to a single chromosome.

We'll be focusing on just a few of samtools functions in this series or exercises:

Since most aligners produce a bam file, we'll work on some basic manipulations of the bam files we produced from our alignments yesterday.  Most functionality while using bam files can be described as such:

  1.     SAM files are converted into BAM files (samstools view)
  2.     BAM files are sorted by reference coordinates (samtools sort)
  3.     Sorted BAM files are indexed (samtools index)
  4.     Sorted, indexed bam files are filtered (based on location, flags, mapping quality)

Take a look here for a detailed manual page for each function in samtools.  These steps presume that you are using a mapper/aligners such as bwa, which records mapped vs unmapped reads - make sure you check how the aligner writes it's output to sam/bam format, or you may get a strange surprise!

The code block below details some basic samtools functionality:

basic samtools functionality
samtools view -bH -o outfile_view.bam infile.bam #use the -c option to just count alignments
samtools sort infile.bam outfile.sorted.bam
samtools index aln.sorted.bam

First, logon to stampede and copy the file yeast_pairedend.bam to your scratch directory:

get bam file from Nathan's scratch
cds
mkdir samtools
cd samtools
cp /scratch/02423/nsabell/core_ngs/alignment/bam/yeast_pairedend.bam .

Sorting and Indexing a bam file

Now that we have a bam file, we need to index it. All bam files need an index, as the tend to be large and the index allows us to perform computationally complex operations on these files without it taking days to complete.

Exercise 1:  sort and index the file "yeast_pairedend.bam"

solution
module load samtools
samtools sort yeast_pairedend.bam yeast_pairedend_sort
samtools index yeast_pairedend_sort.bam

Use ls -lah to see what files you made and how large they are.

Samtools flags and mapping rate

We have a sorted, indexed bam file.  Now we can use other samtools functionality to filter this file and count mapped vs unmapped reads in a given region.  Samtools allows you to sort based on certain flags that are specified on page 4 on the sam format specification.  We'll focus on a couple, below.

Below are three useful flags to sort on, we'll be using the unmapped flag.

a few samtools flags
#sam specifications and common flag usage (from p4)
0x04 = unmapped
0x02 = properly aligned
0x40 = optical duplicate  #look at samtools rmdup if you need to remove these sequences.

Exercise: count unmapped reads vs total reads on chromosome III for the yeast_pairedend_sort.bam file you created above.  What proportion of the reads are mapped?

solution
module load samtools
samtools view -bh yeast_pairedend_sort.bam chrIII | wc -l  #
samtools view -bh -F 0X04 yeast_pairedend_sort.bam chrIII | wc -l  #

Filtering bam files based on mapped status and mapping quality

Mapping qualities are a measure of how likely a given sequence alignment to a location is correct. The idxstats function will produce a summary of how many reads correspond with each mapping quality score. The lowest score is a mapping quality of zero, or mq0 for short. The reads map to multiple places on the genome, and we can't be sure of where the reads originated.  To improve the quality of our data, we can remove these low quality reads from our sorted and indexed file.

Exercise 3:  Remove unmapped and mq0 reads from your bam file.

solution
module load samtools
samtools view -bH -F 0X04 -q 1 -o yeast_pairedend_sort.mapped.q1.bam  yeast_pairedend_sort.bam

Now that we have a bam file with only high-quality mapped reads, we are ready to manipulate this file using bedtools.

 

  • No labels