/usr/share/perl5/Circos/Utils.pm is in circos 0.55-2.
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 | package Circos::Utils;
=pod
=head1 NAME
Circos::Utils - utility routines for 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(
pairwise_or
pairwise_and
round_custom
remap
remap_int
remap_round
str_to_list
sample_list
match_string
parse_as_rx
extract_number
compare_strs
compare_str
round_up
seek_parameter
is_hidden
defined_but_zero
defined_and_zero
locate_file
add_thousands_separator
);
use Carp qw( carp confess croak );
use FindBin;
use File::Spec::Functions;
use Math::Round;
use Params::Validate;
use Regexp::Common;
use POSIX qw(floor ceil);
use lib "$FindBin::RealBin";
use lib "$FindBin::RealBin/../lib";
use lib "$FindBin::RealBin/lib";
use Circos::Constants;
use Circos::Debug;
sub round_custom {
my ($x,$round_type) = @_;
if(! defined $round_type) {
return int $x;
} elsif ($round_type eq "round") {
return round $x;
} elsif ($round_type eq "floor") {
return floor $x;
} elsif ($round_type eq "ceil") {
return ceil $x;
}
}
#################################################################
#
sub remap {
my ($value,$min,$max,$remap_min,$remap_max) = @_;
if(! defined $value ||
! defined $min ||
! defined $max ||
! defined $remap_min ||
! defined $remap_max) {
confess "You must pass five parameters to remap(). The syntax is remap(value,min,max,remap_min,remap_max).";
}
return $remap_min if $value <= $min;
return $remap_max if $value >= $max;
if($min == $max) {
if($remap_min == $remap_max) {
return $remap_min;
} else {
confess "You must pass five parameters to remap(). The syntax is remap(value,min,max,remap_min,remap_max). You have set min=max ($min=$max), but this only makes sense if remap_min=remap_max ($remap_min != $remap_max)";
}
}
my $f = ( $value - $min ) / ( $max - $min );
my $value_remap = $remap_min + $f * ($remap_max - $remap_min);
#printinfo($value,$min,$max,$remap_min,$remap_max,$value_remap);
return $value_remap;
}
sub pairwise_or {
my ($a,$b,$x,$y) = @_;
if(! defined $y) {
confess "You have tried to use the pairwise_or(a,b,x,y) function but one of the values passed to it is not defined.";
} else {
return ($a eq $x && $b eq $y) || ($a eq $y && $b eq $x)
}
}
sub pairwise_and {
my ($a,$b,$x,$y) = @_;
if(! defined $y) {
confess "You have tried to use the pairwise_and(a,b,x,y) function but one of the values passed to it is not defined.";
} else {
return $a eq $x && $b eq $y;
}
}
sub remap_int {
return int remap(@_);
}
sub remap_round {
return round remap(@_);
}
sub str_to_list {
my $str = shift;
return split(/\s*,\s*/, $str);
}
# -------------------------------------------------------------------
sub sample_list {
# Given a list and regular expression, return the elements in the
# list that match the regular expression.
#
# The results are sorted based on capture buffers from the regular expression.
my ($rx,$list) = @_;
my $rev = 0;
if($rx =~ /rev\((.+)\)/) {
$rx = $1;
$rev = 1;
}
confess "Argument in sample_list must be a list" unless ref($list) eq "ARRAY";
return undef if ! $list;
my @matches;
for my $item (@$list) {
if($item =~ /^$rx$/) {
# pull out captured strings
my @captures = ();
for my $i (1..@+ - 1) {
my $str = substr($item,$-[$i],$+[$i]-$-[$i]);
push @captures, $str;
}
push @matches, {item=>$item,captures=>\@captures};
}
}
my @result = map { $_->{item} } sort { compare_strs($a->{captures},$b->{captures}) } @matches;
if($rev) {
return reverse @result;
} else {
return @result;
}
}
sub parse_as_rx {
my $rx = shift;
if ($rx =~ /^-?\/(.+)\/$/) {
return qr/$1/;
} else {
return;
}
}
sub match_string {
my ($str,$rx) = @_;
#printinfo(ref($rx));
return unless defined $rx;
if(ref($rx)) {
return $str =~ /$rx/;
} else {
return $str eq $rx;
}
}
# -------------------------------------------------------------------
sub compare_strs {
my ($list1,$list2) = @_;
my $result = 0;
for my $i (0..@$list1-1) {
return $result if ! defined $list1->[$i] || ! defined $list2->[$i];
$result ||= compare_str($list1->[$i],$list2->[$i]);
}
return $result;
}
# -------------------------------------------------------------------
sub extract_number {
my $str = shift;
if($str =~ /0*(\d+)/) {
return $1;
} else {
return "";
}
}
# -------------------------------------------------------------------
sub compare_str {
my ($x,$y) = @_;
if( $x =~ /$RE{num}{real}/ && $y =~ /$RE{num}{real}/ ) {
$x =~ s/^0*//;
$y =~ s/^0*//;
$x ||= 0;
$y ||= 0;
return $x <=> $y;
} else {
return $x cmp $y;
}
}
# -------------------------------------------------------------------
sub round_up {
my $value = shift;
if($value - int($value) > 0.5) {
return round($value);
} else {
return 1 + int($value);
}
}
# -------------------------------------------------------------------
sub seek_parameter {
# Given a parameter name and a list of hash references (or list
# references to hashes), looks for the parameter and returns the
# associated value. The parameter will also be extracted from any
# hash pointed to by the "param" key in the data structure.
#
# If the parameter name contains "|" then this is used as a
# delimiter to define synonyms of the parameter. This is helpful
# when parameters have changed names but you wish to maintain
# backward compatibility.
#
# value of x returned from $hash
# seek_parameter("x",$hash);
# value of x returned from $hash, and if not found, $anotherhash is tried
# seek_parameter("x",$hash,$anotherhash);
# value of x or y, whichever is seen first is returned
# seek_parameter("x|y",$hash,$anotherhash);
my ( $param_name, @data_structs ) = @_;
my @target_string = split( /\|/, $param_name );
for my $str (@target_string) {
for my $struct (@data_structs) {
if ( ref($struct) eq "ARRAY" ) {
for my $substruct (@$struct) {
if(exists $substruct->{param} && defined $substruct->{param}{$str}) {
return $substruct->{param}{$str};
}
if(exists $substruct->{$str} && defined $substruct->{$str}) {
return $substruct->{$str};
}
}
} elsif ( ref($struct) eq "HASH" ) {
if(exists $struct->{param} && defined $struct->{param}{$str}) {
return $struct->{param}{$str};
}
if(exists $struct->{$str} && defined $struct->{$str}) {
return $struct->{$str};
}
} else {
printdumper(\@data_structs);
croak "cannot extract parameter from this data structure (shown above - report this please)";
}
}
}
return undef;
}
sub is_hidden {
my @datapath = @_;
my $show_state = seek_parameter("show", @datapath);
return defined_and_zero($show_state);
}
# -------------------------------------------------------------------
sub locate_file {
$Circos::Configuration::CONF{debug_validate} && validate( @_, { file => 1, return_undef => 0 } );
my %params = @_;
my $file = $params{file};
printdebug_group("io","locating file",$file);
my @dirs;
if ( -e $file && -r _ ) {
return $file;
} elsif ( -e $file && !-r _ ) {
confess "File [$file] exists, but cannot be read. Do you have permissions to read it?";
} else {
# look for the file elsewhere
@dirs = (
"/usr/share/circos",
"$FindBin::RealBin/../",
"$FindBin::RealBin/",
"$FindBin::RealBin/etc",
"$FindBin::RealBin/../etc",
"$FindBin::RealBin/../../etc",
);
for my $dir (@dirs) {
printdebug_group("io","trying $dir/$file");
if ( -e catfile($dir,$file) && -r catfile($dir,$file) ) {
printdebug_group("io","$file found in $dir/$file");
return catfile($dir,$file);
}
}
}
if ( $params{return_undef} ) {
return undef;
} else {
printinfo("Tried to find file [$file] in these directories, but failed.");
for my $dir (@dirs) {
printinfo($dir);
}
confess "Could not locate file [$file].";
}
}
# -------------------------------------------------------------------
sub add_thousands_separator {
my $str = shift;
my $sep = shift || $COMMA;
if ( $str =~ /\./ ) {
$str =~ s/(?<=\d)(?=(\d{3})+\.)/,/g;
} else {
$str =~ s/(?<=\d)(?=(\d{3})+$)/,/g;
}
return $str;
}
# -------------------------------------------------------------------
sub defined_but_zero {
return defined $_[0] && !$_[0];
}
# -------------------------------------------------------------------
sub defined_and_zero {
return defined $_[0] && !$_[0];
}
1;
|