forked from Clinical-Genomics/MIP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qccollect.pl
executable file
·1967 lines (1635 loc) · 70.3 KB
/
qccollect.pl
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env perl
#### Collects MPS QC from MIP. Loads information on files to examine and values to extract from in YAML format and outputs exracted metrics in YAML format.
use 5.026;
use Carp;
use charnames qw{ :full :short };
use Cwd;
use Cwd qw{ abs_path };
use File::Basename qw{ dirname basename fileparse };
use File::Spec::Functions qw{ catdir catfile devnull };
use FindBin qw{ $Bin };
use English qw{ -no_match_vars };
use Getopt::Long;
use open qw{ :encoding(UTF-8) :std };
use Params::Check qw{ check allow last_error };
use Pod::Usage;
use Pod::Text;
use POSIX;
use utf8;
use warnings qw{ FATAL utf8 };
$Params::Check::PRESERVE_CASE = 1; #Do not convert to lower case
## CPANM
use autodie qw{ open close :all };
use Modern::Perl qw{ 2017 };
use Readonly;
##MIPs lib/
use lib catdir( $Bin, q{lib} );
use MIP::Check::Modules qw{ check_perl_modules };
use MIP::Constants qw{ $NEWLINE $SPACE };
use MIP::File::Format::Yaml qw{ load_yaml write_yaml };
use MIP::Log::MIP_log4perl qw{ initiate_logger };
use MIP::Script::Utils qw{ help };
our $USAGE = build_usage( {} );
BEGIN {
require MIP::Check::Modules;
require MIP::Check::Modules;
use MIP::Check::Modules qw{ check_perl_modules parse_cpan_file };
my @modules =
parse_cpan_file { cpanfile_path => catfile( $Bin, qw{ definitions cpanfile } ), };
## Evaluate that all modules required are installed
check_perl_modules(
{
modules_ref => \@modules,
program_name => $PROGRAM_NAME,
}
);
}
my ( $sample_info_file, $regexp_file, $print_regexp, $skip_evaluation,
$evaluate_plink_gender );
## Scalar parameters with defaults
my ( $outfile, $print_regexp_outfile, $log_file ) =
( q{qcmetrics.yaml}, q{qc_regexp.yaml}, catfile( cwd(), q{qccollect.log} ) );
my ( %qc_data, %evaluate_metric );
## Save header(s) in each outfile
my %qc_header;
## Save data in each outfile
my %qc_recipe_data;
my $qccollect_version = q{2.1.1};
###User Options
GetOptions(
q{si|sample_info_file:s} => \$sample_info_file,
q{r|regexp_file:s} => \$regexp_file,
q{o|outfile:s} => \$outfile,
q{preg|print_regexp} => \$print_regexp,
q{prego|print_regexp_outfile:s} => \$print_regexp_outfile,
q{ske|skip_evaluation} => \$skip_evaluation,
q{epg|evaluate_plink_gender} => \$evaluate_plink_gender,
q{l|log_file:s} => \$log_file,
## Display help text
q{h|help} => sub { say STDOUT $USAGE; exit; },
## Display version number
q{v|version} => sub {
say STDOUT $NEWLINE . basename($PROGRAM_NAME) . $SPACE . $qccollect_version,
$NEWLINE;
exit;
},
)
or help(
{
USAGE => $USAGE,
exit_code => 1,
}
);
## Creates log object
my $log = initiate_logger(
{
file_path => $log_file,
log_name => q{Qccollect},
}
);
if ($print_regexp) {
## Write default regexp to YAML
regexp_to_yaml( { print_regexp_outfile => $print_regexp_outfile, } );
$log->info( q{Wrote regexp YAML file to: } . $print_regexp_outfile );
exit;
}
if ( not $sample_info_file ) {
$log->info($USAGE);
$log->fatal( q{Must supply a '-sample_info_file' (supply whole path)}, $NEWLINE );
exit;
}
if ( not $regexp_file ) {
$log->info($USAGE);
$log->fatal( q{Must supply a '-regexp_file' (supply whole path)}, $NEWLINE );
exit;
}
###########
####MAIN###
###########
## Loads a YAML file into an arbitrary hash and returns it
my %sample_info = load_yaml( { yaml_file => $sample_info_file, } );
$log->info( q{Loaded: } . $sample_info_file );
## Loads a YAML file into an arbitrary hash and returns it
my %regexp = load_yaml( { yaml_file => $regexp_file, } );
$log->info( q{Loaded: } . $regexp_file );
## Extracts all qcdata on sample_id level using information in %sample_info and %regexp
sample_qc(
{
sample_info_href => \%sample_info,
regexp_href => \%regexp,
qc_data_href => \%qc_data,
qc_header_href => \%qc_header,
qc_recipe_data_href => \%qc_recipe_data,
}
);
## Extracts all qcdata on case level using information in %sample_info_file and %regexp
case_qc(
{
sample_info_href => \%sample_info,
regexp_href => \%regexp,
qc_data_href => \%qc_data,
qc_header_href => \%qc_header,
qc_recipe_data_href => \%qc_recipe_data,
}
);
##Add qcCollect version to qc_data yaml file
$qc_data{recipe}{qccollect}{version} = $qccollect_version;
$qc_data{recipe}{qccollect}{regexp_file} = $regexp_file;
SAMPLE_ID:
foreach my $sample_id ( keys %{ $sample_info{sample} } ) {
## Defines recipes, metrics and thresholds to evaluate
define_evaluate_metric(
{
sample_info_href => \%sample_info,
sample_id => $sample_id,
}
);
}
if ( not $skip_evaluation ) {
## Evaluate the metrics
evaluate_qc_parameters(
{
qc_data_href => \%qc_data,
evaluate_metric_href => \%evaluate_metric,
}
);
}
## Writes a YAML hash to file
write_yaml(
{
yaml_href => \%qc_data,
yaml_file_path => $outfile,
}
);
$log->info( q{Wrote: } . $outfile );
######################
####Sub routines######
######################
sub build_usage {
##Function : Build the USAGE instructions
##Returns :
##Arguments: $program_name => Name of the script
my ($arg_href) = @_;
## Default(s)
my $program_name;
my $tmpl = {
program_name => {
default => basename($PROGRAM_NAME),
strict_type => 1,
store => \$program_name,
},
};
check( $tmpl, $arg_href, 1 ) or croak q{Could not parse arguments!};
return <<"END_USAGE";
$program_name [options] -si [sample_info.yaml] -r [regexp.yaml] -o [outfile]
-si/--sample_info_file Sample info file (YAML format, Supply whole path, mandatory)
-r/--regexp_file Regular expression file (YAML format, Supply whole path, mandatory)
-o/--outfile Data file output (Supply whole path, defaults to "qcmetrics.yaml")
-preg/--print_regexp Print the regexp used at CMMS switch (defaults to "0" (=no))
-prego/--print_regexp_outfile Regexp YAML outfile (defaults to "qc_regexp.yaml")
-ske/--skip_evaluation Skip evaluation step (boolean)
-epg/--evaluate_plink_gender Evaluate plink gender (boolean)
-l/--log_file Log file (Default: "qccollect.log")
-h/--help Display this help message
-v/--version Display version
END_USAGE
}
sub case_qc {
## Function : Extracts all qcdata on case level using information in %sample_info_file and %regexp
## Returns :
## Arguments: $qc_data_href => QCData hash {REF}
## : $qc_header_href => Save header(s) in each outfile {REF}
## : $qc_recipe_data_href => Hash to save data in each outfile {REF}
## : $regexp_href => RegExp hash {REF}
## : $sample_info_href => Info on samples and case hash {REF}
my ($arg_href) = @_;
## Flatten argument(s)
my $qc_data_href;
my $qc_header_href;
my $qc_recipe_data_href;
my $regexp_href;
my $sample_info_href;
my $tmpl = {
sample_info_href => {
default => {},
defined => 1,
required => 1,
store => \$sample_info_href,
strict_type => 1,
},
regexp_href => {
default => {},
defined => 1,
required => 1,
store => \$regexp_href,
strict_type => 1,
},
qc_data_href => {
default => {},
defined => 1,
required => 1,
store => \$qc_data_href,
strict_type => 1,
},
qc_header_href => {
default => {},
defined => 1,
required => 1,
store => \$qc_header_href,
strict_type => 1,
},
qc_recipe_data_href => {
default => {},
defined => 1,
required => 1,
store => \$qc_recipe_data_href,
strict_type => 1,
},
};
check( $tmpl, $arg_href, 1 ) or croak q{Could not parse arguments!};
## For every recipe
RECIPE:
for my $recipe ( keys %{ $sample_info_href->{recipe} } ) {
my $outdirectory;
my $outfile;
if ( $sample_info_href->{recipe}{$recipe}{version} ) {
## Add version to qc_data
$qc_data_href->{recipe}{$recipe}{version} =
$sample_info_href->{recipe}{$recipe}{version};
}
if ( $sample_info_href->{recipe}{$recipe}{outdirectory} ) {
## Extract outdirectory
$outdirectory =
$sample_info_href->{recipe}{$recipe}{outdirectory};
}
if ( $sample_info_href->{recipe}{$recipe}{outfile} ) {
## Extract outfile
$outfile = $sample_info_href->{recipe}{$recipe}{outfile};
}
if ( $sample_info_href->{recipe}{$recipe}{path} ) {
( $outfile, $outdirectory ) =
fileparse( $sample_info_href->{recipe}{$recipe}{path} );
}
## Parses the RegExpHash structure to identify if the info is 1) Paragraf section(s) (both header and data line(s)); 2) Seperate data line.
parse_regexp_hash_and_collect(
{
outdirectory => $outdirectory,
outfile => $outfile,
recipe => $recipe,
qc_recipe_data_href => $qc_recipe_data_href,
qc_header_href => $qc_header_href,
regexp_href => $regexp_href,
}
);
## Add extracted information to qc_data
add_to_qc_data(
{
evaluate_plink_gender => $evaluate_plink_gender,
qc_data_href => $qc_data_href,
qc_header_href => $qc_header_href,
qc_recipe_data_href => $qc_recipe_data_href,
recipe => $recipe,
regexp_href => $regexp_href,
sample_info_href => $sample_info_href,
}
);
}
return;
}
sub sample_qc {
## Function : Collects all sample qc in files defined by sample_info_file and regular expressions defined by regexp.
## Returns :
## Arguments: $sample_info_href => Info on samples and case hash {REF}
## : $regexp_href => RegExp hash {REF}
## : $qc_data_href => QCData hash {REF}
## : $qc_header_href => Save header(s) in each outfile {REF}
## : $qc_recipe_data_href => Hash to save data in each outfile {REF}
my ($arg_href) = @_;
## Flatten argument(s)
my $sample_info_href;
my $regexp_href;
my $qc_data_href;
my $qc_header_href;
my $qc_recipe_data_href;
my $tmpl = {
sample_info_href => {
required => 1,
defined => 1,
default => {},
strict_type => 1,
store => \$sample_info_href
},
regexp_href => {
required => 1,
defined => 1,
default => {},
strict_type => 1,
store => \$regexp_href
},
qc_data_href => {
required => 1,
defined => 1,
default => {},
strict_type => 1,
store => \$qc_data_href
},
qc_header_href => {
required => 1,
defined => 1,
default => {},
strict_type => 1,
store => \$qc_header_href
},
qc_recipe_data_href => {
required => 1,
defined => 1,
default => {},
strict_type => 1,
store => \$qc_recipe_data_href
},
};
check( $tmpl, $arg_href, 1 ) or croak q{Could not parse arguments!};
SAMPLE_ID:
for my $sample_id ( keys %{ $sample_info_href->{sample} } ) {
RECIPE:
for my $recipe ( keys %{ $sample_info_href->{sample}{$sample_id}{recipe} } ) {
INFILE:
for my $infile (
keys %{ $sample_info_href->{sample}{$sample_id}{recipe}{$recipe} } )
{
my $outdirectory;
my $outfile;
if ( $sample_info_href->{sample}{$sample_id}{recipe}{$recipe}
{$infile}{outdirectory} )
{
## Extract OutDirectory
$outdirectory =
$sample_info_href->{sample}{$sample_id}{recipe}
{$recipe}{$infile}{outdirectory};
}
if ( $sample_info_href->{sample}{$sample_id}{recipe}{$recipe}
{$infile}{outfile} )
{
## Extract OutFile
$outfile = $sample_info_href->{sample}{$sample_id}{recipe}
{$recipe}{$infile}{outfile};
}
if ( $sample_info_href->{sample}{$sample_id}{recipe}{$recipe}
{$infile}{path} )
{
( $outfile, $outdirectory ) =
fileparse( $sample_info_href->{sample}{$sample_id}{recipe}
{$recipe}{$infile}{path} );
}
## Parses the RegExpHash structure to identify if the info is 1) Paragraf section(s) (both header and data line(s)); 2) Seperate data line.
parse_regexp_hash_and_collect(
{
regexp_href => $regexp_href,
qc_recipe_data_href => $qc_recipe_data_href,
qc_header_href => $qc_header_href,
recipe => $recipe,
outdirectory => $outdirectory,
outfile => $outfile,
}
);
## Add extracted information to qc_data
add_to_qc_data(
{
infile => $infile,
qc_data_href => $qc_data_href,
qc_header_href => $qc_header_href,
qc_recipe_data_href => $qc_recipe_data_href,
recipe => $recipe,
regexp_href => $regexp_href,
sample_id => $sample_id,
sample_info_href => $sample_info_href,
}
);
}
}
}
return;
}
sub parse_regexp_hash_and_collect {
## Function : Parses the regexp hash structure to identify if the info is 1) Paragraf section(s) (both header and data line(s)); 2) Seperate data line.
## Returns :
## Arguments : $outdirectory => Recipes outdirectory
## : $outfile => Recipes outfile containing parameter to evaluate
## : $qc_header_href => Save header(s) in each outfile {REF}
## : $qc_recipe_data_href => Hash to save data in each outfile {REF}
## : $recipe => The recipe to examine
## : $regexp_href => Regexp hash {REF}
my ($arg_href) = @_;
## Flatten argument(s)
my $outdirectory;
my $outfile;
my $qc_header_href;
my $qc_recipe_data_href;
my $recipe;
my $regexp_href;
my $tmpl = {
regexp_href => {
default => {},
defined => 1,
required => 1,
store => \$regexp_href,
strict_type => 1,
},
qc_header_href => {
default => {},
defined => 1,
required => 1,
store => \$qc_header_href,
strict_type => 1,
},
qc_recipe_data_href => {
default => {},
defined => 1,
required => 1,
store => \$qc_recipe_data_href,
strict_type => 1,
},
recipe => {
defined => 1,
required => 1,
store => \$recipe,
strict_type => 1,
},
outdirectory => { store => \$outdirectory, strict_type => 1, },
outfile => { store => \$outfile, strict_type => 1, },
};
check( $tmpl, $arg_href, 1 ) or croak q{Could not parse arguments!};
## Holds the current regexp
my $regexp;
## Covers both whitespace and tab. Add other separators if required
my @separators = ( qw{ \s+ ! }, q{,} );
## Find the actual regular expression(s) for each recipe that is used
REG_EXP:
for my $regexp_key ( keys %{ $regexp_href->{$recipe} } ) {
if ( $regexp_key =~ /^header|header$/i ) {
## Detect if the outfile contains paragrafs/header info in the outfile i.e. data is formated as a paragraf with header(s) and line(s). "regexp_key" should either start with or end with "header". This section extracts the header line(s) for the entire outdata file. Necessary to assign correct data entry to header entry later (headers and data are saved in seperate hashes).
## Format outfile: Paragraf section
PARAGRAPH:
for my $regexp_header_key ( keys %{ $regexp_href->{$recipe}{$regexp_key} } ) {
## Regular expression used to collect paragraf header info
$regexp =
$regexp_href->{$recipe}{$regexp_key}{$regexp_header_key};
## Loop through possible separators to seperate any eventual header elements
SEPARATOR:
for (
my $separator_element_counter = 0 ;
$separator_element_counter < scalar(@separators) ;
$separator_element_counter++
)
{
## Detect if the regexp key is a paragraf header and not paragraf data (header line and data line(s))
if ( $regexp_header_key =~ /^header|header$/i ) {
## Collect paragraf header
@{ ${$qc_header_href}{$recipe}{$regexp_key}{$regexp_header_key} }
= split(
/$separators[$separator_element_counter]/,
`$regexp $outdirectory/$outfile`
);
#Then split should have been successful
if (
defined(
${$qc_header_href}{$recipe}{$regexp_key}
{$regexp_header_key}
)
)
{
## Found correct separator - do not continue
last SEPARATOR;
}
}
else {
## For paragraf data line(s)
## Collect paragraf data
@{ $qc_recipe_data_href->{$recipe}{$regexp_key}
{$regexp_header_key} } = split(
/$separators[$separator_element_counter]/,
`$regexp $outdirectory/$outfile`
);
## Then split should have been successful
if (
defined(
$qc_recipe_data_href->{$recipe}{$regexp_key}
{$regexp_header_key}[1]
)
)
{
## Found correct separator - do not continue
last SEPARATOR;
}
}
}
}
}
else {
##For info contained in Entry --> Value i.e. same line.
## The regular expression used to collect info
$regexp = $regexp_href->{$recipe}{$regexp_key};
## Loop through possible separators
SEPARATOR:
for (
my $separator_element_counter = 0 ;
$separator_element_counter < scalar(@separators) ;
$separator_element_counter++
)
{
## Collect data. Use regexp_key as element header
@{ $qc_recipe_data_href->{$recipe}{$regexp_key} } = split(
/$separators[$separator_element_counter]/,
`$regexp $outdirectory/$outfile`
);
## Then split should have been successful
if ( defined( $qc_recipe_data_href->{$recipe}{$regexp_key}[1] ) ) {
## Found correct separator do not continue
last SEPARATOR;
}
}
}
}
return;
}
sub add_to_qc_data {
## Function : Add to qc_data hash to enable write to yaml format
## Returns :
## Arguments : $evaluate_plink_gender => Evaluate plink gender
## : $infile => Infile to recipe
## : $recipe => Recipe to examine
## : $qc_data_href => QCData hash {REF}
## : $qc_header_href => Save header(s) in each outfile {REF}
## : $qc_recipe_data_href => Hash to save data in each outfile {REF}
## : $regexp_href => RegExp hash {REF}
## : $sample_id => SampleID
## : $sample_info_href => Info on samples and case hash {REF}
my ($arg_href) = @_;
## Flatten argument(s)
my $evaluate_plink_gender;
my $infile;
my $recipe;
my $qc_data_href;
my $qc_header_href;
my $qc_recipe_data_href;
my $regexp_href;
my $sample_id;
my $sample_info_href;
my $tmpl = {
evaluate_plink_gender => {
allow => [ undef, 0, 1 ],
store => \$evaluate_plink_gender,
strict_type => 1,
},
infile => { store => \$infile, strict_type => 1, },
recipe => {
defined => 1,
required => 1,
store => \$recipe,
strict_type => 1,
},
qc_data_href => {
default => {},
defined => 1,
required => 1,
store => \$qc_data_href,
strict_type => 1,
},
qc_header_href => {
default => {},
defined => 1,
required => 1,
store => \$qc_header_href,
strict_type => 1,
},
qc_recipe_data_href => {
default => {},
defined => 1,
required => 1,
store => \$qc_recipe_data_href,
strict_type => 1,
},
regexp_href => {
default => {},
defined => 1,
required => 1,
store => \$regexp_href,
strict_type => 1,
},
sample_info_href => {
default => {},
defined => 1,
required => 1,
store => \$sample_info_href,
strict_type => 1,
},
sample_id => { store => \$sample_id, strict_type => 1, },
};
check( $tmpl, $arg_href, 1 ) or croak q{Could not parse arguments!};
REGEXP:
for my $regexp_key ( keys %{ $regexp_href->{$recipe} } ) {
## For info contained in entry --> Value i.e. same line
if ( $regexp_key !~ /^header|header$/i ) {
## Enable seperation of writing array or key-->value in qc_data
if ( scalar( @{ $qc_recipe_data_href->{$recipe}{$regexp_key} } ) == 1 ) {
if ( ($sample_id) && ($infile) ) {
## key-->value for sample_id
$qc_data_href->{sample}{$sample_id}{$infile}{$recipe}{$regexp_key} =
$qc_recipe_data_href->{$recipe}{$regexp_key}[0];
}
else {
## Family level
## key-->value for caseID
$qc_data_href->{recipe}{$recipe}{$regexp_key} =
$qc_recipe_data_href->{$recipe}{$regexp_key}[0];
}
## Check gender for sample_id
if ( $recipe eq q{chanjo_sexcheck} ) {
## Array_ref
my $chanjo_sexcheck =
@{ $qc_recipe_data_href->{$recipe}{$regexp_key} }[0];
## Check that assumed gender is supported by coverage on chrX and chrY
_chanjo_gender_check(
{
sample_info_href => $sample_info_href,
qc_data_href => $qc_data_href,
sample_id => $sample_id,
infile => $infile,
chanjo_sexcheck_gender => $chanjo_sexcheck,
}
);
}
}
else {
## Write array to qc_data
for (
my $regexp_key_counter = 0 ;
$regexp_key_counter <
scalar( @{ $qc_recipe_data_href->{$recipe}{$regexp_key} } ) ;
$regexp_key_counter++
)
{
if ( ($sample_id) && ($infile) ) {
$qc_data_href->{sample}{$sample_id}{$infile}{$recipe}
{$regexp_key}[$regexp_key_counter] =
$qc_recipe_data_href->{$recipe}{$regexp_key}
[$regexp_key_counter];
}
else {
$qc_data_href->{recipe}{$recipe}{$regexp_key}[$regexp_key_counter]
= $qc_recipe_data_href->{$recipe}{$regexp_key}
[$regexp_key_counter];
}
## Check gender for sample_id
if ( $recipe eq q{plink_sexcheck}
&& $evaluate_plink_gender )
{
## Array ref
my @sexchecks = split( q{:},
@{ $qc_recipe_data_href->{$recipe}{$regexp_key} }
[$regexp_key_counter] );
## Check that assumed gender is supported by variants on chrX and chrY
plink_gender_check(
{
sample_info_href => $sample_info_href,
qc_data_href => $qc_data_href,
sample_id_ref => \$sexchecks[0],
plink_sexcheck_gender_ref => \$sexchecks[1],
}
);
}
}
if (
defined(
$qc_data_href->{recipe}{relation_check}{sample_relation_check}
)
&& (
defined( $qc_data_href->{recipe}{pedigree_check}{sample_order} ) )
)
{
relation_check(
{
sample_info_href => $sample_info_href,
qc_data_href => $qc_data_href,
relationship_values_ref => \@{
$qc_data_href->{recipe}{relation_check}
{sample_relation_check}
},
sample_orders_ref =>
\@{ $qc_data_href->{recipe}{pedigree_check}{sample_order} },
}
);
}
}
}
else {
## Paragraf data i.e. header and subsequent data lines
HEADER_INFO:
for
my $regexp_header_key ( keys %{ ${$qc_header_href}{$recipe}{$regexp_key} } )
{
PARAGRAPH_KEYS:
for
my $regexp_key_header ( keys %{ $regexp_href->{$recipe}{$regexp_key} } )
{
## All paragraf keys (header and data line(s))
## Detect if the regexp id for headers and not data.
if ( $regexp_key_header !~ /^header|header$/i ) {
## For all collected headers
for (
my $qc_headers_counter = 0 ;
$qc_headers_counter < scalar(
@{
${$qc_header_href}{$recipe}{$regexp_key}
{$regexp_header_key}
}
) ;
$qc_headers_counter++
)
{
if ( ($sample_id) && ($infile) ) {
## Add to qc_data using header element[X] --> data[X] to correctly position elements in qc_data hash
$qc_data_href->{sample}{$sample_id}{$infile}
{$recipe}{$regexp_header_key}{$regexp_key_header}
{ ${$qc_header_href}{$recipe}{$regexp_key}
{$regexp_header_key}[$qc_headers_counter] } =
$qc_recipe_data_href->{$recipe}
{$regexp_key}{$regexp_key_header}[$qc_headers_counter];
}
else {
## Add to qc_data using header element[X] --> data[X] to correctly position elements in qc_data hash
$qc_data_href->{$recipe}{$regexp_header_key}
{$regexp_key_header}
{ ${$qc_header_href}{$recipe}{$regexp_key}
{$regexp_header_key}[$qc_headers_counter] } =
$qc_recipe_data_href->{$recipe}
{$regexp_key}{$regexp_key_header}[$qc_headers_counter];
}
}
}
}
}
}
}
return;
}
sub define_evaluate_metric {
## Function : Sets recipes and recipe metrics and thresholds to be evaluated
## Returns :
## Arguments : $sample_info_href => Info on samples and case hash {REF}
## : $sample_id => Sample ID
my ($arg_href) = @_;
## Flatten argument(s)
my $sample_info_href;
my $sample_id;
my $tmpl = {
sample_info_href => {
required => 1,
defined => 1,
default => {},
strict_type => 1,
store => \$sample_info_href
},
sample_id => {
required => 1,
defined => 1,
strict_type => 1,
store => \$sample_id
},
};
check( $tmpl, $arg_href, 1 ) or croak q{Could not parse arguments!};
$evaluate_metric{$sample_id}{bamstats}{percentage_mapped_reads}{lt} = 95;
$evaluate_metric{$sample_id}{collecthsmetrics}{PCT_TARGET_BASES_10X}{lt} =
0.95;
$evaluate_metric{$sample_id}{collectmultiplemetrics}{PCT_PF_READS_ALIGNED}{lt} = 0.95;
$evaluate_metric{$sample_id}{collectmultiplemetrics}{PCT_ADAPTER}{gt} =
0.0005;
$evaluate_metric{$sample_id}{markduplicates}{fraction_duplicates}{gt} = 0.2;
if ( exists $sample_info_href->{sample}{$sample_id}{expected_coverage} ) {
$evaluate_metric{$sample_id}{collecthsmetrics}{MEAN_TARGET_COVERAGE}{lt} =
$sample_info_href->{sample}{$sample_id}{expected_coverage};
}
$evaluate_metric{mendel}{fraction_of_errors}{gt} = 0.06;
$evaluate_metric{father}{fraction_of_common_variants}{lt} =
0.55;
return;
}
sub evaluate_qc_parameters {
## Function : Evaluate parameters to detect parameters falling below threshold
## Returns :
## Arguments: $qc_data_href => QC data hash {REF}
## : $evaluate_metric_href => Hash for metrics to evaluate
my ($arg_href) = @_;
## Flatten argument(s)
my $qc_data_href;
my $evaluate_metric_href;
my $tmpl = {
qc_data_href => {
required => 1,
defined => 1,
default => {},
strict_type => 1,
store => \$qc_data_href
},
evaluate_metric_href => {
required => 1,
defined => 1,
default => {},
strict_type => 1,
store => \$evaluate_metric_href
},
};
check( $tmpl, $arg_href, 1 ) or croak q{Could not parse arguments!};
RECIPE:
for my $recipe ( keys %{ $qc_data_href->{recipe} } ) {
## Recipe to be evaluated
if ( exists $evaluate_metric_href->{$recipe} ) {
METRIC:
for my $metric ( keys %{ $qc_data_href->{recipe}{$recipe} } ) {
FAMILY_LEVEL:
if ( exists $evaluate_metric_href->{$recipe}{$metric} ) {