lib/App/Sandy/Quality.pm

filter: Statementstmt 100% 3 / 3 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 1 / 1 Podpod n/a totaltotal 100% 4 / 4 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
CRAP 1.0
« lib/App/Sandy/PieceTable.pm ↑ Summary lib/App/Sandy/RNG.pm »
1 package App::Sandy::Quality;
2 # ABSTRACT: Class to simulate quality entries
3  
4 1 ▶use App::Sandy::Base 'class';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 with 'App::Sandy::Role::Counter';
7  
8 our $VERSION = '0.25'; # VERSION
9  
10 has 'quality_profile' => (
11 is => 'ro',
12 isa => 'My:QualityP',
13 required => 1,
14 coerce => 1
15 );
16  
17 has '_quality_by_system' => (
18 traits => ['Hash'],
19 is => 'ro',
20 isa => 'My:QualityH',
21 builder => '_build_quality_by_system',
22 lazy_build => 1,
23 handles => {
24 _get_quality_by_system => 'get'
25 }
26 );
27  
28 has '_gen_quality' => (
29 traits => ['Code'],
30 is => 'ro',
31 isa => 'CodeRef',
32 builder => '_build_gen_quality',
33 lazy_build => 1,
34 handles => {
35 gen_quality => 'execute'
36 }
37 );
38  
39 has '_phred_score' => (
40 traits => ['Array'],
41 isa => 'ro',
42 isa => 'ArrayRef',
43 builder => '_build_phred_score',
44 handles => {
45 _count_phred_score => 'count',
46 _get_phred_score => 'get'
47 }
48 );
49  
50 sub BUILD {
51 my $self = shift;
52 ## Just to ensure that the lazy attributes are built before &new returns
53 $self->_quality_by_system if $self->quality_profile ne 'poisson';
54 }
55  
56 sub _build_gen_quality {
57 my $self = shift;
58 my $fun;
59  
60 if ($self->quality_profile eq 'poisson') {
61 $fun = sub { $self->_gen_quality_by_poisson_dist(@_) };
62 } else {
63 $fun = sub { $self->_gen_quality_by_system(@_) };
64 }
65  
66 return $fun;
67 }
68  
69 sub _build_phred_score {
70 my $self = shift;
71  
72 my @phred_score = (
73 {
74 score => ['I', 'H', 'G', 'F', 'E', 'D', 'C', 'B', 'A', '@', '?'],
75 size => 11,
76 ratio => 1.5
77 },
78 {
79 score => ['>', '=', '<', ';', ':', '9', '8', '7', '6', '5'],
80 size => 10,
81 ratio => 2
82 },
83 {
84 score => ['4', '3', '2', '1', '0', '/', '.', '-', ',', '+'],
85 size => 10,
86 ratio => 2
87 },
88 {
89 score => ['*', ')', '(', '\'', '&', '%', '$', '#', '"', '!'],
90 size => 10,
91 ratio => 1
92 }
93 );
94  
95 return \@phred_score;
96 }
97  
98 sub _build_quality_by_system {
99 my $self = shift;
100 my $db = App::Sandy::DB::Handle::Quality->new;
101 my ($matrix, $deepth, $partil) = $db->retrievedb($self->quality_profile);
102 return {
103 matrix => $matrix,
104 deepth => $deepth,
105 partil => $partil
106 };
107 }
108  
109 sub _gen_quality_by_system {
110 my ($self, $read_size, $rng) = @_;
111  
112 my ($matrix, $deepth, $partil) = $self->_get_quality_by_system(
113 qw/matrix deepth partil/
114 );
115  
116 my ($bin, $left);
117  
118 # To make this routine more robust.
119 # It is necessary to work on reads
120 # lesser than read_size
121 if ($read_size < $partil) {
122 $partil = $read_size;
123 $bin = 1;
124 $left = 0;
125 } else {
126 $bin = int($read_size / $partil);
127 $left = $read_size % $partil;
128 }
129  
130 my $pick_again = $self->with_make_counter($read_size - $left, $left, $rng);
131 my $quality;
132  
133 for (my $i = 0; $i < $partil; $i++) {
134 for (my $j = 0; $j < $bin; $j++) {
135 $quality .= $matrix->[$i][$rng->get_n($deepth)];
136 if ($pick_again->()) {
137 $quality .= $matrix->[$i][$rng->get_n($deepth)];
138 }
139 }
140 }
141  
142 return \$quality;
143 }
144  
145 sub _gen_quality_by_poisson_dist {
146 my ($self, $read_size, $rng) = @_;
147 my $quality;
148 return $self->_poisson_dist(\$quality, $read_size, $self->_count_phred_score, $rng);
149 }
150  
151 sub _poisson_dist {
152 my ($self, $quality_ref, $size, $countdown, $rng) = @_;
153 return $quality_ref if not $countdown;
154  
155 my $phred_score = $self->_get_phred_score($self->_count_phred_score - $countdown);
156 my $part = int($size / $phred_score->{ratio}) + ($size % $phred_score->{ratio});
157  
158 for (my $i = 0; $i < $part; $i++) {
159 $$quality_ref .= $phred_score->{score}[$rng->get_n($phred_score->{size})];
160 }
161  
162 return $self->_poisson_dist($quality_ref, $size - $part, $countdown - 1, $rng);
163 }
164  
165 __END__
« lib/App/Sandy/PieceTable.pm ↑ Summary lib/App/Sandy/RNG.pm »