File Coverage

lib/App/Sandy/Quality.pm
Criterion Covered Total %
statement 27 50 54.0
branch 4 10 40.0
condition n/a
subroutine 8 11 72.7
pod 0 1 0.0
total 39 72 54.1


line stmt bran cond sub pod time code
1             package App::Sandy::Quality;
2             # ABSTRACT: Class to simulate quality entries
3              
4 6     6   47 use App::Sandy::Base 'class';
  6         12  
  6         37  
5 6     6   3549 use App::Sandy::DB::Handle::Quality;
  6         2481  
  6         5767  
6              
7             with 'App::Sandy::Role::Counter';
8              
9             our $VERSION = '0.22'; # VERSION
10              
11             has 'quality_profile' => (
12             is => 'ro',
13             isa => 'My:QualityP',
14             required => 1,
15             coerce => 1
16             );
17              
18             has '_quality_by_system' => (
19             traits => ['Hash'],
20             is => 'ro',
21             isa => 'My:QualityH',
22             builder => '_build_quality_by_system',
23             lazy_build => 1,
24             handles => {
25             _get_quality_by_system => 'get'
26             }
27             );
28              
29             has '_gen_quality' => (
30             traits => ['Code'],
31             is => 'ro',
32             isa => 'CodeRef',
33             builder => '_build_gen_quality',
34             lazy_build => 1,
35             handles => {
36             gen_quality => 'execute'
37             }
38             );
39              
40             has '_phred_score' => (
41             traits => ['Array'],
42             isa => 'ro',
43             isa => 'ArrayRef',
44             builder => '_build_phred_score',
45             handles => {
46             _count_phred_score => 'count',
47             _get_phred_score => 'get'
48             }
49             );
50              
51             sub BUILD {
52 70     70 0 130 my $self = shift;
53             ## Just to ensure that the lazy attributes are built before &new returns
54 70 50       1955 $self->_quality_by_system if $self->quality_profile ne 'poisson';
55             }
56              
57             sub _build_gen_quality {
58 19     19   50 my $self = shift;
59 19         29 my $fun;
60              
61 19 50       653 if ($self->quality_profile eq 'poisson') {
62 19     2360   181 $fun = sub { $self->_gen_quality_by_poisson_dist(@_) };
  2360         6276  
63             } else {
64 0     0   0 $fun = sub { $self->_gen_quality_by_system(@_) };
  0         0  
65             }
66              
67 19         657 return $fun;
68             }
69              
70             sub _build_phred_score {
71 70     70   120 my $self = shift;
72              
73 70         1175 my @phred_score = (
74             {
75             score => ['I', 'H', 'G', 'F', 'E', 'D', 'C', 'B', 'A', '@', '?'],
76             size => 11,
77             ratio => 1.5
78             },
79             {
80             score => ['>', '=', '<', ';', ':', '9', '8', '7', '6', '5'],
81             size => 10,
82             ratio => 2
83             },
84             {
85             score => ['4', '3', '2', '1', '0', '/', '.', '-', ',', '+'],
86             size => 10,
87             ratio => 2
88             },
89             {
90             score => ['*', ')', '(', '\'', '&', '%', '$', '#', '"', '!'],
91             size => 10,
92             ratio => 1
93             }
94             );
95              
96 70         1930 return \@phred_score;
97             }
98              
99             sub _build_quality_by_system {
100 0     0   0 my $self = shift;
101 0         0 my $db = App::Sandy::DB::Handle::Quality->new;
102 0         0 my ($matrix, $deepth, $partil) = $db->retrievedb($self->quality_profile);
103             return {
104 0         0 matrix => $matrix,
105             deepth => $deepth,
106             partil => $partil
107             };
108             }
109              
110             sub _gen_quality_by_system {
111 0     0   0 my ($self, $read_size) = @_;
112              
113 0         0 my ($matrix, $deepth, $partil) = $self->_get_quality_by_system(
114             qw/matrix deepth partil/
115             );
116              
117 0         0 my ($bin, $left);
118              
119             # To make this routine more robust.
120             # It is necessary to work on reads
121             # lesser than read_size
122 0 0       0 if ($read_size < $partil) {
123 0         0 $partil = $read_size;
124 0         0 $bin = 1;
125 0         0 $left = 0;
126             } else {
127 0         0 $bin = int($read_size / $partil);
128 0         0 $left = $read_size % $partil;
129             }
130              
131 0         0 my $pick_again = $self->with_make_counter($read_size - $left, $left);
132 0         0 my $quality;
133              
134 0         0 for (my $i = 0; $i < $partil; $i++) {
135 0         0 for (my $j = 0; $j < $bin; $j++) {
136 0         0 $quality .= $matrix->[$i][int(rand($deepth))];
137 0 0       0 if ($pick_again->()) {
138 0         0 $quality .= $matrix->[$i][int(rand($deepth))];
139             }
140             }
141             }
142              
143 0         0 return \$quality;
144             }
145              
146             sub _gen_quality_by_poisson_dist {
147 2360     2360   4660 my ($self, $read_size) = @_;
148 2360         3783 my $quality;
149 2360         83388 return $self->_poisson_dist(\$quality, $read_size, $self->_count_phred_score);
150             }
151              
152             sub _poisson_dist {
153 11800     11800   23828 my ($self, $quality_ref, $size, $countdown) = @_;
154 11800 100       29525 return $quality_ref if not $countdown;
155              
156 9440         338683 my $phred_score = $self->_get_phred_score($self->_count_phred_score - $countdown);
157 9440         26324 my $part = int($size / $phred_score->{ratio}) + ($size % $phred_score->{ratio});
158              
159 9440         20506 for (my $i = 0; $i < $part; $i++) {
160 23600         62263 $$quality_ref .= $phred_score->{score}[int(rand($phred_score->{size}))];
161             }
162              
163 9440         24018 return $self->_poisson_dist($quality_ref, $size - $part, $countdown - 1);
164             }
165              
166             __END__
167              
168             =pod
169              
170             =encoding UTF-8
171              
172             =head1 NAME
173              
174             App::Sandy::Quality - Class to simulate quality entries
175              
176             =head1 VERSION
177              
178             version 0.22
179              
180             =head1 AUTHORS
181              
182             =over 4
183              
184             =item *
185              
186             Thiago L. A. Miller <tmiller@mochsl.org.br>
187              
188             =item *
189              
190             J. Leonel Buzzo <lbuzzo@mochsl.org.br>
191              
192             =item *
193              
194             Felipe R. C. dos Santos <fsantos@mochsl.org.br>
195              
196             =item *
197              
198             Helena B. Conceição <hconceicao@mochsl.org.br>
199              
200             =item *
201              
202             Gabriela Guardia <gguardia@mochsl.org.br>
203              
204             =item *
205              
206             Fernanda Orpinelli <forpinelli@mochsl.org.br>
207              
208             =item *
209              
210             Pedro A. F. Galante <pgalante@mochsl.org.br>
211              
212             =back
213              
214             =head1 COPYRIGHT AND LICENSE
215              
216             This software is Copyright (c) 2018 by Teaching and Research Institute from Sírio-Libanês Hospital.
217              
218             This is free software, licensed under:
219              
220             The GNU General Public License, Version 3, June 2007
221              
222             =cut