lib/App/Sandy/Role/Digest.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/Role/Counter.pm ↑ Summary lib/App/Sandy/Role/IO.pm »
1 package App::Sandy::Role::Digest;
2 # ABSTRACT: Wrapper on Simulator class for genome/transcriptome sequencing
3  
4 1 ▶use App::Sandy::Base 'role';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 use App::Sandy::DB::Handle::Quality;
6 use App::Sandy::DB::Handle::Expression;
7 use App::Sandy::DB::Handle::Variation;
8 use App::Sandy::Seq::SingleEnd;
9 use App::Sandy::Seq::PairedEnd;
10 use App::Sandy::Simulator;
11 use Path::Class 'file';
12 use File::Path 'make_path';
13 use List::Util 'uniq';
14  
15 requires qw/default_opt opt_spec rm_opt/;
16  
17 our $VERSION = '0.25'; # VERSION
18  
19 use constant {
20 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 };
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 my ($self, $opts) = @_;
67 while (my ($key, $value) = each %$opts) {
68 next if ref($value) =~ /Seq/;
69 next if $key eq 'argv';
70 next if not defined $value;
71  
72 $key =~ s/_/ /g;
73  
74 if (ref $value eq 'ARRAY') {
75 $value = join ', ' => @$value;
76 }
77  
78 log_msg " => $key $value";
79 }
80 }
81  
82 sub _quality_profile_report {
83 state $report = App::Sandy::DB::Handle::Quality->new->make_report;
84 return $report;
85 }
86  
87 sub _expression_matrix_report {
88 state $report = App::Sandy::DB::Handle::Expression->new->make_report;
89 return $report;
90 }
91  
92 sub _genomic_variation_report {
93 state $report = App::Sandy::DB::Handle::Variation->new->make_report;
94 return $report;
95 }
96  
97 sub validate_args {
98 my ($self, $args) = @_;
99 my $fasta_file = shift @$args;
100  
101 # Mandatory fasta file
102 if (not defined $fasta_file) {
103 die "Missing fasta file\n";
104 }
105  
106 # Is it really a file?
107 if (not -f $fasta_file) {
108 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 if ($fasta_file !~ /.+\.(fasta|fa|fna|ffn)(\.gz)?$/) {
113 die "<$fasta_file> does not seem to be a fasta file. Please check the file extension\n";
114 }
115  
116 die "Too many arguments: '@$args'\n" if @$args;
117 }
118  
119 sub validate_opts {
120 my ($self, $opts) = @_;
121 my $progname = $self->progname;
122 my %default_opt = $self->default_opt;
123 $self->fill_opts($opts, \%default_opt);
124  
125 # Possible alternatives
126 my %STRAND_BIAS = map { $_ => 1 } @{ &STRAND_BIAS_OPT };
127 my %SEQID_WEIGHT = map { $_ => 1 } @{ &SEQID_WEIGHT_OPT };
128 my %SEQUENCING_TYPE = map { $_ => 1 } @{ &SEQUENCING_TYPE_OPT };
129 my %COUNT_LOOPS_BY = map { $_ => 1 } @{ &COUNT_LOOPS_BY_OPT };
130 my %OUTPUT_FORMAT = map { $_ => 1 } @{ &OUTPUT_FORMAT_OPT };
131 my %QUALITY_PROFILE = %{ $self->_quality_profile_report };
132 my %EXPRESSION_MATRIX = %{ $self->_expression_matrix_report };
133 my %STRUCTURAL_VARIATION = %{ $self->_genomic_variation_report };
134  
135 # prefix
136 if ($opts->{prefix} =~ /([\/\\])/) {
137 die "Invalid character in 'prefix' option: $opts->{prefix} => '$1'\n";
138 }
139  
140 # jobs > 0
141 if ($opts->{jobs} <= 0) {
142 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 if ($opts->{'quality-profile'} eq 'poisson') {
149 if (0 >= $opts->{'read-mean'}) {
150 die "Option 'read-mean' requires an integer greater than zero, not $opts->{'read-mean'}\n";
151 }
152  
153 if (0 > $opts->{'read-stdd'}) {
154 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 if (0 > $opts->{'sequencing-error'} || $opts->{'sequencing-error'} > 1) {
159 die "Option 'sequencing-error' requires a value between zero and one, not $opts->{'sequencing-error'}\n";
160 }
161 } else {
162 if (%QUALITY_PROFILE && exists $QUALITY_PROFILE{$opts->{'quality-profile'}}) {
163 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 $opts->{'read-mean'} = $entry->{'mean'};
168 $opts->{'read-stdd'} = $entry->{'stdd'};
169 $opts->{'sequencing-error'} = $entry->{'error'};
170 $opts->{'sequencing-type'} = 'single-end' if $entry->{'type'} eq 'single-molecule';
171 } else {
172 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 if (exists $opts->{'genomic-variation'}) {
179 for my $sv (split(/,/ => join(',', @{ $opts->{'genomic-variation'} }))) {
180 unless (%STRUCTURAL_VARIATION && exists $STRUCTURAL_VARIATION{$sv}) {
181 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 if (exists $opts->{'genomic-variation-regex'}) {
189 for my $sv_pattern (split(/,/ => join(',', @{ $opts->{'genomic-variation-regex'} }))) {
190 my $pattern = qr/$sv_pattern/;
191 my $fail = 1;
192 for my $sv (keys %STRUCTURAL_VARIATION) {
193 if ($sv =~ /$pattern/) {
194 $fail = 0;
195 last;
196 }
197 }
198  
199 if ($fail) {
200 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 if (not exists $STRAND_BIAS{$opts->{'strand-bias'}}) {
208 my $opt = join ', ' => keys %STRAND_BIAS;
209 die "Option 'strand-bias' requires one of these arguments: $opt. Not $opts->{'strand-bias'}\n";
210 }
211  
212 # sequencing_type (SEQUENCING_TYPE_OPT)
213 if (not exists $SEQUENCING_TYPE{$opts->{'sequencing-type'}}) {
214 my $opt = join ', ' => keys %SEQUENCING_TYPE;
215 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 if ($opts->{'sequencing-type'} eq 'paired-end') {
221 # fragment_mean > 0
222 if ($opts->{'fragment-mean'} <= 0) {
223 die "Option 'fragment-mean' requires an integer greater than zero, not $opts->{'fragment-mean'}\n";
224 }
225  
226 # fragment_stdd > 0
227 if ($opts->{'fragment-stdd'} < 0) {
228 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 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 ($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 if (not exists $COUNT_LOOPS_BY{$default_opt{'count-loops-by'}}) {
240 my $opt = join ', ' => keys %COUNT_LOOPS_BY;
241 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 if ($default_opt{'count-loops-by'} eq 'coverage') {
246 if (not defined $opts->{coverage}) {
247 die "The provider must define the 'coverage' if count-loop-by = coverage";
248 }
249 }
250  
251 if (defined $opts->{coverage} && $opts->{coverage} <= 0) {
252 die "Option 'coverage' requires a value greater than zero, not $opts->{coverage}\n";
253 }
254  
255 # If default is 'number-of-reads'
256 if ($default_opt{'count-loops-by'} eq 'number-of-reads') {
257 if (not defined $opts->{'number-of-reads'}) {
258 die "The provider must define the 'number-of-reads' if count-loop-by = number-of-reads";
259 }
260 }
261  
262 if (defined $opts->{'number-of-reads'} && $opts->{'number-of-reads'} <= 0) {
263 die "Option 'number-of-reads' requires a value greater than zero, not $opts->{'number-of-reads'}\n";
264 }
265  
266 if (not exists $OUTPUT_FORMAT{$opts->{'output-format'}}) {
267 my $opt = join ', ' => keys %OUTPUT_FORMAT;
268 die "Option 'output-format' requires one of these arguments: $opt not $opts->{'output-format'}\n";
269 }
270  
271 if ($opts->{'compression-level'} !~ /^[1-9]$/) {
272 die "Option 'compression-level' requires an integer between 1-9, not $opts->{'compression-level'}\n";
273 }
274  
275 # seqid-weight (SEQID_WEIGHT_OPT)
276 if (not exists $SEQID_WEIGHT{$opts->{'seqid-weight'}}) {
277 my $opt = join ', ' => keys %SEQID_WEIGHT;
278 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 if ($opts->{'expression-matrix'}) {
283 $opts->{'seqid-weight'} = 'count';
284 }
285  
286 # seqid-weight eq 'count' requires an expression-matrix
287 if ($opts->{'seqid-weight'} eq 'count') {
288 if (not defined $opts->{'expression-matrix'}) {
289 die "Option 'expression-matrix' requires a database entry\n";
290 }
291  
292 # It is defined, but the entry exists?
293 unless (%EXPRESSION_MATRIX && exists $EXPRESSION_MATRIX{$opts->{'expression-matrix'}}) {
294 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 my ($self, $opts, $args) = @_;
302 my $fasta_file = shift @$args;
303  
304 my %default_opt = $self->default_opt;
305 $self->fill_opts($opts, \%default_opt);
306  
307 my $report = $self->_quality_profile_report;
308 my $entry = $report->{$opts->{'quality-profile'}};
309  
310 # Set if user wants a verbose log
311 $LOG_VERBOSE = $opts->{verbose};
312  
313 # Override default 'count-loops-by'
314 if ($default_opt{'count-loops-by'} eq 'coverage') {
315 $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 $opts->{'count-loops-by'} = 'coverage' if exists $opts->{'coverage'};
319 } else {
320 die "'count-lopps-by' must be defined"
321 }
322  
323 # Now expression-matrix is an option
324 if ($opts->{'expression-matrix'}) {
325 $opts->{'seqid-weight'} = 'count';
326 }
327  
328 # Override read-size if quality-profile comes from database
329 if ($opts->{'quality-profile'} ne 'poisson') {
330 # Override default or user-defined value
331 $opts->{'read-mean'} = $entry->{'mean'};
332 $opts->{'read-stdd'} = $entry->{'stdd'};
333 $opts->{'sequencing-error'} = $entry->{'error'};
334 $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 : $opts->{'single-end-id'};
341  
342 # Append extra id
343 $opts->{'id'} .= " $opts->{'append-id'}" if defined $opts->{'append-id'};
344  
345 # If bam, leave only the first field;
346 if ($opts->{'output-format'} =~ /^(bam|sam)$/) {
347 $opts->{'id'} = (split ' ' => $opts->{'id'})[0];
348 }
349  
350 # In this case, try to make simulation less redundant
351 if ($opts->{'output-format'} =~ /fastq/ && $opts->{'sequencing-type'} eq 'paired-end'
352 && $opts->{'join-paired-ends'}) {
353 # Try to guess if the user passed a char to distinguish single/paired-end reads
354 $opts->{'id'} =~ /%R/ || $opts->{'id'} =~ s/(\S+)/$1\/\%R/;
355 }
356  
357 # Structural Variation
358 if ($opts->{'genomic-variation'}) {
359 my @svs = split(/,/ => join(',', @{ $opts->{'genomic-variation'} }));
360 @svs = uniq sort @svs;
361 $opts->{'genomic-variation'} = \@svs;
362 }
363  
364 # Structural Variation Regex
365 if ($opts->{'genomic-variation-regex'}) {
366 my @sv_list = keys %{ $self->_genomic_variation_report };
367 my @sv_patterns = split(/,/ => join(',', @{ $opts->{'genomic-variation-regex'} }));
368 my @svs_rg;
369  
370 for my $sv_pattern (@sv_patterns) {
371 my $pattern = qr/$sv_pattern/;
372 for (@sv_list) {
373 if (/$pattern/) {
374 push @svs_rg => $_;
375 }
376 }
377 }
378  
379 my @svs;
380  
381 if ($opts->{'genomic-variation'}) {
382 push @svs => @{ $opts->{'genomic-variation'} };
383 }
384  
385 push @svs => @svs_rg;
386 @svs = uniq sort @svs;
387 $opts->{'genomic-variation'} = \@svs;
388 }
389  
390 # Create output directory if it not exist
391 make_path($opts->{'output-dir'}, {error => \my $err_list});
392 my $err_dir;
393 if (@$err_list) {
394 for (@$err_list) {
395 my ($dir, $message) = %$_;
396 $err_dir .= "Problem creating '$dir': $message\n";
397 }
398 die "$err_dir\n";
399 }
400  
401 # Concatenate output-dir to prefix
402 my $prefix = file($opts->{'output-dir'}, $opts->{prefix});
403 $opts->{prefix} = "$prefix";
404  
405 #-------------------------------------------------------------------------------
406 # Log presentation header
407 #-------------------------------------------------------------------------------
408 my $time_stamp = localtime;
409 my $progname = $self->progname;
410 my $argv = $self->argv;
411 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 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 read_stdd => $opts->{'read-stdd'}
440 );
441  
442 my $seq;
443 if ($opts->{'sequencing-type'} eq 'paired-end') {
444 log_msg ":: Creating paired-end seq generator ...";
445 $self->_log_msg_opt(\%paired_end_param);
446 $seq = App::Sandy::Seq::PairedEnd->new(%paired_end_param);
447 } else {
448 log_msg ":: Creating single-end seq generator ...";
449 $self->_log_msg_opt(\%single_end_param);
450 $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 genomic_variation => $opts->{'genomic-variation'}
471 );
472  
473 my $simulator;
474 log_msg ":: Creating simulator ...";
475 $self->_log_msg_opt(\%simulator_param);
476 $simulator = App::Sandy::Simulator->new(%simulator_param);
477  
478 #-------------------------------------------------------------------------------
479 # Let's simulate it!
480 #-------------------------------------------------------------------------------
481 log_msg ":: Running simulation ...";
482 $simulator->run_simulation;
483  
484 log_msg ":: End simulation. So long, and thanks for all the fish!";
485 }
486  
487 __END__
« lib/App/Sandy/Role/Counter.pm ↑ Summary lib/App/Sandy/Role/IO.pm »