- 论坛徽章:
- 7
|
是不是指这样?- #!/usr/bin/perl
- use 5.018;
- @ARGV = glob "/tmp/A/*";
- my %A;
- while (<>) {
- my ( $C, $N, $X, $G ) = split;
- push @{ $A{$C} }, { min => $N, max => $X, G => $G, F => $ARGV };
- }
- while ( my ( $k, $v ) = each %A ) {
- my ($N) = $k =~ /(\d+)$/;
- open my $B, "/tmp/B/bed_chr_$N.bed" or next;
- my @CHR = map { (split)[1] } <$B>;
- for my $S (@$v) {
- open my $NSP, '>>', $S->{F} . '.nsp';
- say $NSP $S->{G}, "\t", SUM( \@CHR, $S->{min}, $S->{max} );
- close $NSP;
- }
- }
复制代码 sub SUM:- sub SUM {
- my ( $A, $N, $X, $S ) = @_;
- return 0 if $N > $A->[ $#{$A} ] or $X < $A->[0];
- for (@$A) {
- last if $_ > $X;
- next if $_ < $N;
- $S++;
- }
- $S;
- }
复制代码 OR sub SUM:- sub SUM {
- my ( $A, $N, $X, $C ) = @_;
- return 0 if $N > $A->[ $#{$A} ] or $X < $A->[0];
- my $bs = sub {
- my ( $W, $l, $r, $m ) = ( shift, shift, $#{$A} );
- return $r if $W >= $A->[$r];
- return 0 if $W <= $A->[$l];
- goto R if $C;
- while ( $l <= $r ) {
- $m = int 0.5 * ( $l + $r );
- $W > $A->[ $m - 1 ]
- ? $W <= $A->[$m] ? return $m : ( $l = $m + 1 )
- : ( $r = $m - 1 );
- }
- R: while ( $l <= $r ) {
- $m = int 0.5 * ( $l + $r );
- $W < $A->[ $m + 1 ]
- ? $W >= $A->[$m] ? return $m : ( $r = $m - 1 )
- : ( $l = $m + 1 );
- }
- };
- my $L = $bs->( $N, 0 );
- $bs->( $X, $L, $C++ ) - $L + 1;
- }
复制代码 |
|