| 1 | package Bio::Roary::Output::QueryGroups; | ||||||||||
| 2 | 3 | $Bio::Roary::Output::QueryGroups::VERSION = '3.13.0'; | |||||||||
| 3 | # ABSTRACT: Output the groups of the union of a set of input isolates | ||||||||||
| 4 | |||||||||||
| 5 | |||||||||||
| 6 | 3 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 3 | ▶ | use Bio::SeqIO; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 3 | ▶ | use Bio::Roary::Exceptions; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 3 | ▶ | use Bio::Roary::AnalyseGroups; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 3 | ▶ | use POSIX; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 3 | has 'analyse_groups' => ( is => 'ro', isa => 'Bio::Roary::AnalyseGroups', required => 1 ); | |||||||||
| 13 | 3 | has 'input_filenames' => ( is => 'ro', isa => 'ArrayRef', required => 1 ); | |||||||||
| 14 | 3 | has 'output_union_filename' => ( is => 'ro', isa => 'Str', default => 'union_of_groups.gg' ); | |||||||||
| 15 | 3 | has 'output_intersection_filename' => ( is => 'ro', isa => 'Str', default => 'intersection_of_groups.gg' ); | |||||||||
| 16 | 3 | has 'output_complement_filename' => ( is => 'ro', isa => 'Str', default => 'complement_of_groups.gg' ); | |||||||||
| 17 | 3 | has 'core_definition' => ( is => 'ro', isa => 'Num', default => 1.0 ); | |||||||||
| 18 | |||||||||||
| 19 | 3 | has '_groups_freq' => ( is => 'ro', isa => 'HashRef', lazy => 1, builder => '_build__groups_freq' ); | |||||||||
| 20 | 3 | has '_groups_intersection' => ( is => 'ro', isa => 'ArrayRef', lazy => 1, builder => '_build__groups_intersection' ); | |||||||||
| 21 | 3 | has '_groups_complement' => ( is => 'ro', isa => 'ArrayRef', lazy => 1, builder => '_build__groups_complement' ); | |||||||||
| 22 | 3 | has '_groups' => ( is => 'ro', isa => 'ArrayRef', lazy => 1, builder => '_build__groups' ); | |||||||||
| 23 | 3 | has '_number_of_isolates' => ( is => 'ro', isa => 'Int', lazy => 1, builder => '_builder__number_of_isolates' ); | |||||||||
| 24 | 3 | has '_min_no_isolates_for_core' => ( is => 'rw', isa => 'Int', lazy_build => 1 ); | |||||||||
| 25 | |||||||||||
| 26 | 5 | ▶ | sub _build__min_no_isolates_for_core { | ||||||||
|
Subroutine
_build__min_no_isolates_for_core: called CC=1 SCAR=0.0
| |||||||||||
| 27 | 5 | my ( $self ) = @_; | |||||||||
| 28 | 5 | my $threshold = ceil( $self->_number_of_isolates * $self->core_definition ); | |||||||||
| 29 | |||||||||||
| 30 | 5 | return $threshold; | |||||||||
| 31 | } | ||||||||||
| 32 | |||||||||||
| 33 | 5 | ▶ | sub _builder__number_of_isolates { | ||||||||
|
Subroutine
_builder__number_of_isolates: called CC=1 SCAR=0.0
| |||||||||||
| 34 | 5 | my ($self) = @_; | |||||||||
| 35 | 5 | return @{ $self->input_filenames }; | |||||||||
| 36 | } | ||||||||||
| 37 | |||||||||||
| 38 | 31 | ▶ | sub _build__groups_freq { | ||||||||
|
Subroutine
_build__groups_freq: called CC=6 SCAR=18.0
| |||||||||||
| 39 | 31 | my ($self) = @_; | |||||||||
| 40 | 31 | my %groups_freq; | |||||||||
| 41 | |||||||||||
| 42 | 31 | for my $filename ( @{ $self->input_filenames } ) { | |||||||||
| 43 | 68 | my $genes = $self->analyse_groups->_files_to_genes->{$filename}; | |||||||||
| 44 | |||||||||||
| 45 | 68 | my %file_groups_seen; | |||||||||
| 46 | 68 | for my $gene ( @{$genes} ) { | |||||||||
| 47 | 249 | ▶ | next if(!defined($gene)); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 48 | 249 | ▶ | next if(!defined($self->analyse_groups->_genes_to_groups->{$gene})); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | 249 | ▶ | next if(defined($file_groups_seen{$self->analyse_groups->_genes_to_groups->{$gene}})); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 50 | |||||||||||
| 51 | 246 | push(@{$groups_freq{ $self->analyse_groups->_genes_to_groups->{$gene} }}, $gene); | |||||||||
| 52 | 246 | $file_groups_seen{$self->analyse_groups->_genes_to_groups->{$gene}} = 1; | |||||||||
| 53 | } | ||||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | 31 | return \%groups_freq; | |||||||||
| 57 | } | ||||||||||
| 58 | |||||||||||
| 59 | 31 | ▶ | sub _build__groups { | ||||||||
|
Subroutine
_build__groups: called CC=1 SCAR=0.0
| |||||||||||
| 60 | 31 | my ($self) = @_; | |||||||||
| 61 | 31 | my %groups_freq = %{ $self->_groups_freq }; | |||||||||
| 62 | 31 | my @groups = sort { @{$groups_freq{$b}} <=> @{$groups_freq{$a}} } keys %groups_freq; | |||||||||
| 63 | 31 | return \@groups; | |||||||||
| 64 | } | ||||||||||
| 65 | |||||||||||
| 66 | 5 | ▶ | sub _build__groups_intersection { | ||||||||
|
Subroutine
_build__groups_intersection: called CC=3 SCAR=11.0
| |||||||||||
| 67 | 5 | my ($self) = @_; | |||||||||
| 68 | 5 | my @groups_intersection; | |||||||||
| 69 | |||||||||||
| 70 | 5 | for my $group ( @{$self->_groups} ) { | |||||||||
| 71 | 34 | ▶ | if ( scalar @{$self->_groups_freq->{$group}} >= $self->_min_no_isolates_for_core ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | 9 | push( @groups_intersection, $group ); | |||||||||
| 73 | } | ||||||||||
| 74 | } | ||||||||||
| 75 | 5 | return \@groups_intersection; | |||||||||
| 76 | } | ||||||||||
| 77 | |||||||||||
| 78 | 3 | ▶ | sub _build__groups_complement { | ||||||||
|
Subroutine
_build__groups_complement: called CC=1 SCAR=0.0
| |||||||||||
| 79 | 3 | my ($self) = @_; | |||||||||
| 80 | 3 | my %groups_intersection = map { $_ => 1 } @{ $self->_groups_intersection }; | |||||||||
| 81 | 3 | my @complement = grep { not $groups_intersection{$_} } @{ $self->_groups }; | |||||||||
| 82 | 3 | return \@complement; | |||||||||
| 83 | } | ||||||||||
| 84 | |||||||||||
| 85 | 34 | ▶ | sub _print_out_groups { | ||||||||
|
Subroutine
_print_out_groups: called CC=3 SCAR=11.0
| |||||||||||
| 86 | 34 | my ( $self, $filename, $groups ) = @_; | |||||||||
| 87 | 34 | ▶ | open( my $fh, '>', $filename ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 88 | or Bio::Roary::Exceptions::CouldntWriteToFile->throw( error => 'Couldnt write to file: ' . $filename ); | ||||||||||
| 89 | |||||||||||
| 90 | 34 | my %groups_freq = %{ $self->_groups_freq }; | |||||||||
| 91 | 34 | my @sorted_groups = sort { @{$groups_freq{$b}} <=> @{$groups_freq{$a}} } @{$groups}; | |||||||||
| 92 | |||||||||||
| 93 | 34 | for my $group ( @sorted_groups ) { | |||||||||
| 94 | 92 | print {$fh} $group.': '.join("\t",@{$self->_groups_freq->{$group}}) . "\n"; | |||||||||
| 95 | } | ||||||||||
| 96 | 34 | close($fh); | |||||||||
| 97 | 34 | return $self; | |||||||||
| 98 | } | ||||||||||
| 99 | |||||||||||
| 100 | 3 | ▶ | sub groups_complement { | ||||||||
|
Subroutine
groups_complement: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 101 | 3 | my ($self) = @_; | |||||||||
| 102 | 3 | $self->_print_out_groups( $self->output_complement_filename, $self->_groups_complement ); | |||||||||
| 103 | } | ||||||||||
| 104 | |||||||||||
| 105 | 4 | ▶ | sub groups_intersection { | ||||||||
|
Subroutine
groups_intersection: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 106 | 4 | my ($self) = @_; | |||||||||
| 107 | 4 | $self->_print_out_groups( $self->output_intersection_filename, $self->_groups_intersection ); | |||||||||
| 108 | } | ||||||||||
| 109 | |||||||||||
| 110 | 3 | ▶ | sub groups_union { | ||||||||
|
Subroutine
groups_union: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 111 | 3 | my ($self) = @_; | |||||||||
| 112 | 3 | $self->_print_out_groups( $self->output_union_filename, $self->_groups ); | |||||||||
| 113 | } | ||||||||||
| 114 | |||||||||||
| 115 | sub groups_with_external_inputs | ||||||||||
| 116 | 24 | ▶ | { | ||||||||
|
Subroutine
groups_with_external_inputs: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 117 | 24 | my ($self, $output_filename,$groups) = @_; | |||||||||
| 118 | 24 | $self->_print_out_groups( $output_filename, $groups ); | |||||||||
| 119 | |||||||||||
| 120 | } | ||||||||||
| 121 | |||||||||||
| 122 | 3 | ▶ | no Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 123 | 3 | __PACKAGE__->meta->make_immutable; | |||||||||
| 124 | |||||||||||
| 125 | 3 | 1; | |||||||||
| 126 | |||||||||||
| 127 | __END__ | ||||||||||