Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re^3: Help the counting!

by jethro (Monsignor)
on Mar 22, 2012 at 13:48 UTC ( #961018=note: print w/replies, xml ) Need Help??

in reply to Re^2: Help the counting!
in thread Help the counting!

I think you made a mistake with chromosom 3 in your example output

Here is a working solution:

#!/usr/bin/perl use warnings; use strict; use Data::Dumper; my @chrom; while (<>) { my ($chr, $start,$end,$c,$d,$strand)= split; push @chrom, {'chrom'=>$chr, 'start'=>$start, 'end'=>$end, 'strand +'=>$strand }; } my @chroms= sort { $a->{start} <=> $b->{start} } @chrom; #print Dumper(\@chroms); exit(0) if (@chroms==0); my $coord= shift @chroms; #use first coordinate as range counter my $overlap=1; my $id= 1; while( my $co= shift @chroms ) { if ($co->{chrom} ne $coord->{chrom}) { printresults($coord,$overlap,$id); $coord= $co; $overlap=1; $id=1; } else { if ($co->{start}>=$coord->{end}) { printresults($coord,$overlap,$id); $coord= $co; $overlap=1; $id++; } else { $overlap++; $coord->{end}= $co->{end}; } } } printresults($coord,$overlap,$id); #------------- sub printresults { my ($coord,$overlap,$id)= @_; print "ID_$id $coord->{chrom} $coord->{start} $coor +d->{end} $coord->{strand} $overlap\n"; }
ID_1 chr1 101 111 - 3 ID_2 chr1 112 113 - 1 ID_3 chr1 113 115 - 1 ID_1 chr2 114 118 - 1 ID_2 chr2 119 123 - 2 ID_1 chr3 125 130 - 1 ID_2 chr3 131 132 - 1

You can remove the '#' in front of the 'print Dumper' line if you want to see how the data in @chroms looks like

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://961018]
[Eily]: moritz but that should be fine unless you modify them
[choroba]: if you modify them, Perl will crash with "Modification of a read-only value attempted" for $1
[choroba]: I can't replicate the situation, but I remember it was very hard to debug

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (8)
As of 2018-02-22 17:28 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (296 votes). Check out past polls.