lib/Bio/MUST/Apps/Roles/OrgProcable.pm

filter: Statementstmt 36.0% 35 / 97 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 71.4% 10 / 14 Podpod 0.0% 0 / 1 totaltotal 27.9% 45 / 161 CC 21 SCAR 47.7
CC
21
Cov
25%
_fetch_and_trim_hits
240.0
_collect_hsps
56.0
__ANON__
2.0
CRAP 117.8
« lib/Bio/MUST/Apps/FortyTwo/OrgProcessor.pm ↑ Summary lib/Bio/MUST/Apps/FortyTwo/AliProcessor.pm »
1 package Bio::MUST::Apps::Roles::OrgProcable;
2 # ABSTRACT: Attributes and methods common to OrgProcessor objects
3 1 $Bio::MUST::Apps::Roles::OrgProcable::VERSION = '0.262780';
4 1 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use feature qw(say);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use Smart::Comments -ENV;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use List::AllUtils qw(each_array nsort_by);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Number::Interval;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶use Bio::MUST::Core;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use aliased 'Bio::MUST::Core::SeqId';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use aliased 'Bio::MUST::Core::SeqMask';
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18  
19 1 has 'org' => (
20 is => 'ro',
21 isa => 'Str',
22 required => 1,
23 );
24  
25  
26 1 has 'banks' => (
27 traits => ['Array'],
28 is => 'ro',
29 isa => 'ArrayRef[Str]',
30 required => 1,
31 handles => {
32 all_banks => 'elements',
33 },
34 );
35  
36  
37 1 has 'code' => (
38 is => 'ro',
39 isa => 'Str',
40 default => '1',
41 );
42  
43  
44 0 ▶sub _collect_hsps { ## no critic (RequireArgUnpacking)
Subroutine _collect_hsps: not called CC=7 SCAR=40.3
45 0 my $self = shift;
46 0 my %args = @_;
47  
48 my ($parser, $protein, $transcript, $mask_for, $hit_for, $strand_for)
49 0 = @args{ qw(parser protein transcript mask_for hit_for strand_for) };
50  
51 # Note: this method should work both for
52 # - XML HSPs within a single hit (1331)
53 # - multi-query/multi-hit tabular HSPs (42)
54 0 ▶ $parser //= $transcript;
Condition0%0/3
$parser //= $transcript
l!l&&r!l&&!r
000
MC/DC0%0/2
$parser //= $transcript
$parser $transcript
Truth table0%0/3
A //= B
A= $parserB= $transcript
ABresult
1X1
011
000
55  
56 0 ▶ while (my $hsp = $parser->next_hsp) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $hsp = $parser->next_hsp) 0 0 0
57  
58 # get mask key from query protein (1331) or hit hsp (42)
59 0 ▶ my $key = $transcript ? $protein->query_def : $hsp->hit_id;
Branch: 1 branch
BranchTrueFalseTotal
$transcript ? : 0 0 0
60  
61 # update coverage on corresponding hit
62 0 push @{ $mask_for->{$key} }, Number::Interval->new(
63 min => $hsp->hit_start, max => $hsp->hit_end,
64 incmin => 1, incmax => 1,
65 );
66  
67 # record protein => best transcript pair (only for 1331)
68 # Note: we do this here rather than in leel OrgProcessor for symmetry
69 0 ▶ $hit_for->{$key} //= $transcript->id
Branch: 1 branch
BranchTrueFalseTotal
if defined $hit_for 0 0 0
Condition0%0/3
$hit_for->{$key} //= $transcript->id
l!l&&r!l&&!r
000
MC/DC0%0/2
$hit_for->{$key} //= $transcript->id
$hit_for->{$key} $transcript->id
Truth table0%0/3
A //= B
A= $hit_for->{$key}B= $transcript->id
ABresult
1X1
011
000
70 if defined $hit_for;
71  
72 # record strand (only for 42)
73 0 ▶ $strand_for->{$key} += $hsp->hit_strand
Branch: 1 branch
BranchTrueFalseTotal
if defined $strand_for 0 0 0
74 if defined $strand_for;
75 }
76  
77 0 return;
78 }
79  
80  
81 0 ▶sub _fetch_and_trim_hits { ## no critic (RequireArgUnpacking)
Subroutine _fetch_and_trim_hits: not called CC=15 SCAR=54.8
82 0 my $self = shift;
83 0 my %args = @_;
84  
85 my ($blastdb, $mask_for, $hit_for, $strand_for)
86 0 = @args{ qw(blastdb mask_for hit_for strand_for) };
87  
88 0 my $rp = $self->ali_proc->run_proc;
89  
90 # compute seq chunks to extract from all hits (default behavior)
91 0 my @entries;
92 0 ▶ if ($rp->trim_homologues eq 'on') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rp->trim_homologues eq 'on') { } 0 0 0
93  
94 # tie might help making app completely deterministic
95 0 tie my %chunks_for, 'Tie::IxHash';
96  
97 # loop through masks to consolidate neighboring ranges into chunks
98 # Note: while ranges always deal with hit seqs...
99 # ... the key is either a protein abbr_id (1331) or a hit id (42)
100 0 ▶ while ( my ($key, $mask) = each %{$mask_for} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$mask_for;}) 0 0 0
101  
102 # first fetch seed range (corresponding to the best HSP)
103 # this will be useful for 1331 only (see below)
104 0 my $seed = $mask->[0]->min;
105  
106 # then sort ranges in ascending order
107 0 ▶ my @ranges = nsort_by { $_->min } @{$mask};
Subroutine __ANON__: not called CC=1 SCAR=6.9
108  
109 # merge successive ranges not farther away than trim_max_shift
110 0 my $range1 = shift @ranges;
111 0 ▶ while (my $range2 = shift @ranges) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $range2 = shift @ranges) 0 0 0
112  
113 # too distant: store current chunk and start new one
114 0 ▶ if ( $range2->min - $range1->max > $rp->trim_max_shift) {
Branch: 1 branch
BranchTrueFalseTotal
if ($range2->min - $range1->max > $rp->trim_max_shift) { } 0 0 0
115 0 push @{ $chunks_for{$key} }, $range1;
116 0 $range1 = $range2;
117 }
118  
119 # close enough: extend current chunk
120 else {
121 0 $range1->max(
122 List::AllUtils::max( $range1->max, $range2->max )
123 ); # Note: this is needed as BLAST can return HSPs
124 } # that are completely included within better HSPs...
125 }
126  
127 # store last chunk
128 0 push @{ $chunks_for{$key} }, $range1;
129  
130 # only keep the (extended) range containing seed (only for 1331)
131 # this allows extracting the correct seq in case of paralogy
132 0 ▶ if (defined $hit_for) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hit_for) 0 0 0
133 $chunks_for{$key} = [
134 0 grep { $_->contains($seed) } @{ $chunks_for{$key} }
135 ];
136 }
137 }
138  
139 # format chunks for retrieval with blastdbcmd...
140 # ... allowing for hit_extra_margin (no risk of out-of-bound max)
141 # keys are converted to hit ids on the fly (only for 1331)
142 0 ▶ while ( my ($key, $chunks) = each %chunks_for ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %chunks_for) 0 0 0
143 push @entries, map {
144 0 ▶ (defined $hit_for ? $hit_for->{$key} : $key) . q{ }
Branch: 1 branch
BranchTrueFalseTotal
defined $hit_for ? : 0 0 0
145 # TODO: check why max is not enough to prevent some crashes with non-null trim_extra_margin
146 . List::AllUtils::max(1, $_->min - $rp->trim_extra_margin)
147 . q{-} . ($_->max + $rp->trim_extra_margin)
148 0 } @{$chunks};
149 }
150 }
151  
152 # otherwise simply extract whole hits
153 else {
154 @entries = map { # again on the fly (see above)
155 0 ▶ defined $hit_for ? $hit_for->{$_} : $_
Branch: 1 branch
BranchTrueFalseTotal
defined $hit_for ? : 0 0 0
156 0 } keys %{$mask_for};
157 }
158  
159 # fetch hits from BLAST database
160 # in any case extracted seqs are hits (for both 1331 and 42)
161 # trimming is left to blastdbcmd (should be very efficient)
162 0 my @hits = $blastdb->blastdbcmd( \@entries )->all_seqs;
163  
164 # rename hits after (long) id of corresponding protein (only for 1331)
165 0 ▶ if (defined $hit_for) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hit_for) 0 0 0
166 0 my @abbr_ids = keys %{$mask_for};
167 0 my $ea = each_array(@abbr_ids, @hits);
168 0 ▶ while (my ($abbr_id, $hit) = $ea->() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = &$ea()) 0 0 0
169 0 my $new_id = $self->long_id_for($abbr_id);
170 0 $hit->set_seq_id( SeqId->new( full_id => $new_id ) );
171 }
172 }
173  
174 # rename hits to fill in tax-reports (only for 42)
175 # this is only needed when trim_homologues is on (default behavior)
176 0 ▶ if (defined $strand_for && $rp->trim_homologues eq 'on') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $strand_for and $rp->trim_homologues eq "on") 0 0 0
Condition0%0/3
defined $strand_for and $rp->trim_homologues eq "on"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $strand_for and $rp->trim_homologues eq "on"
defined $strand_for $rp->trim_homologues eq "on"
Truth table0%0/3
A and B
A= defined $strand_forB= $rp->trim_homologues eq "on"
ABresult
0X0
100
111
177 0 my %count_for;
178 0 my $ea = each_array(@entries, @hits);
179 0 ▶ while (my ($entry, $hit) = $ea->() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = &$ea()) 0 0 0
180  
181 # extract details from entry
182 0 my ($hit_id, $start, $end) = $entry =~ m/(\S+)\s(\d+)\-(\d+)/xms;
183  
184 # compute hit strand (through HSPs votes)
185 # Note: strand might be inaccurate in case of multiple chunks
186 0 ▶ my $strand = $strand_for->{$hit_id} >= 0 ? 1 : -1;
Branch: 1 branch
BranchTrueFalseTotal
$strand_for->{$hit_id} >= 0 ? : 0 0 0
187  
188 # embed range and strand in hit_id
189 0 my $new_id = join ':::', $hit_id, $start, $end, $strand;
190 0 $hit->set_seq_id( SeqId->new( full_id => $new_id ) );
191 }
192 }
193  
194 0 return @hits;
195 }
196  
197  
198 0 ▶sub display { ## no critic (RequireArgUnpacking)
Subroutine display: not called CC=1 SCAR=6.9
Pod undocumented
199 0 return join "\n--- ", q{}, @_
200 }
201  
202  
203 1 ▶no Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
204 1 1;
205  
206 __END__
« lib/Bio/MUST/Apps/FortyTwo/OrgProcessor.pm ↑ Summary lib/Bio/MUST/Apps/FortyTwo/AliProcessor.pm »