Question

Throughout this script, can you provide helpful comments about how each function works in the script...

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()

0 0
Add a comment Improve this question Transcribed image text
Answer #1
 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()
Add a comment
Know the answer?
Add Answer to:
Throughout this script, can you provide helpful comments about how each function works in the script...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Need help with a 2D list - I'm almost there! #Write a function called check_winner which...

    Need help with a 2D list - I'm almost there! #Write a function called check_winner which takes #as input a 2D list. It should return "X" if there are four #adjacent "X" values anywhere in the list (row, column, #diagonal); "O" if there are four adjacent "O" values #anywhere in the list; and None if there are neither. # #Here are the ways Connect-4 is different from tic-tac-toe: # # - Connect-4 is played with 6 rows and 7 columns...

  • How can I rewrite and better understand the code since it is not working. Thank you...

    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)

  • can someone indent this code correctly in python programming def count_neighbors(cells,row,col): rows=len(cells) #storing no. of rows...

    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):...

  • My tests for sorting algorithms are not working. sorting.py has 6 sorting algorithms and test_sorting.py has...

    My tests for sorting algorithms are not working. sorting.py has 6 sorting algorithms and test_sorting.py has test cases to run those algorithms, but test cases are not working. please correct the errors in test_sorting.py file so that it can test all the sorting algorithms. WARNING: DON'T COPY AND PASTE THE QUESTION IN ANSWER. I WILL REPORT YOU. sorting.py # 1. selection sort # 2. insertion sort # 3. shell sort # 4. heap sort # 5. merge sort # 6....

  • python: how would I format the grid below so that it is aligned correctly? as you...

    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)...

  • 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 - dis...

    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 -...

  • Can you add code comments where required throughout this Python Program, Guess My Number Program, and...

    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...

    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...

  • Something is preventing this python code from running properly. Can you please go through it and...

    Something is preventing this python code from running properly. Can you please go through it and improve it so it can work. The specifications are below the code. Thanks list1=[] list2=[] def add_player(): d={} name=input("Enter name of the player:") d["name"]=name position=input ("Enter a position:") if position in Pos: d["position"]=position at_bats=int(input("Enter AB:")) d["at_bats"] = at_bats hits= int(input("Enter H:")) d["hits"] = hits d["AVG"]= hits/at_bats list1.append(d) def display(): if len(list1)==0: print("{:15} {:8} {:8} {:8} {:8}".format("Player", "Pos", "AB", "H", "AVG")) print("ORIGINAL TEAM") for x...

  • Write a module of utility functions called util.py for manipulating 2-dimensional arrays of size 4x4. (These...

    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...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT