/usr/share/perl5/GO/Utils.pm is in libgo-perl 0.13-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 | # $Id: Utils.pm,v 1.2 2004/11/24 02:28:00 cmungall Exp $
#
# This GO module is maintained by Chris Mungall <cjm@fruitfly.org>
#
# see also - http://www.geneontology.org
# - http://www.godatabase.org/dev
#
# You may distribute this module under the same terms as perl itself
package GO::Utils;
use Exporter;
@ISA = qw(Exporter);
@EXPORT_OK = qw(rearrange remove_duplicates merge_hashes get_method_ref
get_param pset2hash dd spell_greek max check_obj_graph);
use strict;
use Carp;
use Data::Dumper;
=head1 NAME
GO::Utils - utilities for GO modules
=head2 rearrange()
Usage : n/a
Function : Rearranges named parameters to requested order.
Returns : @params - an array of parameters in the requested order.
Argument : $order : a reference to an array which describes the desired
order of the named parameters.
@param : an array of parameters, either as a list (in
which case the function simply returns the list),
or as an associative array (in which case the
function sorts the values according to @{$order}
and returns that new array.
Exceptions : carps if a non-recognised parameter is sent
=cut
sub rearrange {
# This function was taken from CGI.pm, written by Dr. Lincoln
# Stein, and adapted for use in Bio::Seq by Richard Resnick.
# ...then Chris Mungall came along and adapted it for BDGP
my($order,@param) = @_;
# If there are no parameters, we simply wish to return
# an undef array which is the size of the @{$order} array.
return (undef) x $#{$order} unless @param;
# If we've got parameters, we need to check to see whether
# they are named or simply listed. If they are listed, we
# can just return them.
return @param unless (defined($param[0]) && $param[0]=~/^-/);
# Now we've got to do some work on the named parameters.
# The next few lines strip out the '-' characters which
# preceed the keys, and capitalizes them.
my $i;
for ($i=0;$i<@param;$i+=2) {
if (!defined($param[$i])) {
carp("Hmmm in $i ".join(";", @param)." == ".join(";",@$order)."\n");
}
else {
$param[$i]=~s/^\-//;
$param[$i]=~tr/a-z/A-Z/;
}
}
# Now we'll convert the @params variable into an associative array.
my(%param) = @param;
my(@return_array);
# What we intend to do is loop through the @{$order} variable,
# and for each value, we use that as a key into our associative
# array, pushing the value at that key onto our return array.
my($key);
foreach $key (@{$order}) {
$key=~tr/a-z/A-Z/;
my($value) = $param{$key};
delete $param{$key};
push(@return_array,$value);
}
# catch user misspellings resulting in unrecognized names
my(@restkeys) = keys %param;
if (scalar(@restkeys) > 0) {
carp("@restkeys not processed in rearrange(), did you use a
non-recognized parameter name ? ");
}
return @return_array;
}
=head2 get_param()
Usage : get_param('name',(-att1=>'ben',-name=>'the_name'))
Function : Fetches a named parameter.
Returns : The value of the requested parameter.
Argument : $name : The name of the the parameter desired
@param : an array of parameters, as an associative array
Exceptions : carps if a non-recognised parameter is sent
Based on rearrange(), which is originally from CGI.pm by Lincoln
Stein and BioPerl by Richard Resnick. See rearrange() for details.
=cut
sub get_param
{
# This function was taken from CGI.pm, written by Dr. Lincoln
# Stein, and adapted for use in Bio::Seq by Richard Resnick.
# ...then Chris Mungall came along and adapted it for BDGP
# ... and ben berman added his 2 cents.
my($name,@param) = @_;
# If there are no parameters, we simply wish to return
# false.
return '' unless @param;
# If we've got parameters, we need to check to see whether
# they are named or simply listed. If they are listed, we
# can't return anything.
return '' unless (defined($param[0]) && $param[0]=~/^-/);
# Now we've got to do some work on the named parameters.
# The next few lines strip out the '-' characters which
# preceed the keys, and capitalizes them.
my $i;
for ($i=0;$i<@param;$i+=2) {
$param[$i]=~s/^\-//;
$param[$i] = uc($param[$i]);
}
# Now we'll convert the @params variable into an associative array.
my(%param) = @param;
# We capitalize the key, and use it as a key into our
# associative array
my $key = uc($name);
my $val = $param{$key};
return $val;
}
=head2 remove_duplicates
remove duplicate items from an array
usage: remove_duplicates(\@arr)
affects the array passed in, and returns the modified array
=cut
sub remove_duplicates {
my $arr_r = shift;
my @arr = @{$arr_r};
my %h = ();
my $el;
foreach $el (@arr) {
$h{$el} = 1;
}
my @new_arr = ();
foreach $el (keys %h) {
push (@new_arr, $el);
}
@{$arr_r} = @new_arr;
@new_arr;
}
=head1 merge_hashes
joins two hashes together
usage: merge_hashes(\%h1, \%h2);
%h1 will now contain the key/val pairs of %h2 as well. if there are
key conflicts, %h2 values will take precedence.
=cut
sub merge_hashes {
my ($h1, $h2) = @_;
map {
$h1->{$_} = $h2->{$_};
} keys %{$h2};
return $h1;
}
=head1 get_method_ref
returns a pointer to a particular objects method
e.g. my $length_f = get_method_ref($seq, 'length');
$len = &$length_f();
=cut
sub get_method_ref {
my $self = shift;
my $method = shift;
return sub {return $self->$method(@_)};
}
=head2 pset2hash
Usage - my $h = pset2hash([{name=>"id", value=>"56"}, {name=>"name", value=>"jim"}]);
Returns - hashref
Args - arrayref of name/value keyed hashrefs
=cut
sub pset2hash {
my $pset = shift;
my $h = {};
# printf STDERR "REF=%s;\n", ref($pset);
if (ref($pset) eq "ARRAY") {
map {$h->{$_->{name}} = $_->{value}} @$pset;
}
elsif (ref($pset) eq "HASH") {
$h = $pset;
}
else {
$h = $pset;
}
return $h;
}
sub dd {
my $obj = shift;
my $d= Data::Dumper->new(['obj',$obj]);
print $d->Dump;
}
=head2 spell_greek
takes a word as a parameter and spells out any greek symbols encoded
within (eg s/&agr;/alpha/g)
=cut
sub spell_greek
{
my $name = shift;
$name =~ s/&agr\;/alpha/g;
$name =~ s/&Agr\;/Alpha/g;
$name =~ s/&bgr\;/beta/g;
$name =~ s/&Bgr\;/Beta/g;
$name =~ s/&ggr\;/gamma/g;
$name =~ s/&Ggr\;/Gamma/g;
$name =~ s/&dgr\;/delta/g;
$name =~ s/&Dgr\;/Delta/g;
$name =~ s/&egr\;/epsilon/g;
$name =~ s/&Egr\;/Epsilon/g;
$name =~ s/&zgr\;/zeta/g;
$name =~ s/&Zgr\;/Zeta/g;
$name =~ s/&eegr\;/eta/g;
$name =~ s/&EEgr\;/Eta/g;
$name =~ s/&thgr\;/theta/g;
$name =~ s/&THgr\;/Theta/g;
$name =~ s/&igr\;/iota/g;
$name =~ s/&Igr\;/Iota/g;
$name =~ s/&kgr\;/kappa/g;
$name =~ s/&Kgr\;/Kappa/g;
$name =~ s/&lgr\;/lambda/g;
$name =~ s/&Lgr\;/Lambda/g;
$name =~ s/&mgr\;/mu/g;
$name =~ s/&Mgr\;/Mu/g;
$name =~ s/&ngr\;/nu/g;
$name =~ s/&Ngr\;/Nu/g;
$name =~ s/&xgr\;/xi/g;
$name =~ s/&Xgr\;/Xi/g;
$name =~ s/&ogr\;/omicron/g;
$name =~ s/&Ogr\;/Omicron/g;
$name =~ s/&pgr\;/pi/g;
$name =~ s/&Pgr\;/Pi/g;
$name =~ s/&rgr\;/rho/g;
$name =~ s/&Rgr\;/Rho/g;
$name =~ s/&sgr\;/sigma/g;
$name =~ s/&Sgr\;/Sigma/g;
$name =~ s/&tgr\;/tau/g;
$name =~ s/&Tgr\;/Tau/g;
$name =~ s/&ugr\;/upsilon/g;
$name =~ s/&Ugr\;/Upsilon/g;
$name =~ s/&phgr\;/phi/g;
$name =~ s/&PHgr\;/Phi/g;
$name =~ s/&khgr\;/chi/g;
$name =~ s/&KHgr\;/Chi/g;
$name =~ s/&psgr\;/psi/g;
$name =~ s/&PSgr\;/Psi/g;
$name =~ s/&ohgr\;/omega/g;
$name =~ s/&OHgr\;/Omega/g;
$name =~ s/<up>/\[/g;
$name =~ s/<\/up>/\]/g;
$name =~ s/<down>/\[\[/g;
$name =~ s/<\/down>/\]\]/g;
return $name;
}
=head2 check_obj_graph
Usage -
Returns - true if cycle detected
Args - any object
=cut
sub check_obj_graph {
my $object = shift;
my $h = {};
my $cnt = 1;
my @nodes = ({obj=>$object,path=>[]});
my @path = ();
my $cycle = 0;
while (!$cycle && @nodes) {
my $node = shift @nodes;
my $obj = $node->{obj};
my $id = sprintf("%s", $node->{obj});
if (ref($obj) && $id !~ /GLOB/) {
if (!$h->{$id}) {
$h->{$id} = $cnt;
$cnt++;
}
# check for cycles
if (grep {my $idelt = sprintf("%s", $_); $idelt eq $id}
@{$node->{path}}) {
$cycle = $node;
}
printf
"* OB:%5s %20s [%s]\n",
$h->{$id},
$obj,
join(", ", map {$h->{$_}} @{$node->{path}});
my @newobjs = ();
if (ref($obj) eq "ARRAY") {
@newobjs = @$obj;
}
## if (ref($obj) eq "HASH") {
elsif (ref($obj) eq "GLOB") {
}
else {
@newobjs = values %$obj;
}
map {
my @newpath = (@{$node->{path}}, $obj);
my $newnode = {obj=>$_, path=>\@newpath};
push(@nodes, $newnode);
} @newobjs;
}
}
return $cycle;
}
sub max
{
my @items = @_;
my $max;
my $item;
foreach $item (@items)
{
if (!defined($max))
{
$max = $item;
}
else
{
$max = $item if ($item > $max);
}
}
return $max;
}
1;
|