| 1 | package Bio::Roary::IterativeCdhit; | ||
| 2 | $Bio::Roary::IterativeCdhit::VERSION = '3.13.0'; | ||
| 3 | # ABSTRACT: Run CDhit iteratively with reducing thresholds, removing full clusters each time | ||
| 4 | |||
| 5 | |||
| 6 | use Moose; | ||
| 7 | use Bio::SeqIO; | ||
| 8 | use Bio::Roary::Exceptions; | ||
| 9 | use Bio::Roary::External::Cdhit; | ||
| 10 | use Bio::Roary::FilterFullClusters; | ||
| 11 | use File::Copy; | ||
| 12 | use Log::Log4perl qw(:easy); | ||
| 13 | # CD hit is run locally | ||
| 14 | |||
| 15 | has 'output_cd_hit_filename' => ( is => 'ro', isa => 'Str', required => 1 ); | ||
| 16 | has 'output_combined_filename' => ( is => 'ro', isa => 'Str', required => 1 ); | ||
| 17 | has 'number_of_input_files' => ( is => 'ro', isa => 'Int', required => 1 ); | ||
| 18 | has 'output_filtered_clustered_fasta' => ( is => 'ro', isa => 'Str', required => 1 ); | ||
| 19 | |||
| 20 | has 'lower_bound_percentage' => ( is => 'ro', isa => 'Num', default => 0.98 ); | ||
| 21 | has 'upper_bound_percentage' => ( is => 'ro', isa => 'Num', default => 0.99 ); | ||
| 22 | has 'step_size_percentage' => ( is => 'ro', isa => 'Num', default => 0.005 ); | ||
| 23 | has 'cpus' => ( is => 'ro', isa => 'Int', default => 1 ); | ||
| 24 | has 'logger' => ( is => 'ro', lazy => 1, builder => '_build_logger'); | ||
| 25 | |||
| 26 | sub _build_logger | ||
| 27 | { | ||
| 28 | my ($self) = @_; | ||
| 29 | Log::Log4perl->easy_init(level => $ERROR); | ||
| 30 | my $logger = get_logger(); | ||
| 31 | return $logger; | ||
| 32 | } | ||
| 33 | |||
| 34 | |||
| 35 | sub run { | ||
| 36 | my ($self) = @_; | ||
| 37 | |||
| 38 | $self->filter_complete_clusters( | ||
| 39 | $self->output_cd_hit_filename, | ||
| 40 | 1, | ||
| 41 | $self->output_combined_filename, | ||
| 42 | $self->number_of_input_files, | ||
| 43 | $self->output_filtered_clustered_fasta, 1 | ||
| 44 | ); | ||
| 45 | |||
| 46 | for ( my $percent_match = $self->upper_bound_percentage ; $percent_match >= $self->lower_bound_percentage ; $percent_match -= $self->step_size_percentage ) { | ||
| 47 | $self->filter_complete_clusters( | ||
| 48 | $self->output_cd_hit_filename, | ||
| 49 | $percent_match, | ||
| 50 | $self->output_combined_filename, | ||
| 51 | $self->number_of_input_files, | ||
| 52 | $self->output_filtered_clustered_fasta, 0 | ||
| 53 | ); | ||
| 54 | } | ||
| 55 | |||
| 56 | my $cdhit_obj = Bio::Roary::External::Cdhit->new( | ||
| 57 | input_file => $self->output_combined_filename, | ||
| 58 | output_base => $self->output_cd_hit_filename, | ||
| 59 | _length_difference_cutoff => $self->lower_bound_percentage, | ||
| 60 | _sequence_identity_threshold => $self->lower_bound_percentage, | ||
| 61 | cpus => $self->cpus, | ||
| 62 | logger => $self->logger | ||
| 63 | ); | ||
| 64 | $cdhit_obj->run(); | ||
| 65 | return $cdhit_obj->clusters_filename; | ||
| 66 | } | ||
| 67 | |||
| 68 | sub filter_complete_clusters { | ||
| 69 | my ( $self, $output_cd_hit_filename, $percentage_match, $output_combined_filename, $number_of_input_files, | ||
| 70 | $output_filtered_clustered_fasta, | ||
| 71 | $greater_than_or_equal ) | ||
| 72 | = @_; | ||
| 73 | my $cdhit_obj = Bio::Roary::External::Cdhit->new( | ||
| 74 | input_file => $output_combined_filename, | ||
| 75 | output_base => $output_cd_hit_filename, | ||
| 76 | _length_difference_cutoff => $percentage_match, | ||
| 77 | _sequence_identity_threshold => $percentage_match, | ||
| 78 | cpus => $self->cpus, | ||
| 79 | ); | ||
| 80 | $cdhit_obj->run(); | ||
| 81 | |||
| 82 | my $filter_clusters = Bio::Roary::FilterFullClusters->new( | ||
| 83 | clusters_filename => $cdhit_obj->clusters_filename, | ||
| 84 | fasta_file => $output_cd_hit_filename, | ||
| 85 | number_of_input_files => $number_of_input_files, | ||
| 86 | output_file => $output_filtered_clustered_fasta, | ||
| 87 | _greater_than_or_equal => $greater_than_or_equal, | ||
| 88 | cdhit_input_fasta_file => $output_combined_filename, | ||
| 89 | cdhit_output_fasta_file => $output_combined_filename . '.filtered', | ||
| 90 | output_groups_file => $output_combined_filename . '.groups' | ||
| 91 | ); | ||
| 92 | |||
| 93 | $filter_clusters->filter_complete_cluster_from_original_fasta(); | ||
| 94 | move( $filter_clusters->cdhit_output_fasta_file, $output_combined_filename ); | ||
| 95 | } | ||
| 96 | |||
| 97 | no Moose; | ||
| 98 | __PACKAGE__->meta->make_immutable; | ||
| 99 | |||
| 100 | 1; | ||
| 101 | |||
| 102 | __END__ |