def loadMatrix(fn='blosum50.txt'): FH = open(fn,'r') data = FH.read() FH.close() L = data.strip().split('\n') # matrix has metadata lines beginning w/'#' # also has extra rows,cols for 'BZX*' L = [e for e in L if not e[0] in '#BZX*'] # the last 4 cols are also 'BZX*' L = [e.split()[:-4] for e in L] aaNames = L.pop(0) # each row also starts with the AA name L = [t[1:] for t in L] M = dict() for i in range(len(aaNames)): for j in range(len(aaNames)): k = aaNames[i] + aaNames[j] M[k] = int(L[i][j]) return aaNames,M def showM(M,aaNames): #each element takes up 4 spaces print ' ' + ' '.join(aaNames) for i,aa1 in enumerate(aaNames): print aa1, L = list() for j in range(i+1): aa2 = aaNames[j] k = aa1 + aa2 L.append(str(M[k]).rjust(4)) print ''.join(L) for aa1 in aaNames: for aa2 in aaNames: assert M[aa1+aa2] == M[aa2+aa1],aa1+aa2
if __name__ == '__main__': aaNames,M = loadMatrix(fn='blosum50.txt') showM(M,aaNames) |