#!/usr/bin/perl -w
# USE: $0 SourcePath DestPath
# Sample: o2.pl d:\soft\bin\1 d:\soft\bin\2
use strict;
my $SPath=shift;
my $DPath=shift;
my %hash;
my %hash0;
my %hash1;
my %hash2; # 存放正向 最终符合 条件的结果
my %hash3; # 存放反向 最终符合 条件的结果
my @h1=qw/Charge deltCn XCorr/;
my @h2=qw/1 4 3/;
&Set_Hash(1,\@h1,\@h2); # 将正向放到hash1中
%hash1=%hash;
#foreach my $key (sort keys %hash1) { print "$key $hash1{$key}\n"; }
%hash=();
&Set_Hash(0,\@h1,\@h2); # 将反向放到hash0中
%hash0=%hash;
#foreach my $key (sort keys %hash0) { print "$key $hash0{$key}\n"; }
&Get_All();
&Save_Hash($DPath."\\Xcorr_DeltaCn");
#foreach my $key ( keys %hash3) { print "$key $hash3{$key}\n"; }
# 将运算结果保存到目标File
sub Save_Hash
{
my $filename = shift;
my $str;
my @s;
print "\nSave To File: $filename\n";
for my $Charge(1..3)
{
open(FH, "> $filename"."_$Charge".".txt") or die "Couldn't open $filename for writing: $!";
print FH "Charge\tdeltCn\tXCorr\tFPR(%)\tPosCnt\tRevCnt\n";
foreach my $key(sort{$hash2{$b} <=> $hash2{$a}} keys %hash2)
{
$str=sprintf("$key:$hash3{$key}");
if(substr($str,0,1) eq $Charge)
{
@s = split /:/,$str;
foreach(@s) {print FH $_,"\t";}
print FH "\n";
}
}
close(FH);
}
}
# 根据不同条件(步长),计算各种情况下数目
sub Get_All
{
my ($x,$y,$z,$n1,$n2,$key,$p,$value);
print "Begin Count ... :\n";
foreach $z(1..3)
{
for($x=0.05;$x<0.2;$x+=0.01) # DeltaCn
{
for($y=1.0;$y<5.0;$y+=0.1) # Xcorr
{
$n1=&Get_No(1,$z,$x,$y);
$n2=&Get_No(0,$z,$x,$y);
last if($n1<=0 and $n2<=0);
print ".";
$key=sprintf("%d:%.2f:%.1f",$z,$x,$y);
$p=sprintf("%.2f",$n2>0?$n1/$n2*100:0);
$value=sprintf("%.2f:%5d:%5d",$p,$n1,$n2);
$hash2{$key}=$p if($n1>0 or $n2>0);
$hash3{$key}=$value if($n1>0 or $n2>0);
}
print " $z -- $x |\n";
}
}
}
#将多个文件中数据放置到散列中
# Flag="1" 正向, 0 反向
# In Flag %Hash (Charge,deltCn) (1,4,3)
sub Set_Hash
{
my $Flag=shift;
my $h1=shift;
my $h2=shift;
my ($line,$x2,$x3,$filename);
opendir FH,$SPath || die "Couldn't open Path:$SPath for reading: $!";
my @dir = readdir FH;
my @file = $Flag ? grep(/pos.bre$/,@dir) :grep(/rev.bre$/,@dir);
close(FH);
print "Pos File:\n";
foreach $filename(@file)
{
$filename=$SPath."\\".$filename;
print "\t",$filename,"\n";
open(FH,"<$filename") || die "Couldn't open $filename for reading: $!";
$line=<FH>;
$line=<FH>;
my @b;
for(@$h1)
{
push(@b,index($line,$_));
}
#print "@b";
#next;
while (<FH>)
{
$line=$_;
my $str_key=();
next if(length($_)<100);
my $i=0;
for(@b)
{
$str_key.=":".substr($line,$_,$$h2[$i++]);
#$str_key.=substr($line,$_,3);
}
$str_key=substr($str_key,1);
next unless ( "$str_key" =~ /^\d.*\d$/ );
# print $str_key," ===============================\n";
$hash{"$str_key"}++;
}
close(FH);
}
}
# 从散列中取出正向符合 指定values 条件的数目
# 例如: 1:0.25:1.1 取出符合大于的数目
# Flag="1" 正向, 0 反向
sub Get_No
{
my $Flag=shift;
my $i = shift;
my $j = shift;
my $k = shift;
my ($r,$x,$i1,$j1,$k1);
$r=0;
my %Temp=$Flag ? %hash1 : %hash0;
foreach $x(keys %Temp)
{
($i1,$j1,$k1) = split /:/,$x;
if($i1 eq $i and $j1 ge $j and $k1 ge $k) {$r+=$Temp{$x};}
}
$r;
}
文章评论(0条评论)
登录后参与讨论