EnsEMBL/Utils/ScriptUtils.pm untested

filter: Statementstmt 0.0% 0 / 80 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 2 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 137 CC 17 SCAR 32.6
CC
17
Cov
0%
_by_chr_num
56.0
user_proceed
42.0
inject
20.0
CRAP 26.0
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::ScriptUtils;
34  
35 =head1 SYNOPSIS
36  
37 =head1 DESCRIPTION
38  
39 =head1 METHODS
40  
41 =cut
42  
43 package Bio::EnsEMBL::Utils::ScriptUtils;
44 $Bio::EnsEMBL::Utils::ScriptUtils::VERSION = '114.0.0_50'; # TRIAL
45 $Bio::EnsEMBL::Utils::ScriptUtils::VERSION = '114.0.050';
46 use strict;
47 use warnings;
48 no warnings 'uninitialized';
49  
50 use Exporter;
51 our @ISA = qw(Exporter);
52  
53 our @EXPORT_OK = qw(
54 user_proceed
55 commify
56 sort_chromosomes
57 parse_bytes
58 directory_hash
59 path_append
60 dynamic_use
61 inject
62 );
63  
64  
65 =head2 user_proceed
66  
67 Arg[1] : (optional) String $text - notification text to present to user
68 Example : # run a code snipped conditionally
69 if ($support->user_proceed("Run the next code snipped?")) {
70 # run some code
71 }
72  
73 # exit if requested by user
74 exit unless ($support->user_proceed("Want to continue?"));
75 Description : If running interactively, the user is asked if he wants to
76 perform a script action. If he doesn't, this section is skipped
77 and the script proceeds with the code. When running
78 non-interactively, the section is run by default.
79 Return type : TRUE to proceed, FALSE to skip.
80 Exceptions : none
81 Caller : general
82  
83 =cut
84  
85 sub user_proceed {
86 my ($text, $interactive, $default) = @_;
87  
88 unless (defined($default)) {
89 die("Need a default answer for non-interactive runs.");
90 }
91  
92 my $input;
93  
94 if ($interactive) {
95 print "$text\n" if $text;
96 print "[y/N] ";
97
98 $input = lc(<>);
99 chomp $input;
100 } else {
101 $input = $default;
102 }
103
104 if ($input eq 'y') {
105 return(1);
106 } else {
107 print "Skipping.\n" if ($interactive);
108 return(0);
109 }
110 }
111  
112  
113 =head2 sort_chromosomes
114  
115 Arg[1] : (optional) Hashref $chr_hashref - Hashref with chr_name as keys
116 Example : my $chr = { '6-COX' => 1, '1' => 1, 'X' => 1 };
117 my @sorted = $support->sort_chromosomes($chr);
118 Description : Sorts chromosomes in an intuitive way (numerically, then
119 alphabetically). If no chromosome hashref is passed, it's
120 retrieve by calling $self->get_chrlength()
121 Return type : List - sorted chromosome names
122 Exceptions : thrown if no hashref is provided
123 Caller : general
124  
125 =cut
126  
127 sub sort_chromosomes {
128 my @chromosomes = @_;
129
130 return (sort _by_chr_num @chromosomes);
131 }
132  
133  
134 =head2 _by_chr_num
135  
136 Example : my @sorted = sort _by_chr_num qw(X, 6-COX, 14, 7);
137 Description : Subroutine to use in sort for sorting chromosomes. Sorts
138 numerically, then alphabetically
139 Return type : values to be used by sort
140 Exceptions : none
141 Caller : internal ($self->sort_chromosomes)
142  
143 =cut
144  
145 sub _by_chr_num {
146 my @awords = split /-/, $a;
147 my @bwords = split /-/, $b;
148  
149 my $anum = $awords[0];
150 my $bnum = $bwords[0];
151  
152 if ($anum !~ /^[0-9]*$/) {
153 if ($bnum !~ /^[0-9]*$/) {
154 return $anum cmp $bnum;
155 } else {
156 return 1;
157 }
158 }
159 if ($bnum !~ /^[0-9]*$/) {
160 return -1;
161 }
162  
163 if ($anum <=> $bnum) {
164 return $anum <=> $bnum;
165 } else {
166 if ($#awords == 0) {
167 return -1;
168 } elsif ($#bwords == 0) {
169 return 1;
170 } else {
171 return $awords[1] cmp $bwords[1];
172 }
173 }
174 }
175  
176  
177 =head2 commify
178  
179 Arg[1] : Int $num - a number to commify
180 Example : print "An easy to read number: ".$self->commify(100000000);
181 # will print 100,000,000
182 Description : put commas into a number to make it easier to read
183 Return type : a string representing the commified number
184 Exceptions : none
185 Caller : general
186 Status : stable
187  
188 =cut
189  
190 sub commify {
191 my $num = shift;
192  
193 $num = reverse($num);
194 $num =~ s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g;
195  
196 return scalar reverse $num;
197 }
198  
199  
200 sub parse_bytes {
201 my $bytes = shift;
202  
203 my @suffixes = qw(bytes kb Mb Gb Tb);
204  
205 my $length = length($bytes);
206 my $order = int(($length-1)/3);
207  
208 my $parsed = sprintf('%.1f', $bytes/10**(3*$order));
209  
210 return "$parsed ".$suffixes[$order];
211 }
212  
213  
214 sub directory_hash {
215 my $filename = shift;
216  
217 my (@md5) = md5_hex($filename) =~ /\G(..)/g;
218 return join('/', @md5[0..2]);
219 }
220  
221  
222 sub path_append {
223 my $path1 = shift;
224 my $path2 = shift;
225  
226 # default to current directory
227 $path1 = '.' unless (defined($path1));
228  
229 my $return_path = "$path1/$path2";
230  
231 unless (-d $return_path) {
232 system("mkdir -p $return_path") == 0 or
233 die("Unable to create directory $return_path: $!\n");
234 }
235
236 return $return_path;
237 }
238  
239  
240 =head2 inject
241  
242 Arg [1] : String $classname - The name of the class to require/import
243 Example : $self->inject('Bio::EnsEMBL::DBSQL::DBAdaptor');
244 Description: Requires and imports the methods for the classname provided,
245 checks the symbol table so that it doesnot re-require modules
246 that have already been required.
247 Returntype : true on success
248 Exceptions : Warns to standard error if module fails to compile
249 Caller : internal
250  
251 =cut
252  
253 sub inject {
254 my $classname = shift;
255 my ($parent_namespace, $module) = $classname =~/^(.*::)(.*)$/ ?
256 ($1,$2) : ('::', $classname);
257 no strict 'refs'; ## no critic
258  
259 # return if module has already been imported
260 return 1 if $parent_namespace->{$module.'::'};
261
262 eval "require $classname"; ## no critic
263 die("Failed to require $classname: $@") if ($@);
264  
265 $classname->import();
266
267 return 1;
268 }
269  
270  
271 sub dynamic_use {
272 return inject(@_);
273 }
274  
275 1;
276