import scoringMatrix seq1 = open("seq1.txt",'r').read() # read the two sequences seq2 = open("seq2.txt",'r').read() print(seq1) # print them just to make sure they were read properly print(seq2) # create the matrix of scores and of directions scorematrix = [[0] * (len(seq1)+1) for _ in range(len(seq2)+1)] directionmatrix = [[8] * (len(seq1)+1) for _ in range(len(seq2)+1)] # Fill in the first column, the first row is done below for i in range(0,len(seq2)+1): scorematrix[i][0] = 0 # -8*i directionmatrix[i][0] = 'v' threshold = 20 # set the cut-off threshold for i in range(1,len(seq1)+1): # column for j in range(0,len(seq2)+1): # row, start at 0 v = scorematrix[j-1][i] + scoringMatrix.gappenalty h = scorematrix[j][i-1] + scoringMatrix.gappenalty x = scoringMatrix.aminoDictionary[seq1[i-1]] y = scoringMatrix.aminoDictionary[seq2[j-1]] d = scorematrix[j-1][i-1] + scoringMatrix.blosum50[x][y] if(j == 0): # if in row 0 use the "special" rule for values best = scorematrix[0][i-1] # best score so far besty = 0 for y in range(1,len(seq2)+1): # check each value in the previous column if(scorematrix[y][i-1]-threshold > best): # if the score is better than the best so far best = scorematrix[y][i-1]-threshold # remember the score besty = y # remember the row the score came from scorematrix[0][i] = best # store the best score found if(besty == 0): directionmatrix[0][i] = 'h' # horizontal move else: directionmatrix[0][i] = besty # jump down in the previous column else: # if not in the first row, use the "normal" method to caluculate the score scorematrix[j][i] = max(scorematrix[0][i],v,h,d) if(v == max(scorematrix[0][i],v,h,d)): directionmatrix[j][i] = 'v' # vertical move if(h == max(scorematrix[0][i],v,h,d)): directionmatrix[j][i] = 'h' # horizontal move if(d == max(scorematrix[0][i],v,h,d)): directionmatrix[j][i] = 'd' # diagonal move if(scorematrix[0][i] == max(scorematrix[0][i],v,h,d)): directionmatrix[j][i] = 'r' # jump to the top row for j in range(0,len(seq2)+1): for i in range(0,len(seq1)+1): print(scorematrix[j][i], end =" ") print() print() print() for j in range(0,len(seq2)+1): for i in range(0,len(seq1)+1): print(directionmatrix[j][i], end =" ") print() x = len(seq1) # start at the top right corner (technically one to the right of that) y = 0 print("Score = ",scorematrix[y][x]) alignedseq1 = [] alignedseq2 = [] while(x != 0): # while not at the left edge of the table if(directionmatrix[y][x] == 'd'): # diagonal x = x - 1 # x -= 1 or x-- y = y - 1 alignedseq1.append(seq1[x]) alignedseq2.append(seq2[y]) #print("seq1[x], " ", seq2[y]); elif(directionmatrix[y][x] == 'v'): # vertical y = y - 1 alignedseq1.append('_') alignedseq2.append(seq2[y]) #print("_", " ", seq2[y]) elif(directionmatrix[y][x] == 'h'): # horizontal x = x - 1 alignedseq1.append(seq1[x]) alignedseq2.append('_') #print(seq1[x], " ", "_") elif(directionmatrix[y][x] == 'r'): # toprow y = 0 # jump to top row #alignedseq1.append(seq2[y]) #alignedseq2.append('.') #print(seq1[x], " ", "_") else: # jump down in previous column y = directionmatrix[y][x] x = x - 1 alignedseq1.append(seq1[x]) alignedseq2.append(".") #print(seq1[x], " ", "_") #print(alignedseq1) # just extra output #print(alignedseq2) alignedseq1.reverse() alignedseq2.reverse() print(''.join(alignedseq1)) print(''.join(alignedseq2))