lib/Bio/ToolBox/utility.pm

filter: Statementstmt 76.2% 125 / 164 Branchbran 65.7% 46 / 70 Conditioncond 20.0% 4 / 20 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 90.0% 9 / 10 Podpod 100% 5 / 5 totaltotal 67.5% 189 / 280 CC 50 SCAR 37.6
CC
50
Cov
69%
ask_user_for_index
132.0
sane_chromo_sort
38.6
format_with_commas
8.0
CRAP 43.0
« lib/Bio/ToolBox/db_helper/useq.pm ↑ Summary lib/Bio/ToolBox/Parser/ucsc.pm »
1 package Bio::ToolBox::utility;
2  
3 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use IO::Prompt::Tiny qw(prompt);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 require Exporter;
9  
10 6 our $VERSION = '2.04';
11  
12 ### Variables
13 # Export
14 6 our @ISA = qw(Exporter);
15  
16 6 our @EXPORT_OK = qw(
17 parse_list
18 format_with_commas
19 ask_user_for_index
20 simplify_dataset_name
21 sane_chromo_sort
22 );
23  
24 6 my $DATA_COLNAMES = q();
25 6 my $DATA_FILENAME = q();
26  
27 ### Parse string into list
28 2 ▶sub parse_list {
Subroutine parse_list: called CC=6 SCAR=18.2
Pod documented
29  
30 # this subroutine will parse a string into an array
31 # it is designed for a string of numbers delimited by commas
32 # a range of numbers may be specified using a dash
33 # hence 1,2,5-7 would become an array of 1,2,5,6,7
34  
35 2 my $string = shift;
36 2 ▶ return unless defined $string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string 0 2 2
37 2 ▶ if ( $string =~ /[^\d,\-\s\&]/x ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /[^\d,\-\s\&]/x) 0 2 2
38 0 carp 'ERROR: the string contains characters that cannot be parsed';
39 0 return;
40 }
41 2 $string =~ s/\s+//g;
42 2 my @list;
43 2 foreach ( split /,/, $string ) {
44  
45 # check for a range
46 5 ▶ if (/\-/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\-/) { } 2 3 5
47 2 my ( $start, $stop ) = split /\-/;
48  
49 # add each item in the range to the list
50 2 ▶ for ( my $i = $start; $i <= $stop; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $stop) 13 2 15
51 13 push @list, $i;
52 }
53 2 next;
54 }
55 else {
56 # either an ordinary number or an "&"ed list of numbers
57 3 push @list, $_;
58 }
59 }
60 2 return @list;
61 }
62  
63 ### Format a number into readable comma-delimited by thousands number
64 3 ▶sub format_with_commas {
Subroutine format_with_commas: called CC=8 SCAR=20.8
Pod documented
65  
66 # for formatting a number with commas
67 3 my $number = shift;
68  
69 # check number
70 3 my ( $integers, $decimals, $sign );
71 3 ▶ if ( $number =~ m/^ (\-)? (\d+) \. (\d+) $/x ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($number =~ /^ (\-)? (\d+) \. (\d+) $/x) { } 2 1 3
elsif ($number =~ /^ (\-)? (\d+) $/x) { } 1 0 1
72 2 $sign = $1;
73 2 $integers = $2;
74 2 $decimals = $3;
75 }
76 elsif ( $number =~ m/^ (\-)? (\d+) $/x ) {
77 1 $sign = $1;
78 1 $integers = $2;
79 }
80 else {
81 0 carp 'ERROR: the string contains characters that cannot be parsed';
82 0 return $number;
83 }
84  
85 # format
86 3 my @digits = split //, $integers;
87 3 my @formatted;
88 3 ▶ while (@digits) {
Branch: 1 branch
BranchTrueFalseTotal
if (@digits) 10 3 13
89 10 ▶ if ( @digits > 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@digits > 3) { } 7 3 10
90 7 unshift @formatted, pop @digits;
91 7 unshift @formatted, pop @digits;
92 7 unshift @formatted, pop @digits;
93 7 unshift @formatted, ',';
94 }
95 else {
96 3 ▶ while (@digits) {
Branch: 1 branch
BranchTrueFalseTotal
if (@digits) 4 3 7
97 4 unshift @formatted, pop @digits;
98 }
99 }
100 }
101  
102 # finish
103 3 ▶ if ($sign) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sign) 1 2 3
104 1 unshift @formatted, $sign;
105 }
106 3 ▶ if ( defined $decimals ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $decimals) 2 1 3
107 2 push @formatted, q(.), $decimals;
108 }
109 3 return join( q(), @formatted );
110 }
111  
112 0 ▶sub ask_user_for_index {
Subroutine ask_user_for_index: not called CC=11 SCAR=48.8
Pod documented
113 0 my $Data = shift;
114 0 ▶ my $line = shift || ' Enter the desired column index ';
Condition0%0/2
shift() || ' Enter the desired column index '
l!l
00
MC/DC0%0/1
shift() || ' Enter the desired column index '
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
115 0 ▶ unless ( ref($Data) =~ /Bio::ToolBox::Data/x ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref($Data) =~ /Bio::ToolBox::Data/x) 0 0 0
116 0 carp 'ERROR: Must pass a Bio::ToolBox::Data object!';
117 0 return;
118 }
119  
120 # print column header names only if we have not done so before
121 0 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless ($Data->filename eq $DATA_FILENAME and join(";", $Data->list_columns) eq $DATA_COLNAMES) 0 0 0
Condition0%0/3
$Data->filename eq $DATA_FILENAME and join(";", $Data->list_columns) eq $DATA_COLNAMES
!ll&&!rl&&r
000
MC/DC0%0/2
$Data->filename eq $DATA_FILENAME and join(";", $Data->list_columns) eq $DATA_COLNAMES
$Data->filename eq $DATA_FILENAME join(";", $Data->list_columns) eq $DATA_COLNAMES
Truth table0%0/3
A and B
A= $Data->filename eq $DATA_FILENAMEB= join(";", $Data->list_columns) eq $DATA_COLNAMES
ABresult
0X0
100
111
122 # we use filename and column number as indicators
123 $Data->filename eq $DATA_FILENAME
124 and join( ';', $Data->list_columns ) eq $DATA_COLNAMES
125 )
126 {
127 0 print " These are the columns in the file\n";
128 0 my $i = 1;
129 0 foreach ( $Data->list_columns ) {
130 0 print " $i\t$_\n";
131 0 $i++;
132 }
133  
134 # remember for next time
135 0 $DATA_FILENAME = $Data->filename;
136 0 $DATA_COLNAMES = join( ';', $Data->list_columns );
137 }
138  
139 # get response
140 0 my $response = prompt($line);
141 0 my @indices = parse_list($response);
142  
143 # verify
144 0 my @good;
145 0 my $n = $Data->number_columns;
146 0 foreach my $i (@indices) {
147 0 ▶ if ( $i =~ /^\d+$/ and $i > 0 and $i <= $n ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i =~ /^\d+$/ and $i > 0 and $i <= $n) { } 0 0 0
Condition0%0/6
$i =~ /^\d+$/ and $i > 0 and $i <= $n
!ll&&!rl&&r
000
$i =~ /^\d+$/ and $i > 0
!ll&&!rl&&r
000
MC/DC0%0/3
$i =~ /^\d+$/ and $i > 0 and $i <= $n
$i =~ /^\d+$/ $i > 0 $i <= $n
Truth table0%0/4
A and B and C
A= $i =~ /^\d+$/B= $i > 0C= $i <= $n
ABCresult
0XX0
10X0
1100
1111
148 0 push @good, $i;
149 }
150 else {
151 0 print " $i is not a valid index!\n";
152 }
153 }
154 0 ▶ return wantarray ? @good : $good[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
155 }
156  
157 14 ▶sub simplify_dataset_name {
Subroutine simplify_dataset_name: called CC=4 SCAR=13.9
Pod documented
158 14 my $dataset = shift;
159 14 my $new_name;
160  
161 # strip any file prefix
162 14 $dataset =~ s/^(?: file | http | ftp ):\/*//x;
163  
164 14 ▶ if ( $dataset =~ /&/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dataset =~ /&/) { } 2 12 14
165  
166 # a combination dataset
167 2 foreach ( split /&/, $dataset ) {
168 4 my $n = simplify_dataset_name($_);
169 4 ▶ if ($new_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_name) { } 2 2 4
170 2 $new_name .= '&' . $n;
171 }
172 else {
173 2 $new_name = $n;
174 }
175 }
176 }
177 else {
178 # a single dataset
179 # this could be either a file name or an entry in a BioPerl or BigWigSet database
180 # remove any possible paths
181 12 ( undef, undef, $new_name ) = File::Spec->splitpath($dataset);
182  
183 # remove any known file extensions
184 12 $new_name =~
185 s/\. (?: bw | bam | bb | useq | bigwig | bigbed | g[tf]f3? | cram | wig | bdg | bedgraph ) (?:\.gz)? $//xi;
186  
187 # remove common non-useful stuff
188 # trying to imagine all sorts of possible things
189 12 $new_name =~
190 s/[_\.\-] (?: sort | sorted | dedup | dedupe | deduplicated | rmdup | mkdup | markdup | dup | unique | filt | filtered ) \b //xgi;
191 12 $new_name =~
192 s/[_\.\-] (?: coverage | rpm | ext\d* | extend\d* | log2fe | log\d+ | qvalue | fragment | count | lambda_control | fe | fold.?enrichment | ratio | log\d*ratio ) \b //xgi;
193 }
194 14 return $new_name;
195 }
196  
197 5 ▶sub sane_chromo_sort {
Subroutine sane_chromo_sort: called CC=25 SCAR=36.5
Pod documented
198 5 my @chroms = @_;
199 5 ▶ return unless scalar @chroms;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @chroms 0 5 5
200  
201 # let's try and sort in some kind of rational order
202 5 my @numeric;
203 my @romanic;
204 5 my @mixed;
205 5 my @alphic;
206 5 my @sex;
207 5 my @mito;
208 5 foreach my $c (@chroms) {
209  
210 43 my $name;
211 43 ▶ if ( ref($c) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $c eq 'ARRAY') { } 0 43 43
212 0 $name = $c->[0];
213 }
214 else {
215 43 $name = $c;
216 }
217  
218 # identify the type of chromosome name to sort
219 43 ▶ if ( $name =~ m/^ (?:chr)? ( [wxyz] ) $/xi ) {
Branch: 7 branches
BranchTrueFalseTotal
if ($name =~ /^ (?:chr)? ( [wxyz] ) $/xi) { } 4 39 43
elsif ($name =~ /^ (?:chr)? (?: m | mt | mito ) (?:dna)? $/xi) { } 3 36 39
elsif ($name =~ /^ \d+ $/x and length $name > 2) { } 0 36 36
elsif ($name =~ /^ (?:chr)? (\d+) $/xi) { } 22 14 36
elsif ($name =~ /^ (?:chr)? ( [IVX]+ ) $/x) { } 8 6 14
elsif ($name =~ /^ (?:chr)? ( \d+ ) ( [lrpq] ) $/xi) { } 0 6 6
elsif ($name =~ /^ ( [a-zA-Z_\-\.]+ ) (\d+)/x) { } 5 1 6
220  
221 # sex chromosomes
222 4 push @sex, [ $1, $c ];
223 }
224 elsif ( $name =~ m/^ (?:chr)? (?: m | mt | mito ) (?:dna)? $/xi ) {
225  
226 # mitochondrial
227 3 push @mito, [ $name, $c ];
228 }
229 ▶ elsif ( $name =~ m/^ \d+ $/x and length($name) > 2 ) {
Condition66%2/3
$name =~ /^ \d+ $/x and length $name > 2
!ll&&!rl&&r
3330
MC/DC0%0/2
$name =~ /^ \d+ $/x and length $name > 2
$name =~ /^ \d+ $/x length $name > 2
Truth table66%2/3
A and B
A= $name =~ /^ \d+ $/xB= length $name > 2
ABresult
0X0
100
111
230  
231 # I'm going to go out on a limb here and presume this is some weird scaffold
232 # since it's unlikely or at least rare for organisms to have over 100
233 # numbered chromosomes. For example, Drosophila BDGP6 has a whole bunch of
234 # scaffolds with just numbers.
235 # Push these to mixed and hope for the best
236 0 push @mixed, [ q( ), $name, $name, $c ];
237 }
238 elsif ( $name =~ m/^ (?:chr)? (\d+) $/xi ) {
239  
240 # standard numeric chromosome
241 22 push @numeric, [ $1, $c ];
242 }
243 elsif ( $name =~ m/^ (?:chr)? ( [IVX]+ ) $/x ) {
244  
245 # Roman numerals - silly Saccharomyces cerevisiae
246 8 push @romanic, [ $1, $c ];
247 }
248 elsif ( $name =~ m/^ (?:chr)? ( \d+ ) ( [lrpq] ) $/xi ) {
249  
250 # chromosome arms
251 0 my $i = $1;
252 0 my $a = lc $2;
253 0 ▶ if ( $a eq 'l' ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($a eq 'l') { } 0 0 0
elsif ($a eq 'r') { } 0 0 0
elsif ($a eq 'p') { } 0 0 0
elsif ($a eq 'q') { } 0 0 0
254 0 $i += 0.1;
255 }
256 elsif ( $a eq 'r' ) {
257 0 $i += 0.2;
258 }
259 elsif ( $a eq 'p' ) {
260 0 $i += 0.3;
261 }
262 elsif ( $a eq 'q' ) {
263 0 $i += 0.4;
264 }
265 0 push @numeric, [ $i, $c ];
266 }
267 elsif ( $name =~ m/^ ( [a-zA-Z_\-\.]+ ) (\d+)/x ) {
268  
269 # presumed contigs and such?
270 5 push @mixed, [ $1, $2, $name, $c ];
271 }
272 else {
273 # everything else
274 1 push @alphic, [ $name, $c ];
275 }
276 }
277  
278 # check romanic
279 5 ▶ if ( scalar @romanic ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @romanic) 3 2 5
280  
281 # looks like we have romanic chromosomes
282 3 ▶ if ( scalar @sex ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @sex) 1 2 3
283  
284 # probably caught up chrX, unlikely WYZ
285 1 my @x = grep { $sex[$_]->[0] =~ m/^X$/ } ( 0 .. $#sex );
286 1 foreach ( reverse @x ) {
287  
288 # I'm assuming and hoping there's only one chrX found
289 # but reverse the list, just in case - assuming grep returns in order
290 1 push @romanic, ( splice( @sex, $_, 1 ) );
291 }
292 }
293 3 ▶ if ( scalar @numeric ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @numeric) { } 0 3 3
294  
295 # well, shoot, this is weird, mix of both numeric and romanic chromosomes?
296 # just merge romanic with alphic and hope for the best
297 0 push @alphic, @romanic;
298 }
299 else {
300 # convert the romanic to numeric
301 3 ▶ while (@romanic) {
Branch: 1 branch
BranchTrueFalseTotal
if (@romanic) 9 3 12
302 9 my $r = shift @romanic;
303 9 my $c = $r->[0];
304 9 $c =~ s/IV/4/;
305 9 $c =~ s/IX/9/;
306 9 $c =~ s/V/5/;
307 9 $c =~ s/I/1/g;
308 9 my $n = 0;
309 9 foreach ( split m//, $c ) {
310 16 ▶ if ( $_ eq 'X' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq 'X') { } 2 14 16
311 2 $n += 10;
312 }
313 else {
314 14 $n += $_;
315 }
316 }
317 9 push @numeric, [ $n, $r->[1] ];
318 }
319 }
320 }
321  
322 # sort
323 5 my @sorted;
324 5 push @sorted, map { $_->[1] } sort { $a->[0] <=> $b->[0] } @numeric;
325 5 push @sorted, map { $_->[1] } sort { $a->[0] cmp $b->[0] } @sex;
326 0 push @sorted, map { $_->[1] } sort { $a->[0] cmp $b->[0] } @mito;
327 5 push @sorted, map { $_->[3] }
328 5 ▶ sort { $a->[0] cmp $b->[0] or $a->[1] <=> $b->[1] or $a->[2] cmp $b->[2] } @mixed;
Condition33%2/6
$Bio::ToolBox::utility::a->[0] cmp $Bio::ToolBox::utility::b->[0] || $Bio::ToolBox::utility::a->[1] <=> $Bio::ToolBox::utility::b->[1] || $Bio::ToolBox::utility::a->[2] cmp $Bio::ToolBox::utility::b->[2]
l!l&&r!l&&!r
400
$Bio::ToolBox::utility::a->[0] cmp $Bio::ToolBox::utility::b->[0] or $Bio::ToolBox::utility::a->[1] <=> $Bio::ToolBox::utility::b->[1]
l!l&&r!l&&!r
040
MC/DC0%0/3
$Bio::ToolBox::utility::a->[0] cmp $Bio::ToolBox::utility::b->[0] || $Bio::ToolBox::utility::a->[1] <=> $Bio::ToolBox::utility::b->[1] || $Bio::ToolBox::utility::a->[2] cmp $Bio::ToolBox::utility::b->[2]
$Bio::ToolBox::utility::a->[0] cmp $Bio::ToolBox::utility::b->[0] $Bio::ToolBox::utility::a->[1] <=> $Bio::ToolBox::utility::b->[1] $Bio::ToolBox::utility::a->[2] cmp $Bio::ToolBox::utility::b->[2]
Truth table25%1/4
A or B || C
A= $Bio::ToolBox::utility::a->[0] cmp $Bio::ToolBox::utility::b->[0]B= $Bio::ToolBox::utility::a->[1] <=> $Bio::ToolBox::utility::b->[1]C= $Bio::ToolBox::utility::a->[2] cmp $Bio::ToolBox::utility::b->[2]
ABCresult
1XX1
01X1
0011
0000
329 0 push @sorted, map { $_->[1] } sort { $a->[0] cmp $b->[0] } @alphic;
330  
331 5 return @sorted;
332 }
333  
334 6 1;
335  
336 __END__
« lib/Bio/ToolBox/db_helper/useq.pm ↑ Summary lib/Bio/ToolBox/Parser/ucsc.pm »