blib/lib/Bio/ViennaNGS/FeatureIO.pm

filter: Statementstmt 36.0% 40 / 111 Branchbran 0.0% 0 / 34 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 64.7% 11 / 17 Podpod 40.0% 2 / 5 totaltotal 31.7% 53 / 167 CC 21 SCAR 39.5
CC
21
Cov
28%
BUILD
132.0
parse_bed6_file
56.0
parse_bedgraph_file
12.0
CRAP 52.2
« blib/lib/Bio/ViennaNGS/Util.pm ↑ Summary blib/lib/Bio/ViennaNGS/Expression.pm »
1 # -*-CPerl-*-
2 # Last changed Time-stamp: <2017-06-10 19:07:22 michl>
3 package Bio::ViennaNGS::FeatureIO;
4  
5 1 ▶use Bio::ViennaNGS;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use File::Slurp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Bio::ViennaNGS::Bed;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Bio::ViennaNGS::Feature;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Bio::ViennaNGS::FeatureChain;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Bio::ViennaNGS::BedGraphEntry;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use version; our $VERSION = version->declare("$Bio::ViennaNGS::VERSION");
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 has 'file' => ( # file path
17 is => 'ro',
18 isa => 'Str',
19 predicate => 'has_file',
20 required => 1,
21 );
22  
23 1 has 'filetype' => ( # BED6, BED12, GFF, GTF
24 is => 'ro',
25 isa => 'Str',
26 predicate => 'has_filetype',
27 required => 1,
28 );
29  
30 1 has 'instanceOf' => (
31 is => 'rw',
32 isa => 'Str', # BedGraph, ContainerFeature
33 predicate => 'has_instanceOf',
34 required => 1,
35 writer => 'set_instanceOf',
36 );
37  
38 has 'data' => (
39 is => 'rw',
40 isa => 'ArrayRef',
41 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
42 1 traits => ['Array'],
43 predicate => 'has_data',
44 handles => {
45 all => 'elements',
46 count => 'count',
47 add => 'push',
48 pop => 'pop',
49 },
50 );
51  
52 1 has '_entries' => ( # of elements in $self->data
53 is => 'rw',
54 isa => 'Int',
55 predicate => 'nr_entries',
56 init_arg => undef, # make this unsettable via constructor
57 builder => 'count_entries',
58 lazy => 1,
59 );
60  
61 sub BUILD { # call a parser method, depending on $self->instanceOf
62 0 ▶ my $self = shift;
Subroutine BUILD: not called CC=11 SCAR=48.8
Pod undocumented
63 0 my $this_function = (caller(0))[3];
64 0 my $type;
65  
66 0 ▶ confess "ERROR [$this_function] \$self->file not available"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_file 0 0 0
67 unless ($self->has_file);
68 0 ▶ confess "ERROR [$this_function] \$self->filetype not available"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_filetype 0 0 0
69 unless ($self->has_filetype);
70 0 ▶ confess "ERROR [$this_function] \$self->instanceOf not available"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_instanceOf 0 0 0
71 unless ($self->has_instanceOf);
72  
73 0 ▶ if ($self->filetype eq "BedGraph"){
Branch: 3 branches
BranchTrueFalseTotal
if ($self->filetype eq 'BedGraph') { } 0 0 0
elsif ($self->filetype =~ /[Bb]ed6/) { } 0 0 0
elsif ($self->filetype =~ /[Bb]ed12/) { } 0 0 0
74 #carp "INFO [$this_function] \$self->instanceOf is BedGraph\n";
75 0 $self->parse_bedgraph_file($self->file);
76 0 return $self->data;
77 }
78 elsif ($self->filetype =~ m/[Bb]ed6/){
79 0 ▶ if($self->instanceOf eq "Feature"){
Branch: 3 branches
BranchTrueFalseTotal
if ($self->instanceOf eq 'Feature') { } 0 0 0
elsif ($self->instanceOf eq 'FeatureChain') { } 0 0 0
elsif ($self->instanceOf eq 'FeatureChainBlock') { } 0 0 0
80 #carp "INFO [$this_function] \$self->instanceOf is Feature\n";
81 0 $type=0; # ArrayRef of individual Feature objects
82 }
83 elsif ($self->instanceOf eq "FeatureChain"){
84 #carp "INFO [$this_function] \$self->instanceOf is FeatureChain\n";
85 0 $type=1; # ArrayRef of FeatureChain objects, one per Feature object
86 }
87 elsif ($self->instanceOf eq "FeatureChainBlock"){
88 #carp "INFO [$this_function] \$self->instanceOf is FeatureChainBlock\n";
89 0 $type=2; # ArrayRef of the entire block of Features (aka Bed12 from Bed6 block)
90 }
91 else{
92 0 croak "ERROR [$this_function] Invalid type for \$self->instanceOf: $self->instanceOf";
93 }
94 0 $self->parse_bed6_file($self->file,$type);
95 0 return $self->data;
96 }
97 elsif ($self->filetype =~ m/[Bb]ed12/){
98 0 ▶ if($self->instanceOf eq "Bed"){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->instanceOf eq 'Bed') { } 0 0 0
99 #carp "INFO [$this_function] \$self->instanceOf is Bed\n";
100 0 $type=0; # ArrayRef of individual Bio::ViennaNGS::Bed objects
101 }
102 0 else {croak "ERROR [$this_function] currently only 'Bed' is a valid option for \$self->instance";}
103 0 $self->parse_bed12_file($self->file,$type);
104 0 return $self->data;
105 }
106 else{
107 0 croak "ERROR [$this_function] Invalid type for \$self->filetyp: $self->filetype";
108 }
109 0 $self->count_entries();
110 }
111  
112 sub count_entries {
113 0 ▶ my $self = shift;
Subroutine count_entries: not called CC=1 SCAR=6.9
Pod undocumented
114 0 my $cnt = scalar @{$self->data};
115 0 $self->_entries($cnt);
116 }
117  
118 sub parse_bedgraph_file{
119 0 ▶ my ($self,$filename) = @_;
Subroutine parse_bedgraph_file: not called CC=3 SCAR=24.8
Pod documented
120 0 my $this_function = (caller(0))[3];
121 0 my ($file,$line,$entry,$chr,$start,$end,$val);
122  
123 0 $file = read_file( $filename, array_ref => 1, chomp =>1 ) ;
124 0 foreach $line (@$file){
125 0 ▶ croak "ERROR [$this_function] cannot parse bedGraph input from $filename"
Branch: 1 branch
BranchTrueFalseTotal
unless {$line =~ /^([a-zA-Z0-9._]+)\t(\d+)\t(\d+)\t(-?\d+\.?\d*)$/} 0 0 0
126 unless {$line =~ m/^([a-zA-Z0-9._]+)\t(\d+)\t(\d+)\t(-?\d+\.?\d*)$/};
127 0 $chr = $1; $start = $2; $end = $3, $val = $4;
128 #print "++ \$chr $chr ++ \$start $start ++ \$end $end ++ \$val $val\n";
129 0 $entry = Bio::ViennaNGS::BedGraphEntry->new(chromosome => $chr,
130 start => $start,
131 end => $end,
132 dataValue => $val);
133 0 push @{$self->data}, $entry;
134 }
135 }
136  
137 sub parse_bed6_file{
138 0 ▶ my ($self,$file,$typ) = @_;
Subroutine parse_bed6_file: not called CC=7 SCAR=40.3
Pod undocumented
139 0 my $this_function = (caller(0))[3];
140 0 my ($line,$feat,$fc);
141 0 $file = read_file( $file, array_ref => 1, chomp =>1 );
142  
143 0 ▶ if ($typ == 2){ # initialize an empty FeatureChain object
Branch: 1 branch
BranchTrueFalseTotal
if ($typ == 2) 0 0 0
144 0 $fc = Bio::ViennaNGS::FeatureChain->new(type => "feature");
145 }
146  
147 # print "********** in parse_bed6: typ= $typ ************\n";
148 0 foreach $line (@$file){
149 0 my @feat = split /\t/,$line;
150 0 $feat = Bio::ViennaNGS::Feature->new(chromosome=>$feat[0],
151 start=>$feat[1],
152 end=>$feat[2],
153 name=>$feat[3],
154 score=>$feat[4],
155 strand=>$feat[5]);
156 0 ▶ if($typ == 0){ # ArrayRef of individual Feature objects
Branch: 3 branches
BranchTrueFalseTotal
if ($typ == 0) { } 0 0 0
elsif ($typ == 1) { } 0 0 0
elsif ($typ == 2) { } 0 0 0
157 0 push @{$self->data}, $feat;
158 }
159 elsif ($typ == 1) { # ArrayRef of FeatureChain objects, one per Feature object
160 0 $fc = Bio::ViennaNGS::FeatureChain->new(type => "feature",
161 chain => [$feat]);
162 # $fc->count_entries();
163 0 push @{$self->data}, $fc;
164 }
165 elsif($typ == 2){
166 0 $fc->add($feat);
167 0 $fc->count_entries();
168 }
169 else{
170 0 croak "ERROR [$this_function] don't know how to handle typ $typ";
171 }
172 } #end foreach
173 0 ▶ if ($typ == 2) { push @{$self->data}, $fc; }
Branch: 1 branch
BranchTrueFalseTotal
if ($typ == 2) 0 0 0
174 # print Dumper($self);
175 }
176  
177 sub parse_bed12_file{
178 0 ▶ my ($self,$file,$typ) = @_;
Subroutine parse_bed12_file: not called CC=3 SCAR=24.8
Pod documented
179 0 my $this_function = (caller(0))[3];
180 0 my ($line,$feat,$fc);
181 0 $file = read_file( $file, array_ref => 1, chomp =>1 );
182 0 foreach $line (@$file){
183 0 my @mcData = split /\t/,$line;
184 0 ▶ if ($typ == 0){ # ArrayRef of Bio::ViennaNGS::Bed objects
Branch: 1 branch
BranchTrueFalseTotal
if ($typ == 0) 0 0 0
185 0 my $bo = Bio::ViennaNGS::Bed->new(chromosome => $mcData[0],
186 start => $mcData[1],
187 end => $mcData[2],
188 name => $mcData[3],
189 score => $mcData[4],
190 strand => $mcData[5],
191 thickStart => $mcData[6],
192 thickEnd => $mcData[7],
193 itemRgb => $mcData[8],
194 blockCount => $mcData[9],
195 blockSizes => $mcData[10],
196 blockStarts => $mcData[11],
197 );
198 0 push @{$self->data}, $bo;
199 }
200 }
201  
202 }
203  
204 1 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
205  
206 1 1;
207  
208 __END__
209  
210 =head1 NAME
211  
212 Bio::ViennaNGS::FeatureIO - Versatile I/O interface for Bio::ViennaNGS
213 feature annotation classes
214  
215 =head1 SYNOPSIS
216  
217 use Bio::ViennaNGS::FeatureIO;
218  
219 # initialize a FeatureIO object from a Bed6 file
220 my $data_bed = Bio::ViennaNGS::FeatureIO->new(
221 file => "file.bed6",
222 filetype => 'Bed6',
223 instanceOf => 'Feature',
224 );
225  
226 # initialize a FeatureIO object from a Bed12 file
227 my $data_bed = Bio::ViennaNGS::FeatureIO->new(
228 file => "file.bed12",
229 filetype => 'Bed12',
230 instanceOf => 'Bed',
231 );
232  
233 # initialize a FeatureIO object from a bedGraph file
234 my $obj = Bio::ViennaNGS::FeatureIO->new(file => "file.bg",
235 filetype => "BedGraph",
236 instanceOf => "BedGraph",
237 );
238  
239  
240 =head1 DESCRIPTION
241  
242 This module provides an object-oriented interface for easy
243 input/output operations on common feature annotation file formats. It
244 is - by design - a very generic module that stores all annotation data
245 within the C<$self-E<gt>data> ArrayRef. C<$self-E<gt>filetype>
246 specifies the file type to be processed. Currently parsing of I<Bed6>,
247 I<Bed12> and I<bedGraph> files is supported. C<$self-E<gt>instanceOf>
248 determines the object type of elements held in the C<$self-E<gt>data>
249 ArrayRef(s).
250  
251 In case of parsing Bed6 data, C<$self-E<gt>instanceOf> can either be
252 C<Feature>, C<FeatureChain> or C<FeatureChainBlock>. While the first
253 causes C<$self-E<gt>data> to hold an ArrayRef to individual
254 L<Bio::ViennaNGS::Feature> objects, the second triggers creation of
255 individual L<Bio::ViennaNGS::FeatureChain> objects (each containing
256 exactly one feature interval, corresponding to individual Bed6
257 entries). A C<FeatureChainBlock> value to the C<$self-E<gt>instanceOf>
258 causes C<$self-E<gt>data> to hold an ArrayRef to a combined
259 L<Bio::ViennaNGS::FeatureChain> containing an entire block of
260 features. In the context of Bed annotation this corresponds to a
261 single Bed12 line (e.g. gene/transcript) that contains all individual
262 Bed6 features (e.g. exons). Evidently, this only makes sense if all
263 Bed6 features originate from the same chromosome and strand.
264  
265 In case of parsing Bed12 data, currently only C<Bed> is supported for
266 C<$self-E<gt>instanceOf>, causing C<$self-E<gt>data> to hold an
267 ArrayRef to L<Bio::ViennaNGS::Bed> (aka Bed12) features. This will be
268 adjusted to L<Bio::ViennaNGS::FeatureChain> in the future.
269  
270 In case of pasring bedGraph data, C<$self-E<gt>instanceOf> is ignored
271 and C<$self-E<gt>data> holds an ArrayRef to individual
272 L<Bio::ViennaNGS::BedGraph> objects.
273  
274 =head1 METHODS
275  
276 =over
277  
278 =item parse_bedgraph_file
279  
280 =item parse_bed6_file.
281  
282 =item parse_bed12_file
283  
284 These methods are used for object construction and should not be
285 called directly.
286  
287 =back
288  
289 =head1 DEPENDENCIES
290  
291 =over
292  
293 =item L<Bio::ViennaNGS::Bed>
294  
295 =item L<Bio::ViennaNGS::Feature>
296  
297 =item L<Bio::ViennaNGS::FeatureChain>
298  
299 =item L<Bio::ViennaNGS::BedGraphEntry>
300  
301 =item L<File::Slurp>
302  
303 =item L<Carp>
304  
305 =back
306  
307 =head1 AUTHORS
308  
309 Michael T. Wolfinger E<lt>michael@wolfinger.euE<gt>
310  
311 =head1 COPYRIGHT AND LICENSE
312  
313 Copyright (C) 2014-2016 Michael T. Wolfinger E<lt>michael@wolfinger.euE<gt>
314  
315 This library is free software; you can redistribute it and/or modify
316 it under the same terms as Perl itself, either Perl version 5.10.0 or,
317 at your option, any later version of Perl 5 you may have available.
318  
319 This program is distributed in the hope that it will be useful, but
320 WITHOUT ANY WARRANTY; without even the implied warranty of
321 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
322 General Public License for more details.
323  
324  
325 =cut
« blib/lib/Bio/ViennaNGS/Util.pm ↑ Summary blib/lib/Bio/ViennaNGS/Expression.pm »