Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re^4: Find overlap

by Anonymous Monk
on Oct 14, 2012 at 16:28 UTC ( #998961=note: print w/replies, xml ) Need Help??

in reply to Re^3: Find overlap
in thread Find overlap

Hi, Thank you for your answer, but this wasn't the problem. I dont want to find just the minima and maxima of a chromosome. I want to find the overlapping regions and find the minima and maxima of that. So when there are lines in the 4 files like this:
file1: chr1 100 500 chr1 25 50 file2: chr1 10 50 chr1 60 80 file3: chr1 12 40 chr1 41 45 file4: chr1 20 45 chr1 48 80
The line of the first file does not overlap with the other 3 files, so i dont want to find the minima and maxima. I want to find only the minima and maxima of the regions in which the lines of all four files overlap each other. So the minima and maxima of the other regions in the files. Minima = 10 maxima = 80

Replies are listed 'Best First'.
Re^5: Find overlap
by Cristoforo (Curate) on Oct 17, 2012 at 15:42 UTC
    Here is a solution that won't include non-overlapping regions. It uses the Sort::Naturally and List::Util modules.

    It also prints out the number of overlapping records, (merged 3 says 3 records overlapped this range). You can change the print statement to not print this if you (probably) want.

    For each chromosome, the records are sorted in order from the smallest beginning position to the largest beginning position. This simplifies the logic and makes a solution possible. The code that does this is:

    my ($first, @in_order) =  sort {$a->[0] <=> $b->[0]} @{ $data{$chr} };

    And, this is the program.

    #!/usr/bin/perl use strict; use warnings; use Sort::Naturally qw/ nsort /; use List::Util qw / max /; @ARGV = qw/ 148N.txt 162N.txt 174N.txt 175N.txt /; my %data; while (<>) { my ($chr, @start_stop) = split; push @{ $data{$chr} }, \@start_stop; } for my $chr (nsort keys %data) { my ($first, @in_order) = sort {$a->[0] <=> $b->[0]} @{ $data{$chr +} }; my ($lo, $hi) = @$first; my $merged; for my $aref (@in_order) { # array reference my ($start, $stop) = @$aref; if ($start <= $hi) { $hi = max $hi, $stop; $merged++; } else { printf "%s %s %s merged: %s\n", $chr, $lo, $hi, $merged + +1 if $merged; #print "$chr $lo $hi\n" if $merged; ($lo, $hi) = ($start, $stop); $merged = 0; } } printf "%s %s %s merged: %s\n", $chr, $lo, $hi, $merged + 1 if $me +rged; #print "$chr $lo $hi\n" if $merged; }
    Update: Just a note - you don't need the 'nsort' function from the Sort::Naturally module for the program to process correctly. This would merely order your chromosomes in your output.
    chr1 xx xx chr2 xx xx chr4 xx xx ...
    And, if you can't use List::Util for the max function, you could easily define it yourself. I used the module just to save some additional code.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://998961]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2018-05-21 05:31 GMT
Find Nodes?
    Voting Booth?