diff Roary/lib/Bio/Roary/SequenceLengths.pm @ 0:c47a5f61bc9f draft

Uploaded
author dereeper
date Fri, 14 May 2021 20:27:06 +0000
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Roary/lib/Bio/Roary/SequenceLengths.pm	Fri May 14 20:27:06 2021 +0000
@@ -0,0 +1,43 @@
+package Bio::Roary::SequenceLengths;
+
+# ABSTRACT:  Take in a fasta file and create a hash with the length of each sequence
+
+=head1 SYNOPSIS
+
+Add labels to the groups
+   use Bio::Roary::SequenceLengths;
+   
+   my $obj = Bio::Roary::SequenceLengths->new(
+     fasta_file   => 'abc.fa',
+   );
+   $obj->sequence_lengths;
+
+=cut
+
+use Moose;
+use Bio::SeqIO;
+use Bio::Roary::Exceptions;
+
+has 'fasta_file'       => ( is => 'ro', isa => 'Str',        required => 1 );
+has 'sequence_lengths' => ( is => 'ro', isa => 'HashRef',    lazy     => 1, builder => '_build_sequence_lengths' );
+has '_input_seqio'     => ( is => 'ro', isa => 'Bio::SeqIO', lazy     => 1, builder => '_build__input_seqio' );
+
+sub _build__input_seqio {
+    my ($self) = @_;
+    return Bio::SeqIO->new( -file => $self->fasta_file, -format => 'Fasta' );
+}
+
+sub _build_sequence_lengths {
+    my ($self) = @_;
+
+    my %sequence_lengths;
+    while ( my $input_seq = $self->_input_seqio->next_seq() ) {
+        $sequence_lengths{ $input_seq->display_id } = $input_seq->length();
+    }
+    return \%sequence_lengths;
+}
+
+no Moose;
+__PACKAGE__->meta->make_immutable;
+
+1;