#!/bin/bash
# this script simplifies a gfa graph using gfaview (tip, bubbles)
#
# it takes as input a GFA file that doesn't have any sequences, along with the .sequences file that do contain them
# it will output a simplified GFA file that contains sequences
# the trick is that sequences will be incorporated after the first round of simplifications

# boilerplate code for finding folder where this script really resides
# from https://stackoverflow.com/questions/59895/get-the-source-directory-of-a-bash-script-from-within-the-script-itself
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
  DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"
  SOURCE="$(readlink "$SOURCE")"
  [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # for relative symlink
done
DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"

base="$1"

set -x

if ! command -v gfatools &> /dev/null
then
echo "please install gfatools"
    exit
fi


/usr/bin/time gfatools asm $base.gfa       -t 10,50000 -t 10,50000 -b 100000 -b 100000 -t 10,50000 -b 100000 -b 100000 -b 100000 -t 10,50000 -b 100000 -t 10,50000 -b 1000000 -t 10,150000 -b 1000000 -u > $base.tmp1.gfa
/opt/gensoft/exe/rust-mdbg/1.0.1/venv/bin/python3.8 $DIR/gfa_break_loops.py $base.tmp1.gfa > $base.tmp2.gfa

# reincorporation of sequences into the gfa
# except if you pass --old-behavior, for usage with complete_gfa.py
if [[ ! "$*" == *--old-behavior* ]]
then
    to_basespace --gfa $base.tmp2.gfa --sequences $base
    mv $base.tmp2.gfa.complete.gfa $base.tmp2.gfa
fi

# https://stackoverflow.com/a/15522969
filesize=$(ls -Lon "$base.tmp2.gfa" | awk '{ print $4 }')

current_graph=$base.tmp2.gfa

if (( filesize > 1000000)); then
    /usr/bin/time gfatools asm $current_graph  -t 10,50000 -b 100000 -t 10,100000  -b 1000000 -t 10,150000 -b 1000000 -u > $base.tmp3.gfa
    current_graph=$base.tmp3.gfa
fi

# for large genomes: more aggressive tip clipping
if (( filesize > 100000000)); then
    /opt/gensoft/exe/rust-mdbg/1.0.1/venv/bin/python3.8 $DIR/gfa_break_loops.py $current_graph > $base.tmp4.gfa
    current_graph=$base.tmp4.gfa
    /usr/bin/time gfatools asm $current_graph  -t 10,50000 -b 100000 -t 10,100000  -b 1000000 -t 10,200000 -b 1000000 -u > $base.msimpl.gfa
else
    mv $current_graph  $base.msimpl.gfa
fi

if [[ $@ != *'--keep'* ]]
then
    rm -rf $base.tmp*.gfa
fi


bash $DIR/gfa2fasta.sh $base.msimpl
