Throughout this script, can you provide helpful comments about how each function works in the script plus it's significance to the alignment process.
Also are there any errors in this script or a way to improve this script?
Any help would be appreciated. Thank you.
THIS IS A PYTHON CODE AND IS IN IT'S FORMAT _ ITS CLEAR!
#!/usr/bin/env python
#
file=input("Please provide fasta file with 2 sequences:")
match=float(input('What is the match score?:'))
missmatch=float(input('What is the missmatch score?:'))
gap=float(input('What is the gap cost?:'))
fasta=open(file,'r')
sequence_list=[]
sequence=''
for line in fasta:
line = line.rstrip()
if line.startswith(">"):
sequence_list.append(sequence)
sequence=''
continue
else:
sequence += line
sequence_list.append(sequence)
sequence_list = sequence_list[1:]
sequence1 = sequence_list[0]
sequence2 = sequence_list[1]
def main():
rows=len(sequence1)+1
columns=len(sequence2)+1
score_matrix, start_position =
create_scoring_matrix(rows, columns)
FirstSeq_WithGaps, SecondSeq_WithGaps =
traceback(score_matrix, start_position)
alignment_str, identical, gap, missmatches =
alignment_string(FirstSeq_WithGaps, SecondSeq_WithGaps)
alignment_length = len(FirstSeq_WithGaps)
print ('\n')
print (' Identities = {0}/{1} ({2:.1%}), ''Gaps =
{3}/{4} ({5:.1%})'.format(identical, alignment_length, identical /
alignment_length, gap, alignment_length, gap /
alignment_length))
print ('\n')
for i in range(0, alignment_length, 60):
sequence1_slice =
FirstSeq_WithGaps[i:i+60]
print('Query {0:<4} {1}
{2:<4}'.format(i + 1, sequence1_slice, i +
len(sequence1_slice)))
print('
{0}'.format(alignment_str[i:i+60]))
sequence2_slice =
SecondSeq_WithGaps[i:i+60]
print('Subject {0:<4} {1}
{2:<4}'.format(i + 1, sequence2_slice, i +
len(sequence2_slice)))
print('\n')
def create_scoring_matrix(rows, columns):
score_matrix=[[0 for column in range(columns)] for row
in range (rows)]
max_score = 0;
max_position = None
for i in range (1, rows):
for j in range(1, columns):
score =
calculate_score(score_matrix, i, j)
if score >
max_score:
max_score = score
max_position = (i, j)
score_matrix[i][j] = score
return score_matrix, max_position
def calculate_score(matrix, x, y):
similarity = match if sequence1[x-1] == sequence2[y-1]
else missmatch
diagonal_score = matrix[x-1][y-1] + similarity
up_score = matrix[x-1][y] + gap
left_score = matrix[x][y-1] + gap
return max(0, diagonal_score, up_score,
left_score)
def traceback(score_matrix, start_position):
END, DIAGONAL, UP, LEFT = range(4)
FirstSeq_WithGaps = []
SecondSeq_WithGaps = []
x, y = start_position
move = next_move(score_matrix, x, y)
while move != END:
if move == DIAGONAL:
FirstSeq_WithGaps.append(sequence1[x-1])
SecondSeq_WithGaps.append(sequence2[y-1])
x -= 1
y -= 1
elif move == UP:
FirstSeq_WithGaps.append(sequence1[x-1])
SecondSeq_WithGaps.append('-')
x -= 1
else:
FirstSeq_WithGaps.append('-')
SecondSeq_WithGaps.append(sequence2[y-1])
y -= 1
move = next_move(score_matrix, x,
y)
return
''.join(reversed(FirstSeq_WithGaps)),''.join(reversed(SecondSeq_WithGaps))
def next_move(score_matrix, x, y,):
diagonal = score_matrix[x-1][y-1]
up = score_matrix[x-1][y]
left = score_matrix[x][y-1]
if diagonal >= up and diagonal >= left:
return 1 if diagonal !=0 else
0
elif up > diagonal and up >= left:
return 2 if up !=0 else 0
elif left > diagonal and left > up:
return 3 if left !=0 else 0
def alignment_string(FirstSeq_WithGaps, SecondSeq_WithGaps):
identical, gap, missmatch = 0, 0, 0
alignment_string = []
for position1, position2 in zip(FirstSeq_WithGaps,
SecondSeq_WithGaps):
if position1 == position2:
alignment_string.append('|')
identical +=
1
elif '-' in (position1,
position2):
alignment_string.append(' ')
gap += 1
else:
alignment_string.append(':')
missmatch +=
1
return ''.join(alignment_string), identical, gap,
missmatch
if __name__ == '__main__':
main()
IndentationError: expected an indented block
The formatted code will be
#!/usr/bin/env python # file = input("Please provide fasta file with 2 sequences:") match = float(input('What is the match score?:')) missmatch = float(input('What is the missmatch score?:')) gap = float(input('What is the gap cost?:')) fasta = open(file, 'r') sequence_list = [] sequence = '' for line in fasta: line = line.rstrip() if line.startswith(">"): sequence_list.append(sequence) sequence = '' continue else: sequence += line sequence_list.append(sequence) sequence_list = sequence_list[1:] sequence1 = sequence_list[0] sequence2 = sequence_list[1] def main(): rows = len(sequence1) + 1 columns = len(sequence2) + 1 score_matrix, start_position = create_scoring_matrix(rows, columns) FirstSeq_WithGaps, SecondSeq_WithGaps = traceback(score_matrix, start_position) alignment_str, identical, gap, missmatches = alignment_string(FirstSeq_WithGaps, SecondSeq_WithGaps) alignment_length = len(FirstSeq_WithGaps) print('\n') print(' Identities = {0}/{1} ({2:.1%}), ''Gaps = {3}/{4} ({5:.1%})'.format(identical, alignment_length, identical / alignment_length, gap, alignment_length, gap / alignment_length)) print('\n') for i in range(0, alignment_length, 60): sequence1_slice = FirstSeq_WithGaps[i:i + 60] print('Query {0:<4} {1} {2:<4}'.format(i + 1, sequence1_slice, i + len(sequence1_slice))) print(' {0}'.format(alignment_str[i:i + 60])) sequence2_slice = SecondSeq_WithGaps[i:i + 60] print('Subject {0:<4} {1} {2:<4}'.format(i + 1, sequence2_slice, i + len(sequence2_slice))) print('\n') def create_scoring_matrix(rows, columns): score_matrix = [[0 for column in range(columns)] for row in range(rows)] max_score = 0; max_position = None for i in range(1, rows): for j in range(1, columns): score = calculate_score(score_matrix, i, j) if score > max_score: max_score = score max_position = (i, j) score_matrix[i][j] = score return score_matrix, max_position def calculate_score(matrix, x, y): similarity = match if sequence1[x - 1] == sequence2[y - 1] else missmatch diagonal_score = matrix[x - 1][y - 1] + similarity up_score = matrix[x - 1][y] + gap left_score = matrix[x][y - 1] + gap return max(0, diagonal_score, up_score, left_score) def traceback(score_matrix, start_position): END, DIAGONAL, UP, LEFT = range(4) FirstSeq_WithGaps = [] SecondSeq_WithGaps = [] x, y = start_position move = next_move(score_matrix, x, y) while move != END: if move == DIAGONAL: FirstSeq_WithGaps.append(sequence1[x - 1]) SecondSeq_WithGaps.append(sequence2[y - 1]) x -= 1 y -= 1 elif move == UP: FirstSeq_WithGaps.append(sequence1[x - 1]) SecondSeq_WithGaps.append('-') x -= 1 else: FirstSeq_WithGaps.append('-') SecondSeq_WithGaps.append(sequence2[y - 1]) y -= 1 move = next_move(score_matrix, x, y) return ''.join(reversed(FirstSeq_WithGaps)), ''.join(reversed(SecondSeq_WithGaps)) def next_move(score_matrix, x, y, ): diagonal = score_matrix[x - 1][y - 1] up = score_matrix[x - 1][y] left = score_matrix[x][y - 1] if diagonal >= up and diagonal >= left: return 1 if diagonal != 0 else 0 elif up > diagonal and up >= left: return 2 if up != 0 else 0 elif left > diagonal and left > up: return 3 if left != 0 else 0 def alignment_string(FirstSeq_WithGaps, SecondSeq_WithGaps): identical, gap, missmatch = 0, 0, 0 alignment_string = [] for position1, position2 in zip(FirstSeq_WithGaps, SecondSeq_WithGaps): if position1 == position2: alignment_string.append('|') identical += 1 elif '-' in (position1, position2): alignment_string.append(' ') gap += 1 else: alignment_string.append(':') missmatch += 1 return ''.join(alignment_string), identical, gap, missmatch if __name__ == '__main__': main()
Throughout this script, can you provide helpful comments about how each function works in the script...
python programming: Can you please add comments to describe in detail what the following code does: import os,sys,time sl = [] try: f = open("shopping2.txt","r") for line in f: sl.append(line.strip()) f.close() except: pass def mainScreen(): os.system('cls') # for linux 'clear' print("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%") print(" SHOPPING LIST ") print("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%") print("\n\nYour list contains",len(sl),"items.\n") print("Please choose from the following options:\n") print("(a)dd to the list") print("(d)elete from the list") print("(v)iew the...
can someone indent this code correctly in python programming def count_neighbors(cells,row,col): rows=len(cells) #storing no. of rows cols=len(cells[0]) #storing no. of columns if(row<0 or col<0 or row>rows-1 or col>cols-1): # when row or column is out of range return -1 count=0 if(row==0 and cells[rows-1][col]==1): #cyclic order count+=1 if(col==0 and cells[row][cols-1]==1): count+=1 if(row==rows-1 and cells[0][col]==1): count+=1 if(col==cols-1 and cells[row][0]==1): count+=1 if(col>=1 and cells[row][col-1]==1): #left neighbor count+=1 if (row>=1 and cells[row-1][col]==1): #upper neighbor count+=1 if(row+1<rows and cells[row+1][col]==1): #down neighbor count+=1 if(col+1<cols and cells[row][col+1]==1):...
How can I rewrite and better understand the code since it is not working. Thank you for helping me!! It means a lot :) CODE: import numpy as np def coeff(x): X = x[:,0] Y = x[:,1] if len(X)>=11: L = 10 else: L = len(X)-1 nm = np.zeros((L,1)) for i in range(1,L): fit = np.polyfit(X,Y,i) val = np.polyval(fit,X) nm[i-1,0] = np.linalg.norm(Y-val) I = nm.argmin() coeff = np.polyfit(X,Y,I) print(coeff)
Explain what the code is doing line from line explanations please or summarize lines with an explanation def displayCart(): #displays the cart function """displayCart function - displays the items in the cart ---------------------------------------------------------------------""" import os os.system('clear') print("\n\nCart Contents:") print("Your selected items:", cart) def catMenu(): #function that displays the category menu """catMenu function - displays the categories user picks from ---------------------------------------------------------------------""" import os os.system('clear') print(""" 1 - Books 2 - Electronics 3 - Clothing d - display cart contents x -...
(Please help me with Coding in Python3) AVLTree complete the following implementation of the balanced (AVL) binary search tree. Note that you should not be implementing the map-based API described in the plain (unbalanced) BSTree notebook — i.e., nodes in the AVLTree will only contain a single value. class AVLTree: class Node: def __init__(self, val, left=None, right=None): self.val = val self.left = left self.right = right def rotate_right(self): n = self.left self.val, n.val = n.val, self.val self.left, n.left, self.right, n.right...
Write a module of utility functions called util.py for manipulating 2-dimensional arrays of size 4x4. (These functions will be used in Question 3.) The functions you need to write are as follows: def create_grid(grid): """create a 4x4 array of zeroes within grid""" def print_grid (grid): """print out a 4x4 grid in 5-width columns within a box""" def check_lost (grid): """return True if there are no 0 values and there are no adjacent values that are equal; otherwise False""" def check_won...
python: how would I format the grid below so that it is aligned correctly? as you see, I tried to center it but it is still not correct. import random row=random.randint(1,10) col=random.randint(1,10) for r in range(row): for c in range(col): if r %2==0 and c %2==0: print(format('a','^3'),end=' ') elif r %2!=0 and c %2!=0: print(format("/x\\",'^3'),end=' ') elif c%2!=0: print(format('-','^3'),end='') else: print(format('s','^3'),end=' ') print() #go to next line current Output is: (example if random function choses col=4 and row=3)...
Below you will find a recursive function that computes a Fibonacci sequence (Links to an external site.). # Python program to display the Fibonacci sequence up to n-th term using recursive functions def recur_fibo(n): """Recursive function to print Fibonacci sequence""" if n <= 1: return n else: return(recur_fibo(n-1) + recur_fibo(n-2)) # Change this value for a different result nterms = 10 # uncomment to take input from the user #nterms = int(input("How many terms? ")) # check if the number...
Can you add code comments where required throughout this Python Program, Guess My Number Program, and describe this program as if you were presenting it to the class. What it does and everything step by step. import random def menu(): #function for getting the user input on what he wants to do print("\n\n1. You guess the number\n2. You type a number and see if the computer can guess it\n3. Exit") while True: #using try-except for the choice will handle the...
Can figure out how get my program to keep track of a total score, here is my code so far in python 3.6.6 import math import random import turtle def target(): """Turtle drawing the target""" t = turtle.Turtle() wn = turtle.Screen() wn.bgcolor("black") t.hideturtle() t.speed(0) #most outside circle worth 10 points t.setposition(0,-275) t.color("grey") t.begin_fill() t.circle(275) t.end_fill() #2nd most outter circle worth 20 points t.penup() t.setposition(0,-200) t.pendown() t.color("red") t.begin_fill() t.circle(200) t.end_fill() #3rd most outter circle worth 30 points...