#!/usr/bin/env perl
# evigene/scripts/genoasm/asm_evd_filter.pl

=item about

new table covtab x dmag21nsk8maca8mo_altsc.align.pidcoords (from nucmer, or btall or blastn align tables)
.. tabulate aCovM (& aCovT?) from covtab: sum, nbin, ave for 
 a. scaf total, b. each rscaf x qscaf align (sum over spans or per align-span?)

dmag21nsk8maca8mo_SRR15012074_b2_bwa.cc8j.covtab.gz    mo1ok : only this? but match calc drop-cov to mo2,mo3ok
dmag21nsk8maca8mo2ok_SRR15012074_b2_bwa.cc8j.covtab.gz mo2ok
dmag21nsk8maca8mo3ok_SRR15012074_b2_bwa.cc8j.covtab.gz mo3ok
pt=dmag21nsk8maca8mo
gunzip -c $pt?SRR15012074_b2_bwa.cc8j.covtab.gz | cat dmag21nsk8maca8mo.align.pidcoords - |\
  perl makecovalntab.pl > $pt.covalntab

=item about UPD2303

  maybe merge w/   evigene/scripts/genoasm/asmlink2refchr.pl
  maybe rename as evigene/scripts/genoasm/asm_evd_filter.pl ?
  
  -- inputs: 
      asm.self.aligntab      : asm contigs self-align from blastn, nucmer, other?
      gnodes_asm_dna.covtab  : DNA cover depth on asm
      gnodes_genes.genexcopy : gene copynum from DNA x CDS
      asm_genes.aligntab     : count gene copynum on asm
      contam.tab ?? add this from asmlink2refchr maybe, table of contigs w/ bacter/contam aligns
      
  -- outputs: 
     asm.evd_genecovaln.tab == evidences (intermediate output for keepdrop, then updated)
     asm.keepdrop.tab       == contigs classified w/ evidences as keep/drop  
     
=cut

use strict;
use Getopt::Long;
#use File::Basename qw(basename dirname fileparse);

use constant VERSION => '2023.03.30';  
use constant UPD2303 => 1;
our $EGAPP='asm_evd_filter';  #  ?
our $EGLOG='asmevd';
our $EGtag='asmevd_'; # tag output files

my $debug=$ENV{debug}||1;
my $REUSE= 1;
my $BN=100;  # ENV{binsize}
my $ALN=$ENV{aln}||75; # try 70?
# my $CU=50; # opts fixme: CLO, CHI depend on Cucg .. get from genexcopy
my $CLO=$ENV{clo}||15; # try lower? 10? for drop w/o align
my $CHI=$ENV{chi}||35; # try 40? for CU == 50..55
my $GDUPHI= $ENV{gduphi}||0.65; # option, for gene excess dups
my $ALN_FORMAT=$ENV{alnformat}||"";
my $NCPU=$ENV{NCPU}||1;
my $blopt_self="-task megablast -evalue 1e-9 -perc_identity 98 -word_size 50 "; # unsure wordsize use;

my($hdr,%aln,%alnb,%qcov); #?? need these globals?

my $incovaln=$ENV{covalntab}; # dmag21nsk8maca8mo.covalntab
my $inasmevdtab= $ENV{asmevdtab};
#oldname: my $genecopy= $ENV{genecopy}; # dmag21nsk8maca8mo_genecopy.sccount
my $keepdropids= $ENV{ids}||$ENV{dropids}||$ENV{keepids}|| undef; # dmag21nsk8maca8mo.keepdrop2b.tab 3c.tab or any idlist,
# my $facount= $ENV{facount}; # dmag21nsk8maca8mo_apasc.facount

my @ARGSAVE=@ARGV;
my( $asmfa, $incovtab, $inselfaln, $ingenexf, $ingenealign, $contamtab, $output, $logfile);
my %opts= (
  "asm|assembly|contigs=s", \$asmfa, # dont require? inselfaln + incovtab for make_covaln, then covalntab for make_keepdrop 
  # "chrasm|refchr|chromosomes=s", \$refchrs, # require 
  "asmselfalign|selfalign=s", \$inselfaln, # opt .. FIXME: genecopy.tab now different
  "ALNFORMAT=s", \$ALN_FORMAT, # blastn outfmt6,7 or nucmer now  
  "asmcovtab|gnodescov|covtab=s", \$incovtab, # opt .. FIXME: genecopy.tab now different
  "genexcopy=s", \$ingenexf, # opt .. FIXME: genecopy.tab now different
  "genealigntab=s", \$ingenealign, # genes x asm align tab, now gmap.align.tab format, fixme
  "contamtab=s", \$contamtab, # opt
  "covalntab=s", \$incovaln, # opt, made here
  "asmevdtab=s", \$inasmevdtab, # opt, made here
  "keepdropids|dropids=s", \$keepdropids, # opt

  "ALN|align=i", \$ALN, "CLO=i", \$CLO, "CHI=i", \$CHI,  
  "GDUPHI", \$GDUPHI,  
  "NCPU=i", \$NCPU, # "MAXMEM=i", \$MAXMEM,  
  "REUSE!", \$REUSE, # $ENV{doupdate} == noREUSE
  "debug!", \$debug, 
);

  
my $optok= GetOptions(%opts);
my $optlist= ($debug) ? "All opts: ".join(", ",sort keys %opts) : "";

die "EvidentialGene $EGAPP, VERSION ",VERSION,"

  tabulate evidence supporting chromosome assembly contigs,
  classify contigs as valid (unique & duplicated) or not (contam or excess duplication) 
  draw evidence from genes aligned and gnodes dna cover depth, contam
  
usage: $EGAPP -assembly myasm.fa -selfalign myasm-self.blastn  -gnodescov myasm_gnodes.covtab 
  -genexcopy mygenes_gnodes.genexcopy -genealigntab myasm_mygenes.align.tab
inputs: 
  asm.self.aligntab      : asm contigs self-align from blastn, nucmer, other?
  gnodes_asm_dna.covtab  : DNA cover depth on asm
  gnodes_genes.genexcopy : gene copynum from DNA x CDS
  asm_genes.aligntab     : count gene copynum on asm
  contam.tab ?? add this from asmlink2refchr maybe, table of contigs w/ bacter/contam aligns
outputs: 
   asm.evd_genecovaln.tab == evidences (intermediate output for keepdrop, then updated)
   asm.keepdrop.tab       == contigs classified w/ evidences as keep/drop  

" unless($optok and $asmfa and ($inselfaln or $incovtab or $incovaln));

sub MAIN_stub { }

my($asmname,$asmsize,$asmnsc,$asmtotal)=(0) x 9;
  $asmname=`basename $asmfa`; chomp($asmname); $asmname=~s/\.gz//; $asmname=~s/\.fa\w*$//;  #basename($asmfa);
  $asmsize= $asmfa; $asmsize =~ s/\.fa.*$/.facount/;
  unless(-s $asmsize) { ($asmnsc,$asmtotal)= fasize($asmfa,$asmsize);  }
  # else count scafs, asmtotal?

#FIXME; 23apr11: contamtab not fully used, but now contans asm scaf ids to drop from all other evd inputs
#   e.g  asm.selfalign, asm.covtab, asm.genealn will ignore data on contam scaf ids

my($ncontam, $contamids)= ($contamtab)? read_contam($contamtab) : (0);

#FIXME read head ingenexf for Cucg always ??
if($ingenexf and $ingenealign) {
  unless( $REUSE and -s $inasmevdtab) { 
    my($nsc, $asmevdtab, $ngev, $gcnaltab)= make_asm_gene_evd($asmname,$ingenexf,$ingenealign,$asmsize); # $asmcount = facount or scaf size tab
    $inasmevdtab= $asmevdtab; # $asmevdtab == new name for genecopy table
  }
}

if($incovtab and -s $asmfa and not ($inselfaln or $incovaln)) { 
  (my $sok, $inselfaln)= make_selfaln($asmname,$asmfa,); 
}

if($inselfaln and $incovtab) {
  unless($REUSE and $incovaln) {
    my $outcovaln= "$EGtag$asmname.covalntab";
    my($ntab,$in2covaln)= make_covalntab($asmfa, $inselfaln, $incovtab, $outcovaln);
    $incovaln= $in2covaln;
  }
}

if($incovaln) {

  if(defined $keepdropids) {
    my $isdrop= $ENV{dropids}? 'drop': $ENV{keepids}? 'keep': 0;# fixme
    sumkeepdrop($incovaln,$keepdropids,$isdrop); # genecopy tab optional?
 
  } else {
    #? input any keepdropids?
    my($nid,$ndrop,$covaln,$kdtab,$ng,$genec)= makekeepdrop($incovaln,$inasmevdtab); # genecopy tab optional?

    #insert here, iterate?: recalc keepdrop w/ scafoklist from kdtab
    #  for (i in 1..5){ remake_asm_gene_evd($aname,$scafoklist,$ingcnaltab,$fasize); remake_keepdrop(xxx); last if(nochange); }

    if($keepdropids) { } #? replace, new? but dont get here with above...
    if( -f $incovaln) { ($keepdropids=$incovaln) =~ s/\.\w+$//; $keepdropids.=".keepdrop.ids"; }
    putkeepdrop($kdtab, $keepdropids, $covaln, $genec); # here?
    
    my $nucovtab='';
    if( -f $incovaln) { ($nucovtab=$incovaln) =~ s/\.\w+$//; $nucovtab.=".updcovalntab"; }
    updcovalntab($covaln,$kdtab,$nucovtab);
 }

} 

#==================================

sub loggit { my $ltyp= shift @_; chomp(@_); warn "#",@_,"\n"; }
sub runcmd { my($c)=@_; loggit( 1, "cmd=$c") if($debug);  `$c`; } # or system($c) or `$c` ?
sub openRead { my($f)=@_; my ($ok,$inh); 
  if($f=~m/.gz$/){ $ok=open($inh,"gunzip -c $f|"); } else { $ok=open($inh,$f); }
  return($ok,$inh); }
sub fasize {  
  my ($fa,$otab)=@_; my ($ns,$sw,$id,$len)=(0) x 9;
  loggit 0,"fasize($fa)"  if($debug);
  my($ok,$hin)= openRead($fa); return($ns,$sw) unless($ok);
  rename($otab,"$otab.old") if(-f $otab); open(OUT,">$otab");
  while(<$hin>) { 
    if(/^>(\S+)/){ print OUT "$id\t$len\n" if($id); $id=$1; $sw+=$len; $len=0; $ns++; }
    else { $len += (length($_) - 1); } 
  }
  print OUT "$id\t$len\n" if($id);
  close($hin); close(OUT);
  return ($ns,$sw); 
}


=item make_selfaln
  if($incovtab and -s $asmfa and not ($inselfaln or $incovaln)) { ($inselfaln)= make_selfaln($asmname,$asmfa, ); }

  blopt="-task megablast -evalue 1e-9 -perc_identity 98 -word_size 50 "; # unsure wordsize use
  blastn $blopt -num_threads 3 -db daplx23pbc_asm2dc -query daplx23pbc_asm2d-decon.fa -outfmt 7 -out daplx23pbc_asm2dc-self98i50w.blastn 

=cut

sub make_selfaln {
  my($aname,$asmfa)=@_;
  
  (my $selfdb=$asmfa) =~ s/\.fa.*//;
  my($selfdir,$selfna) = $selfdb =~ m,^(.+)/(\w[^\/]+),;
  
  my $blout="$EGtag$aname-self.blastn";
  if($REUSE and -s $blout ) { return(1,$blout); }

  my $CAT=($asmfa =~ /.gz/) ? "gunzip -c": "cat";    
  unless( -s "$selfdb.nsq") {
    runcmd("$CAT $asmfa | makeblastdb -dbtype nucl -title $selfna -out $selfdb -logfile /dev/null");
  }
  runcmd("$CAT $asmfa | blastn $blopt_self -num_threads $NCPU -db $selfdb  -outfmt 7 -out $blout");
  return(1,$blout);
}


sub readids {
  my($keepdropids,$isdrop)= @_;
  my ($nids,%kdtab,$inh)=(0);
  if(-f $keepdropids) {  
    loggit 0, "# readids $keepdropids \n" if $debug; 
    open($inh,$keepdropids) || die "reading $keepdropids"; 
    while(<$inh>){ next if(/^\W/);
      my @v; my($id)=@v=split; 
      # if($ncontam and $contamids->{$id}) { } #?? change kd val or not
      if(@v==1) { $kdtab{$id}=$isdrop; } else { $kdtab{$id}=$v[1]; }
    }
    $nids=scalar(keys %kdtab);
    close($inh);
  }
  return($nids,\%kdtab);
}

=item sumkeepdrop

grep self dmag21nsk8maca8mo.covalntab | sort -k4,4nr | head -1750 |\
perl -ne '($sc,$cl,$nb,$acov,$scov)=@v=split; $snb+=$nb; $sn++;
$sscov+=$scov; $sacov+=$acov;  END{ $acov=int($sacov/$sn);
$scov=int($sscov/$snb); $mb=int( $snb*100/100_000)/10; 
print "ns=$sn, mb=$mb, snb=$snb, av(acov)=$acov, scov=$scov\n";  }'

# acov > Cu
ns=1750, mb=22.4, snb=224320, av(acov)=221, scov=172

# acov ~= Cu
grep self dmag21nsk8maca8mo.covalntab | sort -k4,4nr | head -2450 | tail -700 | perl ..
ns=700, mb=107, snb=1070325, av(acov)=47, scov=45

# acov < Cu
grep self dmag21nsk8maca8mo.covalntab | sort -k4,4nr | tail -5330 | perl ..
ns=5330, mb=216.5, snb=2165710, av(acov)=14, scov=16
#  ^^ these are scafs needing keep/drop tests, expect ~1/2 keeps ~100mb

env ids=1 covalntab=dmag21nsk8maca8mo.covalntab  $gw/daphnia/daphmag/makecovalntab.pl | head
# sum.covaln ns=7784, mb=346.2, snb=3462327, av(acov)=64, scov=35, dropid=0

env ids=dmag21nsk8maca8mo.keepdrop2b.tab covalntab=dmag21nsk8maca8mo.covalntab  $gw/daphnia/daphmag/makecovalntab.pl | head
# sum.covaln ns=4608, mb=253.8, snb=2538917, av(acov)=100, scov=44, dropid=3176

env ids=dmag21nsk8maca8mo.keepdrop3c.tab covalntab=dmag21nsk8maca8mo.covalntab  $gw/daphnia/daphmag/makecovalntab.pl | head
# sum.covaln ns=3013, mb=206.4, snb=2064756, av(acov)=142, scov=50, dropid=4771

=cut

sub sumkeepdrop {
  my($incovaln,$keepdropids,$isdrop)= @_;
  
  my($snb,$sn,$sscov,$sacov,$ndrop)=(0) x 9;
  my $inh;
  if($incovaln =~ /^stdin|^-/i) { $inh=*STDIN; }
  elsif(-f $incovaln) {  open($inh,$incovaln) || die "reading $incovaln"; }
  else {  $inh=*STDIN; }
  
  my($nids,$keepdrop)= ($keepdropids)? readids($keepdropids,$isdrop) :(0);
  ## change to sub readCovaln() ; store self,any,scaln data for use
  while(<$inh>){
    my($sc,$cl,$nb,$acov,$scov)=split;
    next unless(/^\w/ and defined $scov);
    my $scdrop= ($nids) ? $keepdrop->{$sc}||0 : 0;
    if($scdrop =~ /drop/){ $ndrop++ if($cl eq 'self'); next; }   
    if($cl eq 'self'){
      $snb+=$nb; $sn++; $sscov+=$scov; $sacov+=$acov; 
    } elsif($cl eq 'any') {
    
    } elsif($cl =~ /^\w/) { #aln scafid
    
    }
  } close($inh);
  
  my $acov=int($sacov/$sn);
  my $scov=int($sscov/$snb); my $mb=int( $snb*100/100_000)/10;
  print "# sum.covaln ns=$sn, mb=$mb, snb=$snb, av(acov)=$acov, scov=$scov, dropid=$ndrop\n";
}

sub readCovaln {
  my($incovaln)= @_;
  
  my($ok,$snb,$sn,$sscov,$sacov,$ndrop)=(0) x 9;
  my ($inh,%covaln);
  loggit 0, "# readCovaln $incovaln \n" if $debug; 
  if($incovaln =~ /^stdin|^-/i) { $inh=*STDIN; }
  elsif(-f $incovaln){ ($ok,$inh)= openRead($incovaln); }
  # elsif(-f $incovaln) {  open($inh,$incovaln) || die "reading $incovaln"; }
  else {  $inh=*STDIN; }
  
  while(<$inh>){
    my($sc,$cl,$nb,$acov,$scov)=split;
    next unless(/^\w/ and defined $scov); next if(/^ScafID/);
    next if($ncontam and $contamids->{$sc});
    $covaln{$sc}{$cl}=[$nb,$acov,$scov]; #? yes
    if($cl eq 'self'){
      $snb+=$nb; $sn++; $sscov+=$scov; $sacov+=$acov; 
    } elsif($cl eq 'any') {
    
    } elsif($cl =~ /^\w/) { #aln scafid
    
    }
  } close($inh);
  
  my $acov=int($sacov/$sn); my $scov=int($sscov/$snb); my $mb=int( $snb*100/100_000)/10;
  loggit 0, "# sum.covaln ns=$sn, mb=$mb, snb=$snb, av(acov)=$acov, scov=$scov\n" if($debug);
  return($sn,\%covaln);
}

#UPD2303: add evdgene recalc here, need another genex table, not readGenetab() but per gene x scaf
sub calcCovaln {
  my($sid, $covaln, $kdtab)= @_;
  # $kdtab{$id}=$isdrop; ? use to calc sid covaln, ie 'self' update :
  #  scov.calc= scov.self + sum(dropped-altscaf-scov))
  #  acov.calc= scov.calc / nb
  my $selfcov= $covaln->{$sid}{'self'} or return 0;
  my($nb,$acov,$scov)= @$selfcov;
  my($scovdrop,$nbdrop,$ndrop)=(0,0,0);
  if($kdtab->{$sid} =~ /drop/) { 
    return($nb,$acov,$scov);
    #?? recalc drop anyway? will it change keep/drop class?
    #? probably recalc is invalid as this id score already removed?
  }
  for my $asc (grep { $_ !~ /(self|any|calc)/ } keys %{ $covaln->{$sid} }) {
    if($kdtab->{$asc} =~ /drop/) {
      my($anb,$aacov,$ascov)= @{$covaln->{$sid}{$asc}};
      $scovdrop += $ascov; $nbdrop += $anb; $ndrop++;
    }
  }
  
  # FIXME: covaln{any} *should* be adjusted for drops (span reduced) but need more data on align spans of drops
  
  my $scovnu= $scov + $scovdrop; 
  my $acovnu= ($nb<1)?0:sprintf "%.2f", $scovnu / $nb; #putv: my $qacov= ($qnb<1)? 0: sprintf "%.2f",$qscov/$qnb;
  $covaln->{$sid}{'calc'}= [$nb,$acovnu,$scovnu];
  return($nb,$acovnu,$scovnu);
}

=item genecopy.sccount

dmag21nsk8maca8mo_genecopy.sccount
dmag21ns8moa_sc000001	   1 alo   1 uniq
dmag21ns8moa_sc000003	 268 aeq   7 ahi  20 alo 286 uniq
dmag21ns8moa_sc000004	  81 aeq  34 ahi  16 alo  86 uniq

=item upd evdgene.sctab

daplx23pbc_asm2dc.evdgene.sctab
daplx23pc2d00000009	33106	5	aeq:0 ahi:0 alo:1 uniq:4 na	Wg*5/5 - Xg*0/5 + Wu*0 - Xu*0	4	ok
daplx23pc2d00000010	25200	5	aeq:0 ahi:1 alo:0 uniq:4 na	Wg*4/5 - Xg*1/5 + Wu*0 - Xu*0	3	ok
daplx23pc2d00000022	29099	3	aeq:0 ahi:2 alo:0 uniq:1 na	Wg*1/3 - Xg*2/3 + Wu*0 - Xu*0	1	ok
daplx23pc2d00000025	17701	2	aeq:0 ahi:1 alo:0 uniq:1 na	Wg*1/2 - Xg*1/2 + Wu*0 - Xu*0	2	ok
daplx23pc2d00000026	16109	1	aeq:0 ahi:1 alo:0 uniq:0 na	Wg*0/1 - Xg*1/1 + Wu*0 - Xu*0	-1	drop

=cut

sub readGenetab {
  my($genecopy)= @_;  #evdgene.sctab format
  my ($ng,$ok,$inh,%genec)=(0); %genec=();
  if(-f $genecopy) {  
    loggit 0, "# readEvdGene.sctab $genecopy \n" if $debug; 
    ($ok,$inh)= openRead($genecopy);
    while(<$inh>){
      next if(/^\W/); my $lin=$_;
      my($sid,@val)=split;
      
      my ($gokdrop) = ($lin =~ m/\b(ok|drop)/)? $1: "0";
      my($guni,$gaeq,$gahi,$galo, $gucg, $gxucg) = 
        map{ my($v)= $lin =~ m/\b$_:(\d+)/ ? $1:0; $v; } qw( uniq aeq ahi alo ucg xucg);
      my $gtot= $guni + $gaeq + $gahi+$galo;
      $genec{$sid}{guni}= $guni;
      $genec{$sid}{gtot}= $gtot;
      $genec{$sid}{gahi}= $gahi;
      $genec{$sid}{gucg}= $gucg;
      $genec{$sid}{gokdrop}= $gokdrop;
      $genec{$sid}{gduphi}= ($gtot > 0 and $gahi/$gtot > $GDUPHI)?1:0; # need only gahi/gtot ?
      $ng++;
    } close($inh);
  }
  return($ng,\%genec);
}

sub readGenetab_OLD {
  my($genecopy)= @_;
  my ($ng,$inh,%genec)=(0); %genec=();
  if(-f $genecopy) {  
    loggit 0, "# readGenetab $genecopy \n" if $debug; 
    open($inh,$genecopy) || die "reading $genecopy"; 
    while(<$inh>){
      next if(/^\W/); my $lin=$_;
      my($sid,@val)=split;
      
      my($guni,$gaeq,$gahi,$galo)= map{ my($v)= $lin =~ m/(\d+) $_/ ? $1:0; $v; } qw( uniq aeq ahi alo);
      my $gtot=$gaeq+$gahi+$galo;
      $genec{$sid}{guni}= $guni;
      $genec{$sid}{gtot}= $gtot;
      $genec{$sid}{gahi}= $gahi;
      $genec{$sid}{gduphi}= ($gtot > 0 and $gahi/$gtot > $GDUPHI)?1:0; # need only gahi/gtot ?
      $ng++;
    } close($inh);
  }
  return($ng,\%genec);
}



sub putkeepdrop {
  my($kdtab,$keepdropf,$covaln,$genec)= @_;
  loggit 0, "# putkeepdrop: $keepdropf\n" if $debug;
  my $outh=*STDOUT;
  if($keepdropf) {
    rename($keepdropf,"$keepdropf.old") if(-s $keepdropf);
    open($outh,'>',$keepdropf);
  }
  my $KBN= $BN/1000;
  my @hdr= qw( ScafID	Class	LenSc	CovSc	Guni	GCeq	AlnSc);
  print $outh join("\t",@hdr)."\n";
  for my $sid (sort keys %$kdtab) {
    my $kdv= $kdtab->{$sid}; 
    #?? not here? if($ncontam and $contamids->{$sid}){ }
    # my ($nb,$acov)=(0,0);    
    my($lensc,$acov,$guni,$geq,$alnsc)=(0) x 9;
    my $cself=  ($covaln) ? $covaln->{$sid}{'calc'} || $covaln->{$sid}{'self'} || [0,0,0] : [0,0,0];
    #old if($cself){ ($nb,$acov)= @$cself; $nb=int( $nb*$KBN); $acov=$acov."c,".$nb."k"; }
    if($cself){ ($lensc,$acov)= @$cself; $lensc=int( $lensc*$BN); }
    my $cany=  ($covaln) ? $covaln->{$sid}{'any'} || [0,0,0] : [0,0,0];
    if($cany and $lensc){ $alnsc= int( 100 * $BN * $cany->[0] / $lensc ); $alnsc=100 if($alnsc>100); } 
    if($genec and exists $genec->{$sid}){ 
      my($gtot,$gahi,$gduphi,$gucg); # gxucg ?
      ($guni,$gtot,$gahi,$gduphi,$gucg)= map{ $genec->{$sid}{$_}||0 } qw(guni gtot gahi gduphi gucg);
      $geq= ($gtot < 1)? 0 : int($gtot/2) - $gahi; #? -n .. 0 .. +n for gahi > half
    }
    
    #old: print $outh join("\t",$sid,$kdv,$acov)."\n";
    print $outh join("\t",$sid,$kdv,$lensc,$acov,$guni,$geq,$alnsc)."\n"; # orig format

  }
  
  close($outh);
}

sub updcovalntab {
  my($covaln,$kdtab,$outf)=@_;
  
  loggit 0, "# updcovalntab: $outf\n" if $debug;
  %qcov=(); # global, for output
  my @sid= sort keys %$covaln; my $ndrop=0;
  for my $sid (@sid) {
    my $drop =  $kdtab->{$sid} || 0;
    if($drop =~ /drop/){ $ndrop++; next; } #what?

    my $cself=  $covaln->{$sid}{'calc'} || $covaln->{$sid}{'self'} || [0,0,0];
    my($nb,$acov,$scov)= @$cself;
    $qcov{$sid}{"self"}{nb}= $nb;  $qcov{$sid}{"self"}{scov} = $scov;
    my $cany =  $covaln->{$sid}{'any'} || [0,0,0];
    ($nb,$acov,$scov)= @$cany;
    $qcov{$sid}{"any"}{nb}= $nb;  $qcov{$sid}{"any"}{scov} = $scov;

    my @qid= sort grep { not( $_ =~ /(self|any|calc)/ or $kdtab->{$_} =~ /drop/) } keys %{ $covaln->{$sid} };
    my $any=0;
    for my $qid (@qid) {
      my($qnb,$qacov,$qscov)= @{$covaln->{$sid}{$qid}};
      $qcov{$sid}{$qid}{nb}=$qnb;
      $qcov{$sid}{$qid}{scov}=$qscov; $any++;
      }
    #?? if($any){ $qcov{$sc}{"any"}{nb}++;  $qcov{$sc}{"any"}{scov}+=$acovm; } # count once; which targ? sc is source for bsb, need that
  }

  # FIXME output new covalntab w/ drops removed, recalc self,any scores
  # reuse this put_scAlnCov(id,qcov)
  my $outh=*STDOUT;
  if($outf) {
    rename($outf,"$outf.old") if(-s $outf);
    open($outh,'>',$outf);
  }
  for my $sid (sort keys %qcov) {
    put_scAlnCov($outh, $sid);
  }
  close($outh);
}

sub reclassCovAln {
  my($sid, $covaln, $kdtab, $ng, $genec)=@_;
  
  my $cself=  $covaln->{$sid}{'calc'} || $covaln->{$sid}{'self'} || [0,0,0];
  my($nb,$acov,$scov)= @$cself;
  #? is acov always valid here? see calcCovaln
  #  my $acovnu= ($nb<1)?0:sprintf "%.2f", $scov / $nb; 
  #?? not here? if($ncontam and $contamids->{$sid}){ }

  my $cany =  $covaln->{$sid}{'any'} || [0,0,0];
  my $aln  = ($scov < 1) ? 0 : 100 * $cany->[2] / $scov;
  
  my $guni= ($ng) ? $genec->{$sid}{guni}||0 : 0;
  my $gucg= ($ng) ? $genec->{$sid}{gucg}||0 : 0;
  my $ucgdup= ($ng) ? $genec->{$sid}{gxucg}||0 : 0;
  my $gduphi= ($ng) ? $genec->{$sid}{gduphi}||0 : 0;
  my $gokdrop= ($ng) ? $genec->{$sid}{gokdrop}||0 : 0;
  my $clasold= $kdtab->{$sid} || "";

  my $clas= ($clasold =~ /drop/) ? $clasold # ?? drops cant be reclassed as scores now invalid ?
      :($acov >= $CHI)?"keep.chi" 
      :($gokdrop eq 'ok') ? "keep.evdgn"
      :($gokdrop eq '0' and ($guni>0 or $gucg>0))?"keep.ucg" 
      :($acov < $CHI and $gduphi)?"drop.halfgdup"  # ** FIXME: need drop.updates for this, skip til have
      :($acov < $CHI and $ucgdup > 0 and $gucg < 1)?"drop.ucgdup"
      :($acov < $CLO and $aln > $ALN)?"drop.lowac" 
      :($acov < $CHI and $aln > $ALN)?"drop.halfac"
      :($acov < $CLO)?"drop.lowc" #? ** keep this one maybe **
      #  :($acov < $CHI)?"drop.halfc" # not this one? require align now
      :"unk";
  
  # my $clas= ($clasold =~ /drop/) ? $clasold
  #     :($acov >= $CHI)?"keep.chi" : ($guni>0)?"keep.ugc" 
  #     :($acov < $CLO and $aln > $ALN)?"drop.lowac" 
  #     :($acov < $CHI and $aln > $ALN)?"drop.halfac"
  #     #  :($acov < $CHI and $gduphi)?"drop.halfgdup"  # ** FIXME: need drop.updates for this, skip til have
  #     #  :($acov < $CHI and $ucgdup > 0)?"drop.ucgdup"
  #     :($acov < $CLO)?"drop.lowc" #? ** keep this one maybe **
  #     #  :($acov < $CHI)?"drop.halfc" # not this one? require align now
  #     :"unk";

  # my $clasold= $kdtab->{$sid} || "";
  # $kdtab->{$sid}=$clas; # replace clasold existing?
  return($clas);
}

sub makekeepdrop {
  my($incovaln,$genecopy)= @_;
  
  my($ndrop)= (0) x 9;
  my($nid,$covaln)= readCovaln($incovaln);
  my($ng,$genec)= ($genecopy) ? readGenetab($genecopy) : (0);

  my($nkd,$kdtab)= (0, {}); # make keepdrop tab here
  
  # ($nkd,$kdtab)= ($keepdropids)? readids($keepdropids,$isdrop) :(0); #?
  my(%changeid);
  
  my @sid= sort { $covaln->{$a}{'self'}->[2] <=> $covaln->{$b}{'self'}->[2] } keys %$covaln; # sort by low-high sum score, score= acov x genescore
  for my $sid (@sid) {
  
    #instead: my($clas)= reclassCovAln( $sid, $covaln, $kdtab, $ng, $genec);
    #?? not here? if($ncontam and $contamids->{$sid}){ }
    
    my $cself=  $covaln->{$sid}{'calc'} || $covaln->{$sid}{'self'} || [0,0,0];
    my($nb,$acov,$scov)= @$cself;
    my $cany =  $covaln->{$sid}{'any'} || [0,0,0];
    my $aln  = ($scov < 1) ? 0 : 100 * $cany->[2] / $scov;
    
    my $guni= ($ng) ? $genec->{$sid}{guni}||0 : 0;
    my $gucg= ($ng) ? $genec->{$sid}{gucg}||0 : 0;
    my $ucgdup= ($ng) ? $genec->{$sid}{gxucg}||0 : 0;
    my $gduphi= ($ng) ? $genec->{$sid}{gduphi}||0 : 0;
    my $gokdrop= ($ng) ? $genec->{$sid}{gokdrop}||"0" : "0";
    
    my $clasold= $kdtab->{$sid} || ""; 
    my $clas= ($clasold =~ /drop/) ? $clasold # ?? drops cant be reclassed as scores now invalid ?
        :($acov >= $CHI)?"keep.chi" 
        :($gokdrop eq 'ok') ? "keep.evdgn"
        :($gokdrop eq '0' and ($guni>0 or $gucg>0))?"keep.ucg" 
        :($acov < $CHI and $gduphi)?"drop.halfgdup"  # ** FIXME: need drop.updates for this, skip til have
        :($acov < $CHI and $ucgdup > 0 and $gucg < 1)?"drop.ucgdup"
        :($acov < $CLO and $aln > $ALN)?"drop.lowac" 
        :($acov < $CHI and $aln > $ALN)?"drop.halfac"
        :($acov < $CLO)?"drop.lowc" #? ** keep this one maybe **
        #  :($acov < $CHI)?"drop.halfc" # not this one? require align now
        :"unk";
         
    $kdtab->{$sid}=$clas; # replace clasold existing?
    
    if($clas =~ /drop/) {
      $ndrop++; # ?? recalc of sid containing align todrop sid
      my @aid = grep { $_ !~ /(self|any|calc)/ } keys %{$covaln->{$sid}};
      for my $aid (@aid) {
        $changeid{$aid}++;
        my($anb,$acovnu,$scovnu)= calcCovaln($aid, $covaln, $kdtab);
        #^ need to reclass these, defer to end/output?
      }
    }
  }
  
  for my $sid (sort keys %changeid) {
    my($clas)= reclassCovAln( $sid, $covaln, $kdtab, $ng, $genec);
    $kdtab->{$sid}=$clas;
  }
  
  return($nid,$ndrop,$covaln,$kdtab,$ng,$genec); # putkeepdrop($kdtab) here?
}


=item  makecovalntab($asm, $inselfaln, $incovtab);

=cut

sub readAln_nucmer {
  my($inh)=@_;
  my $nal=0; #globals: %aln
  while(<$inh>) {
    next if(/^\W|^NUC/); 
    if(/^\d/){ # nucmer show-align.pidcoords
      my @v=split;
      my($rb,$re,$qb,$qe,$ral,$qal,$pid,$rlen,$qlen,$rcov,$qcov,$rid,$qid)=@v; 

      #??here? 
      if($ncontam and ($contamids->{$rid} or $contamids->{$qid})){ next; }

      $nal++; my $or=1; if($qb > $qe) { ($qb,$qe,$or)=($qe,$qb,-1); }
      my($brb,$bre,$bqb,$bqe)= map{ int($_/$BN) } ($rb,$re,$qb,$qe);
      for(my $ib=$brb; $ib<=$bre; $ib++) { $aln{$rid}{$qid}{$ib}++; } 
      for(my $ib=$bqb; $ib<=$bqe; $ib++) { $aln{$qid}{$rid}{$ib}++; } #? both ways or 1?
      # note: base-val: $ibout=$BN*(1+$ib); ie ib==0 => 100, ib==1 => 200 .. so i == int(ibout/BN) - 1
    }
  }
  return($nal, \%aln);
}

sub readAln_blast {
  my($inh)=@_;
  my ($nal,$lrid,$lqid)=(0); #globals: %aln
  my $MINALN=$ENV{minaln} || 499; #?? maybe also collect all qid x rid1, limit minalnr for altotal/rid1 ?
  while(<$inh>) {
    # if(/^# Query: (\S+)/){ 
    #   # puth($sid,$sw,$trd,$tal,$tsb,$tse) if($trd and $tal); 
    #   # $sid=$1; ($sw)=m/(?:length|len)=(\d+)/; unless($sw){ $sw=($sid =~ /LG/)?999999:1999; }  
    #   # $trd=$tal=0; $tsb=$tse=0; 
    # } elsif(/^#/){}
    if(/^\w/){  
      my @v=split;
      my($qid,$rid,$pid,$aln,$mism,$ndel,$qb,$qe,$rb,$re,$eval,$bits)=@v;
      my $or=1; if($rb > $re) { ($rb,$re,$or)=($re,$rb,-1); }
      next if($qid eq $rid and $qb <= $re and $qe >= $rb); #? want same-scaf-aligns but not overlapped?
      next if($aln < $MINALN);
      if($ncontam and ($contamids->{$rid} or $contamids->{$qid})){ next; }
      # unless($qid eq $lqid and $rid eq $lrid){  addal($lqid,$lrid,@qral); $taln=0; @qral=(); } 
      # push @qral, [@v]; $taln+=$aln;
      # sub addal{ .. }
      my($brb,$bre,$bqb,$bqe)= map{ int($_/$BN) } ($rb,$re,$qb,$qe);
      for(my $ib=$brb; $ib<=$bre; $ib++) { $aln{$rid}{$qid}{$ib}++; } 
      for(my $ib=$bqb; $ib<=$bqe; $ib++) { $aln{$qid}{$rid}{$ib}++; } #? both ways or 1?
      $nal++; $lrid=$rid; $lqid=$qid;
    } 
  }
  return($nal, \%aln);
}

sub readCovtab_gnodes {
  my($inh, $alnh)=@_;
  my ($nbany,$nbin)=(0,0); #globals: %aln %qcov
  while(<$inh>) {
    if(/^\w/) { # gnodes_dna.covtab
      my @v=split;
      my($sc,$sb,$ccov,$tcov,$ucov,$acovt,$acovm,$acovu)=@v;
      my $bsb=  int($sb/$BN) - 1; # drop 00
      if($ncontam and $contamids->{$sc}){ next; } #? dont need to test @qid= aln{sc} as read_aln drops contam
      $qcov{$sc}{"self"}{nb}++;  $qcov{$sc}{"self"}{scov}+=$acovm;  $nbin++;
      if(defined $aln{$sc} ) { # ??? need pairwise align here: $alnb{sc}{qid}{bsb}
        my @qid= sort keys %{$aln{$sc}}; my $any=0;
        for my $qid (@qid) { 
          # here? add "any"/all for qcov{qid} all-align span?
          if($aln{$sc}{$qid}{$bsb}) {
            #UPD2303: this is wrong?? hash keys:  $qcov{$sc}{$qid}  not $qcov{$qid}{$sc}
            # BUT both sc and qid are scaf ids from @aln{sc}, above self-align table, so rev order here is wanted?
            if(0) { # NOT UPD2303
            $qcov{$sc}{$qid}{nb}++; $qcov{$sc}{$qid}{scov}+=$acovm; $any++;
            } else { # orig: this is proper hash key order?  
            $qcov{$qid}{$sc}{nb}++; $qcov{$qid}{$sc}{scov}+=$acovm; $any++;
            }
            }
          }
        if($any){ $qcov{$sc}{"any"}{nb}++;  $qcov{$sc}{"any"}{scov}+=$acovm; $nbany++; } # count once; which targ? sc is source for bsb, need that
        }
      } 
  }
  return($nbany,$nbin,\%qcov);
}

sub make_covalntab { # orig algo
  my($asmfa, $inselfaln, $incovtab, $outtab)= @_; # UPD2303: alntab.blastn choice now?

  my $ok=1; my $inselfh=*STDIN;
  if($inselfaln) { ($ok,$inselfh)= openRead($inselfaln);  }
  my @intest;
  unless($ALN_FORMAT){
    if($inselfaln =~ /blast/i){ $ALN_FORMAT="blast"; }
    elsif($inselfaln =~ /nucmer|coords/i){ $ALN_FORMAT="nucmer"; } 
    else {      
      for(my $i=0; $i<2; $i++) {
        my $inl= <$inselfh>; push @intest, $inl;
        if($inl =~ /blast/i){ $ALN_FORMAT="blast"; last; }
        elsif($inl =~ /nucmer|coords/i){ $ALN_FORMAT="nucmer"; last; } 
      }
    }
  }
  
  my($naln,$alnh)=(0, {});
  if($ALN_FORMAT =~ /blast/i) {
    ($naln,$alnh)= readAln_blast($inselfh, \@intest);
  } elsif($ALN_FORMAT =~ /nucmer/i) {
    ($naln,$alnh)= readAln_nucmer($inselfh, \@intest);
  } else {
    loggit 1, "#ERR: dont know align format of $inselfaln; -alnformat=blastn|nucmer"; return 0;
  }
  loggit 0, "readAln_$ALN_FORMAT($inselfaln) n=$naln" if $debug;
  
  $ok=1; my $incovh=*STDIN;
  if($incovtab) { ($ok,$incovh)= openRead($incovtab);  }
  my($nbany,$nbin,$qcovh)= readCovtab_gnodes($incovh, $alnh);
  loggit 0, "readCovtab_gnodes($incovtab) n=$nbany" if $debug;
 
  my $outh= *STDOUT; # fixme option
  if($outtab) {
    rename($outtab,"$outtab.old") if(-s $outtab);
    open($outh,'>',$outtab);
  }
  my $ntab=0;
  for my $sid (sort keys %qcov) {
    $ntab+= put_scAlnCov($outh, $sid); 
  }

  loggit 0, "make_covalntab() n=$ntab $outtab" if $debug;
  return($ntab, $outtab);
}
  
sub make_covalntab_OLD { # orig algo
  while(<>) {
    next if(/^\W|^NUC/); 
    my @v=split;
    if(/^\d/){ # nucmer show-align.pidcoords
      my($rb,$re,$qb,$qe,$ral,$qal,$pid,$rlen,$qlen,$rcov,$qcov,$rid,$qid)=@v; 
      # ($rid,$qid)=@v[-2,-1];
      # push @{ $aln{$rid}{$qid}}, [$rb,$re,$qb,$qe]; #? dont need or just rid,qid pairing: 
      my $or=1; if($qb > $qe) { ($qb,$qe,$or)=($qe,$qb,-1); }
      my($brb,$bre,$bqb,$bqe)= map{ int($_/$BN) } ($rb,$re,$qb,$qe);
      for(my $ib=$brb; $ib<=$bre; $ib++) { $aln{$rid}{$qid}{$ib}++; } 
      for(my $ib=$bqb; $ib<=$bqe; $ib++) { $aln{$qid}{$rid}{$ib}++; } #? both ways or 1?
      # for($ib=$brb; $ib<=$bre; $ib++) { $alnb{$rid}{$ib}++; }
      # for($ib=$bqb; $ib<=$bqe; $ib++) { $alnb{$qid}{$ib}++; }
      # note: base-val: $ibout=$BN*(1+$ib); ie ib==0 => 100, ib==1 => 200 .. so i == int(ibout/BN) - 1
      
    } elsif(/^\w/) { # gnodes_dna.covtab
      my($sc,$sb,$ccov,$tcov,$ucov,$acovt,$acovm,$acovu)=@v;
      my $bsb=  int($sb/$BN) - 1; # drop 00
      # if($sc ne $lsc) { put_scAlnCov($lsc);  $scovm=$nb=0; }
      # $scovm+= $acovm; $nb++; $lsc=$sc;
      $qcov{$sc}{"self"}{nb}++;  $qcov{$sc}{"self"}{scov}+=$acovm;
      if(defined $aln{$sc} ) { # BAD need pairwise align here: $alnb{sc}{qid}{bsb}
        my @qid= sort keys %{$aln{$sc}}; my $any=0;
        for my $qid (@qid) { 
          # here? add "any"/all for qcov{qid} all-align span?
          if($aln{$sc}{$qid}{$bsb}) {
            #UPD2303: this is wrong?? hash keys:  $qcov{$sc}{$qid}  not $qcov{$qid}{$sc}
            # BUT both sc and qid are scaf ids from @aln{sc}, above self-align table, so rev order here is wanted?
            if(0) { # NOT UPD2303
            $qcov{$sc}{$qid}{nb}++; $qcov{$sc}{$qid}{scov}+=$acovm; $any++;
            } else { # orig: aint this wrong hash key order? maybe not
            $qcov{$qid}{$sc}{nb}++; $qcov{$qid}{$sc}{scov}+=$acovm; $any++;
            }
            }
          }
        if($any){ $qcov{$sc}{"any"}{nb}++;  $qcov{$sc}{"any"}{scov}+=$acovm; } # count once; which targ? sc is source for bsb, need that
        }
      }  
  }

  # END
  my $outh= *STDOUT; # fixme option
  for my $sid (sort keys %qcov) {
    put_scAlnCov($outh, $sid);
  }
  
}

=item any/all align span needed

 grep  '^dmag21ns8moa_sc007190'  dmag21nsk8maca8mo.covalntab     
dmag21ns8moa_sc007190	self	563	14.90	8387
dmag21ns8moa_sc007190	any	  300-500?	14.90	8387 << insert here, sum span for all below
dmag21ns8moa_sc007190	dmag21ns8moa_sc004350	288	11.41	3285
dmag21ns8moa_sc007190	dmag21ns8moa_sc000005	115	17.85	2053
dmag21ns8moa_sc007190	dmag21ns8moa_sc001238	114	17.87	2037
...

=cut


sub put_scAlnCov { # was putv
  my($outh,$sc)= @_;
  # my $acovm= ($nb<1)? 0: sprintf "%.2f",$scovm/$nb;
  # print join("\t",$sc,"self",$nb,$acovm,$scovm,)."\n";
  my @qid= ($qcov{$sc}) ? keys %{$qcov{$sc}} : ();
  @qid= grep{ $_ !~ /^(self|any)$/ } @qid; # $_ ne "self"
  @qid= sort{ $qcov{$sc}{$b}{nb} <=> $qcov{$sc}{$a}{nb} } @qid; #? add sort by {scov} for same nb?
  my $nout=0;
  print  $outh join("\t",qw(ScafID QueryID Nbin ACovM SumCovM))."\n" if(1>$hdr++);
  for my $qid ("self","any",@qid) {
    my $qnb=  $qcov{$sc}{$qid}{nb}||0;  
    my $qscov=$qcov{$sc}{$qid}{scov}||0;  
    my $qacov= ($qnb<1)? 0: sprintf "%.2f",$qscov/$qnb;  $qscov= sprintf "%.2f",$qscov;
    print $outh join("\t",$sc,$qid,$qnb,$qacov,$qscov,)."\n"; $nout++;
  }
  return($nout);
}

#=========================

=item make_asm_gene_evd / make_genecopy

daphmag/dmag21nsk8asm.info
------------

chr.sc x gene.id copyn from 
  gene7finloc_best1all.alntab: GenomeID(scf:1), AqID(gid:4), cov,pid? npath? : count chr gdups from gids, not npath
  dmag7fincds_SRR15012074_b2_bwa_up8u.genexcopy: Gene_ID,tCopy cols 1,4

# maca8mo.prime.sc vs alt.sc : best gene map table
n=1053 loci on ns=569 alt.sc, vs 33405 loci on ns=901 prime.sc

cat dmag7finloc9b-dmag21nsk8maca8mo_*.align.tab | cut -f1-4,7-12,19  | \
 sort -k4,4 -k6,6nr -k11,11 | \
perl -ne '@v=split; if(/^Genome/){ print if(1>$dhd++); next; } else { ($sc,$sbe,$so,$id)=@v; $t=$v[-1]; if($id ne $lid){ putv($lid) if(@gv); @gv=(); } push @gv,[@v];  $lid=$id; $lt=$t; } sub putv{ my $gva=$gv[0]; my($sc,$sbe,$so,$id,$tw,$cov,$pid,$npa,$ndel,$nex,$tag)=@{$gva}; $tag=$gva->[-1]; if($tag eq "gmap"){ $gsc=$cov*$pid/100; ($pv)=grep{ $_->[-1] ne $tag } @gv; $psc=($pv)? $pv->[5]*$pv->[6]/100 :0; $oktag=($gsc * 0.90 > $psc)? $tag :"gm1sc"; } else { $oktag="$tag"; } @okv=grep{ $_->[-1] eq $oktag } @gv; map{ print join("\t",@$_)."\n" }@okv; } END{ putv($lid); }' \
 > dmag7finloc9b-dmag21nsk8maca8mo_best1all.alntab == dmag21nsk8maca8mo_gene7finloc_best1all.alntab

# ----

=item make_genecopy UPD2303

sort -k4,4 -k7,7nr -k8,8nr -k1,1 daplx23pbc_asm2dc_dp17evgt1m.align.tab | \
 cat gnodesf/daphplx17evgt1m_cds_SRR2148375_b2_mim.genexcopy - | \
 perl -ne 'next if(/^\W/); ($d,$e)=@v=split; if(@v<15){
 ($id,$tw,$nrd,$rdl,$tc,$cm,$ucla,$mer,$cnz)=@v; 
 $ucg=(m/UCG/ and $tc < 1.5)?"ucg":""; $genecn{$id}=$tc.$ucg;
 $genex{$id}=[$tw,$nrd,$tc,$cm,$ucla,$cnz,$ucg];  } else {
 ($sc,$sbe,$so,$gid,$qspa,$qmat,$tw,$cov,$pid,$npath,$ndel,$nex)=@v;
 $tag=$v[-1]; next if($sc eq "NOPATH"); push @{$scaf{$gid}},$sc; push
 @{$gmap{$gid}}, [$sc,$sbe,$so,$tw,$cov,$pid,$npath,$nex,$tag]; } END{
 for $gid (sort keys %genecn) { $gcn=$genecn{$gid}; @scaf=@gmap=();
 if($scaf{$gid}) { @scaf= @{$scaf{$gid}}; @gmap=@{$gmap{$gid}}; }
 $scn=@scaf; unless(@scaf){ push @scaf,"nopath"; } $acla=($scn >
 1.75*$gcn)?"ahi":($scn < 0.66*$gcn)?"alo":"aeq"; if($scn < 1.5) {
 if($acla=~/aeq|alo/){ $acla="uniq"; } else { $acla="unip"; } } 
 for($i=0; $i<=$#scaf; $i++){ print join("\t",qw(GeneID Cgene Aclass
 Casm ScafID ScRank))."\n" if(1>$hd++); print
 join("\t",$gid,$gcn,$acla,$scn,$scaf[$i],1+$i)."\n"; } } } ' \
 > daplx23pbc_asm2dc_dp17evgt1m_genecopy.alntab

      1 Aclass
   3265 aeq  |  ok dups
   6443 ahi  |  extra dups
   1385 alo  |  missing dups
   2512 unip : 1 scaf partial-copy, genex < 1
  26000 uniq : 1 scaf copy, genex=1

.. add genex ucg info: $ucg=(m/UCG/ and $tc < 1.5)?"ucg":""; $genecn{$id}=$tc.$ucg;
 grep ucg daplx23pbc_asm2dc_dp17evgt1m_genecopy.alntabc | sort -k2,2nr | cut -f3 | sort | uniq -c
     40 aeq 220 ahi  1 unip  707 uniq
Daplx7pEVm000650t1	1.4ucg	uniq	1	daplx23pc2d00007365	1
Daplx7pEVm001665t1	1.4ucg	aeq	2	daplx23pc2d00001388	1
Daplx7pEVm001665t1	1.4ucg	aeq	2	daplx23pc2d00005519	2
..
Daplx7pEVm006129t1	1.0ucg	uniq	1	daplx23pc2d00004571	1
Daplx7pEVm006191t1	1.0ucg	ahi	2	daplx23pc2d00001766	1
Daplx7pEVm006191t1	1.0ucg	ahi	2	daplx23pc2d00006802	2
-----

grep ahi daplx23pbc_asm2dc_dp17evgt1m_genecopy.alntab | head
Daplx7pEVm000274t1	1.0	ahi	2	daplx23pc2d00007956	1
Daplx7pEVm000274t1	1.0	ahi	2	daplx23pc2d00008282	2
Daplx7pEVm000380t1	1.1	ahi	2	daplx23pc2d00000510	1
Daplx7pEVm000380t1	1.1	ahi	2	daplx23pc2d00004839	2
Daplx7pEVm000461t1	1.0	ahi	2	daplx23pc2d00004779	1
Daplx7pEVm000461t1	1.0	ahi	2	daplx23pc2d00006924	2

grep ahi daplx23pbc_asm2dc_dp17evgt1m_genecopy.alntab | cut -f5 | sort | uniq -c | sort -k1,1nr | head
     20 daplx23pc2d00004979 :  3 aeq 20 ahi 1 alo 2 unip 107 uniq
     19 daplx23pc2d00005356
     16 daplx23pc2d00004976
     16 daplx23pc2d00005346
     16 daplx23pc2d00005759
     16 daplx23pc2d00006798
     15 daplx23pc2d00004744
     15 daplx23pc2d00005036
     15 daplx23pc2d00005156
     15 daplx23pc2d00005281
grep ahi daplx23pbc_asm2dc_dp17evgt1m_genecopy.alntab | cut -f5 | sort | uniq -c | sort -k1,1nr | wc 
   2590   : contigs need filtering, 1/2 valid?

ahi      20 daplx23pc2d00004979 :  3 aeq 20 ahi 1 alo 2 unip 107 uniq << many valid genes, 20 xdups, on 7 other ctgs
      5 daplx23pc2d00000406
      7 daplx23pc2d00000411
      3 daplx23pc2d00000414
      1 daplx23pc2d00000657
      6 daplx23pc2d00004981
      2 daplx23pc2d00004983
      4 daplx23pc2d00005250

=item mkgc more trials

sort -k4,4 -k7,7nr -k8,8nr -k1,1 daplx23pbc_asm2dc_dp17evgt1m.align.tab | \
cat gnodesf/daphplx17evgt1m_cds_SRR2148375_b2_mim.genexcopy - | perl -ne 'next if(/^\W/); ($d,$e)=@v=split;
if(@v<15){ ($id,$tw,$nrd,$rdl,$tc,$cm,$ucla,$mer,$cnz)=@v; $ucg=(m/UCG/ and $tc < 1.5)?"ucg":"";
$genecn{$id}=$tc.$ucg; $genex{$id}=[$tw,$nrd,$tc,$cm,$ucla,$cnz];  } else {
($sc,$sbe,$so,$gid,$qspa,$qmat,$tw,$cov,$pid,$npath,$ndel,$nex)=@v; $tag=$v[-1]; next if($sc eq "NOPATH"); push
@{$scaf{$gid}},$sc; push @{$gmap{$gid}}, [$sc,$sbe,$so,$tw,$cov,$pid,$npath,$nex,$tag]; } END{ for $gid (sort
keys %genecn) { $gcn=$genecn{$gid}; @scaf=@gmap=(); if($scaf{$gid}) { @scaf= @{$scaf{$gid}};
@gmap=@{$gmap{$gid}}; } $scn=@scaf; unless(@scaf){ push @scaf,"nopath"; } $acla=($scn > 1.75*$gcn)?"ahi":($scn <
0.66*$gcn)?"alo":"aeq"; if($scn < 1.5) { if($acla=~/aeq|alo/){ $acla="uniq"; } else { $acla="unip"; } } 
for($i=0; $i<=$#scaf; $i++){ print join("\t",qw(GeneID Cgene Aclass Casm ScafID ScRank))."\n" if(1>$hd++); print
join("\t",$gid,$gcn,$acla,$scn,$scaf[$i],1+$i)."\n"; } } } ' \
  > daplx23pbc_asm2dc_dp17evgt1m_genecopy.alntab


perl -ne 'use constant { Wu=>8, Xu=>2, Wg=>4, Xg=>1 }; next if(/^\W|^GeneID/); @v=split; if(@v==8){
($sc,$sw)=@v[0,1]; $sw{$sc}=$sw; next; } ($gd,$gc,$acl,$nsc,$sc,$sr)=@v; $acl=~s/unip/uniq/; $ucg="na";
if($gc=~/ucg/){ $ucg=($acl=~/ahi/)?"xucg":"ucg";  } $scg{$sc}{$acl}++; $scg{$sc}{$ucg}++; $acl{$acl}++;
$ucg{$ucg}++;  END{ @acl=sort keys %acl; @ucg=qw(ucg xucg); for $sc (sort keys %scg){ my($cl,$uc,$nc,$nu,$c)=(0)
x 9; $cl= join" ",map{ $c=$scg{$sc}{$_}||0; $nc+=$c; "$_:$c"; } @acl; $uc= join" ", grep/:/, map{
$c=$scg{$sc}{$_}; $nu+=$c; ($c)?"$_:$c":""; } @ucg;  $uc||="na"; $sw=$sw{$sc}||0; $vuni=$scg{$sc}{uniq};
$vhi=$scg{$sc}{ahi}||0; $vxu=$scg{$sc}{xucg}||0; $wg=$nc-$vhi; $wu=$nu-$vxu; $wt="Wg*$wg/$nc - Xg*$vhi/$nc +
Wu*$wu - Xu*$vxu"; $wtc= Wg*$wg/$nc - Xg*$vhi/$nc + Wu*$wu - Xu*$vxu; $ok=($wtc<0 and
$vuni>0)?"okrescu":($wtc<0)?"drop":"ok"; if($ok eq "drop"){ $tdrop+=$sw; $ndrop++; } else { $tok+=$sw; $nok++; }
$wtv=sprintf"%.0f",$wtc; print join("\t",$sc,$sw,$nc,"$cl $uc", $wt, $wtv, $ok)."\n"; } print "#total nok=$nok,
tok=$tok, ndrop=$ndrop, tdrop=$tdrop\n"; } ' \
 daplx23pbc_asm2d.facount daplx23pbc_asm2dc_dp17evgt1m_genecopy.alntab \
  >  daplx23pbc_asm2dc_dp17evgt1m_genecopy.scoreb

daplx23pbc_asm2dc_dp17evgt1m_genecopy.score
#total nok=3533, tok=194765094, ndrop=545, tdrop=9955808
.. rescue all w/ uniq genes but xdup wtc < 0 ..
daplx23pbc_asm2dc_dp17evgt1m_genecopy.scoreb
#total nok=3585, tok=196557636, ndrop=493, tdrop=8163266

grep rescu | tail; echo daplx23pbc_asm2dc_dp17evgt1m_genecopy.score
daplx23pc2d00006472	21116	6	aeq:0 ahi:5 alo:0 uniq:1 na	Wg*1/6 - Xg*5/6 + Wu*0 - Xu*0	-0	okrescu
daplx23pc2d00006649	27684	7	aeq:1 ahi:4 alo:0 uniq:2 xucg:1	Wg*3/7 - Xg*4/7 + Wu*0 - Xu*1	-1	okrescu
daplx23pc2d00006662	22107	6	aeq:0 ahi:5 alo:0 uniq:1 na	Wg*1/6 - Xg*5/6 + Wu*0 - Xu*0	-0	okrescu
daplx23pc2d00006748	101226	9	aeq:0 ahi:5 alo:0 uniq:4 xucg:1	Wg*4/9 - Xg*5/9 + Wu*0 - Xu*1	-1	okrescu
daplx23pc2d00006980	31038	6	aeq:0 ahi:5 alo:0 uniq:1 na	Wg*1/6 - Xg*5/6 + Wu*0 - Xu*0	-0	okrescu
daplx23pc2d00007056	19124	7	aeq:0 ahi:6 alo:0 uniq:1 na	Wg*1/7 - Xg*6/7 + Wu*0 - Xu*0	-0	okrescu
daplx23pc2d00007105	107736	21	aeq:3 ahi:9 alo:0 uniq:9 xucg:1	Wg*12/21 - Xg*9/21 + Wu*0 - Xu*1	-0	okrescu


=item mkgc etc
#-------
sort -k4,4 -k6,6nr -k7,7nr -k1,1  dmag21nsk8maca8mo_gene7finloc_best1all.alntab | \
 cat dmag7fincds_SRR15012074_b2_bwa_up8u.genexcopy - |\
 perl -ne 'next if(/^\W/); ($d,$e)=@v=split; 
if(/^Dapma7bEV/){ ($id,$tw,$nrd,$rdl,$tc,$cm,$ucla,$mer,$cnz)=@v; $genecn{$id}=$tc; $genex{$id}=[$tw,$nrd,$tc,$cm,$ucla,$cnz]; }
elsif(/^dmag/){ ($sc,$sbe,$so,$gid,$tw,$cov,$pid,$npath,$ndel,$nex,$tag)=@v; next if($sc eq "NOPATH"); 
push @{$scaf{$gid}},$sc; push @{$gmap{$gid}}, [$sc,$sbe,$so,$tw,$cov,$pid,$npath,$nex,$tag]; }
END{ for $gid (sort keys %genecn) { 
  $gcn=$genecn{$gid}; $gex=$genex{$gid}; 
  @scaf=@gmap=(); if($scaf{$gid}) { @scaf= @{$scaf{$gid}}; @gmap=@{$gmap{$gid}}; }
  $scn=@scaf; unless(@scaf){ push @scaf,"nopath"; }
  $acla=($scn > 1.75*$gcn)?"ahi":($scn < 0.66*$gcn)?"alo":"aeq";
  for($i=0; $i<=$#scaf; $i++){ 
    print join("\t",qw(GeneID Cgene Aclass Casm ScafID ScRank))."\n" if(1>$hd++);
    print join("\t",$gid,$gcn,$acla,$scn,$scaf[$i],1+$i)."\n";
    # add info from gex or gmap? flag for gcn <> scn ?
} } }' \
 > dmag21nsk8maca8mo_genecopy.alntab

perl -pe '($id,$cg,$acl,$ca,$sc,$si)=@v=split; if($ca == 1 and $acl=~/aeq|alo/){ 
  $v[2]="uniq";  print join("\t",@v)."\n"; }'  dmag21nsk8maca8mo_genecopy.alntab | \
 cut -f3,5 | sort -k2,2 -k1,1 | uniq -c | perl -ne '($n,$ac,$sc)=split;
($nac,$sc)=m/^(.+)\s+(dmag\S+)/; print "\n$sc\t" if($lsc ne $sc); print $nac; 
$lsc=$sc; END{ print "\n"; }' \
  > dmag21nsk8maca8mo_genecopy.sccount
  
dmag21ns8moa_sc000001	   1 alo   1 uniq
dmag21ns8moa_sc000003	 268 aeq   7 ahi  20 alo 286 uniq
dmag21ns8moa_sc000004	  81 aeq  34 ahi  16 alo  86 uniq
dmag21ns8moa_sc000005	   1 ahi   4 alo   1 uniq
dmag21ns8moa_sc000006	  33 aeq   2 ahi  21 alo  27 uniq

    1362 dmag21nsk8maca8mo_genecopy.sccount
     809   aeq   grep -c aeq dmag21nsk8maca8mo_genecopy.sccount 
     743   ahi
     855   alo
     833   uniq  : ~500 are on low-cov scafs

=cut

sub make_asm_gene_evd { # orig:make_genecopy 
  my($aname,$ingenex,$ingenealn,$asmsizef)= @_;
  # add this from above scripts, add  asm.facount input 

  my $outsctab= "$EGtag$aname.evdgene.sctab";
  my $outgcnaltab= "$EGtag$aname.genecopy.alntab"; #? rename genecnal.gtab
  if( $REUSE and -s $outsctab) { return(1, $outsctab, 1, $outgcnaltab); }
   
  my($ngenex, $gcntab, $gxtab, $nucg,$cucg)= read_genexcopy($ingenex);  
  if($cucg > 0) {
    # reset CLO, CHI, ..
    # my $CLO=$ENV{clo}||15; # try lower? 10? for drop w/o align
    # my $CHI=$ENV{chi}||35; # try 40? for CU == 50..55
    my $nCLO= int(0.5 + $cucg * 0.25);
    my $nCHI= int(0.5 + $cucg * 0.80);
    loggit 0,"genex reset dna cover depth Clow($CLO => $nCLO, Chigh($CHI => $nCHI) from Cucg=$cucg";
    $CLO= $nCLO; $CHI=$nCHI;
  }
    # ($ngene, \%genecn, \%genextab, $nucg, $cucg)=read_genexcopy(gnodesf/daphplx17evgt1m_cds_SRR2148375_b2_mim.genexcopy)
  
  my($ngalign, $gnscafs, $gnmap)= read_genealign($ingenealn);  # daplx23pbc_asm2dc_dp17evgt1m.align.tab
  
  my($nfa,%fasize)=(0);
  if(-f $asmsizef){
    my($ok,$inh)= openRead($asmsizef); 
    while(<$inh>){ next if(/^\W|^total/); my($id,$sw)=split; $fasize{$id}=$sw; $nfa++; } close($inh); 
  }
  
  my($ngev, $gcnaltab)=(0);
  if($ngenex and $ngalign) {  
    ($ngev, $gcnaltab)= geneasm_cnal_tab($gcntab, $gnscafs, $outgcnaltab, \%fasize); # table of(GeneID Cgene Aclass Casm ScafID ScRank)
    #    # daplx23pbc_asm2dc_dp17evgt1m_genecopy.alntab
  }
  
  # FIXME: want asm.facount input here ..
  my($nsc, $asmevdtab)= asm_evgene_tab( $gcnaltab, $outsctab, \%fasize); # table of(ScafID Sclen nHiEqLoGenes nUCGenes ScafScore ScafOkDrop)
  #   # daplx23pbc_asm2dc_dp17evgt1m_genecopy.score
  
  loggit 0,"make_asm_gene_evd($aname,$ingenex,$ingenealn,$asmsizef) nsc=$nsc, ngev=$ngev"  if($debug);
  
  return($nsc, $asmevdtab, $ngev, $gcnaltab);
}

sub remake_asm_gene_evd { # update after drop scafs .. BUT see calcCovAln() for per-scaf-drop updates of covaln vals
  my($aname,$scafoklist,$ingcnaltab,$fasize)= @_;
  $fasize={} unless($fasize); #fixme 

  my $outsctab= "$EGtag$aname.evdgene.sctab";
  my $outgcnaltab= "$EGtag$aname.genecopy.alntab";
  # if( $REUSE and -s $outsctab) { return(1, $outsctab, 1, $outgcnaltab); }
 
  my($ok,$inh)= openRead($ingcnaltab); 
  unless($ok) { loggit( 1, "Fail to read $ingcnaltab"); return (0,0); }
  my (%gcntab, %gnscafs, %scg,%acl,%ucg,%sw); 
  my($ngenex);
  while(<$inh>) {
    next if(/^\W|^GeneID/);
    my @v=split; 
    my($gd,$gcn,$acl,$nsc,$sc,$sr)=@v; $acl=~s/unip/uniq/; my $ucg="na";
    next unless($scafoklist->{$sc});
    if($gcn=~/ucg/){ $ucg=($acl=~/ahi/)?"xucg":"ucg";  } 
    push @{$gnscafs{$gd}}, $sc;
    $gcntab{$gd}= $gcn; $ngenex++;
    
   # $scg{$sc}{$acl}++; $scg{$sc}{$ucg}++; $acl{$acl}++;
   # $ucg{$ucg}++;  
  } close($inh);
  
  my $gcntab= \%gcntab; my $gnscafs= \%gnscafs;
  my($ngev, $gcnaltab)=(0);
  if($ngenex) {  # and $ngalign
    ($ngev, $gcnaltab)= geneasm_cnal_tab($gcntab, $gnscafs, $outgcnaltab, $fasize); # table of(GeneID Cgene Aclass Casm ScafID ScRank)
    #    # daplx23pbc_asm2dc_dp17evgt1m_genecopy.alntab
  }
  
  # FIXME: want asm.facount input here ..
  my($nsc, $asmevdtab)= asm_evgene_tab( $gcnaltab, $outsctab, $fasize); # table of(ScafID Sclen nHiEqLoGenes nUCGenes ScafScore ScafOkDrop)
  #   # daplx23pbc_asm2dc_dp17evgt1m_genecopy.score
  
  loggit 0,"remake_asm_gene_evd($aname) nsc=$nsc, ngev=$ngev"  if($debug);
  
  return($nsc, $asmevdtab, $ngev, $gcnaltab);
}

sub asm_evgene_tab {
  my($inevdtab,$outname, $fasizeh)= @_;
  
  use constant { Wu=>8, Xu=>2, Wg=>4, Xg=>1 };  #test weights for evidence types, favor keep uniq genes over drop extra dups
  my $NOSZ= (defined $fasizeh and ref($fasizeh)) ? 0 : 1;
  
  my($ok,$inh)= openRead($inevdtab); 
  unless($ok) { loggit( 1, "Fail to read $inevdtab"); return (0,0); }
  my (%scg,%acl,%ucg,%sw); 
  while(<$inh>) {
    next if(/^\W|^GeneID/);
    my @v=split; 
    # if(@v==8){  my($sc,$sw)=@v[0,1]; $sw{$sc}=$sw; next; } # asm.facount .. get elswhere
    my($gd,$gc,$acl,$nsc,$sc,$sr)=@v; $acl=~s/unip/uniq/; my $ucg="na";
    if($gc=~/ucg/){ $ucg=($acl=~/ahi/)?"xucg":"ucg";  } 
    $scg{$sc}{$acl}++; $scg{$sc}{$ucg}++; $acl{$acl}++;
    $ucg{$ucg}++;  
  } close($inh);
  # END
  
  my($nout,$nok,$tok,$ndrop,$tdrop)=(0) x 9;
  my @acl=sort keys %acl; my @ucg=qw(ucg xucg); 
  my($hd,$outh)=(0);
  open($outh, '>', $outname); 
  for my $sc (sort keys %scg){ 
    my($cl,$uc,$nc,$nu,$c)=(0) x 9; 
    $cl= join" ",map{ $c=$scg{$sc}{$_}||0; $nc+=$c; "$_:$c"; } @acl; 
    $uc= join" ", grep/:/, map{ $c=$scg{$sc}{$_}; $nu+=$c; ($c)?"$_:$c":""; } @ucg;  $uc||="na"; 
    my $sw=($NOSZ) ? 0: $fasizeh->{$sc}||0; 
    my $vuni=$scg{$sc}{uniq};
    my $vhi=$scg{$sc}{ahi}||0; my $vxu=$scg{$sc}{xucg}||0; 
    my $wg=$nc-$vhi; my $wu=$nu-$vxu; 
    my $wt="Wg*$wg/$nc - Xg*$vhi/$nc + Wu*$wu - Xu*$vxu"; 
    my $wtc= Wg*$wg/$nc - Xg*$vhi/$nc + Wu*$wu - Xu*$vxu; 
    my $ok=($wtc<0 and $vuni>0)?"okrescu":($wtc<0)?"drop":"ok"; 
    if($ok eq "drop"){ $tdrop+=$sw; $ndrop++; } else { $tok+=$sw; $nok++; }
    my $wtv=sprintf"%.0f",$wtc; 
    print $outh join("\t",$sc,$sw,$nc,"$cl $uc", $wt, $wtv, $ok)."\n";  $nout++;
  } 
  print $outh "#total nok=$nok, tok=$tok, ndrop=$ndrop, tdrop=$tdrop\n";  
  close($outh);
  loggit 0, "asm_evgene_tab($outname) total nok=$nok, tok=$tok, ndrop=$ndrop, tdrop=$tdrop" if $debug;
  return($nout,$outname); 
}

use constant GSCAF_LOC => 1; # use "scafid:sb-se" or "scafid:sbase" format, else only scafid

sub geneasm_cnal_tab {
  my($genecn, $gscaf, $outname, $fasizeh)= @_; # $gmaptab, 
  use constant { GENECN_AHI => 1.75, GENECN_ALO => 0.66, GENECN_AUNIQ => 1.5 };
  my($hd, $nout, $outh, %geneasm_cnal, %asmgene_cnal)=(0,0);
  open($outh, '>', $outname); 
  for my $gid (sort keys %$genecn) { 
    my $gcn= $genecn->{$gid}||0; # genecn zero?
    my $gcnz= ($gcn<1)?1: $gcn; # dont want near zero here
    my @scaf=(); my @gmap=(); my $acla="na";
    if($gscaf->{$gid}) { @scaf= @{$gscaf->{$gid}};  } # @gmap=@{$gmap->{$gid}};
    my $scn=@scaf; 
    unless(@scaf){ push @scaf,"nopath";  $acla="uniq"; } # skip these ?? acla="zero" but dont need extra cla
    else { $acla= 
        ($scn < GENECN_AUNIQ) ? "uniq" 
        : ($scn > GENECN_AHI*$gcnz)?"ahi" 
        : ($scn < GENECN_ALO*$gcnz)?"alo" 
        : "aeq"; 
    }
    #FIXME: gscaf->gid vals need to be scaf:sb-se:sor not just scaf, for many gndups on same scaf
    
    # my $acla= ($scn > GENECN_AHI*$gcnz)?"ahi" : ($scn < GENECN_ALO*$gcnz)?"alo" :"aeq"; 
    # if($scn < GENECN_AUNIQ){ if($acla=~/aeq|alo/){ $acla="uniq"; } else { $acla="unip"; } } # uniq for 1 asm copy, ahi not valid here
    for(my $i=0; $i<=$#scaf; $i++){ 
      #my $sid=$scaf[$i]; # this val becomes scaf:sbe:sor ?? need here?
      # if(GSCAF_LOC) { ($sid,$sloc)= split":",$scaf[$i]; } else { $sid=$scaf[$i]; } 
      my($sid,$sloc)= split":",$scaf[$i]; $sloc||=0; 
      my $scrank= 1+$i;
      print $outh join("\t",qw(GeneID Cgene Aclass Casm ScafID ScRank ScLoc))."\n" if(1>$hd++); 
      print $outh join("\t",$gid,$gcn,$acla,$scn,$sid,$scrank,$sloc)."\n"; $nout++;
      # push @{$geneasm_cnal{$gid}}, [$gid,$gcn,$acla,$scn,$sid,$scrank,$sloc];
      push @{$asmgene_cnal{$sid}}, [$gid,$gcn,$acla,$scn,$sid,$scrank,$sloc]; # any dups? tandem gene dups on same scaf?
      #?? keep table in hash for recalc scaf scores, as each scaf dropped,  need new Casm, Aclass for gene-linked ScafIDs
    }
  } close($outh);
  loggit 0, "geneasm_cnal_tab() n=$nout, $outname" if $debug;
  return($nout,$outname, \%asmgene_cnal); # add: \%geneasm_cnal or/and \%asmgene_cnal
}

=item read_contam

  see evigene genoasm/asmlink2refchr.pl
  .. should this do same parsing of yes/no contam, or assume all ids are yes contam?

  contam.tab format used in asmlink2refchr.pl
    my($sc,$sw,$bacid,$bacaln,$genes)=@v;
    my($pbaln,$baln,$bb,$be)= split /[,\/\-]/, $bacaln;
    $cclass= ($pbaln < pMINALN and $baln < bMINALN) ? "no" ($genes>0)? "maybe": "yes";
    $contam{$sc}{cla}= $cclass; yes|no|maybe
    $contam{$sc}{val}= "$bacid $bacaln $genes";
    $contamids{$sc}= $cclass if($cclass ne "no"); 

=cut

sub read_contam {
  my($contamtab)=@_;
  my($ncontam, %contamids)=(0);
  if($contamtab and -s $contamtab) {
    my($ok,$inh)= openRead($contamtab); 
    while(<$inh>){ next if(/^\W/); my($sid,$sval)=split; $contamids{$sid}= $sval; } close($inh);
    $ncontam= scalar(keys %contamids);   
    loggit 0, "# contam scafs n=$ncontam if $contamtab \n" if $debug;  
  }
  return($ncontam, \%contamids);
}

sub read_genexcopy {
  my($genexcopy)=@_;
  my($nucg,$ngene,$cucg,$cav,$cerr)= (0) x 9;
  my($ok,$inh)= openRead($genexcopy); 
  unless($ok) { loggit( 1, "Fail to read $genexcopy"); return (0,0); }
  my (%genextab,%genecn); 
  while(<$inh>) {
    if(/^\W/){
      # Uniq Gene Cov Depth n=954, .. C.Map/W=38.4, 38.0 +/-1.26 (mdn,ave,sem) 
      if(/Uniq Gene Cov Depth n=(\d+)/ and not $cucg ) {  
        $nucg=$1;
        if( m/C.Map.W=([\d\.]+), ([\d\.]+)\W+([\d\.]+)/ ) { ($cucg,$cav,$cerr)= ($1,$2,$3);}
        elsif( m/C.Map.W=([\d\.]+)/) { $cucg=$1; }      
      }
    } else { # per gene
      # (Gene_ID Glen Nread Rdlen tCopy C.M Uniq Merr C.nz S.Dup,Unq,Nt,Equ,UCG);
      # my($gid,$glen,$nrd,$rdlen,$tcopy,$cm,$cla,$merr,$cnz,$sdune)= @v;
      my($gid,@gv)= split;
      next if($gid eq 'Gene_ID');
      my $tc=$gv[3]; my $ucg=(m/UCG/ and $tc < 1.4)?"ucg":""; $genecn{$gid}=$tc.$ucg;
      $genextab{$gid}= \@gv; $ngene++;
      # ($id,$tw,$nrd,$rdl,$tc,$cm,$ucla,$mer,$cnz)=@v; $ucg=(m/UCG/ and $tc < 1.5)?"ucg":"";
      # $genecn{$id}=$tc.$ucg; $genex{$id}=[$tw,$nrd,$tc,$cm,$ucla,$cnz]; 
    }
  } close($inh);
  
  loggit(0, "read_genexcopy nucg=$nucg, ngene=$ngene Cucg=$cucg of $genexcopy");
  return($ngene, \%genecn, \%genextab, $nucg, $cucg ,);  
  #orig: return($nucg,$ngene,$cucg,\%genextab,\%genecn);  
}

sub read_genealign {
  my($intab, $isgenesorted)=@_;
  #? intab eq STDIN
  if($intab =~ /gmap|align.tab/){
    return read_genealign_gmap($intab, $isgenesorted);
  } elsif($intab =~ /sumgenetab/){
    return read_genealign_gnodes_sumgene($intab, $isgenesorted);
  } else {
    loggit(1, "read_genealign: unknown format of $intab");
    return(0); # what?
  }
}

=item sumgenetab

 dmag21nsk8maca8mo3ok8u_bwa.sumgenetab
GeneID	gLen	gNread	gCopy	gCM	gCnz	gClass	aCopy	aCovT	aCovM	aCopyRd	aReadT	aReadM	aReadZ	lCopy	lCov	lLoci	lxCopy	axCopy	cCopy	cCovT	cCovM	cxCopy	eCopy	eCovT	eCovM	exCopy	eqToGcopy
Dapma7bEVm000001t1	6030	2035	1.0	52	51.3,52,6022,99.9	dups	1.0	61	59	1.0	2742	2742	0	1.3	58.5,59,100%	dmag21ns8moa_sc000078:794400	1.5	1.2	1.0	58	58	1.1	1.0	52	52	1.0	a:eq,l:eq,lx:eq,ax:eq,c:eq,cx:eq,e:eq,ex:eq
Dapma7bEVm000002t1	6816	1713	1.1	57	56.2,57,6564,96.3	dups	1.0	61	61	1.0	2613	2612	0	1.0	60.8,61,100%	dmag21ns8moa_sc000056:3294800	1.2	1.2	1.0	61	61	1.2	1.0	56	56	1.1	a:eq,l:eq,lx:eq,ax:eq,c:eq,cx:eq,e:eq,ex:eq

use aCopy or lCopy ? use lLoci:17 ..

=cut

sub read_genealign_gnodes_sumgene {
  my($intab, $isgenesorted)=@_;
  my($ngene)= (0) x 9;
  my($ok,$inh)= openRead($intab); 
  unless($ok) { loggit( 1, "Fail to read $intab"); return (0,0); }
  my (%scaf,%gmap); 
  while(<$inh>) {
    next if(/^\W/);
    chomp; my @v=split"\t";
    #gmap.aln: ($sc,$sbe,$so,$gid,$qspa,$qmat,$tw,$cov,$pid,$npath,$ndel,$nex)=@v; my $tag=$v[-1]; 
    my($gid, $tw, $gcn, $gclass)= @v[0,1,3,6];
    my($lcn,$lcov,$lloc)= @v[14,15,16]; # v[14..16] = lCopy	lCov	lLoci
    ## lloc can be many: "dmag21ns8moa_sc000178:326400; dmag21ns8moa_sc000178:325900"
    my $cov=99; my $pid=99; # lcov is Not gmap.cov
    my $npath=$lcn; my $nex=1; my $tag="sumgn"; 
    my @lloc= split /;\s*/, $lloc;
# ** sumgenetab has bad lloc dups of same locus, distance of ~ 100 b
#    filter using min dist? eg 1-5 kb
    use constant LDMIN => 3000;
    my %lloc;
    for my $ll (@lloc) {
      my($sc,$sbe,$so)=split":",$ll; $so||=".";
      next if($ncontam and $contamids->{$sc});
      my $lsame=0; if(my $lsbe=$lloc{$sc}) { for my $lbe (@$lsbe) { if(abs($sbe - $lbe) < LDMIN) { $lsame=1; last; } } }
      next if($lsame); 
      push @{$lloc{$sc}}, $sbe;
      if(GSCAF_LOC){ push @{$scaf{$gid}}, "$sc:$sbe"; } else { push @{$scaf{$gid}}, $sc; } 
      push @{$gmap{$gid}}, [$sc,$sbe,$so,$tw,$cov,$pid,$npath,$nex,$tag]; #?? want these fake vals?
      }
    $ngene++;
  }
  return($ngene,\%scaf,\%gmap);  
}

=item read_genealign

    # FIXME can have tandem dups: same gid, same scaf id, diff sbe locus; change val to "$sc:$sbe:$so" ?
     cat testf/dmag21nsk8maca8mo.genes7align.tab | cut -f1,2,3,4,7,8,9,10,12 | grep -v NOPATH | egrep 'Dapma7bEVm013666t1|Dapma7bEVm019603t1|Dapma7bEVm021705t1'
dmag21ns8moa_sc000016	18547-19282	.	Dapma7bEVm013666t1	736	100.0	99.5	14/31	1.1
dmag21ns8moa_sc000016	48016-48882	.	Dapma7bEVm019603t1	867	100.0	100.0	1/15	1.1
dmag21ns8moa_sc000016	50214-50452	.	Dapma7bEVm021705t1	239	100.0	100.0	1/17	1.1

dmag21ns8moa_sc000016	121600-122335	.	Dapma7bEVm013666t1	736	100.0	99.2	27/31	1.1
dmag21ns8moa_sc000016	125384-126250	.	Dapma7bEVm019603t1	867	100.0	100.0	2/15	1.1
dmag21ns8moa_sc000016	127582-127820	.	Dapma7bEVm021705t1	239	100.0	100.0	2/17	1.1

.. and many more of these dupl gn

testf/dmag21nsk8maca8mo5ok.genecopy.alntab
Dapma7bEVm013666t1	29.1	aeq	31	dmag21ns8moa_sc001433	1
Dapma7bEVm013666t1	29.1	aeq	31	dmag21ns8moa_sc002164	2
Dapma7bEVm013666t1	29.1	aeq	31	dmag21ns8moa_sc000100	3
Dapma7bEVm013666t1	29.1	aeq	31	dmag21ns8moa_sc000474	4
...

egrep 'Dapma7bEVm013666t1|Dapma7bEVm019603t1|Dapma7bEVm021705t1' | grep dmag21ns8moa_sc000016 
Dapma7bEVm013666t1	29.1	aeq	31	dmag21ns8moa_sc000016	14
Dapma7bEVm013666t1	29.1	aeq	31	dmag21ns8moa_sc000016	27
Dapma7bEVm019603t1	4.7	ahi	13	dmag21ns8moa_sc000016	1
Dapma7bEVm019603t1	4.7	ahi	13	dmag21ns8moa_sc000016	2
Dapma7bEVm021705t1	3.3	ahi	16	dmag21ns8moa_sc000016	1
Dapma7bEVm021705t1	3.3	ahi	16	dmag21ns8moa_sc000016	2


=cut

sub read_genealign_gmap {
  my($intab, $isgenesorted)=@_;
  my($ngene)= (0) x 9;
  my($ok,$inh)= openRead($intab); 
  unless($ok) { loggit( 1, "Fail to read $intab"); return (0,0); }
  my (%scaf,%gmap); 
  while(<$inh>) {
    next if(/^\W/);
    my @v=split;
    my ($sc,$sbe,$so,$gid,$qspa,$qmat,$tw,$cov,$pid,$npath,$ndel,$nex)=@v; my $tag=$v[-1]; 
    next if($sc eq "NOPATH"); 
    next if($ncontam and $contamids->{$sc});
    # FIXME: @ gid arrays should be sorted by +cov,+pid best aligns
    # FIXME can have tandem dups: same gid, same scaf id, diff sbe locus; change val to "$sc:$sbe:$so" ?
    if(GSCAF_LOC){ push @{$scaf{$gid}}, "$sc:$sbe"; } else { push @{$scaf{$gid}}, $sc; } 
    push @{$gmap{$gid}}, [$sc,$sbe,$so,$tw,$cov,$pid,$npath,$nex,$tag]; 
    $ngene++;
  } close($inh);
  
  unless($isgenesorted) {
    for my $gid (sort keys %gmap) {
      my @gv= @{$gmap{$gid}};
      if(@gv>1) {
        my @sc; @gv= sort{ $b->[4] <=> $a->[4] or $b->[5] <=> $a->[5] } @gv;
        if(GSCAF_LOC){ @sc= map{ join(":",$_->[0],$_->[1]) } @gv; }
        else { @sc= map{ $_->[0] } @gv;  }
        $gmap{$gid}= \@gv;
        $scaf{$gid}= \@sc;
      }
    }
  }
  return($ngene,\%scaf,\%gmap);  
}

__END__

=item upd2303.try2

../makecovalntab.pl -debug -asm dmag21nsk8maca8mo5ok.fa.gz -selfa dmag21nsk8maca8mo5ok.nucmer.coords -gnodescov dmag21nsk8maca8mo5dg_SRR15012074_b2_bwa.cc8j.covtab.gz -genex dmag7fincds_SRR15012074_b2_bwa.genexcopy  -genealigntab dmag21nsk8maca8mo.genes7align.tab

#read_genexcopy nucg=541, ngene=29127 Cucg=50.7 of dmag7fincds_SRR15012074_b2_bwa.genexcopy
#genex reset dna cover depth Clow(15 => 13, Chigh(35 => 41) from Cucg=50.7
#geneasm_cnal_tab() n=45491, asmevd_dmag21nsk8maca8mo5ok.genecopy.alntab
#asm_evgene_tab(asmevd_dmag21nsk8maca8mo5ok.evdgene.sctab) total nok=2758, tok=202605921, ndrop=685, tdrop=4934536
#make_asm_gene_evd(dmag21nsk8maca8mo5ok,dmag7fincds_SRR15012074_b2_bwa.genexcopy,dmag21nsk8maca8mo.genes7align.tab,dmag21nsk8maca8mo5ok.facount) nsc=3443, ngev=45491
#readAln_nucmer(dmag21nsk8maca8mo5ok.nucmer.coords) n=73689
#readCovtab_gnodes(dmag21nsk8maca8mo5dg_SRR15012074_b2_bwa.cc8j.covtab.gz) n=1510486
#make_covalntab() n=69830 asmevd_dmag21nsk8maca8mo5ok.covalntab
## readCovaln asmevd_dmag21nsk8maca8mo5ok.covalntab 
## sum.covaln ns=7349, mb=229, snb=2290962, av(acov)=64, scov=54
## readEvdGene.sctab asmevd_dmag21nsk8maca8mo5ok.evdgene.sctab 
## putkeepdrop: asmevd_dmag21nsk8maca8mo5ok.keepdrop.ids
## updcovalntab: asmevd_dmag21nsk8maca8mo5ok.updcovalntab

asmevd_dmag21nsk8maca8mo5ok.keepdrop.ids
asmevd_dmag21nsk8maca8mo5ok.updcovalntab
asmevd_dmag21nsk8maca8mo5ok.covalntab	
asmevd_dmag21nsk8maca8mo5ok.evdgene.sctab
asmevd_dmag21nsk8maca8mo5ok.genecopy.alntab

head asmevd_dmag21nsk8maca8mo5ok.genecopy.alntab
GeneID  Cgene   Aclass  Casm    ScafID  ScRank  ScLoc
Dapma7bEVm000001t1      1.0     uniq    1       dmag21ns8moa_sc000078   1       787892-820874
Dapma7bEVm000002t1      1.1     uniq    1       dmag21ns8moa_sc000056   1       3292942-3304883
Dapma7bEVm000003t1      1.0     uniq    1       dmag21ns8moa_sc000825   1       40690-51522
Dapma7bEVm000004t1      1.0ucg  ahi     2       dmag21ns8moa_sc001276   1       10087-35158
Dapma7bEVm000004t1      1.0ucg  ahi     2       dmag21ns8moa_sc006422   2       16478-41554
Dapma7bEVm000005t1      8.8     alo     3       dmag21ns8moa_sc001372   1       69926-74850
Dapma7bEVm000005t1      8.8     alo     3       dmag21ns8moa_sc001585   2       12092-17016
Dapma7bEVm000005t1      8.8     alo     3       dmag21ns8moa_sc000196   3       397908-402833
Dapma7bEVm000006t1      1.1     uniq    1       dmag21ns8moa_sc000384   1       150563-167219

dgmm:testf:% head asmevd_dmag21nsk8maca8mo5ok.evdgene.sctab
dmag21ns8moa_sc000001   55759   1       aeq:0 ahi:0 alo:0 uniq:1 na     Wg*1/1 - Xg*0/1 + Wu*0 - Xu*0   4       ok
dmag21ns8moa_sc000003   998382  271     aeq:11 ahi:39 alo:1 uniq:220 ucg:9 xucg:6       Wg*232/271 - Xg*39/271 + Wu*9 - Xu*6    63      ok
dmag21ns8moa_sc000004   524311  118     aeq:11 ahi:71 alo:4 uniq:32 ucg:1 xucg:2        Wg*47/118 - Xg*71/118 + Wu*1 - Xu*2     ok
dmag21ns8moa_sc000005   105482  4       aeq:2 ahi:1 alo:1 uniq:0 na     Wg*3/4 - Xg*1/4 + Wu*0 - Xu*0   3       ok
dmag21ns8moa_sc000006   487884  54      aeq:12 ahi:14 alo:1 uniq:27 ucg:2       Wg*40/54 - Xg*14/54 + Wu*2 - Xu*0       19      ok
dmag21ns8moa_sc000007   0       1       aeq:0 ahi:0 alo:0 uniq:1 na     Wg*1/1 - Xg*0/1 + Wu*0 - Xu*0   4       ok
dmag21ns8moa_sc000008   374028  111     aeq:0 ahi:11 alo:2 uniq:98 ucg:7        Wg*100/111 - Xg*11/111 + Wu*7 - Xu*0    60      ok
dmag21ns8moa_sc000009   244792  21      aeq:2 ahi:15 alo:0 uniq:4 xucg:4        Wg*6/21 - Xg*15/21 + Wu*0 - Xu*4        -8      okrescu
dmag21ns8moa_sc000010   101351  9       aeq:1 ahi:3 alo:4 uniq:1 na     Wg*6/9 - Xg*3/9 + Wu*0 - Xu*0   2       ok
dmag21ns8moa_sc000012   102843  5       aeq:2 ahi:2 alo:1 uniq:0 na     Wg*3/5 - Xg*2/5 + Wu*0 - Xu*0   2       ok

dgmm:testf:% head asmevd_dmag21nsk8maca8mo5ok.covalntab
ScafID  QueryID Nbin    ACovM   SumCovM
dmag21ns8moa_sc000001   self    558     11.81   6590.64
dmag21ns8moa_sc000001   any     0       0       0.00
dmag21ns8moa_sc000003   self    9984    55.33   552386.23
dmag21ns8moa_sc000003   any     3172    49.07   155651.79
dmag21ns8moa_sc000003   dmag21ns8moa_sc001966   569     30.70   17468.41
dmag21ns8moa_sc000003   dmag21ns8moa_sc002846   538     31.06   16708.40
dmag21ns8moa_sc000003   dmag21ns8moa_sc006274   518     30.27   15677.70
dmag21ns8moa_sc000003   dmag21ns8moa_sc000868   133     36.01   4789.51
dmag21ns8moa_sc000003   dmag21ns8moa_sc000729   133     35.76   4756.25

dgmm:testf:% head asmevd_dmag21nsk8maca8mo5ok.keepdrop.ids
ScafID  Class   LenSc   CovSc   Guni    GCeq    AlnSc
dmag21ns8moa_sc000001   keep.evdgn      55800   11.81   1       0       0
dmag21ns8moa_sc000003   keep.chi        998400  55.33   220     96      31
dmag21ns8moa_sc000004   keep.evdgn      524400  35.75   32      -12     87
dmag21ns8moa_sc000005   keep.evdgn      105500  37.12   0       1       96
dmag21ns8moa_sc000006   keep.evdgn      487900  40.19   27      13      67
dmag21ns8moa_sc000008   keep.chi        374100  51.53   98      44      19
dmag21ns8moa_sc000009   keep.evdgn      244800  29.12   4       -5      86
dmag21ns8moa_sc000010   keep.evdgn      101400  28.35   1       1       91
dmag21ns8moa_sc000012   keep.evdgn      102900  38.10   0       0       99

try2:
../makecovalntab.pl -debug -asm dmag21nsk8maca8mo5ok.fa.gz -keepdropids asmevd_dmag21nsk8maca8mo5ok.keepdrop.ids -covalntab asmevd_dmag21nsk8maca8mo5ok.covalntab
## readids asmevd_dmag21nsk8maca8mo5ok.keepdrop.ids 
# sum.covaln ns=4643, mb=223.6, snb=2236893, av(acov)=101, scov=55, dropid=2706

cut -f2 asmevd_dmag21nsk8maca8mo5ok.keepdrop.ids | sort | uniq -c | head 
2025 drop.lowc  68 drop.halfac 613 drop.halfgdup
2706 keep.chi 1848 keep.evdgn   89 unk

try1:
../makecovalntab.pl -debug -asm dmag21nsk8maca8mo5ok.fa.gz -keepdropids dmag21nsk8maca8mo5ok1try.keepdrop.ids -covalntab dmag21nsk8maca8mo5ok1try.covalntab
## readids dmag21nsk8maca8mo5ok1try.keepdrop.ids 
# sum.covaln ns=3894, mb=207, snb=2070156, av(acov)=119, scov=57, dropid=3455

cut -f2 dmag21nsk8maca8mo5ok1try.keepdrop.ids | sort | uniq -c | head 
3143 drop.lowc 298 drop.halfac  14 drop.lowac
2936 keep.chi  873 keep.ugc     85 unk


=item upd2303.try1

/bio/bio-grid/daph20asm/daph23asm/daph23chrmap/testf

2.3M Mar 31 16:11 dmag21nsk8maca8mo5ok.updcovalntab
331K Mar 31 16:11 dmag21nsk8maca8mo5ok.keepdrop.ids
3.7M Mar 31 16:11 dmag21nsk8maca8mo5ok.covalntab
341K Mar 31 16:08 dmag21nsk8maca8mo5ok.evdgene.sctab
2.3M Mar 31 16:08 dmag21nsk8maca8mo5ok.genecopy.alntab

 54B Mar 31 15:59 dmag21nsk8maca8mo3ok8u_bwa.sumgenetab@  
102B Mar 31 15:43 dmag7fincds_SRR15012074_b2_bwa.genexcopy@  
 96B Mar 31 15:33 dmag21nsk8maca8mo5ok.nucmer.coords@  
 55B Mar 31 15:25 dmag21nsk8maca8mo5dg8u_bwa_genesum.txt@
 68B Mar 31 15:25 dmag21nsk8maca8mo5dg_SRR15012074_b2_bwa.cc8j.chrtab
 71B Mar 31 15:25 dmag21nsk8maca8mo5dg_SRR15012074_b2_bwa.cc8j.covtab.gz
 69B Mar 31 15:25 dmag21nsk8maca8mo5dg_SRR15012074_b2_bwa.cc8j_sum.txt
 77B Mar 31 15:22 dmag21nsk8maca8mo5ok.facount@  
 75B Mar 31 15:22 dmag21nsk8maca8mo5ok.fa.gz@ 
 82B Mar 31 15:21 dmag21nsk8maca8mo.genes7align.tab@ 

../makecovalntab.pl  -debug -asm dmag21nsk8maca8mo5ok.fa.gz \
  -selfa dmag21nsk8maca8mo5ok.nucmer.coords \
  -gnodescov dmag21nsk8maca8mo5dg_SRR15012074_b2_bwa.cc8j.covtab.gz \
  -genex dmag7fincds_SRR15012074_b2_bwa.genexcopy  \
  -genealigntab dmag21nsk8maca8mo.genes7align.tab

head dmag21nsk8maca8mo5ok.genecopy.alntab
GeneID  Cgene   Aclass  Casm    ScafID  ScRank
Dapma7bEVm000001t1      1.0     uniq    1       dmag21ns8moa_sc000078   1
Dapma7bEVm000002t1      1.1     uniq    1       dmag21ns8moa_sc000056   1
Dapma7bEVm000003t1      1.0     uniq    1       dmag21ns8moa_sc000825   1
Dapma7bEVm000004t1      1.0ucg  ahi     2       dmag21ns8moa_sc001276   1
Dapma7bEVm000004t1      1.0ucg  ahi     2       dmag21ns8moa_sc006422   2
Dapma7bEVm000005t1      8.8     alo     3       dmag21ns8moa_sc001372   1
Dapma7bEVm000005t1      8.8     alo     3       dmag21ns8moa_sc001585   2
Dapma7bEVm000005t1      8.8     alo     3       dmag21ns8moa_sc000196   3
Dapma7bEVm000006t1      1.1     uniq    1       dmag21ns8moa_sc000384   1

head dmag21nsk8maca8mo5ok.evdgene.sctab
dmag21ns8moa_sc000001   55759   1       aeq:0 ahi:0 alo:0 uniq:1 zero:0 na      Wg*1/1 - Xg*0/1 + Wu*0 - Xu*0   4       ok
dmag21ns8moa_sc000003   998382  271     aeq:11 ahi:39 alo:1 uniq:220 zero:0 ucg:9 xucg:6        Wg*232/271 - Xg*39/271 + Wu*9 - Xu*6    63      ok
dmag21ns8moa_sc000004   524311  118     aeq:11 ahi:71 alo:4 uniq:32 zero:0 ucg:1 xucg:2 Wg*47/118 - Xg*71/118 + Wu*1 - Xu*2     ok
dmag21ns8moa_sc000005   105482  4       aeq:2 ahi:1 alo:1 uniq:0 zero:0 na      Wg*3/4 - Xg*1/4 + Wu*0 - Xu*0   3       ok
dmag21ns8moa_sc000006   487884  54      aeq:12 ahi:14 alo:1 uniq:27 zero:0 ucg:2        Wg*40/54 - Xg*14/54 + Wu*2 - Xu*0       19      ok
dmag21ns8moa_sc000007   0       1       aeq:0 ahi:0 alo:0 uniq:1 zero:0 na      Wg*1/1 - Xg*0/1 + Wu*0 - Xu*0   4       ok
dmag21ns8moa_sc000008   374028  111     aeq:0 ahi:11 alo:2 uniq:98 zero:0 ucg:7 Wg*100/111 - Xg*11/111 + Wu*7 - Xu*0    60      ok
dmag21ns8moa_sc000009   244792  21      aeq:2 ahi:15 alo:0 uniq:4 zero:0 xucg:4 Wg*6/21 - Xg*15/21 + Wu*0 - Xu*4        -8      okrescu
dmag21ns8moa_sc000010   101351  9       aeq:1 ahi:3 alo:4 uniq:1 zero:0 na      Wg*6/9 - Xg*3/9 + Wu*0 - Xu*0   2       ok
dmag21ns8moa_sc000012   102843  5       aeq:2 ahi:2 alo:1 uniq:0 zero:0 na      Wg*3/5 - Xg*2/5 + Wu*0 - Xu*0   2       ok

head dmag21nsk8maca8mo5ok.covalntab
ScafID  QueryID Nbin    ACovM   SumCovM
dmag21ns8moa_sc000001   self    558     11.81   6590.64000000001
dmag21ns8moa_sc000001   any     0       0       0
dmag21ns8moa_sc000003   self    9984    55.33   552386.230000001
dmag21ns8moa_sc000003   any     3172    49.07   155651.79
dmag21ns8moa_sc000003   dmag21ns8moa_sc001966   569     30.70   17468.41
dmag21ns8moa_sc000003   dmag21ns8moa_sc002846   538     31.06   16708.4
dmag21ns8moa_sc000003   dmag21ns8moa_sc006274   518     30.27   15677.7
dmag21ns8moa_sc000003   dmag21ns8moa_sc000868   133     36.01   4789.51
dmag21ns8moa_sc000003   dmag21ns8moa_sc000729   133     35.76   4756.25

 head dmag21nsk8maca8mo5ok.keepdrop.ids   ** FIXME add genecn ahi/aeq/lo/uniq tests
ScafID  Class   LenSc   CovSc   Guni    GCeq    AlnSc
dmag21ns8moa_sc000001   drop.lowc       55800   11.81   0       0       0
dmag21ns8moa_sc000003   keep.chi        998400  58.60   1       14      31
dmag21ns8moa_sc000004   keep.chi        524400  44.89   4       30      87
dmag21ns8moa_sc000005   keep.chi        105500  38.31   1       -1      96
dmag21ns8moa_sc000006   keep.chi        487900  44.43   1       1       67
dmag21ns8moa_sc000008   keep.chi        374100  53.06   2       5       19
dmag21ns8moa_sc000009   drop.halfac     244800  29.39   0       6       86
dmag21ns8moa_sc000010   keep.ugc        101400  32.43   4       1       91
dmag21ns8moa_sc000012   keep.chi        102900  41.06   1       0       99

cut -f2 dmag21nsk8maca8mo5ok.keepdrop.ids | sort | uniq -c
   1 Class
 298 drop.halfac ??
  14 drop.lowac
3143 drop.lowc  ??? too many
2936 keep.chi
 873 keep.ugc
  85 unk

head dmag21nsk8maca8mo5ok.updcovalntab
dmag21ns8moa_sc000003   self    9984    58.60   585032.940000001
dmag21ns8moa_sc000003   any     3172    49.07   155651.79
dmag21ns8moa_sc000003   dmag21ns8moa_sc001966   569     30.70   17468.41
dmag21ns8moa_sc000003   dmag21ns8moa_sc000729   133     35.76   4756.25
dmag21ns8moa_sc000003   dmag21ns8moa_sc000868   133     36.01   4789.51
dmag21ns8moa_sc000003   dmag21ns8moa_sc007776   105     109.82  11530.69
dmag21ns8moa_sc000003   dmag21ns8moa_sc002854   34      174.04  5917.37
dmag21ns8moa_sc000003   dmag21ns8moa_sc000020   31      38.34   1188.59
dmag21ns8moa_sc000003   dmag21ns8moa_sc001486   23      43.62   1003.17
dmag21ns8moa_sc000003   dmag21ns8moa_sc000132   21      30.85   647.76



=cut

=item try5

   env chi=40 clo=25 aln=70  .. drop more unk w/ clo<25

# sum.covaln ns=3616, mb=229, snb=2290959, av(acov)=130, scov=54, dropid=0
   ^^ substantially smaller, ave scov higher

** use this dropset for gnodes test? ie
    7784 dmag21nsk8maca8mo5t.keepdrop.ids => dmag21nsk8maca8mo.keepdrop5c.tab
   41465 dmag21nsk8maca8mo5t.updcovalntab

 411 drop.halfac | thse two sum to same
2480 drop.lowac  |
1277 drop.lowc   < +800 from unk
2654 keep.chi
 571 keep.ugc << should be 'ucg'?
 391 unk      << most dropped

problem class:
   571 keep.ugc  == ($guni>0) has uniq gene locus
     .. how many of these are mistakes? from gmap align.tab, can change w/ diff scaf set
     .. compare w/ gene locs in anntab
   
   
   
=item try4c

.. corrected  try4b mistake of reclassing drops

env chi=40 clo=10 aln=70 covalntab=dmag21nsk8maca8mo.covalntab genecopy=dmag21nsk8maca8mo_genecopy.sccount $gw/daphnia/daphmag/makecovalntab.pl 
# readCovaln dmag21nsk8maca8mo.covalntab 
# sum.covaln ns=7784, mb=346.2, snb=3462327, av(acov)=64, scov=35

env ids=dmag21nsk8maca8mo.keepdrop.ids covalntab=dmag21nsk8maca8mo.updcovalntab $gw/daphnia/daphmag/makecovalntab.pl 
# sum.covaln ns=4354, mb=252.8, snb=2528842, av(acov)=111, scov=50, dropid=0

cut -f2 dmag21nsk8maca8mo.keepdrop.ids | sort | uniq -c
2125 drop.halfac  < same 4a
 900 drop.lowac   < same 4a
 405 drop.lowc    < same 4a
2600 keep.chi     * added 60 from unk
 584 keep.ugc
1170 unk

=item try4b
  .. added reclassCovAln
env chi=40 clo=10 aln=70 covalntab=dmag21nsk8maca8mo.covalntab genecopy=dmag21nsk8maca8mo_genecopy.sccount $gw/daphnia/daphmag/makecovalntab.pl 
# readCovaln dmag21nsk8maca8mo.covalntab 
# sum.covaln ns=7784, mb=346.2, snb=3462327, av(acov)=64, scov=35
  
env ids=dmag21nsk8maca8mo.keepdrop.ids covalntab=dmag21nsk8maca8mo.updcovalntab $gw/daphnia/daphmag/makecovalntab.pl 
# sum.covaln ns=5396, mb=271.6, snb=2716843, av(acov)=94, scov=48, dropid=0
 ^^ changed too much for reclass..what?

 cut -f2 dmag21nsk8maca8mo.keepdrop.ids | sort | uniq -c
1537 drop.halfac   << LOST much
 299 drop.lowac    << lost
 552 drop.lowc
2701 keep.chi      << gain
 584 keep.ugc
2111 unk           << gain
 
=item try4

env chi=40 clo=10 aln=70 covalntab=dmag21nsk8maca8mo.covalntab genecopy=dmag21nsk8maca8mo_genecopy.sccount $gw/daphnia/daphmag/makecovalntab.pl 
# readCovaln dmag21nsk8maca8mo.covalntab 
# sum.covaln ns=7784, mb=346.2, snb=3462327, av(acov)=64, scov=35

2125 drop.halfac
 900 drop.lowac
 405 drop.lowc   << new, drop clo=10 only 
2538 keep.chi
 587 keep.ugc
1229 unk

env ids=dmag21nsk8maca8mo.keepdrop.ids covalntab=dmag21nsk8maca8mo.updcovalntab $gw/daphnia/daphmag/makecovalntab.pl 
# sum.covaln ns=4354, mb=252.8, snb=2528842, av(acov)=111, scov=50, dropid=0

dgmm:test8tv:% head dmag21nsk8maca8mo.keepdrop.ids
dmag21ns8moa_sc000001	keep.ugc	11.83c,55k
dmag21ns8moa_sc000002	drop.lowc	2.52c,32k
dmag21ns8moa_sc000003	keep.chi	55.66c,998k
..

** unk w/ acov > CHI should be reclassed keep.chi
 cat dmag21nsk8maca8mo.keepdrop.ids | grep -v drop | sort -k3,3n -k1,1 | grep unk | tail 
dmag21ns8moa_sc006075	unk	57.78c,3k
dmag21ns8moa_sc004426	unk	60.24c,12k
dmag21ns8moa_sc005090	unk	62.60c,5k

** keep.ucg w/ lo cov, problem? resolve how? check genecopy data
 cat dmag21nsk8maca8mo.keepdrop.ids | grep -v drop | sort -k3,3n -k1,1 | head -22
dmag21ns8moa_sc000953	keep.ugc	0.68c,74k
dmag21ns8moa_sc006647	keep.ugc	2.94c,33k
dmag21ns8moa_sc003082	keep.ugc	3.25c,44k
dmag21ns8moa_sc000872	keep.ugc	3.50c,218k
dmag21ns8moa_sc000022	keep.ugc	3.56c,193k
dmag21ns8moa_sc001480	keep.ugc	3.68c,51k
dmag21ns8moa_sc001296	keep.ugc	3.70c,153k
dmag21ns8moa_sc000657	keep.ugc	4.10c,124k
dmag21ns8moa_sc007634	keep.ugc	4.26c,37k
dmag21ns8moa_sc000158	keep.ugc	4.41c,113k
dmag21ns8moa_sc001474	keep.ugc	4.95c,36k
dmag21ns8moa_sc001083	keep.ugc	5.09c,118k

=item makekeepdrop try3

my $ALN=$ENV{aln}||75;  my $CLO=$ENV{clo}||15; my $CHI=$ENV{chi}||35; 

env chi=40 or 45 aln=70 ..

# readCovaln dmag21nsk8maca8mo.covalntab 
# sum.covaln ns=7784, mb=346.2, snb=3462327, av(acov)=64, scov=35
# readGenetab dmag21nsk8maca8mo_genecopy.sccount 
# putkeepdrop: dmag21nsk8maca8mo.keepdrop.ids
# updcovalntab: dmag21nsk8maca8mo.updcovalntab

cut -f2 dmag21nsk8maca8mo.keepdrop.ids | sort | uniq -c
1340 drop.halfac   * more
 130 drop.halfgdup  ** FIXME: need drop.updates for this, skip til have
1703 drop.lowac   * more
2539 keep.chi
 586 keep.ugc
1486 unk

env ids=dmag21nsk8maca8mo.keepdrop.ids covalntab=dmag21nsk8maca8mo.covalntab  $gw/daphnia/daphmag/makecovalntab.pl
# sum.covaln ns=4611, mb=257.5, snb=2575580, av(acov)=98, scov=42, dropid=3173

env ids=dmag21nsk8maca8mo.keepdrop.ids covalntab=dmag21nsk8maca8mo.updcovalntab  $gw/daphnia/daphmag/makecovalntab.pl | head
# sum.covaln ns=4611, mb=257.5, snb=2575580, av(acov)=105, scov=49, dropid=0  << ?? try this one? or check for halfc scafs
 .. unk set is problem, call as keep/drop ?

=item makekeepdrop try2

# readCovaln dmag21nsk8maca8mo.covalntab 
# sum.covaln ns=7784, mb=346.2, snb=3462327, av(acov)=64, scov=35
# readGenetab dmag21nsk8maca8mo_genecopy.sccount 
# putkeepdrop: dmag21nsk8maca8mo.keepdrop.ids
# updcovalntab: dmag21nsk8maca8mo.updcovalntab

1167 drop.halfac
 128 drop.halfgdup
1609 drop.lowac
2715 keep.chi
 547 keep.ugc
1618 unk

env ids=dmag21nsk8maca8mo.keepdrop.ids covalntab=dmag21nsk8maca8mo.covalntab  $gw/daphnia/daphmag/makecovalntab.pl | head
# readids dmag21nsk8maca8mo.keepdrop.ids 
# sum.covaln ns=4880, mb=265.1, snb=2651071, av(acov)=94, scov=42, dropid=2904

** using recalc covaln scores - drops, higher
env ids=dmag21nsk8maca8mo.keepdrop.ids covalntab=dmag21nsk8maca8mo.updcovalntab  $gw/daphnia/daphmag/makecovalntab.pl | head
# readids dmag21nsk8maca8mo.keepdrop.ids 
# sum.covaln ns=4880, mb=265.1, snb=2651071, av(acov)=100, scov=48, dropid=0


==> dmag21nsk8maca8mo.updcovalntab <==
ScafID	QueryID	Nbin	ACovM	SumCovM
dmag21ns8moa_sc000001	self	558	11.83	6599 << same
dmag21ns8moa_sc000001	any	0	0	0
dmag21ns8moa_sc000002	self	320	2.52	806  << same
dmag21ns8moa_sc000002	any	0	0	0
dmag21ns8moa_sc000003	self	9984	55.57	554793 << inc
dmag21ns8moa_sc000003	any	3172	36.80	116745
dmag21ns8moa_sc000003	dmag21ns8moa_sc001966	569	27.83	15835
dmag21ns8moa_sc000003	dmag21ns8moa_sc002846	538	30.30	16300
dmag21ns8moa_sc000003	dmag21ns8moa_sc006274	518	25.75	13339
..
dmag21ns8moa_sc000004   self    5244    32.77   171844 << inc
dmag21ns8moa_sc000004   any     4570    20.25   92520
dmag21ns8moa_sc000005   self    1055    32.79   34596  << inc
dmag21ns8moa_sc000005   any     1017    22.67   23051  
dmag21ns8moa_sc000006   self    4879    36.70   179067  << inc
dmag21ns8moa_sc000006   any     3305    21.38   70669

==> dmag21nsk8maca8mo.covalntab <==
ScafID	QueryID	Nbin	ACovM	SumCovM
dmag21ns8moa_sc000001	self	558	11.83	6599
dmag21ns8moa_sc000001	any	0	0	0
dmag21ns8moa_sc000002	self	320	2.52	806
dmag21ns8moa_sc000002	any	0	0	0
dmag21ns8moa_sc000003	self	9984	51.49	514099
dmag21ns8moa_sc000003	any	3172	36.80	116745
dmag21ns8moa_sc000003	dmag21ns8moa_sc006866	698	30.09	21006
dmag21ns8moa_sc000003	dmag21ns8moa_sc006682	655	24.85	16275
dmag21ns8moa_sc000003	dmag21ns8moa_sc001966	569	27.83	15835
..
dmag21ns8moa_sc000004   self    5244    24.77   129915
dmag21ns8moa_sc000004   any     4570    20.25   92520
dmag21ns8moa_sc000005   self    1055    21.94   23145
dmag21ns8moa_sc000005   any     1017    22.67   23051
dmag21ns8moa_sc000006   self    4879    30.28   147725
dmag21ns8moa_sc000006   any     3305    21.38   70669

=item makekeepdrop try1

env genecopy=dmag21nsk8maca8mo_genecopy.sccount  covalntab=dmag21nsk8maca8mo.covalntab  $gw/daphnia/daphmag/makecovalntab.pl

# sum.covaln ns=7784, mb=346.2, snb=3462327, av(acov)=64, scov=35

cut -f2 dmag21nsk8maca8mo.keepdrop.ids | sort | uniq -c
1365 drop.halfac
1657 drop.lowac
2734 keep.chi
2029 unk

env ids=dmag21nsk8maca8mo.keepdrop.ids .. makecovalntab.pl
# sum.covaln ns=4762, mb=254.7, snb=2547371, av(acov)=95, scov=42, dropid=3022
  ^^ looks reasonable but what? 
  FIXME: makekeepdrop needs to output new covalntab w/ drops removed, and new self,any scores adjusted for drop calc
  
=item putkeepdrop tab

==> dmag21nsk8maca8mo.keepdrop5c.tab <==
dmag21ns8moa_sc000001	keep.ugc	11.83c,55k
dmag21ns8moa_sc000002	drop.lowc	2.52c,32k

   ^^ change to this format vv
   
==> dmag21nsk8maca8mo.keepdrop3c.tab <==
ScafID	Class	LenSc	CovSc	Guni	GCeq	AlnSc
dmag21ns8moa_sc000001	keep.ugc	55759	15.83	1	1	0
dmag21ns8moa_sc000002	drop.lowc	32062	2.77	0	0	0
dmag21ns8moa_sc000003	keep.ugc	998382	54.22	286	281	0
dmag21ns8moa_sc000004	keep.ugc	524311	25.89	86	63	0
dmag21ns8moa_sc000005	keep.ugc	105482	20.29	1	3	0
dmag21ns8moa_sc000006	keep.ugc	487884	32.75	27	52	0
dmag21ns8moa_sc000007	drop.gdup	158382	2.54	0	-1	0
  
=cut

