#!/bin/sh

#LINKS v.2.0.1 consist of two parts.First part, from reading long reads, 
#	up to pairing contigs and outputting tigpair_checkpoint is running in C++
#	and rest of the pipeline, which is reading tigpair_checkpoints and outputting 
#	scaffolds is still running PERL code(like v1 .8.7 and before).
#
#Whole pipeline can be run by LINKS - make script provided. However,
#	Makefiles take arguments like "d=4000" where LINKS always took arguments as "-d 4000".
#
#We are providing this script to run makefile with LINKS inputs.
#	So you can run whole LINKS pipeline with previuous argument nomenclature.


usage() {
        echo -e "
		Usage:  LINKS v2.0.1 
                -f  sequences to scaffold (Multi-FASTA format, required)
                -s  file-of-filenames, full path to long sequence reads or MPET pairs [see below] (Multi-FASTA/fastq format, required)
                -d  distance between k-mer pairs (ie. target distances to re-scaffold on. default -d 4000, optional)
                    Multiple distances are separated by comma. eg. -d 500,1000,2000,3000
                -k  k-mer value (default -k 15, optional)
                -t  step of sliding window when extracting k-mer pairs from long reads (default -t 2, optional)
                    Multiple steps are separated by comma. eg. -t 10,5
                -j  threads  (default -j 3, optional) 
                -o  offset position for extracting k-mer pairs (default -o 0, optional)
                -e  error (%) allowed on -d distance   e.g. -e 0.1  == distance +/- 10% (default -e 0.1, optional)
                -l  minimum number of links (k-mer pairs) to compute scaffold (default -l 5, optional)
                -a  maximum link ratio between two best contig pairs (default -a 0.3, optional)
                    *higher values lead to least accurate scaffolding*
                -z  minimum contig length to consider for scaffolding (default -z 500, optional)
                -b  base name for your output files (optional)
                -r  Bloom filter input file for sequences supplied in -s (optional, if none provided will output to .bloom)
                    NOTE: BLOOM FILTER MUST BE DERIVED FROM THE SAME FILE SUPPLIED IN -f WITH SAME -k VALUE
                    IF YOU DO NOT SUPPLY A BLOOM FILTER, ONE WILL BE CREATED (.bloom)
                -p  Bloom filter false positive rate (default -p 0.001, optional; increase to prevent memory allocation errors)
                -x  Turn off Bloom filter functionality (-x 1 = yes, default = no, optional)
                -v  Runs in verbose mode (-v 1 = yes, default = no, optional)
	";
}

    while getopts "f:s:d:k:t:j:o:e:l:a:z:b:r:p:x:v:" args;
do
    case $args in
        f)
            lf=${OPTARG}
	    lf="draft=${lf}"
            ;;
        s)
            ls=${OPTARG}
	    ls="readsFof=${ls}"
            ;;
        d)
            ld=${OPTARG}
	    ld="d=${ld}"
            ;;
        k)
            lk=${OPTARG}
	    lk="k=${lk}"
            ;;
        t)
            lt=${OPTARG}
	    lt="t=${lt}"
            ;;
        j)
            lj=${OPTARG}
	    lj="j=${lj}"
            ;;
	    o)
            lo=${OPTARG}
	    lo="o=${lo}"
            ;;
        e)
            le=${OPTARG}
	    le="e=${le}"
            ;;
        l)
            ll=${OPTARG}
	    ll="l=${ll}"
            ;;
        a)
            la=${OPTARG}
	    la="a=${la}"
            ;;
	    z)
            lz=${OPTARG}
	    lz="z=${lz}"
            ;;
        b)
            lb=${OPTARG}
	    lb="b=${lb}"
            ;;
        r)
            lr=${OPTARG}
	    lr="r=${lr}"
            ;;
        p)
            lp=${OPTARG}
	    lp="p=${lp}"
            ;;
	x)
            lx=${OPTARG}
	    lx="x=${lx}"
            ;;
        v)
            lv=${OPTARG}
            lv="v=${lv}"
            ;;
        *)
            usage
            ;;
    esac
done
shift $((OPTIND-1))

#Check if draft genome and long read fof is provided
if [ -z "${lf}" ] || [ -z "${ls}" ]; then
    usage
    exit 1
fi

printf "\nRunning Command: LINKS-make LINKS $lf $ls $ld $lk $lt $lj $lo $le $ll $la $lz $lb $lr $lp $lx $lv \n"
#Running LINKS - make
LINKS-make LINKS $lf $ls $ld $lk $lt $lj $lo $le $ll $la $lz $lb $lr $lp $lx $lv
