Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2024-04-24 11:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found