lib/Bio/Graphics/Wiggle.pm

filter: Statementstmt 7.5% 32 / 425 Branchbran 0.0% 0 / 176 Conditioncond 0.0% 0 / 105 MC/DCmcdc 0.0% 0 / 64 Subroutinesub 15.3% 10 / 65 Podpod 45.0% 18 / 40 totaltotal 6.8% 60 / 875 CC 135 SCAR 47.9
CC
135
Cov
5%
new
462.0
value
342.0
_retrieve_values
306.0
CRAP 119.7
« blib/lib/Bio/Graphics/Glyph/trace.pm ↑ Summary blib/lib/Bio/Graphics/Glyph/hybrid_plot.pm »
1 package Bio::Graphics::Wiggle;
2  
3 =head1 NAME
4  
5 Bio::Graphics::Wiggle -- Binary storage for dense genomic features
6  
7 =head1 SYNOPSIS
8  
9 # all positions are 1-based
10  
11 my $wig = Bio::Graphics::Wiggle->new('./test.wig',
12 $writeable,
13 { seqid => $seqid,
14 start => $start,
15 step => $step,
16 min => $min,
17 max => $max });
18  
19 $wig->erase;
20  
21 my $seqid = $wig->seqid('new_id');
22 my $max = $wig->max($new_max);
23 my $min = $wig->min($new_min);
24 my $step = $wig->step($new_step); # data stored at modulus step == 0; all else is blank
25  
26 $wig->set_value($position => $value); # store $value at position
27 $wig->set_values($position => \@values); # store array of values at position
28 $wig->set_range($start=>$end,$value); # store the same $value from $start to $end
29  
30 my $value = $wig->value($position); # fetch value from position
31 my $values = $wig->values($start,$end); # fetch range of data from $start to $end
32  
33 $wig->window(100); # sample window size
34 $wig->smoothing('mean'); # when sampling, compute the mean value across sample window
35 my $values = $wig->values($start,$end,$samples); # fetch $samples data points from $start to $end
36  
37  
38 =head1 DESCRIPTION
39  
40 IMPORTANT NOTE: This implementation is still not right. See
41 http://genomewiki.ucsc.edu/index.php/Wiggle for a more space-efficient
42 implementation.
43  
44 This module stores "wiggle" style quantitative genome data for display
45 in a genome browser application. The data for each chromosome (or
46 contig, or other reference sequence) is stored in a single file in the
47 following format:
48  
49 256 byte header
50 50 bytes seqid, zero-terminated C string
51 4 byte long integer, value of "step" (explained later)
52 4 byte perl native float, the "min" value
53 4 byte perl native float, the "max" value
54 4 byte long integer, value of "span"
55 4 byte perl native float, the mean
56 4 byte perl native float, the standard deviation
57 2 byte unsigned short, the version number (currently version 0)
58 4 byte long integer, sequence start position (in 0-based coordinates)
59 null padding to 256 bytes for future use
60  
61 The remainder of the file consists of 8-bit unsigned scaled integer
62 values. This means that all quantitative data will be scaled to 8-bit
63 precision!
64  
65 For a convenient method of creating Wiggle files from UCSC-type WIG
66 input and creating GFF3 output, please see
67 L<Bio::Graphics::Wiggle::Loader>.
68  
69 =head1 METHODS
70  
71 =head2 Constructor and Accessors
72  
73 =over 4
74  
75 =item $wig = Bio::Graphics::Wiggle->new($filename,$writeable,{options})
76  
77 Open/create a wiggle-format data file:
78  
79 $filename -- path to the file to open/create
80 $writeable -- boolean value indicating whether file is
81 writeable. Missing files will only be created
82 if $writeable set to a true value. If path is
83 empty (undef or empty string) and writeable is true,
84 new() will create a temporary file that will be
85 deleted when the object goes out of scope.
86 {options} -- hash ref of the following named options, only valid
87 when creating a new wig file with $writeable true.
88  
89 option name description default
90 ----------- ----- -------
91 seqid name/id of sequence empty name
92 min minimum value of data points 0
93 max maximum value of data points 255
94 step interval between data points 1
95 span width of data points value of "step"
96  
97 The "step" can be used to create sparse files to save space. By
98 default, step is set to 1, in which case a data value will be stored
99 at each base of the sequence. By setting step to 10, then each value
100 is taken to correspond to 10 bp, and the file will be 10x smaller.
101 For example, consider this step 5 data set:
102  
103 1 2 3 4 5 6 7 8 9 10 11 12 13 14
104 20 . . . . 60 . . . . 80 . . .
105  
106 We have stored the values "20" "60" and "80" at positions 1, 6 and 11,
107 respectively. When retrieving this data, it will appear as if
108 positions 1 through 5 have a value of 20, positions 6-10 have a value
109 of 60, and positions 11-14 have a value of 80. In the data file, we
110 store, positions 1,6,and 11 in adjacent bytes.
111  
112 Note that no locking is performed by this module. If you wish to allow
113 multi-user write access to the databases files, you will need to
114 flock() the files yourself.
115  
116 =item $seqid = $wig->seqid(['new_id'])
117  
118 =item $max = $wig->max([$new_max])
119  
120 =item $min = $wig->min([$new_min])
121  
122 =item $step = $wig->step([$new_step])
123  
124 =item $span = $wig->span([$new_span])
125  
126 =item $mean = $wig->mean([$new_mean]);
127  
128 =item $stdev = $wig->stdev([$new_stdev]);
129  
130 These accessors get or set the corresponding values. Setting is only
131 allowed if the file was opened for writing. Note that changing the
132 min, max and step after writing data to the file under another
133 parameter set will produce unexpected (and invalid) results, as the
134 existing data is not automatically updated to be consistent.
135  
136 =item $trim = $wig->trim([$new_trim]);
137  
138 The trim method sets the trimming method, which can be used to trim
139 out extreme values. Three methods are currently supported:
140  
141 none No trimming
142 stdev Trim 1 standard deviation above and below mean
143 stdevN Trim N standard deviations above and below the mean
144  
145 In "stdevN", any can be any positive integer.
146  
147 =back
148  
149 =head2 Setting Data
150  
151 =over 4
152  
153 =item $wig->set_value($position => $value)
154  
155 This method sets the value at $position to $value. If a step>1 is in
156 force, then $position will be rounded down to the nearest multiple of
157 step.
158  
159 =item $wig->set_range($start=>$end, $value)
160  
161 This method sets the value of all bases between $start and $end to
162 $value, honoring step.
163  
164 =item $sig->set_values($position => \@values)
165  
166 This method writes an array of values into the datababase beginning at
167 $position (or the nearest lower multiple of step). If step>1, then
168 values will be written at step intervals.
169  
170 =back
171  
172 =head2 Retrieving Data
173  
174 =over 4
175  
176 =item $value = $wig->value($position)
177  
178 Retrieve the single data item at position $position, or the nearest
179 lower multiple of $step if step>1.
180  
181 =item $values = $wig->values($start=>$end)
182  
183 Retrieve the values in the range $start to $end and return them as an
184 array ref. Note that you will always get an array of size
185 ($end-$start+1) even if step>1; the data in between the step intervals
186 will be filled in.
187  
188 =item $values = $wig->values($start=>$end,$samples)
189  
190 Retrieve a sampling of the values between $start and $end. Nothing
191 very sophisticated is done here; the code simply returns the number of
192 values indicated in $samples, smoothed according to the smoothing
193 method selected (default to "mean"), then selected at even intervals
194 from the range $start to $end. The return value is an arrayref of
195 exactly $samples values.
196  
197 =item $string = $wig->export_to_wif($start,$end)
198  
199 =item $string = $wig->export_to_wif64($start,$end)
200  
201 Export the region from start to end in the "wif" format. This data can
202 later be imported into another Bio::Graphics::Wiggle object. The first
203 version returns a binary string. The second version returns a base64
204 encoded version that is safe for ascii-oriented formata such as GFF3
205 and XML.
206  
207 =item $wig->import_from_wif($string)
208  
209 =item $wig->import_from_wif64($string)
210  
211 Import a wif format data string into the Bio::Graphics::Wiggle
212 object. The first version expects a binary string. The second version
213 expects a base64 encoded version that is safe for ascii-oriented
214 formata such as GFF3 and XML.
215  
216 =back
217  
218  
219 =cut
220  
221 # read/write genome tiling data, to be compatible with Jim Kent's WIG format
222 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
223 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
224 1 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
225 1 ▶use Carp 'croak','carp','confess';
Subroutine BEGIN: called CC=1 SCAR=0.0
226  
227 1 ▶use constant HEADER_LEN => 256;
Subroutine BEGIN: called CC=1 SCAR=0.0
228 # seqid, step, min, max, span, mean, stdev, version, start
229 1 ▶use constant HEADER => '(Z50LFFLFFSL)@'.HEADER_LEN;
Subroutine BEGIN: called CC=1 SCAR=0.0
230 1 ▶use constant BODY => 'C';
Subroutine BEGIN: called CC=1 SCAR=0.0
231 1 ▶use constant DEBUG => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
232 1 ▶use constant DEFAULT_SMOOTHING => 'mean';
Subroutine BEGIN: called CC=1 SCAR=0.0
233 1 ▶use constant VERSION => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
234 1 our $VERSION = '1.0';
235  
236 sub new {
237 0 ▶ my $class = shift;
Subroutine new: not called CC=21 SCAR=61.4
Pod documented
238 0 my ($path,$write,$options) = @_;
239 0 ▶ $path ||= ''; # to avoid uninit warning
Condition0%0/2
$path ||= ""
l!l
00
MC/DC0%0/1
$path ||= ""
$path
Truth table0%0/2
A
A= $path
Aresult
11
00
240 0 ▶ my $mode = $write ? -e $path # if file already exists...
Branch: 2 branches
BranchTrueFalseTotal
$write ? : 0 0 0
-e $path ? : 0 0 0
241 ? '+<' # ...open for read/write
242 : '+>' # ...else clobber and open a new one
243 : '<'; # read only
244 0 my $fh = $class->new_fh($path,$mode);
245 0 ▶ $fh or die (($path||'temporary file').": $!");
Branch: 1 branch
BranchTrueFalseTotal
$fh or die +($path || "temporary file") . ": $!" 0 0 0
Condition0%0/2
$path || 'temporary file'
l!l
00
MC/DC0%0/1
$path || 'temporary file'
$path
Truth table0%0/2
A
A= $path
Aresult
11
00
246  
247 0 ▶ $options ||= {};
Condition0%0/2
$options ||= {}
l!l
00
MC/DC0%0/1
$options ||= {}
$options
Truth table0%0/2
A
A= $options
Aresult
11
00
248  
249 0 ▶ my $self = bless {fh => $fh,
Condition0%0/3
ref $class || $class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table0%0/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
250 write => $write,
251 dirty => scalar keys %$options
252 }, ref $class || $class;
253  
254 0 ▶ my $stored_options = eval {$self->_readoptions} || {};
Condition0%0/2
eval { do { $self->_readoptions } } || {}
l!l
00
MC/DC0%0/1
eval { do { $self->_readoptions } } || {}
eval { do { $self->_readoptions } }
Truth table0%0/2
A
A= eval { do { $self->_readoptions } }
Aresult
11
00
255 0 ▶ $options->{start}-- if defined $options->{start}; # 1-based ==> 0-based coordinates
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"start"} 0 0 0
256 0 my %merged_options = (%$stored_options,%$options);
257 # warn "merged options = ",join ' ',%merged_options;
258 0 ▶ $merged_options{version}||= 0;
Condition0%0/2
$merged_options{"version"} ||= 0
l!l
00
MC/DC0%0/1
$merged_options{"version"} ||= 0
$merged_options{"version"}
Truth table0%0/2
A
A= $merged_options{"version"}
Aresult
11
00
259 0 ▶ $merged_options{seqid} ||= 'chrUnknown';
Condition0%0/2
$merged_options{"seqid"} ||= "chrUnknown"
l!l
00
MC/DC0%0/1
$merged_options{"seqid"} ||= "chrUnknown"
$merged_options{"seqid"}
Truth table0%0/2
A
A= $merged_options{"seqid"}
Aresult
11
00
260 0 ▶ $merged_options{min} ||= 0;
Condition0%0/2
$merged_options{"min"} ||= 0
l!l
00
MC/DC0%0/1
$merged_options{"min"} ||= 0
$merged_options{"min"}
Truth table0%0/2
A
A= $merged_options{"min"}
Aresult
11
00
261 0 ▶ $merged_options{max} ||= 255;
Condition0%0/2
$merged_options{"max"} ||= 255
l!l
00
MC/DC0%0/1
$merged_options{"max"} ||= 255
$merged_options{"max"}
Truth table0%0/2
A
A= $merged_options{"max"}
Aresult
11
00
262 0 ▶ $merged_options{mean} ||= 128;
Condition0%0/2
$merged_options{"mean"} ||= 128
l!l
00
MC/DC0%0/1
$merged_options{"mean"} ||= 128
$merged_options{"mean"}
Truth table0%0/2
A
A= $merged_options{"mean"}
Aresult
11
00
263 0 ▶ $merged_options{stdev} ||= 255;
Condition0%0/2
$merged_options{"stdev"} ||= 255
l!l
00
MC/DC0%0/1
$merged_options{"stdev"} ||= 255
$merged_options{"stdev"}
Truth table0%0/2
A
A= $merged_options{"stdev"}
Aresult
11
00
264 0 ▶ $merged_options{trim} ||= 'none';
Condition0%0/2
$merged_options{"trim"} ||= "none"
l!l
00
MC/DC0%0/1
$merged_options{"trim"} ||= "none"
$merged_options{"trim"}
Truth table0%0/2
A
A= $merged_options{"trim"}
Aresult
11
00
265 0 ▶ $merged_options{step} ||= 1;
Condition0%0/2
$merged_options{"step"} ||= 1
l!l
00
MC/DC0%0/1
$merged_options{"step"} ||= 1
$merged_options{"step"}
Truth table0%0/2
A
A= $merged_options{"step"}
Aresult
11
00
266 0 ▶ $merged_options{start} ||= 0;
Condition0%0/2
$merged_options{"start"} ||= 0
l!l
00
MC/DC0%0/1
$merged_options{"start"} ||= 0
$merged_options{"start"}
Truth table0%0/2
A
A= $merged_options{"start"}
Aresult
11
00
267 0 ▶ $merged_options{span} ||= $merged_options{step};
Condition0%0/3
$merged_options{"span"} ||= $merged_options{"step"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$merged_options{"span"} ||= $merged_options{"step"}
$merged_options{"span"} $merged_options{"step"}
Truth table0%0/3
A ||= B
A= $merged_options{"span"}B= $merged_options{"step"}
ABresult
1X1
011
000
268 0 $self->{options} = \%merged_options;
269 0 ▶ $self->_do_trim unless $self->trim eq 'none';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->trim eq "none" 0 0 0
270 0 return $self;
271 }
272  
273 sub new_fh {
274 0 ▶ my $self = shift;
Subroutine new_fh: not called CC=2 SCAR=17.9
Pod undocumented
275 0 my ($path,$mode) = @_;
276 0 ▶ return $path ? IO::File->new($path,$mode)
Branch: 1 branch
BranchTrueFalseTotal
$path ? : 0 0 0
277 : IO::File->new_tmpfile;
278 }
279  
280 sub end {
281 0 ▶ my $self = shift;
Subroutine end: not called CC=3 SCAR=24.8
Pod undocumented
282 0 ▶ unless (defined $self->{end}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"end"}) 0 0 0
283 0 my $size = (stat($self->fh))[7];
284 0 my $data_len = $size - HEADER_LEN();
285 0 ▶ return unless $data_len>0; # undef end
Branch: 1 branch
BranchTrueFalseTotal
unless $data_len > 0 0 0 0
286 0 $self->{end} = ($self->start-1) + $data_len * $self->step;
287 }
288 0 return $self->{end};
289 }
290  
291 0 ▶sub DESTROY { shift->write }
Subroutine DESTROY: not called CC=1 SCAR=6.9
292  
293 sub erase {
294 0 ▶ my $self = shift;
Subroutine erase: not called CC=1 SCAR=6.9
Pod undocumented
295 0 $self->fh->truncate(HEADER_LEN);
296 }
297  
298 0 ▶sub fh { shift->{fh} }
Subroutine fh: not called CC=1 SCAR=6.9
Pod undocumented
299 0 ▶sub seek { shift->fh->seek(shift,0) }
Subroutine seek: not called CC=1 SCAR=6.9
Pod undocumented
300 0 ▶sub tell { shift->fh->tell() }
Subroutine tell: not called CC=1 SCAR=6.9
Pod undocumented
301  
302 sub _option {
303 0 ▶ my $self = shift;
Subroutine _option: not called CC=4 SCAR=30.0
304 0 my $option = shift;
305 0 my $d = $self->{options}{$option};
306 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
307 0 $self->{dirty}++;
308 0 $self->{options}{$option} = shift;
309 0 ▶ delete $self->{scale} if $option eq 'min' or $option eq 'max';
Branch: 1 branch
BranchTrueFalseTotal
if $option eq "min" or $option eq "max" 0 0 0
Condition0%0/3
$option eq "min" or $option eq "max"
l!l&&r!l&&!r
000
MC/DC0%0/2
$option eq "min" or $option eq "max"
$option eq "min" $option eq "max"
Truth table0%0/3
A or B
A= $option eq "min"B= $option eq "max"
ABresult
1X1
011
000
310 }
311 0 return $d;
312 }
313  
314 0 ▶sub version { shift->_option('version',@_) }
Subroutine version: not called CC=1 SCAR=6.9
Pod undocumented
315 0 ▶sub seqid { shift->_option('seqid',@_) }
Subroutine seqid: not called CC=1 SCAR=6.9
Pod documented
316 0 ▶sub min { shift->_option('min',@_) }
Subroutine min: not called CC=1 SCAR=6.9
Pod documented
317 0 ▶sub max { shift->_option('max',@_) }
Subroutine max: not called CC=1 SCAR=6.9
Pod documented
318 0 ▶sub step { shift->_option('step',@_) }
Subroutine step: not called CC=1 SCAR=6.9
Pod documented
319 0 ▶sub span { shift->_option('span',@_) }
Subroutine span: not called CC=1 SCAR=6.9
Pod documented
320 0 ▶sub mean { shift->_option('mean',@_) }
Subroutine mean: not called CC=1 SCAR=6.9
Pod documented
321 0 ▶sub stdev { shift->_option('stdev',@_) }
Subroutine stdev: not called CC=1 SCAR=6.9
Pod documented
322 0 ▶sub trim { shift->_option('trim',@_) }
Subroutine trim: not called CC=1 SCAR=6.9
Pod documented
323  
324 sub start { # slightly different because we have to deal with 1 vs 0-based coordinates
325 0 ▶ my $self = shift;
Subroutine start: not called CC=2 SCAR=17.9
Pod undocumented
326 0 my $start = $self->_option('start');
327 0 $start++; # convert into 1-based coordinates
328 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
329 0 my $newstart = shift;
330 0 $self->_option('start',$newstart-1); # store in zero-based coordinates
331 }
332 0 return $start;
333 }
334  
335 sub smoothing {
336 0 ▶ my $self = shift;
Subroutine smoothing: not called CC=3 SCAR=24.8
Pod undocumented
337 0 ▶ my $d = $self->{smoothing} || DEFAULT_SMOOTHING;
Condition0%0/2
$self->{'smoothing'} || 'mean'
l!l
00
MC/DC0%0/1
$self->{'smoothing'} || 'mean'
$self->{'smoothing'}
Truth table0%0/2
A
A= $self->{'smoothing'}
Aresult
11
00
338 0 ▶ $self->{smoothing} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
339 0 $d;
340 }
341  
342 sub write {
343 0 ▶ my $self = shift;
Subroutine write: not called CC=3 SCAR=24.8
Pod undocumented
344 0 ▶ if ($self->{dirty} && $self->{write}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"dirty"} and $self->{"write"}) 0 0 0
Condition0%0/3
$self->{"dirty"} and $self->{"write"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"dirty"} and $self->{"write"}
$self->{"dirty"} $self->{"write"}
Truth table0%0/3
A and B
A= $self->{"dirty"}B= $self->{"write"}
ABresult
0X0
100
111
345 0 $self->_writeoptions($self->{options});
346 0 undef $self->{dirty};
347 0 $self->fh->flush;
348 }
349 }
350  
351 sub _readoptions {
352 0 ▶ my $self = shift;
Subroutine _readoptions: not called CC=2 SCAR=17.9
353 0 my $fh = $self->fh;
354 0 my $header;
355 0 $fh->seek(0,0);
356 0 ▶ return unless $fh->read($header,HEADER_LEN) == HEADER_LEN;
Branch: 1 branch
BranchTrueFalseTotal
unless $fh->read($header, 256) == 256 0 0 0
357 0 return $self->_parse_header($header);
358 }
359  
360 sub _parse_header {
361 0 ▶ my $self = shift;
Subroutine _parse_header: not called CC=1 SCAR=6.9
362 0 my $header = shift;
363 0 my ($seqid,$step,$min,$max,$span,
364 $mean,$stdev,$version,$start) = unpack(HEADER,$header);
365 0 return { seqid => $seqid,
366 step => $step,
367 span => $span,
368 min => $min,
369 max => $max,
370 mean => $mean,
371 stdev => $stdev,
372 version => $version,
373 start => $start,
374 };
375 }
376  
377 sub _generate_header {
378 0 ▶ my $self = shift;
Subroutine _generate_header: not called CC=1 SCAR=6.9
379 0 my $options = shift;
380 0 return pack(HEADER,@{$options}{qw(seqid step min max span mean stdev version start)});
381 }
382  
383 sub _writeoptions {
384 0 ▶ my $self = shift;
Subroutine _writeoptions: not called CC=2 SCAR=17.9
385 0 my $options = shift;
386 0 my $fh = $self->fh;
387 0 my $header = $self->_generate_header($options);
388 0 $fh->seek(0,0);
389 0 ▶ $fh->print($header) or die "write failed: $!";
Branch: 1 branch
BranchTrueFalseTotal
$fh->print($header) or die "write failed: $!" 0 0 0
390 }
391  
392 sub _do_trim {
393 0 ▶ my $self = shift;
Subroutine _do_trim: not called CC=4 SCAR=30.0
394  
395 # don't trim if there is no score range
396 0 ▶ ($self->max - $self->min) or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->max - $self->min or return 0 0 0
397
398 0 my $trim = lc $self->trim;
399 0 my ($method,$arg);
400 0 ▶ if ($trim =~ /([a-z]+)(\d+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($trim =~ /([a-z]+)(\d+)/) { } 0 0 0
401 0 $method = "_trim_${1}";
402 0 $arg = $2;
403 }
404 else {
405 0 $method = "_trim_${trim}";
406 }
407 0 ▶ unless ($self->can($method)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->can($method)) 0 0 0
408 0 carp "invalid trim method $trim";
409 0 return;
410 }
411
412 0 $self->$method($arg);
413 }
414  
415 # trim n standard deviations from the mean
416 sub _trim_stdev {
417 0 ▶ my $self = shift;
Subroutine _trim_stdev: not called CC=4 SCAR=30.0
418 0 ▶ my $factor = shift || 1;
Condition0%0/2
shift() || 1
l!l
00
MC/DC0%0/1
shift() || 1
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
419 0 my $mean = $self->mean;
420 0 my $stdev = $self->stdev * $factor;
421 0 ▶ my $min = $self->min > $mean - $stdev ? $self->min : $mean - $stdev;
Branch: 1 branch
BranchTrueFalseTotal
$self->min > $mean - $stdev ? : 0 0 0
422 0 ▶ my $max = $self->max < $mean + $stdev ? $self->max : $mean + $stdev;
Branch: 1 branch
BranchTrueFalseTotal
$self->max < $mean + $stdev ? : 0 0 0
423 0 warn "_trim_stdev (* $factor) : setting min to $min, max to $max (was ",$self->min,',',$self->max,')'
424 if DEBUG;
425 0 $self->min($min);
426 0 $self->max($max);
427 }
428  
429 sub set_value {
430 0 ▶ my $self = shift;
Subroutine set_value: not called CC=2 SCAR=17.9
Pod documented
431 0 ▶ croak "usage: \$wig->set_value(\$position => \$value)"
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 2 0 0 0
432 unless @_ == 2;
433 0 $self->value(@_);
434 }
435  
436 sub set_range {
437 0 ▶ my $self = shift;
Subroutine set_range: not called CC=2 SCAR=17.9
Pod documented
438 0 ▶ croak "usage: \$wig->set_range(\$start_position => \$end_position, \$value)"
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 3 0 0 0
439 unless @_ == 3;
440 0 $self->value(@_);
441 }
442  
443 sub value {
444 0 ▶ my $self = shift;
Subroutine value: not called CC=18 SCAR=58.3
Pod documented
445 0 my $position = shift;
446  
447 0 my $offset = $self->_calculate_offset($position);
448 0 ▶ $offset >= HEADER_LEN or die "Tried to retrieve data from before start position";
Branch: 1 branch
BranchTrueFalseTotal
$offset >= 256 or die "Tried to retrieve data from before start position" 0 0 0
449 0 ▶ $self->seek($offset) or die "Seek failed: $!";
Branch: 1 branch
BranchTrueFalseTotal
$self->seek($offset) or die "Seek failed: $!" 0 0 0
450  
451 0 ▶ if (@_ == 2) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 2) { } 0 0 0
elsif (@_ == 1) { } 0 0 0
452 0 my $end = shift;
453 0 my $new_value = shift;
454 0 my $step = $self->step;
455 0 my $scaled_value = $self->scale($new_value);
456 0 ▶ $self->fh->print(pack('C*',($scaled_value)x(($end-$position+1)/$step))) or die "Write failed: $!";
Branch: 1 branch
BranchTrueFalseTotal
$self->fh->print(pack("C*", ($scaled_value) x (($end - $position + 1) / $step))) or die "Write failed: $!" 0 0 0
457 0 ▶ $self->{end} = $end if !exists $self->{end} || $self->{end} < $end;
Branch: 1 branch
BranchTrueFalseTotal
if not exists $self->{"end"} or $self->{"end"} < $end 0 0 0
Condition0%0/3
not exists $self->{"end"} or $self->{"end"} < $end
l!l&&r!l&&!r
000
MC/DC0%0/2
not exists $self->{"end"} or $self->{"end"} < $end
not exists $self->{"end"} $self->{"end"} < $end
Truth table0%0/3
not(A) or B
A= not exists $self->{"end"}B= $self->{"end"} < $end
ABresult
1X1
011
000
458 }
459  
460 elsif (@_==1) {
461 0 my $new_value = shift;
462 0 my $scaled_value = $self->scale($new_value);
463 0 ▶ $self->fh->print(pack('C*',$scaled_value)) or die "Write failed: $!";
Branch: 1 branch
BranchTrueFalseTotal
$self->fh->print(pack("C*", $scaled_value)) or die "Write failed: $!" 0 0 0
464 0 ▶ $self->{end} = $position if !exists $self->{end} || $self->{end} < $position;
Branch: 1 branch
BranchTrueFalseTotal
if not exists $self->{"end"} or $self->{"end"} < $position 0 0 0
Condition0%0/3
not exists $self->{"end"} or $self->{"end"} < $position
l!l&&r!l&&!r
000
MC/DC0%0/2
not exists $self->{"end"} or $self->{"end"} < $position
not exists $self->{"end"} $self->{"end"} < $position
Truth table0%0/3
not(A) or B
A= not exists $self->{"end"}B= $self->{"end"} < $position
ABresult
1X1
011
000
465 0 return $new_value;
466 }
467  
468 else { # retrieving data
469 0 my $buffer;
470 0 ▶ $self->fh->read($buffer,1) or die "Read failed: $!";
Branch: 1 branch
BranchTrueFalseTotal
$self->fh->read($buffer, 1) or die "Read failed: $!" 0 0 0
471 0 my $scaled_value = unpack('C*',$buffer);
472  
473 # missing data, so look back at most span values to get it
474 0 ▶ if ($scaled_value == 0 && (my $span = $self->span) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scaled_value == 0 and (my $span = $self->span) > 1) 0 0 0
Condition0%0/3
$scaled_value == 0 and (my $span = $self->span) > 1
!ll&&!rl&&r
000
MC/DC0%0/2
$scaled_value == 0 and (my $span = $self->span) > 1
$scaled_value == 0 (my $span = $self->span) > 1
Truth table0%0/3
A and B
A= $scaled_value == 0B= (my $span = $self->span) > 1
ABresult
0X0
100
111
475 0 $offset = $self->_calculate_offset($position-$span+1);
476 0 ▶ $offset >= HEADER_LEN or die "Tried to retrieve data from before start position";
Branch: 1 branch
BranchTrueFalseTotal
$offset >= 256 or die "Tried to retrieve data from before start position" 0 0 0
477 0 ▶ $self->seek($offset) or die "Seek failed: $!";
Branch: 1 branch
BranchTrueFalseTotal
$self->seek($offset) or die "Seek failed: $!" 0 0 0
478  
479 0 $self->fh->read($buffer,$span/$self->step);
480 0 ▶ for (my $i=length($buffer)-2;$i>=0;$i--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 0 0 0
481 0 my $val = substr($buffer,$i,1);
482 0 ▶ next if $val eq "\0";
Branch: 1 branch
BranchTrueFalseTotal
if $val eq "\0" 0 0 0
483 0 $scaled_value = unpack('C*',$val);
484 0 last;
485 }
486  
487 }
488 0 return $self->unscale($scaled_value);
489 }
490 }
491  
492 sub _calculate_offset {
493 0 ▶ my $self = shift;
Subroutine _calculate_offset: not called CC=1 SCAR=6.9
494 0 my $position = shift;
495 0 my $step = $self->step;
496 0 my $start = $self->start;
497 0 return HEADER_LEN + int(($position-$start)/$step);
498 }
499  
500 sub set_values {
501 0 ▶ my $self = shift;
Subroutine set_values: not called CC=3 SCAR=24.8
Pod documented
502 0 ▶ croak "usage: \$wig->set_values(\$position => \@values)"
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 2 and ref $_[1] eq "ARRAY" 0 0 0
Condition0%0/3
@_ == 2 and ref $_[1] eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
@_ == 2 and ref $_[1] eq "ARRAY"
@_ == 2 ref $_[1] eq "ARRAY"
Truth table0%0/3
A and B
A= @_ == 2B= ref $_[1] eq "ARRAY"
ABresult
0X0
100
111
503 unless @_ == 2 and ref $_[1] eq 'ARRAY';
504 0 $self->values(@_);
505 }
506  
507 # read or write a series of values
508 sub values {
509 0 ▶ my $self = shift;
Subroutine values: not called CC=3 SCAR=24.8
Pod documented
510 0 my $start = shift;
511 0 ▶ if (ref $_[0] && ref $_[0] eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] and ref $_[0] eq 'ARRAY') { } 0 0 0
Condition0%0/3
ref $_[0] and ref $_[0] eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[0] and ref $_[0] eq "ARRAY"
ref $_[0] ref $_[0] eq "ARRAY"
Truth table0%0/3
A and B
A= ref $_[0]B= ref $_[0] eq "ARRAY"
ABresult
0X0
100
111
512 0 $self->_store_values($start,@_);
513 } else {
514 0 $self->_retrieve_values($start,@_);
515 }
516 }
517  
518 sub export_to_wif64 {
519 0 ▶ my $self = shift;
Subroutine export_to_wif64: not called CC=2 SCAR=17.9
Pod documented
520 0 my $data = $self->export_to_wif(@_);
521 0 ▶ eval "require MIME::Base64"
Branch: 1 branch
BranchTrueFalseTotal
unless "MIME::Base64"->can("encode_base64") 0 0 0
522 unless MIME::Base64->can('encode_base64');
523 0 return MIME::Base64::encode_base64($data);
524 }
525 sub import_from_wif64 {
526 0 ▶ my $self = shift;
Subroutine import_from_wif64: not called CC=2 SCAR=17.9
Pod documented
527 0 my $data = shift;
528  
529 0 ▶ eval "require MIME::Base64"
Branch: 1 branch
BranchTrueFalseTotal
unless "MIME::Base64"->can("decode_base64") 0 0 0
530 unless MIME::Base64->can('decode_base64');
531 0 return $self->import_from_wif(MIME::Base64::decode_base64($data));
532 }
533  
534 # subregion in "wiggle interchange format" (wif)
535 sub export_to_wif {
536 0 ▶ my $self = shift;
Subroutine export_to_wif: not called CC=1 SCAR=6.9
Pod documented
537 0 my ($start,$end) = @_;
538  
539 # get the 256 byte header
540 0 my $data = $self->_generate_header($self->{options});
541  
542 # add the range to the data (8 bytes overhead)
543 0 $data .= pack("L",$start);
544 0 $data .= pack("L",$end);
545  
546 # add the packed data for this range
547 0 $data .= $self->_retrieve_packed_range($start,$end-$start+1,$self->step);
548 0 return $data;
549 }
550  
551 sub export_to_bedgraph {
552 0 ▶ my $self = shift;
Subroutine export_to_bedgraph: not called CC=5 SCAR=34.0
Pod undocumented
553 0 my ($start,$end,$fh) = @_;
554 0 my $max_range = 100_000;
555  
556 0 ▶ $start ||= 1;
Condition0%0/2
$start ||= 1
l!l
00
MC/DC0%0/1
$start ||= 1
$start
Truth table0%0/2
A
A= $start
Aresult
11
00
557 0 ▶ $end ||= $self->end;
Condition0%0/3
$end ||= $self->end
l!l&&r!l&&!r
000
MC/DC0%0/2
$end ||= $self->end
$end $self->end
Truth table0%0/3
A ||= B
A= $endB= $self->end
ABresult
1X1
011
000
558  
559 0 my $lines;
560 0 ▶ for (my $s=$start;$s<$end;$s+=$max_range) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s < $end) 0 0 0
561 0 my $e = $s + $max_range - 1;
562 0 ▶ $e = $end if $e > $end;
Branch: 1 branch
BranchTrueFalseTotal
if $e > $end 0 0 0
563 0 my $b = $self->values($s,$e);
564 0 $lines .= $self->_bedgraph_lines($s,$b,$fh);
565 }
566  
567 0 return $lines;
568 }
569  
570 sub _bedgraph_lines {
571 0 ▶ my $self = shift;
Subroutine _bedgraph_lines: not called CC=7 SCAR=40.3
572 0 my ($start,$values,$fh) = @_;
573 0 my $seqid = $self->seqid;
574 0 my $result;
575  
576 0 my ($last_val,$last_start,$end);
577 0 $last_start = $start-1; # 0 based indexing
578 0 ▶ for (my $i=0;$i<@$values;$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$values) 0 0 0
579 0 my $v = $values->[$i];
580  
581 0 ▶ if (!defined $v) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $v) 0 0 0
582 0 ▶ if (defined $last_val) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $last_val) 0 0 0
583 0 $result .= $self->_append_or_print_bedgraph($fh,$seqid,$last_start,$start+$i-1,$last_val);
584 0 undef $last_val;
585 }
586 0 $last_start = $start+$i;
587 0 next;
588 }
589  
590 0 ▶ if (defined $last_val && $last_val != $v) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $last_val and $last_val != $v) 0 0 0
Condition0%0/3
defined $last_val and $last_val != $v
!ll&&!rl&&r
000
MC/DC0%0/2
defined $last_val and $last_val != $v
defined $last_val $last_val != $v
Truth table0%0/3
A and B
A= defined $last_valB= $last_val != $v
ABresult
0X0
100
111
591 0 $result .= $self->_append_or_print_bedgraph($fh,$seqid,$last_start,$start+$i-1,$last_val);
592 0 $last_start = $start+$i-1;
593 }
594  
595 0 $last_val = $v;
596 0 $end = $start+$i-1;
597 }
598 0 ▶ $result .= $self->_append_or_print_bedgraph($fh,$seqid,$last_start,$end+1,$last_val) if $last_val;
Branch: 1 branch
BranchTrueFalseTotal
if $last_val 0 0 0
599 0 return $result;
600 }
601  
602 sub _append_or_print_bedgraph {
603 0 ▶ my $self = shift;
Subroutine _append_or_print_bedgraph: not called CC=2 SCAR=17.9
604 0 my ($fh,$seqid,$start,$end,$val) = @_;
605 0 my $data = join("\t",$seqid,$start,$end,sprintf("%.2f",$val))."\n";
606 0 ▶ if ($fh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fh) { } 0 0 0
607 0 print $fh $data;
608 0 return '';
609 } else {
610 0 return $data;
611 }
612 }
613  
614 sub import_from_wif {
615 0 ▶ my $self = shift;
Subroutine import_from_wif: not called CC=5 SCAR=34.0
Pod documented
616 0 my $wifdata = shift;
617  
618 # BUG: should check that header is compatible
619 0 my $header = substr($wifdata,0,HEADER_LEN);
620 0 my $start = unpack('L',substr($wifdata,HEADER_LEN, 4));
621 0 my $end = unpack('L',substr($wifdata,HEADER_LEN+4,4));
622  
623 0 my $options = $self->_parse_header($header);
624 0 ▶ my $stored_options = eval {$self->_readoptions} || {};
Condition0%0/2
eval { do { $self->_readoptions } } || {}
l!l
00
MC/DC0%0/1
eval { do { $self->_readoptions } } || {}
eval { do { $self->_readoptions } }
Truth table0%0/2
A
A= eval { do { $self->_readoptions } }
Aresult
11
00
625 0 my %merged_options = (%$stored_options,%$options);
626 0 $self->{options} = \%merged_options;
627 0 $self->{dirty}++;
628  
629 # write the data
630 0 $self->seek($self->_calculate_offset($start));
631 0 ▶ $self->fh->print(substr($wifdata,HEADER_LEN+8)) or die "write failed: $!";
Branch: 1 branch
BranchTrueFalseTotal
$self->fh->print(substr($wifdata, 264)) or die "write failed: $!" 0 0 0
632 0 ▶ $self->{end} = $end if !defined $self->{end} or $self->{end} < $end;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $self->{"end"} or $self->{"end"} < $end 0 0 0
Condition0%0/3
not defined $self->{"end"} or $self->{"end"} < $end
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $self->{"end"} or $self->{"end"} < $end
not defined $self->{"end"} $self->{"end"} < $end
Truth table0%0/3
not(A) or B
A= not defined $self->{"end"}B= $self->{"end"} < $end
ABresult
1X1
011
000
633 }
634  
635 sub _retrieve_values {
636 0 ▶ my $self = shift;
Subroutine _retrieve_values: not called CC=17 SCAR=57.2
637 0 my ($start,$end,$samples) = @_;
638  
639 0 my $data_start = $self->start;
640 0 my $step = $self->step;
641 0 my $span = $self->span;
642  
643 0 ▶ croak "Value of start position ($start) is less than data start of $data_start"
Branch: 1 branch
BranchTrueFalseTotal
unless $start >= $data_start 0 0 0
644 unless $start >= $data_start;
645 0 ▶ croak "Value of end position ($end) is greater than data end of ",$self->end+$span,
Branch: 1 branch
BranchTrueFalseTotal
unless $end <= $self->end + $span 0 0 0
646 unless $end <= $self->end + $span;
647  
648 # generate list of positions to sample from
649 0 my $length = $end-$start+1;
650 0 ▶ $samples ||= $length;
Condition0%0/3
$samples ||= $length
l!l&&r!l&&!r
000
MC/DC0%0/2
$samples ||= $length
$samples $length
Truth table0%0/3
A ||= B
A= $samplesB= $length
ABresult
1X1
011
000
651  
652 # warn "samples = $samples, length=$length, span=$span, step=$step";
653  
654 # if the length is grossly greater than the samples, then we won't even
655 # bother fetching all the data, but just sample into the disk file
656 0 ▶ if ($length/$samples > 100 && $step == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length / $samples > 100 and $step == 1) 0 0 0
Condition0%0/3
$length / $samples > 100 and $step == 1
!ll&&!rl&&r
000
MC/DC0%0/2
$length / $samples > 100 and $step == 1
$length / $samples > 100 $step == 1
Truth table0%0/3
A and B
A= $length / $samples > 100B= $step == 1
ABresult
0X0
100
111
657 0 my @result;
658 # my $window = 20*($span/$step);
659 0 my $interval = $length/$samples;
660 # my $window = 100*$interval/$span;
661 0 my $window = $interval/2;
662 # warn "window = $window, interval = $interval";
663 0 ▶ for (my $i=0;$i<$samples;$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $samples) 0 0 0
664 0 my $packed_data = $self->_retrieve_packed_range(int($start+$i*$interval-$window),
665 int($window),
666 $step);
667 0 my @bases= grep {$_} unpack('C*',$packed_data);
668 0 ▶ if (@bases) {
Branch: 1 branch
BranchTrueFalseTotal
if (@bases) { } 0 0 0
669 0 local $^W = 0;
670 0 my $arry = $self->unscale(\@bases);
671 0 my $n = @$arry;
672 0 my $total = 0;
673 0 $total += $_ foreach @$arry;
674 0 my $mean = $total/$n;
675 0 my $max;
676 0 ▶ for (@$arry) { $max = $_ if !defined $max || $max < $_ }
Branch: 1 branch
BranchTrueFalseTotal
if not defined $max or $max < $_ 0 0 0
Condition0%0/3
not defined $max or $max < $_
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $max or $max < $_
not defined $max $max < $_
Truth table0%0/3
not(A) or B
A= not defined $maxB= $max < $_
ABresult
1X1
011
000
677 # warn $start+$i*$interval,': ',join(',',map {int($_)} @$arry),
678 # " mean = $mean, max = $max";
679 # push @result,$mean;
680 0 push @result,$max;
681 } else {
682 0 push @result,0;
683 }
684 }
685 0 return \@result;
686 }
687  
688 0 my $packed_data = $self->_retrieve_packed_range($start,$length,$step);
689  
690 0 my @bases;
691 0 $#bases = $length-1;
692  
693 0 ▶ if ($step == $span) {
Branch: 1 branch
BranchTrueFalseTotal
if ($step == $span) { } 0 0 0
694 # in this case, we do not have any partially-empty
695 # steps, so can operate on the step-length data structure
696 # directly
697 0 @bases = unpack('C*',$packed_data);
698 }
699  
700 else {
701 # In this case some regions may have partially missing data,
702 # so we create an array equal to the length of the requested region,
703 # fill it in, and then sample it
704 0 ▶ for (my $i=0; $i<length $packed_data; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $packed_data) 0 0 0
705 0 my $index = $i * $step;
706 0 my $value = unpack('C',substr($packed_data,$i,1));
707 0 ▶ next unless $value; # ignore 0 values
Branch: 1 branch
BranchTrueFalseTotal
unless $value 0 0 0
708 0 @bases[$index..$index+$span-1] = ($value) x $span;
709 }
710 0 $#bases = $length-1;
711 }
712  
713 0 my $r = $self->unscale(\@bases);
714 0 $r = $self->sample($r,$samples);
715 0 ▶ $r = $self->smooth($r,$self->window * $samples/@bases)
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->window and $self->window > 1 0 0 0
Condition0%0/3
defined $self->window and $self->window > 1
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->window and $self->window > 1
defined $self->window $self->window > 1
Truth table0%0/3
A and B
A= defined $self->windowB= $self->window > 1
ABresult
0X0
100
111
716 if defined $self->window && $self->window>1;
717 0 return $r;
718 }
719  
720 sub _retrieve_packed_range {
721 0 ▶ my $self = shift;
Subroutine _retrieve_packed_range: not called CC=2 SCAR=17.9
722 0 my ($start,$length,$step) = @_;
723 0 my $span = $self->span;
724  
725 0 my $offset = $self->_calculate_offset($start);
726  
727 0 $self->seek($offset);
728 0 my $packed_data;
729 0 $self->fh->read($packed_data,$length/$step);
730  
731 # pad data up to required amount
732 0 ▶ $packed_data .= "\0" x ($length/$step-length($packed_data))
Branch: 1 branch
BranchTrueFalseTotal
if length $packed_data < $length / $step 0 0 0
733 if length $packed_data < $length/$step;
734 0 return $packed_data;
735 }
736  
737  
738 sub sample {
739 0 ▶ my $self = shift;
Subroutine sample: not called CC=3 SCAR=24.8
Pod undocumented
740 0 my ($values,$samples) = @_;
741 0 my $length = @$values;
742 0 my $window_size = $length/$samples;
743  
744 0 my @samples;
745 0 $#samples = $samples-1;
746  
747 0 ▶ if ($window_size < 2) { # no data smoothing needed
Branch: 1 branch
BranchTrueFalseTotal
if ($window_size < 2) { } 0 0 0
748 0 @samples = map { $values->[$_*$window_size] } (0..$samples-1);
749 }
750 else {
751 0 my $smoothsub = $self->smoothsub;
752 0 ▶ for (my $i=0; $i<$samples; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $samples) 0 0 0
753 0 my $start = $i * $window_size;
754 0 my $end = $start + $window_size - 1;
755 0 my @window = @{$values}[$start..$end];
756  
757 0 my $value = $smoothsub->(\@window);
758 0 $samples[$i] = $value;
759 }
760 }
761  
762 0 return \@samples;
763 }
764  
765 sub smoothsub {
766 0 ▶ my $self = shift;
Subroutine smoothsub: not called CC=5 SCAR=34.0
Pod undocumented
767
768 0 my $smoothing = $self->smoothing;
769 0 ▶ my $smoothsub = $smoothing eq 'mean' ? \&sample_mean
Branch: 4 branches
BranchTrueFalseTotal
$smoothing eq 'mean' ? : 0 0 0
$smoothing eq 'max' ? : 0 0 0
$smoothing eq 'min' ? : 0 0 0
$smoothing eq 'none' ? : 0 0 0
770 :$smoothing eq 'max' ? \&sample_max
771 :$smoothing eq 'min' ? \&sample_min
772 :$smoothing eq 'none' ? \&sample_center
773 :croak("invalid smoothing type '$smoothing'");
774 0 return $smoothsub;
775 }
776  
777 sub smooth {
778 0 ▶ my ($self,$data,$window) = @_;
Subroutine smooth: not called CC=6 SCAR=37.4
Pod undocumented
779  
780 0 my $smoothing = $self->smoothing;
781 0 ▶ $window ||= $self->window;
Condition0%0/3
$window ||= $self->window
l!l&&r!l&&!r
000
MC/DC0%0/2
$window ||= $self->window
$window $self->window
Truth table0%0/3
A ||= B
A= $windowB= $self->window
ABresult
1X1
011
000
782  
783 0 ▶ return $data if $smoothing eq 'none' || $window < 2;
Branch: 1 branch
BranchTrueFalseTotal
if $smoothing eq "none" or $window < 2 0 0 0
Condition0%0/3
$smoothing eq "none" or $window < 2
l!l&&r!l&&!r
000
MC/DC0%0/2
$smoothing eq "none" or $window < 2
$smoothing eq "none" $window < 2
Truth table0%0/3
A or B
A= $smoothing eq "none"B= $window < 2
ABresult
1X1
011
000
784  
785 0 my @data = @$data;
786 0 my $smoother = $self->smoothsub;
787 0 ▶ $window++ unless $window % 2;
Branch: 1 branch
BranchTrueFalseTotal
unless $window % 2 0 0 0
788 0 my $offset = int($window/2);
789  
790 0 ▶ for (my $i=$offset; $i<@$data-$offset; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$data - $offset) 0 0 0
791 0 my $start = $i - $offset;
792 0 my $end = $i + $offset;
793 0 my @subset = @data[$start..$end];
794 0 $data->[$i] = $smoother->(\@subset);
795 }
796  
797 0 return $data;
798 }
799  
800 sub window {
801 0 ▶ my $self = shift;
Subroutine window: not called CC=2 SCAR=17.9
Pod undocumented
802 0 my $d = $self->{window};
803 0 ▶ $self->{window} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
804 0 $d;
805 }
806  
807 sub sample_mean {
808 0 ▶ my $values = shift;
Subroutine sample_mean: not called CC=4 SCAR=30.0
Pod undocumented
809 0 my ($total,$items);
810 0 for my $v (@$values) {
811 0 ▶ next unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 0 0
812 0 $items++;
813 0 $total+=$v;
814 }
815 0 ▶ return $items ? $total/$items : undef;
Branch: 1 branch
BranchTrueFalseTotal
$items ? : 0 0 0
816 }
817  
818 sub sample_max {
819 0 ▶ my $values = shift;
Subroutine sample_max: not called CC=5 SCAR=34.0
Pod undocumented
820 0 my $max;
821 0 for my $v (@$values) {
822 0 ▶ next unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 0 0
823 0 ▶ $max = $v if !defined $max or $max < $v;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $max or $max < $v 0 0 0
Condition0%0/3
not defined $max or $max < $v
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $max or $max < $v
not defined $max $max < $v
Truth table0%0/3
not(A) or B
A= not defined $maxB= $max < $v
ABresult
1X1
011
000
824 }
825 0 return $max;
826 }
827  
828 sub sample_min {
829 0 ▶ my $values = shift;
Subroutine sample_min: not called CC=5 SCAR=34.0
Pod undocumented
830 0 my $min;
831 0 for my $v (@$values) {
832 0 ▶ next unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 0 0
833 0 ▶ $min = $v if !defined $min or $min > $v;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $min or $min > $v 0 0 0
Condition0%0/3
not defined $min or $min > $v
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $min or $min > $v
not defined $min $min > $v
Truth table0%0/3
not(A) or B
A= not defined $minB= $min > $v
ABresult
1X1
011
000
834 }
835 0 return $min;
836 }
837  
838 sub sample_center {
839 0 ▶ my $values = shift;
Subroutine sample_center: not called CC=1 SCAR=6.9
Pod undocumented
840 0 return $values->[@$values/2];
841 }
842  
843 sub _store_values {
844 0 ▶ my $self = shift;
Subroutine _store_values: not called CC=4 SCAR=30.0
845 0 my ($position,$data) = @_;
846  
847 # where does data start
848 0 my $offset = $self->_calculate_offset($position);
849 0 my $fh = $self->fh;
850 0 my $step = $self->step;
851  
852 0 my $scaled = $self->scale($data);
853  
854 0 $self->seek($offset);
855 0 my $packed_data = pack('C*',@$scaled);
856 0 ▶ $fh->print($packed_data) or die "Write failed: $!";
Branch: 1 branch
BranchTrueFalseTotal
$fh->print($packed_data) or die "Write failed: $!" 0 0 0
857  
858 0 my $new_end = $position+@$data-1;
859 0 ▶ $self->{end} = $new_end if !exists $self->{end} || $self->{end} < $new_end;
Branch: 1 branch
BranchTrueFalseTotal
if not exists $self->{"end"} or $self->{"end"} < $new_end 0 0 0
Condition0%0/3
not exists $self->{"end"} or $self->{"end"} < $new_end
l!l&&r!l&&!r
000
MC/DC0%0/2
not exists $self->{"end"} or $self->{"end"} < $new_end
not exists $self->{"end"} $self->{"end"} < $new_end
Truth table0%0/3
not(A) or B
A= not exists $self->{"end"}B= $self->{"end"} < $new_end
ABresult
1X1
011
000
860 }
861  
862 # zero means "no data"
863 # everything else is scaled from 1-255
864 sub scale {
865 0 ▶ my $self = shift;
Subroutine scale: not called CC=7 SCAR=40.3
Pod undocumented
866 0 my $values = shift;
867 0 my $scale = $self->_get_scale;
868 0 my $min = $self->{options}{min};
869 0 ▶ if (ref $values && ref $values eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $values and ref $values eq 'ARRAY') { } 0 0 0
Condition0%0/3
ref $values and ref $values eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $values and ref $values eq "ARRAY"
ref $values ref $values eq "ARRAY"
Truth table0%0/3
A and B
A= ref $valuesB= ref $values eq "ARRAY"
ABresult
0X0
100
111
870 my @return = map {
871 0 my $i = ($_ - $min)/$scale;
872 0 my $v = 1 + int($i+0.5*($i<=>0)); # avoid call to round()
873 0 ▶ $v = 1 if $v < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $v < 1 0 0 0
874 0 ▶ $v = 255 if $v > 255;
Branch: 1 branch
BranchTrueFalseTotal
if $v > 255 0 0 0
875 0 $v;
876 } @$values;
877 0 return \@return;
878 } else {
879 0 my $v = 1 + round (($values - $min)/$scale);
880 0 ▶ $v = 1 if $v < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $v < 1 0 0 0
881 0 ▶ $v = 255 if $v > 255;
Branch: 1 branch
BranchTrueFalseTotal
if $v > 255 0 0 0
882 0 return $v;
883 }
884 }
885  
886 sub unscale {
887 0 ▶ my $self = shift;
Subroutine unscale: not called CC=5 SCAR=34.0
Pod undocumented
888 0 my $values = shift;
889 0 my $scale = $self->_get_scale;
890 0 my $min = $self->{options}{min};
891  
892 0 ▶ if (ref $values && ref $values eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $values and ref $values eq 'ARRAY') { } 0 0 0
Condition0%0/3
ref $values and ref $values eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $values and ref $values eq "ARRAY"
ref $values ref $values eq "ARRAY"
Truth table0%0/3
A and B
A= ref $valuesB= ref $values eq "ARRAY"
ABresult
0X0
100
111
893 0 ▶ my @return = map {$_ ? (($_-1) * $scale + $min) : undef} @$values;
Branch: 1 branch
BranchTrueFalseTotal
$_ ? : 0 0 0
894 0 return \@return;
895 } else {
896 0 ▶ return $values ? ($values-1) * $scale + $min : undef;
Branch: 1 branch
BranchTrueFalseTotal
$values ? : 0 0 0
897 }
898 }
899  
900 sub _get_scale {
901 0 ▶ my $self = shift;
Subroutine _get_scale: not called CC=3 SCAR=24.8
902 0 ▶ unless ($self->{scale}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"scale"}) 0 0 0
903 0 my $min = $self->{options}{min};
904 0 my $max = $self->{options}{max};
905 0 ▶ my $range = $max - $min || 0.001; # can't be zero!
Condition0%0/2
$max - $min || 0.001
l!l
00
MC/DC0%0/1
$max - $min || 0.001
$max - $min
Truth table0%0/2
A
A= $max - $min
Aresult
11
00
906 0 $self->{scale} = $range/254;
907 }
908 0 return $self->{scale};
909 }
910  
911 sub round {
912 0 ▶ return int($_[0]+0.5*($_[0]<=>0));
Subroutine round: not called CC=1 SCAR=6.9
Pod undocumented
913 }
914  
915  
916 1 1;
917  
918 __END__
« blib/lib/Bio/Graphics/Glyph/trace.pm ↑ Summary blib/lib/Bio/Graphics/Glyph/hybrid_plot.pm »