File Coverage

lib/App/Sandy/Seq.pm
Criterion Covered Total %
statement 34 62 54.8
branch 2 18 11.1
condition n/a
subroutine 12 17 70.5
pod 0 3 0.0
total 48 100 48.0


line stmt bran cond sub pod time code
1             package App::Sandy::Seq;
2             # ABSTRACT: Base class to simulate seq entries
3              
4 6     6   4459 use App::Sandy::Base 'class';
  6         17  
  6         47  
5 6     6   3408 use App::Sandy::Quality;
  6         2683  
  6         292  
6              
7 6     6   55 use constant NUM_TRIES => 1000;
  6         66  
  6         7890  
8              
9             with qw{
10             App::Sandy::Role::RunTimeTemplate
11             App::Sandy::Role::Template::Fastq
12             App::Sandy::Role::Template::Sam
13             App::Sandy::Role::RNorm
14             };
15              
16             our $VERSION = '0.22'; # VERSION
17              
18             has 'format' => (
19             is => 'ro',
20             isa => 'My:Format',
21             required => 1
22             );
23              
24             has 'template_id' => (
25             is => 'ro',
26             isa => 'Str',
27             required => 1
28             );
29              
30             has 'sequencing_error' => (
31             is => 'ro',
32             isa => 'My:NumHS',
33             required => 1
34             );
35              
36             has 'quality_profile' => (
37             is => 'ro',
38             isa => 'My:QualityP',
39             required => 1,
40             coerce => 1
41             );
42              
43             has 'read_mean' => (
44             is => 'ro',
45             isa => 'My:IntGt0',
46             required => 1
47             );
48              
49             has 'read_stdd' => (
50             is => 'ro',
51             isa => 'My:IntGe0',
52             required => 1
53             );
54              
55             has '_template_id' => (
56             traits => ['Code'],
57             is => 'ro',
58             isa => 'CodeRef',
59             builder => '_build_template_id',
60             lazy_build => 1,
61             handles => {
62             _gen_id => 'execute'
63             }
64             );
65              
66             has '_template_seq' => (
67             traits => ['Code'],
68             is => 'ro',
69             isa => 'CodeRef',
70             builder => '_build_template_seq',
71             lazy_build => 1,
72             handles => {
73             _gen_seq => 'execute'
74             }
75             );
76              
77             has '_info' => (
78             traits => ['Hash'],
79             is => 'ro',
80             isa => 'HashRef[Str]',
81             builder => '_build_info',
82             lazy_build => 1,
83             handles => {
84             _set_info => 'set',
85             _get_info => 'get'
86             }
87             );
88              
89             has '_read_size' => (
90             traits => ['Code'],
91             is => 'ro',
92             isa => 'CodeRef',
93             builder => '_build_read_size',
94             lazy_build => 1,
95             handles => {
96             _get_read_size => 'execute'
97             }
98             );
99              
100             has '_quality' => (
101             is => 'ro',
102             isa => 'App::Sandy::Quality',
103             builder => '_build_quality',
104             lazy_build => 1,
105             handles => ['gen_quality']
106             );
107              
108             has '_sym_table' => (
109             is => 'ro',
110             isa => 'HashRef[Str]',
111             builder => '_build_sym_table',
112             lazy_build => 1
113             );
114              
115             sub BUILD {
116 60     60 0 125 my $self = shift;
117             ## Just to ensure that the lazy attributes are built before &new returns
118 60         1740 $self->_quality;
119             }
120              
121             sub _build_sym_table {
122 14     14   660 my $sym_table = {
123             '%q' => '$info->{quality_profile}',
124             '%m' => '$info->{read_mean}',
125             '%d' => '$info->{read_stdd}',
126             '%r' => '$info->{read_size}',
127             '%e' => '$info->{sequencing_error}',
128             '%c' => '$info->{seq_id}',
129             '%C' => '$info->{seq_id_type}',
130             '%a' => '$info->{start_ref}',
131             '%b' => '$info->{end_ref}',
132             '%t' => '$info->{start}',
133             '%n' => '$info->{end}',
134             '%i' => '$info->{instrument}',
135             '%I' => '$info->{id}',
136             '%R' => '$info->{read}',
137             '%U' => '$info->{num}',
138             '%s' => '$info->{strand}',
139             '%x' => '$info->{error}',
140             '%v' => '$info->{var}'
141             };
142              
143 14         238 return $sym_table;
144             }
145              
146             sub _build_template_id {
147 14     14   35 my $self = shift;
148 14         501 my $sym_table = $self->_sym_table;
149 14         617 return $self->with_compile_template($self->template_id, 'info', $sym_table);
150             }
151              
152             sub _build_template_seq {
153 29     29   54 my $self = shift;
154              
155 29         912 my $format = $self->format;
156 29         54 my $gen_seq;
157              
158 29 50       239 if ($format =~ /fastq/) {
    0          
    0          
159 29     2325   207 $gen_seq = sub { $self->with_fastq_template(@_) };
  2325         12381  
160             } elsif ($format eq 'sam') {
161 0     0   0 $gen_seq = sub { $self->with_sam_align_template(@_) };
  0         0  
162             } elsif ($format eq 'bam') {
163 0     0   0 $gen_seq = sub { $self->with_bam_align_template(@_) };
  0         0  
164             } else {
165 0         0 croak "No valid format: '$format'";
166             }
167              
168 29         960 return $gen_seq;
169             }
170              
171             sub _build_info {
172 14     14   685 my $self = shift;
173              
174 14         557 my $info = {
175             instrument => 'SR',
176             quality_profile => $self->quality_profile,
177             sequencing_error => $self->sequencing_error,
178             read_mean => $self->read_mean,
179             read_stdd => $self->read_stdd
180             };
181              
182 14         276 return $info;
183             }
184              
185             sub _build_read_size {
186 14     14   98 my $self = shift;
187 14         58 my $fun;
188              
189 14 50       640 if ($self->read_stdd == 0) {
190 14     1730   181 $fun = sub { $self->read_mean };
  1730         41963  
191             } else {
192             $fun = sub {
193 0     0   0 my $size = 0;
194 0         0 my $random_tries = 0;
195 0         0 until ($size > 0) {
196 0 0       0 if (++$random_tries > NUM_TRIES) {
197 0         0 croak "So many tries to calculate a seq size greater than zero ...";
198             }
199 0         0 $size = $self->with_random_half_normal($self->read_mean,
200             $self->read_stdd)
201             }
202 0         0 return $size;
203 0         0 };
204             }
205              
206 14         548 return $fun;
207             }
208              
209             sub _build_quality {
210 60     60   100 my $self = shift;
211 60         1640 App::Sandy::Quality->new(
212             quality_profile => $self->quality_profile
213             );
214             }
215              
216             sub gen_sam_header {
217 0     0 0   my ($self, $argv) = @_;
218 0           my $format = $self->format;
219 0           my $header;
220              
221 0 0         if ($format eq 'sam') {
    0          
222 0           $header = $self->with_sam_header_template($argv);
223             } elsif ($format eq 'bam') {
224 0           $header = $self->with_bam_header_template($argv);
225             } else {
226 0           croak "No valid format: '$format'";
227             }
228              
229 0           return $header;
230             }
231              
232             sub gen_eof_marker {
233 0     0 0   my ($self, $file) = @_;
234              
235 0 0         open my $fh, ">>" => $file
236             or croak "Cannot open '$file': $!";
237              
238 0           binmode $fh;
239              
240 0           my $eof_ref = $self->with_eof_marker;
241 0           print {$fh} $$eof_ref;
  0            
242              
243 0 0         close $fh
244             or croak "Cannot write to '$file': $!";
245             }
246              
247             __END__
248              
249             =pod
250              
251             =encoding UTF-8
252              
253             =head1 NAME
254              
255             App::Sandy::Seq - Base class to simulate seq entries
256              
257             =head1 VERSION
258              
259             version 0.22
260              
261             =head1 AUTHORS
262              
263             =over 4
264              
265             =item *
266              
267             Thiago L. A. Miller <tmiller@mochsl.org.br>
268              
269             =item *
270              
271             J. Leonel Buzzo <lbuzzo@mochsl.org.br>
272              
273             =item *
274              
275             Felipe R. C. dos Santos <fsantos@mochsl.org.br>
276              
277             =item *
278              
279             Helena B. Conceição <hconceicao@mochsl.org.br>
280              
281             =item *
282              
283             Gabriela Guardia <gguardia@mochsl.org.br>
284              
285             =item *
286              
287             Fernanda Orpinelli <forpinelli@mochsl.org.br>
288              
289             =item *
290              
291             Pedro A. F. Galante <pgalante@mochsl.org.br>
292              
293             =back
294              
295             =head1 COPYRIGHT AND LICENSE
296              
297             This software is Copyright (c) 2018 by Teaching and Research Institute from Sírio-Libanês Hospital.
298              
299             This is free software, licensed under:
300              
301             The GNU General Public License, Version 3, June 2007
302              
303             =cut