#!/usr/bin/perl -w 


$firstn=1;
$minreadsum = 10;
$minspliceraw = 0;

@dirs = qw(
hs1B
hs1D
);
$ndirs = scalar(@dirs);

for($i=0;$i<$ndirs;$i++){
$d = $dirs[$i];

system("ls $d/*_splicingValues.txt > filelist");

open(IF,"filelist");

while($a=<IF>){
chomp($a);
$tgene = $a;
$tgene =~ s/$d\///;
$tgene =~ s/_splicingValues.txt//;
$tn=0;

#find first , max
$firstpos{$d}{$tgene} = 'NA';
$max{$d}{$tgene} = 0;
$maxpos{$d}{$tgene} = 'NA';
$halfmaxpos{$d}{$tgene} = 0;
open(SV,"$a");
$b=<SV>;
while($b = <SV>){
chomp($b);
#relPos	readSum	splicingValueRaw	splicingValueNorm
#-100	6	0	0
($relpos,$readsum,$spliceraw,$splicenorm) = split(/\t/,$b);
if($relpos > 0 && $readsum > $minreadsum && $spliceraw > $minspliceraw){

if($splicenorm>$max{$d}{$tgene}){
$max{$d}{$tgene}= $splicenorm;
$maxpos{$d}{$tgene}= $relpos;
}
if($tn<$firstn){  $firstpos{$d}{$tgene} .= "$relpos"; if($firstn>1){$firstpos{$d}{$tgene} .= ";";}}
$tn++;
$genes{$tgene}++;
}
}
close(SV);

#again for halfmax
open(SV,"$a");
$b=<SV>;
while($b = <SV>){
chomp($b);
#relPos	readSum	splicingValueRaw	splicingValueNorm
#-100	6	0	0
($relpos,$readsum,$spliceraw,$splicenorm) = split(/\t/,$b);
if($relpos > 0 && $readsum > $minreadsum && $spliceraw > $minspliceraw){

if($splicenorm>($max{$d}{$tgene}/2)){
$halfmaxpos{$d}{$tgene}= $relpos;
}
}
}
close(SV);



}
}


print "gene";
for($i=0;$i<$ndirs;$i++){$d = $dirs[$i];print "\t$d";}
print "\n";

foreach $gene (sort keys %genes){
print "$gene";
for($i=0;$i<$ndirs;$i++){
$d = $dirs[$i];
if(exists $firstpos{$d}{$gene}){ print "\t$firstpos{$d}{$gene}";}
else{print "\tNA";}

}
print "\n";
}


