blib/lib/Bio/Gonzales/IDMapIO.pm untested

filter: Statementstmt 0.0% 0 / 99 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 14 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 22 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 182 CC 17 SCAR 23.6
CC
17
Cov
0%
_initialize_idmapio
30.0
map_prefix
20.0
map_add_custom
12.0
CRAP 10.6
1 #Copyright (c) 2010 Joachim Bargsten <code at bargsten dot org>. All rights reserved.
2  
3 package Bio::Gonzales::IDMapIO;
4  
5 use warnings;
6 use strict;
7 use Carp;
8  
9 use 5.010;
10  
11 use parent qw/Bio::Root::Root/;
12 use YAML qw/freeze thaw/;
13 use Data::Dumper;
14 our $VERSION = '0.062'; # VERSION
15  
16 our $MAP_PREFIX = 's';
17 our $IDLENGTH = 9;
18  
19 =head1 NAME
20  
21 Bio::Gonzales::IDMapIO - remap ids of 'objects', should be used in IO-classes, like Bio::SeqIO
22  
23 =head1 SYNOPSIS
24  
25  
26 =head1 DESCRIPTION
27  
28 =head1 METHODS
29  
30 =cut
31 sub _initialize_idmapio {
32 my ( $self, @args ) = @_;
33 $self->SUPER::_initialize(@args);
34  
35 my ( $fh, $file, $prefix, $id_length, $start_idx, $cache )
36 = $self->_rearrange( [qw(map_fh map_file map_prefix map_id_length map_start_idx map_cache)], @args );
37  
38 $self->{'_map_id_len'} = $IDLENGTH;
39 $self->_map_id_len($id_length);
40 $self->map_prefix( $prefix // $MAP_PREFIX );
41 $self->map_reset_id;
42 $self->map_reset_cache;
43 $self->{'_mapidx'} = $start_idx - 1
44 if ( defined $start_idx );
45 $self->{'_cache'} = $cache
46 if ( defined $cache );
47  
48 if ( $fh || $file ) {
49 $self->{'_map_io'} = Bio::Root::IO->new;
50 $self->{'_map_io'}->_initialize_io( -fh => $fh, -file => $file );
51 $self->_cache_from_io( $self->{'_map_io'} ) if ( $self->{'_map_io'}->mode eq 'r' && !defined($cache) );
52 }
53 }
54  
55 =head2 $io->map_reset_id
56  
57 =cut
58 sub map_reset_id {
59 my ($self) = @_;
60 $self->{'_mapidx'} = -1;
61 }
62  
63 =head2 $io->map_reset_cache
64  
65 =cut
66 sub map_reset_cache {
67 my ($self) = @_;
68  
69 $self->{'_cache'} = {};
70 }
71  
72 =head2 $io->map_add
73  
74 =cut
75 sub map_add {
76 my ( $self, $real_id, $desc ) = @_;
77  
78 $self->{'_mapidx'}++;
79 $self->map_add_custom( $self->map_id_current, $real_id, $desc );
80 $self->warn("map id exceeds id length")
81 if ( length( $self->map_prefix . $self->{'_mapidx'} ) > $self->_map_id_len );
82 return $self->map_id_current;
83 }
84  
85 =head2 $io->map_add_custom
86  
87 =cut
88 sub map_add_custom {
89 my ( $self, $map_id, $real_id, $desc ) = @_;
90  
91 $self->throw("Did not provide a valid custom map id for proper mapping")
92 unless defined $map_id;
93 $self->throw("Did not provide a valid id for proper mapping")
94 unless defined $real_id;
95 $desc //= '';
96  
97 $self->{'_cache'}->{$map_id} = { real_id => $real_id, desc => $desc };
98 return $map_id;
99 }
100  
101 =head2 $io->map_write
102  
103 =cut
104 sub map_write {
105 my ( $self, @args ) = @_;
106  
107 my $map_io;
108 if ( $self->{'_map_io'} ) {
109 $map_io = $self->{'_map_io'};
110 } elsif ( @args > 0 ) {
111 $map_io = Bio::Root::IO->new;
112 $map_io->_initialize_io(@args);
113 } else {
114 $self->throw("no io args in constructor and no io args in function args");
115 }
116  
117 $map_io->_print( freeze( $self->{_cache} ) );
118 $map_io->close();
119 }
120  
121 =head2 $io->map_id_current
122  
123 =cut
124 sub map_id_current {
125 my ($self) = @_;
126  
127 return $self->{'_map_prefix'}
128 . sprintf( "%0." . ( $self->_map_id_len - $self->_map_prefix_len ) . "d", $self->{'_mapidx'} );
129 }
130  
131 =head2 $io->map_lookup_id
132  
133 =cut
134 sub map_lookup_id {
135 my ( $self, $map_id ) = @_;
136  
137 return $self->{'_cache'}->{$map_id};
138 }
139  
140 =head2 $io->map_prefix
141  
142 =cut
143 sub map_prefix {
144 my ( $self, $value ) = @_;
145 if ( defined $value ) {
146 $self->throw("map prefix starts with >") if ( $value =~ /^>/ );
147 $self->throw("map prefix too long") if ( length($value) > $self->_map_id_len - 1 );
148 $self->{'_map_prefix'} = $value;
149 }
150 return $self->{'_map_prefix'};
151 }
152  
153 sub _map_id_len {
154 my ( $self, $len ) = @_;
155 return $self->{'_map_id_len'} unless ( defined($len) );
156 $self->throw("map id shorterd than prefix")
157 if ( exists( $self->{'_map_prefix'} ) && $len < length( $self->{'_map_prefix'} ) );
158 $self->{'_map_id_len'} = $len;
159 return $len;
160 }
161  
162 sub _map_prefix_len {
163 my ($self) = @_;
164  
165 return length( $self->{'_map_prefix'} );
166 }
167  
168 =head2 $io->map_idx($new_index_value)
169  
170 set the index value (00001 in S00001)
171  
172 =cut
173 sub map_idx {
174 my ( $self, $idx ) = @_;
175 $self->{'_mapidx'} = $idx
176 if ( defined $idx );
177  
178 return $idx;
179 }
180  
181 =head2 $io->map
182  
183 return the map
184  
185 =cut
186 sub map {
187 my ($self) = @_;
188
189 return $self->{'_cache'};
190 }
191  
192 =head2 $io->map_read
193  
194 read the map from a file
195  
196 =cut
197 sub map_read {
198 my ( $self, @args ) = @_;
199  
200 $self->throw("no io args in function args")
201 unless ( @args > 0 );
202  
203 my $map_io = Bio::Root::IO->new;
204 $map_io->_initialize_io(@args);
205 $self->{'_map_io'} = $map_io;
206  
207 $self->_cache_from_io($map_io);
208 }
209  
210 sub _cache_from_io {
211 my ( $self, $map_io ) = @_;
212  
213 my $yaml_string = '';
214 while ( my $l = $map_io->_readline ) {
215 $yaml_string .= $l;
216 }
217 $self->{'_cache'} = thaw($yaml_string);
218 }
219  
220 1;
221 __END__
222  
223 =head1 SEE ALSO
224  
225 =head1 AUTHOR
226  
227 jw bargsten, C<< <joachim.bargsten at wur.nl> >>
228  
229 =cut