【发布时间】:2014-05-01 00:27:37
【问题描述】:
我正在使用 Python 使用 Smith-Waterman algorithm 生成动态规划矩阵。
这是我目前所拥有的:
def score(base1,base2):
base1=base1.upper()
base2=base2.upper()
if base1 not in 'ACTG' or base2 not in 'ACTG':
print 'Not DNA base!'
sys.exit()
elif base1==base2:
return 3
elif base1+base2=='AG' or base1+base2=='GA':
return -1
elif base1+base2=='CT' or base1+base2=='TC':
return -1
else:
return -2
import sys
seq1 = sys.argv[1]
seq2 = sys.argv[2]
mRows = len(seq1)
nCols = len(seq2)
gap = int(sys.argv[3])
matrix = []
# generate empty matrix
for x in range(mRows + 1):
matrix.append([])
for y in range(nCols + 1):
matrix[x].append(0)
for i in range(1, mRows + 1):
for j in range(1, nCols + 1):
dscore = matrix[i-1][j-1] + score(seq1[i-1], seq2[j-1])
vscore = matrix[i-1][j] + gap
hscore = matrix[i][j-1] + gap
matrix[i][j]=max(0, vscore, hscore, dscore)
有输入: sw.py ATGCAT ACCT -1
我得到矩阵输出:
0 0 0 0 0
0 0 0 0 0
0 0 0 0 3
0 0 0 0 2
0 0 0 0 1
0 0 0 0 0
0 0 0 0 3
通过一些故障排除,我能够看到在嵌套的 for 循环中,只有使用 j 的最终值(对于这个特定输入,4)的分数存储在矩阵中,即只是最后一列。
我的问题是为什么会发生这种情况,我该如何解决?为什么for循环会跳回而不继续变量scores?
我的一些故障排除:
for i in range(1, mRows + 1):
for j in range(1, nCols + 1):
print 'this is i', i
print 'this is j', j
print 'seq1', seq1[i-1], 'seq2', seq2[j-1]
dscore = matrix[i-1][j-1] + score(seq1[i-1], seq2[j-1])
vscore = matrix[i-1][j] + gap
hscore = matrix[i][j-1] + gap
matrix[i][j]=max(0, vscore, hscore, dscore)
print 'Vscore = ', vscore
print 'Hscore = ', hscore
print 'Dscore = ', dscore
print '\n'
给予:
this is i 1
this is j 1
seq1 A seq2 A
this is i 1
this is j 2
seq1 A seq2 C
this is i 1
this is j 3
seq1 A seq2 C
this is i 1
this is j 4
seq1 A seq2 T
Vscore = -1
Hscore = -1
Dscore = -2
this is i 2
this is j 1
seq1 T seq2 A
this is i 2
this is j 2
seq1 T seq2 C
this is i 2
this is j 3
seq1 T seq2 C
this is i 2
this is j 4
seq1 T seq2 T
Vscore = -1
Hscore = -1
Dscore = 3
this is i 3
this is j 1
seq1 G seq2 A
this is i 3
this is j 2
seq1 G seq2 C
this is i 3
this is j 3
seq1 G seq2 C
this is i 3
this is j 4
seq1 G seq2 T
Vscore = 2
Hscore = -1
Dscore = -2
this is i 4
this is j 1
seq1 C seq2 A
this is i 4
this is j 2
seq1 C seq2 C
this is i 4
this is j 3
seq1 C seq2 C
this is i 4
this is j 4
seq1 C seq2 T
Vscore = 1
Hscore = -1
Dscore = -1
this is i 5
this is j 1
seq1 A seq2 A
this is i 5
this is j 2
seq1 A seq2 C
this is i 5
this is j 3
seq1 A seq2 C
this is i 5
this is j 4
seq1 A seq2 T
Vscore = 0
Hscore = -1
Dscore = -2
this is i 6
this is j 1
seq1 T seq2 A
this is i 6
this is j 2
seq1 T seq2 C
this is i 6
this is j 3
seq1 T seq2 C
this is i 6
this is j 4
seq1 T seq2 T
Vscore = -1
Hscore = -1
Dscore = 3
谢谢!
【问题讨论】:
标签: python matrix alignment bioinformatics