<?xml version='1.0'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:georss="http://www.georss.org/georss" xmlns:atom="http://www.w3.org/2005/Atom" >
<channel>
	<title><![CDATA[BOL: Perl script for Smith-Waterman Algorithm]]></title>
	<link>https://bioinformaticsonline.com/snippets/view/43404/perl-script-for-smith-waterman-algorithm?</link>
	<atom:link href="https://bioinformaticsonline.com/snippets/view/43404/perl-script-for-smith-waterman-algorithm?" rel="self" type="application/rss+xml" />
	<description><![CDATA[]]></description>
	
	<item>
	<guid isPermaLink="true">https://bioinformaticsonline.com/snippets/view/43404/perl-script-for-smith-waterman-algorithm</guid>
	<pubDate>Tue, 28 Sep 2021 05:19:18 -0500</pubDate>
	<link>https://bioinformaticsonline.com/snippets/view/43404/perl-script-for-smith-waterman-algorithm</link>
	<title><![CDATA[Perl script for Smith-Waterman Algorithm]]></title>
	<description><![CDATA[<code># Smith-Waterman  Algorithm

# usage statement
die &quot;usage: $0 &lt;sequence 1&gt; &lt;sequence 2&gt;\n&quot; unless @ARGV == 2;

# get sequences from command line
my ($seq1, $seq2) = @ARGV;

# scoring scheme
my $MATCH    =  1; # +1 for letters that match
my $MISMATCH = -1; # -1 for letters that mismatch
my $GAP      = -1; # -1 for any gap

# initialization
my @matrix;
$matrix[0][0]{score}   = 0;
$matrix[0][0]{pointer} = &quot;none&quot;;
for(my $j = 1; $j &lt;= length($seq1); $j++) {
    $matrix[0][$j]{score}   = 0;
    $matrix[0][$j]{pointer} = &quot;none&quot;;
}
for (my $i = 1; $i &lt;= length($seq2); $i++) {
    $matrix[$i][0]{score}   = 0;
    $matrix[$i][0]{pointer} = &quot;none&quot;;
}

# fill
my $max_i     = 0;
my $max_j     = 0;
my $max_score = 0;

for(my $i = 1; $i &lt;= length($seq2); $i++) {
    for(my $j = 1; $j &lt;= length($seq1); $j++) {
        my ($diagonal_score, $left_score, $up_score);
        
        # calculate match score
        my $letter1 = substr($seq1, $j-1, 1);
        my $letter2 = substr($seq2, $i-1, 1);       
        if ($letter1 eq $letter2) {
            $diagonal_score = $matrix[$i-1][$j-1]{score} + $MATCH;
        }
        else {
            $diagonal_score = $matrix[$i-1][$j-1]{score} + $MISMATCH;
        }
        
        # calculate gap scores
        $up_score   = $matrix[$i-1][$j]{score} + $GAP;
        $left_score = $matrix[$i][$j-1]{score} + $GAP;
        
        if ($diagonal_score &lt;= 0 and $up_score &lt;= 0 and $left_score &lt;= 0) {
            $matrix[$i][$j]{score}   = 0;
            $matrix[$i][$j]{pointer} = &quot;none&quot;;
            next; # terminate this iteration of the loop
        }
        
        # choose best score
        if ($diagonal_score &gt;= $up_score) {
            if ($diagonal_score &gt;= $left_score) {
                $matrix[$i][$j]{score}   = $diagonal_score;
                $matrix[$i][$j]{pointer} = &quot;diagonal&quot;;
            }
            else {
                $matrix[$i][$j]{score}   = $left_score;
                $matrix[$i][$j]{pointer} = &quot;left&quot;;
            }
        } else {
            if ($up_score &gt;= $left_score) {
                $matrix[$i][$j]{score}   = $up_score;
                $matrix[$i][$j]{pointer} = &quot;up&quot;;
            }
            else {
                $matrix[$i][$j]{score}   = $left_score;
                $matrix[$i][$j]{pointer} = &quot;left&quot;;
            }
        }
        
        # set maximum score
        if ($matrix[$i][$j]{score} &gt; $max_score) {
            $max_i     = $i;
            $max_j     = $j;
            $max_score = $matrix[$i][$j]{score};
        }
    }
}

# trace-back

my $align1 = &quot;&quot;;
my $align2 = &quot;&quot;;

my $j = $max_j;
my $i = $max_i;

while (1) {
    last if $matrix[$i][$j]{pointer} eq &quot;none&quot;;
    
    if ($matrix[$i][$j]{pointer} eq &quot;diagonal&quot;) {
        $align1 .= substr($seq1, $j-1, 1);
        $align2 .= substr($seq2, $i-1, 1);
        $i--; $j--;
    }
    elsif ($matrix[$i][$j]{pointer} eq &quot;left&quot;) {
        $align1 .= substr($seq1, $j-1, 1);
        $align2 .= &quot;-&quot;;
        $j--;
    }
    elsif ($matrix[$i][$j]{pointer} eq &quot;up&quot;) {
        $align1 .= &quot;-&quot;;
        $align2 .= substr($seq2, $i-1, 1);
        $i--;
    }   
}

$align1 = reverse $align1;
$align2 = reverse $align2;
print &quot;$align1\n&quot;;
print &quot;$align2\n&quot;;</code>]]></description>
	<dc:creator>Surabhi Chaudhary</dc:creator>
</item>

</channel>
</rss>