#!/bin/sh

##### Configuration #####
#Path of 'tasuke_bin'
SCRIPTS='./'

#Database
BACKEND='mysql'

#Database connection
DB='database'
USER='user'
PASS='password'

#oracle
TABLESP=''

#This script 
DATADIR='./'
DIR_FASTA='./'
DIR_GFF='./'
DIR_VCF='./variants/'
DIR_TSV='./depth/'

#File format of your VCF files
VCF_TYPE='samtools'
#########################

BASE=`dirname $0`
. ${BASE}/install.conf

MODE=$1
EXE_OPT=""
TSV_OPT=""
DB_OPT=""
TSV_COND=""
TSV_MODE=0
USG_MSG+="#########################################################################"
USG_MSG+="\nTASUKE multiple genome browser - version:1.5.0\n"
USG_MSG+="\nThis tool supports installation of TASUKE.\nIt automatically detects any datasets and load the data to a database.\n"
USG_MSG+="\n*Caution!\nThis tool treats each file name as registered 'accession_id'.\nBefore running the tool, confirm relation of file names and accession_id.\n"
USG_MSG+="\nUsage: install.sh TASK [Options]\n"
USG_MSG+="\nTASK\n all => All installation process\n init => Setting default tables to a database\n acc => Accession information\n ref => Reference sequence\n"
USG_MSG+=" ann => Annotation\n var => Variant\n tsv => Read depth or General purpose track (default: Read depth)\n"
USG_MSG+="\nOptions\n -r: Delete specified datasets from your database.\n -g: TSV files load to general purpose track.\n -c: Condition of general purpose track.\n"
USG_MSG+="#########################################################################\n"
ACT="Loading"

#Validataion
OPTIND=2
while getopts rghc: OPT
do
	case $OPT in
    "r" ) EXE_OPT="-r"
		  ACT="Deleting" ;;
	"g" ) TSV_OPT="-c" 
		  TSV_MODE=1;;
	"h" ) echo -e $USG_MSG 
		  exit 1 ;;
	"c" ) TSV_OPT+=" -i "
		  TSV_OPT+=$OPTARG
		  TSV_COND=$OPTARG;;
      * ) echo -e $USG_MSG
		  exit 1 ;;
	esac
done

if [ $# -lt 1 -o $# -gt 6 ]; then
	echo -e $USG_MSG
	exit 1
fi

if [ "$MODE" != "all" -a "$MODE" != "init" -a "$MODE" != "acc" -a "$MODE" != "ref" -a "$MODE" != "ann" -a "$MODE" != "var" -a "$MODE" != "tsv" ]; then
	echo -e $USG_MSG
	exit 1
fi

if ! [ -d $DATADIR ]; then
	echo "'$DATADIR' is not found."
	exit 1
fi

if ! [ -d $SCRIPTS ]; then
	echo "'$SCRIPTS' is not found."
	exit 1
fi

if [ "$BACKEND" = "oracle" ]; then
	DB_OPT="-b  oracle -s $TABLESP"
fi

#Initalization (reference.csv)
if ( [ "$MODE" = "all" -o "$MODE" = "init" ] ) && [ "$EXE_OPT" != "-r" ]; then
	DATE=`date +"%Y/%m/%d %I:%M:%S"`
	echo $DATE" [TASUKE] Setting default tables to the database..."
	perl ${SCRIPTS}/tasuke_init.pl $DB_OPT -db $DB -u $USER -p $PASS 1>/dev/null <<_EOT_ 
${DATADIR}/reference.csv
y
_EOT_
  if [ $? -ne 0 ]; then 
		DATE=`date +"%Y/%m/%d %I:%M:%S"`
		echo $DATE" [TASUKE] tasuke_init.pl was failed."
		echo $DATE" [TASUKE] Exit"
		exit 1
	fi
fi

#Accessions (accession.csv)
if [ "$MODE" = "all" -o "$MODE" = "acc" ]; then
	DATE=`date +"%Y/%m/%d %I:%M:%S"`
	echo $DATE" [TASUKE] Setting accessions to the database..."
	perl ${SCRIPTS}/tasuke_accession.pl $DB_OPT -db $DB -u $USER -p $PASS $EXE_OPT 1>/dev/null  <<_EOT_ 
${DATADIR}/accession.csv
y
_EOT_
  if [ $? -ne 0 ]; then 
		DATE=`date +"%Y/%m/%d %I:%M:%S"`
		echo $DATE" [TASUKE] tasuke_accession.pl was failed."
		echo $DATE" [TASUKE] Exit"
		exit 1
	fi
fi

#Reference sequence (.fa .fasta)
if [ "$MODE" = "all" -o "$MODE" = "ref" ]; then
	DATE=`date +"%Y/%m/%d %I:%M:%S"`
	echo $DATE" [TASUKE] $ACT reference genome..."
	perl ${SCRIPTS}/tasuke_ref.pl $DB_OPT -db $DB -u $USER -p $PASS -f $DATADIR/$DIR_FASTA $EXE_OPT
	if [ $? -ne 0 ]; then 
		DATE=`date +"%Y/%m/%d %I:%M:%S"`
		echo $DATE" [TASUKE] tasuke_ref.pl was failed."
		echo $DATE" [TASUKE] Exit"
		exit 1
	fi
fi

#Annotation (.gff)
if [ "$MODE" = "all" -o "$MODE" = "ann" ]; then
	DATE=`date +"%Y/%m/%d %I:%M:%S"`
	echo $DATE" [TASUKE] $ACT annotations..."
	for file in ${DATADIR}/${DIR_GFF}/*; do
		if [[ "${file}" =~ \.gff ]] || [[ "${file}" =~ \.gff.gz ]] || [[ "${file}" =~ \.gff.bz2 ]]; then
			GFF=`basename ${file}`
			if [[ "${file}" =~ \.gff.gz ]]; then
				NAME=`echo $GFF | sed -e 's/\.gff.gz$//g'`
			elif [[ "${file}" =~ \.gff.bz2 ]]; then
				NAME=`echo $GFF | sed -e 's/\.gff.bz2$//g'`
			elif [[ "${file}" =~ \.gff ]]; then
				NAME=`echo $GFF | sed -e 's/\.gff$//g'`
			fi
			perl ${SCRIPTS}/tasuke_track_gff.pl $DB_OPT -db $DB -u $USER -p $PASS -t $NAME -f $file $EXE_OPT
			if [ $? -ne 0 ]; then 
				DATE=`date +"%Y/%m/%d %I:%M:%S"`
				echo $DATE" [TASUKE] tasuke_track_gff.pl was failed."
				echo $DATE" [TASUKE] Exit"
				exit 1
			fi
		fi
	done
fi

#Variant (.vcf)
if [ "$MODE" = "all" -o "$MODE" = "var" ]; then
	DATE=`date +"%Y/%m/%d %I:%M:%S"`
	echo $DATE" [TASUKE] $ACT variants..."
	for file in ${DATADIR}/${DIR_VCF}/*; do
		if [[ "${file}" =~ \.vcf ]] || [[ "${file}" =~ \.vcf.gz ]] || [[ "${file}" =~ \.vcf.bz2 ]]; then
			VCF=`basename ${file}`
			if [[ "${file}" =~ \.vcf.gz ]]; then
				NAME=`echo $VCF | sed -e 's/\(.*\)\.vcf.gz$/\1/'`
			elif [[ "${file}" =~ \.vcf.bz2 ]]; then
				NAME=`echo $VCF | sed -e 's/\(.*\)\.vcf.bz2$/\1/'`
			elif [[ "${file}" =~ \.vcf ]]; then
				NAME=`echo $VCF | sed -e 's/\(.*\)\.vcf$/\1/'`
			fi
			perl ${SCRIPTS}/tasuke_variant_vcf.pl $DB_OPT -db $DB -u $USER -p $PASS -n $NAME -f $file -t $VCF_TYPE $EXE_OPT
			if [ $? -ne 0 ]; then 
				DATE=`date +"%Y/%m/%d %I:%M:%S"`
				echo $DATE" [TASUKE] tasuke_variant_vcf.pl was failed."
				echo $DATE" [TASUKE] Exit"
				exit 1
			fi
		fi
	done
fi

#TSV
if [ "$MODE" = "all" -o "$MODE" = "tsv" ]; then
	DATE=`date +"%Y/%m/%d %I:%M:%S"`
	echo $DATE" [TASUKE] $ACT TSV file..."
	if [ "$TSV_COND" != "" ] && [ $TSV_MODE -eq 1 ]; then 
		perl ${SCRIPTS}/tasuke_add_condition.pl $DB_OPT -db $DB -u $USER -p $PASS -c $TSV_COND
	fi
	for file in ${DATADIR}/${DIR_TSV}/*; do
		if [[ "${file}" =~ \.tsv ]] || [[ "${file}" =~ \.tsv.gz ]] || [[ "${file}" =~ \.tsv.bz2 ]]; then
			TSV=`basename ${file}`
			if [[ "${file}" =~ \.tsv.gz ]]; then
				NAME=`echo $TSV | sed -e 's/\(.*\)\.tsv.gz$/\1/'`
			elif [[ "${file}" =~ \.tsv.bz2 ]]; then
				NAME=`echo $TSV | sed -e 's/\(.*\)\.tsv.bz2$/\1/'`
			elif [[ "${file}" =~ \.tsv ]]; then
				NAME=`echo $TSV | sed -e 's/\(.*\)\.tsv$/\1/'`
			fi
			perl ${SCRIPTS}/tasuke_tsv_db.pl $DB_OPT -db $DB -u $USER -p $PASS -n $NAME -f $file $EXE_OPT $TSV_OPT
			if [ $? -ne 0 ]; then 
				DATE=`date +"%Y/%m/%d %I:%M:%S"`
				echo $DATE" [TASUKE] tasuke_tsv_db.pl was failed."
				echo $DATE" [TASUKE] Exit"
				exit 1
			fi
		fi
	done
fi
DATE=`date +"%Y/%m/%d %I:%M:%S"`
echo $DATE" [TASUKE] Finished!"
