| 1 | package Bio::Roary::MergeMultifastaAlignments; | ||||||||||
| 2 | 1 | $Bio::Roary::MergeMultifastaAlignments::VERSION = '3.13.0'; | |||||||||
| 3 | # ABSTRACT: Merge multifasta alignment files with equal numbers of sequences. | ||||||||||
| 4 | |||||||||||
| 5 | |||||||||||
| 6 | 1 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use Bio::SeqIO; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use Bio::Roary::Output::CoreGeneAlignmentCoordinatesEMBL; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 1 | has 'multifasta_files' => ( is => 'ro', isa => 'ArrayRef', required => 1 ); | |||||||||
| 11 | 1 | has 'sample_names' => ( is => 'ro', isa => 'ArrayRef', required => 1 ); | |||||||||
| 12 | 1 | has 'sample_names_to_genes' => ( is => 'rw', isa => 'HashRef', required => 1 ); | |||||||||
| 13 | 1 | has 'output_filename' => ( is => 'ro', isa => 'Str', default => 'core_alignment.aln' ); | |||||||||
| 14 | 1 | has 'output_header_filename' => ( is => 'ro', isa => 'Str', default => 'core_alignment_header.embl' ); | |||||||||
| 15 | 1 | has '_output_seqio_obj' => ( is => 'ro', isa => 'Bio::SeqIO', lazy => 1, builder => '_build__output_seqio_obj' ); | |||||||||
| 16 | 1 | has '_gene_lengths' => ( is => 'rw', isa => 'HashRef', lazy => 1, builder => '_build__gene_lengths' ); | |||||||||
| 17 | 2 | ▶ | has '_gene_to_sequence' => ( is => 'rw', isa => 'HashRef', default => sub { {} } ); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 18 | 1 | has '_sorted_multifasta_files' => ( is => 'rw', isa => 'ArrayRef', lazy => 1, builder => '_build__sorted_multifasta_files' ); | |||||||||
| 19 | |||||||||||
| 20 | 2 | ▶ | sub BUILD { | ||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 21 | 2 | my ($self) = @_; | |||||||||
| 22 | 2 | $self->_gene_lengths; | |||||||||
| 23 | } | ||||||||||
| 24 | |||||||||||
| 25 | 4 | ▶ | sub _input_seq_io_obj { | ||||||||
|
Subroutine
_input_seq_io_obj: called CC=1 SCAR=0.0
| |||||||||||
| 26 | 4 | my ( $self, $filename ) = @_; | |||||||||
| 27 | 4 | return Bio::SeqIO->new( -file => $filename, -format => 'Fasta' ); | |||||||||
| 28 | } | ||||||||||
| 29 | |||||||||||
| 30 | 2 | ▶ | sub _build__output_seqio_obj { | ||||||||
|
Subroutine
_build__output_seqio_obj: called CC=1 SCAR=0.0
| |||||||||||
| 31 | 2 | my ($self) = @_; | |||||||||
| 32 | 2 | return Bio::SeqIO->new( -file => ">" . $self->output_filename, -format => 'Fasta' ); | |||||||||
| 33 | } | ||||||||||
| 34 | |||||||||||
| 35 | 2 | ▶ | sub _build__gene_lengths { | ||||||||
|
Subroutine
_build__gene_lengths: called CC=5 SCAR=16.1
| |||||||||||
| 36 | 2 | my ($self) = @_; | |||||||||
| 37 | 2 | my %gene_lengths; | |||||||||
| 38 | 2 | for my $filename ( @{ $self->_sorted_multifasta_files } ) { | |||||||||
| 39 | 4 | my $seq_io = $self->_input_seq_io_obj($filename); | |||||||||
| 40 | 4 | ▶ | next unless ( defined($seq_io) ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | 4 | ▶ | while ( my $seq_record = $seq_io->next_seq ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | |||||||||||
| 43 | # Save all of the gene sequences to memory, massive speedup but a bit naughty. | ||||||||||
| 44 | 8 | $self->_gene_to_sequence->{$filename}->{ $seq_record->display_id } = $seq_record->seq; | |||||||||
| 45 | 8 | ▶ | $gene_lengths{$filename} = $seq_record->length() if ( !defined( $gene_lengths{$filename} ) ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | } | ||||||||||
| 47 | } | ||||||||||
| 48 | |||||||||||
| 49 | 2 | return \%gene_lengths; | |||||||||
| 50 | } | ||||||||||
| 51 | |||||||||||
| 52 | 2 | ▶ | sub _build__sorted_multifasta_files { | ||||||||
|
Subroutine
_build__sorted_multifasta_files: called CC=1 SCAR=0.0
| |||||||||||
| 53 | 2 | my ($self) = @_; | |||||||||
| 54 | 2 | my @sorted_gene_files = sort @{ $self->multifasta_files }; | |||||||||
| 55 | 2 | return \@sorted_gene_files; | |||||||||
| 56 | } | ||||||||||
| 57 | |||||||||||
| 58 | 9 | ▶ | sub _sequence_for_sample_from_gene_file { | ||||||||
|
Subroutine
_sequence_for_sample_from_gene_file: called CC=3 SCAR=11.0
| |||||||||||
| 59 | 9 | my ( $self, $sample_name, $gene_file ) = @_; | |||||||||
| 60 | |||||||||||
| 61 | # loop over this to get the geneIDs | ||||||||||
| 62 | 9 | for my $gene_id ( sort keys %{ $self->_gene_to_sequence->{$gene_file} } ) { | |||||||||
| 63 | 14 | ▶ | if ( defined( $self->sample_names_to_genes->{$sample_name}->{$gene_id} ) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | 8 | return $self->_gene_to_sequence->{$gene_file}->{$gene_id}; | |||||||||
| 65 | } | ||||||||||
| 66 | } | ||||||||||
| 67 | 1 | return $self->_padded_string_for_gene_file($gene_file); | |||||||||
| 68 | } | ||||||||||
| 69 | |||||||||||
| 70 | 1 | ▶ | sub _padded_string_for_gene_file { | ||||||||
|
Subroutine
_padded_string_for_gene_file: called CC=2 SCAR=7.1
| |||||||||||
| 71 | 1 | my ( $self, $gene_file ) = @_; | |||||||||
| 72 | 1 | ▶ | return '' unless ( defined( $self->_gene_lengths->{$gene_file} ) ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 73 | 1 | return '-' x ( $self->_gene_lengths->{$gene_file} ); | |||||||||
| 74 | } | ||||||||||
| 75 | |||||||||||
| 76 | 5 | ▶ | sub _create_merged_sequence_for_sample { | ||||||||
|
Subroutine
_create_merged_sequence_for_sample: called CC=2 SCAR=6.9
| |||||||||||
| 77 | 5 | my ( $self, $sample_name ) = @_; | |||||||||
| 78 | 5 | my $merged_sequence = ''; | |||||||||
| 79 | 5 | for my $gene_file ( @{ $self->_sorted_multifasta_files } ) { | |||||||||
| 80 | 9 | $merged_sequence .= $self->_sequence_for_sample_from_gene_file( $sample_name, $gene_file ); | |||||||||
| 81 | } | ||||||||||
| 82 | 5 | return $merged_sequence; | |||||||||
| 83 | } | ||||||||||
| 84 | |||||||||||
| 85 | 2 | ▶ | sub merge_files { | ||||||||
|
Subroutine
merge_files: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 86 | 2 | my ($self) = @_; | |||||||||
| 87 | |||||||||||
| 88 | 2 | for my $sample_name ( @{ $self->sample_names } ) { | |||||||||
| 89 | 5 | my $sequence = $self->_create_merged_sequence_for_sample($sample_name); | |||||||||
| 90 | 5 | my $seq_io = Bio::Seq->new( -display_id => $sample_name, -seq => $sequence ); | |||||||||
| 91 | 5 | $self->_output_seqio_obj->write_seq($seq_io); | |||||||||
| 92 | } | ||||||||||
| 93 | |||||||||||
| 94 | # Create a header file which gives the coordinates of each gene in the multifasta | ||||||||||
| 95 | Bio::Roary::Output::CoreGeneAlignmentCoordinatesEMBL->new( | ||||||||||
| 96 | 2 | multifasta_files => $self->_sorted_multifasta_files, | |||||||||
| 97 | gene_lengths => $self->_gene_lengths, | ||||||||||
| 98 | output_filename => $self->output_header_filename | ||||||||||
| 99 | )->create_file(); | ||||||||||
| 100 | |||||||||||
| 101 | 2 | return 1; | |||||||||
| 102 | } | ||||||||||
| 103 | |||||||||||
| 104 | 1 | ▶ | no Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 105 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||
| 106 | |||||||||||
| 107 | 1 | 1; | |||||||||
| 108 | |||||||||||
| 109 | __END__ | ||||||||||