- 论坛徽章:
- 1
|
程序效率太低..我用的全是for,应该是最慢的,问题是换成foreach能提高很多么?
求拍造成效率低下的原因及改进,谢谢!
sub get_interface{
my ($range,@native)= @_;
my @native_one;
my @native_two;
my @predict_one;
my @predict_two;
my @temp1;
my $count;
my $chain="RANDOM";
my $num;
my $miss;
my $mark;
#read in file
@temp1=grep{/^ATOM.*?/} @native;
#store the files as 2D matrix
$count=0;
$miss=0;
for(my $i=0;$i<scalar(@temp1);$i++){
if($temp1[$i]=~/^ATOM.*?/){
$count++;
#mark first res
if($count == 1){
$chain=substr($temp1[$i],23,3);
$mark=1;
}
#Chain One
if($chain eq substr($temp1[$i],23,3) && $count <30 && $mark==1){
$native_one[$i-$miss][0]=substr($temp1[$i],23,3);#res number
$native_one[$i-$miss][1]=substr($temp1[$i],21,1);#Chain ID
$native_one[$i-$miss][2]=substr($temp1[$i],32,6);#X
$native_one[$i-$miss][3]=substr($temp1[$i],40,6);#Y
$native_one[$i-$miss][4]=substr($temp1[$i],48,6);#Z
$native_one[$i-$miss][5]=$i+1;#line number
$num=scalar(@native_one);
}
if($chain ne substr($temp1[$i],23,3) && $count >1 && $mark==1){
$native_one[$i-$miss][0]=substr($temp1[$i],23,3);#res number
$native_one[$i-$miss][1]=substr($temp1[$i],21,1);#Chain ID
$native_one[$i-$miss][2]=substr($temp1[$i],32,6);#X
$native_one[$i-$miss][3]=substr($temp1[$i],40,6);#Y
$native_one[$i-$miss][4]=substr($temp1[$i],48,6);#Z
$native_one[$i-$miss][5]=$i+1;#line number
$num=scalar(@native_one);
}
#Chain Two
if($count > 30 && $chain eq substr($temp1[$i],23,3)){
$mark=2;
}
if($count >30 && $mark==2){
$native_two[$i-$num-$miss][0]=substr($temp1[$i],23,3);#res number
$native_two[$i-$num-$miss][1]=substr($temp1[$i],21,1);#Chain ID
$native_two[$i-$num-$miss][2]=substr($temp1[$i],32,6);#X
$native_two[$i-$num-$miss][3]=substr($temp1[$i],40,6);#Y
$native_two[$i-$num-$miss][4]=substr($temp1[$i],48,6);#Z
$native_two[$i-$num-$miss][5]=$i+1;#line number
}
}
else{
$miss++;
}
}
#get the line numbers for calculation
my $num1=scalar(@native_one);
my $num2=scalar(@native_two);
my @residue=residue(\@native_one,\@native_two,$num1,$num2,$range);
return @residue;
}
#calculate the distance and find the residues
sub residue{
my($first,$second,$num1,$num2,$range)=@_;
my $distance;
my @result_first;
my @result_second;
for(my $i=0;$i<$num1;$i++){
for(my $j=0;$j<$num2;$j++){
$distance=distance($$first[$i][2],$$first[$i][3],$$first[$i][4],$$second[$j][2],$$second[$j][3],$$second[$j][4]);
if($distance <= $range){
my $temp="$$first[$i][0]";
push @result_first,$temp;
my $temp2="$$second[$j][0]";
push @result_second,$temp2;
}
}
}
#delete the reduntant residues
my %hash=();
my @result1 = grep{$hash{$_}++ <1} @result_first;
my %hash2=();
my @result2 = grep{$hash2{$_}++ <1} @result_second;
my @result_final=(@result1,"divide",@result2);
return @result_final;
}
#Function for distance
sub distance{
my($x1,$y1,$z1,$x2,$y2,$z2)=@_;
my $square=($x1-$x2)**2+($y1-$y2)**2+($z1-$z2)**2;
my $result=sqrt($square);
return $result;
} |
|