Question: using python for pairwise alignment
gravatar for jellevandewege
5.4 years ago by
jellevandewege90 wrote:


I'm writing a Python program and I have to do a pairwise alignment on several thousand DNA sequences. I looked at Biopython but I couldn't fine a function to do a pairwise alignment, this may be my mistake. The function should have gap penalty, gap open, gap extension and Smith Waterman or Needleman Wunsch. Can anyone tell me where I can find a good Python package for doing this kind of alignment? 

thanks in advance

pairwise alignment python • 14k views
ADD COMMENTlink modified 5.4 years ago by João Rodrigues2.5k • written 5.4 years ago by jellevandewege90 may be of some help


ADD REPLYlink written 5.4 years ago by ancient_learner610
gravatar for João Rodrigues
5.4 years ago by
João Rodrigues2.5k
Stanford University, U
João Rodrigues2.5k wrote:


BioPython does exactly what you are asking, you probably looked in the wrong place :) You should look at the Bio.pairwise2 module. See the following example for global pairwise alignment:

from Bio import pairwise2

from Bio.SubsMat import MatrixInfo as matlist

matrix = matlist.blosum62

gap_open = -10

gap_extend = -0.5

# Only using the first 60 aas for visualization reasons..



alns = pairwise2.align.globalds(p53_human, p53_mouse, matrix, gap_open, gap_extend)

top_aln = alns[0]

aln_human, aln_mouse, score, begin, end = top_aln

print aln_human+'\n'+aln_mouse



# Comparing with EBI NEEDLE output


ADD COMMENTlink written 5.4 years ago by João Rodrigues2.5k
gravatar for Mahmoud Mabrouk
5.4 years ago by
Germany / Berlin / Technische Universitaet Berlin
Mahmoud Mabrouk0 wrote:

Hi jellevandewege,

You can use the Bio.Emboss python package for Needleman Wunsch alignment (It is essentially a wrapper around the program).

Here is a short example copied from my code base:

from Bio.Emboss.Applications import NeedleCommandline
from Bio import AlignIO

needle_cli = NeedleCommandline(asequence=seq_fname1, \
                               bsequence=seq_fname2, \
                               gapopen=10, \
                               gapextend=0.5, \

"""This generates the needle file"""
"""That parses the needle file, aln[0] and aln[1] contain the aligned
first and second sequence in the usual format (e.g. - for a gap)"""
aln =, "emboss") 





ADD COMMENTlink modified 5.4 years ago • written 5.4 years ago by Mahmoud Mabrouk0
Please log in to add an answer.


Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1297 users visited in the last hour