#!/usr/local/bin/perl -w # # A 2 A . p # # reads in an Afile and writes out another, permuting # # see also Acleanup.p # this specifies which *current* column should be 1,2,3,4,5... $rowperm="1:7:8:9:2:3:4:5:6:10:11:12"; $colperm="1:2:3:4:5:9:11:12:7:10:14:15:6:8:13:16"; eval "\$$1=\$2" while @ARGV && $ARGV[0]=~ /^([\w\[\]]+)=(.*)/ && shift ; @rperms=split(":",$rowperm); @cperms=split(":",$colperm); # this specifies which new column number each current has print STDERR "$#cperms+1 $#rperms+1\n" ; for ( $n=0;$n<=$#rperms;$n++ ) { $rperm[$rperms[$n]] = $n+1 ; } for ( $n=0;$n<=$#cperms;$n++ ) { $cperm[$cperms[$n]] = $n+1 ; } $rperm[0] = ":" ; $cperm[0] = ":" ; print STDERR "transposed permutations: @rperm\n" ; print STDERR "transposed permutations: @cperm\n" ; print STDERR "--------Acleanup + perm.p----------- DJCM 99/12/29\n" ; # first, read the old alist in $_ = <>; ($L,$M) = split ; print STDERR "------------\nRead in $L $M\n" ; $N = $L ; for ( $n = 1 ; $n <= $N ; $n ++ ) { $nn = $cperm[$n] ; $nlist[$nn] = "" ; $numnlist[$nn] = 0 ; } for ( $m = 1 ; $m <= $M ; $m ++ ) { $mm = $rperm[$m] ; $mlist[$mm] = "" ; $nummlist[$mm] = 0 ; } $_ = <>; # max numbers $_ = <>; # @oldnumnlist = split ; $_ = <>; # @oldnummlist = split ; for ( $l = 1 ; $l <= $L ; $l ++ ) { $_ = <>; @y = split ; for ( $u = 0 ; $u <= $#y ; $u ++ ){ $ty = $y[$u] ; if ( $ty > 0 ) { $m = $ty ; $n = $l ; $mm = $rperm[$m] ; $nn = $cperm[$n] ; &mnadd($mm,$nn); } } } # print STDERR "done N = $N ; M = $M\n" ; if ( 1 ) { $head = "$N $M\n" ; print STDERR "Writing code.\n" ; # find $t (max) and $tr (max) $t = 0 ; $tr = 0 ; for ( $n = 1 ; $n <= $N ; $n ++ ) { if ( $numnlist[$n] > $t ) { $t = $numnlist[$n] ; } } for ( $m = 1 ; $m <= $M ; $m ++ ) { if ( $nummlist[$m] > $tr ) { $tr = $nummlist[$m] ; } } $head .= "$t $tr\n" ; # append zeros to short lines for ( $n = 1 ; $n <= $N ; $n ++ ) { $thisn = $numnlist[$n] ; if ( $thisn ) { $head .= "$thisn " ; while ( $thisn < $t ) { $nlist[$n] .= "0\t" ; $thisn ++ ; } } # otherwise it is a dead column } $head .= "\n" ; for ( $m = 1 ; $m <= $M ; $m ++ ) { $thism = $nummlist[$m] ; $head .= "$thism " ; while ( $thism < $tr ) { $mlist[$m] .= "0\t" ; $thism ++ ; } } $head .= "\n" ; print $head ; # print STDERR $head ; # finish lists for ( $n = 1 ; $n <= $N ; $n ++ ) { $thisn = $numnlist[$n] ; if ( $thisn ) { $nlist[$n] =~ s/\t$/\n/ ; print $nlist[$n] ; } } for ( $m = 1 ; $m <= $M ; $m ++ ) { $mlist[$m] =~ s/\t$/\n/ ; print $mlist[$m] ; } } sub mnadd { local($m,$n)=@_; if ($m>0) { # # put m and n on each others lists $nlist[$n] .= $m."\t" ; $mlist[$m] .= $n."\t" ; $numnlist[$n] ++ ; $nummlist[$m] ++ ; } } # remove from the mlist only sub mnsubtract { local($m,$n)=@_; $count = ( $mlist[$m] =~ s/\b$n\t// ) ; if ( $count ) { $nummlist[$m] -- ; } else { print STDERR "error, unable to remove $n from mlist $m\n $mlist[$m]\n" ; } }