/
Analyse.pm
553 lines (479 loc) · 17 KB
/
Analyse.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
package MIP::Main::Analyse;
#### Master script for analysing paired end reads from the Illumina plattform in fastq(.gz) format to annotated ranked disease causing variants. The program performs QC, aligns reads using BWA, performs variant discovery and annotation as well as ranking the found variants according to disease potential.
use 5.026;
use Carp;
use charnames qw{ :full :short };
use Cwd;
use Cwd qw{ abs_path };
use English qw{ -no_match_vars };
use File::Basename qw{ basename fileparse };
use File::Copy qw{ copy };
use File::Spec::Functions qw{ catfile };
use FindBin qw{ $Bin };
use Getopt::Long;
use open qw{ :encoding(UTF-8) :std };
use Params::Check qw{ check allow last_error };
use POSIX;
use Time::Piece;
use utf8;
use warnings qw{ FATAL utf8 };
## Third party module(s)
use autodie qw{ open close :all };
use IPC::System::Simple;
use Modern::Perl qw{ 2018 };
use Path::Iterator::Rule;
use Readonly;
## MIPs lib/
use MIP::Active_parameter qw{
check_load_env_packages
check_recipe_mode
get_not_allowed_temp_dirs
parse_recipe_resources
set_gender_sample_ids
set_parameter_reference_dir_path
update_recipe_mode_with_dry_run_all
update_to_absolute_path };
use MIP::Analysis qw{ check_analysis_type_to_pipeline get_overall_analysis_type };
use MIP::Config qw{ parse_config };
use MIP::Constants qw{ $DOT $EMPTY_STR $MIP_VERSION $NEWLINE $SINGLE_QUOTE $SPACE $TAB };
use MIP::Contigs qw{ set_contigs };
use MIP::Environment::User qw{ check_email_address };
use MIP::File_info qw{ set_dict_contigs set_human_genome_reference_features };
use MIP::File::Path qw{ check_allowed_temp_directory };
use MIP::Io::Recipes qw{ build_file_prefix_tag };
use MIP::Log::MIP_log4perl qw{ get_log };
use MIP::Parameter qw{
check_recipe_vs_binary_name
get_cache
parse_parameter_files
parse_reference_path
set_cache
set_cache_program_executables
set_default
};
use MIP::Pedigree qw{ create_fam_file
get_is_trio
parse_pedigree
};
use MIP::Pipeline qw{ run_analyse_pipeline };
use MIP::Processmanagement::Processes qw{ write_job_ids_to_file };
use MIP::Recipes::Parse qw{ parse_recipes parse_start_with_recipe };
use MIP::Reference qw{ check_human_genome_file_endings };
use MIP::Sample_info qw{
reload_previous_pedigree_info
set_no_dry_run_parameters
write_sample_info_to_file };
use MIP::Store qw{ store_files };
use MIP::Validate::Case qw{ check_sample_ids };
BEGIN {
use base qw{ Exporter };
require Exporter;
# Set the version for version checking
our $VERSION = 1.59;
# Functions and variables which can be optionally exported
our @EXPORT_OK = qw{ mip_analyse };
}
## Constants
Readonly my %RECIPE_PARAMETERS_TO_CHECK => (
keys => [
qw{ recipe_core_number recipe_memory recipe_time
set_recipe_core_number set_recipe_memory set_recipe_time }
],
elements => [qw{ associated_recipe decompose_normalize_references }],
);
Readonly my $MINUS_ONE => -1;
sub mip_analyse {
## Function : Execute mip analyse pre pipeline parsing
## Returns :
## Arguments: $active_parameter_href => Active parameters for this analysis hash {REF}
## : $file_info_href => File info hash {REF}
## : $order_parameters_ref => Order of addition to parameter array {REF}
## : $parameter_href => Parameter hash {REF}
my ($arg_href) = @_;
## Flatten argument(s)
my $active_parameter_href;
my $file_info_href;
my $order_parameters_ref;
my $parameter_href;
my $tmpl = {
active_parameter_href => {
default => {},
defined => 1,
required => 1,
store => \$active_parameter_href,
strict_type => 1,
},
file_info_href => {
required => 1,
defined => 1,
default => {},
strict_type => 1,
store => \$file_info_href,
},
order_parameters_ref => {
default => [],
defined => 1,
required => 1,
store => \$order_parameters_ref,
strict_type => 1,
},
parameter_href => {
default => {},
defined => 1,
required => 1,
store => \$parameter_href,
strict_type => 1,
},
};
check( $tmpl, $arg_href, 1 ) or croak q{Could not parse arguments!};
# Holds all active parameters values for broadcasting
my @broadcasts;
#### Script parameters
## Add date_time_stamp for later use in log and qc_metrics yaml file
my $date_time = localtime;
my $date_time_stamp = $date_time->datetime;
my $date = $date_time->ymd;
# Catches script name and removes ending
my $script = fileparse( basename( $PROGRAM_NAME, $DOT . q{pl} ) );
#### Set program parameters
my ( %job_id, %sample_info );
#### Staging Area
### Get and/or set input parameters
## Change relative path to absolute path for parameter with "update_path: absolute_path" in config
update_to_absolute_path(
{
active_parameter_href => $active_parameter_href,
parameter_href => $parameter_href,
}
);
### Config
parse_config(
{
active_parameter_href => $active_parameter_href,
parameter_href => $parameter_href,
}
);
## Get log object and set log file in active parameters unless already set from cmd
my $log = get_log(
{
active_parameter_href => $active_parameter_href,
date => $date,
date_time_stamp => $date_time_stamp,
log_name => uc q{mip_analyse},
script => $script,
}
);
## Write MIP VERSION and log file path
$log->info( q{MIP Version: } . $MIP_VERSION );
$log->info( q{Script parameters and info from are saved in file: }
. $active_parameter_href->{log_file} );
## Pedigree
parse_pedigree(
{
active_parameter_href => $active_parameter_href,
pedigree_file_path => $active_parameter_href->{pedigree_file},
parameter_href => $parameter_href,
sample_info_href => \%sample_info,
}
);
## Detect if all samples has the same sequencing type and return consensus if reached
$parameter_href->{cache}{consensus_analysis_type} = get_overall_analysis_type(
{
analysis_type_href => $active_parameter_href->{analysis_type},
}
);
## Set default from parameter hash to active_parameter for uninitilized parameters
set_default(
{
active_parameter_href => $active_parameter_href,
custom_default_parameters_ref =>
$parameter_href->{custom_default_parameters}{default},
parameter_href => $parameter_href,
}
);
my $consensus_analysis_type = get_cache(
{
parameter_href => $parameter_href,
parameter_name => q{consensus_analysis_type},
}
);
## Update path for supplied reference(s) associated with parameter that should
## reside in the mip reference directory to full path
set_parameter_reference_dir_path(
{
active_parameter_href => $active_parameter_href,
parameter_name => q{human_genome_reference},
}
);
## Detect version and source of the human_genome_reference: Source (hg19 or GRCh) and check compression status
set_human_genome_reference_features(
{
file_info_href => $file_info_href,
human_genome_reference =>
basename( $active_parameter_href->{human_genome_reference} ),
parameter_href => $parameter_href,
}
);
## Reference in MIP reference directory
parse_reference_path(
{
active_parameter_href => $active_parameter_href,
parameter_href => $parameter_href,
}
);
### Checks
check_analysis_type_to_pipeline(
{
pipeline => lc _parent_module( {} ),
analysis_type => $consensus_analysis_type,
}
);
## Parse existence of files and directories
parse_parameter_files(
{
active_parameter_href => $active_parameter_href,
consensus_analysis_type => $consensus_analysis_type,
parameter_href => $parameter_href,
}
);
## Updates sample_info hash with previous run pedigree info
reload_previous_pedigree_info(
{
sample_info_href => \%sample_info,
sample_info_file_path => $active_parameter_href->{sample_info_file},
}
);
## Special case since dict is created with .fastq removed
## Check the existance of associated human genome files
check_human_genome_file_endings(
{
human_genome_reference_file_endings_ref =>
$file_info_href->{human_genome_reference_file_endings},
human_genome_reference_path =>
$active_parameter_href->{human_genome_reference},
parameter_href => $parameter_href,
parameter_name => q{human_genome_reference_file_endings},
}
);
## Set sequence contigs used in analysis from human genome sequence dict file
my $dict_file_path = catfile( $active_parameter_href->{reference_dir},
$file_info_href->{human_genome_reference_name_prefix} . $DOT . q{dict} );
set_dict_contigs(
{
dict_file_path => $dict_file_path,
file_info_href => $file_info_href,
parameter_href => $parameter_href,
}
);
## Detect case constellation based on pedigree file
$parameter_href->{cache}{trio} = get_is_trio(
{
active_parameter_href => $active_parameter_href,
sample_info_href => \%sample_info,
}
);
## Check email adress syntax and mail host
check_email_address(
{
email => $active_parameter_href->{email},
}
);
## Check that the temp directory value is allowed
my @is_not_allowed_temp_dirs =
get_not_allowed_temp_dirs( { active_parameter_href => $active_parameter_href, } );
check_allowed_temp_directory(
{
not_allowed_paths_ref => \@is_not_allowed_temp_dirs,
temp_directory => $active_parameter_href->{temp_directory},
}
);
## Parameters that have keys or elements as MIP recipe names
parse_recipes(
{
active_parameter_href => $active_parameter_href,
parameter_href => $parameter_href,
parameter_to_check_href => \%RECIPE_PARAMETERS_TO_CHECK,
}
);
## Check core number requested against environment provisioned
parse_recipe_resources( { active_parameter_href => $active_parameter_href, } );
## Check that the case_id and the sample_id(s) exists and are unique. Check if id sample_id contains "_".
check_sample_ids(
{
case_id => $active_parameter_href->{case_id},
sample_ids_ref => $active_parameter_href->{sample_ids},
}
);
## Adds dynamic aggregate information from definitions to parameter hash
set_cache(
{
aggregates_ref => [
## Collect all aligners
q{recipe_type:aligners},
## Collects all references in that are supposed to be in reference directory
q{reference:reference_dir},
## Collects all structural variant_callers
q{recipe_type:structural_variant_callers},
## Collects all variant_callers
q{recipe_type:variant_callers},
## Collects all recipes that MIP can handle
q{type:recipe},
],
parameter_href => $parameter_href,
}
);
set_cache_program_executables( { parameter_href => $parameter_href, } );
## Check correct value for recipe mode in MIP
check_recipe_mode(
{
active_parameter_href => $active_parameter_href,
parameter_href => $parameter_href,
}
);
## Check that package name name are included in MIP as either "mip", "recipe" or "program"
check_load_env_packages(
{
active_parameter_href => $active_parameter_href,
parameter_href => $parameter_href,
}
);
## Check that recipe name and program name are not identical
check_recipe_vs_binary_name(
{
parameter_href => $parameter_href,
recipe_names_ref => $parameter_href->{cache}{recipe},
}
);
parse_start_with_recipe(
{
active_parameter_href => $active_parameter_href,
parameter_href => $parameter_href,
},
);
## Update recipe mode depending on dry_run_all flag
update_recipe_mode_with_dry_run_all(
{
active_parameter_href => $active_parameter_href,
dry_run_all => $active_parameter_href->{dry_run_all},
recipes_ref => $parameter_href->{cache}{recipe},
}
);
## Set the gender(s) included in current analysis and count them
set_gender_sample_ids(
{
active_parameter_href => $active_parameter_href,
sample_info_href => \%sample_info,
}
);
### Contigs
## Set contig prefix and contig names depending on reference used
set_contigs(
{
file_info_href => $file_info_href,
version => $file_info_href->{human_genome_reference_version},
}
);
## Creates all fileendings as the samples is processed depending on the chain of modules activated
my @order_recipes = get_cache(
{
parameter_href => $parameter_href,
parameter_name => q{order_recipes_ref},
}
);
build_file_prefix_tag(
{
active_parameter_href => $active_parameter_href,
case_id => $active_parameter_href->{case_id},
file_info_href => $file_info_href,
order_recipes_ref => \@order_recipes,
parameter_href => $parameter_href,
sample_ids_ref => $active_parameter_href->{sample_ids},
}
);
## Create .fam file to be used in variant calling analyses
create_fam_file(
{
case_id => $active_parameter_href->{case_id},
execution_mode => q{system},
fam_file_path => $active_parameter_href->{pedigree_fam_file},
parameter_href => $parameter_href,
sample_ids_ref => $active_parameter_href->{sample_ids},
sample_info_href => \%sample_info,
}
);
############
####MAIN####
############
set_no_dry_run_parameters(
{
is_dry_run_all => $active_parameter_href->{dry_run_all},
analysis_date => $date_time_stamp,
mip_version => $MIP_VERSION,
sample_info_href => \%sample_info,
}
);
run_analyse_pipeline(
{
active_parameter_href => $active_parameter_href,
broadcasts_ref => \@broadcasts,
consensus_analysis_type => $consensus_analysis_type,
file_info_href => $file_info_href,
job_id_href => \%job_id,
order_parameters_ref => $order_parameters_ref,
order_recipes_ref => $parameter_href->{cache}{order_recipes_ref},
parameter_href => $parameter_href,
sample_info_href => \%sample_info,
}
);
## Write QC for recipes used in analysis
# Write sample info to yaml file
write_sample_info_to_file(
{
sample_info_file => $active_parameter_href->{sample_info_file},
sample_info_href => \%sample_info,
}
);
write_job_ids_to_file(
{
case_id => $active_parameter_href->{case_id},
job_id_href => \%job_id,
job_ids_file_path => catfile(
$active_parameter_href->{outdata_dir},
q{slurm_job_ids} . $DOT . q{yaml}
),
}
);
store_files(
{
active_parameter_href => $active_parameter_href,
sample_info_href => \%sample_info,
}
);
return;
}
##Investigate potential autodie error
if ( $EVAL_ERROR and $EVAL_ERROR->isa(q{autodie::exception}) ) {
if ( $EVAL_ERROR->matches(q{default}) ) {
say {*STDERR} q{Not an autodie error at all};
}
if ( $EVAL_ERROR->matches(q{open}) ) {
say {*STDERR} q{Error from open};
}
if ( $EVAL_ERROR->matches(q{:io}) ) {
say {*STDERR} q{Non-open, IO error.};
}
}
elsif ($EVAL_ERROR) {
say {*STDERR} q{A non-autodie exception.};
}
sub _parent_module {
## Function : Returns the name of the module that called this one
## Returns : $parent_module
## Arguments:
## Get full path to module
my $parent_module = ( caller 1 )[0];
## Isolate module
$parent_module = ( split /::/xms, $parent_module )[$MINUS_ONE];
return $parent_module;
}
1;