Skip to content

Instantly share code, notes, and snippets.

@Ken-Kuroki
Last active October 18, 2018 16:07
Show Gist options
  • Save Ken-Kuroki/5d9e022cdc9651a7411b0ffe955d43d3 to your computer and use it in GitHub Desktop.
Save Ken-Kuroki/5d9e022cdc9651a7411b0ffe955d43d3 to your computer and use it in GitHub Desktop.
Pairwise alignment with Biopython

You may want to align sequences with Biopython, and after reading its tutorial ( http://biopython.org/DIST/docs/tutorial/Tutorial.html#htoc87 ), you will try

>>> from Bio import Align
>>> aligner = Align.PairwiseAligner()

only to encounter

AttributeError: module 'Bio.Align' has no attribute 'PairwiseAligner'

You have to refer to a subsection of the tutorial ( http://biopython.org/DIST/docs/tutorial/Tutorial.html#htoc86 ) just before the section above. For example, if you want to emulate blastn command of NCBI BLAST+ with default settings (match: +2, mismatch: -1, gapopen: -5, gapextend: -2, according to Table C2 of BLAST User Manual ( https://www.ncbi.nlm.nih.gov/books/NBK279684/ )),

from Bio import pairwise2
from Bio.pairwise2 import format_alignment

def pairwise_wrapper(seq1, seq2):
    return int(pairwise2.align.globalms(seq1, seq2, 2, -1, -5, -2, score_only=True))

this will be the wrapper function which you can call just by giving a pair of sequences. You can remove score_only=True parameter to get the alignment itself in addition to alignment score.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment