This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Custom substitution matrix in biopython pairwise2 aligner

Is there a way to use custom matrix for nucleotide aligner in Biopython pairwise2 module? I want to decrease penalties for A->G, C->T substitutions.

import Bio
from Bio import pairwise2

seq1='AGTCGATCATCGGA'
seq2='ATTAGATCATCGGGGA'

alignment = pairwise2.align.localms(seq1, seq2, 2,-1,-2,-1,one_alignment_only=True)
print(format_alignment(*alignment[0]))

AGTCGATCATCGGA--
|.|.|||||||||
ATTAGATCATCGGGGA
  Score=20
biopython pairwise2 alignment

1 answer

how about trying somethig like this

import Bio
from Bio import pairwise2
from Bio.pairwise2 import format_alignment

seq1 = 'AGTCGATCATCGGA'
seq2 = 'ATTAGATCATCGGGGA'


match_dic = {}
match_dic[('A', 'A')] = 2
match_dic[('A', 'C')] = -1
match_dic[('A', 'T')] = -1
match_dic[('A', 'G')] = 1
match_dic[('C', 'A')] = -1
match_dic[('C', 'C')] = 2
match_dic[('C', 'T')] = 1
match_dic[('C', 'G')] = -1
match_dic[('G', 'A')] = -1
match_dic[('G', 'C')] = -1
match_dic[('G', 'T')] = -1
match_dic[('G', 'G')] = 2
match_dic[('T', 'A')] = -1
match_dic[('T', 'C')] = -1
match_dic[('T', 'T')] = 2
match_dic[('T', 'G')] = -1

alignment = pairwise2.align.localds(
    seq1, seq2, match_dic, -2, -1, one_alignment_only=True)
print(alignment[0])
print(format_alignment(*alignment[0]))

Log in to answer this question.