Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^5: Find overlap

by Cristoforo (Deacon)
on Oct 17, 2012 at 15:42 UTC ( #999564=note: print w/replies, xml ) Need Help??


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

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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://999564]
help
Chatterbox?
[ambrus]: Corion: well Prima::Object says something like that the cleanup method will send an onDestory message and that you can't get more messages after cleanup, or something.
[Corion]: ambrus: Yeah - I don't think the deep source dive will be necessary if things are implemented as simple as they could be :)) And hopefully I won't need (more) timely object destruction. I can update the screen at 60Hz and hopefully even do HTTP ...
[Corion]: ... transfers in the background. Now that I think about it, this maybe even means that I can run the OpenGL filters on Youtube input :)
[ambrus]: Corion: I mentioned that the unix event loop of Prima always wakes up at least once every 0.2 seconds. Have you found out whether the win32 event loop of Prima does that too?
[Corion]: ambrus: Hmm - I would assume that the onDestroy message is sent from the destructor and doesn't go through the messageloop, but maybe it is sent when a window gets destroyed but all components are still alive...
[ambrus]: Corion: partly deep source dive, partly just conservative coding even if it adds an overhead.
[Corion]: ambrus: Hmm - no, I haven't looked at wakeup intervals ... I wonder why it should want to wakeup periodically because it gets a lot of messages from the Windows message loop (on Windows obviously)
[ambrus]: (Alternately a deep source dive and then rewrite that event loop to make it better, and then as a bonus you get an idle method.)
[ambrus]: The 0.2 seconds wakeup is likely a workaround for some bug, but I can't guess what bug that is.
[ambrus]: It's been there since Prima 1.00 iirc

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2016-12-09 10:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (150 votes). Check out past polls.