Pairwise Sequence Alignment using Biopython | by Vijini Mallawaarachchi | Towards Data Science

How similar are two sequences?
If you want to answer this question, you need to have a basic idea about sequence alignment. As described in my previous article, Sequence alignment is a method of arranging sequences of DNA, RNA, or protein to identify regions of similarity. The similarity being identified, may be a result of functional, structural, or evolutionary relationships between the sequences.
In this article, I will be walking you through pairwise sequence alignment. Furthermore, we will be trying out some coding with a cool python tool known as Biopython.
Pairwise sequence alignment is one form of sequence alignment technique, where we compare only two sequences. This process involves finding the optimal alignment between the two sequences, scoring based on their similarity (how similar they are) or distance (how different they are), and then assessing the significance of this score.
Before moving on to the pairwise sequence alignment techniques, let’s go through the process of scoring.
The basis of sequence alignment lies with the scoring process, where the two sequences are given a score on how similar (or different) they are to each other. The pairwise sequence aligning algorithms require a scoring matrix to keep track of the scores assigned. The scoring matrix assigns a positive score for a match, and a penalty for a mismatch.
Three basic aspects are considered when assigning scores. They are,
Match value — Value assigned for matching charactersMismatch value — Value assigned for mismatching charactersGap penalty — Value assigned for spaces
Match value — Value assigned for matching characters
Mismatch value — Value assigned for mismatching characters
Gap penalty — Value assigned for spaces
The Figure 3 given below shows how you can identify a match, mismatch and a gap among two sequences.
The first character of the two sequences is a match, as both are letter A. Second character of the first sequence is C and that of the second sequence is T. So, it is mismatch.
A space is introduced at the end of the second sequence to match with G. This space is known as a gap. A gap is the maximal contiguous run of spaces in a single sequence within a given alignment.
In general, a gap is expressed as a gap penalty function which is a function that measures the cost of a gap as a (possibly nonlinear) function of its length.
Consider that you are given two sequences as below.
X = x1 ... xi ... xnY = y1 ... yj ... ym
Global alignment: This method finds the best alignment over the entire lengths of the 2 sequences. What is the maximum similarity between sequence X and Y?Local alignment: This method finds the most similar subsequences among the 2 sequences. What is the maximum similarity between a subsequence of X and a subsequence of Y?
Global alignment: This method finds the best alignment over the entire lengths of the 2 sequences. What is the maximum similarity between sequence X and Y?
Local alignment: This method finds the most similar subsequences among the 2 sequences. What is the maximum similarity between a subsequence of X and a subsequence of Y?
In this method, we consider the entire length of the 2 sequences and try to match them to obtain the best alignment. It is obtained by inserting gaps (spaces) to X and Y until the length of the two sequences will be the same so that the two sequences are matched.
For example, consider the sequences X = ACGCTGAT and Y = CAGCTAT. One possible global alignment is,
AC-GCTGAT | ||  ||-CAGC-TAT
Not that we have included gaps so that the strings are aligned.
If we set a scoring scheme as match score = 1, mismatch score = 0 and gap penalty = 0, then the overall score for the above alignment will be,
Score = nMatch x 1 + nMismatch x 0 + nGap x 0      = 6x1 + 1x0 + 2x0      = 6
One of the algorithms that uses dynamic programming to obtain global alignment is the Needleman-Wunsch algorithm. This algorithm was published by Needleman and Wunsch in 1970 for alignment of two protein sequences and it was the first application of dynamic programming to biological sequence analysis. The Needleman-Wunsch algorithm finds the best-scoring global alignment between two sequences.
In this method, we consider subsequences within each of the 2 sequences and try to match them to obtain the best alignment.
For example, consider 2 sequences as X=GGTCTGATG and Y=AAACGATC. Characters in bold are the subsequences to be considered. The best local alignment is,
CTGAT (in X)| |||C-GAT (in Y)
Here, one gap is introduced in order to match the 2 subsequences.
If we set a scoring scheme as match score = 1, mismatch score = 0 and gap penalty = 0, then the overall score for the above alignment will be,
Score = nMatch x 1 + nMismatch x 0 + nGap x 0      = 4x1 + 0x0 + 1x0      = 4
One of the algorithms that uses dynamic programming to obtain local alignments within two given sequences is the Smith-Waterman algorithm. Smith and Waterman published an application of dynamic programming to find the optimal local alignments in 1981. This algorithm is similar to Needleman-Wunsch algorithm, but there are slight differences with the scoring process.
We will not go into details of the above two algorithms in this article.
Since we have a basic idea about pairwise sequence alignment and two useful algorithms, it is time to try out some coding.
In this article, I am going to introduce you to set of tools which we will use to play around with biological data from hereafter. Biopython is a set of tools written in Python which can be used for a variety of biological computations, simulations and analysis.
You can download and install Biopython from here. Make sure that you have Python 2.7, 3.4, 3.5, or 3.6 already installed. You can refer the Biopython Tutorial and Cookbook for further details on what we can do and how to use these tools.
Let’s try out some coding to simulate pairwise sequence alignment using Biopython. I will be using pairwise2 module which can be found in the Bio package. This module provides alignment functions to get global and local alignments between two sequences.
The names of the alignment functions follow the convention;
<alignment type>XX 
where <alignment type> is either global or local and XX is a 2 character code indicating the parameters it takes. The first character indicates the parameters for matches (and mismatches), and the second indicates the parameters for gap penalties.
The match parameters are:
CODE  DESCRIPTIONx     No parameters. Identical characters have score of 1, else 0.m     A match score is the score of identical chars, else mismatch      score.d     A dictionary returns the score of any pair of characters.c     A callback function returns scores.
The gap penalty parameters are:
CODE  DESCRIPTIONx     No gap penalties.s     Same open and extend gap penalties for both sequences.d     The sequences have different open and extend gap penalties.c     A callback function returns the gap penalties.
Let’s try out a few examples of pairwise sequence alignments using Bio.pairwise2 module.
Consider two sequences given below. We want to find out all the possible global alignments with the maximum similarity score.
X = ACGGGTY = ACG
Given below is the python code to get the global alignments for the given two sequences. Note how we have used Bio.pairwise2 module and its functionality.
By running the code, we can get all the possible global alignments as given below in Figure 5.
In this example, note that matching characters have been given 1 point. No points have been deducted for mismatches or gaps.
Consider the two sequences given in the previous example. We want to find out all the possible local alignments with the maximum similarity score.
Given below is the python code to get the local alignments for the given two sequences.
By running the code, we can get all the possible local alignments as given below in Figure 6.
In this example, note that matching characters have been given 1 point. No points have been deducted for mismatches or gaps.
In this example, we are going to change the scoring scheme and assign values for matches, mismatches and gaps. We will be considering the same two sequences as before. We want to find out all the possible global alignments with the maximum similarity score.
Matching characters are given 2 points, 1 point is deducted for each mismatching character. 0.5 points are deducted when opening a gap, and 0.1 points are deducted when extending it.
By running the code, we can get all the possible global alignments as given below in Figure 7.
Hope you enjoyed reading this article and learned something useful and interesting.
Since I’m still very new to this field, I would like to hear your advice. 😇
Thanks for reading... 😃