AddTogether.pm 4.92 KB
Newer Older
1 2 3 4
=pod 

=head1 NAME

5
    Bio::EnsEMBL::Hive::RunnableDB::LongMult::AddTogether
6

Leo Gordon's avatar
Leo Gordon committed
7 8
=head1 SYNOPSIS

9 10
    Please refer to Bio::EnsEMBL::Hive::PipeConfig::LongMult_conf pipeline configuration file
    to understand how this particular example pipeline is configured and ran.
Leo Gordon's avatar
Leo Gordon committed
11

12 13
=head1 DESCRIPTION

14 15 16 17 18
    'LongMult::AddTogether' is the final step of the pipeline that, naturally, adds the products together
    and stores the result in 'final_result' database table.

=head1 LICENSE

19
    Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
Matthieu Muffato's avatar
Matthieu Muffato committed
20
    Copyright [2016-2018] EMBL-European Bioinformatics Institute
21 22 23 24 25 26 27 28 29 30 31 32

    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.

=head1 CONTACT

33
    Please subscribe to the Hive mailing list:  http://listserver.ebi.ac.uk/mailman/listinfo/ehive-users  to discuss Hive-related questions or to be notified of our updates
34 35 36

=cut

37

38 39 40 41
package Bio::EnsEMBL::Hive::RunnableDB::LongMult::AddTogether;

use strict;

42
use base ('Bio::EnsEMBL::Hive::Process');
43

44 45 46 47 48 49 50 51 52 53

=head2 param_defaults

    Description : Implements param_defaults() interface method of Bio::EnsEMBL::Hive::Process that defines module defaults for parameters.

=cut

sub param_defaults {

    return {
54 55 56
        'partial_product' => { },   # to be used when b_multiplier only contains digits '0' and '1'

        'take_time' => 0,           # how much time run() method will spend in sleeping state
57 58 59 60
    };
}


Leo Gordon's avatar
Leo Gordon committed
61 62 63
=head2 fetch_input

    Description : Implements fetch_input() interface method of Bio::EnsEMBL::Hive::Process that is used to read in parameters and load data.
64
                  Here all relevant partial products are fetched from the 'partial_product' accumulator and stored in a hash for future use.
Leo Gordon's avatar
Leo Gordon committed
65 66 67 68 69

    param('a_multiplier'):  The first long number (a string of digits - doesn't have to fit a register).

    param('b_multiplier'):  The second long number (also a string of digits).

70 71
    param('take_time'):     How much time to spend sleeping (seconds).

Leo Gordon's avatar
Leo Gordon committed
72 73
=cut

74 75 76
sub fetch_input {   # fetch all the (relevant) precomputed products
    my $self = shift @_;

77 78
    my $a_multiplier    = $self->param_required('a_multiplier');
    my $partial_product = $self->param('partial_product');
79

80 81
    $partial_product->{1} = $a_multiplier;
    $partial_product->{0} = 0;
82 83
}

Leo Gordon's avatar
Leo Gordon committed
84 85 86 87 88 89 90
=head2 run

    Description : Implements run() interface method of Bio::EnsEMBL::Hive::Process that is used to perform the main bulk of the job (minus input and output).
                  The only thing we do here is make a call to the function that will add together the intermediate results.

=cut

91 92 93
sub run {   # call the function that will compute the stuff
    my $self = shift @_;

94 95
    my $b_multiplier    = $self->param_required('b_multiplier');
    my $partial_product = $self->param('partial_product');
96

97
    $self->param('result', $self->_add_together($b_multiplier, $partial_product));
98 99

    sleep( $self->param('take_time') );
100 101
}

Leo Gordon's avatar
Leo Gordon committed
102 103 104
=head2 write_output

    Description : Implements write_output() interface method of Bio::EnsEMBL::Hive::Process that is used to deal with job's output after the execution.
105
                  Dataflows both original multipliers and the final result down branch-1, which will be routed into 'final_result' table.
Leo Gordon's avatar
Leo Gordon committed
106 107 108 109

=cut


110
sub write_output {  # store and dataflow
111 112
    my $self = shift @_;

113
    $self->dataflow_output_id({
114
        'result'       => $self->param('result'),
115
    }, 1);
116 117
}

118

Leo Gordon's avatar
Leo Gordon committed
119 120
=head2 _add_together

121
    Description: this is a private method that adds all the products with a shift
Leo Gordon's avatar
Leo Gordon committed
122 123

=cut
124

Leo Gordon's avatar
Leo Gordon committed
125
sub _add_together {
126
    my ($self, $b_multiplier, $partial_product) = @_;
127 128 129 130 131 132

    my @accu  = ();

    my @b_digits = reverse split(//, $b_multiplier);
    foreach my $b_index (0..(@b_digits-1)) {
        my $b_digit = $b_digits[$b_index];
133 134 135 136
        if (not exists $partial_product->{$b_digit}) {
           $self->input_job->transient_error(0);
           die "Could not find the partial product for the digit '$b_digit'\n";
        }
137
        my $product = $partial_product->{$b_digit};
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161

        my @p_digits = reverse split(//, $product);
        foreach my $p_index (0..(@p_digits-1)) {
            $accu[$b_index+$p_index] += $p_digits[$p_index];
        }
    }

    foreach my $a_index (0..(@accu-1)) {
        my $a_digit       = $accu[$a_index];
        my $carry         = int($a_digit/10);
        $accu[$a_index]   = $a_digit % 10;
        $accu[$a_index+1] += $carry;
    }

        # get rid of the leading zero
    unless($accu[@accu-1]) {
        pop @accu;
    }

    return join('', reverse @accu);
}

1;