#!/bin/bash
set -e
set -u
set -o pipefail


# Goal create sample sheet from all passed samples according to aquamis

# input: aquamis working dir
# input2: list of accessions that should stay (or a file)
# output: samples_assembly_passed.tsv, samples_assembly_fail.tsv


## Helpfile and escape
if [ $# -eq 0 ]; then
    echo "Please provide at least one argument. For the help file, type create_pass_samplesheet.sh  --help"
    [[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1
fi


## search for "--help" flag
if [[ $@ =~ '--help' || $@ =~ '-h' ]]; then
    echo "Help file ----------------------"
    echo "Basic usage: create_pass_samplesheet.sh aquamis_workdir [exceptions] [Options]"
    echo
    echo "aquamis_workdir: Directory where aquamis results were written to"
    echo
    echo "Optional arguments"
    echo "exceptions: a text file that overwrites qc decisions and includes alleged fails in the dataset, one exception per line"
    #echo "--interactive    Ask before execution"
    #echo "--force    Force overwrite existing outdir"
    
    [[ "$0" = "$BASH_SOURCE" ]] && exit 0 || return 0
fi


#if [[ $@ =~ '--interactive' || $@ =~ '-i' ]]; then
	#interactive=true
	#else
	#interactive=false
#fi

#if [[ $@ =~ '--force' || $@ =~ '-f' ]]; then
	#force=true
	#else
	#force=false
#fi


if [ $# -lt 2 ]; then
    echo "lesser than 2 arguments"
    exception_file="NA"
else 
    echo "at least than 2 arguments"
    exception_file=$2
fi

#if [[ -n $2 ]]; then
    #echo "Pos 2 exists"
    #exception_file=$2
#else 
    #echo "Pos 2 NOT exists"
    #exception_file="NA"
#fi

aquamis_workdir=$1

# check if dir/file exist

if [[ ! -d $aquamis_workdir ]]; then
    echo "ERROR: Aquamis workdir $aquamis_workdir does not exist"
    exit 1
fi

if [[ $exception_file != "NA" ]]; then
if [[ ! -f $exception_file ]]; then
    echo "ERROR: exception_file $exception_file does not exist"
    exit 1
fi; fi

# check if correct aquamus files present

report_file="$aquamis_workdir/reports/summary_report.tsv"
assembly_dir="$aquamis_workdir/Assembly/assembly"

if [[ ! -d $assembly_dir ]]; then
    echo "ERROR: Aquamis assembly dir $assembly_dir does not exist"
    exit 1
fi

if [[ ! -f $report_file ]]; then
    echo "ERROR: Aquamis report file $report_file does not exist"
    exit 1
fi

outfile_pass="$assembly_dir/samples_pass.tsv"
outfile_fail="$assembly_dir/samples_fail.tsv"
outfile_warning="$assembly_dir/create_pass_samplesheet.log"

if [[ -f $outfile_pass ]]; then
    echo "WARNING file $outfile_pass already exists and will be overwritten"
fi


echo "Passed all checks"

echo "aquamis_workdir: $aquamis_workdir"
echo "exception_file: $exception_file"

# execution

# initalize emtpy sample sheet
echo -e "sample\tassembly" > $outfile_pass

rm -f $outfile_fail & touch $outfile_fail


# loop through report stats

tail -n +2 $report_file | while read sample qc more; do
    #echo "sample: $sample"
    #echo "qc: $qc"
    if [[ $qc == "FAIL" ]]; then
        echo "sample $sample failed QC"
        echo $sample >> $outfile_fail
    else
        # look up assembly
        sample_assembly_file="$assembly_dir/${sample}.fasta"
        if [[ ! -f $sample_assembly_file ]]; then 
            echo "WARNING: File $sample_assembly_file could not be found";  
        else
            echo -e "$sample\t$sample_assembly_file" >> $outfile_pass 
        fi
    fi
done

#1: Sample_Name                    
#2: QC_Vote 
echo "First summary:"
echo "File $report_file contains `tail -n +2 $report_file | wc -l`"
echo "of these `wc -l $outfile_fail | cut -f 1 -d ' '` samples failed AutoQC"
echo "`tail -n +2 $outfile_pass | wc -l` samples passed AutoQC"

if [[ $exception_file == "NA" ]]; then
    echo "No excepetions specified. Done"
else 
    echo "Found `wc -l $exception_file | cut -f 1 -d ' '` exceptions" 
    cat $exception_file | while read sample more; do
        echo "Adding new exception: $sample"
        sample_assembly_file="$assembly_dir/${sample}.fasta"
        
        if [[ ! -f $sample_assembly_file ]]; then 
            echo "WARNING: File $sample_assembly_file could not be found";  
        else
            echo -e "$sample\t$sample_assembly_file" >> $outfile_pass 
        fi
    done

    echo "`tail -n +2 $outfile_pass | wc -l` samples selected"

fi


echo "All passed samples can be found in the following samplesheet:"
echo "$outfile_pass"
echo
echo "Done."

