/
Me_filter.pm
535 lines (464 loc) · 19 KB
/
Me_filter.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
package MIP::Recipes::Analysis::Me_filter;
use 5.026;
use Carp;
use charnames qw{ :full :short };
use English qw{ -no_match_vars };
use File::Basename qw{ dirname };
use File::Spec::Functions qw{ catdir catfile devnull };
use List::Util qw{ first};
use open qw{ :encoding(UTF-8) :std };
use Params::Check qw{ allow check last_error };
use utf8;
use warnings;
use warnings qw{ FATAL utf8 };
## CPANM
use autodie qw{ :all };
use Readonly;
## MIPs lib/
use MIP::Constants
qw{ %ANALYSIS $DOT $DOUBLE_QUOTE $EMPTY_STR $LOG_NAME $NEWLINE $PIPE $SPACE $UNDERSCORE };
BEGIN {
require Exporter;
use base qw{ Exporter };
# Functions and variables which can be optionally exported
our @EXPORT_OK = qw{ analysis_me_filter };
}
## Constants
Readonly my $ANNOTATION_DISTANCE => $ANALYSIS{ANNOTATION_DISTANCE};
Readonly my $ANNOTATION_DISTANCE_MT => $ANALYSIS{ANNOTATION_DISTANCE_MT};
Readonly my $MT_CONTIG_ID_REGEXP => q{MT | M | chrM};
sub analysis_me_filter {
## Function : Filters mobile elements on frequency and split the vcf in aclinical and research vcf using vcfparser.
## Returns :
## Arguments: $active_parameter_href => Active parameters for this analysis hash {REF}
## : $case_id => Family id
## : $filehandle => Sbatch filehandle to write to
## : $file_info_href => File info hash {REF}
## : $job_id_href => Job id hash {REF}
## : $parameter_href => Parameter hash {REF}
## : $profile_base_command => Submission profile base command
## : $recipe_name => Program name
## : $sample_info_href => Info on samples and case hash {REF}
## : $temp_directory => Temporary directory
## : $xargs_file_counter => The xargs file counter
my ($arg_href) = @_;
## Flatten argument(s)
my $active_parameter_href;
my $file_info_href;
my $job_id_href;
my $parameter_href;
my $recipe_name;
my $sample_info_href;
## Default(s)
my $case_id;
my $profile_base_command;
my $temp_directory;
my $xargs_file_counter;
my $tmpl = {
active_parameter_href => {
default => {},
defined => 1,
required => 1,
store => \$active_parameter_href,
strict_type => 1,
},
case_id => {
default => $arg_href->{active_parameter_href}{case_id},
store => \$case_id,
strict_type => 1,
},
file_info_href => {
default => {},
defined => 1,
required => 1,
store => \$file_info_href,
strict_type => 1,
},
job_id_href => {
default => {},
defined => 1,
required => 1,
store => \$job_id_href,
strict_type => 1,
},
parameter_href => {
default => {},
defined => 1,
required => 1,
store => \$parameter_href,
strict_type => 1,
},
profile_base_command => {
default => q{sbatch},
store => \$profile_base_command,
strict_type => 1,
},
recipe_name => {
defined => 1,
required => 1,
store => \$recipe_name,
strict_type => 1,
},
sample_info_href => {
default => {},
defined => 1,
required => 1,
store => \$sample_info_href,
strict_type => 1,
},
temp_directory => {
default => $arg_href->{active_parameter_href}{temp_directory},
store => \$temp_directory,
strict_type => 1,
},
xargs_file_counter => {
allow => qr{ \A\d+\z }xsm,
default => 0,
store => \$xargs_file_counter,
strict_type => 1,
},
};
check( $tmpl, $arg_href, 1 ) or croak q{Could not parse arguments!};
use MIP::Analysis qw{ get_vcf_parser_analysis_suffix };
use MIP::File_info qw{ get_io_files parse_io_outfiles };
use MIP::List qw{ check_element_exist_hash_of_array };
use MIP::File::Path qw{ remove_file_path_suffix };
use MIP::Processmanagement::Processes qw{ submit_recipe };
use MIP::Program::Bcftools qw{ bcftools_concat bcftools_sort bcftools_view };
use MIP::Program::Htslib qw{ htslib_bgzip htslib_tabix };
use MIP::Program::Mip qw{ mip_vcfparser };
use MIP::Recipe qw{ parse_recipe_prerequisites };
use MIP::Sample_info
qw{ set_file_path_to_store set_gene_panel set_recipe_outfile_in_sample_info };
use MIP::Script::Setup_script qw{ setup_script};
### PREPROCESSING:
## Retrieve logger object
my $log = Log::Log4perl->get_logger($LOG_NAME);
## Unpack parameters
my %io = get_io_files(
{
id => $case_id,
file_info_href => $file_info_href,
parameter_href => $parameter_href,
recipe_name => $recipe_name,
stream => q{in},
temp_directory => $temp_directory,
}
);
my $infile_name_prefix = $io{in}{file_name_prefix};
my $infile_path = $io{in}{file_path};
my %recipe = parse_recipe_prerequisites(
{
active_parameter_href => $active_parameter_href,
parameter_href => $parameter_href,
recipe_name => $recipe_name,
}
);
my @vcfparser_analysis_types = get_vcf_parser_analysis_suffix(
{
vcfparser_outfile_count => $active_parameter_href->{sv_vcfparser_outfile_count},
}
);
## Set and get the io files per chain, id and stream
my @set_outfile_name_prefixes =
map { $infile_name_prefix . $_ } @vcfparser_analysis_types;
%io = (
%io,
parse_io_outfiles(
{
chain_id => $recipe{job_id_chain},
id => $case_id,
file_info_href => $file_info_href,
file_name_prefix => $infile_name_prefix,
iterators_ref => \@vcfparser_analysis_types,
outdata_dir => $active_parameter_href->{outdata_dir},
parameter_href => $parameter_href,
recipe_name => $recipe_name,
temp_directory => $temp_directory,
}
)
);
my $outdir_path = $io{out}{dir_path};
my $outfile_path_prefix = $io{out}{file_path_prefix};
my @outfile_paths = @{ $io{out}{file_paths} };
my %outfile_path = %{ $io{out}{file_path_href} };
my @outfile_suffixes = @{ $io{out}{file_suffixes} };
## Filehandles
my $filehandle = IO::Handle->new();
## Creates recipe directories (info & data & script), recipe script filenames and writes sbatch header
my ( $recipe_file_path, $recipe_info_path ) = setup_script(
{
active_parameter_href => $active_parameter_href,
core_number => $recipe{core_number},
directory_id => $case_id,
filehandle => $filehandle,
job_id_href => $job_id_href,
memory_allocation => $recipe{memory},
process_time => $recipe{time},
recipe_directory => $recipe_name,
recipe_name => $recipe_name,
temp_directory => $temp_directory,
}
);
my $mt_contig = first { / $MT_CONTIG_ID_REGEXP /xms } @{ $file_info_href->{contigs} };
my @select_feature_annotation_columns;
my $select_file;
my $select_file_matching_column;
my ( $no_gz_file_ending, $select_no_gz_file_ending ) =
map { s/[.]gz\z/$EMPTY_STR/r } @outfile_suffixes;
my $non_mt_outfile_path = $outfile_path_prefix . q{_non_MT} . $no_gz_file_ending;
my $select_non_mt_outfile_path = $outfile_path_prefix . q{_non_MT} . $select_no_gz_file_ending;
my $mt_outfile_path = $outfile_path_prefix . q{_MT} . $no_gz_file_ending;
my $select_mt_outfile_path = $outfile_path_prefix . q{_MT} . $select_no_gz_file_ending;
if ( $active_parameter_href->{sv_vcfparser_select_file} ) {
## List of genes to analyse separately
$select_file = catfile( $active_parameter_href->{sv_vcfparser_select_file} );
## Column of HGNC Symbol in select file ("-sf")
$select_file_matching_column =
$active_parameter_href->{sv_vcfparser_select_file_matching_column};
if ( exists $active_parameter_href->{sv_vcfparser_select_feature_annotation_columns} ) {
@select_feature_annotation_columns =
@{ $active_parameter_href->{sv_vcfparser_select_feature_annotation_columns} };
}
}
my $exclude_filter = _build_bcftools_frequency_filter(
{
frequency_threshold => $active_parameter_href->{me_filter_frequency_threshold},
me_annotate_query_info_href => $active_parameter_href->{me_annotate_query_files},
}
);
say {$filehandle} q{## Vcfparser non MT contigs};
bcftools_view(
{
exclude => $exclude_filter,
filehandle => $filehandle,
infile_path => $infile_path,
targets => q{^} . $mt_contig,
}
);
print {$filehandle} $PIPE . $SPACE;
mip_vcfparser(
{
filehandle => $filehandle,
infile_path => catfile( dirname( devnull() ), q{stdin} ),
log_file_path => catfile( $outdir_path, q{vcfparser} . q{.log} ),
padding => $ANNOTATION_DISTANCE,
parse_vep => $active_parameter_href->{sv_varianteffectpredictor},
per_gene => $active_parameter_href->{sv_vcfparser_per_gene},
pli_values_file_path => $active_parameter_href->{vcfparser_pli_score_file},
range_feature_annotation_columns_ref =>
\@{ $active_parameter_href->{sv_vcfparser_range_feature_annotation_columns} },
range_feature_file_path => $active_parameter_href->{sv_vcfparser_range_feature_file},
select_feature_annotation_columns_ref => \@select_feature_annotation_columns,
stdoutfile_path => $non_mt_outfile_path,
select_feature_file_path => $select_file,
select_feature_matching_column => $select_file_matching_column,
select_outfile => $select_non_mt_outfile_path,
variant_type => q{sv},
}
);
say {$filehandle} $NEWLINE;
say {$filehandle} q{## Vcfparser MT};
bcftools_view(
{
exclude => $exclude_filter,
filehandle => $filehandle,
infile_path => $infile_path,
regions_ref => [$mt_contig],
}
);
print {$filehandle} $PIPE . $SPACE;
mip_vcfparser(
{
filehandle => $filehandle,
infile_path => catfile( dirname( devnull() ), q{stdin} ),
log_file_path => catfile( $outdir_path, q{vcfparser} . q{.log} ),
padding => $ANNOTATION_DISTANCE_MT,
parse_vep => $active_parameter_href->{me_varianteffectpredictor},
per_gene => $active_parameter_href->{sv_vcfparser_per_gene},
pli_values_file_path => $active_parameter_href->{vcfparser_pli_score_file},
range_feature_annotation_columns_ref =>
\@{ $active_parameter_href->{sv_vcfparser_range_feature_annotation_columns} },
range_feature_file_path => $active_parameter_href->{sv_vcfparser_range_feature_file},
select_feature_annotation_columns_ref => \@select_feature_annotation_columns,
stdoutfile_path => $mt_outfile_path,
select_feature_file_path => $select_file,
select_feature_matching_column => $select_file_matching_column,
select_outfile => $select_mt_outfile_path,
variant_type => q{sv},
}
);
say {$filehandle} $NEWLINE;
say {$filehandle} q{## Compress and index vcfparser output};
my @files_to_compress_and_index = (
$mt_outfile_path, $non_mt_outfile_path,
$select_mt_outfile_path, $select_non_mt_outfile_path,
);
FILE_TO_COMPRESS_AND_INDEX:
foreach my $file_to_compress_and_index (@files_to_compress_and_index) {
htslib_bgzip(
{
infile_path => $file_to_compress_and_index,
force => 1,
filehandle => $filehandle,
threads => $recipe{core_number},
}
);
say {$filehandle} $NEWLINE;
htslib_tabix(
{
filehandle => $filehandle,
force => 1,
infile_path => $file_to_compress_and_index . q{.gz},
}
);
say {$filehandle} $NEWLINE;
}
### Special case: replace all clinical mitochondrial variants with research mitochondrial variants
$select_mt_outfile_path =
$active_parameter_href->{sv_vcfparser_add_all_mt_var}
? $mt_outfile_path
: $select_mt_outfile_path;
## Concatenate MT variants with the rest
my @file_sets = (
{
files_to_concat_ref => [ $non_mt_outfile_path . q{.gz}, $mt_outfile_path . q{.gz} ],
outfile => $outfile_paths[0],
},
{
files_to_concat_ref =>
[ $select_non_mt_outfile_path . q{.gz}, $select_mt_outfile_path . q{.gz} ],
outfile => $outfile_paths[1],
},
);
say {$filehandle} q{## Concatenate, sort and index};
OUTFILE_SET:
foreach my $outfile_set (@file_sets) {
bcftools_concat(
{
allow_overlaps => 1,
filehandle => $filehandle,
infile_paths_ref => $outfile_set->{files_to_concat_ref},
output_type => q{u},
threads => $recipe{core_number},
}
);
print {$filehandle} $PIPE . $SPACE;
bcftools_sort(
{
filehandle => $filehandle,
output_type => q{z},
outfile_path => $outfile_set->{outfile},
temp_directory => $temp_directory,
}
);
say {$filehandle} $NEWLINE;
htslib_tabix(
{
filehandle => $filehandle,
force => 1,
infile_path => $outfile_set->{outfile},
}
);
say {$filehandle} $NEWLINE;
}
close $filehandle;
if ( $recipe{mode} == 1 ) {
## Collect QC metadata info for later use
set_recipe_outfile_in_sample_info(
{
recipe_name => $recipe_name,
sample_info_href => $sample_info_href,
path => $outfile_paths[0],
}
);
my %gene_panels = (
range_file => q{sv_vcfparser_range_feature_file},
select_file => q{sv_vcfparser_select_file},
);
GENE_PANEL:
while ( my ( $gene_panel_key, $gene_panel_file ) = each %gene_panels ) {
## Collect databases(s) from a potentially merged gene panel file and adds them to sample_info
set_gene_panel(
{
aggregate_gene_panel_file => $active_parameter_href->{$gene_panel_file},
aggregate_gene_panels_key => $gene_panel_key,
recipe_name => $recipe_name,
sample_info_href => $sample_info_href,
}
);
}
OUTFILE:
while ( my ( $file_key, $outfile ) = each %outfile_path ) {
my $metafile_tag = $file_key =~ m/selected/xms ? q{clinical} : q{research};
set_file_path_to_store(
{
format => q{vcf},
id => $case_id,
path => $outfile,
path_index => $outfile . $DOT . q{tbi},
recipe_name => $recipe_name,
sample_info_href => $sample_info_href,
tag => $metafile_tag,
}
);
}
submit_recipe(
{
base_command => $profile_base_command,
case_id => $case_id,
dependency_method => q{sample_to_case},
job_id_chain => $recipe{job_id_chain},
job_id_href => $job_id_href,
job_reservation_name => $active_parameter_href->{job_reservation_name},
log => $log,
max_parallel_processes_count_href =>
$file_info_href->{max_parallel_processes_count},
recipe_file_path => $recipe_file_path,
sample_ids_ref => \@{ $active_parameter_href->{sample_ids} },
submission_profile => $active_parameter_href->{submission_profile},
}
);
}
return 1;
}
sub _build_bcftools_frequency_filter {
## Function : Build the frequency filter command
## Returns :
## Arguments: $frequency_threshold => Frequency annotation to use in filtering
## : $me_annotate_query_info_href => SVDB query files used to annotate ME {REF}
my ($arg_href) = @_;
## Flatten argument(s)
my $frequency_threshold;
my $me_annotate_query_info_href;
my $tmpl = {
frequency_threshold => {
required => 1,
store => \$frequency_threshold,
strict_type => 1,
},
me_annotate_query_info_href => {
default => {},
required => 1,
store => \$me_annotate_query_info_href,
}
};
check( $tmpl, $arg_href, 1 ) or croak q{Could not parse arguments!};
## Skip if no threshold is set
return if ( not $frequency_threshold );
## Find annotations to use for filtering
my @frequency_filters;
my $frequency_expression = $SPACE . q{>} . $SPACE . $frequency_threshold;
ANNOTATION:
while ( my ( $annotation_file, $annotation_tags ) = each %{$me_annotate_query_info_href} ) {
my ( $tag_prefix, $fq_suffix, $cnt_suffix, $file_fq_tag, $file_cnt_tag, $use_in_filter ) =
split /[|]/sxm, $annotation_tags;
next ANNOTATION if ( not $use_in_filter );
push @frequency_filters, q{INFO/} . $tag_prefix . $file_fq_tag . $frequency_expression;
}
## build filter
my $exclude_filter =
$DOUBLE_QUOTE . join( $SPACE . $PIPE . $SPACE, @frequency_filters ) . $DOUBLE_QUOTE;
return $exclude_filter;
}
1;