just write this function for a recursion trace back, but it become a infinite loop and won't get into the next level of recursion. I cannot find the problem, many thanks! p_value gotten from a matrix outside, tested that and it is OK. status, i, j pass form main and start is (1,5,4).

def traceback_alignment(status, i, j):
alignmentA=""
alignmentB=""
p_value=0
while (i>1 and j>1):
if status==1:
alignmentA=seqA[i-1]+alignmentA
alignmentB=seqB[j-1]+alignmentB
p_value=ptr_matrixM.__getitem__(j, i)
print ("p_value="+str(p_value))
if status==2:
alignmentA=seqA[i-1]+alignmentA
alignmentB="_"+alignmentB
p_value=ptr_matrixIx.__getitem__(j, i)
print ("p_value="+str(p_value))
if status==3:
alignmentA="_"+alignmentA
alignmentB=seqB[j-1]+alignmentB
p_value=ptr_matrixIy.__getitem__(j, i)
print ("p_value="+str(p_value))

if p_value==1:
traceback_alignment(2, i-1, j)
if p_value==2:
traceback_alignment(3, i, j-1)
if p_value==4:
traceback_alignment(1, i-1, j-1)
if p_value==5:
traceback_alignment(2, i-1, j)
traceback_alignment(1, i-1, j-1)
if p_value==6:
traceback_alignment(1, i-1, j-1)
traceback_alignment(3, i, j-1)
if p_value==7:
traceback_alignment(3, i, j-1)
traceback_alignment(2, i-1, j)
traceback_alignment(1, i-1, j-1)
return (alignmentA, alignmentB)

Sorry, hope it will work

def traceback_alignment(status, i, j):
alignmentA=""
alignmentB=""
p_value=0
while (i>4 and j>4):
if status==1:
alignmentA=seqA[i-1]+alignmentA
alignmentB=seqB[j-1]+alignmentB
print ("in While i="+str(i), "j="+str(j), "alignmentA="+alignmentA, "alignmentB="+alignmentB, "Status="+str(status))
p_value=ptr_matrixM.__getitem__(j, i)
print ("p_value="+str(p_value))
if status==2:
alignmentA=seqA[i-1]+alignmentA
alignmentB="_"+alignmentB
print ("in While i="+str(i), "j="+str(j), "alignmentA="+alignmentA, "alignmentB="+alignmentB, "Status="+str(status))
p_value=ptr_matrixIx.__getitem__(j, i)
print ("p_value="+str(p_value))
if status==3:
alignmentA="_"+alignmentA
alignmentB=seqB[j-1]+alignmentB
print ("in While i="+str(i), "j="+str(j), "alignmentA="+alignmentA, "alignmentB="+alignmentB, "Status="+str(status))
p_value=ptr_matrixIy.__getitem__(j, i)
print ("p_value="+str(p_value))

if p_value==1:
traceback_alignment(2, i-1, j)
if p_value==2:
traceback_alignment(3, i, j-1)
if p_value==4:
traceback_alignment(1, i-1, j-1)
if p_value==5:
traceback_alignment(2, i-1, j)
traceback_alignment(1, i-1, j-1)
if p_value==6:
traceback_alignment(1, i-1, j-1)
traceback_alignment(3, i, j-1)
if p_value==7:
traceback_alignment(3, i, j-1)
traceback_alignment(2, i-1, j)
traceback_alignment(1, i-1, j-1)
return (alignmentA, alignmentB)

def traceback_alignment(status, i, j):
    alignmentA=""
    alignmentB=""
    p_value=0
    while (i>4 and j>4):
        if status==1:
            alignmentA=seqA[i-1]+alignmentA
            alignmentB=seqB[j-1]+alignmentB
            print ("in While i="+str(i), "j="+str(j), "alignmentA="+alignmentA, "alignmentB="+alignmentB, "Status="+str(status))
            p_value=ptr_matrixM.__getitem__(j, i)
            print ("p_value="+str(p_value))
        if status==2:
            alignmentA=seqA[i-1]+alignmentA
            alignmentB="_"+alignmentB
            print ("in While i="+str(i), "j="+str(j), "alignmentA="+alignmentA, "alignmentB="+alignmentB, "Status="+str(status))
            p_value=ptr_matrixIx.__getitem__(j, i)
            print ("p_value="+str(p_value))
        if status==3:
            alignmentA="_"+alignmentA
            alignmentB=seqB[j-1]+alignmentB
            print ("in While i="+str(i), "j="+str(j), "alignmentA="+alignmentA, "alignmentB="+alignmentB, "Status="+str(status))
            p_value=ptr_matrixIy.__getitem__(j, i)
            print ("p_value="+str(p_value))

        if p_value==1:
            traceback_alignment(2, i-1, j)
        if p_value==2:
            traceback_alignment(3, i, j-1)
        if p_value==4:
            traceback_alignment(1, i-1, j-1)
        if p_value==5:
            traceback_alignment(2, i-1, j)
            traceback_alignment(1, i-1, j-1)
        if p_value==6:
            traceback_alignment(1, i-1, j-1)
            traceback_alignment(3, i, j-1)
        if p_value==7:
            traceback_alignment(3, i, j-1)
            traceback_alignment(2, i-1, j)
            traceback_alignment(1, i-1, j-1)
    return (alignmentA, alignmentB)

The first problem I see is that the values of i and j are never modified, so I don't know how you can leave the loop while (i>4 and j>4) . Also if you could add some more code so that it would be runnable, that would help.

When it gets into recursion, j and i should change, right? p_value is always one of numbers 1-7. Thanks and I will try to get more code on.

Edited 6 Years Ago by pythonlearning: n/a

When it gets into recursion, j and i should change, right? p_value is always one of numbers 1-7. Thanks and I will try to get more code on.

i and j cannot change upon recursion because python functions are always called by value.

This question has already been answered. Start a new discussion instead.