2015-02-23 107 views
1

我需要创建一个计算LCS然后打印出来的矩阵。这是我的代码,但我无法与打印功能(不知道如何让LCSmatrix值到打印)Python:构造和打印矩阵

def compute_LCS(seqA, seqB): 

    for row in seqA: 
    for col in seqB: 
     if seqA[row] == seqB[col]: 
      if row==0 or col==0: 
       LCSmatrix(row,col) = 1 
      else: 
       LCSmatrix(row,col) = LCS(row-1,col-1) + 1 
     else: 
      LCSmatrix(row,col) = 0 
return LCSmatrix 


def printMatrix(parameters...): 
    print ' ', 
    for i in seqA: 
      print i, 
    print 
    for i, element in enumerate(LCSMatrix): 
      print i, ' '.join(element) 

matrix = LCSmatrix 


print printMatrix(compute_LCS(seqA,seqB)) 

任何帮助将非常感激。

回答

0

试试这个:

seqA='AACTGGCAG' 
seqB='TACGCTGGA' 

def compute_LCS(seqA, seqB): 
    LCSmatrix = [len(seqB)*[0] for row in seqA] 
    for row in range(len(seqB)): 
     for col in range(len(seqA)): 
      if seqB[row] == seqA[col]: 
       if row==0 or col==0: 
        LCSmatrix[row][col] = 1 
       else: 
        LCSmatrix[row][col] = LCSmatrix[row-1][col-1] + 1_ 
      else: 
       LCSmatrix[row][col] = 0 
    return LCSmatrix 

def printMatrix(seqA, seqB, LCSmatrix): 
    print ' '.join('%2s' % x for x in ' '+seqA) 
    for i, element in enumerate(LCSmatrix): 
     print '%2s' % seqB[i], ' '.join('%2i' % x for x in element) 

matrix = compute_LCS(seqA, seqB) 
printMatrix(seqA, seqB, matrix) 

上述方法产生:

A A C T G G C A G 
T 0 0 0 1 0 0 0 0 0 
A 1 1 0 0 0 0 0 1 0 
C 0 0 2 0 0 0 1 0 0 
G 0 0 0 0 1 1 0 0 1 
C 0 0 1 0 0 0 2 0 0 
T 0 0 0 2 0 0 0 0 0 
G 0 0 0 0 3 1 0 0 1 
G 0 0 0 0 1 4 0 0 1 
A 1 1 0 0 0 0 0 1 0 
+0

哇哦,太感谢你了!我现在明白你是如何通过打印功能的参数。我还会看到如何为compute_LCS函数创建第一条LCSmatrix线。 – 2015-02-23 06:50:05