2016-12-15 78 views
1

參考問題Calculating the distance between atomic coordinates,其中輸入是在PDB計算距離文件

ATOM 920 CA GLN A 203  39.292 -13.354 17.416 1.00 55.76   C 
ATOM 929 CA HIS A 204  38.546 -15.963 14.792 1.00 29.53   C 
ATOM 939 CA ASN A 205  39.443 -17.018 11.206 1.00 54.49   C 
ATOM 947 CA GLU A 206  41.454 -13.901 10.155 1.00 26.32   C 
ATOM 956 CA VAL A 207  43.664 -14.041 13.279 1.00 40.65   C 
. 
. 
. 

ATOM 963 CA GLU A 208  45.403 -17.443 13.188 1.00 40.25   C 

an answer報告爲

use strict; 
use warnings; 

my @line; 
while (<>) { 
    push @line, $_;   # add line to buffer 
    next if @line < 2;   # skip unless buffer is full 
    print proc(@line), "\n"; # process and print 
    shift @line;    # remove used line 
} 

sub proc { 
    my @a = split ' ', shift; # line 1 
    my @b = split ' ', shift; # line 2 
    my $x = ($a[6]-$b[6]);  # calculate the diffs 
    my $y = ($a[7]-$b[7]); 
    my $z = ($a[8]-$b[8]); 
    my $dist = sprintf "%.1f",    # format the number 
        sqrt($x**2+$y**2+$z**2); # do the calculation 
    return "$a[3]-$b[3]\t$dist"; # return the string for printing 
} 

的上面的代碼的輸出是距離第一個CA到第二個之間,第二個到第三個之間......

如何修改此代碼以查找第一個CA到其餘CA(2,3 ...)之間的距離,以及從第二個CA到其餘CA(3,4 ...)之間的距離以及是否僅打印那些低於5埃的? 我發現push @line, $_;語句應該改變,以增加數組的大小,但不清楚如何做到這一點。

+0

您的預期產出是多少? – ssr1012

+0

GLN-HIS 「距離值」 GLN-ASN 「距離值」 GLN-GLU 「距離值」 ... HIS-ASN 「距離值」 HIS-GLU 「距離值」 HIS-VAL「距離值「 ... ASN-GLU」距離值「 ASN-VAL」距離值「 ...等等... @ ssr1012 –

回答

3

要獲得配對,請將文件讀取到數組中,@data_array。然後循環條目。

更新:添加文件打開並加載@data_array。

open my $fh, '<', 'atom_file.pdb' or die $!; 

my @data_array = <$fh>; 

close $fh or die $!; 

for my $i (0 .. $#data_array) { 
    for my $j ($i+1 .. $#data_array) { 
     process(@data_array[$i,$j]);  
    } 
} 
+0

我應該插入這些行來代替」push @line,$ _; 「保持代碼的其他部分保持原樣?請在添加這些行後發佈完整的代碼。 –

+1

@pradeep pant我添加了2 for循環之前的代碼。 sub'process'是你的子'proc'(你需要編輯你的'proc' sub來打印距離或者返回一個合適的字符串來打印) –

3

可能是試試這個:

use strict; 
use warnings; 

my @alllines =(); 
while(<DATA>) { push(@alllines, $_); } 

#Each Current line 
for(my $i=0; $i<=$#alllines+1; $i++) 
{ 
    #Each Next line 
    for(my $j=$i+1; $j<=$#alllines; $j++) 
    { 
     if($alllines[$i]) 
     { 
      #Split the line into tab delimits 
      my ($line1_tb_1,$line1_tb_2,$line1_tb_3) = split /\t/, $alllines[$i]; 
      print "Main_Line: $line1_tb_1\t$line1_tb_2\t$line1_tb_3"; 
      if($alllines[$j]) 
      { 
       #Split the line into tab delimits 
       my ($line_nxt_tb1,$line_nxt_tb2,$line_nxt_tb3) = split /\t/, $alllines[$j]; 

       print "Next_Line: $line_nxt_tb1\t$line_nxt_tb2\t$line_nxt_tb3"; 

       #Do it your coding/regex here 
      } 
     } 
     #system 'pause'; Testing Purpose!!! 
    } 
} 

__DATA__ 
tab1 123 456 
tab2 789 012 
tab3 345 678 
tab4 901 234 
tab5 567 890 

我希望這會幫助你。