CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

| Download
Views: 13
Image: ubuntu2204
Kernel: Python 3 (system-wide)
import numpy as np #Defining needleman wunsch algorithimn def needleman_wunsch(seq1, seq2, match_score=1, mismatch_score=-1, gap_penalty=-1): m, n = len(seq1), len(seq2) # Create the score matrix and initialize the first row and column score_matrix = np.zeros((m + 1, n + 1), dtype=int) for i in range(m + 1): score_matrix[i, 0] = i * gap_penalty for j in range(n + 1): score_matrix[0, j] = j * gap_penalty # Fill in the score matrix for i in range(1, m + 1): for j in range(1, n + 1): match = score_matrix[i - 1, j - 1] + (match_score if seq1[i - 1] == seq2[j - 1] else mismatch_score) delete = score_matrix[i - 1, j] + gap_penalty insert = score_matrix[i, j - 1] + gap_penalty score_matrix[i, j] = max(match, delete, insert) # Traceback to find the alignment align1, align2 = '', '' i, j = m, n while i > 0 or j > 0: current_score = score_matrix[i, j] if i > 0 and score_matrix[i - 1, j] + gap_penalty == current_score: align1 = seq1[i - 1] + align1 align2 = '-' + align2 i -= 1 elif j > 0 and score_matrix[i, j - 1] + gap_penalty == current_score: align1 = '-' + align1 align2 = seq2[j - 1] + align2 j -= 1 else: align1 = seq1[i - 1] + align1 align2 = seq2[j - 1] + align2 i -= 1 j -= 1 return score_matrix[m, n], align1, align2 # Example usage: seq1 = "AAAATTAC" seq2 = "TATGC" score, aligned_seq1, aligned_seq2 = needleman_wunsch(seq1, seq2) print(f"Alignment Score: {score}") print(f"Aligned Sequence 1: {aligned_seq1}") print(f"Aligned Sequence 2: {aligned_seq2}") # Tried but was not able to create a blosum matrix it giving the error still uploading the file that code didnot work.
Alignment Score: -2 Aligned Sequence 1: AAAATTAC Aligned Sequence 2: TA--TG-C