我在函数实际字符串中遇到了return语句的问题。当我单独运行时,该函数正常运行,但是当我在程序中运行时,它没有返回结果。函数real string应返回结果如下:((.((....)).).)
import numpy
import array
class Structure :
def __init__(self):
self.paired =[]
self.unpaired =[]
def merge(self, s):
for i in s.paired :
self.paired.append(i)
for i in s.unpaired :
self.unpaired.append(i)
def __str__(self):
return "paired\n"+str(self.paired) +"\n unpaired \n"+str(self.unpaired)+"\n"
def l(ch) :
if (ch=='A') :
return 0
elif (ch=='C'):
return 1
elif (ch=='U') :
return 2
elif (ch =='G') :
return 3
else :
print "error"
def cost(i,j,M,w,seq,compare): #Defining the cost functions or can say recurrence formula
j_unpaired = M[i,j-1]
i_unpaired = M[i+1,j]
paired = M[i+1,j-1] + w[l(seq[i]), l(seq[j])]
return compare(j_unpaired,i_unpaired,paired) #return comparison of the three relations
def fill(M, w, seq, compare): #function defining how to fill up the values
n = len(seq)
for k in range(5,n):
for i in range(0,n-k):
j = k+i
c = cost(i,j,M,w,seq,compare)
M[i,j] = c
def trace(M, w, seq, compare, horizontal_arrow, vertical_arrow) :
i = horizontal_arrow
j = vertical_arrow
struct = Structure()
while (j != i-1):
if M[i,j] == M[i,j-1] :
struct.unpaired.append(j)
j -= 1
elif M[i,j] == M[i+1,j] :
struct.unpaired.append(i)
i += 1
elif M[i,j] == M[i+1,j-1] + w[l(seq[i]), l(seq[j])] :
struct.paired.append((i,j))
i += 1
j -= 1
else:
print "error"
return struct
def real_string(M, w, seq, compare, horizontal_arrow, vertical_arrow) :
trace=dict()
struct = Structure()
for element in struct.unpaired:
trace[element] = '.'
for element in struct.paired:
trace[element[0]] = '('
trace[element[1]] = ')'
return list(trace.values())##The glitch is here;not showing result.
f =open("example.fasta","r")
def readSeq(FASTA):
for line in FASTA:
if line.startswith('>'):
continue
line = line.strip()
return line
seq=readSeq(f)
n = len(seq)
w = numpy.zeros([4,4],int)
w.fill(0)
w[l('A'), l('U')] = 1
w[l('U'), l('A')] = 1
w[l('G'), l('C')] = 1
w[l('C'), l('G')] = 1
M = numpy.zeros([n,n],int) #initialisation of matrix
fill(M, w, seq, max) #Filling matrix M with scores
print M
# perform traceback
s = trace(M, w, seq, max, 0, n-1)
print str(s)
print seq
result=real_string(M, w, seq, max,0,n-1)
print "".join(result)
当我运行代码时,它可以工作:
yourDict=dict()
unpaired = [13, 11, 2, 8, 7, 6, 5]
paired = [(0, 14), (1, 12), (3, 10), (4, 9)]
for element in unpaired:
yourDict[element] = '.'
for element in paired:
yourDict[element[0]] = '('
yourDict[element[1]] = ')'
print "".join(list(yourDict.values()))
答案 0 :(得分:1)
您的real_string
函数初始化新的Structure
:
struct = Structure()
__init__
的{{1}}为配对和未配对创建空列表。然后,您在Structure
中迭代这些空列表,这些列表不会将任何内容放入real_string
字典中。
因此该函数返回一个空列表。
我不知道你究竟想要达到的目的,但似乎你没有使用传递给你的函数的大多数参数。很可能您想使用trace
返回的s
:
trace
所以将它作为s = trace(M, w, seq, max, 0, n-1)
的参数并删除struct的初始化。
答案 1 :(得分:1)
将real_string
更改为此,因为它有错误的参数:
def real_string(struct) :
trace=dict()
for element in struct.unpaired:
trace[element] = '.'
for element in struct.paired:
trace[element[0]] = '('
trace[element[1]] = ')'
return list(trace.values())
并将其称为:
# perform traceback
s = trace(M, w, seq, max, 0, n-1)
print str(s)
print seq
result=real_string(s)
print "".join(result)