AddTogether.pm 4.58 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-2014] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
20 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

    Please contact ehive-users@ebi.ac.uk mailing list with questions/suggestions.
33 34 35

=cut

36

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

use strict;

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

43 44 45 46 47 48 49 50 51 52

=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 {
53 54 55
        '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
56 57 58 59
    };
}


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

    Description : Implements fetch_input() interface method of Bio::EnsEMBL::Hive::Process that is used to read in parameters and load data.
63
                  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
64 65 66 67 68

    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).

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

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

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

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

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

Leo Gordon's avatar
Leo Gordon committed
83 84 85 86 87 88 89
=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

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

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

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

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

Leo Gordon's avatar
Leo Gordon committed
101 102 103
=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.
104
                  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
105 106 107 108

=cut


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

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

Leo Gordon's avatar
Leo Gordon committed
117 118 119 120 121
=head2 _add_together

    Description: this is a private function (not a method) that adds all the products with a shift

=cut
122

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

    my @accu  = ();

    my @b_digits = reverse split(//, $b_multiplier);
    foreach my $b_index (0..(@b_digits-1)) {
        my $b_digit = $b_digits[$b_index];
131
        my $product = $partial_product->{$b_digit};
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155

        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;