| 1 | package Bio::Roary::FilterUnknownsFromFasta; | ||||||||||
| 2 | 4 | $Bio::Roary::FilterUnknownsFromFasta::VERSION = '3.13.0'; | |||||||||
| 3 | # ABSTRACT: Take in fasta files, remove sequences with too many unknowns and return a list of the new files | ||||||||||
| 4 | |||||||||||
| 5 | |||||||||||
| 6 | 4 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 4 | ▶ | use Bio::SeqIO; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 4 | ▶ | use Cwd; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 4 | ▶ | use Bio::Roary::Exceptions; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 4 | ▶ | use File::Basename; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 4 | has 'fasta_files' => ( is => 'ro', isa => 'ArrayRef', required => 1 ); | |||||||||
| 13 | 4 | has 'apply_unknowns_filter' => ( is => 'rw', isa => 'Bool', default => 1 ); | |||||||||
| 14 | 4 | has 'maximum_percentage_of_unknowns' => ( is => 'ro', isa => 'Num', default => 5 ); | |||||||||
| 15 | |||||||||||
| 16 | 4 | has 'filtered_fasta_files' => ( is => 'ro', isa => 'ArrayRef', lazy => 1, builder => '_build_filtered_fasta_files' ); | |||||||||
| 17 | |||||||||||
| 18 | 19 | ▶ | has 'input_fasta_to_output_fasta' => ( is => 'ro', isa => 'HashRef', default => sub {{}} ); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 19 | |||||||||||
| 20 | sub _build_filtered_fasta_files | ||||||||||
| 21 | 19 | ▶ | { | ||||||||
|
Subroutine
_build_filtered_fasta_files: called CC=2 SCAR=6.9
| |||||||||||
| 22 | 19 | my ($self) = @_; | |||||||||
| 23 | |||||||||||
| 24 | 19 | my @output_file_names; | |||||||||
| 25 | 19 | for my $fasta_file (@{$self->fasta_files}) | |||||||||
| 26 | { | ||||||||||
| 27 | 44 | my ( $filename, $directories, $suffix ) = fileparse($fasta_file); | |||||||||
| 28 | 44 | push(@output_file_names, $self->_filter_fasta_sequences_and_return_new_file($filename,$fasta_file )); | |||||||||
| 29 | } | ||||||||||
| 30 | 19 | return \@output_file_names; | |||||||||
| 31 | } | ||||||||||
| 32 | |||||||||||
| 33 | sub _does_sequence_contain_too_many_unknowns | ||||||||||
| 34 | 183 | ▶ | { | ||||||||
|
Subroutine
_does_sequence_contain_too_many_unknowns: called CC=2 SCAR=6.9
| |||||||||||
| 35 | 183 | my ($self, $sequence_obj) = @_; | |||||||||
| 36 | 183 | my $maximum_number_of_Xs = int(($sequence_obj->length()*$self->maximum_percentage_of_unknowns)/100); | |||||||||
| 37 | 183 | my $number_of_Xs_found = () = $sequence_obj->seq() =~ /X/g; | |||||||||
| 38 | 183 | ▶ | if($number_of_Xs_found > $maximum_number_of_Xs) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | { | ||||||||||
| 40 | 2 | return 1; | |||||||||
| 41 | } | ||||||||||
| 42 | else | ||||||||||
| 43 | { | ||||||||||
| 44 | 181 | return 0; | |||||||||
| 45 | } | ||||||||||
| 46 | } | ||||||||||
| 47 | |||||||||||
| 48 | |||||||||||
| 49 | sub _filter_fasta_sequences_and_return_new_file | ||||||||||
| 50 | 44 | ▶ | { | ||||||||
|
Subroutine
_filter_fasta_sequences_and_return_new_file: called CC=3 SCAR=11.0
| |||||||||||
| 51 | 44 | my ($self, $output_file, $input_file) = @_; | |||||||||
| 52 | 44 | my $output_filename = $output_file.'.tmp.filtered.fa'; | |||||||||
| 53 | 44 | my $out_fasta_obj = Bio::SeqIO->new( -file => ">".$output_filename, -format => 'Fasta'); | |||||||||
| 54 | 44 | my $fasta_obj = Bio::SeqIO->new( -file => $input_file, -format => 'Fasta'); | |||||||||
| 55 | |||||||||||
| 56 | 44 | $self->input_fasta_to_output_fasta->{$input_file} = $output_filename; | |||||||||
| 57 | |||||||||||
| 58 | 44 | ▶ | while(my $seq = $fasta_obj->next_seq()) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | { | ||||||||||
| 60 | 183 | ▶ | if($self->_does_sequence_contain_too_many_unknowns($seq)) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | { | ||||||||||
| 62 | 2 | next; | |||||||||
| 63 | } | ||||||||||
| 64 | # strip out extra details put in by fastatranslate | ||||||||||
| 65 | 181 | $seq->description(undef); | |||||||||
| 66 | 181 | $out_fasta_obj->write_seq($seq); | |||||||||
| 67 | } | ||||||||||
| 68 | 44 | return $output_filename; | |||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | |||||||||||
| 72 | |||||||||||
| 73 | 4 | ▶ | no Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 74 | 4 | __PACKAGE__->meta->make_immutable; | |||||||||
| 75 | |||||||||||
| 76 | 4 | 1; | |||||||||
| 77 | |||||||||||
| 78 | __END__ | ||||||||||