File Coverage

lib/App/Sandy/Role/Digest.pm
Criterion Covered Total %
statement 33 235 14.0
branch 0 118 0.0
condition 0 30 0.0
subroutine 11 18 61.1
pod 0 3 0.0
total 44 404 10.8


line stmt bran cond sub pod time code
1             package App::Sandy::Role::Digest;
2             # ABSTRACT: Wrapper on Simulator class for genome/transcriptome sequencing
3              
4 1     1   594 use App::Sandy::Base 'role';
  1         2  
  1         8  
5 1     1   8 use App::Sandy::DB::Handle::Quality;
  1         2  
  1         25  
6 1     1   5 use App::Sandy::DB::Handle::Expression;
  1         2  
  1         20  
7 1     1   4 use App::Sandy::DB::Handle::Variation;
  1         2  
  1         20  
8 1     1   5 use App::Sandy::Seq::SingleEnd;
  1         2  
  1         31  
9 1     1   6 use App::Sandy::Seq::PairedEnd;
  1         1  
  1         22  
10 1     1   5 use App::Sandy::Simulator;
  1         2  
  1         32  
11 1     1   7 use Path::Class 'file';
  1         2  
  1         59  
12 1     1   6 use File::Path 'make_path';
  1         2  
  1         67  
13 1     1   7 use List::Util 'uniq';
  1         2  
  1         131  
14              
15             requires qw/default_opt opt_spec rm_opt/;
16              
17             our $VERSION = '0.22'; # VERSION
18              
19             use constant {
20 1         3819 COUNT_LOOPS_BY_OPT => ['coverage', 'number-of-reads'],
21             STRAND_BIAS_OPT => ['random', 'plus', 'minus'],
22             SEQID_WEIGHT_OPT => ['length', 'same', 'count'],
23             SEQUENCING_TYPE_OPT => ['single-end', 'paired-end'],
24             OUTPUT_FORMAT_OPT => ['fastq', 'fastq.gz', 'sam', 'bam']
25 1     1   7 };
  1         1  
26              
27             override 'opt_spec' => sub {
28             my $self = shift;
29             my @rm_opt = $self->rm_opt;
30              
31             my %all_opt = (
32             'seed' => 'seed|s=i',
33             'prefix' => 'prefix|p=s',
34             'id' => 'id|I=s',
35             'append-id' => 'append-id|i=s',
36             'output-format' => 'output-format|O=s',
37             'compression-level' => 'compression-level|x=i',
38             'join-paired-ends' => 'join-paired-ends|1',
39             'verbose' => 'verbose|v',
40             'output-dir' => 'output-dir|o=s',
41             'jobs' => 'jobs|j=i',
42             'coverage' => 'coverage|c=f',
43             'read-mean' => 'read-mean|m=i',
44             'read-stdd' => 'read-stdd|d=i',
45             'fragment-mean' => 'fragment-mean|M=i',
46             'fragment-stdd' => 'fragment-stdd|D=i',
47             'sequencing-error' => 'sequencing-error|e=f',
48             'sequencing-type' => 'sequencing-type|t=s',
49             'quality-profile' => 'quality-profile|q=s',
50             'strand-bias' => 'strand-bias|b=s',
51             'seqid-weight' => 'seqid-weight|w=s',
52             'number-of-reads' => 'number-of-reads|n=i',
53             'expression-matrix' => 'expression-matrix|f=s',
54             'genomic-variation' => 'genomic-variation|a=s@',
55             'genomic-variation-regex' => 'genomic-variation-regex|A=s@'
56             );
57              
58             for my $opt (@rm_opt) {
59             delete $all_opt{$opt} if exists $all_opt{$opt};
60             }
61              
62             return super, values %all_opt;
63             };
64              
65             sub _log_msg_opt {
66 0     0     my ($self, $opts) = @_;
67 0           while (my ($key, $value) = each %$opts) {
68 0 0         next if ref($value) =~ /Seq/;
69 0 0         next if $key eq 'argv';
70 0 0         next if not defined $value;
71              
72 0           $key =~ s/_/ /g;
73              
74 0 0         if (ref $value eq 'ARRAY') {
75 0           $value = join ', ' => @$value;
76             }
77              
78 0           log_msg " => $key $value";
79             }
80             }
81              
82             sub _quality_profile_report {
83 0     0     state $report = App::Sandy::DB::Handle::Quality->new->make_report;
84 0           return $report;
85             }
86              
87             sub _expression_matrix_report {
88 0     0     state $report = App::Sandy::DB::Handle::Expression->new->make_report;
89 0           return $report;
90             }
91              
92             sub _genomic_variation_report {
93 0     0     state $report = App::Sandy::DB::Handle::Variation->new->make_report;
94 0           return $report;
95             }
96              
97             sub validate_args {
98 0     0 0   my ($self, $args) = @_;
99 0           my $fasta_file = shift @$args;
100              
101             # Mandatory fasta file
102 0 0         if (not defined $fasta_file) {
103 0           die "Missing fasta file\n";
104             }
105              
106             # Is it really a file?
107 0 0         if (not -f $fasta_file) {
108 0           die "<$fasta_file> is not a file. Please, give me a valid fasta file\n";
109             }
110              
111             # Check the file extension: fasta, fa, fna, ffn followed, or not, by .gz
112 0 0         if ($fasta_file !~ /.+\.(fasta|fa|fna|ffn)(\.gz)?$/) {
113 0           die "<$fasta_file> does not seem to be a fasta file. Please check the file extension\n";
114             }
115              
116 0 0         die "Too many arguments: '@$args'\n" if @$args;
117             }
118              
119             sub validate_opts {
120 0     0 0   my ($self, $opts) = @_;
121 0           my $progname = $self->progname;
122 0           my %default_opt = $self->default_opt;
123 0           $self->fill_opts($opts, \%default_opt);
124              
125             # Possible alternatives
126 0           my %STRAND_BIAS = map { $_ => 1 } @{ &STRAND_BIAS_OPT };
  0            
  0            
127 0           my %SEQID_WEIGHT = map { $_ => 1 } @{ &SEQID_WEIGHT_OPT };
  0            
  0            
128 0           my %SEQUENCING_TYPE = map { $_ => 1 } @{ &SEQUENCING_TYPE_OPT };
  0            
  0            
129 0           my %COUNT_LOOPS_BY = map { $_ => 1 } @{ &COUNT_LOOPS_BY_OPT };
  0            
  0            
130 0           my %OUTPUT_FORMAT = map { $_ => 1 } @{ &OUTPUT_FORMAT_OPT };
  0            
  0            
131 0           my %QUALITY_PROFILE = %{ $self->_quality_profile_report };
  0            
132 0           my %EXPRESSION_MATRIX = %{ $self->_expression_matrix_report };
  0            
133 0           my %STRUCTURAL_VARIATION = %{ $self->_genomic_variation_report };
  0            
134              
135             # prefix
136 0 0         if ($opts->{prefix} =~ /([\/\\])/) {
137 0           die "Invalid character in 'prefix' option: $opts->{prefix} => '$1'\n";
138             }
139              
140             # jobs > 0
141 0 0         if ($opts->{jobs} <= 0) {
142 0           die "Option 'jobs' requires an integer greater than zero, not $opts->{jobs}\n";
143             }
144              
145             # quality_profile
146             # If the quality_profile is 'poisson', then check the read-mean and rad-stdd.
147             # Else look for the quality-profile into the database
148 0 0         if ($opts->{'quality-profile'} eq 'poisson') {
149 0 0         if (0 >= $opts->{'read-mean'}) {
150 0           die "Option 'read-mean' requires an integer greater than zero, not $opts->{'read-mean'}\n";
151             }
152              
153 0 0         if (0 > $opts->{'read-stdd'}) {
154 0           die "Option 'read-stdd' requires an integer greater or equal to zero, not $opts->{'read-stdd'}\n";
155             }
156              
157             # 0 <= sequencing_error <= 1
158 0 0 0       if (0 > $opts->{'sequencing-error'} || $opts->{'sequencing-error'} > 1) {
159 0           die "Option 'sequencing-error' requires a value between zero and one, not $opts->{'sequencing-error'}\n";
160             }
161             } else {
162 0 0 0       if (%QUALITY_PROFILE && exists $QUALITY_PROFILE{$opts->{'quality-profile'}}) {
163 0           my $entry = $QUALITY_PROFILE{$opts->{'quality-profile'}};
164             # It is necessary for the next validations, so
165             # I set the opts read-size for the value that will be used
166             # afterwards
167 0           $opts->{'read-mean'} = $entry->{'mean'};
168 0           $opts->{'read-stdd'} = $entry->{'stdd'};
169 0           $opts->{'sequencing-error'} = $entry->{'error'};
170 0 0         $opts->{'sequencing-type'} = 'single-end' if $entry->{'type'} eq 'single-molecule';
171             } else {
172 0           die "Option quality-profile='$opts->{'quality-profile'}' does not exist into the database.\n",
173             "Please check '$progname quality' to see the available profiles or use '--quality-profile=poisson'\n";
174             }
175             }
176              
177             # genomic-variation
178 0 0         if (exists $opts->{'genomic-variation'}) {
179 0           for my $sv (split(/,/ => join(',', @{ $opts->{'genomic-variation'} }))) {
  0            
180 0 0 0       unless (%STRUCTURAL_VARIATION && exists $STRUCTURAL_VARIATION{$sv}) {
181 0           die "Option genomic-variation='$sv' does not exist into the database.\n",
182             "Please check '$progname variation' to see the available genomic variations\n";
183             }
184             }
185             }
186              
187             # genomic-variation-regex
188 0 0         if (exists $opts->{'genomic-variation-regex'}) {
189 0           for my $sv_pattern (split(/,/ => join(',', @{ $opts->{'genomic-variation-regex'} }))) {
  0            
190 0           my $pattern = qr/$sv_pattern/;
191 0           my $fail = 1;
192 0           for my $sv (keys %STRUCTURAL_VARIATION) {
193 0 0         if ($sv =~ /$pattern/) {
194 0           $fail = 0;
195 0           last;
196             }
197             }
198              
199 0 0         if ($fail) {
200 0           die "Option genomic-variation-regex='$sv_pattern' does not exist into the database.\n",
201             "Please check '$progname variation' to see the available genomic variations\n";
202             }
203             }
204             }
205              
206             # strand_bias (STRAND_BIAS_OPT)
207 0 0         if (not exists $STRAND_BIAS{$opts->{'strand-bias'}}) {
208 0           my $opt = join ', ' => keys %STRAND_BIAS;
209 0           die "Option 'strand-bias' requires one of these arguments: $opt. Not $opts->{'strand-bias'}\n";
210             }
211              
212             # sequencing_type (SEQUENCING_TYPE_OPT)
213 0 0         if (not exists $SEQUENCING_TYPE{$opts->{'sequencing-type'}}) {
214 0           my $opt = join ', ' => keys %SEQUENCING_TYPE;
215 0           die "Option 'sequencing-type' requires one of these arguments: $opt not $opts->{'sequencing-type'}\n";
216             }
217              
218             ## Dependently validated arguments
219             # fragment_mean and fragment_stdd
220 0 0         if ($opts->{'sequencing-type'} eq 'paired-end') {
221             # fragment_mean > 0
222 0 0         if ($opts->{'fragment-mean'} <= 0) {
223 0           die "Option 'fragment-mean' requires an integer greater than zero, not $opts->{'fragment-mean'}\n";
224             }
225              
226             # fragment_stdd > 0
227 0 0         if ($opts->{'fragment-stdd'} < 0) {
228 0           die "Option 'fragment-stdd' requires an integer greater or equal to zero, not $opts->{'fragment-stdd'}\n";
229             }
230              
231             # (fragment_mean - fragment_stdd) >= read_mean + read_stdd
232 0 0         if (($opts->{'fragment-mean'} - $opts->{'fragment-stdd'}) < ($opts->{'read-mean'} + $opts->{'read-stdd'})) {
233             die "Option 'fragment-mean' minus 'fragment-stdd' requires a value greater or equal 'read-mean' plus 'read-stdd', not " .
234 0           ($opts->{'fragment-mean'} - $opts->{'fragment-stdd'}) . "\n";
235             }
236             }
237              
238             # count-loops-by (COUNT_LOOPS_BY_OPT). The default value is defined into the consuming class
239 0 0         if (not exists $COUNT_LOOPS_BY{$default_opt{'count-loops-by'}}) {
240 0           my $opt = join ', ' => keys %COUNT_LOOPS_BY;
241 0           die "The provider must define the default count-lopps-by: $opt, not $default_opt{'count-loops-by'}";
242             }
243              
244             # If default is 'coverage'
245 0 0         if ($default_opt{'count-loops-by'} eq 'coverage') {
246 0 0         if (not defined $opts->{coverage}) {
247 0           die "The provider must define the 'coverage' if count-loop-by = coverage";
248             }
249             }
250              
251 0 0 0       if (defined $opts->{coverage} && $opts->{coverage} <= 0) {
252 0           die "Option 'coverage' requires a value greater than zero, not $opts->{coverage}\n";
253             }
254              
255             # If default is 'number-of-reads'
256 0 0         if ($default_opt{'count-loops-by'} eq 'number-of-reads') {
257 0 0         if (not defined $opts->{'number-of-reads'}) {
258 0           die "The provider must define the 'number-of-reads' if count-loop-by = number-of-reads";
259             }
260             }
261              
262 0 0 0       if (defined $opts->{'number-of-reads'} && $opts->{'number-of-reads'} <= 0) {
263 0           die "Option 'number-of-reads' requires a value greater than zero, not $opts->{'number-of-reads'}\n";
264             }
265              
266 0 0         if (not exists $OUTPUT_FORMAT{$opts->{'output-format'}}) {
267 0           my $opt = join ', ' => keys %OUTPUT_FORMAT;
268 0           die "Option 'output-format' requires one of these arguments: $opt not $opts->{'output-format'}\n";
269             }
270              
271 0 0         if ($opts->{'compression-level'} !~ /^[1-9]$/) {
272 0           die "Option 'compression-level' requires an integer between 1-9, not $opts->{'compression-level'}\n";
273             }
274              
275             # seqid-weight (SEQID_WEIGHT_OPT)
276 0 0         if (not exists $SEQID_WEIGHT{$opts->{'seqid-weight'}}) {
277 0           my $opt = join ', ' => keys %SEQID_WEIGHT;
278 0           die "Option 'seqid-weight' requires one of these arguments: $opt not $opts->{'seqid_weight'}\n";
279             }
280              
281             # Now expression-matrix is an option
282 0 0         if ($opts->{'expression-matrix'}) {
283 0           $opts->{'seqid-weight'} = 'count';
284             }
285              
286             # seqid-weight eq 'count' requires an expression-matrix
287 0 0         if ($opts->{'seqid-weight'} eq 'count') {
288 0 0         if (not defined $opts->{'expression-matrix'}) {
289 0           die "Option 'expression-matrix' requires a database entry\n";
290             }
291              
292             # It is defined, but the entry exists?
293 0 0 0       unless (%EXPRESSION_MATRIX && exists $EXPRESSION_MATRIX{$opts->{'expression-matrix'}}) {
294 0           die "Option expression-matrix='$opts->{'expression-matrix'}' does not exist into the database.\n",
295             "Please check '$progname expression' to see the available matrices\n";
296             }
297             }
298             }
299              
300             sub execute {
301 0     0 0   my ($self, $opts, $args) = @_;
302 0           my $fasta_file = shift @$args;
303              
304 0           my %default_opt = $self->default_opt;
305 0           $self->fill_opts($opts, \%default_opt);
306              
307 0           my $report = $self->_quality_profile_report;
308 0           my $entry = $report->{$opts->{'quality-profile'}};
309              
310             # Set if user wants a verbose log
311 0           $LOG_VERBOSE = $opts->{verbose};
312              
313             # Override default 'count-loops-by'
314 0 0         if ($default_opt{'count-loops-by'} eq 'coverage') {
    0          
315 0 0         $opts->{'count-loops-by'} = 'number-of-reads' if exists $opts->{'number-of-reads'};
316             }
317             elsif ($default_opt{'count-loops-by'} eq 'number-of-reads') {
318 0 0         $opts->{'count-loops-by'} = 'coverage' if exists $opts->{'coverage'};
319             } else {
320 0           die "'count-lopps-by' must be defined"
321             }
322              
323             # Now expression-matrix is an option
324 0 0         if ($opts->{'expression-matrix'}) {
325 0           $opts->{'seqid-weight'} = 'count';
326             }
327              
328             # Override read-size if quality-profile comes from database
329 0 0         if ($opts->{'quality-profile'} ne 'poisson') {
330             # Override default or user-defined value
331 0           $opts->{'read-mean'} = $entry->{'mean'};
332 0           $opts->{'read-stdd'} = $entry->{'stdd'};
333 0           $opts->{'sequencing-error'} = $entry->{'error'};
334 0 0         $opts->{'sequencing-type'} = 'single-end' if $entry->{'type'} eq 'single-molecule';
335             }
336              
337             # Sequence identifier
338             $opts->{'id'} ||= $opts->{'sequencing-type'} eq 'paired-end'
339             ? $opts->{'paired-end-id'}
340 0 0 0       : $opts->{'single-end-id'};
341              
342             # Append extra id
343 0 0         $opts->{'id'} .= " $opts->{'append-id'}" if defined $opts->{'append-id'};
344              
345             # If bam, leave only the first field;
346 0 0         if ($opts->{'output-format'} =~ /^(bam|sam)$/) {
347 0           $opts->{'id'} = (split ' ' => $opts->{'id'})[0];
348             }
349              
350             # In this case, try to make simulation less redundant
351 0 0 0       if ($opts->{'output-format'} =~ /fastq/ && $opts->{'sequencing-type'} eq 'paired-end'
      0        
352             && $opts->{'join-paired-ends'}) {
353             # Try to guess if the user passed a char to distinguish single/paired-end reads
354 0 0         $opts->{'id'} =~ /%R/ || $opts->{'id'} =~ s/(\S+)/$1\/\%R/;
355             }
356              
357             # Structural Variation
358 0 0         if ($opts->{'genomic-variation'}) {
359 0           my @svs = split(/,/ => join(',', @{ $opts->{'genomic-variation'} }));
  0            
360 0           @svs = uniq sort @svs;
361 0           $opts->{'genomic-variation'} = \@svs;
362             }
363              
364             # Structural Variation Regex
365 0 0         if ($opts->{'genomic-variation-regex'}) {
366 0           my @sv_list = keys %{ $self->_genomic_variation_report };
  0            
367 0           my @sv_patterns = split(/,/ => join(',', @{ $opts->{'genomic-variation-regex'} }));
  0            
368 0           my @svs_rg;
369              
370 0           for my $sv_pattern (@sv_patterns) {
371 0           my $pattern = qr/$sv_pattern/;
372 0           for (@sv_list) {
373 0 0         if (/$pattern/) {
374 0           push @svs_rg => $_;
375             }
376             }
377             }
378              
379 0           my @svs;
380              
381 0 0         if ($opts->{'genomic-variation'}) {
382 0           push @svs => @{ $opts->{'genomic-variation'} };
  0            
383             }
384              
385 0           push @svs => @svs_rg;
386 0           @svs = uniq sort @svs;
387 0           $opts->{'genomic-variation'} = \@svs;
388             }
389              
390             # Create output directory if it not exist
391 0           make_path($opts->{'output-dir'}, {error => \my $err_list});
392 0           my $err_dir;
393 0 0         if (@$err_list) {
394 0           for (@$err_list) {
395 0           my ($dir, $message) = %$_;
396 0           $err_dir .= "Problem creating '$dir': $message\n";
397             }
398 0           die "$err_dir\n";
399             }
400              
401             # Concatenate output-dir to prefix
402 0           my $prefix = file($opts->{'output-dir'}, $opts->{prefix});
403 0           $opts->{prefix} = "$prefix";
404              
405             #-------------------------------------------------------------------------------
406             # Log presentation header
407             #-------------------------------------------------------------------------------
408 0           my $time_stamp = localtime;
409 0           my $progname = $self->progname;
410 0           my $argv = $self->argv;
411 0           log_msg <<"HEADER";
412             --------------------------------------------------------
413             $progname - $time_stamp
414             --------------------------------------------------------
415             :: Arguments passed by the user:
416             => '@$argv'
417             HEADER
418              
419             #-------------------------------------------------------------------------------
420             # Construct the Seq and Simulator classes
421             #-------------------------------------------------------------------------------
422             my %paired_end_param = (
423             template_id => $opts->{'id'},
424             format => $opts->{'output-format'},
425             quality_profile => $opts->{'quality-profile'},
426             sequencing_error => $opts->{'sequencing-error'},
427             read_mean => $opts->{'read-mean'},
428             read_stdd => $opts->{'read-stdd'},
429             fragment_mean => $opts->{'fragment-mean'},
430 0           fragment_stdd => $opts->{'fragment-stdd'}
431             );
432              
433             my %single_end_param = (
434             template_id => $opts->{'id'},
435             format => $opts->{'output-format'},
436             quality_profile => $opts->{'quality-profile'},
437             sequencing_error => $opts->{'sequencing-error'},
438             read_mean => $opts->{'read-mean'},
439 0           read_stdd => $opts->{'read-stdd'}
440             );
441              
442 0           my $seq;
443 0 0         if ($opts->{'sequencing-type'} eq 'paired-end') {
444 0           log_msg ":: Creating paired-end seq generator ...";
445 0           $self->_log_msg_opt(\%paired_end_param);
446 0           $seq = App::Sandy::Seq::PairedEnd->new(%paired_end_param);
447             } else {
448 0           log_msg ":: Creating single-end seq generator ...";
449 0           $self->_log_msg_opt(\%single_end_param);
450 0           $seq = App::Sandy::Seq::SingleEnd->new(%single_end_param);
451             }
452              
453             my %simulator_param = (
454             argv => $argv,
455             seq => $seq,
456             fasta_file => $fasta_file,
457             truncate => $entry->{'type'} && $entry->{'type'} eq 'single-molecule',
458             prefix => $opts->{'prefix'},
459             output_format => $opts->{'output-format'},
460             compression_level => $opts->{'compression-level'},
461             join_paired_ends => $opts->{'join-paired-ends'},
462             seed => $opts->{'seed'},
463             count_loops_by => $opts->{'count-loops-by'},
464             number_of_reads => $opts->{'number-of-reads'},
465             coverage => $opts->{'coverage'},
466             jobs => $opts->{'jobs'},
467             strand_bias => $opts->{'strand-bias'},
468             seqid_weight => $opts->{'seqid-weight'},
469             expression_matrix => $opts->{'expression-matrix'},
470 0   0       genomic_variation => $opts->{'genomic-variation'}
471             );
472              
473 0           my $simulator;
474 0           log_msg ":: Creating simulator ...";
475 0           $self->_log_msg_opt(\%simulator_param);
476 0           $simulator = App::Sandy::Simulator->new(%simulator_param);
477              
478             #-------------------------------------------------------------------------------
479             # Let's simulate it!
480             #-------------------------------------------------------------------------------
481 0           log_msg ":: Running simulation ...";
482 0           $simulator->run_simulation;
483              
484 0           log_msg ":: End simulation. So long, and thanks for all the fish!";
485             }
486              
487             __END__
488              
489             =pod
490              
491             =encoding UTF-8
492              
493             =head1 NAME
494              
495             App::Sandy::Role::Digest - Wrapper on Simulator class for genome/transcriptome sequencing
496              
497             =head1 VERSION
498              
499             version 0.22
500              
501             =head1 AUTHORS
502              
503             =over 4
504              
505             =item *
506              
507             Thiago L. A. Miller <tmiller@mochsl.org.br>
508              
509             =item *
510              
511             J. Leonel Buzzo <lbuzzo@mochsl.org.br>
512              
513             =item *
514              
515             Felipe R. C. dos Santos <fsantos@mochsl.org.br>
516              
517             =item *
518              
519             Helena B. Conceição <hconceicao@mochsl.org.br>
520              
521             =item *
522              
523             Gabriela Guardia <gguardia@mochsl.org.br>
524              
525             =item *
526              
527             Fernanda Orpinelli <forpinelli@mochsl.org.br>
528              
529             =item *
530              
531             Pedro A. F. Galante <pgalante@mochsl.org.br>
532              
533             =back
534              
535             =head1 COPYRIGHT AND LICENSE
536              
537             This software is Copyright (c) 2018 by Teaching and Research Institute from Sírio-Libanês Hospital.
538              
539             This is free software, licensed under:
540              
541             The GNU General Public License, Version 3, June 2007
542              
543             =cut