#!/usr/bin/env perl
# snp3aprecall.pl 

=item usage

  snp3aprecall.pl [opts] \
      -refaltpar $name.refaltpar.idtab  -genex $name.genexcopy  -apvals $name.lzapvals.tab \
      > $name.sn3gdup.aptab

  perl snp3aprecall.pl \
    evgok5asm_hs20ttcds-hs22refua50.refaltpar.idtab \
    ../tr2aacds_hs20il5asm/hs20ilasm5s.okay-human22ttot_chr.genexcopy \
    ../tr2aacds_hs20il5asm/tr2dupgn_hs20ilasm5s-lzapvals.tab \
      > evgok5asm_tr2dupgn2lzap-hs22refua50_sn3gdup2b.aptab
=cut

BEGIN{
  $DUPCN= $ENV{dupcn} || 1.6;  
  $MINA=$ENV{mina}||19; $MINS=$ENV{mins}||6; 
  $pIDT=$ENV{pidt}||98.0; $pVAR=$ENV{pvar}||18; $pIDX=$ENV{pidx}||25;
  ($dIDX,$dVAR,$dIDT,$dMINA,$dMINS)=@dvals=(80,10,99.0,$MINA,9); 
  print "# par_class_opt: pIDX=$pIDX pVAR=$pVAR pIDT=$pIDT MINA=$MINA MINS=$MINS pDUP=@dvals;\n"; 
}

my $DORPART=$ENV{rpart}||0;
# rpart() stats:
# idmax < 41 :  par if sidt < 99.2 & svar >= 12 , else alt
# idmax >= 41:  alt if cn<dupcn;
# par if cn >= dupcn & svar >=5.5 & sidt < 99.2 & sidmax <= 66 else alt


while(<>) {

  next if(/^\W/); ($d,$e)=@v=split; 
  if(/^ID/){ 
    s/^/Refap\tRefgd\tEstap\t/; s/Ispar.par_score.//; s/S_iCBIN.*$//; s/$/\tTcn,Ipar/;
    print if(1>$hdr++); next; } 
  elsif(@v<5){ 
    my($refap,$refgd,$id,$jd)=@v; $refp{$jd}{$id}= $refp{$id}{$jd}=$refap;
    $refgd{$jd}{$id}=$refgd{$id}{$jd}= $refgd;  next; } 
  elsif(@v<11){
    my($id,$tw,$nrd,$rdb,$tcn,$cmm,$ccn,$cer,$cam)=@v; $tcn{$id}=$tcn;
    $gdup{$id}=($ccn=~/dupx/)?2:($ccn=~/uniq|dups/)?1:0;  next; 
    }
    
  my($id,$jd,$api,$pscore,$nal,$nsn,$aps,$idmax,$svar,$sidt,$d3cod,$speak)=@v; 
  my $isrev=($speak=~/\-sens/);  
  my($ppIDX,$ppVAR,$ppIDT,$pMINA,$pMINS,$isdup,$cnv)= genecnv($id,$jd); 

  use constant { BigNSN => 15, MinNSN => 3 };  
if($DORPART) {
  # if($DORPART==2) { # old skip
  #   if($idmax <= 40) { $epar=2 if($sidt <= 99 and $svar >= 12); }
  #   else { $epar=4 if($isdup and $idmax < 66 and $sidt <= 99 and $svar >= 6); }
  # } 
  if($DORPART == 3) { # same result as below
   if($sidt < 99.2 and $idmax <= 66) {
     $epar=($isdup and $svar >= 5.5)?1:($idmax <= 40 and $svar >= 12)?2:0; 
   }
  } else {
  #1 best rpart val match .. does this generalize to other spp data?
  if($sidt < 99.2) {
    if($idmax <= 40) { $epar=2 if($svar >= 12); }
    elsif($idmax <= 66) { $epar=1 if($isdup and $svar >= 5.5); }
  }
  }
} else {
  if($isdup) {
    # test each, allow 3,2,1 pass to class; see below
    $epar=($idmax <= $ppIDX and $svar >= $ppVAR and $sidt <= $ppIDT and $nal >= $pMINA and $nsn >= $pMINS)? 4 :0; 
    unless($epar or $nal < $pMINA or $nsn < MinNSN) {
      #?? use stricter: $pIDX,$pVAR,$pIDT,$MINA,$MINS than ppVars ?
      if($nsn >= BigNSN and $idmax <= $ppIDX and $svar >= $ppVAR) { $epar=3; }
      elsif($nsn < $pMINS and $idmax <= $ppIDX and $sidt <= $ppIDT ) {  $epar=2; } # and $nal < 1000
      elsif($idmax <= $pIDX) {  $epar=1; }

      #.. vv--- too permissive, +630 falsepar
      # elsif($idmax <= $ppIDX and ($svar >= $ppVAR or $sidt <= $ppIDT)) { $epar=1; }
      # elsif(($idmax <= $ppIDX or $svar >= $ppVAR) and $sidt <= $ppIDT) { $epar=1; }
      # elsif($idmax <= $ppIDX) {  $epar=1; }
      }
  } else {
    $epar=($idmax <= $ppIDX and $svar >= $ppVAR and $sidt <= $ppIDT and $nal >= $pMINA and $nsn >= $pMINS)?4:0; 
    $epar=3 if($epar==0 and $nsn >= BigNSN and $idmax <= $ppIDX and $svar >= $ppVAR);
  }
}
  
  $cpar=($epar>0)?"par":($nal>=$MINA)?"alt":"unk"; $cpar.= "e_" . (($isrev)?"a":"i"); 
  $refgd=$refgd{$id}{$jd}||"na"; $refp=$refp{$id}{$jd}||"unk_r"; 
  print join("\t",$refp,$refgd,$cpar,$id,$jd,$nal,$nsn,$aps,$idmax,$svar,$sidt,$d3cod,$speak,"$cnv,$api")."\n"
    unless($did{$jd}{$id}); $did{$id}{$jd}=1; 
}

sub genecnv { 
  my($id,$jd)=@_; my($ci,$cj)=map{ $tcn{$_}||0 } ($id,$jd);
  my $cn=($cj < $ci)?$cj:$ci; 
  my $isdup= ($cn >= $DUPCN);
  my @pc= ($isdup)?($dIDX,$dVAR,$dIDT,$dMINA,$dMINS):($pIDX,$pVAR,$pIDT,$MINA,$MINS); 
  return (@pc,$isdup,$cn); 
} 


      
=item snp3aprecall trial ..

evgok5tr2dupg.info
#... reformat to sn3gdup.aptab

grep -v '#'  ../tr2aacds_hs20il5asm/tr2dupgn_hs20ilasm5s-lzapvals.tab  | \
 cat evgok5asm_hs20ttcds-hs22refua50.refaltpar.idtab \
 ../tr2aacds_hs20il5asm/hs20ilasm5s.okay-human22ttot_chr.genexcopy  - | \
perl -ne 'next if(/^\W/); ($d,$e)=@v=split; if(/^ID/){ s/^/Refap\tRefgd\tEstap\t/; s/Ispar.par_score.//; s/S_iCBIN.*$//; print if(1>$hdr++); next; } elsif(@v<5){ ($refap,$refgd,$id,$jd)=@v; $refp{$jd}{$id}= $refp{$id}{$jd}=$refap; $refgd{$jd}{$id}=$refgd{$id}{$jd}= $refgd;  next; } elsif(@v<11){ ($id,$tw,$nrd,$rdb,$tcn,$cmm,$ccn,$cer,$cam)=@v; $tcn{$id}=$tcn; $gdup{$id}=($ccn=~/dupx/)?2:($ccn=~/uniq|dups/)?1:0;  next; } ($id,$jd,$api,$pscore,$nal,$nsn,$aps,$idmax,$svar,$sidt,$d3cod,$speak)=@v; $isrev=($speak=~/\-sens/); ($ppIDX,$ppVAR,$ppIDT,$pMINA,$pMINS,$cnv)=genecnv($id,$jd); $epar=($idmax <= $ppIDX and $svar >= $ppVAR and $sidt <= $ppIDT and $nal >= $pMINA and $nsn >= $pMINS); $cpar=($epar)?"par":($nal>=$MINA)?"alt":"unk"; $cpar.= "e_" . (($isrev)?"a":"i"); $refgd=$refgd{$id}{$jd}||"na"; $refp=$refp{$id}{$jd}||"unk_r"; print join("\t",$refp,$refgd,$cpar,$id,$jd,$nal,$nsn,$aps,$idmax,$svar,$sidt,$d3cod,$speak,"$cnv,$api")."\n" unless($did{$jd}{$id}); $did{$id}{$jd}=1; sub genecnv{ my($id,$jd)=@_; my($ci,$cj)=map{ $tcn{$_}||0 } ($id,$jd); my $cn=($cj < $ci)?$cj:$ci; my @pc=($cn >= 1.6)?($dIDX,$dVAR,$dIDT,$dMINA,$dMINS):($pIDX,$pVAR,$pIDT,$MINA,$MINS); return (@pc,$cn); } BEGIN{ $MINA=$ENV{mina}||19; $MINS=$ENV{mins}||6; $pIDT=$ENV{pidt}||98.0; $pVAR=$ENV{pvar}||18; $pIDX=$ENV{pidx}||25; ($dIDX,$dVAR,$dIDT,$dMINA,$dMINS)=@dvals=(80,10,99.0,$MINA,9); print "# par_class_opt: pIDX=$pIDX pVAR=$pVAR pIDT=$pIDT MINA=$MINA MINS=$MINS pDUP=@dvals;\n"; }' \
  > evgok5asm_tr2dupgn2lzap-hs22refua50_sn3gdup.aptab

# par_class_opt: pIDX=25 pVAR=18 pIDT=98 MINA=19 MINS=6 pDUP=80 10 99 19 9;
Refap	Refgd	Estap	ID____	JD____	Naln	Nsnp	S_par	S_idmax	S_var	S_ident	d3Codon	S_peak	
unk_r	na	alte_i	HsasmEVm000001t1	HsasmEVm000001t10	26307	0	alt_sn	100	0	100.0	33	s0spot	1.0,alt_i
unk_r	na	alte_i	HsasmEVm000001t1	HsasmEVm000001t11	26307	0	alt_sn	100	0	100.0	33	s0spot	1.0,alt_i
alt_r	g23345	alte_i	HsasmEVm000001t1	HsasmEVm000001t12	26271	0	alt_sn	100	0	100.0	33	s0spot	1.0,alt_i
alt_r	g23345	alte_i	HsasmEVm000001t1	HsasmEVm000001t13	26269	0	alt_sn	100	0	100.0	33	s0spot	1.0,alt_i
alt_r	g23345	alte_i	HsasmEVm000001t1	HsasmEVm000001t14	26271	0	alt_sn	100	0	100.0	33	s0spot	1.0,alt_i


evgok5asm_hs20ttcds-hs22refua50-self95ig_all.fwbltab   << EVGroup orig
Ref     alt_a   alt_i   par_a   par_i   perf_a  perf_i  tot
alt_r   0       82247   0       226     0       14062   96535
par_r   20      1619    50      47      0       46      1782
unk_r0  19628   523260  22356   15603   699     60391   641937
unk_r1  8433    312285  10814   2305    127     34734   368698

evgok5asm_tr2dupgn2lzap-hs22refua50_sn3gdup.aptab    << THIS recall
Ref     alte_a  alte_i  pare_a  pare_i  unke_i  tot
alt_r   0       96213   0       53      0       96266   << lowest falsepar
par_r   20      1239    0       426     0       1685    << highest truepar; Note alte_a should become pare_a
unk_r   35159   855455  1779    57481   106732  1056606

evgok5asm_tr2dupgn2lzape-hs22refua50-self95ig_all.fwbltab  << Last snpdist apcall from lzalign 
Ref     alt_a   alt_i   par_a   par_i   perf_a  perf_i  tot
alt_r   0       82190   0       283     0       14062   96535
par_r   20      1241    50      425     0       46      1782
unk_r0  19223   515627  22761   23236   699     60391   641937
unk_r1  8287    308931  10960   5659    127     34734   368698

=item stats of falsepar, truepar

........
* par_r false alte mostly have alt-like stats
.. 1/2 have npar>=1.5, use that to push into pare class
.. many have pIdent above alt cut; dont require all par vars to meet cut if npar>1.5, ie pare if oneof(Nsnp,idmax,svar)
e.g. hi Nsnp, middle idmax, low svar

Refap	Refgd	Estap	ID____	JD____	Naln	Nsnp	S_par	S_idmax	S_var	S_ident	d3Codon	S_peak	
par_r	g11269/g55308	alte_i	HsasmEVm005250t11	HsasmEVm005250t2	1269	62	alt_sn	61.5	9.7	95.1	69	s3spot	2.0,par_i
par_r	g253725/g387680	alte_i	HsasmEVm000694t13	HsasmEVm000694t20	2251	51	alt_sn	54.9	8.4	97.7	33	s1spot	2.1,par_i
par_r	g728642/g984	alte_i	HsasmEVm002250t8	HsasmEVm002250t9	2334	18	par_sn	58.3	34.9	99.2	67	s3spot	2.0,par_i
.. or low Naln/Nsnp but idmax/snpvar skew to pare 
par_r	g10597/g6399	alte_i	HsasmEVm012856t1	HsasmEVm012856t13	540	6	par_sn	24.3	22.6	98.9	100	s3spot	2.0,par_i
par_r	g124908456/g2483	alte_i	HsasmEVm008648t18	HsasmEVm008648t5	374	7	par_sn	25.7	33.4	98.1	43	s3spot	4.7,par_i
par_r	g124908456/g2483	alte_i	HsasmEVm008648t19	HsasmEVm008648t5	374	7	par_sn	25.7	33.8	98.1	43	s3spot	4.7,par_i
par_r	g728642/g984	alte_i	HsasmEVm002250t36	HsasmEVm002250t44	1571	11	par_sn	26.9	17.2	99.3	82	s3spot	2.0,par_i
........

par_r/pare stats for nt=426 :  egrep 'Refap|par_r' | egrep 'Refap|pare' 
Item  	Median	Mean	SEM	Nitem	StDev	Range
  Naln	 1300	1321.86	36.15	426	746.18	HsasmEVm006208t26-3885
  Nsnp	  62	146.56	12.63	426	260.68	6-975
 S_par	   2	2.80	0.25	426	5.24	1.sn-44
S_idmax	19.30	25.67	0.86	426	17.72	1.5-65.9
 S_var	25.90	25.62	0.35	426	7.18	10.7-55.9
S_ident	96.30	90.30	0.77	426	15.95	12.4-99.0
d3Codon	  43	45.58	0.90	426	18.52	11-100
 Speak	   3	4.37	0.39	426	7.95	1spot-57
   GCn	1.00	1.75	0.06	426	1.26	1.0-8.1
 I_par	   2	2.09	0.03	426	0.52	2.i-6.5,2.i


par_r/alte  stats for nt=1259  : egrep 'Refap|par_r' | egrep 'Refap|alte_'
Item  	Median	Mean	SEM	Nitem	StDev	Range
  Naln	 712	1058.79	32.51	1259	1153.59	HsasmEVm000900t20-7117	<< shorter Naln vs pare
  Nsnp	   4	102.36	12.91	1259	458.14	0-3669			<< few Nsnp
 S_par	   1	1.48	0.03	1259	0.96	0-13
S_idmax	80.30	71.91	0.86	1259	30.44	1.sn-100		<< hi idmax
 S_var	3.30	14.56	0.63	1259	22.40	0-100			<< low snpvar
S_ident	99.60	93.30	0.61	1259	21.80	0-100.0			<< hi pIdent
d3Codon	  33	42.15	0.84	1259	29.69	0-100
 Speak	   1	2.99	0.28	1259	10.08	0spot-100
   GCn	1.00	1.68	0.04	1259	1.30	0spot-10.5
 I_par	   1	1.61	0.03	1259	0.98	1.i-11.1,1.i

=cut