File Coverage

lib/Bio/Roary/ChunkFastaFile.pm
Criterion Covered Total %
statement 41 41 100.0
branch 2 2 100.0
condition n/a
subroutine 11 11 100.0
pod n/a
total 54 54 100.0


line stmt bran cond sub pod time code
1             package Bio::Roary::ChunkFastaFile;
2             $Bio::Roary::ChunkFastaFile::VERSION = '3.10.1';
3             # ABSTRACT: Take in a FASTA file and chunk it up into smaller pieces.
4              
5              
6 4     4   191630 use Moose;
  4         607266  
  4         31  
7 4     4   31527 use Bio::SeqIO;
  4         196397  
  4         132  
8 4     4   306 use Bio::Roary::Exceptions;
  4         9  
  4         86  
9 4     4   33 use Cwd;
  4         10  
  4         249  
10 4     4   23 use File::Temp;
  4         8  
  4         1590  
11              
12             has 'fasta_file' => ( is => 'ro', isa => 'Str', required => 1 );
13             has 'target_chunk_size' => ( is => 'ro', isa => 'Int', default => 200000 );
14             has 'sequence_file_names' => ( is => 'ro', isa => 'ArrayRef', lazy => 1, builder => '_build_sequence_file_names' );
15             has '_working_directory' =>
16             ( is => 'ro', isa => 'File::Temp::Dir', default => sub { File::Temp->newdir( DIR => getcwd, CLEANUP => 1 ); } );
17             has '_working_directory_name' => ( is => 'ro', isa => 'Str', lazy => 1, builder => '_build__working_directory_name' );
18             has '_input_seqio' => ( is => 'ro', isa => 'Bio::SeqIO', lazy => 1, builder => '_build__input_seqio' );
19              
20             sub _build__working_directory_name {
21 5     5   19 my ($self) = @_;
22 5         175 return $self->_working_directory->dirname();
23             }
24              
25             sub _build__input_seqio {
26 5     5   19 my ($self) = @_;
27 5         175 return Bio::SeqIO->new( -file => $self->fasta_file, -format => 'Fasta' );
28             }
29              
30             sub _create_next_chunk_file_name {
31 20     20   93 my ( $self, $chunk_number ) = @_;
32 20         790 return join( '/', ( $self->_working_directory_name, $chunk_number . '.seq' ) );
33             }
34              
35             sub _create_next_chunk_seqio {
36 10     10   31 my ( $self, $chunk_number ) = @_;
37 10         43 return Bio::SeqIO->new( -file => ">".$self->_create_next_chunk_file_name($chunk_number), -format => 'Fasta' );
38             }
39              
40             sub _build_sequence_file_names {
41 5     5   18 my ($self) = @_;
42 5         12 my @sequence_file_names;
43 5         10 my $chunk_number = 0;
44 5         11 my $current_chunk_length = 0;
45 5         22 my $current_chunk_seqio = $self->_create_next_chunk_seqio($chunk_number);
46 5         72948 push( @sequence_file_names, $self->_create_next_chunk_file_name($chunk_number) );
47              
48 5         176 while ( my $input_seq = $self->_input_seqio->next_seq() ) {
49 30 100       7955 if ( $current_chunk_length > $self->target_chunk_size ) {
50              
51             # next chunk
52 5         13 $chunk_number++;
53 5         10 $current_chunk_length = 0;
54 5         19 $current_chunk_seqio = $self->_create_next_chunk_seqio($chunk_number);
55 5         5807 push( @sequence_file_names, $self->_create_next_chunk_file_name($chunk_number) );
56             }
57 30         117 $current_chunk_seqio->write_seq($input_seq);
58 30         8098 $current_chunk_length += $input_seq->length();
59             }
60 5         271 return \@sequence_file_names;
61             }
62              
63 4     4   28 no Moose;
  4         11  
  4         36  
64             __PACKAGE__->meta->make_immutable;
65              
66             1;
67              
68             __END__
69              
70             =pod
71              
72             =encoding UTF-8
73              
74             =head1 NAME
75              
76             Bio::Roary::ChunkFastaFile - Take in a FASTA file and chunk it up into smaller pieces.
77              
78             =head1 VERSION
79              
80             version 3.10.1
81              
82             =head1 SYNOPSIS
83              
84             Take in a FASTA file and chunk it up into smaller pieces.
85             use Bio::Roary::ChunkFastaFile;
86              
87             my $obj = Bio::Roary::ChunkFastaFile->new(
88             fasta_file => 'abc.fa',
89             );
90             $obj->sequence_file_names;
91              
92             =head1 AUTHOR
93              
94             Andrew J. Page <ap13@sanger.ac.uk>
95              
96             =head1 COPYRIGHT AND LICENSE
97              
98             This software is Copyright (c) 2013 by Wellcome Trust Sanger Institute.
99              
100             This is free software, licensed under:
101              
102             The GNU General Public License, Version 3, June 2007
103              
104             =cut