samtools sort -O sam -T yeast_pairedend -o yeast_pairedend.sort.sam yeast_pairedend.sam less yeast_pairedend.sort.sam
You may have noticed in the last help page that samtools sort can specify a BAM file as input or output, which is the smaller, binary form of a SAM file. This is a viable option if the file needs sorting - however, in many cases you may just want to compress a SAM file by conversion to BAM without any modifications. The utility samtools view provides a way of converting SAM files to BAM files directly. It also provides many, many other functions which we will discuss in the next section. To get a preview, execute samtools view without any other arguments. You will see:
This will produce a file named yeast_pairedend.bam.bai. Most of the time when an index is required, it will be automatically located provided it is in the same directory as the BAM file that it was produced from, and shares the same name up until the '.bai'' extension.
Now that we have a sorted, compressed, and indexed BAM file, we might like to get some simple statistics about the alignment run. For example, we might like to know how many reads aligned to each chromosome/contig. The samtools idxstats is a very simple tool that provides this information. If you type the command without any arguments, you will see that it literally could not be simpler - just type the following command: