<commit_before>package HMF::Pipeline::PostStats;

use FindBin::libs;
use discipline;

use File::Spec::Functions qw(:ALL);

use HMF::Pipeline::Config qw(createDirs sampleBamsAndJobs);
use HMF::Pipeline::Job qw(fromTemplate checkReportedDoneFile markDone);
use HMF::Pipeline::Sge qw(qsubTemplate);
use HMF::Pipeline::Metadata qw(linkArtefact linkExtraArtefact);

use parent qw(Exporter);
our @EXPORT_OK = qw(run);

sub run {
    my ($opt) = @_;

    say "\n### SCHEDULING POSTSTATS ###";

    my %snp_check_dirs = map { $_ => catfile($_, "snpcheck") } keys %{$opt->{SAMPLES}};
    my $dirs = createDirs($opt->{OUTPUT_DIR}, out => "QCStats", %snp_check_dirs);
    # weird relative path requirement
    my $exoncov_dir = catfile($dirs->{out}, "exoncov");
    $dirs->{exoncov} = abs2rel($exoncov_dir, $dirs->{tmp});

    my @designs;
    @designs = split '\t', $opt->{SNPCHECK_DESIGNS} if $opt->{SNPCHECK_DESIGNS};

    my ($sample_bams, $running_jobs) = sampleBamsAndJobs($opt);
    my $qsub = qsubTemplate($opt, "POSTSTATS");
    my $done_file = checkReportedDoneFile("PostStats", undef, $dirs, $opt) or return;
    my $stats_job_id = fromTemplate(
        "PostStats",
        undef,
        0,
        $qsub,
        $running_jobs,
        $dirs,
        $opt,
        sample_bams => $sample_bams,
        designs => \@designs,
    );
    my $check_job_id = fromTemplate(
        "PostStatsCheck",
        undef,
        0,
        $qsub,
        [$stats_job_id],
        $dirs,
        $opt,
        sample_bams => $sample_bams,
        designs => \@designs,
    );
    my $job_id = markDone($done_file, [ $stats_job_id, $check_job_id ], $dirs, $opt);

    $opt->{RUNNING_JOBS}->{poststats} = [$job_id];
    linkArtefacts(\@designs, $exoncov_dir, $dirs, $opt);

    return;
}

sub linkArtefacts {
    my ($designs, $exoncov_dir, $dirs, $opt) = @_;

    # SABR: dependent on implicit bamMetrics naming
    my $metrics_path = catfile($dirs->{out}, "$opt->{RUN_NAME}.bamMetrics.pdf");
    linkArtefact($metrics_path, "qc", $opt);

    foreach my $sample (keys %{$opt->{SAMPLES}}) {
        # SABR: ependent on implicit SNPcheck naming (easier to fix)
        foreach my $design (@{$designs}) {
            linkExtraArtefact(catfile($dirs->{$sample}, "${sample}_${design}.vcf"), $opt);
        }
<commit_msg>Typo corrected<commit_after>package HMF::Pipeline::PostStats;

use FindBin::libs;
use discipline;

use File::Spec::Functions qw(:ALL);

use HMF::Pipeline::Config qw(createDirs sampleBamsAndJobs);
use HMF::Pipeline::Job qw(fromTemplate checkReportedDoneFile markDone);
use HMF::Pipeline::Sge qw(qsubTemplate);
use HMF::Pipeline::Metadata qw(linkArtefact linkExtraArtefact);

use parent qw(Exporter);
our @EXPORT_OK = qw(run);

sub run {
    my ($opt) = @_;

    say "\n### SCHEDULING POSTSTATS ###";

    my %snp_check_dirs = map { $_ => catfile($_, "snpcheck") } keys %{$opt->{SAMPLES}};
    my $dirs = createDirs($opt->{OUTPUT_DIR}, out => "QCStats", %snp_check_dirs);
    # weird relative path requirement
    my $exoncov_dir = catfile($dirs->{out}, "exoncov");
    $dirs->{exoncov} = abs2rel($exoncov_dir, $dirs->{tmp});

    my @designs;
    @designs = split '\t', $opt->{SNPCHECK_DESIGNS} if $opt->{SNPCHECK_DESIGNS};

    my ($sample_bams, $running_jobs) = sampleBamsAndJobs($opt);
    my $qsub = qsubTemplate($opt, "POSTSTATS");
    my $done_file = checkReportedDoneFile("PostStats", undef, $dirs, $opt) or return;
    my $stats_job_id = fromTemplate(
        "PostStats",
        undef,
        0,
        $qsub,
        $running_jobs,
        $dirs,
        $opt,
        sample_bams => $sample_bams,
        designs => \@designs,
    );
    my $check_job_id = fromTemplate(
        "PostStatsCheck",
        undef,
        0,
        $qsub,
        [$stats_job_id],
        $dirs,
        $opt,
        sample_bams => $sample_bams,
        designs => \@designs,
    );
    my $job_id = markDone($done_file, [ $stats_job_id, $check_job_id ], $dirs, $opt);

    $opt->{RUNNING_JOBS}->{poststats} = [$job_id];
    linkArtefacts(\@designs, $exoncov_dir, $dirs, $opt);

    return;
}

sub linkArtefacts {
    my ($designs, $exoncov_dir, $dirs, $opt) = @_;

    # SABR: dependent on implicit bamMetrics naming
    my $metrics_path = catfile($dirs->{out}, "$opt->{RUN_NAME}.bamMetrics.pdf");
    linkArtefact($metrics_path, "qc", $opt);

    foreach my $sample (keys %{$opt->{SAMPLES}}) {
        # SABR: dependent on implicit SNPcheck naming (easier to fix)
        foreach my $design (@{$designs}) {
            linkExtraArtefact(catfile($dirs->{$sample}, "${sample}_${design}.vcf"), $opt);
        }

{'fraction_non_alphanumeric': 0.16616052060737527, 'fraction_numerical': 0.0013015184381778742, 'mean_word_length': 3.3747628083491463, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 41, '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': '6788917', 'n_tokens_mistral': 1731, 'n_tokens_neox': 1687, 'n_words': 341}