#! /bin/bash
## run_gnodes9at1knopcr.sh
## --- gnodes_setup.sh for quartz.iu ---    
#SBATCH --job-name="gnodeq_pipe"
#SBATCH --output="gnodeq_pipe.%j.log"
#SBATCH --partition=general
#SBATCH -t 48:00:00
#SBATCH --nodes=1
#SBATCH --ntasks-per-node=32
#SBATCH --mem=120G
#SBATCH --export=ALL

export NCPU=32 MEMT=120G MEMC=3G

# 22jun for 36 PCR-free AT DNA samples, cmp to MGSE, w/ readbases parts counted
# using genoasm/gnodes_sam2covtab22may.pl ; tag cc9k
# SRRIDS from at1k_nopcr19_gnodext4mgse_tab.txt n=35
# egrep 'NoPCR|USA|Wu'  at1k_nopcr19_gnodext4mgse_tab.txt | cut -f1,2,5,11,12,13,24,25,26-
# NoPCR_ID      Etype,      Country  LNCmap  LNCtot     CU      CONTAM  covspan Maprd,pMap      tabdate         MGSE:Cu,GSE,LNr
# ERR2178796    7415,Wu-0       GER     164     172     39.80   58      115.8   35348173,96.4   2022_Mar_21     41,150,6167978268
# ERR2178775    6909,Col-0      USA     167     177     61.61   13      119.4   44090931,94.6   2022_Mar_20     62,130,8101931233

# srrid=ERR2178775
if [ X = "X$intab" ]; then intab=at1k_nopcr19_gnodext4mgse_tab.txt; fi
if [ X = "X$srrid" -a -s $intab ]; then srrid=`cut -f1 $intab | egrep '^[EDS]RR'`; fi
if [ X = "X$srrid" ]; then echo srrid=what; exit -1; fi
if [ X = "X$readsf" ]; then readsf="readat1knopcr"; fi
if [ X = "X$datad" ]; then echo datad=what; exit -1; fi
if [ X = "X$ttag" ]; then ttag="at20nopcr"; fi

genefa=arath18tair1cds.fa
idclass=arath18tair1cds.idclass
chrfa=gdata/arath18tair_chr.fa
anntab=arath18tair_chr.anntab
metad=arath20asm.metad

asmid=`basename $chrfa .fa`
genena=`basename $genefa .fa`;
if [ X = "X$title" ]; then title=${asmid}_$ttag; fi

nbin=$HOME/bio/ncbi/bin; export PATH=$nbin:$PATH;
sambin=$HOME/bio/apps/bin; export PATH=$sambin:$PATH;
sradumpr=$HOME/bio/sratoolkit/sratoolkit300/bin/fasterq-dump

mimbin=$HOME/bio/gnomutil/flye28/bin;  export PATH=$mimbin:$PATH; # export RDMAPPER=minimap2;
if [ X = "X$mimopt" ]; then 
  mimopt="-x sr --secondary=yes -N 9 -p 0.80 "
  # mimopt="-x map-pb" for pachifi, map-ont ..
fi

export EVIGENES=$HOME/bio/apps/evigene/scripts
export PATH=$PATH:$EVIGENES
## --- end gnodes_setup.sh ---    

cd $datad
echo START gnodes $title  `date`

if [ ! -s $chrfa ]; then  echo "MISS chrfa: $chrfa"; exit -1; fi
if [ ! -s $genefa ]; then  echo "MISS genefa: $genefa"; exit -1; fi
if [ ! -s $anntab ]; then  echo "MISS anntab: $anntab"; exit -1; fi
if [ ! -s $idclass ]; then  echo "MISS idclass: $idclass"; exit -1; fi
if [ ! -s $metad ]; then  echo "MISS metad: $metad"; exit -1; fi

mkdir  ${title}_out/
mkdir  ${title}_data/
mkdir  ${title}_tmp/
mkdir parts
# parts == _tmp ??

#====== loop here for SRRid, download reads =============
# echo "for sid in $srrid ... ";
if [ -s $intab ]; then
 echo -n "INFO "; grep "NoPCR_ID" $intab | cut -f1,5,11,12,13,24,25,26,28;
fi

# sid=$srrid
for sid in $srrid; do {

echo START gnodes  $sid  `date`
if [ -s $intab ]; then
 sinfo=`grep "$sid" $intab | cut -f1,5,11,12,13,24,25,26,28`
 echo "INFO " $sinfo; 
fi

reads=$readsf/${sid}_[12].fastq.gz
read1=$readsf/${sid}_1.fastq.gz

readna=`basename $reads | sed 's/\.gz//; s/\.[a-z0-9]*$//;' `
readna=`echo $readna | sed 's/_[12].*/_ab/;'`
chrbname=${asmid}_${readna}_mim 
genebname=${genena}_${readna}_mim 
tmpf=""; outf=""

if [ -s  ${title}_out/$chrbname.cc9k_sum.txt ]; then
  echo "Done ${title}_out/$chrbname.cc9k_sum.txt"; continue;
fi

if [ -s $read1 ]; then 
  echo using $read1
else
  echo NOTRUN: $sradumpr --threads $NCPU --outdir ${title}_data/ --split-3 $sid TO  ${title}_data/sradump.$sid.log 
fi
if [ ! -s $read1 ]; then  echo "SKIP no sra data: $read1"; continue; fi

if [ -s $genebname.bam ]; then 
  echo reusing $genebname.bam;
else
  echo START_dnamap  `date`

  ( minimap2 $mimopt -a -t $NCPU $genefa $reads | samtools view --threads $NCPU -Sb -o $genebname.bam - ) > ${genebname}.log 2>&1

  #? samtools flagstats --threads $NCPU $genebname.bam  > $genebname.fstats.txt
  tmpf="$tmpf $genebname.bam ${genebname}.log"
  echo DONE_dnamap `date`
fi

if [ -s $chrbname.bam ]; then 
  echo reusing $chrbname.bam;
else
  echo START_dnamap  `date`

  ( minimap2 $mimopt -a -t $NCPU $chrfa $reads | samtools view --threads $NCPU -Sb -o $chrbname.bam - ) > ${chrbname}.log 2>&1

  #? samtools flagstats --threads $NCPU $chrbname.bam  > $chrbname.fstats.txt
  tmpf="$tmpf $chrbname.bam ${chrbname}.log"
  echo DONE_dnamap `date`
fi

if [ -s $genebname.bam -a ! -s $genebname.genexcopy ]; then  
  echo START_genecov  `date`
  $EVIGENES/genoasm/gnodes_sam2genecov.pl  -idclassf $idclass -debug -output $genebname.genexcopy -bam $genebname.bam
  outf="$outf $genebname.genexcopy $genebname.genefamtab"
  echo DONE_genecov  `date`
fi

if [ -s $genebname.cc9k.covtab ]; then
  echo reusing $genebname.cc9k.covtab $genebname.cc9k.chrtab;
else
  echo START_sam2covtab  `date`
  i=0; while [ $i -lt $NCPU ]; do {
    $EVIGENES/genoasm/gnodes_sam2covtab22may.pl -icpu $i -ncpu $NCPU -nodebug  -crclassf=$idclass \
      -out $genebname.cc9k.covtab -bam $genebname.bam  &
    sleep 5; 
    i=$(($i + 1));
  } done
  
  wait
 
  $EVIGENES/genoasm/gnodes_sam2covtab22may.pl  -merge  -nodebug  -crclassf=$idclass \
   -out $genebname.cc9k.covtab  -bam $genebname.bam 
  mv $genebname.cc9k.covtab.pt* $genebname.cc9k.chrtab.pt*  parts/
  outf="$outf $genebname.cc9k.covtab $genebname.cc9k.chrtab "
  echo DONE_sam2covtab  `date`
fi

if [ -s $genebname.cc9k.covtab -a ! -s $genebname.cc9k_sum.txt ]; then  
  echo START_covsum  `date`
  $EVIGENES/genoasm/gnodes_covsum.pl  -asmid $genena -title $title.gn -crclass $idclass -genexcopy $genebname.genexcopy \
    -sumdata $metad -debug -output $genebname.cc9k_sum.txt $genebname.cc9k.covtab
  tmpf="$tmpf $genebname.cc9k.means "
  outf="$outf $genebname.cc9k_sum.txt "
  echo DONE_covsum  `date`
fi

# run_gnodes1_sam_chrcds2cov $chrbname.cc9k.covtab
if [ -s $chrbname.cc9k.covtab ]; then
  echo reusing $chrbname.cc9k.covtab $chrbname.cc9k.chrtab;
else
  echo START_sam2covtab $chrbname.cc9k.covtab `date`
  i=0; while [ $i -lt $NCPU ]; do {
    $EVIGENES/genoasm/gnodes_sam2covtab22may.pl -icpu $i -ncpu $NCPU -nodebug  -crclassf=$idclass -genetab  \
      -out $chrbname.cc9k.covtab -cdsbam $genebname.bam -bam $chrbname.bam  &
    sleep 5; 
    i=$(($i + 1));
  } done
  
  wait
 
  $EVIGENES/genoasm/gnodes_sam2covtab22may.pl  -merge -nodebug  -crclassf=$idclass -genetab \
    -out $chrbname.cc9k.covtab  -bam $chrbname.bam 
  mv $chrbname.cc9k.covtab.pt* $chrbname.cc9k.chrtab.pt* $chrbname.cc9k.genetab.pt*  parts/
  outf="$outf $chrbname.cc9k.covtab $chrbname.cc9k.chrtab $chrbname.cc9k.genetab "
  echo DONE_sam2covtab  `date`
fi

if [ -s $chrbname.cc9k.covtab ]; then  
  echo START_covsum  `date`
  $EVIGENES/genoasm/gnodes_covsum.pl  -asmid $asmid -title $title -anntab $anntab -crclass $idclass \
    -genexcopy $genebname.genexcopy -sumdata $metad -debug -output $chrbname.cc9k_sum.txt $chrbname.cc9k.covtab
  tmpf="$tmpf $chrbname.cc9k.means "
  outf="$outf $chrbname.cc9k_sum.txt "
  echo DONE_covsum  `date`
fi

# outf="$outf ${chrbname}$ttag.*"
echo "#TEMP parts/ " `du -sh parts`
echo "#TEMPS   : $tmpf ${title}_tmp/"
echo "#OUTPUTS : $outf ${title}_out/"
mv $tmpf  ${title}_tmp/
mv $outf  ${title}_out/
echo DONE gnodes  $sid  `date`

} done
#====== end loop for SRRid, download reads =============

echo DONE gnodes $title  `date`
