File Coverage

lib/Bio/Roary/Output/GroupMultifasta.pm
Criterion Covered Total %
statement 35 36 97.2
branch 4 6 66.6
condition n/a
subroutine 9 9 100.0
pod 0 1 0.0
total 48 52 92.3


line stmt bran cond sub pod time code
1             package Bio::Roary::Output::GroupMultifasta;
2             $Bio::Roary::Output::GroupMultifasta::VERSION = '3.11.0';
3             # ABSTRACT: Take in a group and create a multifasta file
4              
5              
6 2     2   11 use Moose;
  2         10  
  2         13  
7 2     2   13536 use Bio::SeqIO;
  2         110921  
  2         64  
8 2     2   17 use Bio::Roary::Exceptions;
  2         3  
  2         39  
9 2     2   9 use Bio::Roary::AnalyseGroups;
  2         6  
  2         561  
10              
11             has 'group_name' => ( is => 'ro', isa => 'Str', required => 1 );
12             has 'analyse_groups' => ( is => 'ro', isa => 'Bio::Roary::AnalyseGroups', required => 1 );
13             has 'output_filename_base' => ( is => 'ro', isa => 'Str', default => 'output_groups' );
14             has '_genes' => ( is => 'ro', isa => 'ArrayRef', lazy => 1, builder => '_build__genes' );
15             has '_output_seq_io' => ( is => 'ro', lazy => 1, builder => '_build__output_seq_io' );
16              
17             sub _build__output_seq_io {
18 12     12   24 my ($self) = @_;
19 12         312 my $output_name = $self->output_filename_base . '_' . $self->group_name;
20 12         40 $output_name =~ s!\W!_!g;
21 12         21 $output_name .= '.fa';
22 12         71 return Bio::SeqIO->new( -file => ">" . $output_name, -format => 'Fasta' );
23             }
24              
25             sub _build__genes {
26 12     12   24 my ($self) = @_;
27 12         301 return $self->analyse_groups->_groups_to_genes->{ $self->group_name };
28             }
29              
30             sub _lookup_sequence {
31 24     24   69 my ( $self, $gene, $filename ) = @_;
32 24 50       56 return undef if(! defined($filename));
33 24         163 my $fasta_obj = Bio::SeqIO->new( -file => $filename, -format => 'Fasta' );
34 24         44750 while ( my $seq = $fasta_obj->next_seq() ) {
35 66 100       11542 next unless ( $seq->display_id eq $gene );
36 24         451 return $seq;
37             }
38 0         0 return undef;
39             }
40              
41             sub create_file {
42 12     12 0 32 my ($self) = @_;
43 12         19 for my $gene ( @{ $self->_genes } ) {
  12         326  
44 24         3056 my $seq = $self->_lookup_sequence( $gene, $self->analyse_groups->_genes_to_file->{$gene} );
45 24 50       1792 next unless ( defined($seq) );
46 24         723 $self->_output_seq_io->write_seq($seq);
47             }
48              
49 12         2820 1;
50             }
51              
52 2     2   13 no Moose;
  2         2  
  2         20  
53             __PACKAGE__->meta->make_immutable;
54              
55             1;
56              
57             __END__
58              
59             =pod
60              
61             =encoding UTF-8
62              
63             =head1 NAME
64              
65             Bio::Roary::Output::GroupMultifasta - Take in a group and create a multifasta file
66              
67             =head1 VERSION
68              
69             version 3.11.0
70              
71             =head1 SYNOPSIS
72              
73             Take in a group and create a multifasta file
74             use Bio::Roary::Output::GroupMultifasta;
75              
76             my $obj = Bio::Roary::Output::GroupMultifasta->new(
77             group_name => 'aaa',
78             analyse_groups => $analyse_groups,
79             output_filename_base => 'abc'
80             );
81             $obj->create_file();
82              
83             =head1 AUTHOR
84              
85             Andrew J. Page <ap13@sanger.ac.uk>
86              
87             =head1 COPYRIGHT AND LICENSE
88              
89             This software is Copyright (c) 2013 by Wellcome Trust Sanger Institute.
90              
91             This is free software, licensed under:
92              
93             The GNU General Public License, Version 3, June 2007
94              
95             =cut