lib/Bio/MUST/Apps/HmmCleaner/Cleaner.pm

filter: Statementstmt 92.8% 143 / 154 Branchbran 79.5% 35 / 44 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 88.0% 22 / 25 Podpod 0.0% 0 / 5 totaltotal 87.7% 200 / 228 CC 26 SCAR 18.4
CC
26
Cov
90%
_findingShifts
15.2
_build_nogap_seq
6.0
_get_shifts
5.1
CRAP 6.3
« lib/Bio/MUST/Apps/HmmCleaner.pm ↑ Summary script/HmmCleaner.pl »
1 package Bio::MUST::Apps::HmmCleaner::Cleaner;
2 # ABSTRACT: Cleaner class for HmmCleaner
3 3 $Bio::MUST::Apps::HmmCleaner::Cleaner::VERSION = '0.180750';
4 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Smart::Comments -ENV;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use List::AllUtils qw/max pairmap indexes/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 ▶use Bio::MUST::Core 0.180230;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Bio::MUST::Core::Constants qw(:gaps :files);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use aliased 'Bio::MUST::Core::SeqMask';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use aliased 'Bio::MUST::Core::Seq';
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 ####### [PROCESS] Value of env verbosity : scalar(split ' ', $ENV{Smart_Comments})
18  
19 # ATTRIBUTES
20  
21 3 has 'seq' => (
22 is => 'ro',
23 isa => 'Bio::MUST::Core::Seq',
24 required => 1,
25 );
26  
27 3 has 'score' => (
28 is => 'ro',
29 isa => 'Str',
30 required => 1,
31 );
32  
33 3 has 'nogap_seq' => (
34 is => 'ro',
35 isa => 'Bio::MUST::Core::Seq',
36 lazy => 1,
37 builder => '_build_nogap_seq',
38 );
39  
40 3 has 'threshold' => (
41 is => 'ro',
42 isa => 'Int',
43 required => 1,
44 writer => '_set_threshold',
45 );
46  
47 3 has 'delchar' => (
48 is => 'ro',
49 isa => 'Str',
50 default => ' ',
51 );
52  
53 3 has 'nogap_shifts' => (
54 is => 'ro',
55 isa => 'ArrayRef[Int]', # Future 'Bio::MUST::Core::SeqMask' or not
56 lazy => 1,
57 writer => '_set_nogap_shifts',
58 builder => '_build_nogap_shifts',
59 );
60  
61 3 has 'shifts' => (
62 is => 'ro',
63 isa => 'ArrayRef[Int]', # Future 'Bio::MUST::Core::SeqMask' or not
64 lazy => 1,
65 writer => '_set_shifts',
66 builder => '_build_shifts',
67 );
68  
69 3 has 'costs' => (
70 traits => ['Array'],
71 is => 'ro',
72 isa => 'ArrayRef[Num]',
73 required => 1,
74 writer => '_set_costs',
75 handles => {
76 _get_cost => 'get',
77 }
78
79 );
80  
81 3 has 'consider_X' => (
82 is => 'ro',
83 isa => 'Bool',
84 default => 1,
85 );
86  
87 3 has 'is_protein' => (
88 is => 'ro',
89 isa => 'Bool',
90 default => 1,
91 );
92  
93  
94 # BUILDER
95  
96 sub _build_nogap_seq {
97 0 ▶ my $self = shift;
Subroutine _build_nogap_seq: not called CC=2 SCAR=17.9
98 0 my $seq = $self->seq;
99 0 ▶ my $nogap_seq = ($self->consider_X) ? $seq->clone->gapify('X')->degap : $seq->clone->gapify->degap;
Branch: 1 branch
BranchTrueFalseTotal
$self->consider_X ? : 0 0 0
100 ##### [CLEANER] Nogap seq : $nogap_seq->seq
101 0 return $nogap_seq;
102 }
103  
104 sub _build_nogap_shifts {
105 1 ▶ return shift->_get_nogap_shifts;
Subroutine _build_nogap_shifts: called CC=1 SCAR=0.0
106 }
107  
108 sub _build_shifts {
109 1 ▶ return shift->_get_shifts;
Subroutine _build_shifts: called CC=1 SCAR=0.0
110 }
111  
112  
113 # METHOD
114  
115 sub update {
116 1 ▶ my $self = shift;
Subroutine update: called CC=1 SCAR=0.0
Pod undocumented
117 1 my $threshold = shift;
118 1 my $costs = shift;
119  
120 1 $self->_set_costs($costs);
121 1 $self->_set_threshold($threshold);
122 # watch out, the order is important here
123 1 $self->_set_nogap_shifts($self->_get_nogap_shifts);
124 1 $self->_set_shifts($self->_get_shifts);
125  
126 1 return 1;
127 }
128  
129 sub get_nogap_result {
130 0 ▶ my $self = shift;
Subroutine get_nogap_result: not called CC=1 SCAR=6.9
Pod undocumented
131
132 #~ my $nogap_result = _erasezone($self, $self->nogap_seq->seq, $self->nogap_mask);
133 #~ #### $nogap_result
134 #~ return Seq->new( seq_id => $self->nogap_seq->full_id, seq => $nogap_result );
135  
136 0 return $self->_apply_mask($self->nogap_seq, $self->get_nogap_seqmask);
137
138 }
139  
140 sub get_result {
141 2 ▶ my $self = shift;
Subroutine get_result: called CC=1 SCAR=0.0
Pod undocumented
142  
143 #~ my $result = _erasezone( $self, $self->seq->seq, $self->mask );
144 #~ #### $result
145 #~ return Seq->new( seq_id => $self->seq->full_id, seq => $result );
146
147 2 return $self->_apply_mask($self->seq, $self->get_seqmask);
148 }
149  
150 # PRIVATE SUB
151  
152 sub _get_nogap_shifts {
153 2 ▶ my $self = shift;
Subroutine _get_nogap_shifts: called CC=1 SCAR=0.0
154  
155 2 my $concat_scoreseq = $self->score;
156
157 # this function return a list of shifts between good and bad positions, ArrayRef
158 ##### [CLEANER] Finding shifts...
159 2 my $shifts = _findingShifts($self, $concat_scoreseq);
160 ###### [CLEANER] $shifts
161  
162 2 return $shifts;
163 }
164  
165  
166 sub _get_shifts {
167 2 ▶ my $self = shift;
Subroutine _get_shifts: called CC=5 SCAR=16.3
168 2 my $shifts = $self->nogap_shifts;
169
170 ##### [CLEANER] Looking for gaps in full sequence...
171 2 my @gaps_n_seqs;
172 2 my $push = 0;
173
174 2 ▶ my $regex = ($self->consider_X)
Branch: 2 branches
BranchTrueFalseTotal
$self->consider_X ? : 2 0 2
$self->is_protein ? : 0 0 0
175 ? $GAP
176 : ( ($self->is_protein) ? $GAPPROTMISS : $GAPDNAMISS );
177 2 foreach my $tab ( split /$regex/x, $self->seq->seq ) {
178 228 ▶ if( length($tab) == 0){
Branch: 1 branch
BranchTrueFalseTotal
if (length $tab == 0) { } 174 54 228
179 174 $push++;
180 }
181 else{
182 54 push(@gaps_n_seqs, $push);
183 54 push(@gaps_n_seqs, length($tab));
184 54 $push = 1;
185 }
186 }
187
188 ###### [CLEANER] @gaps_n_seqs
189  
190 # Updating frameshift position by taking gaps into account
191 2 my $shifts_withgaps = _pushingShifts( $shifts, \@gaps_n_seqs );
192
193 ##### [CLEANER] shifts after _pushingShifts: $shifts_withgaps
194  
195 2 return $shifts_withgaps;
196  
197 }
198  
199 sub _shifts2mask {
200 2 ▶ my $shifts = shift; # shifts as created by _build_shifts or _pushingShifts
Subroutine _shifts2mask: called CC=4 SCAR=13.9
201 2 my $max = shift; # max size of mask -> seq length
202  
203 2 my @blocks;
204 2 ▶ push @blocks, [1,$$shifts[0]] if ($$shifts[0]!=0);
Branch: 1 branch
BranchTrueFalseTotal
if $shifts->[0] != 0 0 2 2
205 2 ▶ for (my $i=1; $i<@$shifts-1; $i+=2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$shifts - 1) 2 2 4
206 2 push @blocks, [$$shifts[$i]+1,$$shifts[$i+1]];
207 }
208
209 2 my $mask = SeqMask->empty_mask($max);
210 2 $mask->mark_block( @{$_} ) for @blocks;
211  
212 2 return $mask;
213 }
214  
215 # slide the shifts to match the fullseq with gap
216 sub _pushingShifts {
217 2 ▶ my ($refshifts, $reftabs) = @_;
Subroutine _pushingShifts: called CC=3 SCAR=11.0
218
219 2 my @shifts = @$refshifts;
220 2 my @tabs = @$reftabs;
221
222 2 my $index = 0;
223 2 my $push = 0;
224 2 my $level = 0;
225
226 # iterate throught length of gaps' block and length of seqs' block
227 2 ▶ while(scalar(@tabs) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @tabs != 0) 54 2 56
228
229 54 $push += shift(@tabs); # a total number of gap cross at the moment
230 54 $level += shift(@tabs); # a total number of residue cross at the moment
231
232 # add current crossed number of gap when the number of residue pass the shift position
233 54 ▶ while( $shifts[$index] < $level ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($shifts[$index] < $level) 4 54 58
234 4 $shifts[$index] += $push;
235 4 $index++;
236 }
237
238 }
239 2 $shifts[$#shifts] += $push;
240
241 2 return \@shifts;
242 }
243  
244 # slide the shifts to match the fullseq with gap
245 #~ sub _pushingBlocks {
246 #~ use Smart::Comments;
247 #~ my ($refblocks, $reftabs) = @_;
248 #~
249 #~ my @flat_blocks = map {@$_} @$refblocks;
250 #~ my @tabs = @$reftabs;
251 #~
252 #~ my $index = 0;
253 #~ my $push = 0;
254 #~ my $level = 0;
255 #~
256 #~ # iterate throught length of gaps' block and length of seqs' block
257 #~ while(scalar(@tabs) != 0) {
258 #~
259 #~ $push += shift(@tabs); # a total number of gap cross at the moment
260 #~ $level += shift(@tabs); # a total number of residue cross at the moment
261 #~
262 #~ next if ($index == $#flat_blocks);
263 #~ # add current crossed number of gap when the number of residue pass the shift position
264 #~ while( $flat_blocks[$index] < $level ) {
265 #~ ### in while Blocks : $flat_blocks[$index]
266 #~ ### in while level : $level
267 #~ $flat_blocks[$index] += $push;
268 #~ $index++;
269 #~ }
270 #~ last if ($index == $#flat_blocks);
271 #~ }
272 #~ $flat_blocks[$#flat_blocks] += $push;
273 #~ ### End value level : $level
274 #~ ### End value push : $push
275 #~
276 #~ push my @blocks, pairmap { [ $a , $b ] } @flat_blocks;
277 #~ return \@blocks;
278 #~ }
279  
280  
281 # use shifts informations to replace blocks in sequence by $delchar
282 #~ sub _erasezone {
283 #~ my ($self, $seq, $shifts) = @_;
284 #~
285 #~ my $result = '';
286 #~ my @s = @$shifts;
287 #~ my $a1 = 0;
288 #~
289 #~ while(scalar(@s) != 0) {
290 #~ my $a2 = shift(@s);
291 #~ $result .= substr($seq, $a1, $a2-$a1);
292 #~
293 #~ if ( scalar(@s) != 0 ) {
294 #~ $a1 = shift(@s);
295 #~ } else {
296 #~ $a1 = length($seq);
297 #~ }
298 #~ $result .= $self->delchar x ($a1-$a2);
299 #~ }
300 #~ $result .= substr($seq, $a1, length($seq)-$a1);
301 #~ return $result;
302 #~ }
303  
304 sub _apply_mask {
305 2 ▶ my $self = shift;
Subroutine _apply_mask: called CC=2 SCAR=6.9
306 2 my $seq = shift;
307 2 my $mask = shift;
308  
309 2 my $new_seq = $seq->clone;
310 # select sites for each seq using a precomputed array slice
311 2 ▶ my @indexes = indexes { !$_ } $mask->all_states;
Subroutine __ANON__: called CC=1 SCAR=0.0
312 2 for my $pos (@indexes) {
313 149 $new_seq->edit_seq($pos,1,$self->delchar);
314 }
315  
316 2 return $new_seq;
317 }
318  
319 sub get_nogap_seqmask {
320 0 ▶ my $self = shift;
Subroutine get_nogap_seqmask: not called CC=1 SCAR=6.9
Pod undocumented
321  
322 0 return _shifts2mask($self->nogap_shifts, $self->nogap_seq->seq_len);
323 }
324  
325 sub get_seqmask {
326 2 ▶ my $self = shift;
Subroutine get_seqmask: called CC=1 SCAR=0.0
Pod undocumented
327  
328 2 return _shifts2mask($self->shifts, $self->seq->seq_len);
329 }
330  
331 # Base of algorithm, decide if there are frameshift in seq based on domains score
332 sub _findingShifts {
333 2 ▶ my ($self, $score) = @_;
Subroutine _findingShifts: called CC=15 SCAR=27.2
334
335 2 my $threshold = $self->threshold;
336 2 my $val = $threshold;
337 2 my $previous_pos = -1;
338 2 my $shift = $previous_pos;
339 2 my $hasShift = 0;
340
341 2 my @shifts;
342 2 ▶ for(my $pos=0; $pos != length($score); $pos++){
Branch: 1 branch
BranchTrueFalseTotal
if ($pos != length $score) 1674 2 1676
343 1674 my $c = substr($score, $pos, 1);
344
345 # Updating val
346 1674 ▶ if($c eq " "){
Branch: 4 branches
BranchTrueFalseTotal
if ($c eq ' ') { } 130 1544 1674
elsif ($c eq '+') { } 128 1416 1544
elsif ($c =~ /[a-z]/ux) { } 1416 0 1416
elsif ($c =~ /[A-Z]/ux) { } 0 0 0
347 130 $val += $self->_get_cost(0); # default -0.15
348 }
349 elsif($c eq "+"){
350 128 $val += $self->_get_cost(1); # default -0.08
351 }
352 elsif($c =~ /[a-z]/x){
353 1416 $val += $self->_get_cost(2); # default 0.15
354 }
355 elsif($c =~ /[A-Z]/x){
356 0 $val += $self->_get_cost(3); # default 0.45
357 }
358 else{
359 0 carp "Found unknowed character in Hmmer output : -$c-";
360 0 exit;
361 }
362
363
364 # Putting mininal value back into window
365 1674 ▶ if($val < 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($val < 0) 98 1576 1674
366 98 $val = 0;
367 }
368 1674 ▶ if($val>$threshold){
Branch: 1 branch
BranchTrueFalseTotal
if ($val > $threshold) 1373 301 1674
369 1373 $val = $threshold;
370 }
371
372 # Deciding if shift or not
373 1674 ▶ if($hasShift==0){
Branch: 1 branch
BranchTrueFalseTotal
if ($hasShift == 0) { } 1534 140 1674
374 1534 ▶ if($val==0){
Branch: 3 branches
BranchTrueFalseTotal
if ($val == 0) { } 2 1532 1534
elsif ($val == $threshold) { } 1376 156 1532
elsif ($shift == $previous_pos) { } 99 57 156
375 2 push(@shifts, $shift);
376 2 $previous_pos = $pos;
377 2 $shift = $previous_pos;
378 2 $hasShift = 1;
379 }
380 elsif($val == $threshold){
381 1376 $shift = $previous_pos;
382 }
383 elsif($shift == $previous_pos){
384 99 $shift=$pos;
385 }
386 }
387 else{
388 140 ▶ if($val == 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($val == 0) { } 97 43 140
389 97 $shift = $previous_pos;
390 }
391 else{
392 43 ▶ if($shift == $previous_pos){
Branch: 1 branch
BranchTrueFalseTotal
if ($shift == $previous_pos) 3 40 43
393 3 $shift = $pos;
394 }
395 43 ▶ if($val==$threshold){
Branch: 1 branch
BranchTrueFalseTotal
if ($val == $threshold) 2 41 43
396 2 push(@shifts, $shift);
397 2 $previous_pos = $pos;
398 2 $hasShift=0;
399 2 $shift=$previous_pos;
400 }
401 }
402 }
403
404 }
405 2 push(@shifts, length($score));
406 #foreach (@shifts) {print "$_ ";}print "\n";
407 2 return \@shifts;
408 }
409  
410 # Base of algorithm, decide if there are frameshift in seq based on domains score
411 #~ sub _findingBlocks {
412 #~ my ($self, $score) = @_;
413 #~
414 #~ my $threshold = $self->threshold;
415 #~ my $val = $threshold;
416 #~ my $startok = 0;
417 #~ my $endok = 0;
418 #~ my $isok = 1;
419 #~
420 #~ my @blocks;
421 #~ for(my $pos=1; $pos <= length($score); $pos++){
422 #~ my $c = substr($score, $pos-1, 1);
423 #~
424 #~ # Updating val
425 #~ if($c eq " "){
426 #~ $val += $self->_get_cost(0); # default -3
427 #~ }
428 #~ elsif($c eq "+"){
429 #~ $val += $self->_get_cost(1); # default -1
430 #~ }
431 #~ elsif($c =~ /[a-z]/){
432 #~ $val += $self->_get_cost(2); # default 10/5 = 2
433 #~ }
434 #~ elsif($c =~ /[A-Z]/){
435 #~ $val += $self->_get_cost(3); # default 10/2 = 5
436 #~ }
437 #~ else{
438 #~ carp "Found unknowed character in Hmmer output : -$c-";
439 #~ exit;
440 #~ }
441 #~
442 #~
443 #~ # Putting minimal value back into window
444 #~ $val = 0 if ($val<0);
445 #~ $val = $threshold if ($val>$threshold);
446 #~
447 #~ # Deciding if shift or not
448 #~ if($isok){
449 #~ if ($val==0) {
450 #~ ### not ok anymore start : $startok
451 #~ ### not ok anymore end : $endok
452 #~ push(@blocks, [$startok,$endok]) if ($endok);
453 #~ $isok = 0;
454 #~ $startok = $pos+1;
455 #~ } else {
456 #~ if ($val == $threshold) {
457 #~ $startok = 1 unless ($endok);
458 #~ $endok = -1;
459 #~ } elsif ($endok == -1) {
460 #~ $endok = $pos-1 if ($startok);
461 #~ }
462 #~ }
463 #~ } else {
464 #~ if ($val==$threshold) {
465 #~ $isok = 1;
466 #~ $endok = -1;
467 #~ ### ok again start : $startok
468 #~ ### ok again end : $endok
469 #~ }
470 #~ $startok = $pos+1 if ($val==0);
471 #~ }
472 #~ ### char Val pos : join("\t", ($c,$val,$pos))
473 #~ }
474 #~ push(@blocks, [$startok,length($score)]) if ($isok);
475 #~ #foreach (@shifts) {print "$_ ";}print "\n";
476 #~ return \@blocks;
477 #~ }
478  
479  
480 3 __PACKAGE__->meta->make_immutable;
481 3 1;
482  
483 __END__
484  
485 =pod
486  
487 =encoding UTF-8
488  
489 =head1 NAME
490  
491 Bio::MUST::Apps::HmmCleaner::Cleaner - Cleaner class for HmmCleaner
492  
493 =head1 VERSION
494  
495 version 0.180750
496  
497 =head1 AUTHOR
498  
499 Arnaud Di Franco <arnaud.difranco@gmail.fr>
500  
501 =head1 COPYRIGHT AND LICENSE
502  
503 This software is copyright (c) 2018 by Arnaud Di Franco.
504  
505 This is free software; you can redistribute it and/or modify it under
506 the same terms as the Perl 5 programming language system itself.
507  
508 =cut
« lib/Bio/MUST/Apps/HmmCleaner.pm ↑ Summary script/HmmCleaner.pl »