#! /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 48:00:00
#SBATCH --nodes=1
#SBATCH --ntasks-per-node=128
#SBATCH --mem=500G
#SBATCH --export=ALL

dv=e
export ncpu=128 maxmem=500000 maxmemGB=400;

if [ "X" = "X$asmset" ]; then echo "ERR: asmset =what?"; exit -1; fi
if [ "X" = "X$reads" ]; then echo "ERR:reads=what?"; exit -1; fi
if [ "X" = "X$kmer" ]; then echo "ERR:kmer=what?"; exit -1; fi
# if [ "X" = "X$datad" ]; then echo "ERR:datad=what?"; exit -1; fi

# rdump=10 means dump readdb with -ci10 min count, adump=1 = dump asmdb w/ -ci1
if [ "X" = "X$rdump" ]; then rdump=0; fi
if [ "X" = "X$adump" ]; then adump=0; fi

# drop -v opt
kmopt="-hp -ci1 -cs999999999"
ktopt="-hp"
# add ?? -cx999999999" for histogram to get hicopy millions, but need to remove many zeros

kbin=$HOME/bio/repeats/kmc/bin
export PATH=$PATH:$kbin

k=$kmer
rname=`echo $reads | sed 's/ .*//;'`
rname=`basename $rname | sed 's/.gz//; s/\.fa.*$//; s/\.fq//; s/_[12]//; s/ .*//;'`

## kmcdir is tmpdir not output
kpre=kmc$kmer$dv
kmcdir=${kpre}tmp
kmcdb=${kpre}db
readdb=$kpre.$rname.res

echo "#START " `date`
echo "kmer=$kmer reads=$reads"
echo "asmset=$asmset"
mkdir $kmcdir
mkdir $kmcdb

if [ ! -s $readdb.kmc_pre ]; then
  readone=`echo $reads | sed 's/ .*//;'`;
  readfq=`echo $readone | sed 's/\.fastq//; s/\.fq//;'`
  ftype=m; if [ $readfq != $readone ]; then ftype=q; fi

  if [ 1 = 1 ]; then
  # fixme: 2+ reads, kmc wants @file.list
  # this dont work: @filelist ; kmc fails unknown err..
  ls $reads > $rname.list

  echo $kbin/kmc $kmopt -k$kmer -t$ncpu -m$maxmemGB -f$ftype @$rname.list $readdb $kmcdir
  $kbin/kmc $kmopt -k$kmer -t$ncpu -m$maxmemGB -f$ftype @$rname.list $readdb $kmcdir

  else 
  echo $kbin/kmc $kmopt -k$kmer -t$ncpu -m$maxmemGB -f$ftype $reads $readdb $kmcdir
  $kbin/kmc $kmopt -k$kmer -t$ncpu -m$maxmemGB -f$ftype $reads $readdb $kmcdir
  fi

if [ ! -s $readdb.kmc_pre ]; then
  echo "ERR: no $readdb.kmc_pre" ; exit -1;
fi

  $kbin/kmc_tools $ktopt -t$ncpu transform $readdb histogram $kpre.$rname.hist -cx99999999 
  # remove zeros from hist..
  # grep -v ' 0$'  $kpre.$rname.hist  >  $kpre.$rname.histnz

  if [ $rdump -gt 0 ]; then
    $kbin/kmc_dump -ci$rdump $readdb $kpre.$rname.ci$rdump.dump
  fi
fi

for asm in $asmset; do {

aname=`basename $asm | sed 's/\.fq//; s/\.fa.*$//;'`
asmdb=$kpre.$aname.res

if [ ! -s $asmdb.kmc_pre ]; then
  asmfq=`echo $asm | sed 's/\.fastq//; s/\.fq//;'`
  ftype=m; if [ $asmfq != $asm ]; then ftype=q; fi

  echo $kbin/kmc $kmopt -k$kmer -t$ncpu -m$maxmemGB -f$ftype $asm $asmdb $kmcdir
  $kbin/kmc $kmopt -k$kmer -t$ncpu -m$maxmemGB -f$ftype $asm $asmdb $kmcdir

if [ ! -s $asmdb.kmc_pre ]; then
  echo "ERR: no $asmdb.kmc_pre" ; continue;
fi

  if [ $adump -gt 0 ]; then
    $kbin/kmc_dump -ci$adump $asmdb $kpre.$aname.ci$adump.dump
  fi

fi

oboth=$kpre.$aname.$rname

$kbin/kmc_tools $ktopt -t$ncpu simple $readdb $asmdb  \
 intersect $oboth.rd.res -ocleft  intersect $oboth.as.res -ocright

$kbin/kmc_tools $ktopt -t$ncpu transform \
  $oboth.rd.res histogram $oboth.rd.hist -cx99999999 dump $oboth.rd.dump
$kbin/kmc_tools $ktopt -t$ncpu transform \
  $oboth.as.res histogram $oboth.as.hist -cx999999 dump $oboth.as.dump

if [ -s $oboth.rd.hist -a -s $oboth.as.hist ]; then
perl -pe 's/$/\tza/;' $oboth.as.hist | sort -k1,1n -k3,3 $oboth.rd.hist - | \
perl -ne 'BEGIN{ print "#Nmer\tReads\tAsm\n"; } ($m,$c)=split;  if($m ne $lm){ if($lc){ print "$lm\t$lc\t0\n"; } } else { print "$m\t$lc\t$c\n" if($lc or $c); $c=0; } $lc=$c; $lm=$m; END{ if($lc){ print "$lm\t$lc\t0\n"; } }' \
  >  $oboth.cra.hist
fi

if [ -s $oboth.rd.dump -a -s $oboth.as.dump ]; then
perl -pe 's/$/\tza/;' $oboth.as.dump | sort -k1,1 -k3,3 $oboth.rd.dump - | \
perl -ne 'BEGIN{ print "#Kmer\tReads\tAsm\n"; } ($m,$c)=split; if($m ne $lm){ if($lc){ print "$lm\t$lc\t0\n"; } } else { print "$m\t$lc\t$c\n"; $c=0; } $lc=$c; $lm=$m; END{ if($lc){ print "$lm\t$lc\t0\n"; } }' \
    >  $oboth.cra.dump
fi

mv $oboth.rd.hist  $oboth.as.hist $oboth.as.dump $oboth.rd.dump $oboth.*.res.kmc* $kmcdb/

} done
# asm in asmset

echo "DONE " `date`

