#!/usr/bin/perl
use
POSIX
qw [
floor ceil ] ;
my
@heads
;
my
$pf
;
my
$tf
;
my
$rows
= 0 ;
my
$quot
=
$o
{
q} // 0.5 ;
sub reading ( ) ;
sub showing1 ( ) ;
sub showing2 ( ) ;
sub showing3 ( ) ;
sub showing4 ( ) ;
reading ; # 読取り
showing1 unless $o{N}
||
$o
{T} ;
print
"\n"
;
showing2
unless
$o
{N} ||
$o
{T} ;
showing3
if
$o
{N} ;
showing4
if
$o
{T} ;
exit
0 ;
sub
reading ( ) {
if
(
$o
{
'='
} ) {
my
$head
= <> ;
chomp
$head
;
@heads
=
split
/\t/ ,
$head
, -1 ;
}
while
( <> ) {
chomp
;
my
@F
=
split
/\t/ ,
$_
, -1 ;
if
( !
$o
{N} && !
$o
{T} )
{
for
my
$i
( 0 ..
$#F
) {
for
my
$j
( 0 ..
$#F
) {
$pf
-> [
$i
] [
$j
] {
$F
[
$i
] .
"\t"
.
$F
[
$j
] } ++ ;
}
}
}
elsif
( !
$o
{T} )
{
for
my
$i
( 0 ..
$#F
) {
for
my
$j
( 0 ..
$#F
) {
$pf
-> [
$i
] [
$j
] {
$F
[
$i
] } {
$F
[
$j
] } ++ ;
}
}
}
else
{
for
my
$i
( 0 ..
$#F
) {
for
my
$j
( 0 ..
$#F
) {
for
my
$k
( 0 ..
$#F
) {
$tf
-> [
$i
] [
$j
] [
$k
] {
$F
[
$i
] .
"\t"
.
$F
[
$j
] } {
$F
[
$k
] } ++ ;
}
}
}
}
$rows
++ ;
}
}
sub
showing1 ( ) {
my
$cols
= @{
$pf
} ;
@heads
= ( 1 ..
$cols
)
unless
@heads
;
my
@diag
=
map
{
scalar
keys
%{
$pf
-> [
$_
][
$_
]}} 0 ..
$cols
-1 ;
print
GREEN
join
(
"\t"
,
"pairs"
, 1 ..
$cols
) ,
"\n"
;
my
$cell
;
for
my
$i
( 0 ..
$cols
- 1 ) {
my
@out
= () ;
push
@out
, color(
'green'
) . (
$i
+1) . color(
'reset'
) ;
for
my
$j
( 0 ..
$i
-1 ) {
push
@out
, color(
'blue'
) .
sprintf
(
"%2.1f"
,
$cell
->[
$i
][
$j
] * 100 ). color(
'reset'
);
}
push
@out
, color(
'bright_green'
) . (
scalar
keys
%{
$pf
->[
$i
][
$i
]}) . color(
'reset'
) ;
for
my
$j
(
$i
+ 1 ..
$cols
-1 ) {
my
$val0
=
scalar
keys
%{
$pf
->[
$i
][
$j
] } ;
my
$prod
=
$diag
[
$i
] *
$diag
[
$j
] ;
my
$dmin
= max
$diag
[
$i
] ,
$diag
[
$j
] ;
my
$val
=
$val0
;
$val
= color(
'bright_yellow'
) .
$val
. color(
'reset'
) .
':'
if
$val0
==
$rows
;
$val
= color(
'yellow'
).
$val
.color(
'reset'
) .
'*'
if
$val0
==
$prod
;
$val
= color(
'cyan'
).
$val
.color(
'reset'
) .
'-'
if
$val0
==
$dmin
;
push
@out
,
$val
;
my
$tmp
= min
$prod
,
$rows
;
$cell
-> [
$j
][
$i
] =
$tmp
==
$dmin
?
"nan"
: (
$val0
-
$dmin
) / (
$tmp
-
$dmin
) ;
}
push
@out
, color (
'green'
) .
$heads
[
$i
] . color (
'reset'
) ;
print
join
"\t"
,
@out
;
print
"\n"
;
}
}
sub
showing2 ( ) {
my
$cols
= @{
$pf
} ;
@heads
= ( 1 ..
$cols
)
unless
@heads
;
my
@diag
=
map
{
scalar
keys
%{
$pf
-> [
$_
][
$_
]}} 0 ..
$cols
-1 ;
print
GREEN
join
(
"\t"
,
"freq"
, 1 ..
$cols
) ,
"\n"
;
my
$cell
;
for
my
$i
( 0 ..
$cols
- 1 ) {
my
@out
= () ;
push
@out
, color(
'green'
) . (
$i
+1) . color(
'reset'
) ;
for
my
$j
( 0 ..
$i
- 1 ) {
my
$val
= & median (
values
%{
$pf
->[
$i
][
$j
] } ) ;
push
@out
, color(
'blue'
) .
$val
. color(
'reset'
) ;
}
my
$val
= !
defined
$o
{
q} ?
( min values %{ $pf->[$i][$i] }
) .
"-"
. ( max
values
%{
$pf
->[
$i
][
$i
] } ) :
median (
values
%{
$pf
->[
$i
][
$i
] } ) ;
push
@out
, color(
'bright_green'
) .
$val
. color (
'reset'
) ;
for
my
$j
(
$i
+1 ..
$cols
-1 ) {
my
(
$val
) ;
$val
= ( min
values
%{
$pf
->[
$i
][
$j
] } ) .
"-"
. ( max
values
%{
$pf
->[
$i
][
$j
] } ) ;
push
@out
,
$val
;
}
push
@out
, color (
'green'
) .
$heads
[
$i
] . color (
'reset'
) ;
print
join
"\t"
,
@out
;
print
"\n"
;
}
}
sub
showing3 ( ) {
my
$cols
= @{
$pf
} ;
@heads
= ( 1 ..
$cols
)
unless
@heads
;
my
@diag
=
map
{
scalar
keys
%{
$pf
-> [
$_
][
$_
]}} 0 ..
$cols
-1 ;
print
GREEN
join
(
"\t"
,
"NonD"
, 1 ..
$cols
) ,
"\n"
;
my
$cell
;
for
my
$i
( 0 ..
$cols
- 1 ) {
my
@out
= () ;
push
@out
, color(
'green'
) . (
$i
+1) . color(
'reset'
) ;
for
my
$j
( 0 ..
$i
- 1 ) {
my
$val
= nonDeterminability (
$i
,
$j
) ;
push
@out
,
$val
;
}
push
@out
, color(
'bright_green'
) . (
scalar
keys
%{
$pf
->[
$i
][
$i
]}) . color(
'reset'
) ;
for
my
$j
(
$i
+1 ..
$cols
-1 ) {
my
$val
= nonDeterminability (
$i
,
$j
) ;
push
@out
,
$val
;
}
push
@out
, color (
'green'
) .
$heads
[
$i
] . color (
'reset'
) ;
print
join
"\t"
,
@out
;
print
"\n"
;
}
}
sub
showing4 ( ) {
my
$cols
= @{
$tf
} ;
@heads
= ( 1 ..
$cols
)
unless
@heads
;
my
@diag
=
map
{
scalar
keys
%{
$tf
-> [
$_
][
$_
][
$_
]}} 0 ..
$cols
-1 ;
print
GREEN
join
(
"\t"
,
"wC"
, 1 ..
$cols
,
"dis"
) ,
"\n"
;
my
$cell
;
for
my
$i
( 0 ..
$cols
- 1 ) {
my
@out
= () ;
push
@out
, color(
'green'
) . (
$i
+1) . color(
'reset'
) ;
for
my
$j
( 0 ..
$i
- 1 ) {
push
@out
, color(
'blue'
) .
join
(
","
, whichColDet (
$i
,
$j
, 1 ) ) . color(
'reset'
) ;
}
my
@diagD
= whichColDet (
$i
,
$i
, 0 ) ;
my
%seen
;
$seen
{
$_
} = 1
for
@diagD
;
push
@out
, color(
'bright_green'
) .
join
(
','
,
@diagD
) . color(
'reset'
) ;
for
my
$j
(
$i
+1 ..
$cols
-1 ) {
push
@out
,
join
(
','
,
grep
{ !
$seen
{
$_
} } whichColDet (
$i
,
$j
, 0 ) ) ;
}
push
@out
, color(
'bright_green'
) . (
scalar
keys
%{
$tf
->[
$i
][
$i
][
$i
] } ) . color(
'reset'
) ;
push
@out
, color (
'green'
) .
$heads
[
$i
] . color (
'reset'
) ;
print
join
"\t"
,
@out
;
print
"\n"
;
}
}
sub
whichColDet ( $$ $ ) {
my
$tfij
=
$tf
->[
$_
[0] ][
$_
[1] ] ;
my
@ret
;
for
( 0 ..
scalar
@{
$tfij
} -1 ) {
next
if
$_
==
$_
[0] ||
$_
==
$_
[1] ;
my
$cnt
= 0 ;
for
my
$vi
(
keys
%{
$tfij
-> [
$_
] } ) {
$cnt
++
if
1 <
scalar
keys
%{
$tfij
-> [
$_
]{
$vi
} } ;
}
push
@ret
,
$_
+ 1
if
$cnt
==
$_
[2] ;
}
return
@ret
;
}
sub
nonDeterminability ( $$ ) {
my
$cnt
= 0 ;
my
$pfij
=
$pf
->[
$_
[0] ][
$_
[1] ] ;
for
(
keys
%{
$pfij
} ) {
$cnt
++
if
1 <
scalar
keys
%{
$pfij
-> {
$_
} } ;
}
return
$cnt
;
}
sub
median ( @ ) {
@_
=
sort
{
$a
<=>
$b
}
@_
;
my
$len
=
scalar
@_
- 1 ;
return
(
$_
[ floor
$len
*
$quot
] +
$_
[ ceil
$len
*
$quot
] ) / 2 ;
}
sub
VERSION_MESSAGE {}
sub
HELP_MESSAGE {
$ARGV
[1] //=
''
;
open
my
$FH
,
'<'
, $0 ;
while
(<
$FH
>){
s/\$0/
$Script
/g ;
print
$_
if
s/^=head1// .. s/^=cut// and
$ARGV
[1] =~ /^o(p(t(i(o(ns?)?)?)?)?)?$/i ? m/^\s+\-/ : 1;
}
close
$FH
;
exit
0 ;
}