<commit_before>package CoGe::Builder::Methylation::Metaplot;

use v5.14;
use strict;
use warnings;

use Data::Dumper qw(Dumper);
use File::Basename qw(basename);
use File::Spec::Functions qw(catdir catfile);
use CoGe::Accessory::Web qw(get_defaults);
use CoGe::Core::Storage qw(get_workflow_paths);
use CoGe::Builder::CommonTasks qw(create_gff_generation_job);

our $CONF = CoGe::Accessory::Web::get_defaults();

BEGIN {
    use vars qw ($VERSION @ISA @EXPORT @EXPORT_OK);
    require Exporter;

    $VERSION = 0.1;
    @ISA     = qw(Exporter);
    @EXPORT  = qw(build);
}

sub build {
    my $opts = shift;
    my $genome = $opts->{genome};
    my $user = $opts->{user};
    my $bam_file = $opts->{input_file}; # path to sorted & indexed bam file
#    my $metadata = $opts->{metadata};
#    my $additional_metadata = $opts->{additional_metadata};
    my $wid = $opts->{wid};
    my $methylation_params = $opts->{methylation_params};

    # Setup paths
    my ($staging_dir, $result_dir) = get_workflow_paths($user->name, $wid);

    # Set metadata for the pipeline being used
#    my $annotations = generate_additional_metadata($read_params, $methylation_params);
#    my @annotations2 = CoGe::Core::Metadata::to_annotations($additional_metadata);
#    push @$annotations, @annotations2;

    # Make sure genome is annotated as is required by metaplot script
    my $isAnnotated = $genome->has_gene_features;
    return unless $isAnnotated;

    #
    # Build the workflow
    #
    my (@tasks, @done_files);
    
    # Generate cached gff
    my $gff_task = create_gff_generation_job(
        gid => $genome->id,
        organism_name => $genome->organism->name
    );
    my $gff_file = $gff_task->{outputs}->[0];
    push @tasks, $gff_task;
    
    # Generate metaplot
    my $metaplot_task = create_metaplot_job(
        bam_file => $bam_file,
        gff_file => $gff_file,
        outside => 2000,
        inside => 5000,
        window_size => 100,
        staging_dir => $staging_dir
    );
    push @done_files, @{$metaplot_task->{outputs}};
    push @tasks, $metaplot_task;

    return {
        tasks => \@tasks,
        done_files => \@done_files
    };
}

sub create_metaplot_job {
    my %opts = @_;
    my $bam_file    = $opts{bam_file};
    my $gff_file    = $opts{gff_file};
    my $outside     = $opts{outside};
    my $inside      = $opts{inside};
    my $window_size = $opts{window_size};
    my $staging_dir = $opts{staging_dir};

    my $cmd = catfile($CONF->{SCRIPTDIR}, 'methylation', 'makeMetaplot.pl');

    return {
        cmd => $cmd,
        script => undef,
        args => [
            ['-o', 'metaplot', 0],
            ['-gff', $gff_file, 0],
            ['-outside', $outside, 0],
            ['-inside', $inside, 0],
            ['-w', $window_size, 0],
            ['-outRange', '', 0],
            ['', $bam_file, 0]
        ],
        inputs => [
            $bam_file,
            $bam_file . '.bai',
            $gff_file
        ],
        outputs => [
            catfile($staging_dir, 'metaplot.tab'),
            catfile($staging_dir, 'metaplot.pdf')
        ],
        description => "Generating metaplot..."
    };
}

1;<commit_msg>Add metaplot image as metadata<commit_after>package CoGe::Builder::Methylation::Metaplot;

use v5.14;
use strict;
use warnings;

use Data::Dumper qw(Dumper);
use File::Spec::Functions qw(catfile);
use CoGe::Accessory::Web qw(get_defaults);
use CoGe::Core::Storage qw(get_workflow_paths);
use CoGe::Builder::CommonTasks qw(create_gff_generation_job add_metadata_to_results_job);

our $CONF = CoGe::Accessory::Web::get_defaults();

BEGIN {
    use vars qw ($VERSION @ISA @EXPORT @EXPORT_OK);
    require Exporter;

    $VERSION = 0.1;
    @ISA     = qw(Exporter);
    @EXPORT  = qw(build);
}

sub build {
    my $opts = shift;
    my $genome = $opts->{genome};
    my $user = $opts->{user};
    my $bam_file = $opts->{input_file}; # path to sorted & indexed bam file
#    my $metadata = $opts->{metadata};
#    my $additional_metadata = $opts->{additional_metadata};
    my $wid = $opts->{wid};
    my $methylation_params = $opts->{methylation_params};
    my $metaplot_params = $methylation_params->{metaplot};

    # Setup paths
    my ($staging_dir, $result_dir) = get_workflow_paths($user->name, $wid);

    # Make sure genome is annotated as is required by metaplot script
    my $isAnnotated = $genome->has_gene_features;
    return unless $isAnnotated;

    #
    # Build the workflow
    #
    my (@tasks, @done_files);
    
    # Generate cached gff
    my $gff_task = create_gff_generation_job(
        gid => $genome->id,
        organism_name => $genome->organism->name
    );
    my $gff_file = $gff_task->{outputs}->[0];
    push @tasks, $gff_task;
    
    # Generate metaplot
    my $metaplot_task = create_metaplot_job(
        bam_file => $bam_file,
        gff_file => $gff_file,
        outer => $metaplot_params->{outer},
        inner => $metaplot_params->{inner},
        window_size => $metaplot_params->{window},
        feat_type => 'gene',
        staging_dir => $staging_dir
    );
    push @done_files, @{$metaplot_task->{outputs}};
    push @tasks, $metaplot_task;
    
    # Add metadata
    my $annotations = generate_additional_metadata($metaplot_params, $metaplot_task->{outputs}[1]);
    my $metadata_task = add_metadata_to_results_job(
        user => $user,
        wid => $wid,
        annotations => $annotations,
        staging_dir => $staging_dir,
        done_files => \@done_files
    );
    push @tasks, $metadata_task;

    return {
        tasks => \@tasks,
        done_files => \@done_files
    };
}

sub generate_additional_metadata {
    my $metaplot_params = shift;
    my $metaplot_image_path = shift;
    
    my @annotations;
    push @annotations, "$metaplot_image_path|||metaplot|" . join(' ', map { $_.' '.$metaplot_params->{$_} } ('outer', 'inner', 'window'));
    
    return \@annotations;
}

sub create_metaplot_job {
    my %opts = @_;
    my $bam_file    = $opts{bam_file};
    my $gff_file    = $opts{gff_file};
    my $outer       = $opts{outer} // 2000;
    my $inner       = $opts{inner} // 5000;
    my $window_size = $opts{window} // 100;
    my $feat_type   = $opts{feat_type};
    my $staging_dir = $opts{staging_dir};

    my $cmd = catfile($CONF->{SCRIPTDIR}, 'methylation', 'makeMetaplot.pl');
    my $output_name = 'metaplot';

    return {
        cmd => $cmd,
        script => undef,
        args => [
            ['-o', $output_name, 0],
            ['-gff', $gff_file, 0],
            ['-outside', $outer, 0],
            ['-inside', $inner, 0],
            ['-w', $window_size, 0],
            ['-outRange', '', 0],
            ['-featType', $feat_type, 0],
            ['-quiet', '', 0], # disable frequent printing of feature ID
            ['-bam', $bam_file, 0]
        ],
        inputs => [
            $bam_file,
            $bam_file . '.bai',
            $gff_file
        ],
        outputs => [
            catfile($staging_dir, "$output_name.tab"),
            catfile($staging_dir, "$output_name.png")
        ],
        description => "Generating metaplot..."
    };
}

1;
{'fraction_non_alphanumeric': 0.15263010632344712, 'fraction_numerical': 0.007694459988808058, 'mean_word_length': 2.61608497723824, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 79, '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': '2273281', 'n_tokens_mistral': 2587, 'n_tokens_neox': 2412, 'n_words': 522}