« lib/Bio/AssemblyImprovement/Validate/Executable.pm
↑ Summary
lib/Bio/AssemblyImprovement/IvaQC/Main.pm »
| 1 | package Bio::AssemblyImprovement::Util::OrderContigsByLength; | ||||||||||
| 2 | |||||||||||
| 3 | # ABSTRACT: Sorts a fasta file by sequence length | ||||||||||
| 4 | |||||||||||
| 5 | |||||||||||
| 6 | 2 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 2 | ▶ | use Bio::SeqIO; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 2 | ▶ | use File::Basename; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 2 | ▶ | use File::Copy; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 2 | with 'Bio::AssemblyImprovement::Scaffold::SSpace::TempDirectoryRole'; | |||||||||
| 12 | |||||||||||
| 13 | 2 | has 'input_filename' => ( is => 'ro', isa => 'Str', required => 1 ); | |||||||||
| 14 | 2 | has 'contig_basename' => ( is => 'rw', isa => 'Str', default => 'contig' ); | |||||||||
| 15 | 2 | has 'rename_contigs' => ( is => 'rw', isa => 'Bool', default => 1 ); | |||||||||
| 16 | 2 | has 'output_filename' => ( is => 'rw', isa => 'Str', lazy_build => 1 ); | |||||||||
| 17 | 2 | has '_output_suffix' => ( is => 'ro', isa => 'Str', default => 'sorted' ); | |||||||||
| 18 | 2 | has '_temp_output_filename' => ( is => 'ro', isa => 'Str', lazy_build => 1 ); | |||||||||
| 19 | 2 | has '_input_fh' => ( is => 'ro', isa => 'Bio::SeqIO', lazy_build => 1 ); | |||||||||
| 20 | 2 | has '_output_fh' => ( is => 'ro', isa => 'Bio::SeqIO', lazy_build => 1 ); | |||||||||
| 21 | |||||||||||
| 22 | sub _build_output_filename | ||||||||||
| 23 | 1 | ▶ | { | ||||||||
|
Subroutine
_build_output_filename: called CC=1 SCAR=0.0
| |||||||||||
| 24 | 1 | my ($self) = @_; | |||||||||
| 25 | 1 | my ( $filename, $directories, $suffix ) = fileparse( $self->input_filename, qr/\.[^.]*/ ); | |||||||||
| 26 | 1 | return $directories . $filename . $suffix . "." . $self->_output_suffix; | |||||||||
| 27 | } | ||||||||||
| 28 | |||||||||||
| 29 | sub _build__temp_output_filename | ||||||||||
| 30 | 2 | ▶ | { | ||||||||
|
Subroutine
_build__temp_output_filename: called CC=1 SCAR=0.0
| |||||||||||
| 31 | 2 | my ($self) = @_; | |||||||||
| 32 | 2 | my ( $filename, $directories, $suffix ) = fileparse( $self->input_filename, qr/\.[^.]*/ ); | |||||||||
| 33 | 2 | return join('/',($self->_temp_directory,$filename.'.temp.fa')); | |||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | sub _build__input_fh | ||||||||||
| 37 | 2 | ▶ | { | ||||||||
|
Subroutine
_build__input_fh: called CC=1 SCAR=0.0
| |||||||||||
| 38 | 2 | my ($self) = @_; | |||||||||
| 39 | 2 | return Bio::SeqIO->new( -file => $self->input_filename, -format => 'Fasta' ); | |||||||||
| 40 | } | ||||||||||
| 41 | |||||||||||
| 42 | sub _build__output_fh | ||||||||||
| 43 | 2 | ▶ | { | ||||||||
|
Subroutine
_build__output_fh: called CC=1 SCAR=0.0
| |||||||||||
| 44 | 2 | my ($self) = @_; | |||||||||
| 45 | 2 | return Bio::SeqIO->new( -file => "+>" . $self->_temp_output_filename, -format => 'Fasta' ); | |||||||||
| 46 | } | ||||||||||
| 47 | |||||||||||
| 48 | sub _rename_contig | ||||||||||
| 49 | 9 | ▶ | { | ||||||||
|
Subroutine
_rename_contig: called CC=1 SCAR=0.0
| |||||||||||
| 50 | 9 | my ($self,$count) = @_; | |||||||||
| 51 | 9 | return $self->contig_basename.$count; | |||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | sub run | ||||||||||
| 55 | 2 | ▶ | { | ||||||||
|
Subroutine
run: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||
| 56 | 2 | my ($self) = @_; | |||||||||
| 57 | |||||||||||
| 58 | # get sequences | ||||||||||
| 59 | 2 | my @sequences; | |||||||||
| 60 | 2 | ▶ | while( my $seq = $self->_input_fh->next_seq() ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | { | ||||||||||
| 62 | 8 | push @sequences, $seq; | |||||||||
| 63 | } | ||||||||||
| 64 | |||||||||||
| 65 | # sort sequences | ||||||||||
| 66 | 2 | @sequences = sort {$b->length <=> $a->length} @sequences; | |||||||||
| 67 | |||||||||||
| 68 | # write to temp file | ||||||||||
| 69 | 2 | my $count = 1; | |||||||||
| 70 | 2 | for my $seq (@sequences) | |||||||||
| 71 | { | ||||||||||
| 72 | 8 | ▶ | $seq->display_id($self->_rename_contig($count)) if $self->rename_contigs; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 73 | 8 | $self->_output_fh->write_seq( $seq ); | |||||||||
| 74 | 8 | $count++; | |||||||||
| 75 | } | ||||||||||
| 76 | |||||||||||
| 77 | # move sorted temp file to outputfile | ||||||||||
| 78 | 2 | move($self->_temp_output_filename, $self->output_filename); | |||||||||
| 79 | } | ||||||||||
| 80 | |||||||||||
| 81 | 2 | ▶ | no Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 82 | 2 | __PACKAGE__->meta->make_immutable; | |||||||||
| 83 | 2 | 1; | |||||||||
| 84 | |||||||||||
| 85 | __END__ | ||||||||||