#!/usr/local/bin/perl -w # selectA2A.p - derived from selectraw2A.p # # Used in qecc/CYCLIC # # reads an Alist file and selects SOME # rows from it, writing the rejected rows to stdout in Alist form. # Rows are rejected if they would take us over the column weight limit, # or if we have accepted enough rows. # # The rejected stuff at stdout is what I am actually interested in. # # selectA2A.p kM=500 < string/A > string/A2 # $colweightlimit = 2 ; $N = 0 ; $kM = 0 ; # target number of rows to KILL $verbose = 0 ; eval "\$$1=\$2" while @ARGV && $ARGV[0]=~ /^(\w+)=(.*)/ && shift; print STDERR "===== selectA2A.p ===== (c) DJCM Tue 14/5/02 =====\n" ; $m = 0 ; $m2 = 0 ; if($kM <= 0 ) { print STDERR " need to set kM . exit . \n" ; exit(0) ; } $_ = <>; ($L,$M) = split ; print STDERR "------------\nRead in $L $M\n" ; $N = $L ; # for ( $n = 1 ; $n <= $N ; $n ++ ) { $nlist[$n] = "" ; $numnlist[$n] = 0 ; } for ( $m = 1 ; $m <= $M ; $m ++ ) { $mlist[$m] = "" ; $nummlist[$m] = 0 ; } $_ = <>; # max numbers $_ = <>; # numnlist @oldnumnlist = split ; $_ = <>; @oldnummlist = split ; NL: for ( $l = 1 ; $l <= $L ; $l ++ ) { if ( ! ( $_=<> ) ) { # read the cols print "unexpected end at n = $l\n" ; last NL ; } # above discards the column lists } $m = 0 ; # THIS COUNTS the rows TO BE RETAINED ML: for ( $mm = 1 ; $mm <= $M ; $mm ++ ) { if ( ! ( $_=<> ) ) { # read the rows print STDERR "unexpected end at m = $mm\n" ; last ML ; } if ( $mm eq $M ) { print STDERR "last line read in is\n$_\n"; } @a=split ; if (!(($#a+1) eq $oldnummlist[$m])) { print STDERR "warning, $#a+1 eq $oldnummlist[$m] row $m fail\n" ; } $thisrejected = 1 ; # The idea here is: # rows are rejected from inclusion in the Alist # only if $numnlist would not be taken below the limit # specified by colweightlimit (eg 5) [THIS IS DIFFERENT NOTATION FROM selectraw2A, # where the values of oldnumnlist were not known.] if ($m2 >= $kM ) { # then we keep this row. $thisrejected = 0 ; } else { BLOOP: foreach $b ( @a ) { if ( $b >0 ) { print STDERR "$m,$b,$oldnumnlist[$b-1]." if ($verbose) ; if ( $oldnumnlist[$b-1] <= $colweightlimit ) { # NOT a rejectable row! $thisrejected = 0 ; last BLOOP; # then we keep this row. } } } } if ($thisrejected) { $m2 ++ ; foreach $n ( @a ) { # &mnadd2($m2,$n); # add it to the "2" list, in case we have any use for it. $oldnumnlist[$n-1] --; } } else { ## OK we have decided to `accept' this row. (ie retain it) $m ++ ; foreach $n ( @a ) { &mnadd($m,$n); } # } } $M = $m ; print STDERR "done N = $N ; M = $M retained ($m2 dropped)\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] ; $head .= "$thisn " ; while ( $thisn < $t ) { $nlist[$n] .= "0\t" ; $thisn ++ ; } } $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 ; # finish lists for ( $n = 1 ; $n <= $N ; $n ++ ) { $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)=@_; # put m and n on each others lists $nlist[$n] .= $m."\t" ; $mlist[$m] .= $n."\t" ; $numnlist[$n] ++ ; $nummlist[$m] ++ ; } sub mnadd2 { local($m,$n)=@_; # put m and n on each others lists # $nlist2[$n] .= $m."\t" ; # $mlist2[$m] .= $n."\t" ; # $numnlist2[$n] ++ ; # $nummlist2[$m] ++ ; }