line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Bio::Roary::SortFasta; |
2
|
|
|
|
|
|
|
$Bio::Roary::SortFasta::VERSION = '3.11.0'; |
3
|
|
|
|
|
|
|
# ABSTRACT: sort a fasta file by name |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
|
6
|
4
|
|
|
4
|
|
220427
|
use Moose; |
|
4
|
|
|
|
|
1101576
|
|
|
4
|
|
|
|
|
25
|
|
7
|
4
|
|
|
4
|
|
25473
|
use File::Copy; |
|
4
|
|
|
|
|
5283
|
|
|
4
|
|
|
|
|
222
|
|
8
|
4
|
|
|
4
|
|
976
|
use Bio::SeqIO; |
|
4
|
|
|
|
|
146641
|
|
|
4
|
|
|
|
|
2414
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
has 'input_filename' => ( is => 'ro', isa => 'Str', required => 1 ); |
11
|
|
|
|
|
|
|
has 'output_filename' => ( is => 'ro', isa => 'Str', lazy => 1, builder => '_build_output_filename' ); |
12
|
|
|
|
|
|
|
has 'make_multiple_of_three' => ( is => 'ro', isa => 'Bool', default => 0 ); |
13
|
|
|
|
|
|
|
has 'remove_nnn_from_end' => ( is => 'ro', isa => 'Bool', default => 0 ); |
14
|
|
|
|
|
|
|
has 'similarity' => ( is => 'rw', isa => 'Num', default => 1 ); |
15
|
|
|
|
|
|
|
has 'sequences_unaligned' => ( is => 'rw', isa => 'Bool', default => 0 ); |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
has '_input_seqio' => ( is => 'ro', isa => 'Bio::SeqIO', lazy => 1, builder => '_build__input_seqio' ); |
18
|
|
|
|
|
|
|
has '_output_seqio' => ( is => 'ro', isa => 'Bio::SeqIO', lazy => 1, builder => '_build__output_seqio' ); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub _build_output_filename { |
21
|
5
|
|
|
5
|
|
10
|
my ($self) = @_; |
22
|
5
|
|
|
|
|
100
|
return $self->input_filename . ".sorted.fa"; |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub _build__input_seqio { |
26
|
6
|
|
|
6
|
|
11
|
my ($self) = @_; |
27
|
6
|
|
|
|
|
144
|
return Bio::SeqIO->new( -file => $self->input_filename, -format => 'Fasta' ); |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub _build__output_seqio { |
31
|
4
|
|
|
4
|
|
8
|
my ($self) = @_; |
32
|
4
|
|
|
|
|
74
|
return Bio::SeqIO->new( -file => ">" . $self->output_filename, -format => 'Fasta' ); |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub _add_padding_to_make_sequence_length_multiple_of_three { |
36
|
18
|
|
|
18
|
|
23
|
my ( $self, $input_seq ) = @_; |
37
|
|
|
|
|
|
|
|
38
|
18
|
|
|
|
|
33
|
my $seq_length = $input_seq->length(); |
39
|
18
|
100
|
|
|
|
201
|
if ( $seq_length % 3 == 1 ) { |
|
|
100
|
|
|
|
|
|
40
|
6
|
|
|
|
|
11
|
$input_seq->seq( $input_seq->seq() . "NN" ); |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
elsif ( $seq_length % 3 == 2 ) { |
43
|
6
|
|
|
|
|
11
|
$input_seq->seq( $input_seq->seq() . "N" ); |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
18
|
|
|
|
|
876
|
return $input_seq; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub _remove_nnn_from_all_sequences { |
50
|
2
|
|
|
2
|
|
8
|
my ( $self, $input_sequences ) = @_; |
51
|
|
|
|
|
|
|
|
52
|
2
|
|
|
|
|
4
|
for my $sequence_name ( sort keys %{$input_sequences} ) { |
|
2
|
|
|
|
|
10
|
|
53
|
4
|
|
|
|
|
222
|
my $sequence = $input_sequences->{$sequence_name}->seq(); |
54
|
4
|
|
|
|
|
48
|
$sequence =~ s/NNN$//i; |
55
|
4
|
|
|
|
|
8
|
$input_sequences->{$sequence_name}->seq($sequence); |
56
|
|
|
|
|
|
|
} |
57
|
2
|
|
|
|
|
60
|
return $input_sequences; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub sort_fasta { |
61
|
6
|
|
|
6
|
0
|
1171
|
my ($self) = @_; |
62
|
|
|
|
|
|
|
|
63
|
6
|
|
|
|
|
11
|
my %input_sequences; |
64
|
|
|
|
|
|
|
|
65
|
6
|
|
|
|
|
7
|
my $nnn_at_end_of_all_sequences = 1; |
66
|
6
|
|
|
|
|
10
|
my $sequence; |
67
|
6
|
|
|
|
|
7
|
my $variation_detected = 0; |
68
|
6
|
|
|
|
|
172
|
while ( my $input_seq = $self->_input_seqio->next_seq() ) { |
69
|
27
|
100
|
|
|
|
3564
|
$sequence = $input_seq->seq if(!defined($sequence)); |
70
|
27
|
100
|
|
|
|
609
|
$self->_add_padding_to_make_sequence_length_multiple_of_three($input_seq) if ( $self->make_multiple_of_three ); |
71
|
27
|
100
|
100
|
|
|
62
|
$nnn_at_end_of_all_sequences = 0 if ( $nnn_at_end_of_all_sequences == 1 && !( $input_seq->seq() =~ /NNN$/i ) ); |
72
|
27
|
|
|
|
|
127
|
$input_sequences{ $input_seq->display_id } = $input_seq; |
73
|
|
|
|
|
|
|
|
74
|
27
|
|
|
|
|
281
|
my $factor = $self->_percentage_similarity($sequence, $input_seq->seq); |
75
|
27
|
100
|
|
|
|
523
|
if($factor < $self->similarity) |
76
|
|
|
|
|
|
|
{ |
77
|
2
|
|
|
|
|
32
|
$self->similarity($factor); |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
5
|
100
|
100
|
|
|
303
|
$self->_remove_nnn_from_all_sequences( \%input_sequences ) if ( $self->remove_nnn_from_end && $nnn_at_end_of_all_sequences ); |
82
|
|
|
|
|
|
|
|
83
|
5
|
|
|
|
|
9
|
my $sequence_length = 0; |
84
|
5
|
|
|
|
|
6
|
my $sequences_unaligned = 0; |
85
|
5
|
|
|
|
|
26
|
for my $sequence_name ( sort keys %input_sequences ) { |
86
|
27
|
100
|
|
|
|
3468
|
$sequence_length = $input_sequences{$sequence_name}->length if($sequence_length == 0); |
87
|
27
|
100
|
|
|
|
92
|
$self->sequences_unaligned(1) if($input_sequences{$sequence_name}->length != $sequence_length); |
88
|
27
|
|
|
|
|
601
|
$self->_output_seqio->write_seq( $input_sequences{$sequence_name} ); |
89
|
|
|
|
|
|
|
} |
90
|
5
|
|
|
|
|
599
|
return $self; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub replace_input_with_output_file { |
94
|
1
|
|
|
1
|
0
|
2
|
my ($self) = @_; |
95
|
1
|
|
|
|
|
38
|
move( $self->output_filename, $self->input_filename ); |
96
|
1
|
|
|
|
|
265
|
return $self; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
sub _percentage_similarity |
100
|
|
|
|
|
|
|
{ |
101
|
31
|
|
|
31
|
|
262
|
my ($self, $string1, $string2) = @_; |
102
|
31
|
|
|
|
|
34
|
my $num_differences = 0; |
103
|
31
|
|
|
|
|
33
|
my $string1_length = length($string1); |
104
|
31
|
|
66
|
|
|
91
|
for(my $i = 0; $i < $string1_length && $i< length($string2); $i++) |
105
|
|
|
|
|
|
|
{ |
106
|
91
|
100
|
|
|
|
226
|
$num_differences++ if( substr($string1, $i, 1) ne substr($string2, $i, 1)); |
107
|
|
|
|
|
|
|
} |
108
|
31
|
100
|
|
|
|
61
|
return 1 if($num_differences == 0); |
109
|
8
|
50
|
|
|
|
15
|
return 0 if($string1_length == 0); |
110
|
8
|
|
|
|
|
23
|
return (1.0 - ($num_differences/$string1_length)); |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
4
|
|
|
4
|
|
32
|
no Moose; |
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
30
|
|
114
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
1; |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
__END__ |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=pod |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=encoding UTF-8 |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=head1 NAME |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
Bio::Roary::SortFasta - sort a fasta file by name |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=head1 VERSION |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
version 3.11.0 |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=head1 SYNOPSIS |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
sort a fasta file by name |
135
|
|
|
|
|
|
|
use Bio::Roary::SortFasta; |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
my $obj = Bio::Roary::SortFasta->new( |
138
|
|
|
|
|
|
|
input_filename => 'infasta.fa', |
139
|
|
|
|
|
|
|
); |
140
|
|
|
|
|
|
|
$obj->sort_fasta->replace_input_with_output_file; |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=head1 AUTHOR |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
Andrew J. Page <ap13@sanger.ac.uk> |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
This software is Copyright (c) 2013 by Wellcome Trust Sanger Institute. |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
This is free software, licensed under: |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
The GNU General Public License, Version 3, June 2007 |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=cut |