Dear Brothers,

I have a rather large file (640MB) of coordinates that each show the position (start,end) of a particular feature on a particular chromosome. Something like:

chr1 100 120 feature1 chr1 200 250 feature2 chr2 150 200 feature1 chr2 280 350 feature1 chr3 100 150 feature2 chr3 300 450 feature2

Given another set of coordinates and a chromosome number I want to be able to quickly see if there are any features that the coordinates overlap. For example, given:

chromosome = chr2 start = 160 end = 210

I'd like the result:

chr2 150 200 feature1

Importantly I need to do a large number of these types of lookups in one go (~1000 or so).

So far I have the following. Now this works and I'm quite happy with the performance but it takes up 1.6GB memory when I read in the full 640MB file which contains over 5 million lines of coordinates.

If it helps and/or you really want to you can get this file from HERE but you'll have to change the column numbers to pull out the right fields!

#!/usr/bin/perl use strict; my %reps = (); while(my $line = <DATA>){ $line =~ s/[\n\r]//g; my @array = split(/\s+/,$line); $reps{$array[0]}{$array[1]}{'end'} = $array[2]; $reps{$array[0]}{$array[1]}{'rep'} = $array[3]; } my $start = 160; my $end = 210; my $chr = "chr2"; for my $s (sort {$a<=>$b} keys %{$reps{$chr}}){ if ($start <= $reps{$chr}{$s}{'end'}) { last if $s >= $end; print "$chr $s $reps{$chr}{$s}{'end'} $reps{$chr}{$s}{'rep'}\n"; } } __DATA__ chr1 100 120 feature1 chr1 200 250 feature2 chr2 150 200 feature1 chr2 280 350 feature1 chr3 100 150 feature2 chr3 300 450 feature2

My questions are:
a) Is there a better way to do this?
b) Can the memory footprint be reduced any?

Many, many thanks for any advice.

Rich


In reply to Reducing memory footprint when doing a lookup of millions of coordinates by richardwfrancis

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.