1 package Bio
::DB
::SeqFeature
::Store
::LoadHelper
;
5 Bio::DB::SeqFeature::Store::LoadHelper -- Internal utility for Bio::DB::SeqFeature::Store
9 # For internal use only.
18 L<Bio::DB::SeqFeature::Store>,
19 L<Bio::DB::SeqFeature::Segment>,
20 L<Bio::DB::SeqFeature::NormalizedFeature>,
21 L<Bio::DB::SeqFeature::GFF2Loader>,
22 L<Bio::DB::SeqFeature::Store::DBI::mysql>,
23 L<Bio::DB::SeqFeature::Store::berkeleydb>
27 Lincoln Stein E<lt>lstein@cshl.orgE<gt>.
29 Copyright (c) 2006 Cold Spring Harbor Laboratory.
31 This library is free software; you can redistribute it and/or modify
32 it under the same terms as Perl itself.
38 use File
::Path
'rmtree';
39 use File
::Temp
'tempdir';
41 use Fcntl
qw(O_CREAT O_RDWR);
43 our $VERSION = '1.12';
51 my $template = 'SeqFeatureLoadHelper_XXXXXX';
53 my @tmpargs = $tmpdir ?
($template,DIR
=>$tmpdir) : ($template);
54 my $tmppath = tempdir
(@tmpargs,CLEANUP
=>1);
55 my $self = $class->create_dbs($tmppath);
56 $self->{tmppath
} = $tmppath;
57 return bless $self,$class;
62 # Destroy all filehandle references
63 # before trying to delete files and folder
65 undef $self->{IndexIt
};
66 undef $self->{TopLevel
};
67 undef $self->{Local2Global
};
68 undef $self->{Parent2Child
};
69 rmtree
$self->{tmppath
};
70 # File::Temp::cleanup() unless $self->{keep};
77 # experiment with caching these handles in memory
78 my $hash_options = DB_File
::HASHINFO
->new();
79 # Each of these hashes allow only unique keys
80 for my $dbname (qw(IndexIt TopLevel Local2Global)) {
81 unless ($DBHandles{$dbname}) {
83 tie
(%h,'DB_File',File
::Spec
->catfile($tmp,$dbname),
84 O_CREAT
|O_RDWR
,0666,$hash_options);
85 $DBHandles{$dbname} = \
%h;
87 $self{$dbname} = $DBHandles{$dbname};
88 %{$self{$dbname}} = ();
91 # The Parent2Child hash allows duplicate keys, so we
92 # create it with the R_DUP flag.
93 my $btree_options = DB_File
::BTREEINFO
->new();
94 $btree_options->{flags
} = R_DUP
;
95 unless ($DBHandles{'Parent2Child'}) {
97 tie
(%h,'DB_File',File
::Spec
->catfile($tmp,'Parent2Child'),
98 O_CREAT
|O_RDWR
,0666,$btree_options);
99 $DBHandles{'Parent2Child'} = \
%h;
101 $self{Parent2Child
} = $DBHandles{'Parent2Child'};
102 %{$self{Parent2Child
}} = ();
109 $self->{IndexIt
}{$id} = shift if @_;
110 return $self->{IndexIt
}{$id};
116 $self->{TopLevel
}{$id} = shift if @_;
117 return $self->{TopLevel
}{$id};
122 my ($id) = each %{$self->{TopLevel
}};
129 $self->{Local2Global
}{$id} = shift if @_;
130 return $self->{Local2Global
}{$id};
135 my $parent_id = shift;
137 $self->{Parent2Child
}{$parent_id} = shift while @_;
142 my $parent_id = shift;
146 my $db = tied(%{$self->{Parent2Child
}});
147 my $key = $parent_id;
149 for (my $status = $db->seq($key,$value,R_CURSOR
);
150 $status == 0 && $key eq $parent_id;
151 $status = $db->seq($key,$value,R_NEXT
)
153 push @children,$value;
155 return wantarray ?
@children: \
@children;
158 # this acts like each() and returns each parent id and an array ref of children
162 my $db = tied(%{$self->{Parent2Child
}});
164 if ($self->{_cursordone
}) {
165 undef $self->{_cursordone
};
166 undef $self->{_parent
};
167 undef $self->{_child
};
171 # do a slightly tricky cursor search
172 unless (defined $self->{_parent
}) {
173 return unless $db->seq($self->{_parent
},$self->{_child
},R_FIRST
) == 0;
176 my $parent = $self->{_parent
};
177 my @children = $self->{_child
};
180 while (($status = $db->seq($self->{_parent
},$self->{_child
},R_NEXT
)) == 0
181 && $self->{_parent
} eq $parent
183 push @children,$self->{_child
};
186 $self->{_cursordone
}++ if $status != 0;
188 return ($parent,\
@children);
193 my @ids = keys %{$self->{Local2Global
}}
194 if $self->{Local2Global
};
200 my @ids = values %{$self->{Local2Global
}}
201 if $self->{Local2Global
};