This file is indexed.

/usr/share/perl5/Circos/Unit.pm is in circos 0.69.6+dfsg-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
package Circos::Unit;

=pod

=head1 NAME

Circos::Unit - utility routines for units in Circos

=head1 SYNOPSIS

This module is not meant to be used directly.

=head1 DESCRIPTION

Circos is an application for the generation of publication-quality,
circularly composited renditions of genomic data and related
annotations.

Circos is particularly suited for visualizing alignments, conservation
and intra and inter-chromosomal relationships. However, Circos can be
used to plot any kind of 2D data in a circular layout - its use is not
limited to genomics. Circos' use of lines to relate position pairs
(ribbons add a thickness parameter to each end) is effective to
display relationships between objects or positions on one or more
scales.

All documentation is in the form of tutorials at L<http://www.circos.ca>.

=cut

# -------------------------------------------------------------------

use strict;
use warnings;

use base 'Exporter';
our @EXPORT = qw(
unit_fetch
unit_validate
unit_split
unit_strip
unit_test
unit_convert
unit_parse
parse_suffixed_number
);

use Carp qw( carp confess croak );
use FindBin;
use Data::Dumper;
use Params::Validate qw(:all);
use Regexp::Common qw(number);

use lib "$FindBin::RealBin";
use lib "$FindBin::RealBin/../lib";
use lib "$FindBin::RealBin/lib";

use Circos::Configuration qw(%CONF $DIMS);
use Circos::Constants;
use Circos::Debug;
use Circos::Error;

use Memoize;

for my $f (qw(unit_fetch unit_validate unit_split unit_strip unit_test unit_parse )) {
memoize($f);
}
memoize("unit_convert",NORMALIZER=>"unit_convert_normalizer");

# -------------------------------------------------------------------
sub unit_fetch {
  # Return a value's unit, with sanity checks. The unit fetch is the
  # basic unit access function and it should be the basis for any
  # other unit access wrappers. This is the only function that
  # checks against a list of acceptable units.
  #
  # Returns the value of units_nounit if the value has no unit
  # (i.e., bare number)
  #
  # Returns undef if the value string does not end in one of the
  # valid unit types
  #
  # If you just want to test the sanity of a value's format, call
  # unit_fetch in void context

  my ($value,$param) = @_;

  printdebug_group("unit","fetching unit from",$value);
  if ( ! $CONF{units_ok} ) {
      fatal_error("system","missing_units_ok","bupr");
  }
  if ( ! $CONF{units_nounit} ) {
      fatal_error("system","missing_units_nounit","n");
  }
  if (! defined $value) {
      return $CONF{units_nounit};
  } elsif ( $value =~ /\d$/ ) {
      # any value that ends in a number has no u nit
      printdebug_group("unit",$value,"ends in number - no unit");
      return $CONF{units_nounit};
  } elsif ( $value =~ /^(.*)([$CONF{units_ok}$CONF{units_nounit}])$/ ) {
      my ($root,$unit) = ($1,$2);
      printdebug_group("unit",$value,"root",$root,"candidate unit",$unit);
      # in order for $unit to be considered a unit, $root must be a real number
      if($root =~ /$RE{num}{real}/) {
	  printdebug_group("unit",$value,"root",$root,"is a number - unit",$unit);
	  return $unit;
      } else {
	  printdebug_group("unit",$value,"root",$root,"is not a number - no unit");
	  return $CONF{units_nounit};
      }
      #return $1;
  } else {
      return $CONF{units_nounit};
      #confess "The parameter [$param] value [$value] is incorrectly formatted.";
  }
}

# -------------------------------------------------------------------
sub unit_validate {
  # Verify that a value's unit is one out of a provided list
  #
  # potential units are
  #
  # r : relative
  # p : pixel
  # u : chromosome unit (defined by chromosomes_unit parameter)
  # b : bases, or whatever your natural unit of distance is along the ideogram
  # n : no unit; value is expected to end in a digit
  #
  # If called without a list of acceptable units, unit_validate returns
  # the value if it is correctly formatted (i.e., an acceptable unit is found)
  # stripped of its unit

  my ( $value, $param, @unit ) = @_;
  confess "not units provided" unless @unit;

  # unit_fetch will die if $value isn't correctly formatted
  my $value_unit = unit_fetch( $value, $param );
  if ( grep( $_ eq $value_unit, @unit ) ) {
    return $value;
  } else {
      if(defined $value) {
				fatal_error("system","wrong_unit",$param,$value,$value_unit,join($COMMA,@unit));
      } else {
				fatal_error("system","undef_parameter",$param,join($COMMA,@unit));
      }
		}
}

# -------------------------------------------------------------------
sub unit_split {
  # Separate the unit from the value, and return the unit-less
  # number and the unit as a list
    my ($value,$param) = @_;
    my $unit         = unit_fetch( $value, $param );
    my $value_nounit = unit_strip( $value, $param );
    return ( $value_nounit, $unit );
}

# -------------------------------------------------------------------
sub unit_strip {
  # Remove the unit from a value and return the unit-less value
  my $value = shift;
  my $param = shift;
  return undef if ! defined $value;
  my $unit  = unit_fetch($value,$param);
  $value =~ s/$unit$// unless $unit eq $CONF{units_nounit};
  return $value;
}

# -------------------------------------------------------------------
sub unit_test {
  # Verify that a unit is acceptable. If so, return the unit, otherwise
  # die.
  my $unit = shift;
  if ( $unit =~ /[$CONF{units_ok}]/o || $unit eq $CONF{units_nounit} ) {
    return $unit;
  } else {
      fatal_error("system","unit_format_fail",$unit);
  }
}

# -------------------------------------------------------------------
# Create input string for unit_convert used by Memoize
sub unit_convert_normalizer {
	my %params = @_;
	my @norm;
	push @norm, $params{from};
	push @norm, $params{to};
	if(exists $params{factors}) {
		for my $key (sort keys %{$params{factors}}) {
	    push @norm, $key, $params{factors}{$key} || 0;
		}
	}
	return join(",",@norm);
}

# -------------------------------------------------------------------
sub unit_convert {
    # Convert a value from one unit to another.
    start_timer("unitconvert");
    $CONF{debug_validate} && validate(
	@_,
	{
	    from    => { type => SCALAR },
	    to      => { type => SCALAR },
	    factors => { type => HASHREF, optional => 1 },
	}
	);
    my %params = @_;
    start_timer("unitconvert_delegate");
    my ( $value, $unit_from ) = unit_split( $params{from} );
    my $unit_to               = unit_test(  $params{to} );
    stop_timer("unitconvert_delegate");
    my $factors = $params{factors};
    
    # by default, no unit is the same as pixel unit
    $factors->{"np"} ||= 1;
    $factors->{"pn"} ||= 1;
    $factors->{"nb"} ||= 1;
    $factors->{"ub"} ||= Circos::Configuration::fetch_conf("chromosomes_units");
	
    start_timer("unitconvert_decision");
    my $return;
    if ( $factors->{ $unit_from . $unit_to } ) {
	$return = $value * $factors->{ $unit_from . $unit_to };
    } elsif ( $factors->{ $unit_to . $unit_from } ) {
	$return = $value * 1 / $factors->{ $unit_from . $unit_to };
    } elsif ( $unit_to eq $unit_from ) {
	$return = $value;
    } else {
	fatal_error("unit","conversion_fail",
		    $value,$unit_from,$unit_to,
		    join(" ",map { join("->",split("",$_)) } 
			 grep($_ ne "np" && $_ ne "pn", keys %{$params{factors}})));
    }
    stop_timer("unitconvert_decision");
    stop_timer("unitconvert");
    return $return;
}

# -------------------------------------------------------------------
sub unit_parse {
    # Parses a variable value that contains units. The value can be a single
    # value like
    #
    # 0.1r
    #
    # or an arithmetic expression
    #
    # TERM +/- TERM +/- TERM ...
    #
    # where TERM is one of
    #
    # 1. single value with any supported unit
    # 2. the string "dims(a,b)" for some parameters a,b
    
    start_timer("unitparse");

    my $expression = shift;
    my $ideogram   = shift;
    my $side       = shift;
    my $relative   = shift;
    
    printdebug_group("unit","parse",$expression,$side,$relative);
    if(! defined $expression) {
			stop_timer("unitparse");
			return undef;
    }
    
    my $radius_flag;
    if ( defined $side ) {
			if ( $side eq $DASH || !$side || $side =~ /inner/i ) {
				$radius_flag = "radius_inner";
			} elsif ( $side eq $PLUS_SIGN || $side == 1 || $side =~ /outer/i ) {
				$radius_flag = "radius_outer";
			}
    }
    
    if ($ideogram) {
			$expression =~ s/ideogram,/ideogram,$ideogram->{tag},/g;
    } else {
			$expression =~ s/ideogram,/ideogram,default,/g;
		}
    
    while ( $expression =~ /(dims\(([^\)]+)\))/g ) {
			my $string = $1;
			my $hash   = "\$" . $string;
			my @args   = split( $COMMA, $2 );
			
			#printinfo("dims",$string,"args",@args);
			$hash = sprintf( "\$DIMS->%s",join( $EMPTY_STR, map { sprintf( "{'%s'}", $_ ) } @args ) );
			
			#printdumper($DIMS->{ideogram}{default});
			my $hash_value = eval $hash;
			if(! defined $hash_value) {
				printdumper($DIMS->{ideogram});
	    fatal_error("system","bad_dimension",$hash,$expression) if ! defined $hash_value;
			}
			$expression =~ s/\Q$string\E/$hash_value/g;
    }
    
    while ( $expression =~ /([\d\.]+[$CONF{units_ok}])/g ) {

			my $string = $1;
			my ( $value, $unit ) = unit_split($string);
			my $value_converted;
			
			if ( $unit eq "u" ) {
				
				# convert from chromosome units to bases
				$value_converted = unit_convert(
																				from    => $string,
																				to      => "b",
																				factors => { ub => $CONF{chromosomes_units} }
																			 );
			} else {
				
				# convert from relative or pixel to pixel
				my $rpfactor;
				my $tag = $ideogram ? $ideogram->{tag} : "default";
				#printdumper($ideogram) if $ideogram->{chr} eq "hs1";
				if ( $value < 1 ) {
					$rpfactor = $relative
						|| $DIMS->{ideogram}{$tag}{ $radius_flag || "radius_inner" };
				} else {
					$rpfactor = $relative
						|| $DIMS->{ideogram}{$tag}{ $radius_flag || "radius_outer" };
				}
				$value_converted = unit_convert(
																				from    => $string,
																				to      => "p",
																				factors => { rp => $rpfactor }
																			 );
			}
			$expression =~ s/$string/$value_converted/;
    }
    $expression = eval $expression;
    stop_timer("unitparse");
    return $expression;
	}

sub parse_suffixed_number {
	my $str          = shift;
	return if ! defined $str;
	my $suffix_power = { k=>3, m=>6, g=>9, t=>12 };
	my $parsed;
	while($str =~ /(([\d+.]+)([kmgt])b?)/ig) {
		$parsed = 1;
		my ($to_replace,$num,$suffix) = ($1,$2,$3);
		$suffix = lc $suffix;
		if(! defined $suffix_power->{$suffix}) {
			error("ticks","bad_suffix",$suffix,$to_replace);
		} else {
			my $multiplier = 10**$suffix_power->{$suffix};
			$str =~ s/$to_replace/($num*$multiplier)/;
		}
	}
	if($parsed) {
		my $str_eval = eval $str;
		if ($@) {
			error("ticks","unparsable",$str);
		}
		return $str_eval;
	} else {
		return $str;
	}
}

1;