lib/Bio/Roary/AnnotateGroups.pm

filter: Statementstmt 92.5% 223 / 241 Branchbran 71.7% 33 / 46 Conditioncond 55.5% 5 / 9 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 90.6% 29 / 32 Podpod 0.0% 0 / 5 totaltotal 86.0% 291 / 338 CC 43 SCAR 14.1
CC
43
Cov
88%
_build_group_nucleotide_lengths
9.2
_generate_groups_to_consensus_gene_names
6.2
_build__ids_to_verbose_stats
6.1
CRAP 4.1
« lib/Bio/Roary/ClustersRole.pm ↑ Summary lib/Bio/Roary/Output/BlastIdentityFrequency.pm »
1 package Bio::Roary::AnnotateGroups;
2 17 $Bio::Roary::AnnotateGroups::VERSION = '3.13.0';
3 # ABSTRACT: Take in a group file and associated GFF files for the isolates and update the group name to the gene name
4  
5  
6 17 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 17 ▶use Bio::Roary::Exceptions;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 17 ▶use Bio::Roary::GeneNamesFromGFF;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 17 ▶use Array::Utils qw(array_minus);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 17 ▶use List::Util qw(max min sum);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 17 ▶use File::Grep qw(fgrep);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 17 has 'gff_files' => ( is => 'ro', isa => 'ArrayRef', required => 1 );
14 17 has 'output_filename' => ( is => 'ro', isa => 'Str', default => 'reannotated_groups_file' );
15 17 has 'groups_filename' => ( is => 'ro', isa => 'Str', required => 1 );
16 17 has '_ids_to_gene_names' => ( is => 'ro', isa => 'HashRef', lazy => 1, builder => '_build__ids_to_gene_names' );
17 31 ▶has '_ids_to_product' => ( is => 'rw', isa => 'HashRef', default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
18 31 ▶has '_ids_to_gene_size' => ( is => 'rw', isa => 'HashRef', default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
19 17 has 'group_nucleotide_lengths' => ( is => 'ro', isa => 'HashRef', lazy => 1, builder => '_build_group_nucleotide_lengths');
20  
21 17 has '_groups_to_id_names' => ( is => 'ro', isa => 'HashRef', lazy => 1, builder => '_builder__groups_to_id_names' );
22 17 has '_output_fh' => ( is => 'ro', lazy => 1, builder => '_build__output_fh' );
23 17 has '_groups_to_consensus_gene_names' =>
24 ( is => 'rw', isa => 'HashRef', lazy => 1, builder => '_build__groups_to_consensus_gene_names' );
25 17 has '_filtered_gff_files' => ( is => 'ro', isa => 'ArrayRef', lazy => 1, builder => '_build__filtered_gff_files' );
26 17 has '_number_of_files' => ( is => 'ro', isa => 'Int', lazy => 1, builder => '_build__number_of_files' );
27 17 has '_ids_to_groups' => ( is => 'rw', isa => 'HashRef', lazy => 1, builder => '_builder__ids_to_groups' );
28 17 has '_group_counter' => ( is => 'rw', isa => 'Int', lazy => 1, builder => '_builder__group_counter' );
29 17 has '_group_default_prefix' => ( is => 'rw', isa => 'Str', default => 'group_' );
30 17 has '_ids_to_verbose_stats' => ( is => 'rw', isa => 'HashRef', lazy_build => 1 );
31  
32 31 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
33 31 my ($self) = @_;
34 31 $self->_ids_to_gene_names;
35 }
36  
37 1 ▶sub _builder__group_counter {
Subroutine _builder__group_counter: called CC=4 SCAR=13.9
38 1 my ($self) = @_;
39 1 my $prefix = $self->_group_default_prefix;
40 1 my $highest_group = 0;
41 1 for my $group ( @{ $self->_groups } ) {
42 6 ▶ if ( $group =~ /$prefix([\d]+)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($group =~ /$prefix([\d]+)$/) 6 0 6
43 6 my $group_id = $1;
44 6 ▶ if ( $group_id > $highest_group ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($group_id > $highest_group) 3 3 6
45 3 $highest_group = $group_id;
46 }
47 }
48 }
49 1 return $highest_group + 1;
50 }
51  
52 25 ▶sub _generate__ids_to_groups {
Subroutine _generate__ids_to_groups: called CC=3 SCAR=11.0
53 25 my ($self) = @_;
54 25 my %ids_to_groups;
55  
56 25 for my $group ( keys %{ $self->_groups_to_id_names } ) {
57 70 for my $id_name ( @{ $self->_groups_to_id_names->{$group} } ) {
58 122 $ids_to_groups{$id_name} = $group;
59 }
60 }
61 25 return \%ids_to_groups;
62 }
63  
64 0 ▶sub _builder__ids_to_groups {
Subroutine _builder__ids_to_groups: not called CC=1 SCAR=6.9
65 0 my ($self) = @_;
66 0 return $self->_generate__ids_to_groups;
67 }
68  
69 25 ▶sub _build__output_fh {
Subroutine _build__output_fh: called CC=2 SCAR=7.1
70 25 my ($self) = @_;
71 25 ▶ open( my $fh, '>', $self->output_filename )
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $self->output_filename or "Bio::Roary::Exceptions::CouldntWriteToFile"->throw("error", 'Couldnt write output file:' . $self->output_filename) 25 0 25
72 or Bio::Roary::Exceptions::CouldntWriteToFile->throw(
73 error => "Couldnt write output file:" . $self->output_filename );
74 25 return $fh;
75 }
76  
77 31 ▶sub _build__filtered_gff_files {
Subroutine _build__filtered_gff_files: called CC=1 SCAR=0.0
78 31 my ($self) = @_;
79 31 my @gff_files = grep( /\.gff$/, @{ $self->gff_files } );
80 31 return \@gff_files;
81 }
82  
83 31 ▶sub _build__ids_to_gene_names {
Subroutine _build__ids_to_gene_names: called CC=2 SCAR=6.9
84 31 my ($self) = @_;
85 31 my %ids_to_gene_names;
86 my %ids_to_product;
87 31 my %ids_to_gene_size;
88 31 for my $filename ( @{ $self->_filtered_gff_files } ) {
89 39 my $gene_names_from_gff = Bio::Roary::GeneNamesFromGFF->new( gff_file => $filename );
90 39 my %id_to_gene_lookup = %{ $gene_names_from_gff->ids_to_gene_name };
91 39 @ids_to_gene_names{ keys %id_to_gene_lookup } = values %id_to_gene_lookup;
92  
93 39 my %id_to_product_lookup = %{ $gene_names_from_gff->ids_to_product };
94 39 @ids_to_product{ keys %id_to_product_lookup } = values %id_to_product_lookup;
95
96 39 my %ids_to_gene_size_lookup = %{ $gene_names_from_gff->ids_to_gene_size };
97 39 @ids_to_gene_size{ keys %ids_to_gene_size_lookup } = values %ids_to_gene_size_lookup;
98 }
99 31 $self->_ids_to_product( \%ids_to_product );
100 31 $self->_ids_to_gene_size( \%ids_to_gene_size );
101  
102 31 return \%ids_to_gene_names;
103 }
104  
105 1 ▶sub _build__ids_to_verbose_stats {
Subroutine _build__ids_to_verbose_stats: called CC=6 SCAR=18.2
106 1 my $self = shift;
107  
108 1 ▶ my @matches_hash = fgrep { /ID=/i } @{ $self->_filtered_gff_files };
Subroutine __ANON__: called CC=1 SCAR=0.0
109 1 my @matches;
110 1 foreach my $m ( @matches_hash ){
111 3 push( @matches, values %{$m->{matches}} );
112 }
113 # chomp @matches;
114
115 1 my %verbose;
116 1 foreach my $line ( @matches ){
117 48 my ( $id, $inf, $prod );
118 48 ▶ if( $line =~ m/ID=["']?([^;"']+)["']?;?/i ){
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /ID=["']?([^;"']+)["']?;?/i) { } 48 0 48
119 48 $id = $1;
120 }
121 else {
122 0 next;
123 }
124  
125 48 ▶ $inf = $1 if ( $line =~ m/inference=([^;]+);/ );
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /inference=([^;]+);/ 48 0 48
126 48 ▶ $prod = $1 if ( $line =~ m/product=([^;]+)[;\n]/ );
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /product=([^;]+)[;\n]/ 48 0 48
127  
128 48 my %info = ( 'inference' => $inf, 'product' => $prod );
129 48 $verbose{$id} = \%info;
130 }
131 1 return \%verbose;
132 }
133  
134  
135 63 ▶sub consensus_product_for_id_names {
Subroutine consensus_product_for_id_names: called CC=4 SCAR=13.9
Pod undocumented
136 63 my ( $self, $id_names ) = @_;
137 63 my %product_freq;
138 63 for my $id_name ( @{$id_names} ) {
139 109 ▶ next unless ( defined( $self->_ids_to_product->{$id_name} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->_ids_to_product->{$id_name} 78 31 109
140 31 $product_freq{ $self->_ids_to_product->{$id_name} }++;
141 }
142  
143 0 my @sorted_product_keys = sort { $product_freq{$b} <=> $product_freq{$a} } keys(%product_freq);
144  
145 63 ▶ if ( @sorted_product_keys > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@sorted_product_keys > 0) { } 18 45 63
146 18 return $sorted_product_keys[0];
147 }
148 else {
149 45 return '';
150 }
151 }
152  
153 29 ▶sub _builder__groups_to_id_names {
Subroutine _builder__groups_to_id_names: called CC=4 SCAR=13.9
154 29 my ($self) = @_;
155 29 my %groups_to_id_names;
156  
157 29 ▶ open( my $fh, $self->groups_filename )
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, $self->groups_filename or "Bio::Roary::Exceptions::FileNotFound"->throw("error", 'Group file not found:' . $self->groups_filename) 29 0 29
158 or Bio::Roary::Exceptions::FileNotFound->throw( error => "Group file not found:" . $self->groups_filename );
159 29 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 98 29 127
160 98 chomp;
161 98 my $line = $_;
162 98 ▶ if ( $line =~ /^(.+): (.+)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^(.+): (.+)$/) 98 0 98
163 98 my $group_name = $1;
164 98 my $genes = $2;
165 98 my @elements = split( /[\s\t]+/, $genes );
166 98 $groups_to_id_names{$group_name} = \@elements;
167 }
168 }
169
170 29 return \%groups_to_id_names;
171 }
172  
173 47 ▶sub _groups {
Subroutine _groups: called CC=1 SCAR=0.0
174 47 my ($self) = @_;
175 47 my @groups = keys %{ $self->_groups_to_id_names };
176 47 return \@groups;
177 }
178  
179 142 ▶sub _ids_grouped_by_gene_name_for_group {
Subroutine _ids_grouped_by_gene_name_for_group: called CC=4 SCAR=13.9
180 142 my ( $self, $group_name ) = @_;
181 142 my %gene_name_freq;
182 142 for my $id_name ( @{ $self->_groups_to_id_names->{$group_name} } ) {
183 243 ▶ if ( defined( $self->_ids_to_gene_names->{$id_name} ) && $self->_ids_to_gene_names->{$id_name} ne "" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_ids_to_gene_names->{$id_name} and $self->_ids_to_gene_names->{$id_name} ne "") 57 186 243
Condition66%2/3
defined $self->_ids_to_gene_names->{$id_name} and $self->_ids_to_gene_names->{$id_name} ne ""
!ll&&!rl&&r
186057
MC/DC50%1/2
defined $self->_ids_to_gene_names->{$id_name} and $self->_ids_to_gene_names->{$id_name} ne ""
defined $self->_ids_to_gene_names->{$id_name} $self->_ids_to_gene_names->{$id_name} ne ""
Truth table66%2/3
A and B
A= defined $self->_ids_to_gene_names->{$id_name}B= $self->_ids_to_gene_names->{$id_name} ne ""
ABresult
0X0
100
111
184 57 push( @{ $gene_name_freq{ $self->_ids_to_gene_names->{$id_name} } }, $id_name );
185 }
186 }
187 142 return \%gene_name_freq;
188 }
189  
190 142 ▶sub _consensus_gene_name_for_group {
Subroutine _consensus_gene_name_for_group: called CC=2 SCAR=6.9
191 142 my ( $self, $group_name ) = @_;
192 142 my $gene_name_freq = $self->_ids_grouped_by_gene_name_for_group($group_name);
193  
194 142 my @sorted_gene_names = sort { @{ $gene_name_freq->{$b} } <=> @{ $gene_name_freq->{$a} } } keys %{$gene_name_freq};
195 142 ▶ if ( @sorted_gene_names > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@sorted_gene_names > 0) { } 28 114 142
196 28 return shift(@sorted_gene_names);
197 }
198 else {
199 114 return $group_name;
200 }
201 }
202  
203 sub _build_group_nucleotide_lengths
204 21 ▶{
Subroutine _build_group_nucleotide_lengths: called CC=9 SCAR=22.2
205 21 my ($self) = @_;
206 21 my %group_nucleotide_lengths;
207 21 for my $group_name (keys %{ $self->_groups_to_id_names } )
208 {
209 63 my @gene_lengths;
210 63 for my $gene_id (@{$self->_groups_to_id_names->{$group_name}})
211 {
212 109 my $current_gene_size = $self->_ids_to_gene_size->{$gene_id};
213 109 ▶ next unless(defined($current_gene_size) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $current_gene_size 78 31 109
214 31 ▶ next if($current_gene_size < 1);
Branch: 1 branch
BranchTrueFalseTotal
if $current_gene_size < 1 0 31 31
215 31 push(@gene_lengths, $current_gene_size);
216 }
217
218 63 ▶ next if(@gene_lengths == 0);
Branch: 1 branch
BranchTrueFalseTotal
if @gene_lengths == 0 45 18 63
219 18 ▶ my $average_gene_size = (int((sum @gene_lengths)/@gene_lengths)) || 0;
Condition50%1/2
int &sum(@gene_lengths) / @gene_lengths || 0
l!l
180
MC/DC0%0/1
int &sum(@gene_lengths) / @gene_lengths || 0
int &sum(@gene_lengths) / @gene_lengths
Truth table50%1/2
A
A= int &sum(@gene_lengths) / @gene_lengths
Aresult
11
00
220 18 ▶ my $min_gene_size = (min @gene_lengths) || 0;
Condition50%1/2
&min(@gene_lengths) || 0
l!l
180
MC/DC0%0/1
&min(@gene_lengths) || 0
&min(@gene_lengths)
Truth table50%1/2
A
A= &min(@gene_lengths)
Aresult
11
00
221 18 ▶ my $max_gene_size = (max @gene_lengths) || 0;
Condition50%1/2
&max(@gene_lengths) || 0
l!l
180
MC/DC0%0/1
&max(@gene_lengths) || 0
&max(@gene_lengths)
Truth table50%1/2
A
A= &max(@gene_lengths)
Aresult
11
00
222 18 $group_nucleotide_lengths{$group_name} = {'min' => $min_gene_size, 'max' =>$max_gene_size , 'average' => $average_gene_size};
223 }
224 21 return \%group_nucleotide_lengths;
225 }
226  
227 28 ▶sub _generate_groups_to_consensus_gene_names {
Subroutine _generate_groups_to_consensus_gene_names: called CC=6 SCAR=18.3
228 28 my ($self) = @_;
229 28 my %groups_to_gene_names;
230 my %gene_name_freq;
231 28 my $group_prefix = $self->_group_default_prefix;
232  
233 # These are already annotated
234 28 for my $group_name ( sort { @{ $self->_groups_to_id_names->{$b} } <=> @{ $self->_groups_to_id_names->{$a} } }
235 28 keys %{ $self->_groups_to_id_names } )
236 {
237 91 ▶ next if ( $group_name =~ /$group_prefix/ );
Branch: 1 branch
BranchTrueFalseTotal
if $group_name =~ /$group_prefix/ 91 0 91
238 0 $groups_to_gene_names{$group_name} = $group_name;
239 }
240  
241 28 for my $group_name ( sort { @{ $self->_groups_to_id_names->{$b} } <=> @{ $self->_groups_to_id_names->{$a} } }
242 28 keys %{ $self->_groups_to_id_names } )
243 {
244 91 ▶ next unless ( $group_name =~ /$group_prefix/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $group_name =~ /$group_prefix/ 0 91 91
245 91 my $consensus_gene_name = $self->_consensus_gene_name_for_group($group_name);
246  
247 91 ▶ if ( defined( $gene_name_freq{$consensus_gene_name} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $gene_name_freq{$consensus_gene_name}) { } 0 91 91
248 0 $groups_to_gene_names{$group_name} = $group_name;
249 }
250 else {
251 91 $groups_to_gene_names{$group_name} = $consensus_gene_name;
252 91 $gene_name_freq{$consensus_gene_name}++;
253 }
254 }
255 28 return \%groups_to_gene_names;
256 }
257  
258 3 ▶sub _build__groups_to_consensus_gene_names {
Subroutine _build__groups_to_consensus_gene_names: called CC=1 SCAR=0.0
259 3 my ($self) = @_;
260 3 return $self->_generate_groups_to_consensus_gene_names;
261 }
262  
263 0 ▶sub _build__number_of_files {
Subroutine _build__number_of_files: not called CC=1 SCAR=6.9
264 0 my ($self) = @_;
265 0 return @{ $self->gff_files };
266 }
267  
268  
269 25 ▶sub _split_groups {
Subroutine _split_groups: called CC=1 SCAR=0.0
270 25 my ($self) = @_;
271
272 25 $self->_groups_to_consensus_gene_names( $self->_generate_groups_to_consensus_gene_names );
273 25 $self->_ids_to_groups( $self->_generate__ids_to_groups );
274 }
275  
276 0 ▶sub _remove_ids_from_group {
Subroutine _remove_ids_from_group: not called CC=2 SCAR=17.9
277 0 my ( $self, $ids_to_remove, $group ) = @_;
278  
279 0 my @remaining_ids = array_minus( @{ $self->_groups_to_id_names->{$group} }, @{ $ids_to_remove } );
280 0 $self->_groups_to_id_names->{$group} = \@remaining_ids;
281 0 ▶ if ( @{ $self->_groups_to_id_names->{$group} } == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->_groups_to_id_names->{$group};} == 0) 0 0 0
282 0 delete( $self->_groups_to_id_names->{$group} );
283 }
284 }
285  
286 25 ▶sub reannotate {
Subroutine reannotate: called CC=2 SCAR=6.9
Pod undocumented
287 25 my ($self) = @_;
288  
289 25 $self->_split_groups;
290  
291 25 my %groups_to_id_names = %{ $self->_groups_to_id_names };
292 25 for
293 74 my $group_name ( sort { @{ $groups_to_id_names{$b} } <=> @{ $groups_to_id_names{$a} } } keys %groups_to_id_names )
294 {
295 70 my $consensus_gene_name = $self->_groups_to_consensus_gene_names->{$group_name};
296 70 print { $self->_output_fh } $consensus_gene_name . ": "
297 70 . join( "\t", @{ $self->_groups_to_id_names->{$group_name} } ) . "\n";
298 }
299 25 close( $self->_output_fh );
300 25 return $self;
301 }
302  
303 7 ▶sub full_annotation {
Subroutine full_annotation: called CC=4 SCAR=13.9
Pod undocumented
304 7 my ( $self, $group ) = @_;
305  
306 7 my @id_names = @{ $self->_groups_to_id_names->{$group} };
307  
308 7 my %product_freq;
309 7 for my $id_name ( @id_names ) {
310 12 ▶ next unless ( defined( $self->_ids_to_verbose_stats->{$id_name}->{'product'} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->_ids_to_verbose_stats->{$id_name}{"product"} 2 10 12
311 10 $product_freq{ $self->_ids_to_verbose_stats->{$id_name}->{'product'} }++;
312 }
313  
314 0 my @sorted_product_keys = sort { $product_freq{$b} <=> $product_freq{$a} } keys(%product_freq);
315  
316 7 ▶ if ( @sorted_product_keys > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@sorted_product_keys > 0) { } 6 1 7
317 6 return join('; ', @sorted_product_keys);
318 }
319 else {
320 1 return '';
321 }
322
323 }
324  
325 7 ▶sub inference {
Subroutine inference: called CC=3 SCAR=11.0
Pod undocumented
326 7 my ( $self, $group ) = @_;
327  
328 7 my @infs;
329 7 foreach my $g ( @{ $self->_groups_to_id_names->{$group} } ){
330 12 ▶ next unless ( defined $self->_ids_to_verbose_stats->{$g}->{'inference'} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->_ids_to_verbose_stats->{$g}{"inference"} 2 10 12
331 10 push( @infs, $self->_ids_to_verbose_stats->{$g}->{'inference'} );
332 }
333  
334 # maybe make a consensus in the future?
335  
336 7 return $infs[0];
337 }
338  
339 17 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
340 17 __PACKAGE__->meta->make_immutable;
341  
342 17 1;
343  
344 __END__
« lib/Bio/Roary/ClustersRole.pm ↑ Summary lib/Bio/Roary/Output/BlastIdentityFrequency.pm »