| 1 | package Bio::MUST::Apps::Leel::AliProcessor; | ||
| 2 | # ABSTRACT: Internal class for leel tool | ||
| 3 | $Bio::MUST::Apps::Leel::AliProcessor::VERSION = '0.262780'; | ||
| 4 | use Moose; | ||
| 5 | use namespace::autoclean; | ||
| 6 | |||
| 7 | use autodie; | ||
| 8 | use feature qw(say); | ||
| 9 | |||
| 10 | use Smart::Comments -ENV; | ||
| 11 | |||
| 12 | use Carp; | ||
| 13 | use File::Basename; | ||
| 14 | use List::AllUtils 'uniq'; | ||
| 15 | use Path::Class qw(file); | ||
| 16 | use Test::Deep::NoTest 'eq_deeply'; | ||
| 17 | |||
| 18 | use Bio::MUST::Core; | ||
| 19 | use Bio::MUST::Core::Utils qw(:filenames secure_outfile); | ||
| 20 | use aliased 'Bio::MUST::Core::Ali'; | ||
| 21 | use aliased 'Bio::MUST::Core::GeneticCode::Factory'; | ||
| 22 | use aliased 'Bio::MUST::Apps::SlaveAligner::Local'; | ||
| 23 | use aliased 'Bio::MUST::Apps::Leel::OrgProcessor'; | ||
| 24 | |||
| 25 | with 'Bio::MUST::Apps::Roles::AliProcable'; | ||
| 26 | |||
| 27 | |||
| 28 | has 'run_proc' => ( | ||
| 29 | is => 'ro', | ||
| 30 | isa => 'Bio::MUST::Apps::Leel::RunProcessor', | ||
| 31 | required => 1, | ||
| 32 | ); | ||
| 33 | |||
| 34 | |||
| 35 | has '+ali' => ( | ||
| 36 | handles => { | ||
| 37 | protein_for => 'get_seq_with_id', | ||
| 38 | }, | ||
| 39 | ); | ||
| 40 | |||
| 41 | |||
| 42 | has 'cds_ali' => ( | ||
| 43 | is => 'ro', | ||
| 44 | isa => 'Bio::MUST::Core::Ali', | ||
| 45 | init_arg => undef, | ||
| 46 | lazy => 1, | ||
| 47 | builder => '_build_cds_ali', | ||
| 48 | ); | ||
| 49 | |||
| 50 | |||
| 51 | ## no critic (ProhibitUnusedPrivateSubroutines) | ||
| 52 | |||
| 53 | sub _build_cds_ali { | ||
| 54 | return Ali->new( file => shift->ali->filename ); | ||
| 55 | } | ||
| 56 | |||
| 57 | |||
| 58 | sub _build_integrator { | ||
| 59 | return Local->new( ali => shift->cds_ali ); | ||
| 60 | } | ||
| 61 | |||
| 62 | ## use critic | ||
| 63 | |||
| 64 | |||
| 65 | sub _check_ali_by_translation { | ||
| 66 | my $self = shift; | ||
| 67 | |||
| 68 | my $rp = $self->run_proc; | ||
| 69 | |||
| 70 | # build genetic codes for all orgs | ||
| 71 | # Note: we uses the raw config file to fetch code ids... | ||
| 72 | # ... if code ids are not specified behavior is not defined | ||
| 73 | # ... (because the code attribute default has yet been triggered) | ||
| 74 | my $gc_fac = Factory->new; | ||
| 75 | my %code_for = map { | ||
| 76 | $_->{org} => $gc_fac->code_for( $_->{code} ) | ||
| 77 | } $rp->all_orgs; | ||
| 78 | |||
| 79 | # translate aligned transcripts to aligned proteins (using right code)... | ||
| 80 | # ... sort seqs according to full_id... | ||
| 81 | # ... and uniformize gap/missing chars | ||
| 82 | my $cds_ali = $self->cds_ali; | ||
| 83 | my @got_seqs = sort { $a->full_id cmp $b->full_id } map { | ||
| 84 | $code_for{ $_->full_org }->translate($_, 1)->gapify | ||
| 85 | } $cds_ali->all_seqs; | ||
| 86 | |||
| 87 | # fetch original proteins | ||
| 88 | my $ali = $self->ali; | ||
| 89 | my @exp_seqs = sort { $a->full_id cmp $b->full_id } map { | ||
| 90 | $_->gapify | ||
| 91 | } $ali->all_seqs; | ||
| 92 | |||
| 93 | # build uniformized versions of translated and original Alis | ||
| 94 | my $got_ali = Ali->new( seqs => \@got_seqs )->uniformize; | ||
| 95 | my $exp_ali = Ali->new( seqs => \@exp_seqs )->uniformize; | ||
| 96 | |||
| 97 | # check congruence between translated and original Alis | ||
| 98 | unless (eq_deeply $got_ali, $exp_ali) { | ||
| 99 | carp '[ALI] Warning: round-trip check failed;' | ||
| 100 | . ' writing -got and -exp files!'; | ||
| 101 | |||
| 102 | # build ALI outfile honoring out_dir and out_suffix | ||
| 103 | # TODO: try to avoid code duplication here? | ||
| 104 | my $cds_ali = $self->cds_ali; | ||
| 105 | my $outfile = $cds_ali->filename; | ||
| 106 | $outfile = file( $rp->out_dir, basename($outfile) ) if $rp->out_dir; | ||
| 107 | |||
| 108 | $got_ali->store( secure_outfile($outfile, '-got') ); | ||
| 109 | $exp_ali->store( secure_outfile($outfile, '-exp') ); | ||
| 110 | } | ||
| 111 | |||
| 112 | return; | ||
| 113 | } | ||
| 114 | |||
| 115 | |||
| 116 | sub BUILD { | ||
| 117 | my $self = shift; | ||
| 118 | |||
| 119 | my $rp = $self->run_proc; | ||
| 120 | |||
| 121 | my $ali = $self->ali; | ||
| 122 | #### [ALI] #seqs: $ali->count_seqs | ||
| 123 | unless ($ali->count_seqs) { | ||
| 124 | #### [ALI] empty file; skipping! | ||
| 125 | return; | ||
| 126 | } | ||
| 127 | |||
| 128 | for my $org ($rp->all_orgs) { | ||
| 129 | #### [ALI] Processing ORG: $org->{org} | ||
| 130 | OrgProcessor->new( ali_proc => $self, %{$org} ); | ||
| 131 | } | ||
| 132 | |||
| 133 | unless ($rp eq 'off') { | ||
| 134 | #### [ALI] Making delayed indels... | ||
| 135 | $self->integrator->make_indels; | ||
| 136 | |||
| 137 | if ($rp->round_trip_mode eq 'on') { | ||
| 138 | #### [ALI] Retranslating nucleotide sequences... | ||
| 139 | $self->_check_ali_by_translation; | ||
| 140 | } | ||
| 141 | } | ||
| 142 | |||
| 143 | # build ALI outfile honoring out_dir and out_suffix | ||
| 144 | my $cds_ali = $self->cds_ali; | ||
| 145 | my $outfile = insert_suffix($cds_ali->filename, $rp->out_suffix); | ||
| 146 | $outfile = file( $rp->out_dir, basename($outfile) ) if $rp->out_dir; | ||
| 147 | |||
| 148 | #### [ALI] Writing nucleotide file... | ||
| 149 | $cds_ali->store( secure_outfile($outfile) ); | ||
| 150 | |||
| 151 | return; | ||
| 152 | } | ||
| 153 | |||
| 154 | |||
| 155 | __PACKAGE__->meta->make_immutable; | ||
| 156 | 1; | ||
| 157 | |||
| 158 | __END__ |