Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Storing and retrieving a hash of objects

by Frnak (Initiate)
on Aug 29, 2013 at 18:00 UTC ( #1051498=perlquestion: print w/replies, xml ) Need Help??

Frnak has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks
I'm enjoying you wisdom for quite some time now
and first of all I wanted to thank you for making my life easier and me a better perl user!

Now I ran into a problem I just can't figure out and hope you can again share your wisdom with me.

In my current script I am building an IntervalTree with the
package Set::IntervalTree of a file containing genomic
coordinates together with some other info I want to retrieve for coordinate ranges.

This works just perfect and does what I need, but now I
would like to store the built tree to disk and retrieve
it later on so that I do not have to rebuild it every
time I search for coordinates.
I used Storable already in several scripts and thought it
would be a great idea to do so again, from what I have
read in your great Object Serialization Basics this looked very promising.

So I create a hash of trees for each chromosome that is
found in my input file like this

while (<FILE>){ my @line = split (/\t/,$_); ## tab separated file my @tmp = split(/\./,$line[1]); ## chromosome name in field 2, as +part of a dot separated string $chr{"$tmp[1]"} = new Set::IntervalTree() unless (defined $chr{"$ +tmp[1]"}); $chr{"$tmp[1]"} -> insert($count,$line[2]-1,$line[3]+1); ## fields + 3 and 4 contain the coordinates, -1 and +1 to allow search for direc +t overlaps at start and end }

which works fine, then I store this hash using Storable
store (\%chr, "$file.coordinates") or die "Can't store %chr in file.co +ordinates!\n";

In a second run I check for the presence of this file and
if it exists I try to retrieve it via
my $hashref; %chr = %{$hashref = retrieve("$file.coordinates")};

But when I later on try to access coordinates in this
deserialized hash I get a segmentation fault.
I guess the problem is that I would to somehow create a
new IntervalTree object first and just fill it with the
contents from disk but I just can't figure out how.

I hope you can understand the problem from the bit of code I post.
Your help is much appreciated, thank you for your time!

Replies are listed 'Best First'.
Re: Storing and retrieving a hash of objects
by rodion (Chaplain) on Aug 29, 2013 at 21:23 UTC
    It looks like in your use of Storeable, you are not just storing a hash, but are storing a hash whose values are objects of a class. Storable won't know how to serialize and de-serialize those objects unless it can find that out from the class. The first thing to check is whether Set::IntervalTree provides the methods that Storable needs. I'm guessing that it doesn't.

    The documentation to Storable tells you about the methods it needs in the Wizards Only --> Hooks section of the docs.

    Hope that gets you the basics of what you need to decide how deep you want to get. Someone else may have a recommendation on how they have serialized Set::IntervalTree objects. --Rodion

      Hi Rodion,
      thanks for your fast answer and sorry for my late reply,
      had no internet access the last few days.
      So as I understand this de/serialization fails as
      Storable does not know how to do it for a
      Set::IntervalTree object.
      This means that I either find a way to let Storable
      know how to do it or choose an other object type to store.
      I tried to figure out how to do some Storable wizardry
      but I guess it's over the top for my problem.
      If anyone has the will to give me a hint on how one
      could do this I'm very willing to try, but I guess it
      would take some time which may be better spent otherwise.

      For the moment I'll try to use an alternative object to store my coordinates. Thanks again for your help!
        It would take a little work to put together serialization routines for Set::IntervalTree and Storable to play smoothly together. That may be worth it if you're going to make heavy use of this, but a quick workaround is to write specialized serialization and de-serializetion routines good enough for your own code and use those convert from a hash of objects to a hash of strings and back. Then you can use Storable on the hash of strings. It sounds like you're thinking along these lines already.

        One advantage of this approach is that if you decide to do the more general job, you've got some prototyping already under your belt.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1051498]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2021-10-19 15:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (77 votes). Check out past polls.

    Notices?