3 See the NOTICE file distributed with
this work
for additional information
4 regarding copyright ownership.
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
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.
23 Please email comments or questions to the
public Ensembl
24 developers list at <http:
26 Questions may also be sent to the Ensembl help desk at
43 package Bio::EnsEMBL::Utils::ScriptUtils;
47 no warnings
'uninitialized';
50 our @ISA = qw(Exporter);
66 Arg[1] : (optional) String $text - notification text to present to user
67 Example : #
run a code snipped conditionally
68 if ($support->user_proceed(
"Run the next code snipped?")) {
72 # exit if requested by user
73 exit unless ($support->user_proceed(
"Want to continue?"));
74 Description : If running interactively, the user is asked
if he wants to
75 perform a script action. If he doesn
't, this section is skipped
76 and the script proceeds with the code. When running
77 non-interactively, the section is run by default.
78 Return type : TRUE to proceed, FALSE to skip.
85 my ($text, $interactive, $default) = @_;
87 unless (defined($default)) {
88 die("Need a default answer for non-interactive runs.");
94 print "$text\n" if $text;
106 print "Skipping.\n" if ($interactive);
112 =head2 sort_chromosomes
114 Arg[1] : (optional) Hashref $chr_hashref - Hashref with chr_name as keys
115 Example : my $chr = { '6-COX
' => 1, '1
' => 1, 'X
' => 1 };
116 my @sorted = $support->sort_chromosomes($chr);
117 Description : Sorts chromosomes in an intuitive way (numerically, then
118 alphabetically). If no chromosome hashref is passed, it's
119 retrieve by calling $self->get_chrlength()
120 Return type : List - sorted chromosome names
121 Exceptions : thrown
if no hashref is provided
126 sub sort_chromosomes {
127 my @chromosomes = @_;
129 return (sort _by_chr_num @chromosomes);
135 Example : my @sorted = sort _by_chr_num qw(X, 6-COX, 14, 7);
136 Description : Subroutine to use in sort
for sorting chromosomes. Sorts
137 numerically, then alphabetically
138 Return type : values to be used by sort
140 Caller :
internal ($self->sort_chromosomes)
145 my @awords = split /-/, $a;
146 my @bwords = split /-/, $b;
148 my $anum = $awords[0];
149 my $bnum = $bwords[0];
151 if ($anum !~ /^[0-9]*$/) {
152 if ($bnum !~ /^[0-9]*$/) {
153 return $anum cmp $bnum;
158 if ($bnum !~ /^[0-9]*$/) {
162 if ($anum <=> $bnum) {
163 return $anum <=> $bnum;
167 } elsif ($#bwords == 0) {
170 return $awords[1] cmp $bwords[1];
178 Arg[1] : Int $num - a number to commify
179 Example : print
"An easy to read number: ".$self->commify(100000000);
180 # will print 100,000,000
181 Description : put commas into a number to make it easier to read
182 Return type : a
string representing the commified number
192 $num = reverse($num);
193 $num =~ s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g;
195 return scalar reverse $num;
202 my @suffixes = qw(bytes kb Mb Gb Tb);
204 my $length = length($bytes);
205 my $order = int(($length-1)/3);
207 my $parsed = sprintf(
'%.1f', $bytes/10**(3*$order));
209 return "$parsed ".$suffixes[$order];
214 my $filename = shift;
216 my (@md5) = md5_hex($filename) =~ /\G(..)/g;
217 return join(
'/', @md5[0..2]);
225 # default to current directory
226 $path1 =
'.' unless (defined($path1));
228 my $return_path =
"$path1/$path2";
230 unless (-d $return_path) {
231 system(
"mkdir -p $return_path") == 0 or
232 die(
"Unable to create directory $return_path: $!\n");
241 Arg [1] : String $classname - The name of the
class to require/import
242 Example : $self->inject('Bio::EnsEMBL::DBSQL::DBAdaptor');
243 Description: Requires and imports the methods for the classname provided,
244 checks the symbol table so that it doesnot re-require modules
245 that have already been required.
246 Returntype : true on success
247 Exceptions : Warns to standard error if module fails to compile
253 my $classname = shift;
254 my ($parent_namespace, $module) = $classname =~/^(.*::)(.*)$/ ?
255 ($1,$2) : (
'::', $classname);
256 no strict
'refs'; ## no critic
258 # return if module has already been imported
259 return 1
if $parent_namespace->{$module.
'::'};
261 eval
"require $classname"; ## no critic
262 die(
"Failed to require $classname: $@")
if ($@);
264 $classname->import();