Our Sponsors



Download BioinformaticsOnline(BOL) Apps in your chrome browser.




Perl script to find overlaps between two bed files !

  • Public
By LEGE 329 days ago
#!/usr/bin/perl use strict; use warnings; # Check if the correct number of arguments are provided if (@ARGV != 2) { die "Usage: $0 file1.bed file2.bed\n"; } # Read the contents of the two BED files my $file1 = shift @ARGV; my $file2 = shift @ARGV; open my $fh1, '<', $file1 or die "Error opening $file1: $!"; open my $fh2, '<', $file2 or die "Error opening $file2: $!"; # Iterate over each interval in the first BED file while (my $line1 = <$fh1>) { 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 = <$fh2>) { 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 && $start1 < $end2 && $end1 > $start2) { print "Overlap found:\n"; print "File 1: $line1\n"; print "File 2: $line2\n\n"; } } # Rewind file2 to the beginning for the next iteration seek $fh2, 0, 0; } close $fh1; close $fh2; print "Comparison completed.\n";