Site icon EdwardsLab

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!

And now create the SAM file

bowtie2 -f -p 4 -x outputfilename -U input_reads.fna > input.output.sam

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

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!

Exit mobile version