#! /bin/bash
# run_trf2d.sh : trf = tandem repeat finder
## --- gnodes_setup.sh for quartz.iu ---    
#SBATCH --job-name="gnodes_pipe"
#SBATCH --output="gnodes_pipe.%j.log"
#SBATCH --partition=debug
#.. SBATCH --partition=general
#SBATCH -t 24:00:00
#SBATCH --nodes=1
#SBATCH --ntasks-per-node=32
#SBATCH --mem=120G
#SBATCH --export=ALL

if [ X = "X$chrfa" ]; then echo "ERR chrfa=what? "; exit -1; fi
if [ X = "X$datad" ]; then echo "ERR datad=what path"; exit -1; fi
if [ X = "X$nosplit" ]; then nosplit=0; fi
if [ X = "X$nolift" ]; then nolift=0; fi

# nbin=$HOME/bio/ncbi/bin; export PATH=$nbin:$PATH;
jkbin=$HOME/bio/apps/kent/bin
trbin=$HOME/bio/gnomutil/tandreps/bin
trfopt="2 7 7 80 10 50 500 -l 1 -d -h"

export NCPU=32
export EVIGENES=$HOME/bio/apps/evigene/scripts
export PATH=$PATH:$EVIGENES

cd $datad
echo "START trf " `date`

if [ $nosplit = 0 ]; then
  chrna=`basename $chrfa .fa | sed 's/\.gz//; s/\.[a-z]*$//;'`
  cdir=${chrna}_spl; mkdir $cdir
  odir=${chrna}_trf; mkdir $odir
  
  crs=`ls $cdir/*_sp*.fa`
  if [ X = "X$crs" ]; then
    echo $jkbin/faSplit byname,size $chrfa $cdir/
    $jkbin/faSplit byname $chrfa $cdir/
    for cr in $cdir/*.fa; do { 
      cn=`basename $cr .fa`
      $jkbin/faSplit size $cr 10000000 $cdir/${cn}_sp -lift=$cdir/${cn}_sp.lft
    } done
  fi
  # human22ttot_chr_spl/hs*_sp*.fa = 328 parts of <=10Mb from 25 chrs for 3100 Mb chrasm
  
  echo $trbin/trf split-$chrfa $trfopt
  icpu=0; outd="";
  for cr in $cdir/*_sp*.fa; do {
    outd="$outd $cr.*.dat";
    ## outname== $cr.2.7.7.80.10.50.500.dat == param vals, dont need 
    echo trf $cr $trfopt ;
    crn=`basename $cr`
    ( $trbin/trf $cr $trfopt ; if [ -f $crn.*.dat ]; then mv $crn.*.dat $odir/; fi ) &
    
    icpu=$(($icpu + 1)); 
    if [ $icpu -ge $NCPU ]; then wait; icpu=0; fi
  } done
  wait;
  
  # trf chr.split.fa seems ok; 128/328 10Mb parts done in ~1hr, 32 cpu, 120 Gb mem; 
  # .. all 3100 Mb, as 328 parts done in ~3hr x 32cpu, vs 5hr for chr9 139 Mb only, 1cpu process
  
  rename '2.7.7.80.10.50.500' trfout  $odir/*.2.7.7.80.10.50.500.dat 

  if [ $nolift = 0 ]; then
  # lift odir/*.trfout.dat by split locations, use cdir/chr*_sp.lft  
  env odir=$odir perl -ne \
'next if(/^\D/); ($bof,$csp,$blen,$cid)=split; $df="$ENV{odir}/$csp.fa.trfout.dat"; 
unless(open(F,$df)){ warn "#ERR: miss $df\n"; next; } open(O,">$df.lf"); 
while(<F>) { 
  if(/^\d/){ ($cb,$ce)=@v=split; map{ $_ += $bof; } @v[0,1]; print O join(" ",@v)."\n"; $nr++; } 
  else { print O $_; } 
} close(O); close(F); $no++;
rename($df,"$df.old") if($ENV{keepold}); rename("$df.lf",$df); 
END{ warn "#lft offset no=$no, nr=$nr\n"; }' \
 $cdir/*_sp.lft

   ## convert trfout.dat to usable table
   perl -ne '($cr,$cw,$ty)=@v=split; if(/^Sequence:\s*(\S+)/){ putv() if(@val); $crid=$1; $crid=~s/_sp\d+$//; $intrd=1; } elsif(/^Parameters:/){ } elsif($intrd and /^\d/){ $tyc="STR"; $tyc{$tyc}++; ($cb,$ce,$ps,$cpn,$clen,$pi,$pd,$scr,$Ap,$Cp,$Gp,$Tp,$escr,$cseq,$mseq)=@v; $mlen=length($mseq); $bw=1+$ce - $cb; push @val, [$crid,$tyc,"repeat",$cb,$ce,$bw,$clen,$cpn,"$pi/$pd","$Ap,$Cp,$Gp,$Tp",$cseq]; } BEGIN{ @hd=qw(source_id class type begin end tlen clen copy pipd acgt cseq);  print join("\t",@hd)."\n"; } END{ putv() if(@val); } sub putv{ for $cv (@val){ print join("\t",@$cv)."\n"; } @val=();  }' \
   $odir/*trfout.dat > ${chrna}_trf.tab

 fi

else 
 echo $trbin/trf $chrfa $trfopt
 $trbin/trf $chrfa $trfopt
fi

echo "DONE trf " `date`
exit;

# trf File Match Mismatch Delta PM PI Minscore MaxPeriod [options]
# $trbin/trf testf/human19grc_chr9top50k.fa 2 7 7 80 10 50 500 -h
# -h = no out.html; -d = out.dat
# try1a: 4hr for hum_chr9.fa 139 Mb and no output yet.. takes too long for large seq, break down to 10 Mb bites?
# try : -l 1 ;  -l <n>: Specifies that the longest TR array expected; def=2 mill
# .. split to chunks 10Mb for par runs, trf to 5 hr on chr9.fa , 1 cpu
# pt=chr12; $jkbin/faSplit size human19grc_chrs/$pt.fa 10000000 human19grc_chrc/hum_${pt}_ -lift=humspl.$pt.lft
# for many seqs/chrfa, extract chrN.fa, loop each as 1 cpu job
#------------
# wc -l hs22*.dat     
#     4919 hs22tt_chr10_sp00.fa.2.7.7.80.10.50.500.dat
#     3552 hs22tt_chr10_sp01.fa.2.7.7.80.10.50.500.dat
# 31 done quick; wait on hs22tt_chr10_sp04.fa.2.7.7.80.10.50.500.dat ..
# 61 done quick2; wait on 3..
#------
# ~/bio/apps/exonerate/bin/fastaexplode -d human19grc_chrd -f ../human19grc_chr.fa
# creates human19grc_chrd/chr[1..9].fa chr[12]n.fa and lots of tiny extras chrun_xxx.fa NTnnn.fa
# human22ttot_chr.fa has 25 only
# human19grc_chr.fa has 25 main chr and lots of tiny parts
# human19grc_chrs/
# chr1.fa   chr12.fa  chr15.fa  chr18.fa chr20.fa  chr3.fa  chr6.fa  chr9.fa  chrY.fa
# chr10.fa  chr13.fa  chr16.fa  chr19.fa chr21.fa  chr4.fa  chr7.fa  chrM.fa
# chr11.fa  chr14.fa  chr17.fa  chr2.fa	chr22.fa  chr5.fa  chr8.fa  chrX.fa
#-------
# split.dat to lifted dat

#--
# env odir=human22ttot_chr_trf perl -ne 'next if(/^\D/); ($bof,$csp,$blen,$cid)=split; $df="$ENV{odir}/$csp.fa.trfout.dat"; unless(open(F,$df)){ warn "#ERR: miss $df\n"; next; } open(O,">$df.lf"); while(<F>) { if(/^\d/){ ($cb,$ce)=@v=split; map{ $_ += $bof; } @v[0,1]; print O join(" ",@v)."\n"; $nr++; } else { print O $_; } } close(O); close(F); $no++; rename($df,"$df.old"); rename("$df.lf",$df);  END{ warn "#lft offset no=$no, nr=$nr\n"; }' human22ttot_chr_spl/hs22tt_chr10_sp.lft
#lft offset no=14, nr=46042

# human22ttot_chr_spl/hs22tt_chr10_sp.lft
# 0       	hs22tt_chr10_sp00	10000000	hs22tt_chr10	134758134
# 10000000	hs22tt_chr10_sp01	10000000	hs22tt_chr10	134758134
# 20000000	hs22tt_chr10_sp02	10000000	hs22tt_chr10	134758134
# 30000000	hs22tt_chr10_sp03	10000000	hs22tt_chr10	134758134
# 40000000	hs22tt_chr10_sp04	10000000	hs22tt_chr10	134758134
# 50000000	hs22tt_chr10_sp05	10000000	hs22tt_chr10	134758134
# 60000000	hs22tt_chr10_sp06	10000000	hs22tt_chr10	134758134
# 70000000	hs22tt_chr10_sp07	10000000	hs22tt_chr10	134758134
# 80000000	hs22tt_chr10_sp08	10000000	hs22tt_chr10	134758134
# 90000000	hs22tt_chr10_sp09	10000000	hs22tt_chr10	134758134
# 100000000	hs22tt_chr10_sp10	10000000	hs22tt_chr10	134758134
# 110000000	hs22tt_chr10_sp11	10000000	hs22tt_chr10	134758134
# 120000000	hs22tt_chr10_sp12	10000000	hs22tt_chr10	134758134
# 130000000	hs22tt_chr10_sp13	4758134	hs22tt_chr10	134758134
#---
#  hs22tt_chr10_sp03.fa offset=30000000
#  head -22 hs22tt_chr10_sp03.fa.2.7.7.80.10.50.500.dat | tail
# Parameters: 2 7 7 80 10 50 500
# 
# 200 227 14 2.0 14 100 0 56 7 7 42 42 1.59 TGTGTGTCAGTGTG TGTGTGTCAGTGTGTGTGTGTCAGTGTG
# 993 1024 2 16.0 2 93 0 55 50 46 0 3 1.17 CA CACACATACACACACACACACACACACACACA
# 4393 4430 1 38.0 1 100 0 76 100 0 0 0 0.00 A AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
# 8742 8782 20 2.0 20 90 0 64 39 2 12 46 1.54 TTATTTGATGTAAAAAATTA TTATTTGATGTGAAACATTATTATTTGATGTAAAAAATTAT
# 12871 12903 14 2.3 15 89 5 50 84 0 3 12 0.72 TAAAAAAAAAAAAGA TAAAAAATAAAAAATAAAAAAAAAAAAGATAAA
# 13252 13285 17 2.0 17 94 0 59 38 5 14 41 1.70 ATTATATCATTAAAGTG ATTATATCATTAAAGTGATTATATCATTGAAGTG
# 14006 14070 19 3.4 19 74 21 55 38 1 58 1 1.17 AGGAAGGGACAGAAGGAGG AGGAAGGGAAGAAGGAGTGAGGAAGGGACAGAGGAAGGGGAGAGGGAGGGAAGGAGGGAGGAAGG
# 
