用原子坐标计算原子间距离

时间:2013-12-23 10:23:04

标签: perl

我从这个网站下面得到了这个脚本。

use strict;
use warnings;

open(OUTPUT,">/home/guest/Desktop/test.txt") or die ("couldn't write the file");
my @line;
for (<>) {
    push @line, $_;               
    next if (@line)<2;
    print OUTPUT distance(@line), "\n";
}
sub distance {
    my @a = split ' ',shift;   
    my @b = split ' ',;   
    my $x = ($a[6]-$b[6]);      
    my $y = ($a[7]-$b[7]);
    my $z = ($a[8]-$b[8]);
    my $dist = sprintf "%.1f",sqrt($x**2+$y**2+$z**2);   
   return "$a[1] to $b[1] |$a[11]-$b[11]| Distance-\t$dist";
}

我用这个脚本来识别原子之间的距离。我只能用一个原子来实现。

INPUT

HETATM    1  N   MSE A   1      16.272  16.789  53.256  1.00 31.95           N  
HETATM    2  CA  MSE A   1      15.408  15.939  52.384  1.00 30.64           C  
HETATM    3  C   MSE A   1      13.919  16.266  52.544  1.00 27.80           C  
HETATM    4  O   MSE A   1      13.393  16.321  53.659  1.00 26.16           O

OUTPUT

1 to 2 |N-C| Distance-  1.5
1 to 3 |N-C| Distance-  2.5
1 to 4 |N-O| Distance-  2.9

期望的输出

1到2 | N-C |距离 - 1.5
1至3 | N-C |距离 - 2.5
1至4 | N-O |距离 - 2.9
2比1 | N-C |距离 - 1.5
2至3 | N-C |距离 - 2.5
2至4 | N-O |距离 - 2.9
3比1 | N-C |距离 - 1.5
3至2 | N-C |距离 - 2.5
3至4 | N-O |距离 - 2.9
4比1 | N-C |距离 - 1.5
4到2 | N-C |距离 - 2.5
4至3 | N-O |距离 - 2.9

请帮帮我

2 个答案:

答案 0 :(得分:3)

该行

my @b = split ' ',;

$_变量拆分为数组@b,当您想要的是@_数组的顶部时。

看起来你想要计算每对原子之间的距离,你需要一个双环来实现。

如果你编写了一个名为distance的子程序,那么最好是它返回一个距离,而不是调用代码碰巧需要的某个字符串。

此程序似乎可以执行您想要的操作,并使用printf作为输出。

use strict;
use warnings;

open my $out, '>', '/home/guest/Desktop/test.txt' or die "Couldn't open output file: $!";
my @data = map [ split ], grep /\S/, <>;

for my $i (0 .. $#data) {
  for my $j (0 .. $#data) {

    next if $i == $j;

    my @coords_i = @{$data[$i]}[6,7,8];
    my @coords_j = @{$data[$j]}[6,7,8];

    printf $out "%s to %s |%s-%s| Distance-%.3f\n",
        $data[$i][1], $data[$j][1],
        $data[$i][11], $data[$j][11],
        distance(\@coords_i, \@coords_j);
  }
}

sub distance {
  my ($aa, $bb) = @_;
  my ($x, $y, $z) = map { $aa->[$_] - $bb->[$_] } 0 .. $#$aa;
  return sqrt($x * $x + $y * $y + $z * $z);
}

<强>输出

1 to 2 |N-C| Distance-1.493
1 to 3 |N-C| Distance-2.513
1 to 4 |N-O| Distance-2.944
2 to 1 |C-N| Distance-1.493
2 to 3 |C-C| Distance-1.533
2 to 4 |C-O| Distance-2.415
3 to 1 |C-N| Distance-2.513
3 to 2 |C-C| Distance-1.533
3 to 4 |C-O| Distance-1.234
4 to 1 |O-N| Distance-2.944
4 to 2 |O-C| Distance-2.415
4 to 3 |O-C| Distance-1.234

答案 1 :(得分:1)

文件中有4行,所以你的循环将有4次迭代(并且会跳过第一行,因为列表的大小仍为1)。

如果要匹配每两个元素,则需要一个嵌套循环,该循环将在给定n^2行输入的情况下运行n次迭代。只需将主循环切换到此

即可
for (<>) {
    push @line, $_;               
}
for $a (@line) {
    for $b (@line) {
          next if ($a eq $b);
          print OUTPUT distance(@line), "\n"; 
    }
}