lib/Bio/MUST/Apps/FortyTwo/AliProcessor.pm

filter: Statementstmt 27.1% 87 / 320 Branchbran 0.0% 0 / 68 Conditioncond 0.0% 0 / 36 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 47.0% 24 / 51 Podpod 0.0% 0 / 7 totaltotal 22.1% 111 / 502 CC 63 SCAR 39.9
CC
63
Cov
21%
check_brh_among_best_hits
132.0
BUILD
132.0
_build_best_hits_by_ref_org
110.0
CRAP 54.2
« lib/Bio/MUST/Apps/Roles/OrgProcable.pm ↑ Summary lib/Bio/MUST/Apps/SlaveAligner/Local.pm »
1 package Bio::MUST::Apps::FortyTwo::AliProcessor;
2 # ABSTRACT: Internal class for forty-two tool
3 1 $Bio::MUST::Apps::FortyTwo::AliProcessor::VERSION = '0.262780';
4 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use feature qw(say);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use Smart::Comments -ENV;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Const::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use List::AllUtils qw(first part partition_by pairmap);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Path::Class qw(file);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Sort::Naturally;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Tie::IxHash;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 ▶use Bio::MUST::Core;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use Bio::MUST::Core::Utils qw(:filenames secure_outfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use Bio::MUST::Drivers;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use aliased 'Bio::MUST::Core::Ali';
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use aliased 'Bio::MUST::Core::Ali::Temporary';
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use aliased 'Bio::MUST::Core::IdList';
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use aliased 'Bio::MUST::Core::Seq';
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use aliased 'Bio::MUST::Core::SeqId';
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use aliased 'Bio::MUST::Apps::SlaveAligner::Local';
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use aliased 'Bio::MUST::Apps::FortyTwo::OrgProcessor';
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 ▶use aliased 'Bio::MUST::Apps::Debrief42::TaxReport::NewSeq';
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 1 with 'Bio::MUST::Apps::Roles::AliProcable';
34  
35  
36 1 has 'run_proc' => (
37 is => 'ro',
38 isa => 'Bio::MUST::Apps::FortyTwo::RunProcessor',
39 required => 1,
40 );
41  
42  
43 1 has '+ali' => (
44 handles => [ qw(all_new_seqs all_but_new_seqs) ],
45 );
46  
47  
48 1 has 'lookup' => (
49 is => 'ro',
50 isa => 'Bio::MUST::Core::IdList',
51 init_arg => undef,
52 lazy => 1,
53 builder => '_build_lookup',
54 );
55  
56  
57 1 has 'blastdb' => (
58 is => 'ro',
59 isa => 'Bio::MUST::Drivers::Blast::Database::Temporary',
60 init_arg => undef,
61 lazy => 1,
62 builder => '_build_blastdb',
63 );
64  
65  
66 1 has 'para_blastdb' => (
67 is => 'ro',
68 isa => 'Maybe[Bio::MUST::Drivers::Blast::Database::Temporary]',
69 init_arg => undef,
70 lazy => 1,
71 builder => '_build_para_blastdb',
72 );
73  
74  
75 has 'tax_report' => (
76 traits => ['Hash'],
77 is => 'ro',
78 isa => 'HashRef[Bio::MUST::Apps::Debrief42::TaxReport::NewSeq]',
79 init_arg => undef,
80 0 ▶ default => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
81 1 handles => {
82 tax_line_for => 'get',
83 set_tax_line => 'set',
84 all_tax_line_ids => 'keys',
85 },
86 );
87  
88  
89 has '_' . $_ . '_seqs_by_org' => (
90 traits => ['Hash'],
91 is => 'ro',
92 isa => 'HashRef[ArrayRef[Bio::MUST::Core::Seq]]',
93 init_arg => undef,
94 lazy => 1,
95 builder => '_build_' . $_ . '_seqs_by_org',
96 handles => {
97 'count_' . $_ . '_orgs' => 'count',
98 'all_' . $_ . '_orgs' => 'keys',
99 $_ . '_seqs_for' => 'get',
100 },
101 1 ) for qw(ali non new);
102  
103  
104 1 has 'query_seqs' => (
105 is => 'ro',
106 isa => 'Bio::MUST::Core::Ali::Temporary',
107 init_arg => undef,
108 lazy => 1,
109 builder => '_build_query_seqs',
110 );
111  
112  
113 1 has '_best_hits_by_ref_org' => (
114 traits => ['Hash'],
115 is => 'ro',
116 isa => 'HashRef[Bio::MUST::Core::IdList]',
117 init_arg => undef,
118 lazy => 1,
119 builder => '_build_best_hits_by_ref_org',
120 handles => {
121 count_ref_orgs => 'count',
122 all_ref_orgs => 'keys',
123 remove_ref_org => 'delete',
124 all_best_hits => 'values',
125 best_hits_for => 'get',
126 },
127 );
128  
129  
130 has '_seqs_for_removal' => (
131 is => 'ro',
132 isa => 'Bio::MUST::Core::IdList',
133 init_arg => undef,
134 0 ▶ default => sub { IdList->new() },
Subroutine __ANON__: not called CC=1 SCAR=6.9
135 1 handles => {
136 marked_for_removal => 'is_listed',
137 mark_seq_for_removal => 'add_id',
138 all_marked_seqs => 'all_ids',
139 remove_marked_seqs => 'negative_list',
140 },
141 );
142  
143  
144 ## no critic (ProhibitUnusedPrivateSubroutines)
145  
146 0 ▶sub _build_integrator {
Subroutine _build_integrator: not called CC=1 SCAR=6.9
147 0 return Local->new( ali => shift->ali );
148 }
149  
150  
151 0 ▶sub _build_lookup {
Subroutine _build_lookup: not called CC=1 SCAR=6.9
152 0 return shift->ali->new_lookup;
153 }
154  
155  
156 0 ▶sub _build_blastdb {
Subroutine _build_blastdb: not called CC=1 SCAR=6.9
157 0 return Bio::MUST::Drivers::Blast::Database::Temporary->new(
158 seqs => [ shift->all_but_new_seqs ]
159 );
160 }
161  
162  
163 0 ▶sub _build_para_blastdb {
Subroutine _build_para_blastdb: not called CC=2 SCAR=17.9
164 0 my $self = shift;
165  
166 0 my $parafile = change_suffix($self->ali->filename, '.para');
167 0 ▶ return unless -e $parafile;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $parafile 0 0 0
168  
169 # TODO: handle empty PARA files (but existing)
170 # pour le bug dans forty-two... si fichier para vide mais avec des :
171 # # built by filtered_ali
172 # # built by filtered_ali
173 # # built by filtered_ali
174 # ça crashe sur un makeblastdb
175  
176 #### [ALI] PARA file in use: $parafile
177 0 return Bio::MUST::Drivers::Blast::Database::Temporary->new(
178 seqs => $parafile
179 );
180 }
181  
182  
183 0 ▶sub _build_new_seqs_by_org {
Subroutine _build_new_seqs_by_org: not called CC=1 SCAR=6.9
184 0 my $self = shift;
185 0 return $self->_split_seqs_by_org(
186 Ali->new( seqs => [ $self->all_new_seqs ] )
187 );
188 }
189  
190  
191 0 ▶sub _build_ali_seqs_by_org {
Subroutine _build_ali_seqs_by_org: not called CC=1 SCAR=6.9
192 0 my $self = shift;
193 0 return $self->_split_seqs_by_org(
194 Ali->new( seqs => [ $self->all_but_new_seqs ] )
195 );
196 }
197  
198  
199 0 ▶sub _build_non_seqs_by_org {
Subroutine _build_non_seqs_by_org: not called CC=2 SCAR=17.9
200 0 my $self = shift;
201  
202 0 my $nonfile = change_suffix( $self->ali->filename, '.non' );
203 0 ▶ return {} unless -e $nonfile;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $nonfile 0 0 0
204  
205 #### [ALI] NON file in use: $nonfile
206 0 return $self->_split_seqs_by_org( Ali->load($nonfile) );
207 }
208  
209  
210 1 const my $DEF_ORG => ':default';
211  
212 0 ▶sub _split_seqs_by_org {
Subroutine _split_seqs_by_org: not called CC=4 SCAR=30.0
213 0 my $self = shift;
214 0 my $seqs = shift; # actually an Ali object...
215  
216 # split Seqs into one or more ArrayRefs keyed by full_org
217 # tie probably useless here but ensuring reproducible logs
218 0 tie my %seqs_for, 'Tie::IxHash';
219 0 for my $seq ($seqs->all_seqs) {
220 0 ▶ my $genus = $seq->genus // q{};
Condition0%0/2
$seq->genus // ""
l!l
00
MC/DC0%0/1
$seq->genus // ""
$seq->genus
Truth table0%0/2
A
A= $seq->genus
Aresult
11
00
221 0 ▶ my $org = $seq->full_org // $DEF_ORG;
Condition0%0/3
$seq->full_org // $DEF_ORG
l!l&&r!l&&!r
000
MC/DC0%0/2
$seq->full_org // $DEF_ORG
$seq->full_org $DEF_ORG
Truth table0%0/3
A // B
A= $seq->full_orgB= $DEF_ORG
ABresult
1X1
011
000
222 0 push @{ $seqs_for{$org} }, $seq;
223 }
224  
225 0 return \%seqs_for;
226 }
227  
228  
229 0 ▶sub _build_query_seqs {
Subroutine _build_query_seqs: not called CC=3 SCAR=24.8
230 0 my $self = shift;
231  
232 #### [ALI] Collecting queries...
233  
234 0 my $rp = $self->run_proc;
235  
236 my @seqs # filter out doubtful seqs from each query_org
237 0 = grep { not $_->is_doubtful }
238 0 ▶ map { @{ $self->ali_seqs_for($_) // [] } }
Condition0%0/2
$self->ali_seqs_for($_) // []
l!l
00
MC/DC0%0/1
$self->ali_seqs_for($_) // []
$self->ali_seqs_for($_)
Truth table0%0/2
A
A= $self->ali_seqs_for($_)
Aresult
11
00
239 $rp->all_query_orgs
240 ;
241  
242 0 ▶ unless (@seqs) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@seqs) 0 0 0
243 0 carp '[ALI] Warning: no seq for any query_org; using longest instead!';
244 0 @seqs = ( $self->get_longest_query_seq );
245 }
246  
247 0 return Temporary->new( seqs => \@seqs );
248 }
249  
250  
251 0 ▶sub _build_best_hits_by_ref_org {
Subroutine _build_best_hits_by_ref_org: not called CC=10 SCAR=47.0
252 0 my $self = shift;
253  
254 #### [ALI] Identifying best hits for queries in reference orgs...
255  
256 0 my $rp = $self->run_proc;
257 0 ▶ my $args = $rp->blast_args_for('references') // {};
Condition0%0/2
$rp->blast_args_for("references") // {}
l!l
00
MC/DC0%0/1
$rp->blast_args_for("references") // {}
$rp->blast_args_for("references")
Truth table0%0/2
A
A= $rp->blast_args_for("references")
Aresult
11
00
258 0 $args->{-outfmt} = 6;
259 $args->{-max_target_seqs} = 10
260 0 ▶ unless defined $args->{-max_target_seqs};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"-max_target_seqs"} 0 0 0
261  
262 # tie might help making 42 completely deterministic
263 0 tie my %best_hits_for, 'Tie::IxHash';
264 0 tie my %avg_score_for, 'Tie::IxHash';
265  
266 ORG:
267 0 for my $ref_org ($rp->all_ref_orgs) {
268  
269 0 my $query_seqs = $self->query_seqs;
270 0 my $blastdb = $rp->ref_blastdb_for($ref_org);
271 0 my $parser = $blastdb->blastp($query_seqs, $args);
272 #### [ALI] BLASTP: $ref_org . q{ } . $parser->filename
273  
274 # collect best hit(s) for each query against ref proteome
275 0 my $curr_query = q{};
276 0 my $seen_qry_n = 0;
277 0 my $seen_hit_n = 0;
278 0 my $best_score = 0;
279 0 my $cmul_score = 0;
280  
281 # tie might help making 42 completely deterministic
282 0 tie my %hits, 'Tie::IxHash';
283 0 ▶ while ( my $hsp = $parser->next_hit ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $hsp = $parser->next_hit) 0 0 0
284  
285 # fetch query and bitscore for current hit
286 0 my $query = $hsp->query_id;
287 0 my $score = $hsp->bit_score;
288  
289 # reset score if next query
290 0 ▶ if ($query ne $curr_query) {
Branch: 1 branch
BranchTrueFalseTotal
if ($query ne $curr_query) 0 0 0
291 0 $seen_qry_n++;
292 0 $curr_query = $query;
293 0 $best_score = 0;
294 }
295  
296 # include hit if within score tolerance
297 # Note: for consistency with coverage_mul the new lower score
298 # becomes the new 'best_score' (increasing tolerance thus)
299 0 ▶ if ($score >= $best_score * $rp->ref_score_mul) {
Branch: 1 branch
BranchTrueFalseTotal
if ($score >= $best_score * $rp->ref_score_mul) 0 0 0
300 0 my $hit = $hsp->hit_id;
301 ######## [DEBUG] query: $query_seqs->long_id_for($query)
302 ######## [DEBUG] hit: $hit
303 ######## [DEBUG] score: $score
304 0 $hits{$hit}++;
305 0 $seen_hit_n++;
306 0 $best_score = $score;
307 0 $cmul_score += $score;
308 }
309 }
310  
311 0 ▶ $parser->remove unless $rp->debug_mode;
Branch: 1 branch
BranchTrueFalseTotal
unless $rp->debug_mode 0 0 0
312  
313 # skip ref_orgs without best hits
314 0 ▶ unless ($cmul_score) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cmul_score) 0 0 0
315 0 carp "[ALI] Note: no best hit for: $ref_org;"
316 . ' removing it from ref_orgs.';
317 0 next ORG;
318 }
319  
320 # store average score for best hits in current ref proteome
321 # Note: divisor is set to number of best hits (plus unseen queries)
322 0 my $divisor = $seen_hit_n + $query_seqs->count_seqs - $seen_qry_n;
323 0 my $avg_score = $cmul_score / $divisor;
324 0 $avg_score_for{$ref_org} = $avg_score;
325  
326 # store best hit list for current ref proteome
327 0 my $best_hits = IdList->new( ids => [ keys %hits ] );
328 0 $best_hits_for{$ref_org} = $best_hits;
329 }
330 ######## [DEBUG] avg scores: display( pairmap { "$a => $b" } %avg_score_for )
331  
332 0 my $ref_org_n = ceil( $rp->ref_org_mul * $rp->all_ref_orgs );
333 0 my @ref_orgs = keys %avg_score_for;
334 0 ▶ if (@ref_orgs > $ref_org_n) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ref_orgs > $ref_org_n) 0 0 0
335  
336 #### [ALI] Discarding non-best ref_orgs and keeping only: $ref_org_n
337 @ref_orgs = sort {
338 0 $avg_score_for{$b} <=> $avg_score_for{$a}
339 } @ref_orgs;
340 0 @ref_orgs = @ref_orgs[ 0 .. $ref_org_n - 1 ];
341  
342 0 %best_hits_for = map { $_ => $best_hits_for{$_} } @ref_orgs;
343 }
344  
345 0 return \%best_hits_for;
346 }
347  
348 ## use critic
349  
350  
351 0 ▶sub get_longest_query_seq {
Subroutine get_longest_query_seq: not called CC=5 SCAR=34.0
Pod undocumented
352 0 my $self = shift;
353  
354 0 my $max_seq;
355 0 my $max_len = 0;
356  
357 # loop through potential queries and select longest seq
358 0 for my $org ($self->all_ali_orgs) {
359  
360 SEQ:
361 0 for my $seq ( @{ $self->ali_seqs_for($org) } ) {
362  
363 # filter out doubtful seqs from each query_org
364 0 ▶ next SEQ if $seq->is_doubtful;
Branch: 1 branch
BranchTrueFalseTotal
if $seq->is_doubtful 0 0 0
365  
366 # find longest seq using a manual yet fast approach
367 0 my $len = $seq->nomiss_seq_len;
368 0 ▶ if ($len > $max_len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len > $max_len) 0 0 0
369 0 $max_seq = $seq;
370 0 $max_len = $len;
371 }
372 }
373 }
374  
375 0 return $max_seq;
376 }
377  
378  
379 0 ▶sub check_brh_among_best_hits {
Subroutine check_brh_among_best_hits: not called CC=11 SCAR=48.8
Pod undocumented
380 0 my $self = shift;
381  
382 #### [ALI] Checking BRH among best hits in reference orgs...
383 0 ▶ if ($self->count_ref_orgs < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->count_ref_orgs < 2) 0 0 0
384 0 carp '[ALI] Warning: not enough ref_orgs for checking BRH'
385 . ' among best hits!';
386 0 return;
387 }
388  
389 0 my $rp = $self->run_proc;
390 0 ▶ my $args = $rp->blast_args_for('references') // {};
Condition0%0/2
$rp->blast_args_for("references") // {}
l!l
00
MC/DC0%0/1
$rp->blast_args_for("references") // {}
$rp->blast_args_for("references")
Truth table0%0/2
A
A= $rp->blast_args_for("references")
Aresult
11
00
391 0 $args->{-outfmt} = 6;
392 0 $args->{-max_target_seqs} = 1;
393  
394 0 for my $ref_org1 ($self->all_ref_orgs) {
395  
396 # build query_seqs from best hits for current ref_org
397 0 my $query_seqs = Temporary->new(
398 seqs => $rp->ref_blastdb_for($ref_org1)->blastdbcmd(
399 [ $self->best_hits_for($ref_org1)->all_ids ]
400 )->seqs # TODO: improve this through coercion
401 );
402  
403 0 tie my %count_for, 'Tie::IxHash';
404  
405 ORG2:
406 0 for my $ref_org2 ($self->all_ref_orgs) {
407 0 ▶ next ORG2 if $ref_org2 eq $ref_org1;
Branch: 1 branch
BranchTrueFalseTotal
if $ref_org2 eq $ref_org1 0 0 0
408  
409 0 my $blastdb = $rp->ref_blastdb_for($ref_org2);
410 0 my $parser = $blastdb->blastp($query_seqs, $args);
411 #### [ALI] BLASTP: "$ref_org1 vs $ref_org2 " . $parser->filename
412  
413 0 my $best_hits = $self->best_hits_for($ref_org2);
414 0 ▶ while ( my $hsp = $parser->next_query ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $hsp = $parser->next_query) 0 0 0
415 0 my $candidate = $query_seqs->long_id_for( $hsp->query_id );
416 0 my $in_best_hits = $best_hits->is_listed( $hsp->hit_id );
417 ######## [DEBUG]: $candidate . ( $in_best_hits && ' [=BRH=]' )
418 0 ▶ $count_for{$candidate}++ if $in_best_hits;
Branch: 1 branch
BranchTrueFalseTotal
if $in_best_hits 0 0 0
419 }
420 0 ▶ $parser->remove unless $rp->debug_mode;
Branch: 1 branch
BranchTrueFalseTotal
unless $rp->debug_mode 0 0 0
421 }
422  
423 ######## [DEBUG] BRH counts: display( pairmap { "$a => $b" } %count_for )
424  
425 # remove ref_orgs that completely failed BRH
426 # Note: this should not happen too often because of ref_org_mul
427 0 ▶ unless (keys %count_for) {
Branch: 1 branch
BranchTrueFalseTotal
unless (keys %count_for) 0 0 0
428 0 carp "[ALI] Note: failed BRH for: $ref_org1;"
429 . ' removing it from ref_orgs.';
430 0 $self->remove_ref_org($ref_org1);
431 # Note: the remaining ref_orgs (as 1) will not be tested vs. this
432 # ref_org (as 2) from now on; thus early Warnings only due to this
433 # ref_org (as 2) could have been avoided
434 }
435  
436 # check BRH with all *other* ref_orgs (hence - 1)
437 0 my $other_n = $self->all_ref_orgs - 1;
438 carp '[ALI] Warning: best hits not in BRH across all ref_orgs!'
439 0 ▶ unless List::AllUtils::all {
Subroutine __ANON__: not called CC=1 SCAR=6.9
440 0 $count_for{$_} == $other_n
441 0 ▶ } keys %count_for
Branch: 1 branch
BranchTrueFalseTotal
unless &List::AllUtils::all(sub { $count_for{$_} == $other_n; } , keys %count_for) 0 0 0
442 ;
443 }
444  
445 0 return;
446 }
447  
448  
449 0 ▶sub merge_chunks {
Subroutine merge_chunks: not called CC=9 SCAR=45.0
Pod undocumented
450 0 my $self = shift;
451  
452 # fetch all new seqs having a 42 tail
453 # those are the aligned orthologous transcripts
454 0 my $tail_regex = qr{ (:? \.H\d+\.\d+ | \.E\.bf | \.E\.lc ) \b }xms;
455 0 my @new_seqs = grep { $_->full_id =~ $tail_regex } $self->all_new_seqs;
456  
457 # leave early if no new seqs
458 0 ▶ return unless @new_seqs;
Branch: 1 branch
BranchTrueFalseTotal
unless @new_seqs 0 0 0
459  
460 # partition transcripts by full_id ignoring 42 tails
461 0 ▶ my %new_seqs_by_acc = partition_by {
Subroutine __ANON__: not called CC=1 SCAR=6.9
462 0 ( my $full_id = $_->full_id ) =~ s{$tail_regex}{}; $full_id
463 0 } @new_seqs;
464  
465 # separate single-chunk transcripts from multi-chunk transcripts
466 0 ▶ my ($singles, $multiples) = part {
Subroutine __ANON__: not called CC=1 SCAR=6.9
467 0 @{ $new_seqs_by_acc{$_} } > 1
468 0 } sort keys %new_seqs_by_acc;
469 ####### [ALI] multi-chunk transcripts: display( @{$multiples} )
470  
471 # simply remove 42 tails from single-chunk transcripts
472 # Note: ->[0] is required because values are singleton ARRAYREFs
473 $new_seqs_by_acc{$_}->[0]->set_seq_id( SeqId->new( full_id => $_ ) )
474 0 ▶ for @{ $singles // [] };
Condition0%0/2
$singles // []
l!l
00
MC/DC0%0/1
$singles // []
$singles
Truth table0%0/2
A
A= $singles
Aresult
11
00
475  
476 # leave early if no multi-chunk transcripts
477 0 ▶ return unless @{ $multiples // [] };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$multiples // [];} 0 0 0
Condition0%0/2
$multiples // []
l!l
00
MC/DC0%0/1
$multiples // []
$multiples
Truth table0%0/2
A
A= $multiples
Aresult
11
00
478  
479 # get alignment width and regex
480 0 my $width = $self->ali->width;
481 0 my $gap_regex = $self->ali->gapmiss_regex;
482  
483 # merge chunks for each transcript
484 0 for my $merged_id ( @{$multiples} ) {
485  
486 # order transcript chunks following BLAST hit and HSP ranks
487 # this will ensure that best-scoring hits/HSPs come first
488 my @chunks = sort {
489 0 ncmp( $a->full_id, $b->full_id )
490 0 } @{ $new_seqs_by_acc{$merged_id} };
491  
492 0 my $merged_seq;
493 0 my $gap = q{ };
494  
495 # take each state from first chunk where it is not missing/gap
496 # insert '*' if no chunk can provide it
497 # Note: we insert a blank at first site to trigger Seq->spacify
498 0 for my $site (0..$width-1) {
499 0 ▶ my $chunk = first { $_->state_at($site) !~ $gap_regex } @chunks;
Subroutine __ANON__: not called CC=1 SCAR=6.9
500 0 ▶ $merged_seq .= $chunk ? $chunk->state_at($site) : $gap;
Branch: 1 branch
BranchTrueFalseTotal
$chunk ? : 0 0 0
501 0 $gap = '*';
502 }
503  
504 # add merged_seq
505 $self->ali->add_seq(
506 0 Seq->new(
507 seq_id => $merged_id,
508 seq => $merged_seq
509 )
510 );
511  
512 # mark chunks for removal
513 0 $self->mark_seq_for_removal( map { $_->full_id } @chunks );
514 }
515  
516 0 return;
517 }
518  
519  
520 0 ▶sub mark_redundant_seqs {
Subroutine mark_redundant_seqs: not called CC=9 SCAR=45.0
Pod undocumented
521 0 my $self = shift;
522  
523 # fetch all new seqs not yet marked for removal
524 my @new_seqs = grep {
525 0 !$self->marked_for_removal( $_->full_id )
526 } $self->all_new_seqs;
527  
528 # examine each new_seq for redundancy etc
529 0 for my $seq (@new_seqs) {
530  
531 0 my $id = $seq->full_id;
532 0 my $org = $seq->full_org;
533  
534 # remove seq if included in Ali for the org...
535 0 ▶ if ( List::AllUtils::any { $seq->is_subseq_of($_) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 3 branches
BranchTrueFalseTotal
if (&List::AllUtils::any(sub { $seq->is_subseq_of($_); } , @{$self->ali_seqs_for($org) // [];})) { } 0 0 0
elsif (&List::AllUtils::any(sub { $seq->is_subseq_of($_); } , @{$self->non_seqs_for($org) // [];})) { } 0 0 0
elsif (&List::AllUtils::any(sub { $seq->is_subseq_of($_); } , grep({$id ne $_->full_id and not $self->marked_for_removal($_->full_id);} @{$self->new_seqs_for($org) // [];}))) { } 0 0 0
536 0 ▶ @{ $self->ali_seqs_for($org) // [] } ) {
Condition0%0/2
$self->ali_seqs_for($org) // []
l!l
00
MC/DC0%0/1
$self->ali_seqs_for($org) // []
$self->ali_seqs_for($org)
Truth table0%0/2
A
A= $self->ali_seqs_for($org)
Aresult
11
00
537 ####### [ALI] removed for redundancy within ALI: $id
538 0 $self->mark_seq_for_removal($id);
539 }
540 # ... or included in .non file for the org...
541 0 ▶ elsif (List::AllUtils::any { $seq->is_subseq_of($_) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
542 0 ▶ @{ $self->non_seqs_for($org) // [] } ) {
Condition0%0/2
$self->non_seqs_for($org) // []
l!l
00
MC/DC0%0/1
$self->non_seqs_for($org) // []
$self->non_seqs_for($org)
Truth table0%0/2
A
A= $self->non_seqs_for($org)
Aresult
11
00
543 ####### [ALI] removed for inclusion in NON file: $id
544 0 $self->mark_seq_for_removal($id);
545 }
546 # ... or included in new_seqs for the org
547 # but not seq itself and not if already marked for removal
548 # to avoid both auto and mutual removal of identical new_seqs
549 0 ▶ elsif (List::AllUtils::any { $seq->is_subseq_of($_) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
550 0 ▶ grep { $id ne $_->full_id
Condition0%0/3
$id ne $_->full_id && !$self->marked_for_removal($_->full_id)
!ll&&!rl&&r
000
MC/DC0%0/2
$id ne $_->full_id && !$self->marked_for_removal($_->full_id)
$id ne $_->full_id !$self->marked_for_removal($_->full_id)
Truth table0%0/3
A && not(B)
A= $id ne $_->full_idB= !$self->marked_for_removal($_->full_id)
ABresult
0X0
100
111
551 && !$self->marked_for_removal( $_->full_id ) }
552 0 ▶ @{ $self->new_seqs_for($org) // [] } ) {
Condition0%0/2
$self->new_seqs_for($org) // []
l!l
00
MC/DC0%0/1
$self->new_seqs_for($org) // []
$self->new_seqs_for($org)
Truth table0%0/2
A
A= $self->new_seqs_for($org)
Aresult
11
00
553 ####### [ALI] removed for redundancy with other #NEW# seqs: $id
554 0 $self->mark_seq_for_removal($id);
555 } # Note: it is important that marked_seqs... is constantly updated
556 }
557  
558 0 return;
559 }
560  
561  
562 0 ▶sub mark_lengthened_seqs {
Subroutine mark_lengthened_seqs: not called CC=4 SCAR=30.0
Pod undocumented
563 0 my $self = shift;
564  
565 # fetch all new seqs not yet marked for removal
566 my @new_seqs = grep {
567 0 !$self->marked_for_removal( $_->full_id )
568 } $self->all_new_seqs;
569  
570 # examine each new_seq for lengthening
571 0 for my $seq (@new_seqs) {
572 0 my $org = $seq->full_org;
573  
574 # identify pre-existing seqs included in a new_seq...
575 0 my @redundants = map { $_->full_id } # opposite to above...
576 0 grep { $_->is_subseq_of($seq) }
577 0 grep { !$self->marked_for_removal( $_->full_id ) }
578 0 ▶ @{ $self->ali_seqs_for($org) // [] }
Condition0%0/2
$self->ali_seqs_for($org) // []
l!l
00
MC/DC0%0/1
$self->ali_seqs_for($org) // []
$self->ali_seqs_for($org)
Truth table0%0/2
A
A= $self->ali_seqs_for($org)
Aresult
11
00
579 ; # Note: it is important that marked_seqs... is constantly updated
580  
581 # ... and remove them
582 # Note: not sure that more than one per new_seq would be very common
583 0 ▶ if (@redundants) {
Branch: 1 branch
BranchTrueFalseTotal
if (@redundants) 0 0 0
584 ####### [ALI] removed for redundancy by lengthening: join ', ', @redundants
585 0 $self->mark_seq_for_removal(@redundants);
586 }
587 }
588  
589 0 return;
590 }
591  
592  
593 0 ▶sub reorder_new_seqs {
Subroutine reorder_new_seqs: not called CC=4 SCAR=30.0
Pod undocumented
594 0 my $self = shift;
595  
596 0 my $ali = $self->ali;
597  
598 # partition Ali between old and new_seqs
599 0 ▶ my ($old_seqs, $new_seqs) = part { $_->is_new } $ali->all_seqs;
Subroutine __ANON__: not called CC=1 SCAR=6.9
600  
601 # reorder new_seqs naturally on family/full_org/accession
602 # Note: this should ignore the c# tag during sorting
603 0 ▶ $ali->_set_seqs( [ @{ $old_seqs // [] }, sort {
Condition0%0/2
$old_seqs // []
l!l
00
MC/DC0%0/1
$old_seqs // []
$old_seqs
Truth table0%0/2
A
A= $old_seqs
Aresult
11
00
604 0 ▶ ncmp( $a->family_then_full_org, $b->family_then_full_org )
Condition0%0/3
ncmp($a->family_then_full_org, $b->family_then_full_org) || ncmp($a->accession, $b->accession)
l!l&&r!l&&!r
000
MC/DC0%0/2
ncmp($a->family_then_full_org, $b->family_then_full_org) || ncmp($a->accession, $b->accession)
ncmp($a->family_then_full_org, $b->family_then_full_org) ncmp($a->accession, $b->accession)
Truth table0%0/3
A || B
A= ncmp($a->family_then_full_org, $b->family_then_full_org)B= ncmp($a->accession, $b->accession)
ABresult
1X1
011
000
605 ||
606 ncmp( $a->accession, $b->accession )
607 0 ▶ } @{ $new_seqs // [] } ] );
Condition0%0/2
$new_seqs // []
l!l
00
MC/DC0%0/1
$new_seqs // []
$new_seqs
Truth table0%0/2
A
A= $new_seqs
Aresult
11
00
608  
609 0 return;
610 }
611  
612  
613 0 ▶sub BUILD {
Subroutine BUILD: not called CC=11 SCAR=48.8
Pod undocumented
614 0 my $self = shift;
615  
616 0 my $rp = $self->run_proc;
617  
618 0 my $ali = $self->ali;
619 #### [ALI] #seqs: $ali->count_seqs
620  
621 0 ▶ unless ( $ali->count_seqs ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ali->count_seqs) 0 0 0
622 #### [ALI] empty file; skipping!
623 0 return;
624 }
625  
626 # default to clearing new tags from Ali
627 $ali->clear_new_tags
628 0 ▶ unless $rp->ali_keep_old_new_tags eq 'on';
Branch: 1 branch
BranchTrueFalseTotal
unless $rp->ali_keep_old_new_tags eq "on" 0 0 0
629  
630 # check for optional use of .non and .para
631 # Note: lazy builders do not require it but this makes a better log
632 0 $self->count_non_orgs;
633 0 $self->para_blastdb;
634  
635 #### [ALI] queries: display( $self->query_seqs->all_long_ids )
636  
637 0 ▶ if ($rp->ref_brh eq 'on') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rp->ref_brh eq "on") 0 0 0
638  
639 #### [ALI] reference orgs: display( $self->all_ref_orgs )
640  
641 #### [ALI] best hits: display( map { $_->all_ids } $self->all_best_hits )
642  
643 0 $self->check_brh_among_best_hits;
644 # TODO: abort if no ref_org at all!
645 }
646  
647 0 for my $org ($rp->all_orgs) {
648  
649 #### [ALI] Processing ORG: $org->{org}
650 0 OrgProcessor->new( ali_proc => $self, %{$org} );
651 }
652  
653 # build ALI outfile honoring out_dir and out_suffix
654 0 my $outfile = insert_suffix($ali->filename, $rp->out_suffix);
655 0 ▶ $outfile = file( $rp->out_dir, basename($outfile) ) if $rp->out_dir;
Branch: 1 branch
BranchTrueFalseTotal
if $rp->out_dir 0 0 0
656  
657 0 ▶ unless ( $rp->run_mode eq 'metagenomic' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rp->run_mode eq "metagenomic") 0 0 0
658  
659 #### [ALI] Making delayed indels...
660 0 $self->integrator->make_indels;
661  
662 # TODO: make this optional? as this can introduce micro-indels...
663 #### [ALI] Merging sequence chunks...
664 0 $self->merge_chunks;
665  
666 #### [ALI] Removing redundant (and unwanted) seqs...
667 0 $self->mark_redundant_seqs;
668  
669 # TODO: write tests for this
670 0 ▶ if ($rp->ali_keep_lengthened_seqs eq 'off') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rp->ali_keep_lengthened_seqs eq "off") 0 0 0
671 #### [ALI] Removing lengthened seqs...
672 0 $self->mark_lengthened_seqs;
673 }
674  
675 #### [ALI] merged/redundant/unwanted/lengthened: display( $self->all_marked_seqs )
676 0 $ali->apply_list( $self->remove_marked_seqs($ali) );
677  
678 #### [ALI] Re-ordering remaining aligned seqs...
679 0 $self->reorder_new_seqs;
680  
681 #### [ALI] Writing updated file...
682 0 $ali->store( secure_outfile($outfile) ); # note the secure...
683 }
684  
685 0 ▶ if ($rp->tax_reports eq 'on') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rp->tax_reports eq "on") 0 0 0
686 # TODO: consider moving this in TaxReport-like class
687  
688 #### [ALI] Writing taxonomic report...
689 0 my $tax_report = secure_outfile( # note the secure...
690 change_suffix($outfile, '.tax-report')
691 );
692  
693 0 open my $fh, '>', $tax_report;
694 0 say {$fh} '# ' . join "\t", NewSeq->heads;
695  
696 # print tax_lines for all new_seqs
697 my @new_seqs = $rp->run_mode eq 'phylogenomic'
698 0 ▶ ? map { $_->full_id } $self->all_new_seqs # phylogenomic mode
Branch: 1 branch
BranchTrueFalseTotal
$rp->run_mode eq 'phylogenomic' ? : 0 0 0
699 : nsort( $self->all_tax_line_ids ) # metagenomic mode
700 ;
701 0 say {$fh} join "\n", map { $_->stringify }
702 0 ▶ map { $self->tax_line_for($_) // () } @new_seqs;
Condition0%0/3
$self->tax_line_for($_) // ()
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->tax_line_for($_) // ()
$self->tax_line_for($_) ()
Truth table0%0/3
A // B
A= $self->tax_line_for($_)B= ()
ABresult
1X1
011
000
703 } # Note: skip preexisting new_seqs for which there are no tax_lines
704  
705 #### [ALI] Done analyzing file...
706 0 return;
707 }
708  
709  
710 1 __PACKAGE__->meta->make_immutable;
711 1 1;
712  
713 __END__
« lib/Bio/MUST/Apps/Roles/OrgProcable.pm ↑ Summary lib/Bio/MUST/Apps/SlaveAligner/Local.pm »