| 1 | package Bio::Roary::SortFasta; | ||||||||||||||||||||||||||||
| 2 | 4 | $Bio::Roary::SortFasta::VERSION = '3.13.0'; | |||||||||||||||||||||||||||
| 3 | # ABSTRACT: sort a fasta file by name | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use File::Copy; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use Bio::SeqIO; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 4 | has 'input_filename' => ( is => 'ro', isa => 'Str', required => 1 ); | |||||||||||||||||||||||||||
| 11 | 4 | has 'output_filename' => ( is => 'ro', isa => 'Str', lazy => 1, builder => '_build_output_filename' ); | |||||||||||||||||||||||||||
| 12 | 4 | has 'make_multiple_of_three' => ( is => 'ro', isa => 'Bool', default => 0 ); | |||||||||||||||||||||||||||
| 13 | 4 | has 'remove_nnn_from_end' => ( is => 'ro', isa => 'Bool', default => 0 ); | |||||||||||||||||||||||||||
| 14 | 4 | has 'similarity' => ( is => 'rw', isa => 'Num', default => 1 ); | |||||||||||||||||||||||||||
| 15 | 4 | has 'sequences_unaligned' => ( is => 'rw', isa => 'Bool', default => 0 ); | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 4 | has '_input_seqio' => ( is => 'ro', isa => 'Bio::SeqIO', lazy => 1, builder => '_build__input_seqio' ); | |||||||||||||||||||||||||||
| 18 | 4 | has '_output_seqio' => ( is => 'ro', isa => 'Bio::SeqIO', lazy => 1, builder => '_build__output_seqio' ); | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 5 | ▶ | sub _build_output_filename { | ||||||||||||||||||||||||||
|
Subroutine
_build_output_filename: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 5 | my ($self) = @_; | |||||||||||||||||||||||||||
| 22 | 5 | return $self->input_filename . ".sorted.fa"; | |||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 6 | ▶ | sub _build__input_seqio { | ||||||||||||||||||||||||||
|
Subroutine
_build__input_seqio: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 6 | my ($self) = @_; | |||||||||||||||||||||||||||
| 27 | 6 | return Bio::SeqIO->new( -file => $self->input_filename, -format => 'Fasta' ); | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 4 | ▶ | sub _build__output_seqio { | ||||||||||||||||||||||||||
|
Subroutine
_build__output_seqio: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | 4 | my ($self) = @_; | |||||||||||||||||||||||||||
| 32 | 4 | return Bio::SeqIO->new( -file => ">" . $self->output_filename, -format => 'Fasta' ); | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 18 | ▶ | sub _add_padding_to_make_sequence_length_multiple_of_three { | ||||||||||||||||||||||||||
|
Subroutine
_add_padding_to_make_sequence_length_multiple_of_three: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 36 | 18 | my ( $self, $input_seq ) = @_; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 18 | my $seq_length = $input_seq->length(); | |||||||||||||||||||||||||||
| 39 | 18 | ▶ | if ( $seq_length % 3 == 1 ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 40 | 6 | $input_seq->seq( $input_seq->seq() . "NN" ); | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | elsif ( $seq_length % 3 == 2 ) { | ||||||||||||||||||||||||||||
| 43 | 6 | $input_seq->seq( $input_seq->seq() . "N" ); | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 18 | return $input_seq; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 2 | ▶ | sub _remove_nnn_from_all_sequences { | ||||||||||||||||||||||||||
|
Subroutine
_remove_nnn_from_all_sequences: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 50 | 2 | my ( $self, $input_sequences ) = @_; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 2 | for my $sequence_name ( sort keys %{$input_sequences} ) { | |||||||||||||||||||||||||||
| 53 | 4 | my $sequence = $input_sequences->{$sequence_name}->seq(); | |||||||||||||||||||||||||||
| 54 | 4 | $sequence =~ s/NNN$//i; | |||||||||||||||||||||||||||
| 55 | 4 | $input_sequences->{$sequence_name}->seq($sequence); | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | 2 | return $input_sequences; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 6 | ▶ | sub sort_fasta { | ||||||||||||||||||||||||||
|
Subroutine
sort_fasta: called CC=12 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 61 | 6 | my ($self) = @_; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 6 | my %input_sequences; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 6 | my $nnn_at_end_of_all_sequences = 1; | |||||||||||||||||||||||||||
| 66 | 6 | my $sequence; | |||||||||||||||||||||||||||
| 67 | 6 | my $variation_detected = 0; | |||||||||||||||||||||||||||
| 68 | 6 | ▶ | while ( my $input_seq = $self->_input_seqio->next_seq() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 27 | ▶ | $sequence = $input_seq->seq if(!defined($sequence)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 27 | ▶ | $self->_add_padding_to_make_sequence_length_multiple_of_three($input_seq) if ( $self->make_multiple_of_three ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 27 | ▶ | $nnn_at_end_of_all_sequences = 0 if ( $nnn_at_end_of_all_sequences == 1 && !( $input_seq->seq() =~ /NNN$/i ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$nnn_at_end_of_all_sequences == 1 and not $input_seq->seq =~ /NNN$/i
MC/DC100%2/2
$nnn_at_end_of_all_sequences == 1 and not $input_seq->seq =~ /NNN$/i
$nnn_at_end_of_all_sequences == 1
not $input_seq->seq =~ /NNN$/i
Truth table100%3/3
A and not(B)
A= $nnn_at_end_of_all_sequences == 1B= not $input_seq->seq =~ /NNN$/i
| |||||||||||||||||||||||||||||
| 72 | 27 | $input_sequences{ $input_seq->display_id } = $input_seq; | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 27 | my $factor = $self->_percentage_similarity($sequence, $input_seq->seq); | |||||||||||||||||||||||||||
| 75 | 27 | ▶ | if($factor < $self->similarity) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | { | ||||||||||||||||||||||||||||
| 77 | 2 | $self->similarity($factor); | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 5 | ▶ | $self->_remove_nnn_from_all_sequences( \%input_sequences ) if ( $self->remove_nnn_from_end && $nnn_at_end_of_all_sequences ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->remove_nnn_from_end and $nnn_at_end_of_all_sequences
MC/DC100%2/2
$self->remove_nnn_from_end and $nnn_at_end_of_all_sequences
$self->remove_nnn_from_end
$nnn_at_end_of_all_sequences
Truth table100%3/3
A and B
A= $self->remove_nnn_from_endB= $nnn_at_end_of_all_sequences
| |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 5 | my $sequence_length = 0; | |||||||||||||||||||||||||||
| 84 | 5 | my $sequences_unaligned = 0; | |||||||||||||||||||||||||||
| 85 | 5 | for my $sequence_name ( sort keys %input_sequences ) { | |||||||||||||||||||||||||||
| 86 | 27 | ▶ | $sequence_length = $input_sequences{$sequence_name}->length if($sequence_length == 0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 27 | ▶ | $self->sequences_unaligned(1) if($input_sequences{$sequence_name}->length != $sequence_length); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 27 | $self->_output_seqio->write_seq( $input_sequences{$sequence_name} ); | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | 5 | return $self; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 1 | ▶ | sub replace_input_with_output_file { | ||||||||||||||||||||||||||
|
Subroutine
replace_input_with_output_file: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 94 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 95 | 1 | move( $self->output_filename, $self->input_filename ); | |||||||||||||||||||||||||||
| 96 | 1 | return $self; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | sub _percentage_similarity | ||||||||||||||||||||||||||||
| 100 | 31 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_percentage_similarity: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||
| 101 | 31 | my ($self, $string1, $string2) = @_; | |||||||||||||||||||||||||||
| 102 | 31 | my $num_differences = 0; | |||||||||||||||||||||||||||
| 103 | 31 | my $string1_length = length($string1); | |||||||||||||||||||||||||||
| 104 | 31 | ▶ | for(my $i = 0; $i < $string1_length && $i< length($string2); $i++) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$i < $string1_length and $i < length $string2
MC/DC50%1/2
$i < $string1_length and $i < length $string2
$i < $string1_length
$i < length $string2
Truth table66%2/3
A and B
A= $i < $string1_lengthB= $i < length $string2
| |||||||||||||||||||||||||||||
| 105 | { | ||||||||||||||||||||||||||||
| 106 | 91 | ▶ | $num_differences++ if( substr($string1, $i, 1) ne substr($string2, $i, 1)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | 31 | ▶ | return 1 if($num_differences == 0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 8 | ▶ | return 0 if($string1_length == 0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 8 | return (1.0 - ($num_differences/$string1_length)); | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 4 | ▶ | no Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 114 | 4 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 4 | 1; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | __END__ | ||||||||||||||||||||||||||||