blib/lib/Bio/ViennaNGS/AnnoC.pm

filter: Statementstmt 18.0% 32 / 177 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 53.3% 8 / 15 Podpod 100% 3 / 3 totaltotal 15.4% 43 / 278 CC 40 SCAR 52.4
CC
40
Cov
13%
features2bed
420.0
parse_gff
110.0
feature_summary
72.0
CRAP 188.5
« blib/lib/Bio/ViennaNGS/SpliceJunc.pm ↑ Summary blib/lib/Bio/ViennaNGS/UCSC.pm »
1 # -*-CPerl-*-
2 # Last changed Time-stamp: <2017-06-10 18:18:45 michl>
3  
4 package Bio::ViennaNGS::AnnoC;
5 1 ▶use Bio::ViennaNGS;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Bio::ViennaNGS::Util qw(sortbed);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Bio::Tools::GFF;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Path::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use version; our $VERSION = version->declare("$Bio::ViennaNGS::VERSION");
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 #my $VERSION = $Bio::ViennaNGS::VER;
15  
16 1 has 'accession' => (
17 is => 'rw',
18 isa => 'Str',
19 predicate => 'has_accession',
20 );
21  
22 has 'features' => (
23 is => 'ro',
24 isa => 'HashRef',
25 predicate => 'has_features',
26 0 ▶ default => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
27 1 );
28  
29 1 has 'nr_features' => (
30 is => 'ro',
31 isa => 'Int',
32 builder => '_get_nr_of_features',
33 lazy => 1,
34 );
35  
36 1 has 'featstat' => (
37 is => 'ro',
38 isa => 'HashRef',
39 builder => '_set_featstat',
40 predicate => 'has_featstat',
41 lazy => 1,
42 );
43  
44 before 'featstat' => sub {
45 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
46 0 $self->_get_nr_of_features();
47 1 };
48  
49 sub _set_featstat {
50 0 ▶ my $self = shift;
Subroutine _set_featstat: not called CC=4 SCAR=30.0
51 0 my $this_function = (caller(0))[3];
52 0 my %fs = ();
53 0 ▶ confess "ERROR [$this_function] \$self->features not available"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_features 0 0 0
54 unless ($self->has_features);
55 0 $fs{total} = 0;
56 0 $fs{origin} = "$this_function ".$VERSION;
57 0 $fs{count} = $self->nr_features;
58 0 foreach my $uid ( keys %{$self->features} ){
59 0 my $gbkey = ${$self->features}{$uid}->{gbkey};
60 0 $fs{total} += 1;
61 0 ▶ unless (exists $fs{$gbkey}){
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $fs{$gbkey}) 0 0 0
62 0 $fs{$gbkey} = 0;
63 }
64 0 $fs{$gbkey} += 1;
65 }
66 0 return \%fs;
67 }
68  
69 sub _get_nr_of_features {
70 0 ▶ my $self = shift;
Subroutine _get_nr_of_features: not called CC=2 SCAR=17.9
71 0 my $this_function = (caller(0))[3];
72 0 ▶ confess "ERROR [$this_function] \$self->features not available"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_features 0 0 0
73 unless ($self->has_features);
74 0 return (keys %{$self->features});
75 }
76  
77 sub parse_gff {
78 0 ▶ my ($self,$in_file) = @_;
Subroutine parse_gff: not called CC=10 SCAR=47.0
Pod documented
79 0 my ($i,$gffio,$header,$f,$gbkey);
80 0 my $this_function = (caller(0))[3];
81  
82 0 $gffio = Bio::Tools::GFF->new(-file => $in_file,
83 -gff_version => 3,
84 );
85 0 $gffio->ignore_sequence(1);
86 0 ▶ if ($header = $gffio->next_segment() ){
Branch: 1 branch
BranchTrueFalseTotal
if ($header = $gffio->next_segment) { } 0 0 0
87 0 $self->accession( $header->display_id() );
88 }
89 0 else{ carp "ERROR [$this_function] Cannot parse GFF header\n" }
90  
91 0 ▶ while($f = $gffio->next_feature()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f = $gffio->next_feature) 0 0 0
92 0 my ($uid,$feat_name);
93 0 my @name = my @id = my @gbkeys = ();
94  
95 0 ▶ next if ($f->primary_tag() eq "exon");
Branch: 1 branch
BranchTrueFalseTotal
if $f->primary_tag eq "exon" 0 0 0
96  
97 # 1) determine gbkey of the current feature
98 0 @gbkeys = $f->get_tag_values("gbkey");
99 0 $gbkey = $gbkeys[0];
100  
101 # 2) get a unique ID for each feature
102 0 ▶ if ($f->has_tag('ID')){
Branch: 1 branch
BranchTrueFalseTotal
if ($f->has_tag('ID')) { } 0 0 0
103 0 @id = $f->get_tag_values('ID');
104 0 $uid = $id[0]; # ID=id101
105 }
106 else {
107 0 croak "ERROR [$this_function] Feature '$gbkey' at pos.\
108 $f->start does not have \'ID\' attribute\n";
109 }
110  
111 # 3) assign parent's unique ID in case a parent record exists
112 0 ▶ if ($f->has_tag('Parent')){
Branch: 1 branch
BranchTrueFalseTotal
if ($f->has_tag("Parent")) 0 0 0
113 0 @id = $f->get_tag_values('Parent');
114 0 $uid = $id[0]; # ID=id101
115 }
116  
117 # 4) find a name for the current feature, use 'Name' or 'ID' attribute
118 0 ▶ if ($f->has_tag('Name')){
Branch: 2 branches
BranchTrueFalseTotal
if ($f->has_tag('Name')) { } 0 0 0
elsif ($f->has_tag('ID')) { } 0 0 0
119 0 @name = $f->get_tag_values('Name');
120 0 $feat_name = $name[0];
121 }
122 elsif ($f->has_tag('ID')){
123 0 @id = $f->get_tag_values('ID');
124 0 $feat_name = $id[0]; # ID=id101, use ID as feature name
125 }
126 else {
127 0 croak "ERROR [$this_function] Cannot set name for feature \
128 $f->gbkey at pos. $f->start\n";
129 }
130  
131 0 ▶ unless (exists ${$self->features}{$uid}) { # gene / ribosome_entry_site / etc.
Branch: 1 branch
BranchTrueFalseTotal
if (exists do { $self->features }->{$uid}) { } 0 0 0
132 0 ${$self->features}{$uid}->{start} = $f->start;
133 0 ${$self->features}{$uid}->{end} = $f->end;
134 0 ${$self->features}{$uid}->{strand} = $f->strand;
135 0 ${$self->features}{$uid}->{length} = $f->length;
136 0 ${$self->features}{$uid}->{seqid} = $f->seq_id;
137 0 ▶ ${$self->features}{$uid}->{score} = $f->score || 0;
Condition0%0/2
$f->score || 0
l!l
00
MC/DC0%0/1
$f->score || 0
$f->score
Truth table0%0/2
A
A= $f->score
Aresult
11
00
138 0 ${$self->features}{$uid}->{gbkey} = $gbkey;
139 0 ${$self->features}{$uid}->{name} = $feat_name;
140 0 ${$self->features}{$uid}->{uid} = $uid;
141 }
142 else { # CDS / tRNA / rRNA / etc.
143 0 ${$self->features}{$uid}->{gbkey} = $gbkey; # gbkey for tRNA/ rRNA/ CDS etc
144 }
145 }
146 0 $gffio->close();
147 }
148  
149 sub features2bed {
150 0 ▶ my ($self,$gbkey,$dest,$bn,$log) = @_;
Subroutine features2bed: not called CC=20 SCAR=60.4
Pod documented
151 0 my ($chrom,$chrom_start,$chrom_end,$name,$score,$strand,$thick_start);
152 0 my ($thick_end,$reserved,$block_count,$block_sizes,$block_starts);
153 0 my @ft = ();
154 0 my $this_function = (caller(0))[3];
155  
156 0 ▶ croak "ERROR [$this_function] $self->features not available"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_features 0 0 0
157 unless ($self->has_features);
158 0 ▶ croak "ERROT [$this_function] $self->featstat not available"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_featstat 0 0 0
159 unless ($self->has_featstat);
160 0 ▶ croak "ERROR [$this_function] $dest does not exist"
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dest 0 0 0
161 unless (-d $dest);
162 0 ▶ if (defined $log){open(LOG, ">>", $log) or croak $!;}
Branch: 2 branches
BranchTrueFalseTotal
if (defined $log) 0 0 0
open LOG, ">>", $log or croak($!) 0 0 0
163  
164 0 ▶ if (defined $gbkey){ # dump features of just one genbank key
Branch: 1 branch
BranchTrueFalseTotal
if (defined $gbkey) { } 0 0 0
165 confess "ERROR [$this_function] genbank key \'$gbkey\' N/A in hash "
166 0 ▶ unless (exists ${$self->featstat}{$gbkey});
Branch: 1 branch
BranchTrueFalseTotal
unless exists do { $self->featstat }->{$gbkey} 0 0 0
167 0 $ft[0] = $gbkey;
168 }
169 else{ # dump features for all genbank keys
170 0 foreach my $gbk (keys %{$self->featstat}) {
171 0 ▶ next if ($gbk eq 'total' || $gbk eq 'Src' || $gbk eq 'accession' ||
Branch: 1 branch
BranchTrueFalseTotal
if $gbk eq "total" or $gbk eq "Src" or $gbk eq "accession" or $gbk eq "origin" or $gbk eq "count" 0 0 0
Condition0%0/12
$gbk eq "total" or $gbk eq "Src" or $gbk eq "accession" or $gbk eq "origin" or $gbk eq "count"
l!l&&r!l&&!r
000
$gbk eq "total" or $gbk eq "Src" or $gbk eq "accession" or $gbk eq "origin"
l!l&&r!l&&!r
000
$gbk eq "total" or $gbk eq "Src" or $gbk eq "accession"
l!l&&r!l&&!r
000
$gbk eq "total" or $gbk eq "Src"
l!l&&r!l&&!r
000
MC/DC0%0/5
$gbk eq "total" or $gbk eq "Src" or $gbk eq "accession" or $gbk eq "origin" or $gbk eq "count"
$gbk eq "total" $gbk eq "Src" $gbk eq "accession" $gbk eq "origin" $gbk eq "count"
Truth table0%0/6
A or B or C or D or E
A= $gbk eq "total"B= $gbk eq "Src"C= $gbk eq "accession"D= $gbk eq "origin"E= $gbk eq "count"
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
172 $gbk eq 'origin' || $gbk eq 'count');
173 0 push @ft,$gbk;
174 }
175 }
176  
177 0 foreach my $f (@ft){
178 0 my $bedname = file($dest,"$bn.$f.bed");
179 0 my $bedname_u = file($dest,"$bn.$f.u.bed");
180 0 ▶ open (BEDOUT, "> $bedname_u") or croak $!;
Branch: 1 branch
BranchTrueFalseTotal
open BEDOUT, "> $bedname_u" or croak($!) 0 0 0
181  
182 # dump unsorted gene annotation from DS to BED12
183 0 foreach my $uid (keys %{$self->features}){
184 0 ▶ next unless (${$self->features}{$uid}->{gbkey} eq $f);
Branch: 1 branch
BranchTrueFalseTotal
unless do { $self->features }->{$uid}{"gbkey"} eq $f 0 0 0
185 0 my @bedline = ();
186 0 $chrom = ${$self->features}{$uid}->{seqid};
187 0 $chrom_start = ${$self->features}{$uid}->{start};
188 0 $chrom_start--; # BED is 0-based
189 0 $chrom_end = ${$self->features}{$uid}->{end};
190 0 $name = ${$self->features}{$uid}->{name};
191 0 $score = ${$self->features}{$uid}->{score};
192 0 ▶ $strand = ${$self->features}{$uid}->{strand} == -1 ? '-' : '+'; #default to +
Branch: 1 branch
BranchTrueFalseTotal
do { $self->features }->{$uid}{'strand'} == -1 ? : 0 0 0
193 0 $thick_start = $chrom_start;
194 0 $thick_end = $chrom_end;
195 0 $reserved = 0;
196 0 $block_count = 1;
197 0 $block_sizes = ${$self->features}{$uid}->{length}.",";
198 0 $block_starts = "0,";
199 0 @bedline = join ("\t", ($chrom,$chrom_start,$chrom_end,
200 $name,$score,$strand,$thick_start,
201 $thick_end,$reserved,$block_count,
202 $block_sizes, $block_starts));
203 0 print BEDOUT "@bedline\n";
204 }
205 0 close (BEDOUT);
206  
207 0 sortbed($bedname_u,".",$bedname,1,undef); # sort bed file
208  
209 } # end foreach
210 0 ▶ if (defined $log){close(LOG)};
Branch: 1 branch
BranchTrueFalseTotal
if (defined $log) 0 0 0
211 }
212  
213 sub feature_summary {
214 0 ▶ my ($self, $dest) = @_;
Subroutine feature_summary: not called CC=8 SCAR=42.8
Pod documented
215 0 my ($fn,$fh);
216 0 my $this_function = (caller(0))[3];
217  
218 0 ▶ croak "ERROR [$this_function] $dest does not exist\n"
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dest 0 0 0
219 unless (-d $dest);
220 0 ▶ croak "ERROR [$this_function] $self->accession not available\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_accession 0 0 0
221 unless ($self->has_accession);
222  
223 0 $fn = dir($dest,$self->accession.".summary.txt");
224 0 ▶ open $fh, ">", $fn or croak $!;
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">", $fn or croak($!) 0 0 0
225  
226 0 print $fh "Accession\t ".$self->accession."\n";
227 0 print $fh "Origin \t ${$self->featstat}{origin}\n";
228 0 foreach my $ft (sort keys %{$self->featstat}){
229 0 ▶ next if ($ft =~ /total/ || $ft =~ /accession/ || $ft =~ /origin/);
Branch: 1 branch
BranchTrueFalseTotal
if $ft =~ /total/ or $ft =~ /accession/ or $ft =~ /origin/ 0 0 0
Condition0%0/6
$ft =~ /total/ or $ft =~ /accession/ or $ft =~ /origin/
l!l&&r!l&&!r
000
$ft =~ /total/ or $ft =~ /accession/
l!l&&r!l&&!r
000
MC/DC0%0/3
$ft =~ /total/ or $ft =~ /accession/ or $ft =~ /origin/
$ft =~ /total/ $ft =~ /accession/ $ft =~ /origin/
Truth table0%0/4
A or B or C
A= $ft =~ /total/B= $ft =~ /accession/C= $ft =~ /origin/
ABCresult
1XX1
01X1
0011
0000
230 0 print $fh "$ft\t${$self->featstat}{$ft}\n";
231 }
232 0 print $fh "Total\t${$self->featstat}{total}\n";
233 0 close $fh;
234 }
235  
236 1 __PACKAGE__->meta->make_immutable;
237  
238 1 1;
239  
240 __END__
241  
242 =head1 NAME
243  
244 Bio::ViennaNGS::AnnoC - Object-oriented interface for storing and
245 converting biological sequence annotation formats
246  
247 =head1 SYNOPSIS
248  
249 use Bio::ViennaNGS::AnnoC;
250  
251 my $obj = Bio::ViennaNGS::AnnoC->new();
252  
253 # parse GFF3 file to internal data straucture
254 $obj->parse_gff($gff3_file);
255  
256 # compute summary of parsed annotation
257 $obj->featstat;
258  
259 # dump feature summary to file
260 $obj->feature_summary($dest);
261  
262 # dump all tRNAs contained in data structure as BED12
263 $obj->features2bed("tRNA",$dest,$bn,$log)
264  
265 =head1 DESCRIPTION
266  
267 This module provides an object-oriented interface for storing and
268 converting biological sequence annotation data. Based on the C<Moose>
269 object system, it maintains a central data structure which is curently
270 designed to represent simple, non-spliced (ie single-exon) annotation
271 data. Future versions of the module will account for more generic
272 scenarios, including spliced isoforms.
273  
274 =head1 METHODS
275  
276 =over
277  
278 =item parse_gff
279  
280 Title : parse_gff
281  
282 Usage : C<$obj-E<gt>parse_gff($gff3_file);>
283  
284 Function: Parses GFF3 annotation files of non-spliced genomes into
285 C<$self-E<gt>features>
286  
287 Args : The full path to a GFF3 file
288  
289 Returns :
290  
291 Notes : The GFF3 specification is available at
292 L<http://www.sequenceontology.org/resources/gff3.html>. This
293 routine has been tested with NCBI bacteria GFF3 annotation.
294  
295 =item feature_summary
296  
297 Title : feature_summary
298  
299 Usage : C<$obj-E<gt>feature_summary($dest);>
300  
301 Function : Generate a summary file for all features present in
302 C<$self-E<gt>features>
303  
304 Args : Full output path for summary.txt file
305  
306 Returns :
307  
308 =item features2bed
309  
310 Title : features2bed
311  
312 Usage : C<$obj-E<gt>features2bed($feature,$workdir,$bn,$log);>
313  
314 Function : Dumps genomic features from C<$self-E<gt>features> hash to a
315 BED12 file.
316  
317 Args : C<$gbkey> can be either a string corresponding to a genbank
318 key in C<$self-E<gt>featstat> or C<undef>. If defined, only
319 features of the speficied key will be dumped to a single BED12
320 file. If C<$gbkey> is C<undef>, BED12 files will be generated
321 for each type present in C<$self-E<gt>featstat>. C<$dest> is the
322 output directory and C<$bn> the basename for all output
323 files. C<$log> is either be the full path to a logfile or
324 C<undef>.
325  
326 Returns :
327  
328 =back
329  
330 =head1 DEPENDENCIES
331  
332 =over
333  
334 =item L<Bio::Tools::GFF>
335  
336 =item L<IPC::Cmd>
337  
338 =item L<Path::Class>
339  
340 =item L<Carp>
341  
342 =back
343  
344 =head1 AUTHORS
345  
346 Michael T. Wolfinger E<lt>michael@wolfinger.euE<gt>
347  
348 =head1 COPYRIGHT AND LICENSE
349  
350 Copyright (C) 2014-2017 Michael T. Wolfinger E<lt>michael@wolfinger.euE<gt>
351  
352 This library is free software; you can redistribute it and/or modify
353 it under the same terms as Perl itself, either Perl version 5.10.0 or,
354 at your option, any later version of Perl 5 you may have available.
355  
356 This program is distributed in the hope that it will be useful, but
357 WITHOUT ANY WARRANTY; without even the implied warranty of
358 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
359 General Public License for more details.
360  
361  
362 =cut
« blib/lib/Bio/ViennaNGS/SpliceJunc.pm ↑ Summary blib/lib/Bio/ViennaNGS/UCSC.pm »