File Coverage

blib/lib/Bio/Polloc/LocusIO.pm
Criterion Covered Total %
statement 55 67 82.0
branch 14 30 46.6
condition 6 8 75.0
subroutine 11 14 78.5
pod 5 5 100.0
total 91 124 73.3


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             Bio::Polloc::LocusIO - I/O interface of C<Bio::Polloc::Locus::*> objects
4              
5             =head1 AUTHOR - Luis M. Rodriguez-R
6              
7             Email lmrodriguezr at gmail dot com
8              
9             =head1 IMPLEMENTS OR EXTENDS
10              
11             =over
12              
13             =item *
14              
15             L<Bio::Polloc::Polloc::Root>
16              
17             =item *
18              
19             L<Bio::Polloc::Polloc::IO>
20              
21             =back
22              
23             =head1 SYNOPSIS
24              
25             Read & write loci:
26              
27             use strict;
28             use Bio::Polloc::LocusIO;
29              
30             my $locusI = Bio::Polloc::LocusIO->new(-file=>"t/loci.gff3", -format=>"gff3");
31             my $locusO = Bio::Polloc::LocusIO->new(-file=>">out.gff3", -format=>"gff3");
32              
33             while(my $locus = $locusI->next_locus){
34             print "Got a ", $locus->type, " from ", $locus->from, " to ", $locus->to, "\n";
35             # Filter per type
36             if($locus->type eq "repeat"){
37             $locusO->write_locus($locus);
38             }
39             }
40              
41             =cut
42              
43             package Bio::Polloc::LocusIO;
44 2     2   1627 use strict;
  2         3  
  2         80  
45 2     2   11 use base qw(Bio::Polloc::Polloc::Root Bio::Polloc::Polloc::IO);
  2         3  
  2         1731  
46             our $VERSION = 1.0503; # [a-version] from Bio::Polloc::Polloc::Version
47              
48              
49             =head1 PUBLIC METHODS
50              
51             Methods provided by the package
52              
53             =head2 new
54              
55             The basic initialization method
56              
57             =cut
58              
59             sub new {
60 4     4 1 869 my($caller,@args) = @_;
61 4   33     30 my $class = ref($caller) || $caller;
62            
63 4 50       15 if($class !~ m/Bio::Polloc::LocusIO::(\S+)/){
64 4         26 my $bme = Bio::Polloc::Polloc::Root->new(@args);
65 4         27 my($format, $file) = $bme->_rearrange([qw(FORMAT FILE)], @args);
66              
67 4 100 100     38 ($format = $file) =~ s/^.*\.// if $file and not $format;
68 4 50       13 if($format){
69 4         17 $format = __PACKAGE__->_qualify_format($format);
70 4 50       38 $class = "Bio::Polloc::LocusIO::" . $format if $format;
71             }
72             }
73              
74 4 50       39 if($class =~ m/Bio::Polloc::LocusIO::(\S+)/){
75 4         7 my $load = 0;
76 4 50       36 if(__PACKAGE__->_load_module($class)){
77 4         10 $load = $class;
78             }
79            
80 4 50       12 if($load){
81 4         37 my $self = $load->SUPER::new(@args);
82 4         32 $self->debug("Got the LocusIO class $load");
83 4         19 $self->_initialize(@args);
84 4         24 return $self;
85            
86             }
87            
88 0         0 my $bme = Bio::Polloc::Polloc::Root->new(@args);
89 0         0 $bme->throw("Impossible to load the module", $class);
90             }
91 0         0 my $bme = Bio::Polloc::Polloc::Root->new(@args);
92 0         0 $bme->throw("Impossible to load the proper Bio::Polloc::LocusI class with ".
93             "[".join("; ",@args)."]", $class);
94             }
95              
96             =head2 format
97              
98             Gets/sets the format of the file
99              
100             =over
101              
102             =item Arguments
103              
104             Format (str), currently supported: gff3.
105              
106             =item Return
107              
108             Format (str or C<undef>).
109              
110             =back
111              
112             =cut
113              
114             sub format {
115 0     0 1 0 my($self,$value) = @_;
116 0 0       0 if($value){
117 0         0 my $v = $self->_qualify_format($value);
118 0 0       0 $self->throw("Attempting to set an invalid type of locus",$value) unless $v;
119 0         0 $self->{'_format'} = $v;
120             }
121 0         0 return $self->{'_format'};
122             }
123              
124             =head2 write_locus
125              
126             Appends one locus to the output file.
127              
128             =over
129              
130             =item Arguments
131              
132             =over
133              
134             =item -locus I<Bio::Polloc::LocusI>, mandatory
135              
136             The locus to append.
137              
138             =item -force I<Bool (int)>
139              
140             If true, forces re-parsing of the locus. Otherwise,
141             tries to load cached parsing (if any).
142              
143             =back
144              
145             =back
146              
147             =cut
148              
149             sub write_locus {
150 3     3 1 9 my($self, @args) = @_;
151 3         18 my($locus) = $self->_rearrange([qw(LOCUS)], @args);
152 3 50       12 $self->throw("You must provide the locus to append") unless defined $locus;
153 3 50       16 $self->throw("The obtained locus is not an object", $locus)
154             unless UNIVERSAL::can($locus, 'isa');
155 3         13 $self->_write_locus_impl(@args);
156             }
157              
158             =head2 read_loci
159              
160             Gets the loci stored in the input file.
161              
162             =over
163              
164             =item Arguments
165              
166             =over
167              
168             =item -genomes I<arrayref of Bio::Polloc::Genome objects>
169              
170             An arrayref containing the L<Bio::Polloc::Genome> objects associated to
171             the collection of loci. This is not mandatory, but C<seq> and
172             C<genome> properties will not be set on the newly created objects
173             if this parameter is not provided.
174              
175             =back
176              
177             =item Returns
178              
179             A L<Bio::Polloc::LociGroup> object.
180              
181             =back
182              
183             =cut
184              
185 3     3 1 1108 sub read_loci { return shift->_read_loci_impl(@_) }
186              
187             =head2 next_locus
188              
189             Reads the next locus in the buffer.
190              
191             =over
192              
193             =item Arguments
194              
195             Same of L<read_loci>
196              
197             =item Returns
198              
199             A L<Bio::Polloc::LocusI> object.
200              
201             =back
202              
203             =cut
204              
205 90     90 1 270 sub next_locus { return shift->_next_locus_impl(@_) }
206              
207             =head1 INTERNAL METHODS
208              
209             Methods intended to be used only within the scope of Bio::Polloc::*
210              
211             =head2 _qualify_format
212              
213             Uniformizes the distinct names that every format can receive
214              
215             =over
216              
217             =item Arguments
218              
219             The requested format (str)
220              
221             =item Returns
222              
223             The qualified format (str or undef)
224              
225             =back
226              
227             =cut
228              
229             sub _qualify_format {
230 4     4   9 my($self,$value) = @_;
231 4 50       11 return unless $value;
232 4 50       29 $value = 'gff3' if $value =~ /^gff3?$/i;
233 4         12 $value = lc $value;
234 4         11 return $value;
235             }
236              
237             =head2 _write_locus_impl
238              
239             Format-specific implementation of C<write_locus>.
240              
241             =cut
242              
243             sub _write_locus_impl {
244 0     0   0 $_[0]->throw("_write_locus_impl", $_[0], 'Bio::Polloc::Polloc::UnimplementedException');
245             }
246              
247             =head2 _read_loci_impl
248              
249             Format-specific implementation of C<next_locus>.
250              
251             =cut
252              
253             sub _read_loci_impl {
254 3     3   7 my ($self,@args) = @_;
255 3         16 my($genomes) = $self->_rearrange([qw(GENOMES)], @args);
256 3         26 my $group = Bio::Polloc::LociGroup->new(-genomes=>$genomes);
257 3         17 while(my $locus = $self->next_locus(@args)){
258 87         306 $group->add_locus($locus);
259             }
260 3         15 return $group;
261             }
262              
263             =head2 _next_locus_impl
264              
265             =cut
266              
267             sub _next_locus_impl {
268 0     0   0 $_[0]->throw("_next_locus_impl", $_[0], 'Bio::Polloc::Polloc::UnimplementedException');
269             }
270              
271             =head2 _save_locus
272              
273             =cut
274              
275             sub _save_locus {
276 87     87   119 my($self, $locus) = @_;
277 87   100     213 $self->{'_saved_loci'}||= [];
278 87 50       166 push @{$self->{'_saved_loci'}}, $locus if defined $locus;
  87         174  
279 87         628 return $locus;
280             }
281              
282             =head2 _locus_by_id
283              
284             =cut
285              
286             sub _locus_by_id {
287 68     68   105 my($self, $id) = @_;
288 68 50       315 return unless defined $id;
289 68         77 my @col = grep { $_->id eq $id } @{$self->{'_saved_loci'}};
  4386         10283  
  68         732  
290 68         499 return $col[0];
291             }
292              
293             =head2 _initialize
294              
295             =cut
296              
297             sub _initialize {
298 4     4   8 my $self = shift;
299 4         27 $self->_initialize_io(@_);
300             }
301              
302             1;