Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import numpy as np # Function to read sequences from a FASTA file def read _ fasta ( file ) : with open ( file

import numpy as np
# Function to read sequences from a FASTA file def read_fasta(file): with open(file,'r') as f:
sequences =[] sequence ='' for line in f: if line.startswith('>'): if sequence:
sequences.append(sequence) sequence ='' else:
sequence += line.strip() if sequence: sequences.append(sequence) return sequences
# Needleman-Wunsch algorithm implementation for global alignment def needleman_wunsch(seq1, seq2, match_score=1, gap_cost=1, mismatch_cost=1):
n = len(seq1) m = len(seq2) score_matrix = np.zeros((n +1, m +1))
for i in range(n +1):
score_matrix[i][0]=-i * gap_cost for j in range(m +1):
score_matrix[0][j]=-j * gap_cost
for i in range(1, n +1): for j in range(1, m +1):
match = score_matrix[i -1][j -1]+(match_score if seq1[i -1]== seq2[j -1] else -mismatch_cost) delete = score_matrix[i -1][j]- gap_cost insert = score_matrix[i][j -1]- gap_cost score_matrix[i][j]= max(match, delete, insert)
align1, align2='','' i, j = n, m
while i >0 and j >0:
score_current = score_matrix[i][j] score_diagonal = score_matrix[i -1][j -1] score_up = score_matrix[i][j -1] score_left = score_matrix[i -1][j]
if score_current == score_diagonal +(match_score if seq1[i -1]== seq2[j -1] else -mismatch_cost):
align1+= seq1[i -1] align2+= seq2[j -1] i -=1 j -=1 elif score_current == score_left - gap_cost:
align1+= seq1[i -1] align2+='-' i -=1 elif score_current == score_up - gap_cost:
align1+='-' align2+= seq2[j -1] j -=1
while i >0:
align1+= seq1[i -1] align2+='-' i -=1
while j >0:
align1+='-' align2+= seq2[j -1] j -=1
align1= align1[::-1] align2= align2[::-1]
return align1, align2, score_matrix[n][m]
# Function to find differential indices def find_differential_indices(align1, align2):
differential_indices =[] for i in range(len(align1)): if align1[i]!= align2[i]: differential_indices.append(i) return differential_indices
# Main function to execute the workflow def main():
# Example sequences in FASTA format query_fasta = 'query.fasta' database_fasta = 'database.fasta'
# Read sequences from FASTA files query_sequences = read_fasta(query_fasta) database_sequences = read_fasta(database_fasta)
query_seq = query_sequences[0] database_seq = database_sequences[0]
# Perform global alignment using Needleman-Wunsch algorithm align1, align2, score = needleman_wunsch(query_seq, database_seq)
# Find differential indices differential_indices = find_differential_indices(align1, align2)
# Print results print(f"Query Sequence: {query_seq}") print(f"Database Sequence: {database_seq}") print(f"Aligned Query Sequence: {align1}") print(f"Aligned Database Sequence: {align2}") print(f"Alignment Score: {score}") print(f"Differential Indices: {differential_indices}")
if __name__=="__main__":
main()
*It needs to get gapped allignment

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Sams Teach Yourself Beginning Databases In 24 Hours

Authors: Ryan Stephens, Ron Plew

1st Edition

067232492X, 978-0672324925

More Books

Students also viewed these Databases questions

Question

Define promotion.

Answered: 1 week ago

Question

Write a note on transfer policy.

Answered: 1 week ago

Question

Discuss about training and development in India?

Answered: 1 week ago

Question

Explain the various techniques of training and development.

Answered: 1 week ago