ensembl-hive  2.7.0
GrabN.pm
Go to the documentation of this file.
1 =pod
2 
3 =head1 NAME
4 
6 
7 =head1 SYNOPSIS
8 
9  standaloneJob.pl Bio::EnsEMBL::Hive::Examples::Factories::RunnableDB::GrabN -input_id_list '[{"a"=>1},{"a"=>2}]' -debug 1
10 
11 =head1 DESCRIPTION
12 
13  Runnable that takes a list of input_ids ("input_id_list"), removes some elements from it that are dataflown to branch #2
14  whilst the remainder of the list is dataflown is dataflown to branch #1. Several things can be parametrized: the number
15  of elements taken from the list (on each end) and to which branch they are flown.
16  The Runnable makes sense in the Bio::EnsEMBL::Hive::Examples::Factories::PipeConfig::FoldLeft_conf where we can recursively
17  consume the list one element at a time and compute something along the way.
18 
19 =head1 LICENSE
20 
21  See the NOTICE file distributed with this work for additional information
22  regarding copyright ownership.
23 
24  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
25  You may obtain a copy of the License at
26 
27  http://www.apache.org/licenses/LICENSE-2.0
28 
29  Unless required by applicable law or agreed to in writing, software distributed under the License
30  is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  See the License for the specific language governing permissions and limitations under the License.
32 
33 =head1 CONTACT
34 
35  Please subscribe to the Hive mailing list: http://listserver.ebi.ac.uk/mailman/listinfo/ehive-users
36  to discuss Hive-related questions or to be notified of our updates
37 
38 =cut
39 
40 
41 package Bio::EnsEMBL::Hive::Examples::Factories::RunnableDB::GrabN;
42 
43 use strict;
44 use warnings;
45 
46 use base ('Bio::EnsEMBL::Hive::Process');
47 
48 
49 sub param_defaults {
50  return {
51  'grab_n_left' => 1, # How many elements to take from the left-end of the list ("shift")
52  'grab_n_right' => 0, # How many elements to take from the right-end of the list ("pop")
53  'fan_branch_code' => 2, # On which branch flow the elements
54  }
55 }
56 
57 =head2 fetch_input
58 
59  Description : Implements fetch_input() interface method of Bio::EnsEMBL::Hive::Process that is used to read in parameters and load data.
60  This reads the list of hash "input_id_list" and extracts as many elements as requested (following the "grab_n_left" and
61  "grab_n_right" parameters) and push them to the "output_ids" array parameter. This represents the input_ids of the jobs
62  to be created.
63 
64 =cut
65 
66 sub fetch_input {
67  my $self = shift @_;
68 
69  my $input_id_list = $self->param_required('input_id_list');
70  my $grab_n_left = $self->param_required('grab_n_left');
71  my $grab_n_right = $self->param_required('grab_n_right');
72 
73  die "Negative values are not allowed for 'grab_n_left'\n" if $grab_n_left < 0;
74  die "Negative values are not allowed for 'grab_n_right'\n" if $grab_n_right < 0;
75 
76  my @output_ids;
77  if ($grab_n_left+$grab_n_right <= scalar(@$input_id_list)) {
78  push @output_ids, splice(@$input_id_list, 0, $grab_n_left);
79  push @output_ids, splice(@$input_id_list, -$grab_n_right, $grab_n_right);
80 
81  } else {
82  # Take the whole list
83  @output_ids = @$input_id_list;
84  @$input_id_list = ();
85  }
86 
87  $self->param('output_ids', \@output_ids);
88 }
89 
90 
91 =head2 write_output
92 
93  Description : Implements write_output() interface method of Bio::EnsEMBL::Hive::Process that is used to deal with job's output after the execution.
94  This flows all the elements (hashes) of "output_ids" to the branch refered by "fan_branch_code" (default #2)
95  It then flows to branch #1 the remainder of the list, and a "_list_exhausted" flag that tells whether the list is now empty or not.
96 
97 =cut
98 
99 sub write_output {
100  my $self = shift @_;
101 
102  my $output_ids = $self->param_required('output_ids');
103  my $input_id_list = $self->param_required('input_id_list');
104  my $fan_branch_code = $self->param_required('fan_branch_code');
105 
106  # Fan out the jobs (if any)
107  $self->dataflow_output_id($output_ids, $fan_branch_code) if @$output_ids;
108 
109  # Collector job. This dataflow replaces the autoflow
110  $self->dataflow_output_id({'_list_exhausted' => !scalar(@$input_id_list), 'input_id_list' => $input_id_list}, 1);
111 }
112 
113 1;
114 
Bio::EnsEMBL::Hive::Version
Definition: Version.pm:19
Bio::EnsEMBL::Hive::Examples::Factories::RunnableDB::GrabN
Definition: GrabN.pm:22
debug
public debug()
Bio::EnsEMBL::Hive::Examples::Factories::PipeConfig::FoldLeft_conf
Definition: FoldLeft_conf.pm:40
Bio::EnsEMBL::Hive
Definition: Hive.pm:38
Bio
Definition: AltAlleleGroup.pm:4