<?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 to find edit distance between two sequences !]]></title>
	<link>https://bioinformaticsonline.com/snippets/view/44221/perl-script-to-find-edit-distance-between-two-sequences?</link>
	<atom:link href="https://bioinformaticsonline.com/snippets/view/44221/perl-script-to-find-edit-distance-between-two-sequences?" rel="self" type="application/rss+xml" />
	<description><![CDATA[]]></description>
	
	<item>
	<guid isPermaLink="true">https://bioinformaticsonline.com/snippets/view/44221/perl-script-to-find-edit-distance-between-two-sequences</guid>
	<pubDate>Tue, 07 Mar 2023 14:11:00 -0600</pubDate>
	<link>https://bioinformaticsonline.com/snippets/view/44221/perl-script-to-find-edit-distance-between-two-sequences</link>
	<title><![CDATA[Perl script to find edit distance between two sequences !]]></title>
	<description><![CDATA[<code>#!/usr/bin/perl

use strict;
use warnings;

sub edit_distance {
    my ($s1, $s2) = @_;

    my $len1 = length($s1);
    my $len2 = length($s2);

    my @dp;
    for (my $i = 0; $i &lt;= $len1; $i++) {
        for (my $j = 0; $j &lt;= $len2; $j++) {
            $dp[$i][$j] = 0;
        }
    }

    for (my $i = 0; $i &lt;= $len1; $i++) {
        $dp[$i][0] = $i;
    }

    for (my $j = 0; $j &lt;= $len2; $j++) {
        $dp[0][$j] = $j;
    }

    for (my $i = 1; $i &lt;= $len1; $i++) {
        for (my $j = 1; $j &lt;= $len2; $j++) {
            my $cost = substr($s1, $i-1, 1) eq substr($s2, $j-1, 1) ? 0 : 1;
            $dp[$i][$j] = min($dp[$i-1][$j]+1, $dp[$i][$j-1]+1, $dp[$i-1][$j-1]+$cost);
        }
    }

    return $dp[$len1][$len2];
}

sub min {
    my $min = shift @_;
    foreach (@_) {
        $min = $_ if $_ &lt; $min;
    }
    return $min;
}

# Example usage
my $seq1 = &quot;ACGTAGCTAGCTGACTGAC&quot;;
my $seq2 = &quot;CGTAGCTAGCTGACAGCTA&quot;;
my $distance = edit_distance($seq1, $seq2);
print &quot;The edit distance between $seq1 and $seq2 is $distance.\n&quot;;</code>]]></description>
	<dc:creator>BioStar</dc:creator>
</item>

</channel>
</rss>