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

use FindBin::libs;
use discipline;

use File::Basename;
use File::Spec::Functions;

use HMF::Pipeline::Config qw(createDirs addSubDir sampleBamAndJobs sampleControlBamsAndJobs);
use HMF::Pipeline::Job qw(fromTemplate checkReportedDoneFile markDone);
use HMF::Pipeline::Metadata;
use HMF::Pipeline::Sge qw(qsubTemplate);
use HMF::Pipeline::Template qw(writeFromTemplate);

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


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

    say "\n### SCHEDULING COPY NUMBER TOOLS ###";

    if ($opt->{CNV_MODE} eq "sample_control") {
        my ($ref_sample, $tumor_sample, $ref_bam_path, $tumor_bam_path, $joint_name, $running_jobs) = sampleControlBamsAndJobs($opt);
        my $job_id = runSampleCnv($tumor_sample, $ref_sample, $tumor_bam_path, $ref_bam_path, $joint_name, $running_jobs, $opt);
        push @{$opt->{RUNNING_JOBS}->{cnv}}, $job_id;
    } elsif ($opt->{CNV_MODE} eq "sample") {
        foreach my $sample (keys %{$opt->{SAMPLES}}) {
            my ($sample_bam, $running_jobs) = sampleBamAndJobs($sample, $opt);
            my $job_id = runSampleCnv($sample, undef, $sample_bam, undef, $sample, $running_jobs, $opt);
            push @{$opt->{RUNNING_JOBS}->{cnv}}, $job_id;
        }
    }
    return;
}

sub runSampleCnv {
    my ($sample, $control, $sample_bam, $control_bam, $joint_name, $running_jobs, $opt) = @_;

    my $dirs = createDirs(catfile($opt->{OUTPUT_DIR}, "copyNumber", $joint_name));

    $control_bam //= "";
    say "\n$joint_name \t $control_bam \t $sample_bam";

    my @cnv_jobs;
    my $done_file = checkReportedDoneFile($joint_name, undef, $dirs, $opt) or return;
    push @cnv_jobs, runFreec($sample, $control, $sample_bam, $control_bam, $running_jobs, $dirs, $opt) if $opt->{CNV_FREEC} eq "yes";
    push @cnv_jobs, runQDNAseq($sample, $sample_bam, $running_jobs, $dirs, $opt) if $opt->{CNV_QDNASEQ} eq "yes";
    my $job_id = markDone($done_file, \@cnv_jobs, $dirs, $opt);
    return $job_id;
}

sub runFreec {
    my ($sample_name, $control_name, $sample_bam, $control_bam, $running_jobs, $dirs, $opt) = @_;

    say "\n### SCHEDULING FREEC ###";

    $dirs->{freec}{out} = addSubDir($dirs, "freec");

    my ($sample_path, $control_path);
    #    if ($opt->{FREEC_BAF} eq "yes") {
    #        $sample_path = $opt->{PILEUP_FILES}->{$sample_name};
    #        $control_path = $control_name ? $opt->{PILEUP_FILES}->{$control_name} : "";
    #    } else {
    $sample_path = $sample_bam;
    $control_path = $control_bam;
    #    }

    my $config_file = catfile($dirs->{freec}{out}, "freec_config.txt");
    writeFromTemplate(
        "FreecConfig.tt",
        $config_file,
        sample_path => $sample_path,
        control_path => $control_path,
        dirs => $dirs,
        opt => $opt,
    );

    my @dependent_jobs = @{$running_jobs};

    my $sample_file_name = fileparse($sample_path);
    my $job_id = fromTemplate(
        "Freec",
        undef,
        1,
        qsubTemplate($opt, "FREEC"),<commit_msg>revert to using pileup for Freec<commit_after>package HMF::Pipeline::CopyNumber;

use FindBin::libs;
use discipline;

use File::Basename;
use File::Spec::Functions;

use HMF::Pipeline::Config qw(createDirs addSubDir sampleBamAndJobs sampleControlBamsAndJobs);
use HMF::Pipeline::Job qw(fromTemplate checkReportedDoneFile markDone);
use HMF::Pipeline::Metadata;
use HMF::Pipeline::Sge qw(qsubTemplate);
use HMF::Pipeline::Template qw(writeFromTemplate);

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


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

    say "\n### SCHEDULING COPY NUMBER TOOLS ###";

    if ($opt->{CNV_MODE} eq "sample_control") {
        my ($ref_sample, $tumor_sample, $ref_bam_path, $tumor_bam_path, $joint_name, $running_jobs) = sampleControlBamsAndJobs($opt);
        my $job_id = runSampleCnv($tumor_sample, $ref_sample, $tumor_bam_path, $ref_bam_path, $joint_name, $running_jobs, $opt);
        push @{$opt->{RUNNING_JOBS}->{cnv}}, $job_id;
    } elsif ($opt->{CNV_MODE} eq "sample") {
        foreach my $sample (keys %{$opt->{SAMPLES}}) {
            my ($sample_bam, $running_jobs) = sampleBamAndJobs($sample, $opt);
            my $job_id = runSampleCnv($sample, undef, $sample_bam, undef, $sample, $running_jobs, $opt);
            push @{$opt->{RUNNING_JOBS}->{cnv}}, $job_id;
        }
    }
    return;
}

sub runSampleCnv {
    my ($sample, $control, $sample_bam, $control_bam, $joint_name, $running_jobs, $opt) = @_;

    my $dirs = createDirs(catfile($opt->{OUTPUT_DIR}, "copyNumber", $joint_name));

    $control_bam //= "";
    say "\n$joint_name \t $control_bam \t $sample_bam";

    my @cnv_jobs;
    my $done_file = checkReportedDoneFile($joint_name, undef, $dirs, $opt) or return;
    push @cnv_jobs, runFreec($sample, $control, $sample_bam, $control_bam, $running_jobs, $dirs, $opt) if $opt->{CNV_FREEC} eq "yes";
    push @cnv_jobs, runQDNAseq($sample, $sample_bam, $running_jobs, $dirs, $opt) if $opt->{CNV_QDNASEQ} eq "yes";
    my $job_id = markDone($done_file, \@cnv_jobs, $dirs, $opt);
    return $job_id;
}

sub runFreec {
    my ($sample_name, $control_name, $sample_bam, $control_bam, $running_jobs, $dirs, $opt) = @_;

    say "\n### SCHEDULING FREEC ###";

    $dirs->{freec}{out} = addSubDir($dirs, "freec");

    my ($sample_path, $control_path);
        if ($opt->{FREEC_BAF} eq "yes") {
            $sample_path = $opt->{PILEUP_FILES}->{$sample_name};
            $control_path = $control_name ? $opt->{PILEUP_FILES}->{$control_name} : "";
        } else {
    $sample_path = $sample_bam;
    $control_path = $control_bam;
        }

    my $config_file = catfile($dirs->{freec}{out}, "freec_config.txt");
    writeFromTemplate(
        "FreecConfig.tt",
        $config_file,
        sample_path => $sample_path,
        control_path => $control_path,
        dirs => $dirs,
        opt => $opt,
    );

    my @dependent_jobs = @{$running_jobs};

    my $sample_file_name = fileparse($sample_path);
    my $job_id = fromTemplate(
        "Freec",
        undef,
        1,
        qsubTemplate($opt, "FREEC"),
{'fraction_non_alphanumeric': 0.16704918032786886, 'fraction_numerical': 0.0003278688524590164, 'mean_word_length': 3.7888540031397175, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 45, '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': '7564186', 'n_tokens_mistral': 2369, 'n_tokens_neox': 2294, 'n_words': 450}