| 1 | package Bio::Roary::InflateClusters; | ||||||||||
| 2 | 3 | $Bio::Roary::InflateClusters::VERSION = '3.13.0'; | |||||||||
| 3 | # ABSTRACT: Take the clusters file from cd-hit and use it to inflate the output of MCL | ||||||||||
| 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 | with 'Bio::Roary::ClustersRole'; | |||||||||
| 9 | |||||||||||
| 10 | 3 | has 'mcl_filename' => ( is => 'ro', isa => 'Str', required => 1 ); | |||||||||
| 11 | 3 | has 'output_file' => ( is => 'ro', isa => 'Str', default => 'inflated_results' ); | |||||||||
| 12 | 3 | has '_mcl_fh' => ( is => 'ro',lazy => 1, builder => '_build__mcl_fh' ); | |||||||||
| 13 | 3 | has '_output_fh' => ( is => 'ro',lazy => 1, builder => '_build__output_fh' ); | |||||||||
| 14 | 3 | has 'cdhit_groups_filename' => ( is => 'ro', isa => 'Maybe[Str]' ); | |||||||||
| 15 | |||||||||||
| 16 | sub _build__output_fh | ||||||||||
| 17 | 2 | ▶ | { | ||||||||
|
Subroutine
_build__output_fh: called CC=2 SCAR=7.1
| |||||||||||
| 18 | 2 | my($self) = @_; | |||||||||
| 19 | 2 | ▶ | open(my $fh, '>', $self->output_file) or Bio::Roary::Exceptions::CouldntWriteToFile->throw( error => 'Cant write to file: ' . $self->output_file ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 20 | 2 | return $fh; | |||||||||
| 21 | } | ||||||||||
| 22 | |||||||||||
| 23 | sub _build__mcl_fh | ||||||||||
| 24 | 2 | ▶ | { | ||||||||
|
Subroutine
_build__mcl_fh: called CC=2 SCAR=7.1
| |||||||||||
| 25 | 2 | my($self) = @_; | |||||||||
| 26 | 2 | ▶ | open(my $fh, $self->mcl_filename) or Bio::Roary::Exceptions::FileNotFound->throw( error => 'Cant open file: ' . $self->mcl_filename ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 27 | 2 | return $fh; | |||||||||
| 28 | } | ||||||||||
| 29 | |||||||||||
| 30 | sub _inflate_line | ||||||||||
| 31 | 9 | ▶ | { | ||||||||
|
Subroutine
_inflate_line: called CC=2 SCAR=6.9
| |||||||||||
| 32 | 9 | my($self, $line) = @_; | |||||||||
| 33 | 9 | my @inflated_genes; | |||||||||
| 34 | 9 | chomp($line); | |||||||||
| 35 | 9 | my @gene_names = split(/[\t\s]+/, $line); | |||||||||
| 36 | 9 | for my $gene_name (@gene_names) | |||||||||
| 37 | { | ||||||||||
| 38 | 21 | push(@inflated_genes, $self->_inflate_gene($gene_name)); | |||||||||
| 39 | } | ||||||||||
| 40 | 9 | return join(' ',@inflated_genes); | |||||||||
| 41 | } | ||||||||||
| 42 | |||||||||||
| 43 | sub _inflate_gene | ||||||||||
| 44 | 21 | ▶ | { | ||||||||
|
Subroutine
_inflate_gene: called CC=2 SCAR=6.9
| |||||||||||
| 45 | 21 | my($self, $gene_name) = @_; | |||||||||
| 46 | 21 | my $inflated_gene = $gene_name; | |||||||||
| 47 | 21 | ▶ | if(defined($self->_clustered_genes->{$gene_name})) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 48 | { | ||||||||||
| 49 | 5 | $inflated_gene = $inflated_gene."\t". join("\t",@{$self->_clustered_genes->{$gene_name}}); | |||||||||
| 50 | 5 | delete($self->_clustered_genes->{$gene_name}); | |||||||||
| 51 | } | ||||||||||
| 52 | 21 | return $inflated_gene; | |||||||||
| 53 | } | ||||||||||
| 54 | |||||||||||
| 55 | sub inflate | ||||||||||
| 56 | 2 | ▶ | { | ||||||||
|
Subroutine
inflate: called CC=8 SCAR=28.2
Pod
undocumented
| |||||||||||
| 57 | 2 | my($self) = @_; | |||||||||
| 58 | 2 | my $mcl_fh = $self->_mcl_fh; | |||||||||
| 59 | |||||||||||
| 60 | # Inflate genes from cdhit which were sent to mcl | ||||||||||
| 61 | 2 | ▶ | while(<$mcl_fh>) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | { | ||||||||||
| 63 | 9 | my $line = $_; | |||||||||
| 64 | 9 | print { $self->_output_fh } $self->_inflate_line($line) . "\n"; | |||||||||
| 65 | } | ||||||||||
| 66 | |||||||||||
| 67 | # Inflate any clusters that were in the clusters file but not sent to mcl | ||||||||||
| 68 | 2 | for my $gene_name(keys %{$self->_clustered_genes}) | |||||||||
| 69 | { | ||||||||||
| 70 | 16 | ▶ | next unless(defined($self->_clustered_genes->{$gene_name})); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | 0 | print { $self->_output_fh } $gene_name."\t". join("\t",@{$self->_clustered_genes->{$gene_name}})."\n"; | |||||||||
| 72 | } | ||||||||||
| 73 | |||||||||||
| 74 | 2 | ▶ | if(defined($self->cdhit_groups_filename)) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 75 | { | ||||||||||
| 76 | # Add clusters which were excluded because the groups were full at the cdhit stage | ||||||||||
| 77 | 0 | ▶ | open(my $cdhit_fh, $self->cdhit_groups_filename) or Bio::Roary::Exceptions::FileNotFound->throw( error => "CD hit group file not found: " . $self->cdhit_groups_filename); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | 0 | ▶ | while(<$cdhit_fh>) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | { | ||||||||||
| 80 | 0 | my $line = $_; | |||||||||
| 81 | |||||||||||
| 82 | 0 | ▶ | if(defined($line)) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 83 | { | ||||||||||
| 84 | 0 | print { $self->_output_fh } $line ; | |||||||||
| 85 | } | ||||||||||
| 86 | } | ||||||||||
| 87 | } | ||||||||||
| 88 | |||||||||||
| 89 | 2 | close($self->_output_fh); | |||||||||
| 90 | 2 | 1; | |||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | 3 | ▶ | no Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 94 | 3 | __PACKAGE__->meta->make_immutable; | |||||||||
| 95 | |||||||||||
| 96 | 3 | 1; | |||||||||
| 97 | |||||||||||
| 98 | __END__ | ||||||||||