0

I'm trying to import this table into a table in Python, how can I do this? I found a .MATRIX file that has the table but idk how to use that either. I'm new to python so any help would be appreciated

John Connor
  • 21
  • 1
  • 3

3 Answers3

1

A simple way is to just use biopython, which contains the major substitution matrices as a dict. This way you won't have to deal with file parsing.

from Bio.SubsMat import MatrixInfo
MatrixInfo.blosum50

Below the first few lines of MatrixInfo.blosum50

{('A', 'A'): 5,
 ('B', 'A'): -2,
 ('B', 'B'): 5,
 ('B', 'C'): -3,
 ('B', 'D'): 5,
 ('B', 'E'): 1,
 ('B', 'F'): -4,

See also this post.

Does this help you?

Community
  • 1
  • 1
user1981275
  • 13,002
  • 8
  • 72
  • 101
0

This is how you can do it. Assuming your matrix is in this format: http://alg.csie.ncnu.edu.tw/DataSet/BLOSUM%2050.txt

The printing method is brute force and just for testing.


#!/usr/local/bin/python2.7


def readmatrix(filename):
  handle = open(filename, "r")
  content = handle.readlines()
  handle.close()

  matrix = {}
  letters = []

  first = True
  for line in content:
    splitted = line.split()
    if first:
      for a in splitted:
        matrix[a] = {}
        letters.append(a)
      first = False
    else:
      a = splitted[0]
      for i in range(1, len(splitted)):
        b = letters[i-1]
        matrix[a][b] = splitted[i]

  return matrix

matrix=readmatrix("input.txt")

line = "    "
for a in matrix:
  line += a + "  "

print line

for a in matrix:
  line = a + " "
  for b in matrix[a]:
    score = matrix[a][b]

    if len(score) == 1:
      line += "  "
    elif len(score) == 2:
      line += " "
    line += score
  print line
Verena Haunschmid
  • 1,252
  • 15
  • 40
0

There is an updated and much smaller package called blosum which does exactly that.

import blosum as bl
matrix = bl.BLOSUM(62)                      # Load default matrix
matrix = bl.BLOSUM("path/to/blosum.matrix") #  Or custom one
val = matrix["AY"]