Hi,

I have a series of tables with the following interval information:

start end ID 36701 40200 1 37901 39700 2 36701 39700 3
I want to find overlaps between the intervals of the IDs. Do different IDs have overlapping intervals? If so, what is the overlap and where?

I realise that the best way to approach my problem is to use an interval tree. I have been trying to use the module Set::IntervalTree but I am stuck.

Basically I am not sure how to loop through my file columns to fill the interval tree.

This is what I have so far:

#!/usr/local/bin/perl use strict; use warnings; use Set::IntervalTree; use Data::Dumper; #get the scaffold file name from user input (@ARGV) and stores in $fil +e #opens the scaffold file so that it can be used to fill the empty inte +rval tree my $file = shift; open my $fh, '<', $file or die "Cannot open $file: $!"; #create an empty interval tree my $tree = Set::IntervalTree -> new(); #loop to the file, read each line and add objects to the empty interva +l tree #there will be as many objects in the interval tree as there are hits +for the specific file my %overlap_table; while (my $line=<$fh>){ #while there are lines my @low = split("\t", $line); #get the <code> value from the 1 +st column (start position = low BT) print "$low[0]\n"; $overlap_table{$low[0]}++; my @high = split("\t", $line); #get the value from the 2nd co +lumn (end position = high BT) print "$high[1]\n"; $overlap_table{$high[1]}++; my @ID = split ("", $line); #ID information is the "value" print "$ID[2]\n"; $overlap_table{$ID[2]}++; } close($fh); print Dumper \%overlap_table;
I now want to use each one of the $low, $high and $ID to fill in the tree.

I would like to be able to loop through the file automatically.

I have lots of files with lots of lines and so entering the values manually is not a good option

To sum up, I would love if someone could help me understand how I loop through the lines of my $fh so that I can fill in the interval tree according to the following requirement:

 $tree->insert($ID, $low, $high) for each one of the lines.

Thank you so much in advance!


In reply to Loop through file to create interval tree by pinha

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.