lib/Bio/MUST/Apps/HmmCleaner.pm

filter: Statementstmt 46.7% 87 / 186 Branchbran 23.0% 6 / 26 Conditioncond 20.0% 3 / 15 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 64.5% 20 / 31 Podpod 0.0% 0 / 11 totaltotal 41.8% 116 / 277 CC 28 SCAR 22.0
CC
28
Cov
42%
_build_cleaners
24.1
get_result_ali
12.0
store_log
12.0
CRAP 9.1
« script/frameshiftSimul_final.pl ↑ Summary lib/Bio/MUST/Apps/HmmCleaner/Cleaner.pm »
1 package Bio::MUST::Apps::HmmCleaner;
2 # ABSTRACT: Main class for HmmCleaner
3 3 $Bio::MUST::Apps::HmmCleaner::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 Smart::Comments -ENV;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use List::AllUtils qw/uniq max/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Path::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 ▶use Bio::MUST::Core 0.180230;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Bio::MUST::Core::Constants qw(:gaps :files);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use Bio::MUST::Drivers 0.180270;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use Bio::MUST::Drivers::Hmmer::Model::Temporary;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 3 ▶use aliased 'Bio::MUST::Core::Seq';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶use aliased 'Bio::MUST::Core::Ali';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶use aliased 'Bio::MUST::Core::IdList';
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 3 ▶use aliased 'Bio::MUST::Apps::HmmCleaner::Process';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 3 ▶use aliased 'Bio::MUST::Apps::HmmCleaner::Cleaner';
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 # ATTRIBUTES
27  
28 3 has 'ali' => (
29 is => 'ro',
30 isa => 'Bio::MUST::Core::Ali',
31 required => 1,
32 coerce => 1,
33 );
34  
35 3 has 'ali_model' => (
36 is => 'ro',
37 isa => 'Bio::MUST::Core::Ali',
38 required => 1,
39 coerce => 1,
40 );
41  
42 3 has 'threshold' => (
43 is => 'ro',
44 isa => 'Int',
45 required => 1,
46 writer => '_set_threshold',
47 default => 1,
48 );
49  
50 3 has 'changeID' => (
51 is => 'ro',
52 isa => 'Bool',
53 default => 0,
54 );
55  
56 3 has 'consider_X' => (
57 is => 'ro',
58 isa => 'Bool',
59 default => 1,
60 );
61  
62 3 has 'symfrac' => (
63 is => 'ro',
64 isa => 'Num',
65 default => 0.5,
66 );
67  
68 3 has 'delchar' => (
69 is => 'ro',
70 isa => 'Str',
71 default => ' ',
72 );
73  
74 3 has 'costs' => (
75 traits => ['Array'],
76 is => 'ro',
77 isa => 'ArrayRef[Num]',
78 lazy => 1,
79 builder => '_build_cost',
80 handles => {
81 _get_cost => 'get',
82 }
83
84 );
85  
86 3 has '_cleaners' => (
87 traits => ['Array'],
88 is => 'ro',
89 isa => 'ArrayRef[Bio::MUST::Apps::HmmCleaner::Cleaner]',
90 lazy => 1,
91 builder => '_build_cleaners',
92 handles => {
93 all_cleaners => 'elements',
94 },
95 );
96  
97 3 has 'outfile' => (
98 is => 'ro',
99 isa => 'Str',
100 lazy => 1,
101 builder => '_build_outfile',
102 writer => '_set_outfile',
103 );
104  
105 3 has 'outfile_type' => (
106 is => 'ro',
107 isa => 'Bool',
108 default => 0,
109 );
110  
111 3 has 'perseq_profile' => (
112 is => 'ro',
113 isa => 'Bool',
114 default => 1,
115 );
116  
117 3 has 'debug_mode' => (
118 is => 'ro',
119 isa => 'Bool',
120 default => 0,
121 );
122  
123  
124 # BUILD
125  
126 # launch on new but before the object construction
127 sub BUILD {
128 ##### [HMMCLEANER] BUILD...
129 1 ▶ my $self = shift;
Subroutine BUILD: called CC=6 SCAR=22.9
Pod undocumented
130
131 1 my $costs = $self->costs;
132 1 ▶ carp "Your costs must be increasing" if ( ($$costs[0] > $$costs[1]) || ($$costs[1] > 0) || ($$costs[2] < 0) || ($$costs[2] > $$costs[3]) );
Branch: 1 branch
BranchTrueFalseTotal
if $costs->[0] > $costs->[1] or $costs->[1] > 0 or $costs->[2] < 0 or $costs->[2] > $costs->[3] 0 1 1
Condition33%3/9
$costs->[0] > $costs->[1] or $costs->[1] > 0 or $costs->[2] < 0 or $costs->[2] > $costs->[3]
l!l&&r!l&&!r
001
$costs->[0] > $costs->[1] or $costs->[1] > 0 or $costs->[2] < 0
l!l&&r!l&&!r
001
$costs->[0] > $costs->[1] or $costs->[1] > 0
l!l&&r!l&&!r
001
MC/DC0%0/4
$costs->[0] > $costs->[1] or $costs->[1] > 0 or $costs->[2] < 0 or $costs->[2] > $costs->[3]
$costs->[0] > $costs->[1] $costs->[1] > 0 $costs->[2] < 0 $costs->[2] > $costs->[3]
Truth table20%1/5
A or B or C or D
A= $costs->[0] > $costs->[1]B= $costs->[1] > 0C= $costs->[2] < 0D= $costs->[2] > $costs->[3]
ABCDresult
1XXX1
01XX1
001X1
00011
00000
133
134 1 ▶ carp 'Your MSA file is not aligned' unless ($self->ali->is_aligned);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->ali->is_aligned 0 1 1
135
136 1 $self->_cleaners;
137
138 ##### End of BUILD...
139 0 return;
140 }
141  
142  
143 # BUILDER
144  
145 # delayed costs creation
146 sub _build_cost {
147 1 ▶ return shift->_get_default_cost;
Subroutine _build_cost: called CC=1 SCAR=0.0
148 }
149  
150 sub _build_outfile {
151 0 ▶ my $self = shift;
Subroutine _build_outfile: not called CC=1 SCAR=6.9
152
153 0 my $infile = $self->ali->filename;
154
155 0 (my $outfile = $infile) =~ s/\.[^\.]+$//x;
156 0 $outfile .= '_hmm';
157
158 0 return $outfile;
159 }
160  
161 # produce the cleaned sequences
162 sub _build_cleaners {
163 #### [HMMCLEANER] building cleaners...
164
165 1 ▶ my $self = shift;
Subroutine _build_cleaners: called CC=9 SCAR=31.8
166  
167 1 my $ali = $self->ali;
168 1 my $ali_model = $self->ali_model;
169 1 my $lookup = $ali->new_lookup;
170  
171 # Cleaners container
172 1 my @cleaners;
173  
174 # arguments for Hmmer driver
175 1 my $model_args = {
176 '--plaplace' => undef,
177 '--fragthresh' => "0.0",
178 '--symfrac' => $self->symfrac,
179 #~ '--wnone' => undef,
180 };
181  
182 # arguments for Temporary fasta files
183 1 ▶ my $alitemp_args = {
Branch: 2 branches
BranchTrueFalseTotal
$self->debug_mode ? : 0 1 1
$self->consider_X ? : 0 1 1
184 degap => 0,
185 persistent => ($self->debug_mode) ? 1 : 0,
186 gapify => ($self->consider_X) ? 'X' : '*', # if consider_X changing MISS Char to X ...
187 clean => 1,
188 };
189
190 # Creation of global profile
191 1 my $hmmer;
192 1 ▶ unless ($self->perseq_profile) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->perseq_profile) 1 0 1
193 ### Global profile ...
194 1 $hmmer = Bio::MUST::Drivers::Hmmer::Model::Temporary->new(
195 seqs => [$ali_model->all_seqs],
196 model_args => $model_args,
197 args => $alitemp_args,
198 );
199 }
200
201 SEQ:
202 1 for my $seq ($ali->all_seqs) {
203 ##### [HMMCLEANER] actual sequence : $seq->foreign_id
204  
205 # Creation of perseq profile
206 1 ▶ if ($self->perseq_profile) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->perseq_profile) 0 1 1
207 ### Perseq profile ...
208 0 my $lookup = $self->ali_model->new_lookup;
209 0 my @new = map { $_->full_id } grep { $_->full_id ne $seq->full_id } $self->ali_model->all_seqs;
210 0 my $list = IdList->new( ids => \@new);
211  
212 # ali without current seq
213 0 my $shorted_ali = $list->reordered_ali($self->ali_model, $lookup);
214
215 0 $hmmer = Bio::MUST::Drivers::Hmmer::Model::Temporary->new(
216 seqs => [ $shorted_ali->all_seqs ],
217 model_args => $model_args,
218 args => $alitemp_args,
219 );
220 }
221  
222 ##### [HMMCLEANER] HMM driver : $hmmer->model->filename
223
224 # Creation of process
225 1 my $process = Process->new(
226 'ali' => $self->ali,
227 'seq' => $seq,
228 'model' => $hmmer,
229 'consider_X' => $self->consider_X,
230 'debug_mode' => $self->debug_mode,
231 );
232  
233 1 my $score = $process->score;
234 0 undef $process;
235  
236 0 my $cleaner = Cleaner->new(
237 'seq' => $seq,
238 'score' => $score,
239 'consider_X' => $self->consider_X,
240 'costs' => $self->costs,
241 'threshold' => $self->threshold,
242 'delchar' => $self->delchar,
243 'is_protein' => $self->ali->is_protein,
244 );
245
246 0 push @cleaners, $cleaner;
247  
248 0 ▶ $hmmer->model->remove if ($self->perseq_profile && !$self->debug_mode); # if debug
Branch: 1 branch
BranchTrueFalseTotal
if $self->perseq_profile and not $self->debug_mode 0 0 0
Condition0%0/3
$self->perseq_profile and not $self->debug_mode
!ll&&!rl&&r
000
MC/DC0%0/2
$self->perseq_profile and not $self->debug_mode
$self->perseq_profile not $self->debug_mode
Truth table0%0/3
A and not(B)
A= $self->perseq_profileB= not $self->debug_mode
ABresult
0X0
100
111
249 }
250
251 ##### [HMMCLEANER] end of building cleaners...
252  
253 0 ▶ $hmmer->model->remove unless ($self->debug_mode); # if debug
Branch: 1 branch
BranchTrueFalseTotal
unless $self->debug_mode 0 0 0
254  
255 0 return \@cleaners;
256 }
257  
258 # PRIVATE SUB
259  
260 sub _get_default_cost {
261 ###### [HMMCLEANER] building cost...
262 1 ▶ my $self = shift;
Subroutine _get_default_cost: called CC=1 SCAR=0.0
263
264 # need condition to enable non defaults values
265 # if set, builder is not activated so ok
266 1 my $costs = [ -0.15, -0.08, 0.15, 0.45 ];
267
268 ###### [HMMCLEANER] end of build cost...
269 1 return $costs;
270 }
271  
272 # METHOD
273  
274 sub get_results {
275 0 ▶ my $self = shift;
Subroutine get_results: not called CC=2 SCAR=17.9
Pod undocumented
276  
277 0 my $results;
278 0 for my $cleaner ($self->all_cleaners) {
279 0 push @$results, $cleaner->get_result;
280 }
281  
282 0 return $results
283 }
284  
285 sub get_result_ali {
286 0 ▶ my $self = shift;
Subroutine get_result_ali: not called CC=3 SCAR=24.8
Pod undocumented
287  
288 0 ▶ unless ($self->changeID) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->changeID) { } 0 0 0
289 0 return Ali->new( seqs => $self->get_results );
290 } else {
291 0 my $seqs = $self->get_results;
292 0 for my $seq (@$seqs) {
293 0 $seq->_set_full_id($seq->full_id.'_hmmcleaned');
294 };
295
296 0 return Ali->new( seqs => $seqs );
297 }
298 }
299  
300 sub store_results {
301 0 ▶ my $self = shift;
Subroutine store_results: not called CC=2 SCAR=17.9
Pod undocumented
302
303 0 my $ali = $self->get_result_ali;
304 0 ▶ if ($self->outfile_type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->outfile_type) { } 0 0 0
305 0 $ali->store($self->outfile.'.ali');
306 } else {
307 0 $ali->store_fasta($self->outfile.'.fasta');
308 }
309
310 0 return;
311 }
312  
313 # output file with alignment of degap seq, score and result
314 sub store_score {
315 0 ▶ my $self = shift;
Subroutine store_score: not called CC=2 SCAR=17.9
Pod undocumented
316
317 0 my $outscore = file($self->outfile.'.score');
318 0 my $score_fh = $outscore->openw;
319  
320 0 for my $cleaner ($self->all_cleaners) {
321 # Writing into score file
322 0 say {$score_fh} $cleaner->seq->full_id."\t".$cleaner->nogap_seq->seq;
323 0 say {$score_fh} (" " x length($cleaner->seq->full_id) )."\t".$cleaner->score;
324 0 say {$score_fh} (" " x length($cleaner->seq->full_id) )."\t".$cleaner->get_nogap_result->seq."\n";
325 }
326  
327 0 return;
328 }
329  
330 # Writing log at the same time, listings of cleaned blocks foreach seq
331 sub store_log {
332 0 ▶ my $self = shift;
Subroutine store_log: not called CC=3 SCAR=24.8
Pod undocumented
333
334 0 my $outlog = file($self->outfile.'.log');
335 0 my $log_fh = $outlog->openw;
336  
337 0 for my $cleaner ($self->all_cleaners) {
338 0 my $seq = $cleaner->seq;
339 # erased will be the number of removed positions
340 0 my $erased = 0;
341  
342 0 my $shifts_withgaps = $cleaner->shifts;
343 0 say {$log_fh} $seq->full_id;
344 0 ▶ for(my $i = 0; $i < $#$shifts_withgaps; $i+=2){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $shifts_withgaps->$#*) 0 0 0
345 0 $erased += $$shifts_withgaps[$i+1] - $$shifts_withgaps[$i];
346 0 say {$log_fh} "\t".( 1+$$shifts_withgaps[$i] )."-".( $$shifts_withgaps[$i+1] );
347 }
348
349 ##### Number of removed positions : $erased
350  
351 0 say $self->ali->file."\t".$seq->full_id."\t".$erased;
352 }
353
354 0 return;
355 }
356  
357 # get log for simulation from non gap seq
358 sub get_log_simu {
359 0 ▶ my $self = shift;
Subroutine get_log_simu: not called CC=3 SCAR=24.8
Pod undocumented
360  
361 0 my %log;
362 0 for my $cleaner ($self->all_cleaners) {
363 0 my $seq = $cleaner->seq;
364
365 0 my $shifts = $cleaner->nogap_shifts;
366 0 ▶ for(my $i = 0; $i < $#$shifts; $i+=2){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $shifts->$#*) 0 0 0
367
368 0 push @{$log{$self->ali->file}->{$seq->full_id}}, [(1+$$shifts[$i] ), ( $$shifts[$i+1] )];
369 }
370
371 }
372
373 0 return \%log;
374 }
375  
376 # get log for simulation from gap seq
377 sub get_log_simualign {
378 0 ▶ my $self = shift;
Subroutine get_log_simualign: not called CC=3 SCAR=24.8
Pod undocumented
379  
380 0 my %log;
381 0 for my $cleaner ($self->all_cleaners) {
382 0 my $seq = $cleaner->seq;
383
384 0 my $shifts_withgaps = $cleaner->shifts;
385 0 ▶ for(my $i = 0; $i < $#$shifts_withgaps; $i+=2){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $shifts_withgaps->$#*) 0 0 0
386
387 0 push @{$log{$self->ali->file}->{$seq->full_id}}, [(1+$$shifts_withgaps[$i] ), ( $$shifts_withgaps[$i+1] )];
388 }
389
390 }
391
392 0 return \%log;
393 }
394  
395 sub get_matrix_seqmask {
396 0 ▶ my $self = shift;
Subroutine get_matrix_seqmask: not called CC=2 SCAR=17.9
Pod undocumented
397  
398 0 my %matrix;
399 0 for my $cleaner ($self->all_cleaners) {
400 0 my $seqid = $cleaner->seq->full_id;
401 0 $matrix{$seqid} = $cleaner->get_seqmask;
402 }
403
404 0 return \%matrix;
405 }
406  
407 sub update_cleaners {
408 0 ▶ my $self = shift;
Subroutine update_cleaners: not called CC=3 SCAR=24.8
Pod undocumented
409 0 my $threshold = shift;
410 0 $self->_set_threshold($threshold);
411 0 ▶ my $costs = shift // $self->_get_default_cost;
Condition0%0/3
shift() // $self->_get_default_cost
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() // $self->_get_default_cost
shift() $self->_get_default_cost
Truth table0%0/3
A // B
A= shift()B= $self->_get_default_cost
ABresult
1X1
011
000
412  
413 0 for my $cleaner ($self->all_cleaners) {
414 0 $cleaner->update($threshold,$costs);
415 }
416  
417 0 return;
418 }
419  
420 sub store_all {
421 0 ▶ my $self = shift;
Subroutine store_all: not called CC=1 SCAR=6.9
Pod undocumented
422  
423 0 $self->store_results;
424 0 $self->store_score;
425 0 $self->store_log;
426  
427 0 return;
428 }
429  
430 3 __PACKAGE__->meta->make_immutable;
431 3 1;
432  
433 __END__
434  
435 =pod
436  
437 =encoding UTF-8
438  
439 =head1 NAME
440  
441 Bio::MUST::Apps::HmmCleaner - Main class for HmmCleaner
442  
443 =head1 VERSION
444  
445 version 0.180750
446  
447 =head1 AUTHOR
448  
449 Arnaud Di Franco <arnaud.difranco@gmail.fr>
450  
451 =head1 COPYRIGHT AND LICENSE
452  
453 This software is copyright (c) 2018 by Arnaud Di Franco.
454  
455 This is free software; you can redistribute it and/or modify it under
456 the same terms as the Perl 5 programming language system itself.
457  
458 =cut
« script/frameshiftSimul_final.pl ↑ Summary lib/Bio/MUST/Apps/HmmCleaner/Cleaner.pm »