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 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815
|
#!/usr/bin/env perl
=pod
=head1 NAME
parse-table - parse tabular data, such as created by make-table, and
report values suitable for make-conf which creates Circos input files
to visualize the table
=head1 SYNOPSIS
cat table.txt | parse-table
parse-table -file table.txt
=head1 DESCRIPTION
This script parses tabular data and generates the circos input and
configuration files used to create circular table views as described
here
http://mkweb.bcgsc.ca/tableviewer/
=head1 INPUT FORMAT
All input files must be plain-text. The simplest format is
- D E F
A 1 2 3
B 4 5 6
C 7 8 9
The first row contains the column labels and the first column contains
row labels. Value of the first cell is unimportant (e.g. '-') but
required to keep the number of fields in each row constant.
Three additional rows and columns can be added, which specify the order
of rows/columns, their color and their size.
Order values are used to determine placement of column and row segments.
# table with order values
#
# requires
# col_order_row = yes
# row_order_col = yes
- - 3 6 5
- - D E F
1 A 1 2 3
4 B 4 5 6
2 C 7 8 9
# table with color values
#
# requires
# col_color_row = yes
# row_color_col = yes
- - 255,0,0 0,255,0 0,0,255
- - D E F
255,255,0 A 1 2 3
255,0,255 B 4 5 6
0,255,255 C 7 8 9
# table with row order and color values
#
# requires
# col_order_row = yes
# row_order_col = yes
# col_color_row = yes
# row_color_col = yes
- - - 3 6 5
- - - 255,0,0 0,255,0 0,0,255
- - - D E F
1 255,255,0 A 1 2 3
4 255,0,255 B 4 5 6
2 0,255,255 C 7 8 9
To specify the color of a subset of columns and rows, use "-" in color cells for which there is no color value.
=head1 SAMPLE DATA
There are various sample data files in C<samples/>. You can create images for each of the sample data sets using the C<makeimage> script
makeimage 01
makeimage 02a
makeimage 02b
makeimage 02c
makeimage 02d
makeimage 03
makeimage 04a
makeimage 04b
makeimage 05
makeimage 06
=head1 CONFIGURATION
All configuration parameters are controlled by the configuration
file. For details about the file's structure, see
C<etc/parse-table.conf>.
=head1 HISTORY
=over
=item * 26 Nov 2012 v0.31
No longer uses Graphics::ColorObject.
=item * 22 Nov 2012 v0.3
Updated documentation.
=item * 1 Apr 2009 v0.2
Added new segment order schemes: row_size, col_size, row_to_col_ratio, col_to_row_ratio, file.
Added ability to represent (A,B)=x and (B,A)=y table values as one ribbon of thickness x at segment A and y at segmetn B.
Added ability to normalize segment sizes using these schemes: total, average, row_total, col_total, row_average, col_average, or uniform.
Added ability to have segments internally named after row/col labels. Previously each segment had an internally generated name (e.g. id0, id1, etc).
Added ability to layer ribbons (ribbon_layer_order) using these schemes: size_desc, size_asc
Added strip_leading_space and remove_lines_rx parameters to toggle removal of any leading spaces in input lines and remove lines that match a given regular expression.
Added field_delim parameter to explicitly specify field delimiter in the input file. By default, this is set to space. Note that adjacent delimiters will not be collapsed and will be interpreted to define an empty cell.
Added percentile_data_domain to generate percentile color palette based on either raw or filtered values.
Added cell_remap_formula to allow remapping of cell values.
Added colour_source in <linkcolor> block to specify whether ribbons are colored by row or column segments.
=item * 21 Jan 2009 v0.1
Standardized configuration and added segment/cell order features.
Bundled with Circos distribution.
=item * 25 Jun 2008
Added more error traps.
=item * 11 June 2008
Continuing to refine and debug.
=item * 2 June 2008
Versioned and updated.
=back
=head1 BUGS
=head1 AUTHOR
Martin Krzywinski
=head1 CONTACT
Martin Krzywinski
Genome Sciences Centre
Vancouver BC Canada
www.bcgsc.ca
martink@bcgsc.ca
=cut
################################################################
#
# Copyright 2002-2012 Martin Krzywinski
#
# This file is part of the Genome Sciences Centre Perl code base.
#
# This script is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This script is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this script; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
################################################################
use strict;
use Config::General;
use Data::Dumper;
use File::Basename;
use FindBin;
use Getopt::Long;
use IO::File;
use Math::VecStat qw(sum min max average);
use Math::Round qw(round nearest);
use Pod::Usage;
use Set::IntSpan;
use Statistics::Descriptive;
use Storable;
use Time::HiRes qw(gettimeofday tv_interval);
use lib "$FindBin::RealBin";
use lib "$FindBin::RealBin/../lib";
use lib "$FindBin::RealBin/lib";
use vars qw(%OPT %CONF);
use tableviewer_io;
use tableviewer_format;
################################################################
#
# *** YOUR MODULE IMPORTS HERE
#
################################################################
GetOptions(\%OPT,
"file=s",
"color_prefix=s",
"skip_rows=i",
"header",
"row_order_col",
"col_order_row",
"use_row_order_col",
"use_col_order_row",
"row_color_col",
"col_color_row",
"use_row_color_col",
"use_col_color_row",
"row_size_col",
"col_size_row",
"use_row_size_col",
"use_col_size_row",
"intra_cell_exclude",
"intra_cell_size_remove",
"size_recompute",
"use_scaling",
"scale_factor=f",
"segment_order=s",
"segment_color_order=s",
"percentile_sampling=f",
"percentile_hue_start=i",
"percentile_hue_end=i",
"percentile_saturation_start=i",
"percentile_saturation_end=i",
"percentile_brightness_start=i",
"percentile_brightness_end=i",
"cell_q1_transparency=i",
"cell_q2_transparency=i",
"cell_q3_transparency=i",
"cell_q4_transparency=i",
"cell_q1_color=s",
"cell_q2_color=s",
"cell_q3_color=s",
"cell_q4_color=s",
"cell_q1_nostroke",
"cell_q2_nostroke",
"cell_q3_nostroke",
"cell_q4_nostroke",
"cell_min_percentile=f",
"cell_max_percentile=f",
"cell_min_value=f",
"cell_max_value=f",
"data_mult=f",
"color_idx_mult=f",
"color_remap",
"color_autoremap",
"use_segment_normalization",
"segment_normalization_scheme=s",
"normalize_contribution",
"ribbon_bundle_order=s",
"ribbon_layer_order=s",
"ribbon_variable",
"intra_cell_handling=s",
"show_parsed",
"field_delim=s",
"field_delim_collapse!",
"strip_leading_space!",
"blank_means_missing!",
"missing_cell_value=s",
"remove_cell_rx=s",
"placement_order=s",
"reverse_rows",
"reverse_columns",
"transparency=i",
"fade_transparency=i",
"color_source=s",
"interpolate_type=s",
"max_col_num=i",
"max_row_num=i",
"configfile=s","help","man","debug+");
pod2usage() if $OPT{help};
pod2usage(-verbose=>2) if $OPT{man};
loadconfiguration($OPT{configfile});
populateconfiguration(); # copy command line options to config hash
validateconfiguration();
if($CONF{debug} > 1) {
$Data::Dumper::Pad = "debug parameters";
$Data::Dumper::Indent = 1;
$Data::Dumper::Quotekeys = 0;
$Data::Dumper::Terse = 1;
print Dumper(\%CONF);
}
################################################################
# setup the input handle to be the file, if specified, otherwise
# read from STDIN
my $inputhandle = prep_handle($CONF{file});
################################################################
# all the table data (row/column/cell) is stored in $table
# all statistics (row/column/cell/label) is stored in $stat
my $table;
# counters for things such as skipped lines, row index, column index
my $counter;
my ($ncols,$seen,$row_idx,$segment_order,$label_size,$row_idx,$label_color,$nskip);
LINE:
while(<$inputhandle>) {
chomp;
# skip lines that match a regular expression
next if $CONF{remove_lines_rx} && /$CONF{remove_lines_rx}/;
# skip comment lines
next if /^\s*\#/;
my $line = $_;
next if $CONF{skip_rows} && $counter->{skipped_lines}++ < $CONF{skip_rows};
my @tok = clean_and_split_line($_);
next unless @tok;
# determine the number of columns - do this every time to catch any lines
# with different number of fields
printdebug("read line",@tok);
printdebug("columns in line",@tok+1);
my $ncols_this_line = @tok;
if(! defined $ncols) {
$ncols = $ncols_this_line;
} elsif ($ncols_this_line != $ncols) {
report_error("This line [$line] has a different number of columns ($ncols_this_line) than the previous line which had ($ncols) columns. Please make sure that every line has the same number of fields. Empty cells should appear in the input file as either (A) string defined by missing_cell_value (e.g. -), or (B) adjacent delimiters (e.g. 2<tab><tab>3) with -blank_means_missing=yes and field_delim_collapse=no. For an example of how missing data is encoded, see samples/parse-example-N.txt N=2,3");
}
# parse column information
# - extract column names
# - extract column order, if available
# - extract column size, if available
# - extract column color, if available
# column information is stored in in $table->{col}
# - $table->{col}{list}[IDX] = $coldata;
# {name,idx,size?,order?,color?}
if($CONF{col_order_row} && ! $seen->{col_order_row}++) {
my $skip_cols = $CONF{use_row_order_col} + $CONF{use_row_color_col} + $CONF{use_row_size_col} + 1;
printdebug("reading column order starting at col",$skip_cols);
for my $i ( $skip_cols .. $ncols - 1) {
my $col_idx = $i - $skip_cols;
last if $col_idx >= $CONF{max_col_num};
my $col_order = $tok[$i];
printdebug("read column order",$col_order,"from column",$i,"col_idx",$col_idx);
if($CONF{use_col_order_row}) {
report_error("Column at order value ($tok[$i]) already exists. Please make sure that there are no duplicate order values in the row that specifies column order.") if $seen->{col_order}{$col_order}++;
$table->{col}{list}[$col_idx]{order} = $col_order;
report_error("Column order value ($col_order) is not numeric",$line) unless $col_order =~ /^\d+$/;
printdebug("found column order",$col_idx,$col_order);
}
}
next LINE;
}
if($CONF{col_size_row} && ! $seen->{col_size_row}++) {
my $skip_cols = $CONF{use_row_order_col} + $CONF{use_row_color_col} + $CONF{use_row_size_col} + 1;
printdebug("reading column size starting at col",$skip_cols);
for my $i ( $skip_cols .. $ncols - 1) {
my $col_idx = $i - $skip_cols;
last if $col_idx >= $CONF{max_col_num};
my $col_size = $tok[$i];
printdebug("read column size",$col_size,"from column",$i,"col_idx",$col_idx);
if($CONF{use_col_size_row}) {
report_error("Column size ($col_size) is not numeric. You have specified that a row with column sizes exists, but it doesn't seem that way. Are you sure that your setting of col_size_row=yes is correct?") unless $col_size =~ /^\d*\.?\d*$/;
$table->{col}{list}[$col_idx]{size} = $col_size;
printdebug("found column size",$col_idx,$col_size);
}
}
next LINE;
}
if($CONF{col_color_row} && ! $seen->{col_color_row}++) {
my $skip_cols = $CONF{use_row_order_col} + $CONF{use_row_color_col} + $CONF{use_row_size_col} + 1;
printdebug("reading column color starting at col",$skip_cols);
for my $i ( $skip_cols .. $ncols - 1) {
my $col_idx = $i - $skip_cols;
last if $col_idx >= $CONF{max_col_num};
my $col_color = $tok[$i];
report_error("Column color must be in r,g,b format (e.g. 100,50,150). The value ($col_color) does not match this format."."\n \n"."line: $line") if $col_color && $col_color !~ /^\d+,\d+,\d+$/;
if($CONF{use_col_color_row}) {
$table->{col}{list}[$col_idx]{color} = $col_color;
printdebug("Found column color",$col_idx,$col_color);
}
}
next LINE;
}
if($CONF{header} && ! $seen->{header}++) {
my $skip_cols = $CONF{use_row_order_col} + $CONF{use_row_color_col} + $CONF{use_row_size_col} + 1;
printdebug("reading column names starting at col",$skip_cols);
for my $i ( $skip_cols .. $ncols - 1) {
my $col_idx = $i - $skip_cols;
last if $col_idx >= $CONF{max_col_num};
my $name = parse_label($tok[$i]);
printdebug("labelparse",$col_idx,$tok[$i],$name);
if($name eq "") {
report_error("Blank column name found. This is likely due to multiple adjacent whitespaces in your input and field_delim_collapse=no. If your input uses whitespace and there is more than one whitespace between adjacent fields, make sure field_delim_collapse=yes. Please see sample/parse-example-4.txt");
}
if($seen->{col_name}{$name}++) {
report_error("Duplicate column name ($name) found. Please makes sure that all column names in the header are different.")
}
@{$table->{col}{list}[$col_idx]}{qw(name idx j)} = ($name,$col_idx,$i);
$segment_order->{$name} = $table->{col}{list}[$col_idx]{order};
$label_size->{$name} = $table->{col}{list}[$col_idx]{size};
printdebug("found column name",$col_idx,$name);
}
report_error("could not parse column header") unless $table->{col}{list};
next LINE;
}
################################################################
# parse each data row of table
my $row_idx = $table->{row}{list} ? @{$table->{row}{list}} : 0;
last LINE if $row_idx >= $CONF{max_row_num};
my $col_idx = 0;
my $row_data;
# process the row order, if this column exists
if($CONF{row_order_col}) {
my $order = $tok[ $col_idx++ ];
if($CONF{use_row_order_col}) {
report_error("Row at order value ($order) already exists.") if $seen->{row_order}{$order}++;
report_error("Row order value ($order) is not numeric.") unless $order =~ /^\d+$/;
printdebug("found row order",$row_idx,$order);
$row_data->{order} = $order;
}
}
# process row size, if this column exists
if($CONF{row_size_col}) {
my $size = $tok[ $col_idx++ ];
if($CONF{use_row_size_col}) {
report_error("Row size value [$row_data->{size}] is not numeric.") unless $size && $size =~ /^\d*\.?\d*$/;
printdebug("found row size",$row_idx,$size);
$row_data->{size} = $size;
}
}
# process row color, if this column exists
if($CONF{row_color_col}) {
my $color = $tok[ $col_idx++ ];
printdebug("found color",$color);
if($CONF{use_row_color_col}) {
report_error("Row color value ($color) does is not in r,g,b format.") if $color && $color !~ /^\d+,\d+,\d+$/;
printdebug("found row color",$row_idx,$color);
$row_data->{color} = $color;
}
}
my $row_name = parse_label($tok[ $col_idx++ ]);
report_error("Duplicate row name ($row_name) found. Please make sure that all row names are different.") if $seen->{row_name}{$row_name}++;
printdebug("found row name",$row_idx,$row_name);
@{$row_data}{qw(name idx)} = ($row_name,$row_idx);
push @{$table->{row}{list}}, $row_data;
$row_idx++;
$segment_order->{$row_name} ||= $row_data->{order};
$label_size->{$row_name} += $row_data->{size};
# now that we've parsed the row fields for order, size, color and name, read in the
# actual cell values
# this is done by iterating across the previously collected column data and advancing one field at
# a time for each column datum
for my $col_data ( @{$table->{col}{list}} ) {
# this is the original column position for this column
my $j = $col_data->{j};
# this is the cell value at this column position
my $cell_value = $tok[$j];
my $cell_value_clean = clean_value($cell_value,$row_data->{name},$col_data->{name});
printdebug("cleaned value",$cell_value,$cell_value_clean);
# if the cell value is blank or 0, it will be populated with the missing_cell_value
# otherwise, apply the data_mult multiplier, if it is defined
if($cell_value_clean ne "") {
if($cell_value_clean eq $CONF{missing_cell_value}) {
# data is missing - keep cleaned value as is
} elsif ($cell_value_clean =~ /^[+-]?\d*\.?\d*$/) {
$cell_value_clean *= $CONF{data_mult} if $CONF{data_mult};
report_error("All cell values must be non-negative, but you have ($row_data->{name},$col_data->{name})=$cell_value_clean") if $cell_value_clean < 0;
} else {
report_error("Cell value ($cell_value) for ($row_data->{name},$col_data->{name}) is not numeric.");
}
}
my $cell_data = { col => $col_data,
row => $row_data,
raw_value => $cell_value,
missing => $cell_value_clean eq $CONF{missing_cell_value},
value => $cell_value_clean };
# handle the case for intra-segment cells (i.e. cells for row/column of the same name)
if($col_data->{name} eq $row_data->{name}) {
if(! $CONF{intra_cell_handling} || $CONF{intra_cell_handling} eq "show") {
# treat this as any other cell
} elsif ($CONF{intra_cell_handling} eq "hide") {
printdebug("hiding intra-cell value",$row_data->{name},$col_data->{name},$cell_value);
$cell_data->{hide} = 1;
} elsif ($CONF{intra_cell_handling} eq "remove") {
printdebug("removing intra-cell value",$row_data->{name},$col_data->{name},$cell_value);
$cell_data->{value} = $CONF{missing_cell_value};
$cell_data->{missing} = 1;
}
}
printdebug("registered cell value",$row_data->{name},$col_data->{name},$cell_data->{value});
push @{$table->{cell_list}}, $cell_data;
$table->{cell}{$row_data->{name}}{$col_data->{name}} = $cell_data;
}
}
#printdumper($table->{col});exit;
for my $row (@{$table->{row}{list}}) {
$table->{row}{label}{$row->{name}} = $row;
}
for my $col (@{$table->{col}{list}}) {
$table->{col}{label}{$col->{name}} = $col;
}
#print Dumper($table);
if($CONF{show_parsed}) {
report_parse_table($table);
exit;
}
################################################################
#
# Compile cells statistics based on cleaned (but unfiltered) values
my $stat;
$stat->{cell}{raw} = Statistics::Descriptive::Full->new();
$stat->{cell}{raw}->add_data(map { $_->{value} } grep(! $_->{missing}, @{$table->{cell_list}}));
#printdebug("raw data",$stat->{cell}{raw}->get_data);
printdebug("raw cell stats","min",$stat->{cell}{raw}->min);
printdebug("raw cell stats","average",$stat->{cell}{raw}->mean);
printdebug("raw cell stats","median",$stat->{cell}{raw}->median);
printdebug("raw cell stats","max",$stat->{cell}{raw}->max);
################################################################
#
# Filter the cell values based on min/max value and percentile filters.
# If a cell value does not pass these filters, then it can be either
# hidden or removed. If it is hidden, it won't be shown but the col/row size
# will not be affected. If it is removed, then the result is the same as if
# the cell value was missing in the input file.
#
for my $cell (grep(! $_->{missing}, @{$table->{cell_list}})) {
my $reject;
$reject = 1 if defined $CONF{cell_min_value} && $cell->{value} < $CONF{cell_min_value};
$reject = 1 if defined $CONF{cell_max_value} && $cell->{value} > $CONF{cell_max_value};
$reject = 1 if defined $CONF{cell_min_percentile} && $cell->{value} < $stat->{cell}{raw}->percentile($CONF{cell_min_percentile});
$reject = 1 if defined $CONF{cell_max_percentile} && $cell->{value} > $stat->{cell}{raw}->percentile($CONF{cell_max_percentile});
if($reject && ($cell->{col}{idx} < 15 || $cell->{col}{idx} > 113) ) {
#printdumper($cell);
printdebug("filtering out cell ($cell->{row}{name},$cell->{col}{name})=$cell->{value}",$cell->{col}{idx});
if(! defined $CONF{cutoff_cell_handling} || $CONF{cutoff_cell_handling} eq "hide") {
$cell->{hide} = 1;
} elsif ($CONF{cutoff_cell_handling} eq "remove") {
$cell->{value} = $CONF{missing_cell_value};
$cell->{missing} = 1;
} else {
report_error("The cutoff_cell_handling value ($CONF{cutoff_cell_handling}) does not correspond to a valid choice (hide,remove)");
}
}
}
################################################################
#
# Compile cells statistics based on filtered values - these stats
# will be the same if no cell filters were used.
#
$stat->{cell}{filtered} = Statistics::Descriptive::Full->new();
$stat->{cell}{filtered}->add_data(map { $_->{value} } grep(! $_->{hide} && ! $_->{missing}, @{$table->{cell_list}}));
printdebug("filtered cell stats","min",$stat->{cell}{filtered}->min);
printdebug("filtered cell stats","average",$stat->{cell}{filtered}->mean);
printdebug("filtered cell stats","median",$stat->{cell}{filtered}->median);
printdebug("filtered cell stats","max",$stat->{cell}{filtered}->max);
################################################################
#
# scale cell values
#
for my $cell_data (grep(! $_->{missing}, @{$table->{cell_list}})) {
my $value_scaled = scale_value($cell_data->{value},$stat->{cell}{raw}->max);
$cell_data->{scaled_value} = $value_scaled;
printinfo("scale",$CONF{scaling_type},$cell_data->{value},$value_scaled);
}
my $value_type = $CONF{use_scaling} ? "scaled_value" : "value";
################################################################
#
# Compile cells statistics based on scaled values - these stats
# will be the same if no scaling was applied.
$stat->{cell}{scaled} = Statistics::Descriptive::Full->new();
$stat->{cell}{scaled}->add_data(map { $_->{$value_type} } grep(! $_->{hide} && ! $_->{missing}, @{$table->{cell_list}}));
printdebug("scaled cell stats","min",$stat->{cell}{scaled}->min);
printdebug("scaled cell stats","average",$stat->{cell}{scaled}->mean);
printdebug("scaled cell stats","median",$stat->{cell}{scaled}->median);
printdebug("scaled cell stats","max",$stat->{cell}{scaled}->max);
################################################################
#
# Create colors that encode cell value by percentile
#
# The colors will be determined using the raw, filtered or scaled values, depending
# on the value of the percentile_data_domain parameter (raw, filtered, or scaled).
#
for my $i (0 .. 100/$CONF{percentile_sampling}) {
my $percentile = $i * $CONF{percentile_sampling};
report_error("The value for percentile_data_domain must be either 'raw', 'filtered' or 'scaled'. This parameter controls whether original or filtered cell values are used to generate the color palette based on percentile values.") unless grep($CONF{percentile_data_domain} eq $_, (qw(raw filtered scaled)));
my $value = $stat->{cell}{ $CONF{percentile_data_domain} }->percentile($percentile);
my $h = map_value($percentile,0,100,$CONF{colors}{h0},$CONF{colors}{h1});
my $s = map_value($percentile,0,100,$CONF{colors}{s0},$CONF{colors}{s1});
my $v = map_value($percentile,0,100,$CONF{colors}{v0},$CONF{colors}{v1});
#my $c = Graphics::ColorObject->new_HSV([ int($h),$s,$v ]);
my @rgb = rgb_to_rgb255(hsv_to_rgb(int($h),$s,$v));
printinfo("hsvpercentile",$percentile,int($h),$s,$v);
printinfo("colorpercentile",sprintf("percentile%05d = %d,%d,%d",$percentile,@rgb));
}
################################################################
#
# report row/col/cell data
#
my $ribbon_variable_check;
for my $type (qw(row col)) {
for my $data (@{$table->{$type}{list}}) {
if($CONF{ribbon_variable}) {
if($type eq "row") {
if(exists $ribbon_variable_check->{$data->{name}}) {
die "Row with name $data->{name} seen twice.";
} else {
$ribbon_variable_check->{$data->{name}}++;
}
} else {
if(! exists $ribbon_variable_check->{$data->{name}}) {
die "Your data has a column with name $data->{name} but no row with this name. This row must exist when -ribbon_variable is used. For this data set, you should not be using ratio layout.";
} else {
$ribbon_variable_check->{$data->{name}}--;
}
}
}
printinfo("table report",$type,
join(" ",@{$data}{qw(idx name order)}));
}
}
if($CONF{ribbon_variable}) {
if(my @names = grep($_, values %$ribbon_variable_check)) {
die "You selected ratio layout with -ribbon_variable but not all rows/columns have the same name. For this data set, you should not be using ratio layout. Unmatched names were ".join(" ",@names);
}
}
for my $cell (@{$table->{cell_list}}) {
printinfo("table report cell",
join(" ",
$cell->{row}{idx},
$cell->{row}{name},
$cell->{col}{idx},
$cell->{col}{name},
"raw",
$cell->{raw_value},
"cleaned/remapped",
$cell->{value} . ($value_type eq "value" ? "*" : ""),
"scaled",
$cell->{scaled_value} . ($value_type eq "scaled_value" ? "*" : ""),
"missing?",
$cell->{missing} || 0,
"hidden?",
$cell->{hide} || 0));
}
#
################################################################
################################################################
#
# calculate statistics for row/col/cells based on scaled or cleaned/remapped values
#
my @types = qw(row col);
for my $cell (grep(! $_->{missing}, @{$table->{cell_list}})) {
for my $type (qw(row col)) {
my $name = $cell->{$type}{name};
my $row = $cell->{row}{name};
my $col = $cell->{col}{name};
$stat->{$type}{$name} ||= Statistics::Descriptive::Full->new();
$stat->{label}{$name} ||= Statistics::Descriptive::Full->new();
$stat->{cells} ||= Statistics::Descriptive::Full->new();
my $skip;
if($CONF{ribbon_variable} &&
exists $table->{cell}{$row}{$col} && exists $table->{cell}{$col}{$row}) {
if($type eq "row") {
$stat->{label}{$name}->add_data( $cell->{$value_type} );
$stat->{$type}{$name}->add_data( $cell->{$value_type} );
} elsif ($type eq "col") {
# TODO is this working???
if($CONF{ribbon_variable_intra_collapse} && $row eq $col) {
# do not add to the size of the segment if we're collapsing
# intrasegment ribbons
} else {
$stat->{$type}{$name}->add_data( $table->{cell}{$row}{$col}{$value_type});
}
#$seen->{labelstat}{$row}{$col}++;
#$seen->{labelstat}{$col}{$row}++;
} else {
#
}
} else {
$stat->{label}{$name}->add_data($cell->{$value_type});
$stat->{$type}{$name}->add_data($cell->{$value_type});
}
$stat->{cells}->add_data( $cell->{$value_type} );
}
}
for my $stattype (qw(row col label)) {
for my $name (sort keys %{$stat->{$stattype}}) {
printinfo("row/col/label stat",$stattype,$name,
"sum",
$stat->{$stattype}{$name}->sum,
"n",
$stat->{$stattype}{$name}->count,
"min",
$stat->{$stattype}{$name}->min,
"max",
$stat->{$stattype}{$name}->max,
"avg",
$stat->{$stattype}{$name}->mean);
}
}
# No longer interested in the cell associated with missing values - remove these
# from the cell list.
@{$table->{cell_list}} = grep(! $_->{missing}, @{$table->{cell_list}});
# Collect a list of all unique labels
my @unique_labels = unique ( map { $_->{row}{name}, $_->{col}{name}} @{$table->{cell_list}} );
# Apply normalization
my $normalization;
if($CONF{use_segment_normalization}) {
for my $label (@unique_labels) {
my $norm;
if($CONF{segment_normalization_function} eq "total") {
$norm = sum( map {$_->{$value_type}} grep($_->{row}{name} eq $label || $_->{col}{name} eq $label, @{$table->{cell_list}}));
} elsif($CONF{segment_normalization_function} eq "average") {
$norm = average( map {$_->{$value_type}} grep($_->{row}{name} eq $label || $_->{col}{name} eq $label, @{$table->{cell_list}}));
} elsif ($CONF{segment_normalization_function} eq "row_total") {
$norm = sum( map {$_->{$value_type}} grep($_->{row}{name} eq $label, @{$table->{cell_list}}));
} elsif ($CONF{segment_normalization_function} eq "row_average") {
$norm = average( map {$_->{$value_type}} grep($_->{row}{name} eq $label, @{$table->{cell_list}}));
} elsif ($CONF{segment_normalization_function} eq "col_total") {
$norm = sum( map {$_->{$value_type}} grep($_->{col}{name} eq $label, @{$table->{cell_list}}));
} elsif ($CONF{segment_normalization_function} eq "col_average") {
$norm = average( map {$_->{$value_type}} grep($_->{col}{name} eq $label, @{$table->{cell_list}}));
} elsif ($CONF{segment_normalization_function} eq "row_size") {
if(exists $table->{row}{label}{$label}) {
if(! defined $table->{row}{label}{$label}{size}) {
report_error("You asked for normalization based on optional row size column, but no such column was detected.");
} else {
$norm = $table->{row}{label}{$label}{size};
}
}
} elsif ($CONF{segment_normalization_function} eq "col_size") {
if(exists $table->{col}{label}{$label}) {
if(! defined $table->{col}{label}{$label}{size}) {
report_error("You asked for normalization based on optional col size row, but no such row was detected.");
} else {
$norm = $table->{col}{label}{$label}{size};
}
}
} elsif ($CONF{segment_normalization_function} eq "total_size") {
if(exists $table->{col}{label}{$label}) {
if(! defined $table->{col}{label}{$label}{size}) {
report_error("You asked for normalization based on optional size values, but no column that stored row sizes was detected.");
} else {
$norm += $table->{col}{label}{$label}{size};
}
}
if(exists $table->{row}{label}{$label}) {
if(! defined $table->{row}{label}{$label}{size}) {
report_error("You asked for normalization based on optional size values, but no row that stored column sizes was detected.");
} else {
$norm += $table->{row}{label}{$label}{size};
}
}
} elsif ($CONF{segment_normalization_function} =~ /(\d+)/) {
$norm = $1;
} else {
report_error("You asked for segment normalization but the segment_normalization_function field value ($CONF{segment_normalization_function}) is one one of the valid choices.");
}
printdebug("segment normalization function",$CONF{segment_normalization_function},"segment",$label,"normvalue",$norm);
$normalization->{$label} = round($norm);
}
}
# Statistics for normalized cell values
for my $cell (grep(! $_->{missing}, @{$table->{cell_list}})) {
my $row = $cell->{row}{name};
$stat->{cells_norm} ||= Statistics::Descriptive::Full->new();
my $vn = normvalue($cell->{$value_type},$row);
printdebug("cellnorm",$row,$cell->{col}{name},$cell->{$value_type},$vn);
$stat->{cells_norm}->add_data($vn);
}
# compute percentiles
my $ptmp;
for my $value (sort {$a <=> $b} $stat->{cells_norm}->get_data) {
if($CONF{percentile_method} eq "value") {
if($CONF{percentile_unique_only}) {
$ptmp->{$value} = remap_for_percentile($value);
} else {
$ptmp->{$value}+= remap_for_percentile($value);
}
} else {
if($CONF{percentile_unique_only}) {
$ptmp->{$value} = 1;
} else {
$ptmp->{$value} ++;
}
}
}
my $ctmp;
for my $value (sort {$a <=> $b} keys %$ptmp) {
my $p;
if($CONF{percentile_method} eq "value") {
$ctmp += $ptmp->{$value};
my $den = sum(values %$ptmp);
$p = $ctmp / $den;
} else {
my $den = sum(values %$ptmp);
$p = average ($ctmp,$ctmp+$ptmp->{$value}) / $den;
$ctmp += $ptmp->{$value};
}
$stat->{cell_percentile}{$value} = 100*$p;
printdebug("pstat",$value,remap_for_percentile($value),$p);
}
################################################################
#
# compile a list of unique row/column names and determine the
# order in which corresponding segments will be drawn in the image
#
# each label will correspond to an ideogram (segment) in the figure
#
printdebug("unique labels",@unique_labels);
my @labels = order_labels($CONF{segment_order},@unique_labels);
# register label order
my $label_order;
for my $i ( 0 .. @labels-1) {
$label_order->{ $labels[$i] } = $i;
}
################################################################
# determine the mapping between row/col names and the names
# of the ideograms in Circos files. If the segment_prefix is set, then
# the ideograms are named segment_prefix + IDX where IDX=0,1,2,3... in the
# order of segments in the image. If the chr-Prefix is not set, then
# the ideograms are named after the label.
my $label2seg;
for my $label_idx (0..@labels-1) {
if($CONF{segment_prefix}) {
$label2seg->{ $labels[$label_idx] } = sprintf("%s%d",$CONF{segment_prefix},$label_idx);
} else {
$label2seg->{ $labels[$label_idx] } = $labels[$label_idx];
}
printinfo("label2seg",$label_idx,$labels[$label_idx],$label2seg->{$labels[$label_idx]});
}
################################################################
# compile a list of colors associated with a row/col name
for my $type (qw(row col)) {
for my $elem (@{$table->{$type}{list}}) {
if($label_color->{$elem->{name}} && $elem->{color} && $label_color->{$elem->{name}} ne $elem->{color}) {
#report_error("color for label [$elem->{name}] is already defined to be [$label_color->{$elem->{name}}] but saw new definition [$elem->{color}]");
} elsif ($elem->{color}) {
$label_color->{$elem->{name}} = $elem->{color};
printdebug("assigned color from data file",$elem->{name},$elem->{color});
}
}
}
# for row/col names that do not have a specified color, generate
# a color by interpolating
my @label_for_color;
if($CONF{segment_color_order}) {
@label_for_color = order_labels($CONF{segment_color_order},grep(! defined $label_color->{$_}, @labels));
} else {
@label_for_color = grep(! defined $label_color->{$_}, @labels);
}
my $cumul_size = 0;
for my $i (0..@label_for_color-1) {
my $label = $label_for_color[$i];
my ($h,$s,$v);
if($CONF{segment_colors}{interpolate_type} =~ /size/) {
my $total_label_size = sum ( map { normvalue($stat->{label}{$_}->sum,$_) } @label_for_color );
$total_label_size -= normvalue($stat->{label}{ $label_for_color[0] }->sum, $label_for_color[0])/2;
$total_label_size -= normvalue($stat->{label}{ $label_for_color[-1] }->sum, $label_for_color[-1])/2;
if($cumul_size) {
$cumul_size += normvalue($stat->{label}{$label}->sum,$label)/2;
}
my $x = $cumul_size / $total_label_size;
$h = map_value($x,0,1,$CONF{segment_colors}{h0},$CONF{segment_colors}{h1});
$s = map_value($x,0,1,$CONF{segment_colors}{s0},$CONF{segment_colors}{s1});
$v = map_value($x,0,1,$CONF{segment_colors}{v0},$CONF{segment_colors}{v1});
$cumul_size += normvalue($stat->{label}{$label}->sum,$label)/2;
} else {
$h = map_value($i,0,@label_for_color-1,$CONF{segment_colors}{h0},$CONF{segment_colors}{h1});
$s = map_value($i,0,@label_for_color-1,$CONF{segment_colors}{s0},$CONF{segment_colors}{s1});
$v = map_value($i,0,@label_for_color-1,$CONF{segment_colors}{v0},$CONF{segment_colors}{v1});
}
printdebug("interpolated hsv for segment",$h,$s,$v);
#my $c = Graphics::ColorObject->new_HSV([ $h,$s,$v ]);
my @rgb = rgb_to_rgb255(hsv_to_rgb(int($h),$s,$v));
$label_color->{$label_for_color[$i]} = join(",",@rgb);
printdebug("assigned color from interpolated range",$label_for_color[$i],join(",",@rgb));
}
################################################################
#
# create new color definitions
for my $label (keys %$label_color) {
if($label2seg->{$label}) {
printinfo(sprintf("colordef %s%s = %s",lc $CONF{color_prefix},lc $label2seg->{$label},$label_color->{$label}));
}
}
################################################################
#
# create highlights based on row and column values
#
for my $type (qw(row col)) {
my $atype = $type eq "row" ? "col" : "row";
# create a hash of all cell values that lead from $type/label combination (e.g. row/A)
# to $atype/label combination (e.g. col/B)
my %values;
for my $label (keys %{$table->{$type}{label}}) {
for my $cell (@{$table->{cell_list}}) {
if($cell->{$type}{name} eq $label) {
$values{$label}{$cell->{$atype}{name}} = $cell->{$value_type};
}
}
}
for my $label ( keys %values ) {
my $cumul_pos = 0;
my $total_sum = sum( values %{$values{$label}} );
for my $alabel ( sort {$values{$label}{$b} <=> $values{$label}{$a}} keys %{$values{$label}} ) {
my $norm_factor = $CONF{normalize_contribution} ? fetch_segment_size($label) / $total_sum : 1;
printinfo(sprintf("%s %s %s%s %.0f %.0f fill_color=%s%s%s",
"highlight",
$type,
$CONF{segment_prefix},
$label2seg->{ $label },
$norm_factor * $cumul_pos,
$norm_factor * ($cumul_pos + $values{$label}{$alabel}),
lc $CONF{color_prefix},
lc $CONF{segment_prefix},
lc $label2seg->{$alabel}));
$cumul_pos += $values{$label}{$alabel};
}
}
}
# create combined row/col highlights
for my $label (@labels) {
my $cumul_pos = 0;
my %values;
for my $cell (@{$table->{cell_list}}) {
if($cell->{row}{name} eq $label) {
$values{$cell->{col}{name}} += $cell->{$value_type};
}
if ($cell->{col}{name} eq $label) {
$values{$cell->{row}{name}} += $cell->{$value_type};
}
}
die "label stat for label [$label] not defined" if ! defined $stat->{label}{$label};
my $sum = $stat->{label}{$label}->sum;
my $norm_factor = $CONF{normalize_contribution} ? fetch_segment_size($label) / $sum : 1;
for my $alabel ( sort {$values{$b} <=> $values{$a}} keys %values ) {
printinfo(sprintf("%s %s %s%s %.0f %.0f fill_color=%s%s%s",
"highlight",
"all",
$CONF{segment_prefix},
$label2seg->{ $label },
$norm_factor * $cumul_pos,
$norm_factor * ($cumul_pos + $values{$alabel}),
lc $CONF{color_prefix},
lc $CONF{segment_prefix},
lc $label2seg->{$alabel}));
$cumul_pos += $values{$alabel};
}
}
################################################################
#
# generate segment sizes and coordinates for each row/col label
#
# this information is passed to Circos using the karyotype parameter
for my $label ( @labels ) {
my $size = normvalue($stat->{label}{$label}->sum,$label);
# if this row/col has a specific size, use it
if(exists $table->{row}{label}{$label} && $table->{row}{label}{$label}{size}) {
$size = $table->{row}{label}{$label}{size};
} elsif (exists $table->{col}{label}{$label} && $table->{col}{label}{$label}{size}) {
$size = $table->{col}{label}{$label}{size};
}
$size = fetch_segment_size($label);
next unless $size;
printinfo("karyotype",
sprintf("chr - %s %s 0 %.0f %s%s",
$label2seg->{$label},
$label2seg->{$label},
$size,
lc $CONF{color_prefix},
lc $label2seg->{$label}));
(my $text_label = $label) =~ s/ /:/g;
$text_label = uc $text_label if $CONF{segment_label_uppercase};
if($CONF{segment_label_size_range}) {
my ($min_size,$max_size) = split(/,/,$CONF{segment_label_size_range});
my $min_label = min( map {normvalue($stat->{label}{$_}->sum,$_)} @labels);
my $max_label = max( map {normvalue($stat->{label}{$_}->sum,$_)} @labels);
my $label_size;
if($max_label - $min_label) {
my $k = $CONF{segment_label_size_progression} || 1;
$label_size = $min_size + ($max_size-$min_size)*((normvalue($stat->{label}{$label}->sum,$label) - $min_label)/($max_label-$min_label))**(1/$k);
} else {
$label_size = $max_size;
}
printinfo("segmentlabel",
sprintf("%s %s %s %s label_size=%dp",
$label2seg->{$label},
#$size/2,
#$size/2,
0,
$size,
$text_label,$label_size));
} else {
printinfo("segmentlabel",
sprintf("%s %s %s %s",
$label2seg->{$label},
0,
$size,
$text_label));
}
}
################################################################
# if segment normalization is done by Circos (i.e. values are not
# remapped, but segments are scaled visually), then report a line
# that gives the segment scaling
# report scaling line
my @chromosomes_scale;
for my $label (@labels) {
my $scale = 1;
# include the third force parameter to force normalization
if($CONF{use_segment_normalization} && $CONF{segment_normalization_scheme} eq "visual") {
my $orig_size = $stat->{label}{$label}->sum;
my $norm_size = normvalue($stat->{label}{$label}->sum,$label,1);
$scale = $norm_size / $orig_size;
printinfo("scaling",
sprintf("%s pre %d norm %d scale %.3f",
$label2seg->{$label},
$orig_size,
$norm_size,
$scale));
}
push @chromosomes_scale, sprintf("%s:%.4f",$label2seg->{$label},$scale);
}
printinfo("chromosomes_scale =",join(",",@chromosomes_scale));
################################################################
#
# assign segment position for each cell
#
my $pos;
my $assigned;
my @loop1 = $CONF{ribbon_variable} ? split(/,/,$CONF{placement_order}) : @labels;
my @loop2 = $CONF{ribbon_variable} ? @labels : split(/,/,$CONF{placement_order});
# ribbon-variable = yes
# loop1 = row | col
# loop2 = label
# ribbon-variable = no
# loop1 = label
# loop2 = row | col
for my $loop1 (@loop1) {
for my $loop2 (@loop2) {
printinfo("loop",$loop1,$loop2);
my ($label,$type) = $CONF{ribbon_variable} ? ($loop2,$loop1) : ($loop1,$loop2);
# $atype is the corresponding table object for the given $type
# $type=row $atype=col
# $type=col $atype=row
my $atype = $type eq "row" ? "col" : "row";
my @cells = grep(! $_->{missing} && $_->{$type}{name} eq $label, @{$table->{cell_list}});
my @cells_sorted;
if($CONF{ribbon_bundle_order} =~ /size_desc/) {
@cells_sorted = sort {$b->{$value_type} <=> $a->{$value_type}} @cells;
} elsif ($CONF{ribbon_bundle_order} =~ /size_asc/) {
@cells_sorted = sort {$a->{$value_type} <=> $b->{$value_type}} @cells;
} elsif ($CONF{ribbon_bundle_order} =~ /ascii/) {
@cells_sorted = sort {$a->{$atype}{name} cmp $b->{$atype}{name}} @cells;
} elsif ($CONF{ribbon_bundle_order} =~ /native/) {
# TODO - make sure this option is working correctly
@cells_sorted = sort {$label_order->{$b->{$atype}{name}} <=> $label_order->{$a->{$atype}{name}}} @cells;
} else {
die "ribbon_bundle_order value [$CONF{ribbon_bundle_order}] not supported";
}
for my $cell (@cells_sorted) {
my $row = $cell->{row}{name};
my $col = $cell->{col}{name};
my $value_norm = normvalue($cell->{$value_type},$label);
printdebug("processing cell ($row,$col)=",$value_norm,"end $type");
if( ($type eq "row" && $CONF{reverse_rows})
||
($type eq "col" && $CONF{reverse_columns})
) {
$pos->{$label} ||= normvalue($stat->{label}{$label}->sum,$label);
$cell->{$type."_end"} = $pos->{$label} || 0;
$cell->{$type."_start"} = $pos->{$label} - $value_norm;
$pos->{$label} = $cell->{$type."_start"};
$assigned->{$row}{$col}++;
} else {
if($CONF{ribbon_variable}) {
if($type eq "row") {
if($assigned->{$col}{$row}) {
my $tcell = $table->{cell}{$col}{$row};
printdebug("transposeinfo",$col,$row);
if($col eq $row) {
$tcell->{"col_start"} = $tcell->{"row_end"};
$tcell->{"col_end"} = $tcell->{"row_end"};
} else {
#$value_norm = normvalue($tcell->{$value_type},$label);
$tcell->{$atype."_start"} = $pos->{$label} || 0;
$tcell->{$atype."_end"} = $tcell->{$atype."_start"} + $value_norm;
$pos->{$label} = $tcell->{$atype."_end"};
$assigned->{$col}{$row}++;
#printdumper($tcell);
}
} else {
$cell->{$type."_start"} = $pos->{$label} || 0;
$cell->{$type."_end"} = $cell->{$type."_start"} + $value_norm;
$pos->{$label} = $cell->{$type."_end"};
$assigned->{$row}{$col}++;
}
} else {
if($row eq $col) {
$cell->{"col_start"} = $cell->{"row_end"};
$cell->{"col_end"} = $cell->{"row_end"};
}
}
} else {
$cell->{$type."_start"} = $pos->{$label} || 0;
$cell->{$type."_end"} = $cell->{$type."_start"} + $value_norm;
$pos->{$label} = $cell->{$type."_end"};
$assigned->{$row}{$col}++;
}
}
printdebug("cellinfo",$type,$row,$col,
"row",defor($cell->{row_start},"-"),defor($cell->{row_end},"-"),
"col",defor($cell->{col_start},"-"),defor($cell->{col_end},"-"));
printdebug("posinfo",
map { sprintf("%s %d",$_,$pos->{$_}) } @labels);
}
}
}
my @cell_values = map { $_->{$value_type} } grep(! $_->{missing}, @{$table->{cell_list}});
my $linkid=0;
for my $cell ( grep(! $_->{missing} && ! $_->{hide}, @{$table->{cell_list}}) ) {
next unless defined $cell->{"row_end"} && defined $cell->{"col_end"};
next unless $cell->{$value_type};
my $row = $cell->{row}{name};
my $col = $cell->{col}{name};
printdebug("drawinginfo",$cell->{row}{name},$cell->{col}{name});
my $z;
if($CONF{ribbon_layer_order} eq "size_desc") {
$z = map_value($cell->{$value_type},scalar(min(@cell_values)),scalar(max(@cell_values)),100,0);
} else {
$z = map_value($cell->{$value_type},scalar(min(@cell_values)),scalar(max(@cell_values)),0,100);
}
my %param = (z=>round($z),
%{$CONF{linkparam}},
);
my $value_for_percentile;
if($CONF{ribbon_variable}) {
my $label;
my $vrc = normvalue($table->{cell}{$row}{$col}{$value_type},$row);
my $vcr = normvalue($table->{cell}{$col}{$row}{$value_type},$col);
if($CONF{linkcolor}{color_source} =~ /large/) {
$label = $vrc >= $vcr ? $row : $col;
$value_for_percentile = $vrc;
} elsif ($CONF{linkcolor}{color_source} =~ /small/) {
$label = $vrc <= $vcr ? $row : $col;
$value_for_percentile = $vrc;
} elsif ($CONF{linkcolor}{color_source} =~ /row/) {
$label = $row;
$value_for_percentile = $vrc;
} elsif ($CONF{linkcolor}{color_source} =~ /col/) {
$label = $col;
$value_for_percentile = $vcr;
} else {
die "Cannot understand color_source parameter [$CONF{linkcolor}{color_source}] when ribbon_variable=yes";
}
if($CONF{linkcolor}{percentile_source} =~ /large/) {
$value_for_percentile = $vrc >= $vcr ? $vrc : $vcr;
} elsif ($CONF{linkcolor}{percentile_source} =~ /small/) {
$value_for_percentile = $vrc <= $vcr ? $vrc : $vcr;
} elsif ($CONF{linkcolor}{percentile_source} eq "row") {
$value_for_percentile = $vrc;
} elsif ($CONF{linkcolor}{percentile_source} eq "col") {
$value_for_percentile = $vcr;
} else {
die "Cannot understand percentile_source parameter [$CONF{linkcolor}{percentile_source}] when ribbon_variable=yes";
}
$param{color} = lc sprintf("%s%s",$CONF{color_prefix},$label2seg->{$label});
} else {
$value_for_percentile = normvalue($table->{cell}{$row}{$col}{$value_type},$row);
if($CONF{linkcolor}{color_source}) {
$param{color} = lc sprintf("%s%s",$CONF{color_prefix},$label2seg->{$cell->{$CONF{linkcolor}{color_source}}{name}}),
}
}
$param{color} = change_transparency($param{color},$CONF{linkcolor}{color_transparency}) if exists $CONF{linkcolor}{color_transparency};
if($CONF{linkcolor}{color_remap}) {
if($CONF{linkcolor}{percentile} && ! $CONF{linkcolor}{color_autoremap}) {
for my $p (sort {$a <=> $b} keys %{$CONF{linkcolor}{percentile}}) {
my $v = $value_for_percentile;
if($stat->{cell_percentile}{$v} < $p || $p == 100) {
printdebug("pcremap",$row,$col,$v,$stat->{cell_percentile}{$v},$p);
for my $param (sort keys %{$CONF{linkcolor}{percentile}{$p}}) {
my $value = $CONF{linkcolor}{percentile}{$p}{$param};
if($param =~ /transparency/) {
if($value == 0) {
$param{color} =~ s/_a\d+//;
} elsif ($param{color} =~ /_a\d+/) {
$param{color} =~ s/_a\d+/_a$value/;
} else {
$param{color} .= sprintf("_a%d",$value);
}
} else {
$param{$param} = $value;
}
}
last;
}
}
} elsif ($CONF{linkcolor}{value} && ! $CONF{linkcolor}{color_autoremap}) {
for my $value (sort {$a <=> $b} keys %{$CONF{linkcolor}{value}}) {
if ($cell->{$value_type} <= $value) {
for my $param (sort keys %{$CONF{linkcolor}{value}{$value}}) {
my $value = $CONF{linkcolor}{value}{$value}{$param};
if($param =~ /transparency/) {
if($value == 0) {
$param{color} =~ s/_a\d+//;
} elsif ($param{color} =~ /_a\d+/) {
$param{color} =~ s/_a\d+/_a$value/;
} else {
$param{color} .= sprintf("_a%d",$value);
}
} else {
$param{$param} = $value;
}
}
last;
}
}
} else {
my $v = $value_for_percentile;
my $cell_percentile = $stat->{cell_percentile}{$v};
$param{color} = sprintf("percentile%05d",$CONF{percentile_sampling}*int($cell_percentile/$CONF{percentile_sampling}));
printinfo("percentile remap",$v,$cell_percentile,$CONF{percentile_sampling}*int($cell_percentile/$CONF{percentile_sampling}),$param{color});
}
}
if($cell->{$value_type} < $stat->{cell}{raw}->percentile(25)) {
$param{color} = $CONF{cell_q1_color} if $CONF{cell_q1_color};
$param{stroke_thickness} = "0" if $CONF{cell_q1_nostroke};
$param{color} = change_transparency($param{color},$CONF{linkcolor}{color_transparency}) if exists $CONF{linkcolor}{color_transparency};
$param{color} = change_transparency($param{color},$CONF{cell_q1_transparency}) if exists $CONF{cell_q1_transparency};
} elsif ($cell->{$value_type} < $stat->{cell}{raw}->percentile(50)) {
$param{color} = $CONF{cell_q2_color} if $CONF{cell_q2_color};
$param{stroke_thickness} = "0" if $CONF{cell_q2_nostroke};
$param{color} = change_transparency($param{color},$CONF{linkcolor}{color_transparency}) if exists $CONF{linkcolor}{color_transparency};
$param{color} = change_transparency($param{color},$CONF{cell_q2_transparency}) if exists $CONF{cell_q2_transparency};
} elsif ($cell->{$value_type} < $stat->{cell}{raw}->percentile(75)) {
$param{color} = $CONF{cell_q3_color} if $CONF{cell_q3_color};
$param{stroke_thickness} = "0" if $CONF{cell_q3_nostroke};
$param{color} = change_transparency($param{color},$CONF{linkcolor}{color_transparency}) if exists $CONF{linkcolor}{color_transparency};
$param{color} = change_transparency($param{color},$CONF{cell_q3_transparency}) if exists $CONF{cell_q3_transparency};
} elsif ($cell->{$value_type} <= $stat->{cell}{raw}->percentile(100)) {
$param{color} = $CONF{cell_q4_color} if $CONF{cell_q4_color};
$param{stroke_thickness} = "0" if $CONF{cell_q4_nostroke};
$param{color} = change_transparency($param{color},$CONF{linkcolor}{color_transparency}) if exists $CONF{linkcolor}{color_transparency};
$param{color} = change_transparency($param{color},$CONF{cell_q4_transparency}) if exists $CONF{cell_q4_transparency};
}
printinfo("link",
sprintf("cell_%04d %s %.0f %.0f %s",
$linkid,
$label2seg->{ $cell->{row}{name} },
$cell->{row_start},
$cell->{row_end},
join(",", map {sprintf("%s=%s",$_,$param{$_})} keys %param)
));
printinfo("link",
sprintf("cell_%04d %s %.0f %.0f %s",
$linkid,
$label2seg->{ $cell->{col}{name} },
$cell->{col_start},
$cell->{col_end},
join(",", map {sprintf("%s=%s",$_,$param{$_})} keys %param)
));
printinfo("colribboncap",
sprintf("%s %.0f %.0f fill_color=%s%s",
$label2seg->{ $cell->{col}{name} },
$cell->{col_start},
$cell->{col_end},
lc $CONF{color_prefix},
lc $label2seg->{ $cell->{row}{name} }
));
printinfo("rowribboncap",
sprintf("%s %.0f %.0f fill_color=%s%s",
$label2seg->{ $cell->{row}{name} },
$cell->{row_start},
$cell->{row_end},
lc $CONF{color_prefix},
lc $label2seg->{ $cell->{col}{name} }
));
$linkid++;
}
report_error("could not parse any cell values to draw") if ! $linkid;
################################################################
################################################################
################################################################
sub hsv_to_rgb {
my ($h, $s, $v) = @_;
my @rgb;
$h = $h % 360 if $h < 0 || $h > 360;
# hue segment
$h /= 60;
my $i = POSIX::floor( $h );
my $f = $h - $i;
my $p = $v * ( 1 - $s );
my $q = $v * ( 1 - $s * $f );
my $t = $v * ( 1 - $s * ( 1 - $f ) );
if ($i == 0) {
@rgb = ($v,$t,$p);
} elsif ($i == 1) {
@rgb = ($q,$v,$p);
} elsif ($i == 2) {
@rgb = ($p,$v,$t);
} elsif ($i == 3) {
@rgb = ($p,$q,$v);
} elsif ($i == 4) {
@rgb = ($t,$p,$v);
} else {
@rgb = ($v,$p,$q);
}
return @rgb;
}
sub rgb_to_rgb255 {
my @rgb = @_;
# make sure values are in [0,1]
@rgb = map { put_between($_,0,1) } @rgb;
my @rgb255 = map { round( 255 * $_) } @rgb;
return @rgb255;
}
sub put_between {
my ($x,$min,$max) = @_;
return $min if $x < $min;
return $max if $x > $max;
return $x;
}
sub change_transparency {
my ($color,$t) = @_;
if($color =~ /(.+)_a\d+/) {
if($t) {
return sprintf("%s_a%d",$1,$t);
} else {
return $1;
}
} else {
if($t) {
return sprintf("%s_a%d",$color,$t);
} else {
return $color;
}
}
}
sub fetch_segment_size {
my $label = shift;
my $size = normvalue($stat->{label}{$label}->sum,$label);
# if this row/col has a specific size, use it
if(exists $table->{row}{label}{$label} && $table->{row}{label}{$label}{size}) {
$size = $table->{row}{label}{$label}{size};
} elsif (exists $table->{col}{label}{$label} && $table->{col}{label}{$label}{size}) {
$size = $table->{col}{label}{$label}{size};
}
return $size;
}
sub report_parse_table {
my $table = shift;
my @lines;
push @lines,["data",map {$_->{name}} @{$table->{col}{list}}];
for my $row (@{$table->{row}{list}}) {
push @lines, [$row->{name}];
for my $col (@{$table->{col}{list}}) {
push @{$lines[-1]},$table->{cell}{$row->{name}}{$col->{name}}{raw_value};
}
}
my @strings =
my $max_length = max (map {length($_)} (map {@$_} @lines) );
for my $line (@lines) {
printf("%${max_length}s "x@lines . "\n",@$line);
}
}
sub remap_for_percentile {
my $value = shift;
if($CONF{percentile_remap}) {
(my $cmd = $CONF{percentile_remap}) =~ s/X/$value/g;
$cmd = eval $cmd;
die "could not parse the percentile remap function $CONF{percentile_remap} with error [$@]" if $@;
return $cmd;
} else {
return $value;
}
}
sub defor {
my $value = shift;
my $or = shift;
return defined $value ? $value : $or;
}
sub order_labels {
my ($scheme,@unique_labels) = @_;
# first, add labels for which a label order exists
my @labels_preorder = sort {$segment_order->{$a} <=> $segment_order->{$b}} grep(defined $segment_order->{$_}, @unique_labels);
printdebug("label order preorder",@labels_preorder);
# now deal with remaining labels
my @labels_to_order = grep(! defined $segment_order->{$_}, @unique_labels);
my @labels_row = grep( $table->{row}{label}{$_}, @labels_to_order);
my @labels_col = grep( $table->{col}{label}{$_}, @labels_to_order);
my @labels_ordered;
printdebug("label order scheme",$scheme);
if($scheme =~ /file:(.*)/) {
my $file = $1;
open(F,$file) || report_error("Cannot open segment order file ($file).");
while(<F>) {
chomp;
my $segment = $_;
if(! grep($segment eq $_, @labels_to_order)) {
#report_error("You asked that segments be ordered using the file ($file) and the segment in the file ($segment) does not correspond to any row/col name that requires ordering.");
} else {
push @labels_ordered, $segment;
}
}
close(F);
} elsif ($scheme =~ /ascii/) {
if($scheme =~ /row_major/) {
@labels_ordered = unique ( ( sort @labels_row ), ( sort @labels_col ) );
} elsif ($scheme =~ /col_major/) {
@labels_ordered = unique ( ( sort @labels_col ), ( sort @labels_row ) );
} elsif ($scheme =~ /size_desc/) {
@labels_ordered = unique ( sort {$b cmp $a} (@labels_row,@labels_col) );
} else {
@labels_ordered = unique ( sort {$a cmp $b} (@labels_row,@labels_col) );
}
} elsif ($scheme =~ /size_asc/) {
if($scheme =~ /row_major/) {
@labels_ordered = unique ( ( sort {$stat->{label}{$a}->sum <=> $stat->{label}{$b}->sum} @labels_row ),
( sort {$stat->{label}{$a}->sum <=> $stat->{label}{$b}->sum} @labels_col ) );
} elsif ($scheme =~ /col_major/) {
@labels_ordered = unique ( ( sort {$stat->{label}{$a}->sum <=> $stat->{label}{$b}->sum} @labels_col ),
( sort {$stat->{label}{$a}->sum <=> $stat->{label}{$b}->sum} @labels_row ) );
} elsif ($scheme =~ /row_size/) {
@labels_ordered = unique ( sort {$stat->{row}{$a}->sum <=> $stat->{row}{$b}->sum} @labels_to_order );
} elsif ($scheme =~ /col_size/) {
@labels_ordered = unique ( sort {$stat->{col}{$a}->sum <=> $stat->{col}{$b}->sum} @labels_to_order);
} elsif ($scheme =~ /row_to_col_ratio/) {
@labels_ordered = unique ( sort { safe_divide($stat->{row}{$a}->sum,$stat->{col}{$a}->sum) <=> safe_divide($stat->{row}{$b}->sum,$stat->{col}{$b}->sum)} @labels_to_order );
} elsif ($scheme =~ /col_to_row_ratio/) {
@labels_ordered = unique ( sort { safe_divide($stat->{col}{$a}->sum,$stat->{row}{$a}->sum) <=> safe_divide($stat->{col}{$b}->sum,$stat->{row}{$b}->sum)} @labels_to_order );
} else {
@labels_ordered = unique ( ( sort {$stat->{label}{$a}->sum <=> $stat->{label}{$b}->sum} (@labels_row,@labels_col)) );
}
} elsif ($scheme =~ /size_desc/) {
if($scheme =~ /row_major/) {
@labels_ordered = unique ( ( sort {$stat->{label}{$b}->sum <=> $stat->{label}{$a}->sum} @labels_row ),
( sort {$stat->{label}{$b}->sum <=> $stat->{label}{$a}->sum} @labels_col ) );
} elsif ($scheme =~ /col_major/) {
@labels_ordered = unique ( ( sort {$stat->{label}{$b}->sum <=> $stat->{label}{$a}->sum} @labels_col ),
( sort {$stat->{label}{$b}->sum <=> $stat->{label}{$a}->sum} @labels_row ) );
} elsif ($scheme =~ /row_size/) {
@labels_ordered = unique ( sort {$stat->{row}{$b}->sum <=> $stat->{row}{$a}->sum} @labels_to_order );
} elsif ($scheme =~ /col_size/) {
@labels_ordered = unique ( sort {$stat->{col}{$b}->sum <=> $stat->{col}{$a}->sum} @labels_to_order );
} elsif ($scheme =~ /row_to_col_ratio/) {
@labels_ordered = unique ( sort { safe_divide($stat->{row}{$b}->sum/$stat->{col}{$b}->sum) <=> safe_divide($stat->{row}{$a}->sum/$stat->{col}{$a}->sum)} @labels_to_order );
} elsif ($scheme =~ /col_to_row_ratio/) {
@labels_ordered = unique ( sort { safe_divide($stat->{col}{$b}->sum,$stat->{row}{$b}->sum) <=> safe_divide($stat->{col}{$a}->sum,$stat->{row}{$a}->sum)} @labels_to_order );
} else {
@labels_ordered = unique ( ( sort {$stat->{label}{$b}->sum <=> $stat->{label}{$a}->sum} (@labels_row,@labels_col)) );
}
} elsif ($scheme =~ /row_major/) {
if($scheme =~ /size_desc/) {
@labels_ordered = unique ( sort {$b cmp $a } ( @labels_row, @labels_col ))
} else {
@labels_ordered = unique ( sort {$a cmp $b } ( @labels_row, @labels_col ))
}
} elsif ($scheme =~ /col_major/) {
if($scheme =~ /size_desc/) {
@labels_ordered = unique ( sort {$b cmp $a} (@labels_col, @labels_row ));
} else {
@labels_ordered = unique ( sort {$a cmp $b} (@labels_col, @labels_row ));
}
} else {
die "don't understand segment_order value [$scheme]";
}
my @labels = (@labels_preorder,@labels_ordered);
printinfo("labels ordered",@labels);
#for my $l (@labels) {
# printinfo("labelorder",$l,safe_divide($stat->{row}{$l}->sum,$stat->{col}{$l}->sum),$stat->{row}{$l}->sum,$stat->{col}{$l}->sum);
#}
return @labels;
}
# return a normalized value for a row/col label
#
# the normalization is previously determined based on the *_segment_normalization_* parameter
# values. the un-normalized denominator for the label is $stat->{label}{$label}->sum
sub normvalue {
my ($value,$label,$force) = @_;
if($normalization->{$label} && ($force || $CONF{segment_normalization_scheme} eq "value")) {
return $normalization->{$label} * safe_divide($value,$stat->{label}{$label}->sum);
} else {
return $value;
}
}
# linearly remap a $value which falls in range $vmin-$vmax onto
# the range given by $tmin-$tmax so that
#
# $vmin -> $tmin
# $vmax -> $tmax
sub map_value {
my ($value,$vmin,$vmax,$tmin,$tmax) = @_;
if($vmax != $vmin) {
return $tmin + ($tmax-$tmin)*($value-$vmin)/($vmax-$vmin);
} else {
return $tmin;
}
}
sub safe_divide {
my ($a,$b) = @_;
return $b ? $a/$b : 0;
}
sub limit_array_size {
my $max = shift;
my @array = @_;
if(@_ <= $max) {
return @_;
} else {
return splice(@_,0,$max);
}
}
sub scale_value {
my ($v,$vm) = @_;
my $vscaled;
return $v if ! $CONF{use_scaling};
if($CONF{scaling_type} eq "atten_small") {
my $k = (exp($CONF{scale_factor}*$v/$vm)-exp(0)) / (exp($CONF{scale_factor})-exp(0));
$vscaled = $k * $vm;
} else {
if($v && $vm) {
my $k = log($v*$CONF{scale_factor})/log($vm*$CONF{scale_factor});
$vscaled = $k * $vm;
} else {
$vscaled = 0;
}
}
#printinfo("factor",$v,$v/$vm,$k);
return $vscaled;
}
sub unique {
my %seen;
my @new;
for my $elem (@_) {
push @new, $elem if ! $seen{$elem}++;
}
return @new;
}
sub validateconfiguration {
$CONF{data_mult} ||= 1;
$CONF{color_idx_mult} ||= 1;
$CONF{colors}{h0} = $CONF{percentile_hue_start} if defined $CONF{percentile_hue_start};
$CONF{colors}{h1} = $CONF{percentile_hue_end} if defined $CONF{percentile_hue_end};
$CONF{colors}{s0} = $CONF{percentile_saturation_start} if defined $CONF{percentile_saturation_start};
$CONF{colors}{s1} = $CONF{percentile_saturation_end} if defined $CONF{percentile_saturation_end};
$CONF{colors}{v0} = $CONF{percentile_brightness_start} if defined $CONF{percentile_brightness_start};
$CONF{colors}{v1} = $CONF{percentile_brightness_end} if defined $CONF{percentile_brightness_end};
$CONF{linkcolor}{color_remap} ||= $CONF{color_remap};
$CONF{linkcolor}{color_autoremap} ||= $CONF{color_autoremap};
$CONF{linkcolor}{color_transparency} = $CONF{transparency} if exists $CONF{transparency};
$CONF{linkcolor}{color_source} = $CONF{color_source} if exists $CONF{color_source};
$CONF{segment_colors}{interpolate_type} = $CONF{interpolate_type} if exists $CONF{interpolate_type};
}
sub report_error {
my $msg = shift;
my $line = shift;
print STDERR uc "error parsing your table file\n \n";
print STDERR "problem: $msg\n \n";
if(defined $line) {
print STDERR "offending line was\n";
print STDERR $line,"\n";
}
die if $CONF{strict_sanity};
}
sub report_warning {
my $msg = shift;
my $line = shift;
print STDERR "WARNING $msg\n";
}
################################################################
#
# *** DO NOT EDIT BELOW THIS LINE ***
#
################################################################
sub populateconfiguration {
foreach my $key (keys %OPT) {
$CONF{$key} = $OPT{$key};
}
# any configuration fields of the form __XXX__ are parsed and replaced with eval(XXX). The configuration
# can therefore depend on itself.
#
# flag = 10
# note = __2*$CONF{flag}__ # would become 2*10 = 20
for my $key (keys %CONF) {
my $value = $CONF{$key};
while($value =~ /__([^_].+?)__/g) {
my $source = "__" . $1 . "__";
my $target = eval $1;
$value =~ s/\Q$source\E/$target/g;
#printinfo($source,$target,$value);
}
$CONF{$key} = $value;
}
}
sub loadconfiguration {
my $file = shift;
my ($scriptname) = fileparse($0);
if(-e $file && -r _) {
# great the file exists
} elsif (-e "/home/$ENV{LOGNAME}/.$scriptname.conf" && -r _) {
$file = "/home/$ENV{LOGNAME}/.$scriptname.conf";
} elsif (-e "$FindBin::RealBin/$scriptname.conf" && -r _) {
$file = "$FindBin::RealBin/$scriptname.conf";
} elsif (-e "$FindBin::RealBin/etc/$scriptname.conf" && -r _) {
$file = "$FindBin::RealBin/etc/$scriptname.conf";
} elsif (-e "$FindBin::RealBin/../etc/$scriptname.conf" && -r _) {
$file = "$FindBin::RealBin/../etc/$scriptname.conf";
} else {
return undef;
}
$OPT{configfile} = $file;
my $conf = new Config::General(-ConfigFile=>$file,
-AllowMultiOptions=>"yes",
-LowerCaseNames=>1,
-AutoTrue=>1);
%CONF = $conf->getall;
}
sub printdumper {
printinfo(Dumper(@_));
}
sub printdebug {
printinfo("debug",@_) if $CONF{debug};
}
sub printinfo {
printf("%s\n",join(" ",@_));
}
sub printdumper {
printinfo(Dumper(@_));
}
|