StandaloneJob.pm 5.46 KB
Newer Older
1 2
=head1 LICENSE

3
Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
nwillhoft's avatar
nwillhoft committed
4
Copyright [2016-2021] EMBL-European Bioinformatics Institute
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

=cut

20 21 22 23 24 25 26

package Bio::EnsEMBL::Hive::Scripts::StandaloneJob;

use strict;
use warnings;

use Bio::EnsEMBL::Hive::Process;
27
use Bio::EnsEMBL::Hive::GuestProcess;
28 29 30 31
use Bio::EnsEMBL::Hive::AnalysisJob;
use Bio::EnsEMBL::Hive::Utils ('load_file_or_module', 'stringify', 'destringify');

sub standaloneJob {
32
    my ($module_or_file, $input_id, $flags, $flow_into, $language) = @_;
33

34
    my $runnable_module = $language ? 'Bio::EnsEMBL::Hive::GuestProcess' : load_file_or_module( $module_or_file );
35

36 37

    my $runnable_object = $runnable_module->new($language, $module_or_file);    # Only GuestProcess will read the arguments
38
    die "Runnable $module_or_file not created\n" unless $runnable_object;
39 40 41
    $runnable_object->debug($flags->{debug}) if $flags->{debug};
    $runnable_object->execute_writes(not $flags->{no_write});

42 43 44 45
    my $dummy_analysis = Bio::EnsEMBL::Hive::Analysis->new(
        'logic_name'    => 'Standalone_Dummy_Analysis',     # looks nicer when printing out DFRs
        'dbID'          => -1,
    );
46

47
    my $job = Bio::EnsEMBL::Hive::AnalysisJob->new(
48
        'analysis'  => $dummy_analysis,
49 50 51 52 53
        'input_id'  => $input_id,
        'dbID'      => -1,
    );

    $job->load_parameters( $runnable_object );
54

55
    Bio::EnsEMBL::Hive::DataflowRule->collection( Bio::EnsEMBL::Hive::Utils::Collection->new() );
56 57 58

    $flow_into = $flow_into ? destringify($flow_into) : []; # empty dataflow for branch 1 by default

59
    # -------------------------------------------- DFR parser cloned from HiveGeneric_conf
60

61
        $flow_into   = { 1 => $flow_into } unless(ref($flow_into) eq 'HASH'); # force non-hash into a hash
62

63
        my %group_tag_to_funnel_dataflow_rule = ();
64

65
        my $semaphore_sign = '->';
66

67 68
        my @all_branch_tags = keys %$flow_into;
        foreach my $branch_tag ((grep {/^[A-Z]$semaphore_sign/} @all_branch_tags), (grep {/$semaphore_sign[A-Z]$/} @all_branch_tags), (grep {!/$semaphore_sign/} @all_branch_tags)) {
69

70 71 72 73 74 75 76 77 78 79 80 81
            my ($branch_name_or_code, $group_role, $group_tag);

            if($branch_tag=~/^([A-Z])$semaphore_sign(-?\w+)$/) {
                ($branch_name_or_code, $group_role, $group_tag) = ($2, 'funnel', $1);
            } elsif($branch_tag=~/^(-?\w+)$semaphore_sign([A-Z])$/) {
                ($branch_name_or_code, $group_role, $group_tag) = ($1, 'fan', $2);
            } elsif($branch_tag=~/^(-?\w+)$/) {
                ($branch_name_or_code, $group_role, $group_tag) = ($1, '');
            } elsif($branch_tag=~/:/) {
                die "Please use newer '2${semaphore_sign}A' and 'A${semaphore_sign}1' notation instead of '2:1' and '1'\n";
            } else {
                die "Error parsing the group tag '$branch_tag'\n";
82
            }
83 84 85 86 87 88 89

            my $funnel_dataflow_rule = undef;    # NULL by default

            if($group_role eq 'fan') {
                unless($funnel_dataflow_rule = $group_tag_to_funnel_dataflow_rule{$group_tag}) {
                    die "No funnel dataflow_rule defined for group '$group_tag'\n";
                }
90
            }
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121

            my $heirs = $flow_into->{$branch_tag};
            $heirs = [ $heirs ] unless(ref($heirs)); # force scalar into an arrayref first
            $heirs = { map { ($_ => undef) } @$heirs } if(ref($heirs) eq 'ARRAY'); # now force it into a hash if it wasn't

            while(my ($heir_url, $input_id_template_list) = each %$heirs) {

                $input_id_template_list = [ $input_id_template_list ] unless(ref($input_id_template_list) eq 'ARRAY');  # allow for more than one template per analysis

                foreach my $input_id_template (@$input_id_template_list) {

                    my $df_rule = Bio::EnsEMBL::Hive::DataflowRule->add_new_or_update(
                        'from_analysis'             => $dummy_analysis,
                        'to_analysis_url'           => $heir_url,
                        'branch_code'               => $branch_name_or_code,
                        'funnel_dataflow_rule'      => $funnel_dataflow_rule,
                        'input_id_template'         => $input_id_template,
                    );

                    if($group_role eq 'funnel') {
                        if($group_tag_to_funnel_dataflow_rule{$group_tag}) {
                            die "More than one funnel dataflow_rule defined for group '$group_tag'\n";
                        } else {
                            $group_tag_to_funnel_dataflow_rule{$group_tag} = $df_rule;
                        }
                    }
                } # /for all templates
            } # /for all heirs
        } # /for all branch_tags

    # -------------------------------------------- / DFR parser cloned from HiveGeneric_conf
122 123 124 125 126 127 128 129 130 131 132

    $runnable_object->input_job($job);
    $runnable_object->life_cycle();

    $runnable_object->cleanup_worker_temp_directory() unless $flags->{no_cleanup};

    return !$job->died_somewhere()
}


1;