Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Find overlap

by frozenwithjoy (Curate)
on Oct 14, 2012 at 00:19 UTC ( #998891=note: print w/ replies, xml ) Need Help??


in reply to Find overlap

Hi, a few questions to start off:
You say there are 4 columns/line, but I only see 3, or do you mean 'chr' is in column 1 and the chromosome # is in column 2?
Related to this, are there multiple chromosomes represented/file or are all regions from chr1?
Approximately how many ranges are in each file?
Finally, are the columns tab-delimited or?


Comment on Re: Find overlap
Re^2: Find overlap
by linseyr (Acolyte) on Oct 14, 2012 at 01:06 UTC
    Hi, No there are three columns per line: chr, start and end. There are 4 different files which contain these three columns. In total there are 24 chromosomes, and every files contains a different number of lines per chromosome. Every file has aprox 240,000 lines. so about 10,000 per chromosome.
      hi,

      i'm not sure if i get the problem right but this script will find the min of all values in column 2 of all given files and the max of all values in column 3.

      you just call it with the file names as arguments.

      #!/usr/bin/perl -w use strict; my $min = undef; my $max = undef; foreach (@ARGV) { open(F, "< $_"); while(my $line = <F>) { chop($line); $line =~ s/[\ \t]+/:/g; my @p = split(/:/,$line); if(!defined($min)) { $min = $p[1]; } if(!defined($max)) { $max = $p[2]; } if($p[1] < $min) { $min = $p[1]; } if($p[2] > $max) { $max = $p[2]; } } close(F); } print "$min $max\n";
      but if you need the first column as well, and if the columns are tab separated, this script would give you the minima of chr1..chrN of column2 of all given files and the maxima of chr1..chrN of column3 of all given files. do you need the file names as well? and i hope i understood the problem right
      #!/usr/bin/perl -w use strict; my %minmax; foreach (@ARGV) { open(F, "< $_"); while(my $line = <F>) { chop($line); my @p = split(/\t/,$line); if(!exists( $minmax{ $p[0] }{ min } )) { $minmax{ $p[0] }{ min } = $p[1]; } if($p[1] < $minmax{ $p[0] }{ min }) { $minmax{ $p[0] }{ min } = $p[1]; } if(!exists( $minmax{ $p[0] } { max } )) { $minmax{ $p[0] }{ max } = $p[2]; } if($p[2] > $minmax{ $p[0] }{ max }) { $minmax{ $p[0] }{ max } = $p[2]; } } close(F); } foreach (keys %minmax) { print "$_: min: $minmax{$_}{min} max: $minmax{$_}{max}\n"; }
      for the given input:

      file1:

      chr1 4 60 chr2 2 40 chr3 4 90 chr1 5 40
      file2:
      chr2 1 30 chr1 6 20 chr4 9 100
      file3:
      chr1 2 20 chr2 2 90 chr1 6 20 chr4 4 30
      file4:
      chr2 4 90 chr3 3 90 chr2 4 90 chr4 3 90 chr2 4 30
      it would output:
      chr1: min: 2 max: 60 chr2: min: 1 max: 90 chr3: min: 3 max: 90 chr4: min: 3 max: 100

        Your solution is an excellent use of a HoH! Here are a few items to consider within your foreach:

        foreach (@ARGV) { open my $fh, '<', $_ or die $!; while ( my $line = <$fh> ) { next unless my @p = $line =~ /^(chr.+?)\t+(\d+)\t+(\d+)/; $minmax{ $p[0] }{min} //= $p[1]; $minmax{ $p[0] }{max} //= $p[2]; if ( $p[1] < $minmax{ $p[0] }{min} ) { $minmax{ $p[0] }{min} = $p[1]; } if ( $p[2] > $minmax{ $p[0] }{max} ) { $minmax{ $p[0] }{max} = $p[2]; } } close $fh; }
        • A three-argument open is used
        • A lexically-scoped variable is used for the file handle
        • or die $! checks for open errors
        • The regex line:
          • Skips possible comment and track definition lines in the BED files
          • And then makes using chomp and split unnecessary
        • Uses defined-or-equals (//=) to replace the !exists

        Hope this is helpful.

        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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://998891]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (12)
As of 2014-09-02 08:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (20 votes), past polls