Creating indexed bam files from bowtie alignments

Searching with bowtie Creating indexed bam files from sam files is something we need to do all the time. This is just a hand full of simple commands to remind you how to do that.

We also use the PySAM module to extract some information about the alignments.

We start by building our database to search against:

bowtie2-build database.fasta outputfilename

NOTE: Make sure that you use bowtie2-build for bowtie2 indices OR bowtie-build for bowtie indices. They are not backwards (forwards) compatible!

  • database.fasta is our database file in fasta format
  • outputfilename is the base name for the bowtie indexed database

And now create the SAM file

bowtie2 -f -p 4 -x outputfilename -U input_reads.fna > input.output.sam
  • -f means the input is fasta (use -q for fastaq)
  • -p is the number of processors to use: increase this on rambox!
  • -x is the bowtie index file from bowtie2-build
  • -U is the file to search

Now we have a sam file, we need to convert that to a binary format bam file. We need to sort the file before we can build the index, so we’ll do all of it in one step. We’ll use samtools for these steps. See Dave’s Wiki for more commands.

samtools view -bS input.output.sam | samtools sort - input.output
  • -b means make a bam file
  • -S means input is a sam file
  • this automatically adds .bam to the filename

Finally, we just need to build an index on that file so we can extract the information from it:

samtools index input.output.bam input.output.bai

Now you can use the bam file with PySAM and other tools!