lib/Bio/Roary/OrderGenes.pm

filter: Statementstmt 93.2% 248 / 266 Branchbran 83.3% 30 / 36 Conditioncond 33.3% 5 / 15 MC/DCmcdc 11.1% 1 / 9 Subroutinesub 95.2% 20 / 21 Podpod n/a totaltotal 87.6% 304 / 347 CC 55 SCAR 21.2
CC
55
Cov
89%
_reorder_connected_components
13.0
_build_groups_to_contigs
12.2
_order_by_samples_and_weights
10.2
CRAP 8.3
« lib/Bio/Roary/CommandLine/TransferAnnotationToGroups.pm ↑ Summary lib/Bio/Roary/InflateClusters.pm »
1 package Bio::Roary::OrderGenes;
2 3 $Bio::Roary::OrderGenes::VERSION = '3.13.0';
3 # ABSTRACT: Take in GFF files and create a matrix of what genes are beside what other genes
4  
5  
6 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Bio::Roary::Exceptions;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Bio::Roary::AnalyseGroups;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Bio::Roary::ContigsToGeneIDsFromGFF;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Graph;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Graph::Writer::Dot;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 has 'gff_files' => ( is => 'ro', isa => 'ArrayRef', required => 1 );
15 3 has 'analyse_groups_obj' => ( is => 'ro', isa => 'Bio::Roary::AnalyseGroups', required => 1 );
16 3 has 'core_definition' => ( is => 'ro', isa => 'Num', default => 1.0 );
17 3 has 'pan_graph_filename' => ( is => 'ro', isa => 'Str', default => 'core_accessory_graph.dot' );
18 3 has 'accessory_graph_filename' => ( is => 'ro', isa => 'Str', default => 'accessory_graph.dot' );
19 3 has 'sample_weights' => ( is => 'ro', isa => 'Maybe[HashRef]' );
20 3 has 'samples_to_clusters' => ( is => 'ro', isa => 'Maybe[HashRef]' );
21 3 has 'group_order' => ( is => 'ro', isa => 'HashRef', lazy => 1, builder => '_build_group_order' );
22 37 ▶has 'groups_to_sample_names' => ( is => 'rw', isa => 'HashRef', default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
23 3 has 'group_graphs' => ( is => 'ro', isa => 'Graph', lazy => 1, builder => '_build_group_graphs' );
24 3 has 'groups_to_contigs' => ( is => 'ro', isa => 'HashRef', lazy => 1, builder => '_build_groups_to_contigs' );
25 3 has '_groups_to_file_contigs' => ( is => 'ro', isa => 'HashRef', lazy => 1, builder => '_build__groups_to_file_contigs' );
26 3 has '_groups' => ( is => 'ro', isa => 'HashRef', lazy => 1, builder => '_build_groups' );
27 3 has 'number_of_files' => ( is => 'ro', isa => 'Int', lazy => 1, builder => '_build_number_of_files' );
28 37 ▶has '_groups_qc' => ( is => 'ro', isa => 'HashRef', default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
29 3 has '_percentage_of_largest_weak_threshold' => ( is => 'ro', isa => 'Num', default => 0.9 );
30  
31 15 ▶sub _build_number_of_files {
Subroutine _build_number_of_files: called CC=1 SCAR=0.0
32 15 my ($self) = @_;
33 15 return @{ $self->gff_files };
34 }
35  
36 0 ▶sub _build_groups {
Subroutine _build_groups: not called CC=2 SCAR=17.9
37 0 my ($self) = @_;
38 0 my %groups;
39 0 for my $group_name ( @{ $self->analyse_groups_obj->_groups } ) {
40 0 $groups{$group_name}++;
41 }
42 0 return \%groups;
43 }
44  
45 36 ▶sub _build__groups_to_file_contigs {
Subroutine _build__groups_to_file_contigs: called CC=6 SCAR=17.9
46 36 my ($self) = @_;
47  
48 36 my @overlapping_hypothetical_gene_ids;
49 my %samples_to_groups_contigs;
50  
51 # Open each GFF file
52 36 for my $filename ( @{ $self->gff_files } ) {
53 108 my @groups_to_contigs;
54 108 my $contigs_to_ids_obj = Bio::Roary::ContigsToGeneIDsFromGFF->new( gff_file => $filename );
55  
56 108 my ( $sample_name, $directories, $suffix ) = fileparse($filename);
57 108 $sample_name =~ s/\.gff//gi;
58  
59 # Loop over each contig in the GFF file
60 108 for my $contig_name ( keys %{ $contigs_to_ids_obj->contig_to_ids } ) {
61 189 my @groups_on_contig;
62  
63 # loop over each gene in each contig in the GFF file
64 189 for my $gene_id ( @{ $contigs_to_ids_obj->contig_to_ids->{$contig_name} } ) {
65  
66 # convert to group name
67 981 my $group_name = $self->analyse_groups_obj->_genes_to_groups->{$gene_id};
68 981 ▶ next unless ( defined($group_name) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $group_name 805 176 981
69  
70 176 ▶ if ( $contigs_to_ids_obj->overlapping_hypothetical_protein_ids->{$gene_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($contigs_to_ids_obj->overlapping_hypothetical_protein_ids->{$gene_id}) 0 176 176
71 0 $self->_groups_qc->{$group_name} =
72 'Hypothetical protein with no hits to refseq/uniprot/clusters/cdd/tigrfams/pfam overlapping another protein with hits';
73 }
74 176 push( @groups_on_contig, $group_name );
75 }
76 189 push( @groups_to_contigs, \@groups_on_contig );
77 }
78 108 $samples_to_groups_contigs{$sample_name} = \@groups_to_contigs;
79 }
80  
81 36 return \%samples_to_groups_contigs;
82  
83 }
84  
85 36 ▶sub _build_group_order {
Subroutine _build_group_order: called CC=9 SCAR=22.4
86 36 my ($self) = @_;
87 36 my %group_order;
88  
89 my %groups_to_sample_names;
90 36 for my $sample_name ( keys %{ $self->_groups_to_file_contigs } ) {
91 108 my $groups_to_file_contigs = $self->_groups_to_file_contigs->{$sample_name};
92 108 for my $groups_on_contig ( @{$groups_to_file_contigs} ) {
93 189 ▶ for ( my $i = 1 ; $i < @{$groups_on_contig} ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$groups_on_contig;}) 131 189 320
94 131 my $group_from = $groups_on_contig->[ $i - 1 ];
95 131 my $group_to = $groups_on_contig->[$i];
96  
97 131 ▶ if ( defined( $self->sample_weights ) && $self->sample_weights->{$sample_name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->sample_weights and $self->sample_weights->{$sample_name}) { } 11 120 131
Condition66%2/3
defined $self->sample_weights and $self->sample_weights->{$sample_name}
!ll&&!rl&&r
120011
MC/DC50%1/2
defined $self->sample_weights and $self->sample_weights->{$sample_name}
defined $self->sample_weights $self->sample_weights->{$sample_name}
Truth table66%2/3
A and B
A= defined $self->sample_weightsB= $self->sample_weights->{$sample_name}
ABresult
0X0
100
111
98 11 $group_order{$group_from}{$group_to} += $self->sample_weights->{$sample_name};
99 11 push( @{ $groups_to_sample_names{$group_from} }, $sample_name );
100 }
101 else {
102 120 $group_order{$group_from}{$group_to}++;
103 }
104 }
105 189 ▶ if ( @{$groups_on_contig} == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$groups_on_contig;} == 1) 6 183 189
106 6 my $group_from = $groups_on_contig->[0];
107 6 my $group_to = $groups_on_contig->[0];
108 6 ▶ if ( defined( $self->sample_weights ) && $self->sample_weights->{$sample_name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->sample_weights and $self->sample_weights->{$sample_name}) { } 0 6 6
Condition33%1/3
defined $self->sample_weights and $self->sample_weights->{$sample_name}
!ll&&!rl&&r
600
MC/DC0%0/2
defined $self->sample_weights and $self->sample_weights->{$sample_name}
defined $self->sample_weights $self->sample_weights->{$sample_name}
Truth table33%1/3
A and B
A= defined $self->sample_weightsB= $self->sample_weights->{$sample_name}
ABresult
0X0
100
111
109 0 $group_order{$group_from}{$group_to} += $self->sample_weights->{$sample_name};
110 0 push( @{ $groups_to_sample_names{$group_from} }, $sample_name );
111 }
112 else {
113 6 $group_order{$group_from}{$group_to}++;
114 }
115 }
116 }
117 }
118  
119 36 $self->groups_to_sample_names( \%groups_to_sample_names );
120 36 return \%group_order;
121 }
122  
123 36 ▶sub _build_group_graphs {
Subroutine _build_group_graphs: called CC=1 SCAR=0.0
124 36 my ($self) = @_;
125 36 return Graph->new( undirected => 1 );
126 }
127  
128 72 ▶sub _save_graph_to_file {
Subroutine _save_graph_to_file: called CC=1 SCAR=0.0
129 72 my ( $self, $graph, $output_filename ) = @_;
130 72 my $writer = Graph::Writer::Dot->new();
131 72 $writer->write_graph( $graph, $output_filename );
132 72 return 1;
133 }
134  
135 36 ▶sub _add_groups_to_graph {
Subroutine _add_groups_to_graph: called CC=3 SCAR=11.0
136 36 my ($self) = @_;
137  
138 36 for my $current_group ( keys %{ $self->group_order() } ) {
139 65 for my $group_to ( keys %{ $self->group_order->{$current_group} } ) {
140 71 my $weight = 1.0 / ( $self->group_order->{$current_group}->{$group_to} );
141 71 $self->group_graphs->add_weighted_edge( $current_group, $group_to, $weight );
142 }
143 }
144  
145 }
146  
147 72 ▶sub _reorder_connected_components {
Subroutine _reorder_connected_components: called CC=13 SCAR=25.6
148 72 my ( $self, $graph_groups ) = @_;
149 72 my @ordered_graph_groups;
150 my @paths_and_weights;
151  
152 72 for my $graph_group ( @{$graph_groups} ) {
153 29 my %groups;
154 29 $groups{$_}++ for ( @{$graph_group} );
155 29 my $edge_sum = 0;
156  
157 29 for my $current_group ( keys %groups ) {
158 107 for my $group_to ( keys %{ $self->group_order->{$current_group} } ) {
159 97 ▶ next unless defined( $groups{$group_to} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $groups{$group_to} 11 86 97
160 86 $edge_sum += $self->group_order->{$current_group}->{$group_to};
161 }
162 }
163  
164 29 my %samples_in_graph;
165 29 for my $current_group ( keys %groups ) {
166 107 my $sample_names = $self->groups_to_sample_names->{$current_group};
167 107 ▶ if ( defined($sample_names) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sample_names) 11 96 107
168 11 for my $sample_name ( @{$sample_names} ) {
169 15 $samples_in_graph{$sample_name}++;
170 }
171 }
172 }
173 29 my @sample_names = sort keys %samples_in_graph;
174  
175 29 ▶ if ( @{$graph_group} == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$graph_group;} == 1) { } 6 23 29
176  
177 6 push(
178 @paths_and_weights,
179 {
180 path => $graph_group,
181 average_weight => $edge_sum,
182 sample_names => \@sample_names
183 }
184 );
185 }
186 else {
187 23 my $graph = Graph->new( undirected => 1 );
188 23 for my $current_group ( keys %groups ) {
189 101 for my $group_to ( keys %{ $self->group_order->{$current_group} } ) {
190 91 ▶ if ( $groups{$group_to} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($groups{$group_to}) 84 7 91
191 84 my $weight = 1 / $self->group_order->{$current_group}->{$group_to};
192 84 $graph->add_weighted_edge( $current_group, $group_to, $weight );
193 }
194 }
195 }
196 23 my $minimum_spanning_tree = $graph->minimum_spanning_tree;
197 23 my $dfs_obj = Graph::Traversal::DFS->new($minimum_spanning_tree);
198 23 my @reordered_dfs_groups = $dfs_obj->dfs;
199 23 push(
200 @paths_and_weights,
201 {
202 path => \@reordered_dfs_groups,
203 average_weight => $edge_sum,
204 sample_names => \@sample_names
205 }
206 );
207 }
208  
209 }
210  
211 72 return $self->_order_by_samples_and_weights( \@paths_and_weights );
212 }
213  
214 73 ▶sub _order_by_samples_and_weights {
Subroutine _order_by_samples_and_weights: called CC=9 SCAR=23.2
215 73 my ( $self, $paths_and_weights ) = @_;
216  
217 73 my @ordered_graph_groups;
218 73 ▶ if ( !defined( $self->samples_to_clusters ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->samples_to_clusters) 72 1 73
219 72 my @ordered_paths_and_weights = sort { $a->{average_weight} <=> $b->{average_weight} } @{$paths_and_weights};
220 72 @ordered_graph_groups = map { $_->{path} } @ordered_paths_and_weights;
221 72 return \@ordered_graph_groups;
222 }
223  
224 # Find the largest cluster in each graph and regroup
225 1 my %largest_cluster_to_paths_and_weights;
226 1 for my $graph_details ( @{$paths_and_weights} ) {
227 3 my %cluster_count;
228 3 for my $sample_name ( @{ $graph_details->{sample_names} } ) {
229 6 ▶ if ( defined( $self->samples_to_clusters->{$sample_name} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->samples_to_clusters->{$sample_name}) 6 0 6
230 6 $cluster_count{ $self->samples_to_clusters->{$sample_name} }++;
231 }
232 }
233 0 ▶ my $largest_cluster = ( sort { $cluster_count{$b} <=> $cluster_count{$a} || $a cmp $b} keys %cluster_count )[0];
Condition0%0/3
$cluster_count{$b} <=> $cluster_count{$a} || $a cmp $b
l!l&&r!l&&!r
000
MC/DC0%0/2
$cluster_count{$b} <=> $cluster_count{$a} || $a cmp $b
$cluster_count{$b} <=> $cluster_count{$a} $a cmp $b
Truth table0%0/3
A || B
A= $cluster_count{$b} <=> $cluster_count{$a}B= $a cmp $b
ABresult
1X1
011
000
234 3 ▶ if ( !defined($largest_cluster) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $largest_cluster) 0 3 3
235 0 my @ordered_paths_and_weights = sort { $b->{average_weight} <=> $a->{average_weight} } @{$paths_and_weights};
236 0 @ordered_graph_groups = map { $_->{path} } @ordered_paths_and_weights;
237 0 return \@ordered_graph_groups;
238 }
239  
240 3 push( @{ $largest_cluster_to_paths_and_weights{$largest_cluster}{graph_details} }, $graph_details );
241 3 $largest_cluster_to_paths_and_weights{$largest_cluster}{largest_cluster_size} += $cluster_count{$largest_cluster};
242 }
243  
244 # go through each cluster group and order by weight
245 1 my @clustered_ordered_graph_groups;
246 1 for my $cluster_name (
247 sort {
248 $largest_cluster_to_paths_and_weights{$b}->{largest_cluster_size}
249 <=> $largest_cluster_to_paths_and_weights{$a}->{largest_cluster_size}
250 1 } keys %largest_cluster_to_paths_and_weights
251 )
252 {
253
254 my @ordered_paths_and_weights =
255 2 sort { $b->{average_weight} <=> $a->{average_weight} } @{ $largest_cluster_to_paths_and_weights{$cluster_name}->{graph_details} };
256 2 @ordered_graph_groups = map { $_->{path} } @ordered_paths_and_weights;
257  
258 2 for my $graph_group (@ordered_graph_groups) {
259 3 push( @clustered_ordered_graph_groups, $graph_group );
260 }
261 }
262 1 return \@clustered_ordered_graph_groups;
263 }
264  
265 36 ▶sub _build_groups_to_contigs {
Subroutine _build_groups_to_contigs: called CC=12 SCAR=25.0
266 36 my ($self) = @_;
267 36 $self->_add_groups_to_graph;
268  
269 36 my %groups_to_contigs;
270 36 my $counter = 1;
271 36 my $overall_counter = 1;
272 36 my $counter_filtered = 1;
273  
274 # Accessory
275 36 my $accessory_graph = $self->_create_accessory_graph;
276 36 my @group_graphs = $accessory_graph->connected_components();
277 36 my $reordered_graphs = $self->_reorder_connected_components( \@group_graphs );
278  
279 36 $self->_save_graph_to_file( $accessory_graph, $self->accessory_graph_filename );
280  
281 36 for my $contig_groups ( @{$reordered_graphs} ) {
282 14 my $order_counter = 1;
283  
284 14 for my $group_name ( @{$contig_groups} ) {
285 29 $groups_to_contigs{$group_name}{accessory_label} = $counter;
286 29 $groups_to_contigs{$group_name}{accessory_order} = $order_counter;
287 29 $groups_to_contigs{$group_name}{'accessory_overall_order'} = $overall_counter;
288 29 $order_counter++;
289 29 $overall_counter++;
290 }
291 14 $counter++;
292 }
293  
294 # Core + accessory
295 36 my @group_graphs_all = $self->group_graphs->connected_components();
296 36 my $reordered_graphs_all = $self->_reorder_connected_components( \@group_graphs_all );
297 36 $self->_save_graph_to_file( $self->group_graphs, $self->pan_graph_filename );
298  
299 36 $overall_counter = 1;
300 36 $counter = 1;
301 36 $counter_filtered = 1;
302 36 for my $contig_groups ( @{$reordered_graphs_all} ) {
303 15 my $order_counter = 1;
304  
305 15 for my $group_name ( @{$contig_groups} ) {
306 78 $groups_to_contigs{$group_name}{label} = $counter;
307 78 $groups_to_contigs{$group_name}{comment} = '';
308 78 $groups_to_contigs{$group_name}{order} = $order_counter;
309 78 $groups_to_contigs{$group_name}{'core_accessory_overall_order'} = $overall_counter;
310  
311 78 ▶ if ( @{$contig_groups} <= 2 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@{$contig_groups;} <= 2) { } 2 76 78
elsif ($self->_groups_qc->{$group_name}) { } 0 76 76
312 2 $groups_to_contigs{$group_name}{comment} = 'Investigate';
313 }
314 elsif ( $self->_groups_qc->{$group_name} ) {
315 0 $groups_to_contigs{$group_name}{comment} = $self->_groups_qc->{$group_name};
316 }
317 else {
318 76 $groups_to_contigs{$group_name}{'core_accessory_overall_order_filtered'} = $counter_filtered;
319 76 $counter_filtered++;
320 }
321 78 $order_counter++;
322 78 $overall_counter++;
323 }
324 15 $counter++;
325 }
326  
327 36 $counter_filtered = 1;
328 36 for my $contig_groups ( @{$reordered_graphs} ) {
329 14 for my $group_name ( @{$contig_groups} ) {
330 29 ▶ if ( ( !defined( $groups_to_contigs{$group_name}{comment} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $groups_to_contigs{$group_name}{"comment"} or defined $groups_to_contigs{$group_name}{"comment"} and $groups_to_contigs{$group_name}{"comment"} eq "") 29 0 29
Condition33%2/6
not defined $groups_to_contigs{$group_name}{"comment"} or defined $groups_to_contigs{$group_name}{"comment"} and $groups_to_contigs{$group_name}{"comment"} eq ""
l!l&&r!l&&!r
0290
defined $groups_to_contigs{$group_name}{"comment"} and $groups_to_contigs{$group_name}{"comment"} eq ""
!ll&&!rl&&r
0029
MC/DC0%0/3
not defined $groups_to_contigs{$group_name}{"comment"} or defined $groups_to_contigs{$group_name}{"comment"} and $groups_to_contigs{$group_name}{"comment"} eq ""
not defined $groups_to_contigs{$group_name}{"comment"} defined $groups_to_contigs{$group_name}{"comment"} $groups_to_contigs{$group_name}{"comment"} eq ""
Truth table25%1/4
not(A) or B and C
A= not defined $groups_to_contigs{$group_name}{"comment"}B= defined $groups_to_contigs{$group_name}{"comment"}C= $groups_to_contigs{$group_name}{"comment"} eq ""
ABCresult
1XX1
0111
00X0
0100
331 || ( defined( $groups_to_contigs{$group_name}{comment} ) && $groups_to_contigs{$group_name}{comment} eq '' ) )
332 {
333 29 $groups_to_contigs{$group_name}{'accessory_overall_order_filtered'} = $counter_filtered;
334 29 $counter_filtered++;
335 }
336 }
337 }
338  
339 36 return \%groups_to_contigs;
340 }
341  
342 36 ▶sub _create_accessory_graph {
Subroutine _create_accessory_graph: called CC=8 SCAR=20.8
343 36 my ($self) = @_;
344 36 my $graph = Graph->new( undirected => 1 );
345  
346 36 my %core_groups;
347 my %group_freq;
348  
349 36 for my $sample_name ( keys %{ $self->_groups_to_file_contigs } ) {
350 108 my $groups_to_file_contigs = $self->_groups_to_file_contigs->{$sample_name};
351  
352 108 for my $groups_on_contig ( @{$groups_to_file_contigs} ) {
353 189 for my $current_group ( @{$groups_on_contig} ) {
354 176 $group_freq{$current_group}++;
355 }
356 }
357 }
358  
359 36 for my $current_group ( keys %{ $self->group_order() } ) {
360 65 ▶ next if ( $group_freq{$current_group} >= ( $self->number_of_files * $self->core_definition ) );
Branch: 1 branch
BranchTrueFalseTotal
if $group_freq{$current_group} >= $self->number_of_files * $self->core_definition 39 26 65
361
362 26 for my $group_to ( keys %{ $self->group_order->{$current_group} } ) {
363 26 ▶ if ( $group_freq{$group_to} >= ( $self->number_of_files * $self->core_definition ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($group_freq{$group_to} >= $self->number_of_files * $self->core_definition) { } 11 15 26
364 11 $graph->add_vertex($current_group);
365 }
366 else {
367 15 my $weight = 1.0 / ( $self->group_order->{$current_group}->{$group_to} );
368 15 $graph->add_weighted_edge( $current_group, $group_to, $weight );
369 }
370 }
371 }
372  
373 36 return $graph;
374 }
375  
376 3 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
377 3 __PACKAGE__->meta->make_immutable;
378  
379 3 1;
380  
381 __END__
« lib/Bio/Roary/CommandLine/TransferAnnotationToGroups.pm ↑ Summary lib/Bio/Roary/InflateClusters.pm »