| 1 | package Bio::MUST::Apps::Leel::OrgProcessor; | ||
| 2 | # ABSTRACT: Internal class for leel tool | ||
| 3 | $Bio::MUST::Apps::Leel::OrgProcessor::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 Path::Class qw(file); | ||
| 14 | use Tie::IxHash; | ||
| 15 | |||
| 16 | use Bio::MUST::Core; | ||
| 17 | use Bio::MUST::Core::Constants qw(:seqids); | ||
| 18 | use Bio::MUST::Drivers; | ||
| 19 | use aliased 'Bio::MUST::Core::Ali'; | ||
| 20 | use aliased 'Bio::MUST::Core::Ali::Temporary'; | ||
| 21 | use aliased 'Bio::MUST::Core::IdMapper'; | ||
| 22 | |||
| 23 | with 'Bio::MUST::Apps::Roles::OrgProcable'; | ||
| 24 | |||
| 25 | |||
| 26 | has 'ali_proc' => ( | ||
| 27 | is => 'ro', | ||
| 28 | isa => 'Bio::MUST::Apps::Leel::AliProcessor', | ||
| 29 | required => 1, | ||
| 30 | ); | ||
| 31 | |||
| 32 | |||
| 33 | has 'protein_seqs' => ( | ||
| 34 | is => 'ro', | ||
| 35 | isa => 'Bio::MUST::Core::Ali::Temporary', | ||
| 36 | init_arg => undef, | ||
| 37 | lazy => 1, | ||
| 38 | builder => '_build_protein_seqs', | ||
| 39 | handles => [ | ||
| 40 | qw(abbr_id_for long_id_for) | ||
| 41 | ], | ||
| 42 | ); | ||
| 43 | |||
| 44 | |||
| 45 | has 'protein_seq_mapper' => ( | ||
| 46 | is => 'ro', | ||
| 47 | isa => 'Bio::MUST::Core::IdMapper', | ||
| 48 | init_arg => undef, | ||
| 49 | lazy => 1, | ||
| 50 | builder => '_build_protein_seq_mapper', | ||
| 51 | handles => { | ||
| 52 | accession_for => 'long_id_for', | ||
| 53 | }, | ||
| 54 | ); | ||
| 55 | |||
| 56 | |||
| 57 | has $_ . '_seqs' => ( | ||
| 58 | is => 'ro', | ||
| 59 | isa => 'Bio::MUST::Core::Ali', | ||
| 60 | init_arg => undef, | ||
| 61 | lazy => 1, | ||
| 62 | builder => '_build_' . $_ . '_seqs', | ||
| 63 | ) for qw(transcript aligned); | ||
| 64 | |||
| 65 | |||
| 66 | ## no critic (ProhibitUnusedPrivateSubroutines) | ||
| 67 | |||
| 68 | sub _build_protein_seqs { | ||
| 69 | my $self = shift; | ||
| 70 | |||
| 71 | ##### [ORG] Collecting proteins... | ||
| 72 | |||
| 73 | my $ap = $self->ali_proc; | ||
| 74 | |||
| 75 | my $org = $self->org; | ||
| 76 | my $ali = $ap->ali; | ||
| 77 | |||
| 78 | my @seqs = $ali->filter_seqs( sub { $_->full_org eq $org } ); | ||
| 79 | |||
| 80 | carp "[ORG] Warning: no seq for org $org; skipping org!" | ||
| 81 | unless @seqs; | ||
| 82 | |||
| 83 | return Temporary->new( seqs => \@seqs ); | ||
| 84 | } | ||
| 85 | |||
| 86 | |||
| 87 | sub _build_protein_seq_mapper { | ||
| 88 | my $self = shift; | ||
| 89 | |||
| 90 | my @protein_seq_ids = $self->protein_seqs->all_seq_ids; | ||
| 91 | |||
| 92 | # abbr_ids are protein ids turned to query_ids for BLAST report (seq1 etc) | ||
| 93 | my @abbr_ids = map { | ||
| 94 | $self->abbr_id_for( $_->full_id ) | ||
| 95 | } @protein_seq_ids; | ||
| 96 | |||
| 97 | # TODO: put regex in common with Mick's one-on-one in BMC | ||
| 98 | |||
| 99 | # long_ids are bare accessions extracted from protein ids (Ahyp8251 etc) | ||
| 100 | # these protein accessions will be matched against transcript accessions | ||
| 101 | my @long_ids = map { | ||
| 102 | $_->accession | ||
| 103 | =~ s{ $TAIL_42 }{}xmsgr | ||
| 104 | } @protein_seq_ids; | ||
| 105 | |||
| 106 | return IdMapper->new( | ||
| 107 | long_ids => \@long_ids, | ||
| 108 | abbr_ids => \@abbr_ids, | ||
| 109 | ); | ||
| 110 | } | ||
| 111 | |||
| 112 | |||
| 113 | sub _build_transcript_seqs { | ||
| 114 | my $self = shift; | ||
| 115 | |||
| 116 | ##### [ORG] Searching for transcripts... | ||
| 117 | |||
| 118 | my $ap = $self->ali_proc; | ||
| 119 | my $rp = $ap->run_proc; | ||
| 120 | |||
| 121 | my $args = $rp->blast_args_for('transcripts') // {}; | ||
| 122 | $args->{-outfmt} = 5; | ||
| 123 | $args->{-max_target_seqs} = 5 | ||
| 124 | unless defined $args->{-max_target_seqs}; | ||
| 125 | $args->{-db_gencode} = $self->code; | ||
| 126 | |||
| 127 | my @seqs; | ||
| 128 | for my $bank ($self->all_banks) { | ||
| 129 | |||
| 130 | ###### [ORG] Processing BANK: $bank | ||
| 131 | my $file = file( $rp->bank_dir, $bank ); | ||
| 132 | my $blastdb = Bio::MUST::Drivers::Blast::Database->new( file => $file ); | ||
| 133 | my $parser = $blastdb->tblastn($self->protein_seqs, $args); | ||
| 134 | ###### [ORG] XML TBLASTN: $bank . q{ } . $parser->filename | ||
| 135 | my $bo = $parser->blast_output; | ||
| 136 | |||
| 137 | # collect transcript ids and ranges covered by proteins | ||
| 138 | # tie might help making 1331 completely deterministic | ||
| 139 | tie my %mask_for, 'Tie::IxHash'; | ||
| 140 | my %hit_for; | ||
| 141 | |||
| 142 | PROTEIN: | ||
| 143 | for my $protein ($bo->all_iterations) { | ||
| 144 | |||
| 145 | # get protein accession | ||
| 146 | my $protein_acc = $self->accession_for( $protein->query_def ); | ||
| 147 | |||
| 148 | TRANSCRIPT: | ||
| 149 | for my $transcript ($protein->all_hits) { | ||
| 150 | |||
| 151 | # get (corresponding?) transcript accession | ||
| 152 | my @fields = split /\|/xms, $transcript->id; | ||
| 153 | my $transcript_acc = pop @fields; | ||
| 154 | |||
| 155 | # check that both accessions are identical | ||
| 156 | my $rank = $transcript->num; | ||
| 157 | unless ($protein_acc eq $transcript_acc) { | ||
| 158 | ####### [ORG] ids do not match: "$protein_acc vs. $transcript_acc (hit $rank)" | ||
| 159 | |||
| 160 | # optionally try next transcript if accessions are different | ||
| 161 | next TRANSCRIPT if $rp->id_match_mode eq 'enforce'; | ||
| 162 | } | ||
| 163 | |||
| 164 | # mention success only in case of previous failure | ||
| 165 | elsif ($rank > 1) { | ||
| 166 | ####### [ORG] ids now do match: "$protein_acc (hit $rank)" | ||
| 167 | } | ||
| 168 | |||
| 169 | # collect protein id and mask for transcript | ||
| 170 | $self->_collect_hsps( | ||
| 171 | protein => $protein, # Blast::Xml::Iteration | ||
| 172 | transcript => $transcript, # Blast::Xml::Hit | ||
| 173 | mask_for => \%mask_for, | ||
| 174 | hit_for => \%hit_for, | ||
| 175 | ); | ||
| 176 | |||
| 177 | # no need to examine remaining transcripts | ||
| 178 | next PROTEIN; | ||
| 179 | } | ||
| 180 | } | ||
| 181 | $parser->remove unless $rp->debug_mode; | ||
| 182 | |||
| 183 | # fetch (and optionally trim) transcripts to their max range | ||
| 184 | my @hits = $self->_fetch_and_trim_hits( | ||
| 185 | blastdb => $blastdb, | ||
| 186 | mask_for => \%mask_for, | ||
| 187 | hit_for => \%hit_for, | ||
| 188 | ); | ||
| 189 | ######## [DEBUG] transcripts: display( map { $_->seq } @hits ) | ||
| 190 | |||
| 191 | # add transcripts from current bank to seqs | ||
| 192 | push @seqs, @hits | ||
| 193 | } | ||
| 194 | |||
| 195 | # check that all transcripts were indeed recovered | ||
| 196 | my $missing_n = $self->protein_seqs->count_seqs - @seqs; | ||
| 197 | carp "[ORG] Warning: cannot find $missing_n transcript(s):" | ||
| 198 | . ' skipping it (them)!' if $missing_n; | ||
| 199 | |||
| 200 | return Ali->new( seqs => \@seqs ); | ||
| 201 | } | ||
| 202 | |||
| 203 | |||
| 204 | # TODO: integrate directly into 42? | ||
| 205 | |||
| 206 | sub _build_aligned_seqs { | ||
| 207 | my $self = shift; | ||
| 208 | |||
| 209 | ##### [ORG] Aligning transcripts... | ||
| 210 | |||
| 211 | my $ap = $self->ali_proc; | ||
| 212 | my $rp = $ap->run_proc; | ||
| 213 | |||
| 214 | my $aligned_seqs = Ali->new(); | ||
| 215 | |||
| 216 | TRANSCRIPT: | ||
| 217 | for my $transcript_seq ( $self->transcript_seqs->all_seqs ) { | ||
| 218 | my $protein_seq = $ap->protein_for( $transcript_seq->full_id ); | ||
| 219 | |||
| 220 | # align transcript on template protein and get its translation | ||
| 221 | # Note: in exonerate the translated dna_seq is called 'target_seq' | ||
| 222 | my $exo = Bio::MUST::Drivers::Exonerate::Aligned->new( | ||
| 223 | dna_seq => $transcript_seq, | ||
| 224 | pep_seq => $protein_seq->clone->degap, | ||
| 225 | code => $self->code, | ||
| 226 | ); | ||
| 227 | |||
| 228 | # build new_seq from exonerate report | ||
| 229 | my $new_seq = $exo->target_seq; | ||
| 230 | unless ($new_seq->seq_len) { | ||
| 231 | ### exonerate failure: 'discarded' | ||
| 232 | next TRANSCRIPT; | ||
| 233 | } | ||
| 234 | |||
| 235 | # align cds_seq on protein_seq using new_seq and subject_seq as guides | ||
| 236 | $aligned_seqs->add_seq( | ||
| 237 | $ap->integrator->align( | ||
| 238 | new_seq => $new_seq, | ||
| 239 | subject => $exo->query_seq, | ||
| 240 | template => $protein_seq, | ||
| 241 | cds_seq => $exo->spliced_seq, | ||
| 242 | start => $exo->query_start, | ||
| 243 | ) | ||
| 244 | ); | ||
| 245 | } | ||
| 246 | |||
| 247 | return $aligned_seqs; | ||
| 248 | } | ||
| 249 | |||
| 250 | ## use critic | ||
| 251 | |||
| 252 | |||
| 253 | sub BUILD { | ||
| 254 | my $self = shift; | ||
| 255 | |||
| 256 | my $ap = $self->ali_proc; | ||
| 257 | my $rp = $ap->run_proc; | ||
| 258 | |||
| 259 | # TODO1: allow starting from .tax_reports (possibly post-CAP3) | ||
| 260 | # TODO2: deal with CAP3-generated ALIs... (not easy at all, hence TODO1) | ||
| 261 | |||
| 262 | ##### [ORG] proteins: display( $self->protein_seqs->all_long_ids ) | ||
| 263 | return unless $self->protein_seqs->all_long_ids; | ||
| 264 | |||
| 265 | ##### [ORG] transcripts: display( map { $_->full_id } $self->transcript_seqs->all_seq_ids ) | ||
| 266 | return unless $self->transcript_seqs->all_seq_ids; | ||
| 267 | |||
| 268 | # optionally add transcript_seqs as is (without alignment) | ||
| 269 | if ($rp->aligner_mode eq 'off') { | ||
| 270 | # TODO: handle reversed-complemented seqs | ||
| 271 | ##### [ORG] Adding raw seqs to file... | ||
| 272 | $ap->cds_ali->add_seq( $self->transcript_seqs->all_seqs ); | ||
| 273 | return; | ||
| 274 | } | ||
| 275 | |||
| 276 | ##### [ORG] aligned: display( map { $_->full_id } $self->aligned_seqs->all_seq_ids ) | ||
| 277 | return unless $self->aligned_seqs->all_seq_ids; | ||
| 278 | |||
| 279 | ##### [ORG] Adding aligned seqs to file... | ||
| 280 | $ap->cds_ali->add_seq( $self->aligned_seqs->all_seqs ); | ||
| 281 | |||
| 282 | return; | ||
| 283 | } | ||
| 284 | |||
| 285 | |||
| 286 | __PACKAGE__->meta->make_immutable; | ||
| 287 | 1; | ||
| 288 | |||
| 289 | __END__ |