lib/Bio/ToolBox/db_helper/useq.pm

filter: Statementstmt 84.6% 110 / 130 Branchbran 70.5% 55 / 78 Conditioncond 70.0% 14 / 20 MC/DCmcdc 53.8% 7 / 13 Subroutinesub 100% 12 / 12 Podpod 100% 3 / 3 totaltotal 78.5% 201 / 256 CC 56 SCAR 38.2
CC
56
Cov
79%
collect_useq_position_scores
79.2
collect_useq_scores
18.7
_get_useq
5.1
CRAP 45.8
« script/get_intersecting_features.pl ↑ Summary lib/Bio/ToolBox/utility.pm »
1 package Bio::ToolBox::db_helper::useq;
2  
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use List::Util qw(min max sum);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Statistics::Lite qw(median);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Bio::ToolBox::db_helper::constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Bio::DB::USeq;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 require Exporter;
12  
13 1 our $VERSION = '2.00';
14  
15 # Exported names
16 1 our @ISA = qw(Exporter);
17  
18 ## no critic
19 ## this is never intended to be used directly by end users
20 ## and exporting everything is required
21 1 our @EXPORT = qw(
22 collect_useq_scores
23 collect_useq_position_scores
24 open_useq_db
25 );
26 ## use critic
27  
28 # Hash of USeq chromosomes
29 1 my %USEQ_CHROMOS;
30  
31 # sometimes user may request a chromosome that's not in the useq file
32 # that could lead to an exception
33 # we will record the chromosomes list in this hash
34 # $USEQ_CHROMOS{useqfile}{chromos}
35 # we also record the chromosome name variant with or without chr prefix
36 # to accommodate different naming conventions
37  
38 # Opened USeq db objects
39 my %OPENED_USEQ;
40  
41 # a cache for opened USeq databases, primarily for collecting scores
42 # caching here is only for local purposes of collecting scores
43 # db_helper also provides caching of db objects but with option to force open in
44 # the case of forking processes - we don't have that here
45  
46 7 ▶sub collect_useq_scores {
Subroutine collect_useq_scores: called CC=18 SCAR=29.3
Pod documented
47  
48 # passed parameters as array ref
49 # chromosome, start, stop, strand, strandedness, method, db, dataset
50 7 my $param = shift;
51  
52 # adjust strand method
53 7 my $strand;
54 7 ▶ if ( $param->[STND] eq 'antisense' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($param->[4] eq 'antisense') { } 1 6 7
elsif ($param->[4] eq 'all') { } 5 1 6
55 1 $strand = $param->[STR] * -1;
56 }
57 elsif ( $param->[STND] eq 'all' ) {
58  
59 # Bio::DB::USeq will translate this properly, and collect from
60 # both strands as necessary
61 5 $strand = 0;
62 }
63 else {
64 # default
65 1 $strand = $param->[STR];
66 }
67  
68 # unlikely there are more than one useq file, but just in case
69 7 my @scores;
70 7 ▶ for ( my $d = DATA; $d < scalar @{$param}; $d++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d < scalar @{$param;}) 7 7 14
71  
72 # open a new db object
73 7 my $useq = _get_useq( $param->[$d] );
74  
75 # check chromosome first
76 7 ▶ my $chromo = $USEQ_CHROMOS{ $param->[$d] }{ $param->[CHR] } or next;
Branch: 1 branch
BranchTrueFalseTotal
my $chromo = $USEQ_CHROMOS{$param->[$d]}{$param->[0]} or next 7 0 7
77  
78 # need to collect the scores based on the type of score requested
79 7 ▶ if ( $param->[METH] eq 'count' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($param->[5] eq 'count') { } 1 6 7
elsif ($param->[5] eq 'ncount') { } 1 5 6
elsif ($param->[5] eq 'pcount') { } 1 4 5
80  
81 # need to collect features across the region
82 1 my $iterator = $useq->get_seq_stream(
83 -seq_id => $chromo,
84 -start => $param->[STRT],
85 -end => $param->[STOP],
86 -strand => $strand,
87 );
88 1 ▶ return unless $iterator;
Branch: 1 branch
BranchTrueFalseTotal
unless $iterator 0 1 1
89  
90 # count each feature
91 1 ▶ while ( my $f = $iterator->next_seq ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $f = $iterator->next_seq) 437 1 438
92 437 push @scores, 1;
93 }
94 }
95 elsif ( $param->[METH] eq 'ncount' ) {
96  
97 # need to collect features across the region
98 1 my $iterator = $useq->get_seq_stream(
99 -seq_id => $chromo,
100 -start => $param->[STRT],
101 -end => $param->[STOP],
102 -strand => $strand,
103 );
104 1 ▶ return unless $iterator;
Branch: 1 branch
BranchTrueFalseTotal
unless $iterator 0 1 1
105  
106 # store the names
107 1 ▶ while ( my $f = $iterator->next_seq ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $f = $iterator->next_seq) 437 1 438
108 437 ▶ push @scores, $f->display_name || $f->primary_id;
Condition33%1/3
$f->display_name || $f->primary_id
l!l&&r!l&&!r
43700
MC/DC0%0/2
$f->display_name || $f->primary_id
$f->display_name $f->primary_id
Truth table33%1/3
A || B
A= $f->display_nameB= $f->primary_id
ABresult
1X1
011
000
109  
110 # if no display name, a primary_id should automatically be generated
111 }
112 }
113 elsif ( $param->[METH] eq 'pcount' ) {
114  
115 # need to collect features across the region
116 1 my $iterator = $useq->get_seq_stream(
117 -seq_id => $chromo,
118 -start => $param->[STRT],
119 -end => $param->[STOP],
120 -strand => $strand,
121 );
122 1 ▶ return unless $iterator;
Branch: 1 branch
BranchTrueFalseTotal
unless $iterator 0 1 1
123  
124 # precisely count each feature
125 1 ▶ while ( my $f = $iterator->next_seq ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $f = $iterator->next_seq) 437 1 438
126 437 ▶ push @scores, 1
Branch: 1 branch
BranchTrueFalseTotal
if $f->start >= $param->[1] and $f->end <= $param->[2] 431 6 437
Condition66%2/3
$f->start >= $param->[1] and $f->end <= $param->[2]
!ll&&!rl&&r
06431
MC/DC50%1/2
$f->start >= $param->[1] and $f->end <= $param->[2]
$f->start >= $param->[1] $f->end <= $param->[2]
Truth table66%2/3
A and B
A= $f->start >= $param->[1]B= $f->end <= $param->[2]
ABresult
0X0
100
111
127 if ( $f->start >= $param->[STRT] and $f->end <= $param->[STOP] );
128 }
129 }
130 else {
131 # everything else is just scores
132 4 push @scores,
133 $useq->scores(
134 -seq_id => $chromo,
135 -start => $param->[STRT],
136 -end => $param->[STOP],
137 -strand => $strand,
138 );
139 }
140 }
141  
142 7 ▶ return wantarray ? @scores : \@scores;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 7 7
143 }
144  
145 5 ▶sub collect_useq_position_scores {
Subroutine collect_useq_position_scores: called CC=34 SCAR=43.7
Pod documented
146  
147 # passed parameters as array ref
148 # chromosome, start, stop, strand, strandedness, method, db, dataset
149 5 my $param = shift;
150  
151 # adjust strand method
152 5 my $strand;
153 5 ▶ if ( $param->[STND] eq 'antisense' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($param->[4] eq 'antisense') { } 0 5 5
elsif ($param->[4] eq 'all') { } 0 5 5
154 0 $strand = $param->[STR] * -1;
155 }
156 elsif ( $param->[STND] eq 'all' ) {
157  
158 # Bio::DB::USeq will translate this properly, and collect from
159 # both strands as necessary
160 0 $strand = 0;
161 }
162 else {
163 # default
164 5 $strand = $param->[STR];
165 }
166  
167 # unlikely there are more than one useq file, but just in case
168 5 my %pos2score;
169 5 ▶ for ( my $d = DATA; $d < scalar @{$param}; $d++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d < scalar @{$param;}) 5 5 10
170  
171 # open a new db object
172 5 my $useq = _get_useq( $param->[$d] );
173  
174 # check chromosome first
175 5 ▶ my $chromo = $USEQ_CHROMOS{ $param->[$d] }{ $param->[CHR] } or next;
Branch: 1 branch
BranchTrueFalseTotal
my $chromo = $USEQ_CHROMOS{$param->[$d]}{$param->[0]} or next 5 0 5
176  
177 # collect the features overlapping the region
178 5 my $iterator = $useq->get_seq_stream(
179 -seq_id => $chromo,
180 -start => $param->[STRT],
181 -end => $param->[STOP],
182 -strand => $strand,
183 );
184 5 ▶ return unless $iterator;
Branch: 1 branch
BranchTrueFalseTotal
unless $iterator 0 5 5
185  
186 # collect each feature
187 5 ▶ while ( my $f = $iterator->next_seq ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $f = $iterator->next_seq) 244 5 249
188  
189 # determine position to record
190 244 my $position;
191 244 ▶ if ( $f->start == $f->end ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f->start == $f->end) { } 0 244 244
192  
193 # just one position recorded
194 0 $position = $f->start;
195 }
196 else {
197 # calculate the midpoint
198 244 $position = int( ( ( $f->start + $f->end ) / 2 ) + 0.5 );
199 }
200  
201 # check the position
202 next
203 unless (
204 # want to avoid those whose midpoint are not technically
205 # within the region of interest
206 244 ▶ $position >= $param->[STRT] and $position <= $param->[STOP]
Branch: 1 branch
BranchTrueFalseTotal
unless $position >= $param->[1] and $position <= $param->[2] 19 225 244
Condition100%3/3
$position >= $param->[1] and $position <= $param->[2]
!ll&&!rl&&r
910225
MC/DC100%2/2
$position >= $param->[1] and $position <= $param->[2]
$position >= $param->[1] $position <= $param->[2]
Truth table100%3/3
A and B
A= $position >= $param->[1]B= $position <= $param->[2]
ABresult
0X0
100
111
207 );
208  
209 # record the value
210 225 ▶ if ( $param->[METH] eq 'count' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($param->[5] eq 'count') { } 44 181 225
elsif ($param->[5] eq 'ncount') { } 44 137 181
elsif ($param->[5] eq 'pcount') { } 44 93 137
211 44 $pos2score{$position} += 1;
212 }
213 elsif ( $param->[METH] eq 'ncount' ) {
214 44 ▶ $pos2score{$position} ||= [];
Condition50%1/2
$pos2score{$position} ||= []
l!l
044
MC/DC0%0/1
$pos2score{$position} ||= []
$pos2score{$position}
Truth table50%1/2
A
A= $pos2score{$position}
Aresult
11
00
215 44 ▶ push @{ $pos2score{$position} }, $f->display_name
Condition33%1/3
$f->display_name || $f->primary_id
l!l&&r!l&&!r
4400
MC/DC0%0/2
$f->display_name || $f->primary_id
$f->display_name $f->primary_id
Truth table33%1/3
A || B
A= $f->display_nameB= $f->primary_id
ABresult
1X1
011
000
216 || $f->primary_id;
217 }
218 elsif ( $param->[METH] eq 'pcount' ) {
219 44 ▶ $pos2score{$position} += 1
Branch: 1 branch
BranchTrueFalseTotal
if $f->start >= $param->[1] and $f->end <= $param->[2] 42 2 44
Condition100%3/3
$f->start >= $param->[1] and $f->end <= $param->[2]
!ll&&!rl&&r
1142
MC/DC100%2/2
$f->start >= $param->[1] and $f->end <= $param->[2]
$f->start >= $param->[1] $f->end <= $param->[2]
Truth table100%3/3
A and B
A= $f->start >= $param->[1]B= $f->end <= $param->[2]
ABresult
0X0
100
111
220 if ( $f->start >= $param->[STRT] and $f->end <= $param->[STOP] );
221 }
222 else {
223 # everything else we take the score
224 93 push @{ $pos2score{$position} }, $f->score;
225 }
226 }
227 }
228  
229 # combine multiple datapoints at the same position
230 5 ▶ if ( $param->[METH] eq 'ncount' ) {
Branch: 7 branches
BranchTrueFalseTotal
if ($param->[5] eq 'ncount') { } 1 4 5
elsif ($param->[5] eq 'count' or $param->[5] eq 'pcount') { } 2 2 4
elsif ($param->[5] eq 'mean') { } 2 0 2
elsif ($param->[5] eq 'median') { } 0 0 0
elsif ($param->[5] eq 'min') { } 0 0 0
elsif ($param->[5] eq 'max') { } 0 0 0
elsif ($param->[5] eq 'sum') { } 0 0 0
231 1 foreach my $position ( keys %pos2score ) {
232 44 my %name2count;
233 44 foreach ( @{ $pos2score{$position} } ) { $name2count{$_} += 1 }
234 44 $pos2score{$position} = scalar( keys %name2count );
235 }
236 }
237 ▶ elsif ( $param->[METH] eq 'count' or $param->[METH] eq 'pcount' ) {
Condition100%3/3
$param->[5] eq "count" or $param->[5] eq "pcount"
l!l&&r!l&&!r
112
MC/DC100%2/2
$param->[5] eq "count" or $param->[5] eq "pcount"
$param->[5] eq "count" $param->[5] eq "pcount"
Truth table100%3/3
A or B
A= $param->[5] eq "count"B= $param->[5] eq "pcount"
ABresult
1X1
011
000
238  
239 # do nothing, these aren't arrays
240 }
241 elsif ( $param->[METH] eq 'mean' ) {
242 2 foreach my $position ( keys %pos2score ) {
243 $pos2score{$position} =
244 93 sum( @{ $pos2score{$position} } ) / scalar( @{ $pos2score{$position} } );
245 }
246 }
247 elsif ( $param->[METH] eq 'median' ) {
248 0 foreach my $position ( keys %pos2score ) {
249 0 $pos2score{$position} = median( @{ $pos2score{$position} } );
250 }
251 }
252 elsif ( $param->[METH] eq 'min' ) {
253 0 foreach my $position ( keys %pos2score ) {
254 0 $pos2score{$position} = min( @{ $pos2score{$position} } );
255 }
256 }
257 elsif ( $param->[METH] eq 'max' ) {
258 0 foreach my $position ( keys %pos2score ) {
259 0 $pos2score{$position} = max( @{ $pos2score{$position} } );
260 }
261 }
262 elsif ( $param->[METH] eq 'sum' ) {
263 0 foreach my $position ( keys %pos2score ) {
264 0 $pos2score{$position} = sum( @{ $pos2score{$position} } );
265 }
266 }
267 else {
268 # just take the mean for everything else
269 0 foreach my $position ( keys %pos2score ) {
270 $pos2score{$position} =
271 0 sum( @{ $pos2score{$position} } ) / scalar( @{ $pos2score{$position} } );
272 }
273 }
274  
275 # return collected data
276 5 ▶ return wantarray ? %pos2score : \%pos2score;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 5 5
277 }
278  
279 2 ▶sub open_useq_db {
Subroutine open_useq_db: called CC=2 SCAR=7.0
Pod documented
280  
281 # path
282 2 my $useqfile = shift;
283 2 my $path = $useqfile;
284 2 $path =~ s/^file://; # clean up file prefix if present
285  
286 # open
287 2 my $useq;
288 2 eval { $useq = Bio::DB::USeq->new($path); };
289 2 ▶ return unless $useq;
Branch: 1 branch
BranchTrueFalseTotal
unless $useq 0 2 2
290  
291 2 return $useq;
292 }
293  
294 ### Internal subroutine for getting the cached USeq object
295 12 ▶sub _get_useq {
Subroutine _get_useq: called CC=5 SCAR=16.4
296 12 my $useqfile = shift;
297  
298 12 ▶ return $OPENED_USEQ{$useqfile} if exists $OPENED_USEQ{$useqfile};
Branch: 1 branch
BranchTrueFalseTotal
if exists $OPENED_USEQ{$useqfile} 11 1 12
299  
300 # open and cache the USeq object
301 1 ▶ my $useq = open_useq_db($useqfile)
Branch: 1 branch
BranchTrueFalseTotal
my $useq = open_useq_db($useqfile) or croak("FATAL: Unable to open USeq file '${useqfile}'! $Bio::ToolBox::db_helper::useq::OS_ERROR") 1 0 1
302 or croak "FATAL: Unable to open USeq file '$useqfile'! $OS_ERROR";
303 1 $OPENED_USEQ{$useqfile} = $useq;
304  
305 # record the chromosomes and possible variants
306 1 $USEQ_CHROMOS{$useqfile} = {};
307 1 foreach my $s ( $useq->seq_ids ) {
308 1 $USEQ_CHROMOS{$useqfile}{$s} = $s;
309 1 ▶ if ( $s =~ /^chr(.+)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /^chr(.+)$/) { } 1 0 1
310 1 $USEQ_CHROMOS{$useqfile}{$1} = $s;
311 }
312 else {
313 0 $USEQ_CHROMOS{$useqfile}{"chr$s"} = $s;
314 }
315 }
316 1 return $useq;
317 }
318  
319 1 1;
320  
321 __END__
« script/get_intersecting_features.pl ↑ Summary lib/Bio/ToolBox/utility.pm »