#!/bin/bash
# -- this is an old version that used gfaview and also used a bunch of script to keep trace fo sequences corresponding to the (non-seq) gfa file
# this script simplifies a gfa graph using gfaview (tip, bubbles)
# and outputs a fasta assembly 

# boilerplate code for finding folder where this script really reised
#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" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"

if [[ $# -eq 0 ]] ; then
    #echo 'running main.py'
    #/opt/gensoft/exe/rust-mdbg/1.0.1/venv/bin/python3.8 main.py
    base="graph"
else
    base="$1"
fi
set -x

GFAVIEW_PARAMS=""
if [[ "$*" == *--only-compact* ]]
then
        GFAVIEW_PARAMS="-u"
else
        GFAVIEW_PARAMS="-t -b -b -b -b -r -u"
fi

#gfaview $base.gfa -u > $base.unitigs.gfa
#gfaview $base.gfa -t -u > $base.notips.gfa
#gfaview $base.gfa                 $GFAVIEW_PARAMS > $base.notips.nobubbles.gfa
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 100000 -u > $base.notips.nobubbles.gfa

#gfaview $base.notips.nobubbles.gfa $GFAVIEW_PARAMS > $base.notips.nobubbles.again.gfa  #FIXME there is a loss of info, it seems, when doing compaction twice. It's not an idempotent operation in gfaview?! removing it until i understand what's going on. To restore: just uncomment all commented #FIXME's and remove all uncommented FIXME's
#echo ~/Desktop/Bandage load $base.notips.nobubbles.again.gfa 

/opt/gensoft/exe/rust-mdbg/1.0.1/venv/bin/python3.8 $DIR/retrace_minimizers.py $base.sequences $base.notips.nobubbles.gfa
#/opt/gensoft/exe/rust-mdbg/1.0.1/venv/bin/python3.8 $DIR/retrace_minimizers.py $base.notips.nobubbles.sequences $base.notips.nobubbles.again.gfa # FIXME

#/opt/gensoft/exe/rust-mdbg/1.0.1/venv/bin/python3.8 $DIR/output_basic_sequences.py  $base.sequences $base.notips.nobubbles.again.sequences # FIXME
/opt/gensoft/exe/rust-mdbg/1.0.1/venv/bin/python3.8 $DIR/output_basic_sequences.py  $base.sequences $base.notips.nobubbles.sequences # FIXME
#/opt/gensoft/exe/rust-mdbg/1.0.1/venv/bin/python3.8 $DIR/sequences_file_to_fasta.py  $base.notips.nobubbles.again.sequences $base.notips.nobubbles.again.fasta # FIXME
/opt/gensoft/exe/rust-mdbg/1.0.1/venv/bin/python3.8 $DIR/sequences_file_to_fasta.py  $base.notips.nobubbles.sequences $base.notips.nobubbles.fasta # FIXME

# finishing up
#mv $base.notips.nobubbles.again.gfa $base.assembled.gfa # FIXME
#mv $base.notips.nobubbles.again.fasta $base.assembled.fasta # FIXME
mv $base.notips.nobubbles.gfa $base.assembled.gfa #FIXME
mv $base.notips.nobubbles.fasta $base.assembled.fasta # FIXME

# cleanup
#rm -f $base.gfa $base.notips.nobubbles.gfa $base.notips.nobubbles.sequences $base.sequences $base.notips.nobubbles.again.sequences
