OUTPUT BUFFER:
# Smith-Waterman Algorithm Implementation for Protein Sequences with BLOSUM62 Matrix # Function to load BLOSUM62 matrix from a file proc loadBLOSUM62Matrix {} { set blosum62 [dict create] # Provide the path to your BLOSUM62 matrix file set matrix_file_path "/home/moumou/ordali2/etc/blosum62.dat" set file [open $matrix_file_path "r"] while {[gets $file line] != -1} { if {[string index j$line] ne "#"} { break } } set Laa [gets $file] regsub -all {+ } $line " " line set Laa [split [string trim $Laa]] while {[gets $file line] != -1} { set elements [regexp -all -inline {\S+} $line] set aa1 [lindex $elements 0] set scores [lrange $elements 1 end] foreach aa2 $Laa sco $scores { dict set blosum62 "$aa1$aa2" $sco } } close $file return $blosum62 } # Function to perform Smith-Waterman alignment with BLOSUM62 matrix proc smithWatermanProtein {seq1 seq2 blosum62 gap_penalty} { set m [string length $seq1] set n [string length $seq2] # Initialize the scoring matrix set score_matrix [lrepeat [expr {$m + 1}] [lrepeat [expr {$n + 1}] 0]] # Initialize variables to store the maximum score and its position set max_score 0 set max_i 0 set max_j 0 # Load BLOSUM62 matrix set blosum62_matrix [loadBLOSUM62Matrix] # Fill in the scoring matrix for {set i 1} {$i <= $m} {incr i} { for {set j 1} {$j <= $n} {incr j} { set diag [lindex $score_matrix [expr {$i - 1}] [expr {$j - 1}]] set left [lindex $score_matrix $i [expr {$j - 1}]] set up [lindex $score_matrix [expr {$i - 1}] $j] set aa1 [string index $seq1 [expr {$i - 1}]] set aa2 [string index $seq2 [expr {$j - 1}]] # Lookup BLOSUM62 score set match_score [dict get $blosum62_matrix "$aa1$aa2"] set gap_penalty_left [expr {$left - $gap_penalty}] set gap_penalty_up [expr {$up - $gap_penalty}] set score [expr {[expr {$diag + $match_score}]} > [expr {$gap_penalty_left > 0 ? $gap_penalty_left : 0}] ? [expr {$diag + $match_score}] : 0}] set score [expr {[expr {$score > $gap_penalty_up ? $score : $gap_penalty_up}] > 0 ? [expr {$score > $gap_penalty_up ? $score : $gap_penalty_up}] : 0}] lset score_matrix $i $j $score # Update the maximum score and its position if {$score > $max_score} { set max_score $score set max_i $i set max_j $j } } } # Traceback to find the aligned sequences set aligned_seq1 "" set aligned_seq2 "" while {$max_i > 0 && $max_j > 0 && [lindex $score_matrix $max_i $max_j] > 0} { set current_score [lindex $score_matrix $max_i $max_j] set diagonal_score [lindex $score_matrix [expr {$max_i - 1}] [expr {$max_j - 1}]] set left_score [lindex $score_matrix $max_i [expr {$max_j - 1}]] set up_score [lindex $score_matrix [expr {$max_i - 1}] $max_j] set aa1 [string index $seq1 [expr {$max_i - 1}]] set aa2 [string index $seq2 [expr {$max_j - 1}]] # Lookup BLOSUM62 score set match_score [dict get $blosum62_matrix "$aa1$aa2"] if {$current_score == $diagonal_score + $match_score} { append aligned_seq1 $aa1 append aligned_seq2 $aa2 incr max_i -1 incr max_j -1 } elseif {$current_score == $left_score - $gap_penalty} { append aligned_seq1 "-" append aligned_seq2 $aa2 incr max_j -1 } elseif {$current_score == $up_score - $gap_penalty} { append aligned_seq1 $aa1 append aligned_seq2 "-" incr max_i -1 } } # Reverse the aligned sequences set aligned_seq1 [string reverse $aligned_seq1] set aligned_seq2 [string reverse $aligned_seq2] return [list $aligned_seq1 $aligned_seq2 $max_score] } # Example usage set seq1 "ARNDC" set seq2 "AQEGH" set gap_penalty 4 set result [smithWatermanProtein $seq1 $seq2 $gap_penalty] puts "Aligned Sequence 1: [lindex $result 0]" puts "Aligned Sequence 2: [lindex $result 1]" puts "Alignment Score: [lindex $result 2]"