<?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 overlaps between two bed files !]]></title>
	<link>https://bioinformaticsonline.com/snippets/view/44456/perl-script-to-find-overlaps-between-two-bed-files?</link>
	<atom:link href="https://bioinformaticsonline.com/snippets/view/44456/perl-script-to-find-overlaps-between-two-bed-files?" rel="self" type="application/rss+xml" />
	<description><![CDATA[]]></description>
	
	<item>
	<guid isPermaLink="true">https://bioinformaticsonline.com/snippets/view/44456/perl-script-to-find-overlaps-between-two-bed-files</guid>
	<pubDate>Thu, 01 Feb 2024 02:04:04 -0600</pubDate>
	<link>https://bioinformaticsonline.com/snippets/view/44456/perl-script-to-find-overlaps-between-two-bed-files</link>
	<title><![CDATA[Perl script to find overlaps between two bed files !]]></title>
	<description><![CDATA[<code>#!/usr/bin/perl

use strict;
use warnings;

# Check if the correct number of arguments are provided
if (@ARGV != 2) {
    die &quot;Usage: $0 file1.bed file2.bed\n&quot;;
}

# Read the contents of the two BED files
my $file1 = shift @ARGV;
my $file2 = shift @ARGV;

open my $fh1, &#039;&lt;&#039;, $file1 or die &quot;Error opening $file1: $!&quot;;
open my $fh2, &#039;&lt;&#039;, $file2 or die &quot;Error opening $file2: $!&quot;;

# Iterate over each interval in the first BED file
while (my $line1 = &lt;$fh1&gt;) {
    chomp $line1;
    my @fields1 = split /\t/, $line1;
    my $chr1 = $fields1[0];
    my $start1 = $fields1[1];
    my $end1 = $fields1[2];

    # Check for overlaps with intervals in the second BED file
    while (my $line2 = &lt;$fh2&gt;) {
        chomp $line2;
        my @fields2 = split /\t/, $line2;
        my $chr2 = $fields2[0];
        my $start2 = $fields2[1];
        my $end2 = $fields2[2];

        # Check for chromosome match and overlap
        if ($chr1 eq $chr2 &amp;&amp; $start1 &lt; $end2 &amp;&amp; $end1 &gt; $start2) {
            print &quot;Overlap found:\n&quot;;
            print &quot;File 1: $line1\n&quot;;
            print &quot;File 2: $line2\n\n&quot;;
        }
    }

    # Rewind file2 to the beginning for the next iteration
    seek $fh2, 0, 0;
}

close $fh1;
close $fh2;

print &quot;Comparison completed.\n&quot;;</code>]]></description>
	<dc:creator>LEGE</dc:creator>
</item>

</channel>
</rss>