<commit_before>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{ catdir catfile devnull };
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 List::MoreUtils qw { any uniq all };
use Modern::Perl qw{ 2018 };
use Path::Iterator::Rule;
use Readonly;

## MIPs lib/
use MIP::Check::Modules qw{ check_perl_modules };
use MIP::Check::Parameter qw{ check_allowed_temp_directory
  check_cmd_config_vs_definition_file
  check_email_address
  check_load_env_packages
  check_parameter_hash
  check_recipe_exists_in_hash
  check_recipe_name
  check_recipe_mode
  check_sample_ids
};
use MIP::Check::Path qw{ check_executable_in_path check_parameter_files };
use MIP::Check::Reference qw{ check_human_genome_file_endings };
use MIP::Constants qw{ $DOT $EMPTY_STR $MIP_VERSION $NEWLINE $SINGLE_QUOTE $SPACE $TAB };
use MIP::Cluster qw{ check_max_core_number check_recipe_memory_allocation };
use MIP::File::Format::Mip qw{ build_file_prefix_tag };
use MIP::File::Format::Pedigree
  qw{ create_fam_file detect_founders detect_sample_id_gender detect_trio parse_yaml_pedigree_file reload_previous_pedigree_info };
use MIP::File::Format::Store qw{ set_analysis_files_to_store };
use MIP::File::Format::Yaml qw{ load_yaml write_yaml order_parameter_names };
use MIP::Get::Analysis qw{ get_overall_analysis_type };
use MIP::Get::Parameter qw{ get_program_executables };
use MIP::Log::MIP_log4perl qw{ initiate_logger set_default_log4perl_file };
use MIP::Parse::Parameter qw{ parse_dynamic_config_parameters parse_start_with_recipe };
use MIP::Processmanagement::Processes qw{ write_job_ids_to_file };
use MIP::Set::Contigs qw{ set_contigs };
use MIP::Set::Parameter qw{ set_config_to_active_parameters
  set_custom_default_to_active_parameter
  set_default_config_dynamic_parameters
  set_default_to_active_parameter
  set_cache
  set_human_genome_reference_features
  set_no_dry_run_parameters
  set_parameter_reference_dir_path
  set_recipe_resource };
use MIP::Update::Parameters qw{ update_reference_parameters
  update_vcfparser_outfile_counter };
use MIP::Update::Path qw{ update_to_absolute_path };
use MIP::Update::Recipes qw{ update_recipe_mode_with_dry_run_all };

## Recipes
use MIP::Recipes::Pipeline::Analyse_dragen_rd_dna qw{ pipeline_analyse_dragen_rd_dna };
use MIP::Recipes::Pipeline::Analyse_rd_dna qw{ pipeline_analyse_rd_dna };
use MIP::Recipes::Pipeline::Analyse_rd_rna qw{ pipeline_analyse_rd_rna };
use MIP::Recipes::Pipeline::Analyse_rd_dna_vcf_rerun
  qw{ pipeline_analyse_rd_dna_vcf_rerun };
use MIP::Sample_info qw{ set_file_path_to_store };

BEGIN {

    use base qw{ Exporter };
    require Exporter;

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

    # Functions and variables which can be optionally exported
    our @EXPORT_OK = qw{ mip_analyse };
}

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!};

    ## Transfer to lexical variables
    # Parameters to include in each analysis run
    my %active_parameter = %{$active_parameter_href};

    # Holds all active parameters values for broadcasting
    my @broadcasts;

    # File information
    my %file_info = %{$file_info_href};

    # Order parameters for logical broadcast of parameters
    my @order_parameters = @{$order_parameters_ref};

    # All parameters MIP analyse knows
    my %parameter = %{$parameter_href};

#### 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} ) );
    chomp( $date_time_stamp, $date, $script );

#### Set program parameters

## Directories, files, job_ids and sample_info
    my ( %infile_lane_prefix, %infile_both_strands_prefix, %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(<commit_msg>feat(rm_chomp): Removed chomp as it is not needed<commit_after>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{ catdir catfile devnull };
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 List::MoreUtils qw { any uniq all };
use Modern::Perl qw{ 2018 };
use Path::Iterator::Rule;
use Readonly;

## MIPs lib/
use MIP::Check::Modules qw{ check_perl_modules };
use MIP::Check::Parameter qw{ check_allowed_temp_directory
  check_cmd_config_vs_definition_file
  check_email_address
  check_load_env_packages
  check_parameter_hash
  check_recipe_exists_in_hash
  check_recipe_name
  check_recipe_mode
  check_sample_ids
};
use MIP::Check::Path qw{ check_executable_in_path check_parameter_files };
use MIP::Check::Reference qw{ check_human_genome_file_endings };
use MIP::Constants qw{ $DOT $EMPTY_STR $MIP_VERSION $NEWLINE $SINGLE_QUOTE $SPACE $TAB };
use MIP::Cluster qw{ check_max_core_number check_recipe_memory_allocation };
use MIP::File::Format::Mip qw{ build_file_prefix_tag };
use MIP::File::Format::Pedigree
  qw{ create_fam_file detect_founders detect_sample_id_gender detect_trio parse_yaml_pedigree_file reload_previous_pedigree_info };
use MIP::File::Format::Store qw{ set_analysis_files_to_store };
use MIP::File::Format::Yaml qw{ load_yaml write_yaml order_parameter_names };
use MIP::Get::Analysis qw{ get_overall_analysis_type };
use MIP::Get::Parameter qw{ get_program_executables };
use MIP::Log::MIP_log4perl qw{ initiate_logger set_default_log4perl_file };
use MIP::Parse::Parameter qw{ parse_dynamic_config_parameters parse_start_with_recipe };
use MIP::Processmanagement::Processes qw{ write_job_ids_to_file };
use MIP::Set::Contigs qw{ set_contigs };
use MIP::Set::Parameter qw{ set_config_to_active_parameters
  set_custom_default_to_active_parameter
  set_default_config_dynamic_parameters
  set_default_to_active_parameter
  set_cache
  set_human_genome_reference_features
  set_no_dry_run_parameters
  set_parameter_reference_dir_path
  set_recipe_resource };
use MIP::Update::Parameters qw{ update_reference_parameters
  update_vcfparser_outfile_counter };
use MIP::Update::Path qw{ update_to_absolute_path };
use MIP::Update::Recipes qw{ update_recipe_mode_with_dry_run_all };

## Recipes
use MIP::Recipes::Pipeline::Analyse_dragen_rd_dna qw{ pipeline_analyse_dragen_rd_dna };
use MIP::Recipes::Pipeline::Analyse_rd_dna qw{ pipeline_analyse_rd_dna };
use MIP::Recipes::Pipeline::Analyse_rd_rna qw{ pipeline_analyse_rd_rna };
use MIP::Recipes::Pipeline::Analyse_rd_dna_vcf_rerun
  qw{ pipeline_analyse_rd_dna_vcf_rerun };
use MIP::Sample_info qw{ set_file_path_to_store };

BEGIN {

    use base qw{ Exporter };
    require Exporter;

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

    # Functions and variables which can be optionally exported
    our @EXPORT_OK = qw{ mip_analyse };
}

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!};

    ## Transfer to lexical variables
    # Parameters to include in each analysis run
    my %active_parameter = %{$active_parameter_href};

    # Holds all active parameters values for broadcasting
    my @broadcasts;

    # File information
    my %file_info = %{$file_info_href};

    # Order parameters for logical broadcast of parameters
    my @order_parameters = @{$order_parameters_ref};

    # All parameters MIP analyse knows
    my %parameter = %{$parameter_href};

#### 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

## Directories, files, job_ids and sample_info
    my ( %infile_lane_prefix, %infile_both_strands_prefix, %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(
{'fraction_non_alphanumeric': 0.09628798204120902, 'fraction_numerical': 0.0046500440952457305, 'mean_word_length': 3.7884836852207293, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 63, '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': '4703532', 'n_tokens_mistral': 4077, 'n_tokens_neox': 3933, 'n_words': 1036}