从列的头部和尾部选择最小值 - Perl

时间:2015-04-17 06:45:07

标签: perl

以下是每个残基距蛋白质质心距离的代码。

use strict;
use warnings;

my $chain = 'A';
my $s1 = 0;
my $s2 = 0;
my $s3 = 0;

my $cx=0;
my $cy=0;
my $cz=0;

my @pdb;
while(<>){
    my @col = split;
    next unless $col[0] eq 'ATOM' and $col[4] eq $chain;
    push @pdb, [@col[2,3,5,6,7,8]];
}

for (my $i=0;$i<=$#pdb;$i++){
    my($a, $r, $n, $x, $y, $z) = @{$pdb[$i]};

    $s1 = $s1+$x;
    $cx++;

    $s2 = $s2+$y;
    $cy++;

    $s3 = $s3+$z;
    $cz++;
}

my $X = sprintf "%0.3f", $s1/$cx;
my $Y = sprintf "%0.3f", $s2/$cy;
my $Z = sprintf "%0.3f", $s3/$cz;

#distance of every atom from COM.

for my $j(0..$#pdb){
    my($a1, $r1, $n1, $x1, $y1, $z1) = @{$pdb[$j]};
    my $dist = sprintf "%0.3f", sqrt(($X-$x1)**2 + ($Y-$y1)**2 + ($Z-$z1)**2);
    if($a1 eq 'CA'){
        &rmin($dist,"\n");
    }
}
sub rmin{
    my @pdb1 = @_;
    print @pdb1;    
}

子程序rmin打印COM中每个残留物的距离作为列。我需要将前10个和最后10个距离的最小值发送到两个单独的变量中。我使用反引号尝试了headtail命令:

@res = `head -10` @pdb1

1 个答案:

答案 0 :(得分:3)

使用List::Util's min和几个数组切片应该可以工作:

use List::Util qw(min);

$smallest_of_first_ten = min @pdb1[0 .. 9];
$smallest_of_last_ten  = min @pdb1[-10 .. -1];