| 1 | package Bio::Roary::AnalyseGroups; | ||||||||||
| 2 | 21 | $Bio::Roary::AnalyseGroups::VERSION = '3.13.0'; | |||||||||
| 3 | # ABSTRACT: Take in a groups file and the original FASTA files and create plots and stats | ||||||||||
| 4 | |||||||||||
| 5 | |||||||||||
| 6 | 21 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 21 | ▶ | use Bio::Roary::Exceptions; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 21 | has 'fasta_files' => ( is => 'ro', isa => 'ArrayRef', required => 1 ); | |||||||||
| 10 | 21 | has 'groups_filename' => ( is => 'ro', isa => 'Str', required => 1 ); | |||||||||
| 11 | 21 | has 'output_filename' => ( is => 'ro', isa => 'Str', default => 'summary_of_groups' ); | |||||||||
| 12 | |||||||||||
| 13 | 21 | has '_number_of_isolates' => ( is => 'ro', isa => 'Int', lazy => 1, builder => '_builder__number_of_isolates' ); | |||||||||
| 14 | 21 | has '_genes_to_file' => ( is => 'rw', isa => 'HashRef' ); | |||||||||
| 15 | 21 | has '_files_to_genes' => ( is => 'ro', isa => 'HashRef', lazy => 1, builder => '_builder__files_to_genes' ); | |||||||||
| 16 | 21 | has '_groups_to_genes' => ( is => 'ro', isa => 'HashRef', lazy => 1, builder => '_builder__groups_to_genes' ); | |||||||||
| 17 | 21 | has '_genes_to_groups' => ( is => 'rw', isa => 'HashRef' ); | |||||||||
| 18 | |||||||||||
| 19 | 21 | has '_groups' => ( is => 'ro', isa => 'ArrayRef', lazy => 1, builder => '_builder__groups' ); | |||||||||
| 20 | |||||||||||
| 21 | |||||||||||
| 22 | 67 | ▶ | sub BUILD { | ||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 23 | 67 | my ($self) = @_; | |||||||||
| 24 | # This triggers _genes_to_groups to be built | ||||||||||
| 25 | 67 | $self->_groups_to_genes; | |||||||||
| 26 | # This triggers _genes_to_file to be built | ||||||||||
| 27 | 67 | $self->_files_to_genes; | |||||||||
| 28 | } | ||||||||||
| 29 | |||||||||||
| 30 | sub _builder__groups | ||||||||||
| 31 | 0 | ▶ | { | ||||||||
|
Subroutine
_builder__groups: not called CC=1 SCAR=6.9
| |||||||||||
| 32 | 0 | my ($self) = @_; | |||||||||
| 33 | 0 | my @groups = sort keys %{$self->_groups_to_genes}; | |||||||||
| 34 | 0 | return \@groups; | |||||||||
| 35 | } | ||||||||||
| 36 | |||||||||||
| 37 | 1 | ▶ | sub _builder__number_of_isolates { | ||||||||
|
Subroutine
_builder__number_of_isolates: called CC=1 SCAR=0.0
| |||||||||||
| 38 | 1 | my ($self) = @_; | |||||||||
| 39 | 1 | return @{ $self->fasta_files }; | |||||||||
| 40 | } | ||||||||||
| 41 | |||||||||||
| 42 | 67 | ▶ | sub _builder__files_to_genes { | ||||||||
|
Subroutine
_builder__files_to_genes: called CC=4 SCAR=13.9
| |||||||||||
| 43 | 67 | my ($self) = @_; | |||||||||
| 44 | 67 | my %files_to_genes; | |||||||||
| 45 | my %genes_to_file; | ||||||||||
| 46 | 67 | for my $filename ( @{ $self->fasta_files } ) { | |||||||||
| 47 | 190 | open( my $fh, '-|', 'grep \> ' . $filename . ' | awk \'{print $1}\' | sed \'s/>//\' ' ); | |||||||||
| 48 | 190 | ▶ | while (<$fh>) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | 1672 | chomp; | |||||||||
| 50 | 1672 | my $gene_name = $_; | |||||||||
| 51 | 1672 | ▶ | next if($gene_name eq ""); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | 1672 | push( @{ $files_to_genes{$filename} }, $gene_name ); | |||||||||
| 53 | 1672 | $genes_to_file{$gene_name} = $filename; | |||||||||
| 54 | } | ||||||||||
| 55 | 190 | close($fh); | |||||||||
| 56 | } | ||||||||||
| 57 | 67 | $self->_genes_to_file(\%genes_to_file); | |||||||||
| 58 | |||||||||||
| 59 | 67 | return \%files_to_genes; | |||||||||
| 60 | } | ||||||||||
| 61 | |||||||||||
| 62 | 126 | ▶ | sub _count_num_files_in_group { | ||||||||
|
Subroutine
_count_num_files_in_group: called CC=4 SCAR=14.0
| |||||||||||
| 63 | 126 | my ( $self, $genes ) = @_; | |||||||||
| 64 | 126 | my $count = 0; | |||||||||
| 65 | 126 | my %filename_freq; | |||||||||
| 66 | 126 | for my $gene ( @{$genes} ) { | |||||||||
| 67 | 218 | ▶ | next if ( $gene eq "" ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | 218 | ▶ | if ( defined( $self->_genes_to_file->{$gene} ) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | 218 | $filename_freq{ $self->_genes_to_file->{$gene} }++; | |||||||||
| 70 | } | ||||||||||
| 71 | } | ||||||||||
| 72 | 126 | my @uniq_filenames = keys %filename_freq; | |||||||||
| 73 | 126 | return @uniq_filenames; | |||||||||
| 74 | } | ||||||||||
| 75 | |||||||||||
| 76 | 67 | ▶ | sub _builder__groups_to_genes { | ||||||||
|
Subroutine
_builder__groups_to_genes: called CC=5 SCAR=16.1
| |||||||||||
| 77 | 67 | my ($self) = @_; | |||||||||
| 78 | 67 | my %groups_to_genes; | |||||||||
| 79 | my %genes_to_groups; | ||||||||||
| 80 | |||||||||||
| 81 | 67 | ▶ | open( my $fh, $self->groups_filename ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | or Bio::Roary::Exceptions::FileNotFound->throw( error => "Group file not found:" . $self->groups_filename ); | ||||||||||
| 83 | 67 | ▶ | while (<$fh>) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | 350 | chomp; | |||||||||
| 85 | 350 | my $line = $_; | |||||||||
| 86 | 350 | ▶ | if ( $line =~ /^(.+): (.+)$/ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | 299 | my $group_name = $1; | |||||||||
| 88 | 299 | my $genes = $2; | |||||||||
| 89 | 299 | my @elements = split( /[\s\t]+/, $genes ); | |||||||||
| 90 | 299 | $groups_to_genes{$group_name} = \@elements; | |||||||||
| 91 | |||||||||||
| 92 | 299 | for my $gene (@elements) { | |||||||||
| 93 | 564 | $genes_to_groups{$gene} = $group_name; | |||||||||
| 94 | } | ||||||||||
| 95 | } | ||||||||||
| 96 | } | ||||||||||
| 97 | 67 | $self->_genes_to_groups(\%genes_to_groups); | |||||||||
| 98 | |||||||||||
| 99 | 67 | return \%groups_to_genes; | |||||||||
| 100 | } | ||||||||||
| 101 | |||||||||||
| 102 | 21 | ▶ | no Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 103 | 21 | __PACKAGE__->meta->make_immutable; | |||||||||
| 104 | |||||||||||
| 105 | 21 | 1; | |||||||||
| 106 | |||||||||||
| 107 | __END__ | ||||||||||