#! /bin/bash
# run_kmc_readasmset4d.sh 
## --- gnodes_setup.sh for quartz.iu ---    
#SBATCH -A general
#SBATCH --job-name="gnodes_pipe"
#SBATCH --output="gnodes_pipe.%j.log"
#.. SBATCH --partition=debug
#.. SBATCH -t 4:00:00
#SBATCH --partition=general
#SBATCH -t 18:00:00
#SBATCH --nodes=1
#SBATCH --ntasks-per-node=4
#SBATCH --mem=64G
#SBATCH --export=ALL

dv=e
export ncpu=4 maxmem=66000 maxmemGB=50;

# input asmids, seqt seqd sequcg kid did dit
# kmer=55; kid=kmc55e; did=SRR9087597; dit=r597; kcu=0;
# asmset="human22ttot_chr human19grc_chr human20ash_chr";

if [ "X" = "X$seqt" ]; then seqt=hs22ttchr_strf; fi
if [ "X" = "X$seqd" ]; then seqd=human18nc_cds1t_dna2dupx; fi
if [ "X" = "X$sucg" ]; then sucg=human18nc_cds1t_dna2ucg; fi
if [ "X" = "X$kmer" ]; then kmer=55; fi
if [ "X" = "X$did" ]; then did=SRR9087597; fi

kid=kmc${kmer}e
dit=`env d=$did perl -e'print "r",substr($ENV{d},-3),"\n";'`; 
kcu=0

if [ "X" = "X$asmset" ]; then 
asmset="human22ttot_chr human19grc_chr human20ash_chr";
fi

#....
echo "START " `date`
echo "asmset=$asmset"
echo "kid=$kid did=$did seqt=$seqt seqd=$seqd sucg=$sucg"

nucg=`gunzip -c $kid.$sucg.$did.cra.dump.gz | wc -l`; 
halfucg=$(($nucg / 2));
kcu=`gunzip -c $kid.$sucg.$did.cra.dump.gz | sort -k2,2nr | head -$halfucg | tail -n1 | cut -f2` 
echo "kcu=$kcu nucg=$nucg for $kid.$sucg.$did.cra.dump.gz"
if [ $kcu -lt 1 ]; then
  echo "ERR kcu=$kcu no good"; exit -1;
fi

for seq in $seqt $seqd; do {
if [ ! -s $kid.$seq.$did.cra.merc ]; then
gunzip -c $kid.$seq.$did.cra.dump.gz |  cut -f1,2 > $kid.$seq.$did.cra.merc
fi
} done

for pt in $asmset; do {

if [ -s $kid.$pt.$dit.cra3gdupstr.rtab ]; then
  echo "reusing output $kid.$pt.$dit.cra3gdupstr.rtab"; continue
fi

if [ ! -s $kid.$pt.$did.cra.dump.gz ]; then
  echo "Missing $kid.$pt.$did.cra.dump.gz"; continue;
fi

echo "output to " $kid.$pt.$dit.cra3gdupstr.rtab

for seq in $seqt $seqd; do {

 if [ -s $kid.$pt-$seq.$did.cra.npair ]; then echo "reuse $kid.$pt-$seq.$did.cra.npair"; continue; fi
 if [ ! -s $kid.$seq.$did.cra.merc ]; then echo "Missing $kid.$seq.$did.cra.merc"; continue; fi

env cramerc=$kid.$seq.$did.cra.merc cradump=$kid.$pt.$did.cra.dump.gz perl -e \
'$merf=$ENV{cramerc}; $ardf=$ENV{cradump};
open(F,$merf); while(<F>){ next if(/^\W/); ($m,$cr)= split; $mok{$m}=$cr||1; } close(F);
if($ardf=~/.gz/){ $ok=open(D,"gunzip -c $ardf |"); } else { $ok=open(D,$ardf); }
while(<D>){ ($m,$cr,$ca)=split; if($mok{$m}){ print "$cr\t$ca\n"; $mok{$m}=0; } } close(D);
END{ for $m (sort keys %mok){ $cr=$mok{$m}; print "$cr\t0\n" if($cr>0);} } ' \
  > $kid.$pt-$seq.$did.hptmp

  sort -k1,1nr -k2,2nr $kid.$pt-$seq.$did.hptmp | uniq -c | perl -ne \
'($c,$r,$a)=split; next if($r eq "Reads"); print join("\t",$r,$a,$c)."\n"; BEGIN{ print join("\t",qw(Cread Casm Nit))."\n"; }' \
 >  $kid.$pt-$seq.$did.cra.npair

  if [ -s $kid.$pt-$seq.$did.cra.npair ]; then rm $kid.$pt-$seq.$did.hptmp; fi

} done
# seqt,d

env cu=$kcu perl -ne 'BEGIN{ $CU=$ENV{cu}||15; $LOG10=log(10);  } @v=split; if(/^Cread/){ print join("\t",@v,qw(Cnread logCnread logCasm class))."\n" if(1 > $hdr++); my($kid,$asm,$flav,$did)= split/\W+/,$ARGV; $clas=($ARGV =~ /strf/)?"str":($ARGV =~ /dupx/)?"gdup":"other"; print "# class=$clas, asm=$asm, reads=$did, classfn=$flav of $ARGV\n"; next; } ($cr,$ca,$ni)=@v; $cnr=$cr/$CU; $fc=($cnr<2)?"%.3f":"%.0f"; $cnr=sprintf($fc,$cnr); $lcnr=log10(1+$cnr); $lca=log10(1+$ca); print join("\t",@v,$cnr,$lcnr,$lca,$clas)."\n"; sub log10{ my($v)=@_; return sprintf"%.5f", log($v)/$LOG10;}' \
  $kid.$pt-{$seqd,$seqt}.$did.cra.npair \
  > $kid.$pt.$dit.cra3gdupstr.rtab

} done
# asmset

echo "DONE " `date`

