ensembl-hive  2.7.0
Argument.pm
Go to the documentation of this file.
1 =head1 LICENSE
2 
3 See the NOTICE file distributed with this work for additional information
4 regarding copyright ownership.
5 
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9 
10  http://www.apache.org/licenses/LICENSE-2.0
11 
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 
18 =cut
19 
20 
21 =head1 CONTACT
22 
23  Please email comments or questions to the public Ensembl
24  developers list at <http://lists.ensembl.org/mailman/listinfo/dev>.
25 
26  Questions may also be sent to the Ensembl help desk at
27  <http://www.ensembl.org/Help/Contact>.
28 
29 =cut
30 
31 =head1 NAME
32 
33 Bio::EnsEMBL::Utils::Argument - Utility functions for argument handling
34 
35 =head1 SYNOPSIS
36 
37  use Bio::EnsEMBL::Utils::Argument qw(rearrange)
38 
39  package Feature;
40 
41  sub new {
42  my $class = shift;
43  my ( $start, $end, $strand ) =
44  rearrange( [ 'START', 'END', 'STRAND' ], @_ );
45 
46  return
47  bless( { 'start' => $start, 'end' => $end, 'strand' => $strand },
48  $class );
49  }
50 
51 =head1 DESCRIPTION
52 
53 This is derived from the Bio::Root module in BioPerl. The _rearrange
54 object method taken from BioPerl has been renamed rearrange and is now
55 a static class method. This method was originally written by Lincoln
56 Stein, and has since been refactored several times by various people (as
57 described below).
58 
59 It is recommended that this package be used instead of inheriting
60 unnecessarily from the Bio::EnsEMBL::Root or Bio::Root object.
61 
62 =head1 METHODS
63 
64 =cut
65 
67 
68 use strict;
69 use warnings;
70 
71 #
72 # Interface with some of the module function XS reimplementation
73 #
74 # If Bio::EnsEMBL::XS is installed, assign the function glob to
75 # the XS counterpart, otherwise assign to the original function
76 #
77 BEGIN {
78 
79  if (eval { require Bio::EnsEMBL::XS; 1 }) {
80  *rearrange = \&Bio::EnsEMBL::XS::Utils::Argument::rearrange;
81  } else {
82  *rearrange = \&rearrange_pp;
83  }
84 
85 }
86 
87 use Exporter;
88 
89 use vars qw(@ISA @EXPORT);
90 
91 @ISA = qw(Exporter);
92 @EXPORT = qw(rearrange);
93 
94 
95 
96 =head2 rearrange_pp
97 
98  Usage : rearrange( array_ref, list_of_arguments)
99  Purpose : Rearranges named parameters to requested order.
100  Example : use Bio::EnsEMBL::Utils::Argument qw(rearrange);
101  : rearrange([qw(SEQUENCE ID DESC)],@param);
102  : Where @param = (-sequence => $s,
103  : -id => $i,
104  : -desc => $d);
105  Returns : @params - an array of parameters in the requested order.
106  : The above example would return ($s, $i, $d)
107  Argument : $order : a reference to an array which describes the desired
108  : order of the named parameters.
109  : @param : an array of parameters, either as a list (in
110  : which case the function simply returns the list),
111  : or as an associative array with hyphenated tags
112  : (in which case the function sorts the values
113  : according to @{$order} and returns that new array.)
114  : The tags can be upper, lower, or mixed case
115  : but they must start with a hyphen (at least the
116  : first one should be hyphenated.)
117  Source : This function was taken from CGI.pm, written by Dr. Lincoln
118  : Stein, and adapted for use in Bio::Seq by Richard Resnick and
119  : then adapted for use in Bio::Root::Object.pm by Steve A. Chervitz.
120  : This has since been adapted as an exported static method in this
122  Comments : (SAC)
123  : This method may not be appropriate for method calls that are
124  : within in an inner loop if efficiency is a concern.
125  :
126  : Parameters can be specified using any of these formats:
127  : @param = (-name=>'me', -color=>'blue');
128  : @param = (-NAME=>'me', -COLOR=>'blue');
129  : @param = (-Name=>'me', -Color=>'blue');
130  : A leading hyphenated argument is used by this function to
131  : indicate that named parameters are being used.
132  : Therefore, a ('me', 'blue') list will be returned as-is.
133  :
134  : Note that Perl will confuse unquoted, hyphenated tags as
135  : function calls if there is a function of the same name
136  : in the current namespace:
137  : -name => 'foo' is interpreted as -&name => 'foo'
138  :
139  : For ultimate safety, put single quotes around the tag:
140  : ('-name'=>'me', '-color' =>'blue');
141  : This can be a bit cumbersome and I find not as readable
142  : as using all uppercase, which is also fairly safe:
143  : (-NAME=>'me', -COLOR =>'blue');
144  :
145  : Personal note (SAC): I have found all uppercase tags to
146  : be more managable: it involves less single-quoting,
147  : the code is more readable, and there are no method naming
148  : conlicts.
149  : Regardless of the style, it greatly helps to line
150  : the parameters up vertically for long/complex lists.
151 
152 =cut
153 
154 
155 sub rearrange_pp {
156  my $order = shift;
157 
158  if ( $order eq "Bio::EnsEMBL::Utils::Argument" ) {
159  # skip object if one provided
160  $order = shift;
161  }
162 
163  # If we've got parameters, we need to check to see whether
164  # they are named or simply listed. If they are listed, we
165  # can just return them.
166  unless ( @_ && $_[0] && substr( $_[0], 0, 1 ) eq '-' ) {
167  return @_;
168  }
169 
170  # Push undef onto the end if % 2 != 0 to stop warnings
171  push @_,undef unless $#_ %2;
172  my %param;
173  while( @_ ) {
174  #deletes all dashes & uppercases at the same time
175  (my $key = shift) =~ tr/a-z\055/A-Z/d;
176  $param{$key} = shift;
177  }
178 
179  # What we intend to do is loop through the @{$order} variable,
180  # and for each value, we use that as a key into our associative
181  # array, pushing the value at that key onto our return array.
182  return map { $param{uc($_)} } @$order;
183 }
184 
185 1;
186 
187 
map
public map()
Bio::EnsEMBL::Utils::Argument::rearrange_pp
public rearrange_pp()
BEGIN
public BEGIN()
Bio
Definition: AltAlleleGroup.pm:4
Bio::EnsEMBL::Utils::Argument
Definition: Argument.pm:34