How to import a Blosum Matrix in Python

biopython

There is rarely only one way to do something in this field. Importing a scoring matrix is one of them. There are also many different types of scoring matrices each better suited for different applications. The two Blosum (BLOcks SUbstitution Matrix) scoring matrices I use for protein sequence alignment are Blosum 62 and Blosum 50. Again, there are many ways to import a Blosum Matrix, but I will give you two:

  1. You can use BioPython:
    from Bio.SubsMat import MatrixInfo
    MatrixInfo.blosum50

    Or to assign your matrix to a new variable:
    from Bio.SubMat import MatrixInfo as matrixFile
    matrix = matrixFile.blosum62

  2. You can use “brute force” and parse a Blosum text file. After reading in the text file lines I put mine into a dictionary by { (protein 1, protein 2) : score } for further use. The text file will look something like this (This is a Blosum50 Matrix):


A R N D C Q E G H I L K M F P S T W Y V B J Z X *
A 5 -2 -1 -2 -1 -1 -1 0 -2 -1 -2 -1 -1 -3 -1 1 0 -3 -2 0 -2 -2 -1 -1 -5
R -2 7 -1 -2 -4 1 0 -3 0 -4 -3 3 -2 -3 -3 -1 -1 -3 -1 -3 -1 -3 0 -1 -5
N -1 -1 7 2 -2 0 0 0 1 -3 -4 0 -2 -4 -2 1 0 -4 -2 -3 5 -4 0 -1 -5
D -2 -2 2 8 -4 0 2 -1 -1 -4 -4 -1 -4 -5 -1 0 -1 -5 -3 -4 6 -4 1 -1 -5
C -1 -4 -2 -4 13 -3 -3 -3 -3 -2 -2 -3 -2 -2 -4 -1 -1 -5 -3 -1 -3 -2 -3 -1 -5
Q -1 1 0 0 -3 7 2 -2 1 -3 -2 2 0 -4 -1 0 -1 -1 -1 -3 0 -3 4 -1 -5
E -1 0 0 2 -3 2 6 -3 0 -4 -3 1 -2 -3 -1 -1 -1 -3 -2 -3 1 -3 5 -1 -5
G 0 -3 0 -1 -3 -2 -3 8 -2 -4 -4 -2 -3 -4 -2 0 -2 -3 -3 -4 -1 -4 -2 -1 -5
H -2 0 1 -1 -3 1 0 -2 10 -4 -3 0 -1 -1 -2 -1 -2 -3 2 -4 0 -3 0 -1 -5
I -1 -4 -3 -4 -2 -3 -4 -4 -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
* -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 1

Using argparse, here is the python code for importing the above Blosum50 text file into a referenceable dictionary.

if __name__ == ‘__main__’:
    parser.add_argument(‘-m’, dest = ‘matrixFile’)
    args = parser.parse_args()
    matrixFile = args.matrixFile

with open(args.matrixFile, ‘r’) as matrixFile:
    blosum_dict = populate_matrix(matrixFile)

def populate_matrix(matrixFile):
”’
reads in blosum matrix txt file and sets key: value pair as tuple(i, j) = score
”’
    plines = matrixFile.readlines()
    matrixFile.close()
    dictaa = {}
    aminoacidstring = lines[0]
    aminoacidstring = aminoacidstring.split()

    i = 1
    while i <= (len(lines)-1):
        row = lines[i]
        row = row.split()

        j = 1
        for character in row[1:25]:
            dictaa[aminoacidstring[i-1],aminoacidstring[j-1]] = character
            j+=1
        +=1

    return(dictaa)

From here, you can read in protein sequences and get the scores for each amino acid match using the dictionary you created, and likely do some more calculating, but this should get you started!

Full Smith Waterman alignment project available on GitHub:
https://github.com/cjthomasson/SmithWaterman, this project finds the alignment and max score for two protein sequences.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s