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

filter: Statementstmt 27.1% 87 / 320 Branchbran 0.0% 0 / 72 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 47.0% 24 / 51 Podpod 0.0% 0 / 7 totaltotal 22.3% 111 / 496 SCAR 76.2
CC
63
Cov
21%
check_brh_among_best_hits
132.0
BUILD
132.0
_build_best_hits_by_ref_org
110.0
CRAP 2048.5
« lib/Bio/MUST/Apps/FortyTwo/OrgProcessor.pm ↑ Summary script/yaml-generator-42.pl »
1 package Bio::MUST::Apps::FortyTwo::AliProcessor;
2 # ABSTRACT: Internal class for forty-two tool
3 1 $Bio::MUST::Apps::FortyTwo::AliProcessor::VERSION = '0.210570';
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 sub _build_integrator {
147 0 ▶ return Local->new( ali => shift->ali );
Subroutine _build_integrator: not called CC=1 SCAR=6.9
148 }
149  
150  
151 sub _build_lookup {
152 0 ▶ return shift->ali->new_lookup;
Subroutine _build_lookup: not called CC=1 SCAR=6.9
153 }
154  
155  
156 sub _build_blastdb {
157 0 ▶ return Bio::MUST::Drivers::Blast::Database::Temporary->new(
Subroutine _build_blastdb: not called CC=1 SCAR=6.9
158 seqs => [ shift->all_but_new_seqs ]
159 );
160 }
161  
162  
163 sub _build_para_blastdb {
164 0 ▶ my $self = shift;
Subroutine _build_para_blastdb: not called CC=2 SCAR=17.9
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 #### [ALI] PARA file in use: $parafile
170 0 return Bio::MUST::Drivers::Blast::Database::Temporary->new(
171 seqs => $parafile
172 );
173 }
174  
175  
176 sub _build_new_seqs_by_org {
177 0 ▶ my $self = shift;
Subroutine _build_new_seqs_by_org: not called CC=1 SCAR=6.9
178 0 return $self->_split_seqs_by_org(
179 Ali->new( seqs => [ $self->all_new_seqs ] )
180 );
181 }
182  
183  
184 sub _build_ali_seqs_by_org {
185 0 ▶ my $self = shift;
Subroutine _build_ali_seqs_by_org: not called CC=1 SCAR=6.9
186 0 return $self->_split_seqs_by_org(
187 Ali->new( seqs => [ $self->all_but_new_seqs ] )
188 );
189 }
190  
191  
192 sub _build_non_seqs_by_org {
193 0 ▶ my $self = shift;
Subroutine _build_non_seqs_by_org: not called CC=2 SCAR=17.9
194  
195 0 my $nonfile = change_suffix( $self->ali->filename, '.non' );
196 0 ▶ return {} unless -e $nonfile;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $nonfile 0 0 0
197  
198 #### [ALI] NON file in use: $nonfile
199 0 return $self->_split_seqs_by_org( Ali->load($nonfile) );
200 }
201  
202  
203 1 const my $DEF_ORG => ':default';
204  
205 sub _split_seqs_by_org {
206 0 ▶ my $self = shift;
Subroutine _split_seqs_by_org: not called CC=4 SCAR=30.0
207 0 my $seqs = shift; # actually an Ali object...
208  
209 # split Seqs into one or more ArrayRefs keyed by full_org
210 # tie probably useless here but ensuring reproducible logs
211 0 tie my %seqs_for, 'Tie::IxHash';
212 0 for my $seq ($seqs->all_seqs) {
213 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
214 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
215 0 push @{ $seqs_for{$org} }, $seq;
216 }
217  
218 0 return \%seqs_for;
219 }
220  
221  
222 sub _build_query_seqs {
223 0 ▶ my $self = shift;
Subroutine _build_query_seqs: not called CC=3 SCAR=24.8
224  
225 #### [ALI] Collecting queries...
226  
227 0 my $rp = $self->run_proc;
228  
229 my @seqs # filter out doubtful seqs from each query_org
230 0 = grep { not $_->is_doubtful }
231 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
232 $rp->all_query_orgs
233 ;
234  
235 0 ▶ unless (@seqs) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@seqs) 0 0 0
236 0 carp '[ALI] Warning: no seq for any query_org; using longest instead!';
237 0 @seqs = ( $self->get_longest_query_seq );
238 }
239  
240 0 return Temporary->new( seqs => \@seqs );
241 }
242  
243  
244 sub _build_best_hits_by_ref_org {
245 0 ▶ my $self = shift;
Subroutine _build_best_hits_by_ref_org: not called CC=10 SCAR=47.0
246  
247 #### [ALI] Identifying best hits for queries in reference orgs...
248  
249 0 my $rp = $self->run_proc;
250 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
251 0 $args->{-outfmt} = 6;
252 $args->{-max_target_seqs} = 10
253 0 ▶ unless defined $args->{-max_target_seqs};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"-max_target_seqs"} 0 0 0
254  
255 # tie might help making 42 completely deterministic
256 0 tie my %best_hits_for, 'Tie::IxHash';
257 0 tie my %avg_score_for, 'Tie::IxHash';
258  
259 ORG:
260 0 for my $ref_org ($rp->all_ref_orgs) {
261  
262 0 my $query_seqs = $self->query_seqs;
263 0 my $blastdb = $rp->ref_blastdb_for($ref_org);
264 0 my $parser = $blastdb->blastp($query_seqs, $args);
265 #### [ALI] BLASTP: $ref_org . q{ } . $parser->filename
266  
267 # collect best hit(s) for each query against ref proteome
268 0 my $curr_query = q{};
269 0 my $seen_qry_n = 0;
270 0 my $seen_hit_n = 0;
271 0 my $best_score = 0;
272 0 my $cmul_score = 0;
273  
274 # tie might help making 42 completely deterministic
275 0 tie my %hits, 'Tie::IxHash';
276 0 ▶ while ( my $hsp = $parser->next_hit ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $hsp = $parser->next_hit) 0 0 0
277  
278 # fetch query and bitscore for current hit
279 0 my $query = $hsp->query_id;
280 0 my $score = $hsp->bit_score;
281  
282 # reset score if next query
283 0 ▶ if ($query ne $curr_query) {
Branch: 1 branch
BranchTrueFalseTotal
if ($query ne $curr_query) 0 0 0
284 0 $seen_qry_n++;
285 0 $curr_query = $query;
286 0 $best_score = 0;
287 }
288  
289 # include hit if within score tolerance
290 # Note: for consistency with coverage_mul the new lower score
291 # becomes the new 'best_score' (increasing tolerance thus)
292 0 ▶ if ($score >= $best_score * $rp->ref_score_mul) {
Branch: 1 branch
BranchTrueFalseTotal
if ($score >= $best_score * $rp->ref_score_mul) 0 0 0
293 0 my $hit = $hsp->hit_id;
294 ######## [DEBUG] query: $query_seqs->long_id_for($query)
295 ######## [DEBUG] hit: $hit
296 ######## [DEBUG] score: $score
297 0 $hits{$hit}++;
298 0 $seen_hit_n++;
299 0 $best_score = $score;
300 0 $cmul_score += $score;
301 }
302 }
303  
304 0 ▶ $parser->remove unless $rp->debug_mode;
Branch: 1 branch
BranchTrueFalseTotal
unless $rp->debug_mode 0 0 0
305  
306 # skip ref_orgs without best hits
307 0 ▶ unless ($cmul_score) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cmul_score) 0 0 0
308 0 carp "[ALI] Note: no best hit for: $ref_org;"
309 . ' removing it from ref_orgs.';
310 0 next ORG;
311 }
312  
313 # store average score for best hits in current ref proteome
314 # Note: divisor is set to number of best hits (plus unseen queries)
315 0 my $divisor = $seen_hit_n + $query_seqs->count_seqs - $seen_qry_n;
316 0 my $avg_score = $cmul_score / $divisor;
317 0 $avg_score_for{$ref_org} = $avg_score;
318  
319 # store best hit list for current ref proteome
320 0 my $best_hits = IdList->new( ids => [ keys %hits ] );
321 0 $best_hits_for{$ref_org} = $best_hits;
322 }
323 ######## [DEBUG] avg scores: display( pairmap { "$a => $b" } %avg_score_for )
324  
325 0 my $ref_org_n = ceil( $rp->ref_org_mul * $rp->all_ref_orgs );
326 0 my @ref_orgs = keys %avg_score_for;
327 0 ▶ if (@ref_orgs > $ref_org_n) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ref_orgs > $ref_org_n) 0 0 0
328  
329 #### [ALI] Discarding non-best ref_orgs and keeping only: $ref_org_n
330 @ref_orgs = sort {
331 0 $avg_score_for{$b} <=> $avg_score_for{$a}
332 } @ref_orgs;
333 0 @ref_orgs = @ref_orgs[ 0 .. $ref_org_n - 1 ];
334  
335 0 %best_hits_for = map { $_ => $best_hits_for{$_} } @ref_orgs;
336 }
337  
338 0 return \%best_hits_for;
339 }
340  
341 ## use critic
342  
343  
344 sub get_longest_query_seq {
345 0 ▶ my $self = shift;
Subroutine get_longest_query_seq: not called CC=5 SCAR=34.0
Pod undocumented
346  
347 0 my $max_seq;
348 0 my $max_len = 0;
349  
350 # loop through potential queries and select longest seq
351 0 for my $org ($self->all_ali_orgs) {
352  
353 SEQ:
354 0 for my $seq ( @{ $self->ali_seqs_for($org) } ) {
355  
356 # filter out doubtful seqs from each query_org
357 0 ▶ next SEQ if $seq->is_doubtful;
Branch: 1 branch
BranchTrueFalseTotal
if $seq->is_doubtful 0 0 0
358  
359 # find longest seq using a manual yet fast approach
360 0 my $len = $seq->nomiss_seq_len;
361 0 ▶ if ($len > $max_len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len > $max_len) 0 0 0
362 0 $max_seq = $seq;
363 0 $max_len = $len;
364 }
365 }
366 }
367  
368 0 return $max_seq;
369 }
370  
371  
372 sub check_brh_among_best_hits {
373 0 ▶ my $self = shift;
Subroutine check_brh_among_best_hits: not called CC=11 SCAR=48.8
Pod undocumented
374  
375 #### [ALI] Checking BRH among best hits in reference orgs...
376 0 ▶ if ($self->count_ref_orgs < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->count_ref_orgs < 2) 0 0 0
377 0 carp '[ALI] Warning: not enough ref_orgs for checking BRH'
378 . ' among best hits!';
379 0 return;
380 }
381  
382 0 my $rp = $self->run_proc;
383 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
384 0 $args->{-outfmt} = 6;
385 0 $args->{-max_target_seqs} = 1;
386  
387 0 for my $ref_org1 ($self->all_ref_orgs) {
388  
389 # build query_seqs from best hits for current ref_org
390 0 my $query_seqs = Temporary->new(
391 seqs => $rp->ref_blastdb_for($ref_org1)->blastdbcmd(
392 [ $self->best_hits_for($ref_org1)->all_ids ]
393 )->seqs # TODO: improve this through coercion
394 );
395  
396 0 tie my %count_for, 'Tie::IxHash';
397  
398 ORG2:
399 0 for my $ref_org2 ($self->all_ref_orgs) {
400 0 ▶ next ORG2 if $ref_org2 eq $ref_org1;
Branch: 1 branch
BranchTrueFalseTotal
if $ref_org2 eq $ref_org1 0 0 0
401  
402 0 my $blastdb = $rp->ref_blastdb_for($ref_org2);
403 0 my $parser = $blastdb->blastp($query_seqs, $args);
404 #### [ALI] BLASTP: "$ref_org1 vs $ref_org2 " . $parser->filename
405  
406 0 my $best_hits = $self->best_hits_for($ref_org2);
407 0 ▶ while ( my $hsp = $parser->next_query ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $hsp = $parser->next_query) 0 0 0
408 0 my $candidate = $query_seqs->long_id_for( $hsp->query_id );
409 0 my $in_best_hits = $best_hits->is_listed( $hsp->hit_id );
410 ######## [DEBUG]: $candidate . ( $in_best_hits && ' [=BRH=]' )
411 0 ▶ $count_for{$candidate}++ if $in_best_hits;
Branch: 1 branch
BranchTrueFalseTotal
if $in_best_hits 0 0 0
412 }
413 0 ▶ $parser->remove unless $rp->debug_mode;
Branch: 1 branch
BranchTrueFalseTotal
unless $rp->debug_mode 0 0 0
414 }
415  
416 ######## [DEBUG] BRH counts: display( pairmap { "$a => $b" } %count_for )
417  
418 # remove ref_orgs that completely failed BRH
419 # Note: this should not happen too often because of ref_org_mul
420 0 ▶ unless (keys %count_for) {
Branch: 1 branch
BranchTrueFalseTotal
unless (keys %count_for) 0 0 0
421 0 carp "[ALI] Note: failed BRH for: $ref_org1;"
422 . ' removing it from ref_orgs.';
423 0 $self->remove_ref_org($ref_org1);
424 # Note: the remaining ref_orgs (as 1) will not be tested vs. this
425 # ref_org (as 2) from now on; thus early Warnings only due to this
426 # ref_org (as 2) could have been avoided
427 }
428  
429 # check BRH with all *other* ref_orgs (hence - 1)
430 0 my $other_n = $self->all_ref_orgs - 1;
431 carp '[ALI] Warning: best hits not in BRH across all ref_orgs!'
432 unless List::AllUtils::all {
433 0 ▶ $count_for{$_} == $other_n
Subroutine __ANON__: not called CC=1 SCAR=6.9
434 0 ▶ } keys %count_for
Branch: 1 branch
BranchTrueFalseTotal
unless &List::AllUtils::all(sub { $count_for{$_} == $other_n; } , keys %count_for) 0 0 0
435 ;
436 }
437  
438 0 return;
439 }
440  
441  
442 sub merge_chunks {
443 0 ▶ my $self = shift;
Subroutine merge_chunks: not called CC=9 SCAR=45.0
Pod undocumented
444  
445 # fetch all new seqs having a 42 tail
446 # those are the aligned orthologous transcripts
447 0 my $tail_regex = qr{ (:? \.H\d+\.\d+ | \.E\.bf | \.E\.lc ) \b }xms;
448 0 my @new_seqs = grep { $_->full_id =~ $tail_regex } $self->all_new_seqs;
449  
450 # leave early if no new seqs
451 0 ▶ return unless @new_seqs;
Branch: 1 branch
BranchTrueFalseTotal
unless @new_seqs 0 0 0
452  
453 # partition transcripts by full_id ignoring 42 tails
454 my %new_seqs_by_acc = partition_by {
455 0 ▶ ( my $full_id = $_->full_id ) =~ s{$tail_regex}{}; $full_id
Subroutine __ANON__: not called CC=1 SCAR=6.9
456 0 } @new_seqs;
457  
458 # separate single-chunk transcripts from multi-chunk transcripts
459 my ($singles, $multiples) = part {
460 0 ▶ @{ $new_seqs_by_acc{$_} } > 1
Subroutine __ANON__: not called CC=1 SCAR=6.9
461 0 } sort keys %new_seqs_by_acc;
462 ####### [ALI] multi-chunk transcripts: display( @{$multiples} )
463  
464 # simply remove 42 tails from single-chunk transcripts
465 # Note: ->[0] is required because values are singleton ARRAYREFs
466 $new_seqs_by_acc{$_}->[0]->set_seq_id( SeqId->new( full_id => $_ ) )
467 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
468  
469 # leave early if no multi-chunk transcripts
470 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
471  
472 # get alignment width and regex
473 0 my $width = $self->ali->width;
474 0 my $gap_regex = $self->ali->gapmiss_regex;
475  
476 # merge chunks for each transcript
477 0 for my $merged_id ( @{$multiples} ) {
478  
479 # order transcript chunks following BLAST hit and HSP ranks
480 # this will ensure that best-scoring hits/HSPs come first
481 my @chunks = sort {
482 0 ncmp( $a->full_id, $b->full_id )
483 0 } @{ $new_seqs_by_acc{$merged_id} };
484  
485 0 my $merged_seq;
486 0 my $gap = q{ };
487  
488 # take each state from first chunk where it is not missing/gap
489 # insert '*' if no chunk can provide it
490 # Note: we insert a blank at first site to trigger Seq->spacify
491 0 for my $site (0..$width-1) {
492 0 ▶ my $chunk = first { $_->state_at($site) !~ $gap_regex } @chunks;
Subroutine __ANON__: not called CC=1 SCAR=6.9
493 0 ▶ $merged_seq .= $chunk ? $chunk->state_at($site) : $gap;
Branch: 1 branch
BranchTrueFalseTotal
$chunk ? : 0 0 0
494 0 $gap = '*';
495 }
496  
497 # add merged_seq
498 $self->ali->add_seq(
499 0 Seq->new(
500 seq_id => $merged_id,
501 seq => $merged_seq
502 )
503 );
504  
505 # mark chunks for removal
506 0 $self->mark_seq_for_removal( map { $_->full_id } @chunks );
507 }
508  
509 0 return;
510 }
511  
512  
513 sub mark_redundant_seqs {
514 0 ▶ my $self = shift;
Subroutine mark_redundant_seqs: not called CC=9 SCAR=45.0
Pod undocumented
515  
516 # fetch all new seqs not yet marked for removal
517 my @new_seqs = grep {
518 0 !$self->marked_for_removal( $_->full_id )
519 } $self->all_new_seqs;
520  
521 # examine each new_seq for redundancy etc
522 0 for my $seq (@new_seqs) {
523  
524 0 my $id = $seq->full_id;
525 0 my $org = $seq->full_org;
526  
527 # remove seq if included in Ali for the org...
528 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
529 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
530 ####### [ALI] removed for redundancy within ALI: $id
531 0 $self->mark_seq_for_removal($id);
532 }
533 # ... or included in .non file for the org...
534 0 ▶ elsif (List::AllUtils::any { $seq->is_subseq_of($_) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
535 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
536 ####### [ALI] removed for inclusion in NON file: $id
537 0 $self->mark_seq_for_removal($id);
538 }
539 # ... or included in new_seqs for the org
540 # but not seq itself and not if already marked for removal
541 # to avoid both auto and mutual removal of identical new_seqs
542 0 ▶ elsif (List::AllUtils::any { $seq->is_subseq_of($_) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
543 0 ▶ grep { $id ne $_->full_id
Branch: 1 branch
BranchTrueFalseTotal
$id ne $_->full_id and not $self->marked_for_removal($_->full_id) 0 0 0
544 && !$self->marked_for_removal( $_->full_id ) }
545 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
546 ####### [ALI] removed for redundancy with other #NEW# seqs: $id
547 0 $self->mark_seq_for_removal($id);
548 } # Note: it is important that marked_seqs... is constantly updated
549 }
550  
551 0 return;
552 }
553  
554  
555 sub mark_lengthened_seqs {
556 0 ▶ my $self = shift;
Subroutine mark_lengthened_seqs: not called CC=4 SCAR=30.0
Pod undocumented
557  
558 # fetch all new seqs not yet marked for removal
559 my @new_seqs = grep {
560 0 !$self->marked_for_removal( $_->full_id )
561 } $self->all_new_seqs;
562  
563 # examine each new_seq for lengthening
564 0 for my $seq (@new_seqs) {
565 0 my $org = $seq->full_org;
566  
567 # identify pre-existing seqs included in a new_seq...
568 0 my @redundants = map { $_->full_id } # opposite to above...
569 0 grep { $_->is_subseq_of($seq) }
570 0 grep { !$self->marked_for_removal( $_->full_id ) }
571 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
572 ; # Note: it is important that marked_seqs... is constantly updated
573  
574 # ... and remove them
575 # Note: not sure that more than one per new_seq would be very common
576 0 ▶ if (@redundants) {
Branch: 1 branch
BranchTrueFalseTotal
if (@redundants) 0 0 0
577 ####### [ALI] removed for redundancy by lengthening: join ', ', @redundants
578 0 $self->mark_seq_for_removal(@redundants);
579 }
580 }
581  
582 0 return;
583 }
584  
585  
586 sub reorder_new_seqs {
587 0 ▶ my $self = shift;
Subroutine reorder_new_seqs: not called CC=4 SCAR=30.0
Pod undocumented
588  
589 0 my $ali = $self->ali;
590  
591 # partition Ali between old and new_seqs
592 0 ▶ my ($old_seqs, $new_seqs) = part { $_->is_new } $ali->all_seqs;
Subroutine __ANON__: not called CC=1 SCAR=6.9
593  
594 # reorder new_seqs naturally on family/full_org/accession
595 # Note: this should ignore the c# tag during sorting
596 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
597 0 ▶ ncmp( $a->family_then_full_org, $b->family_then_full_org )
Branch: 1 branch
BranchTrueFalseTotal
ncmp($a->family_then_full_org, $b->family_then_full_org) or ncmp($a->accession, $b->accession) 0 0 0
598 ||
599 ncmp( $a->accession, $b->accession )
600 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
601  
602 0 return;
603 }
604  
605  
606 sub BUILD {
607 0 ▶ my $self = shift;
Subroutine BUILD: not called CC=11 SCAR=48.8
Pod undocumented
608  
609 0 my $rp = $self->run_proc;
610  
611 0 my $ali = $self->ali;
612 #### [ALI] #seqs: $ali->count_seqs
613  
614 0 ▶ unless ( $ali->count_seqs ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ali->count_seqs) 0 0 0
615 #### [ALI] empty file; skipping!
616 0 return;
617 }
618  
619 # default to clearing new tags from Ali
620 $ali->clear_new_tags
621 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
622  
623 # check for optional use of .non and .para
624 # Note: lazy builders do not require it but this makes a better log
625 0 $self->count_non_orgs;
626 0 $self->para_blastdb;
627  
628 #### [ALI] queries: display( $self->query_seqs->all_long_ids )
629  
630 0 ▶ if ($rp->ref_brh eq 'on') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rp->ref_brh eq "on") 0 0 0
631  
632 #### [ALI] reference orgs: display( $self->all_ref_orgs )
633  
634 #### [ALI] best hits: display( map { $_->all_ids } $self->all_best_hits )
635  
636 0 $self->check_brh_among_best_hits;
637 }
638  
639 0 for my $org ($rp->all_orgs) {
640  
641 #### [ALI] Processing ORG: $org->{org}
642 0 OrgProcessor->new( ali_proc => $self, %{$org} );
643 }
644  
645 # build ALI outfile honoring out_dir and out_suffix
646 0 my $outfile = insert_suffix($ali->filename, $rp->out_suffix);
647 0 ▶ $outfile = file( $rp->out_dir, basename($outfile) ) if $rp->out_dir;
Branch: 1 branch
BranchTrueFalseTotal
if $rp->out_dir 0 0 0
648  
649 0 ▶ unless ( $rp->run_mode eq 'metagenomic' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rp->run_mode eq "metagenomic") 0 0 0
650  
651 #### [ALI] Making delayed indels...
652 0 $self->integrator->make_indels;
653  
654 #### [ALI] Merging sequence chunks...
655 0 $self->merge_chunks;
656  
657 #### [ALI] Removing redundant (and unwanted) seqs...
658 0 $self->mark_redundant_seqs;
659  
660 # TODO: write tests for this
661 0 ▶ if ($rp->ali_keep_lengthened_seqs eq 'off') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rp->ali_keep_lengthened_seqs eq "off") 0 0 0
662 #### [ALI] Removing lengthened seqs...
663 0 $self->mark_lengthened_seqs;
664 }
665  
666 #### [ALI] merged/redundant/unwanted/lengthened: display( $self->all_marked_seqs )
667 0 $ali->apply_list( $self->remove_marked_seqs($ali) );
668  
669 #### [ALI] Re-ordering remaining aligned seqs...
670 0 $self->reorder_new_seqs;
671  
672 #### [ALI] Writing updated file...
673 0 $ali->store( secure_outfile($outfile) ); # note the secure...
674 }
675  
676 0 ▶ if ($rp->tax_reports eq 'on') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rp->tax_reports eq "on") 0 0 0
677 # TODO: consider moving this in TaxReport-like class
678  
679 #### [ALI] Writing taxonomic report...
680 0 my $tax_report = secure_outfile( # note the secure...
681 change_suffix($outfile, '.tax-report')
682 );
683  
684 0 open my $fh, '>', $tax_report;
685 0 say {$fh} '# ' . join "\t", NewSeq->heads;
686  
687 # print tax_lines for all new_seqs
688 my @new_seqs = $rp->run_mode eq 'phylogenomic'
689 0 ▶ ? map { $_->full_id } $self->all_new_seqs # phylogenomic mode
Branch: 1 branch
BranchTrueFalseTotal
$rp->run_mode eq 'phylogenomic' ? : 0 0 0
690 : nsort( $self->all_tax_line_ids ) # metagenomic mode
691 ;
692 0 say {$fh} join "\n", map { $_->stringify }
693 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
694 } # Note: skip preexisting new_seqs for which there are no tax_lines
695  
696 #### [ALI] Done analyzing file...
697 0 return;
698 }
699  
700  
701 1 __PACKAGE__->meta->make_immutable;
702 1 1;
703  
704 __END__
705  
706 =pod
707  
708 =head1 NAME
709  
710 Bio::MUST::Apps::FortyTwo::AliProcessor - Internal class for forty-two tool
711  
712 =head1 VERSION
713  
714 version 0.210570
715  
716 =head1 AUTHOR
717  
718 Denis BAURAIN <denis.baurain@uliege.be>
719  
720 =head1 COPYRIGHT AND LICENSE
721  
722 This software is copyright (c) 2013 by University of Liege / Unit of Eukaryotic Phylogenomics / Denis BAURAIN.
723  
724 This is free software; you can redistribute it and/or modify it under
725 the same terms as the Perl 5 programming language system itself.
726  
727 =cut
« lib/Bio/MUST/Apps/FortyTwo/OrgProcessor.pm ↑ Summary script/yaml-generator-42.pl »