shortest path alignement python

Asked by At

I have this function to find the shortest path in a matrix from M[0][0] to M[m][n] i created the matrix and everything i want to draw the shortest path everything in working yet aseq1, and aseq2 are empty im unable to locate the problem

def path(seq1,seq2,matrix,gap):
    aseq1 = '' 
    aseq2 = ''

#reconstruction de l'alignement aseq1 pour la premiere sequence et aseq2 pour la deuxieme  
i = len(seq1[0])-1
j = len(seq2[1])-1
while i>0 and j>0:


    if i%10==0:
        stderr.write('.') 


 #utilisation de la matrice remplie pour tracer le chemin le plus court de M[0][0] a M[m][n]      
    score = M[i][j] 
    score_diag = M[i-1][j-1] 
    score_up = M[i][j-1] 
    score_left = M[i-1][j]

    if score == score_diag + d[[seq1[i-1]][seq2[j-1]]]:
        aseq1 = seq1[i-1] + aseq1 
        aseq2 = seq2[j-1] + aseq2 
        i -= 1 
        j -= 1
    if score == score_left + gap:
        aseq1 = seq1[i-1] + aseq1 
        aseq2 = '-' + aseq2 
        i -= 1 
    elif score == score_up + gap:
        aseq1 = '-' + aseq1 
        aseq2 = seq2[j-1] + aseq2 
        j -= 1
    else:
            print("Erreur?")


while i>0:
    # si j==0 avant i==0 on continue l'iteration avec i
    aseq1 = seq1[i] + aseq1 
    aseq2 = '-' + aseq2 
    i -= 1



while j>0: 
    # si i==0 avant j==o on continue l'iteration avec j
    aseq1 = '-' + aseq1 
    aseq2 = seq2[j] + aseq2
    print (aseq1,aseq2)
    j -= 1
print(aseq1,aseq2)
return (aseq1,aseq2)

1 Answers

0
Millie Smith On Best Solutions

Pay attention to indentation in Python. All of the code inside the first while loop is inside if i%10==0. If i is ever not divisible by 10, the program will loop forever.

This is what I'm talking about. Compare the tabbing to your code above:

while i>0 and j>0:
    if i%10==0:
        stderr.write('.') 

    #by performing a traceback of how the matrix was filled out above, 
    #i.e. we find a shortest path from a[n,m] to a[0,0] 
    score = M[i][j] 
    score_diag = M[i-1][j-1] 
    score_up = M[i][j-1] 
    score_left = M[i-1][j]