<commit_before>package MIP::Config;

use Carp;
use charnames qw{ :full :short };
use English qw{ -no_match_vars };
use open qw{ :encoding(UTF-8) :std };
use Params::Check qw{ allow check last_error };
use strict;
use utf8;
use warnings;
use warnings qw{ FATAL utf8 };

## CPANM
use autodie qw{ :all };
use List::MoreUtils qw { any };
use Readonly;

BEGIN {
    require Exporter;
    use base qw{ Exporter };

    # Set the version for version checking
    our $VERSION = 1.03;

    # Functions and variables which can be optionally exported
    our @EXPORT_OK = qw{ check_cmd_config_vs_definition_file
      parse_config
      parse_dynamic_config_parameters
      set_config_to_active_parameters
      set_default_config_dynamic_parameters
      write_mip_config };
}

sub check_cmd_config_vs_definition_file {

## Function : Compare keys from config and cmd with definitions file
## Returns  :
## Arguments: $active_parameter_href => Active parameters for this analysis hash {REF}
##          : $parameter_href        => Parameter hash {REF}

    my ($arg_href) = @_;

    ## Flatten argument(s)
    my $active_parameter_href;
    my $parameter_href;

    my $tmpl = {
        active_parameter_href => {
            default     => {},
            defined     => 1,
            required    => 1,
            store       => \$active_parameter_href,
            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!};

    my @allowed_unique_keys =
      ( q{vcfparser_outfile_count}, $active_parameter_href->{case_id} );
    my @unique;

  ACTIVE_PARAMETER:
    foreach my $key ( keys %{$active_parameter_href} ) {

        ## Parameters from definitions file
        if ( not exists $parameter_href->{$key} ) {

            push @unique, $key;
        }
    }
  UNIQUE_KEYS:
    foreach my $unique_key (@unique) {

        ## Do not print if allowed_unique_keys that have been created dynamically from previous runs
        if ( not any { $_ eq $unique_key } @allowed_unique_keys ) {

            say {*STDERR} q{Found illegal key: }
              . $unique_key
              . q{ in config file or command line that is not defined in define_parameters.yaml};
            croak();
        }
    }
    return;
}

sub parse_config {

## Function : Parse config by loading from cli, removing prior dynamic entries,
##            setting to active_parameter and parsing config dynamic parameters
## Returns  : 1
## Arguments: $active_parameter_href => Active parameters for this analysis hash {REF}
##          : $parameter_href        => Parameter hash {REF}

    my ($arg_href) = @_;

    ## Flatten argument(s)
    my $active_parameter_href;
    my $parameter_href;

    my $tmpl = {
        active_parameter_href => {
            default     => {},
            defined     => 1,
            required    => 1,
            store       => \$active_parameter_href,
            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!};

    use MIP::Io::Read qw{ read_from_file };

    ## Loads a YAML file into an arbitrary hash and returns it.
    my %config_parameter = read_from_file(
        {
            format => q{yaml},
            path   => $active_parameter_href->{config_file},
        }
    );

    ## Remove previous analysis specific info not relevant for current run e.g. log file, which is read from pedigree or cmd
    my @remove_keys =
      qw{ found_female found_male found_other gender log_file dry_run_all };

    delete @config_parameter{@remove_keys};

## Set config parameters into %active_parameter unless $parameter
## has been supplied on the command line
    set_config_to_active_parameters(
        {
            active_parameter_href => $active_parameter_href,
            config_parameter_href => \%config_parameter,
        }
    );

    ## Compare keys from config and cmd (%active_parameter) with definitions file (%parameter)
    check_cmd_config_vs_definition_file(
        {
            active_parameter_href => $active_parameter_href,
            parameter_href        => $parameter_href,
        }
    );

    my @config_dynamic_parameters = qw{ cluster_constant_path analysis_constant_path };

    ## Replace config parameter with cmd info for config dynamic parameter
    set_default_config_dynamic_parameters(
        {
            active_parameter_href => $active_parameter_href,
            parameter_href        => $parameter_href,
            parameter_names_ref   => \@config_dynamic_parameters,
        }
    );

    ## Updates first the dynamic config parameters and then all other
    ## parameters to particular user/cluster following specifications<commit_msg>feat(config): Added binary_paths to be removed when reloading previous analysis config<commit_after>package MIP::Config;

use Carp;
use charnames qw{ :full :short };
use English qw{ -no_match_vars };
use open qw{ :encoding(UTF-8) :std };
use Params::Check qw{ allow check last_error };
use strict;
use utf8;
use warnings;
use warnings qw{ FATAL utf8 };

## CPANM
use autodie qw{ :all };
use List::MoreUtils qw { any };
use Readonly;

BEGIN {
    require Exporter;
    use base qw{ Exporter };

    # Set the version for version checking
    our $VERSION = 1.04;

    # Functions and variables which can be optionally exported
    our @EXPORT_OK = qw{ check_cmd_config_vs_definition_file
      parse_config
      parse_dynamic_config_parameters
      set_config_to_active_parameters
      set_default_config_dynamic_parameters
      write_mip_config };
}

sub check_cmd_config_vs_definition_file {

## Function : Compare keys from config and cmd with definitions file
## Returns  :
## Arguments: $active_parameter_href => Active parameters for this analysis hash {REF}
##          : $parameter_href        => Parameter hash {REF}

    my ($arg_href) = @_;

    ## Flatten argument(s)
    my $active_parameter_href;
    my $parameter_href;

    my $tmpl = {
        active_parameter_href => {
            default     => {},
            defined     => 1,
            required    => 1,
            store       => \$active_parameter_href,
            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!};

    my @allowed_unique_keys =
      ( q{vcfparser_outfile_count}, $active_parameter_href->{case_id} );
    my @unique;

  ACTIVE_PARAMETER:
    foreach my $key ( keys %{$active_parameter_href} ) {

        ## Parameters from definitions file
        if ( not exists $parameter_href->{$key} ) {

            push @unique, $key;
        }
    }
  UNIQUE_KEYS:
    foreach my $unique_key (@unique) {

        ## Do not print if allowed_unique_keys that have been created dynamically from previous runs
        if ( not any { $_ eq $unique_key } @allowed_unique_keys ) {

            say {*STDERR} q{Found illegal key: }
              . $unique_key
              . q{ in config file or command line that is not defined in define_parameters.yaml};
            croak();
        }
    }
    return;
}

sub parse_config {

## Function : Parse config by loading from cli, removing prior dynamic entries,
##            setting to active_parameter and parsing config dynamic parameters
## Returns  : 1
## Arguments: $active_parameter_href => Active parameters for this analysis hash {REF}
##          : $parameter_href        => Parameter hash {REF}

    my ($arg_href) = @_;

    ## Flatten argument(s)
    my $active_parameter_href;
    my $parameter_href;

    my $tmpl = {
        active_parameter_href => {
            default     => {},
            defined     => 1,
            required    => 1,
            store       => \$active_parameter_href,
            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!};

    use MIP::Io::Read qw{ read_from_file };

    ## Constants
    ## Remove previous analysis specific info not relevant for current run e.g. log file, which is read from pedigree or cmd
    my @REMOVE_KEYS =
      qw{ binary_path found_female found_male found_other gender log_file dry_run_all };

    ## Loads a YAML file into an arbitrary hash and returns it.
    my %config_parameter = read_from_file(
        {
            format => q{yaml},
            path   => $active_parameter_href->{config_file},
        }
    );

    delete @config_parameter{@REMOVE_KEYS};

## Set config parameters into %active_parameter unless $parameter
## has been supplied on the command line
    set_config_to_active_parameters(
        {
            active_parameter_href => $active_parameter_href,
            config_parameter_href => \%config_parameter,
        }
    );

    ## Compare keys from config and cmd (%active_parameter) with definitions file (%parameter)
    check_cmd_config_vs_definition_file(
        {
            active_parameter_href => $active_parameter_href,
            parameter_href        => $parameter_href,
        }
    );

    my @config_dynamic_parameters = qw{ cluster_constant_path analysis_constant_path };

    ## Replace config parameter with cmd info for config dynamic parameter
    set_default_config_dynamic_parameters(
        {
            active_parameter_href => $active_parameter_href,
            parameter_href        => $parameter_href,
            parameter_names_ref   => \@config_dynamic_parameters,
        }
    );

    ## Updates first the dynamic config parameters and then all other
    ## parameters to particular user/cluster following specifications
{'fraction_non_alphanumeric': 0.08951332560834299, 'fraction_numerical': 0.004055619930475087, 'mean_word_length': 2.3026147959183674, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 83, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '574957', 'n_tokens_mistral': 2970, 'n_tokens_neox': 2844, 'n_words': 841}