A Full Understanding of Pairwise Alignment with Biopython
By: Eugenia Anello
Tags: Sequence, Python, Protein, Bioinformatics, Biopython

A Full Understanding of Pairwise Alignment with Biopython

Comparison with EMBOSS Needle Tools for Global and Local Alignments

Figure 1: Photo by geralt on pixabay

Biopython is an international association of developers of freely available Python tools for computational molecular biology. In particular, it allows to deal with Sequence Alignments, that are methods able to compare and detect similarities between Biological Sequences. Sequence similarity means that the sequences compared have similar or identical residues at the same positions of the alignment.

In this article, I’m going to focus on the Pairwise Alignment. My goal is to replicate the same results obtained with Biopython with respect to Emboss Needle, an efficient webserver that provides many tools for sequence alignment. When we need to handle a huge volume of data, it’s preferable to have a good knowledge of Biopython to obtain the output faster than manually doing it on a website. For prior knowledge, I suggest you read my previous article, in which I explained the concepts of Similarity, Identity, Global and Local Alignments in a simple way. These concepts will be useful while I try to apply them with Biopython. Before talking about pairwise alignment, I’m going to introduce the standard sequence class that deals with sequences.

Seq Objects

Biological Sequences are surely the central object in Bioinformatics and are essentially strings of letters like AGTACACTGGT, which is the most common way that sequences are seen in biological file formats. Biopython deals with the sequences through the Seq object. In most ways, we can deal with Seq objects as if they were normal Python strings, for example getting the length, or iterating over the elements:

from Bio.Seq import Seq my_seq = Seq("ASEIELV") for i, letter in enumerate(my_seq):

print("%i %s" % (i, letter)) '''

0 A

1 S

2 E

3 I

4 E

5 L

6 V

''' len(my_seq) #7 my_seq[4:12]

#Seq('ELV')

Although Seq object supports different methods from normal Python strings:

obtain the complement or reverse complement of a Seq object

of a Seq object do the transcription

translate mRNA into the corresponding protein sequence

For nucleotide sequences, we can easily obtain the complement or reverse complement of a Seq object using the methods complement and reverse_complement:

from Bio.Seq import Seq

my_seq = Seq("GATCGAT") print(my_seq.complement())

#CTAGCTA print(my_seq.reverse_complement())

#ATCGATC

To transcribe the coding strand into the corresponding mRNA, we’ll use the transcribe method:

coding_dna = Seq("ATGGCCATT")

messenger_rna = coding_dna.transcribe()

print(messenger_rna)

#AUGGCCAUU

Now let’s translate this mRNA into the corresponding protein sequence using the method translation:

coding_dna.translate()

Seq('MAI')

SeqRecord Object

The SeqRecord allows to hold a sequence (as a Seq object) with identifiers (ID and name), description and optionally annotation and sub-features. It is the basic data type for the Bio.SeqIO sequence input/output interface. In the example below we can see the informations offered by SeqRecord Object:

seq : The sequence itself, typically a Seq object

: The sequence itself, typically a Seq object id : The primary ID used to identify the sequence

: The primary ID used to identify the sequence name : a common name/id for the sequence

: a common name/id for the sequence description: a “human” readable description

from Bio.Seq import Seq

from Bio.SeqRecord import SeqRecord record = SeqRecord(

Seq("MKQHKAMIVALIVICITAVVAALVTRKDLCEVHIRTGQTEVAVF"),

id="YP_025292.1",

name="HokC",

description="toxic membrane protein, small",

)

print(record) '''

ID: YP_025292.1

Name: HokC

Description: toxic membrane protein, small

Number of features: 0

Seq('MKQHKAMIVALIVICITAVVAALVTRKDLCEVHIRTGQTEVAVF')'''

Pairwise alignment

The pairwise alignment is the process of aligning two sequences to each other by optimizing the score between them. Bio.pairwise2 is the module for these type of alignments and contains the same algorithms as water (local) and needle (global) from the EMBOSS suite and returns the same results if we use the same default parameters.

The names of the alignment functions in this module 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:

x = No parameters. Identical characters have a score of 1, otherwise 0.

= No parameters. Identical characters have a score of 1, otherwise 0. m = A match score is the score of identical chars, otherwise mismatch score.

= A match score is the score of identical chars, otherwise mismatch score. d = A dictionary returns the score of any pair of characters

= A dictionary returns the score of any pair of characters c = A callback function returns scores

The gap penalty parameters are:

x = No gap penalties

= No gap penalties s = Same open and extend gap penalties for both sequences

= Same open and extend gap penalties for both sequences d = The sequences have different open and extend gap penalties

= The sequences have different open and extend gap penalties c = A callback function returns the gap penalties

All alignment functions have the following arguments:

Two sequences : strings, Biopython sequence objects or lists. Lists are useful for supplying sequences which contain residues that are encoded by more than one letter.

: strings, Biopython sequence objects or lists. Lists are useful for supplying sequences which contain residues that are encoded by more than one letter. penalize_end_gaps : boolean. Whether to count the gaps at the ends of an alignment. By default, they are counted for global alignments but not for local ones. Setting penalize_end_gaps to (boolean, boolean) allows you to specify for the two sequences separately whether gaps at the end of the alignment should be counted.

: boolean. Whether to count the gaps at the ends of an alignment. By default, they are counted for global alignments but not for local ones. Setting to (boolean, boolean) allows you to specify for the two sequences separately whether gaps at the end of the alignment should be counted. gap_char : string (default: '-' ). Which character to use as a gap character in the alignment returned. If your input sequences are lists, you must change this to ['-']

: string (default: ). Which character to use as a gap character in the alignment returned. If your input sequences are lists, you must change this to open : gap penalty when a gap is opened. It should be negative.

: gap penalty when a gap is opened. It should be negative. extend : gap penalty when a gap is extended. It should be negative.

Global Alignment

The Global Alignment is a method that aligns and compares two sequences along with their entire length and comes up with the best alignment that displays the maximum number of nucleotides or amino acids aligned.

Example 1: