ensembl-hive  2.7.0
PartMultiply.pm
Go to the documentation of this file.
1 =pod
2 
3 =head1 NAME
4 
6 
7 =head1 SYNOPSIS
8 
9  Please refer to Bio::EnsEMBL::Hive::Examples::LongMult::PipeConfig::LongMult_conf pipeline configuration file
10  to understand how this particular example pipeline is configured and ran.
11 
12 =head1 DESCRIPTION
13 
14  'Bio::EnsEMBL::Hive::Examples::LongMult::RunnableDB::PartMultiply' has a separate task of multiplying 'a_multiplier' by the given 'digit',
15  then it passes its partial product on.
16 
17 =head1 LICENSE
18 
19  See the NOTICE file distributed with this work for additional information
20  regarding copyright ownership.
21 
22  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
23  You may obtain a copy of the License at
24 
25  http://www.apache.org/licenses/LICENSE-2.0
26 
27  Unless required by applicable law or agreed to in writing, software distributed under the License
28  is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29  See the License for the specific language governing permissions and limitations under the License.
30 
31 =head1 CONTACT
32 
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 =cut
36 
37 
38 package Bio::EnsEMBL::Hive::Examples::LongMult::RunnableDB::PartMultiply;
39 
40 use strict;
41 use warnings;
42 
43 use base ('Bio::EnsEMBL::Hive::Process');
44 
45 
46 =head2 param_defaults
47 
48  Description : Implements param_defaults() interface method of Bio::EnsEMBL::Hive::Process that defines module defaults for parameters.
49 
50 =cut
51 
52 sub param_defaults {
53 
54  return {
55  'take_time' => 0, # how much time run() method will spend in sleeping state
56  };
57 }
58 
59 
60 =head2 fetch_input
61 
62  Description : Implements fetch_input() interface method of Bio::EnsEMBL::Hive::Process that is used to read in parameters and load data.
63  Here we have nothing to fetch.
64 
65 =cut
66 
67 sub fetch_input {
68 }
69 
70 =head2 run
71 
72  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).
73  The only thing we do here is make a call to the recursive function that will compute the product.
74 
75  param('a_multiplier'): The first long number (a string of digits - doesn't have to fit a register).
76 
77  param('digit'): A decimal digit that is a part of the second multiplier.
78 
79  param('take_time'): How much time to spend sleeping (seconds).
80 
81 =cut
82 
83 sub run { # call the recursive function that will compute the stuff
84  my $self = shift @_;
85 
86  my $a_multiplier = $self->param_required('a_multiplier');
87  my $digit = $self->param_required('digit');
88 
89  $self->param('product', _rec_multiply($a_multiplier, $digit, 0) || 0);
90 
91  sleep( $self->param('take_time') );
92 }
93 
94 =head2 write_output
95 
96  Description : Implements write_output() interface method of Bio::EnsEMBL::Hive::Process that is used to deal with job's output after the execution.
97  Dataflows the intermediate results down branch 1, which will be routed into 'partial_product' accumulator.
98 
99 =cut
100 
101 sub write_output { # but this time we have something to store
102  my $self = shift @_;
103 
104  $self->dataflow_output_id( {
105  'product' => $self->param('product')
106  }, 1);
107 }
108 
109 =head2 _rec_multiply
110 
111  Description: this is a private function (not a method) that performs recursive multiplication of a long number by a digit with a carry.
112 
113 =cut
114 
115 sub _rec_multiply {
116  my ($a_multiplier, $digit, $carry) = @_;
117 
118  # recursion end:
119  unless($a_multiplier) {
120  return ($carry || '');
121  }
122 
123  # recursion step:
124  if($a_multiplier=~/^(\d*)(\d)$/) {
125  my ($prefix, $last_digit) = ($1, $2);
126 
127  my $this_product = $last_digit*$digit+$carry;
128  my $this_result = $this_product % 10;
129  my $this_carry = int($this_product / 10);
130 
131  return _rec_multiply($prefix, $digit, $this_carry).$this_result;
132  } else {
133  die "'a_multiplier' has to be a decimal number";
134  }
135 }
136 
137 1;
138 
Bio::EnsEMBL::Hive::Examples::LongMult::RunnableDB::PartMultiply
Definition: PartMultiply.pm:20
Bio::EnsEMBL::Hive::Version
Definition: Version.pm:19
main
public main()
run
public run()
Bio::EnsEMBL::Hive
Definition: Hive.pm:38
Bio::EnsEMBL::Hive::Examples::LongMult::PipeConfig::LongMult_conf
Definition: LongMult_conf.pm:47
Bio
Definition: AltAlleleGroup.pm:4