#! /bin/sh
er=0;
myself=`dirname $0`/`basename $0`; export myself
version="v6.240 (2007/04/04)"; export version
LANG=C; export LANG
os=`uname`
progname=`basename $0`
if [ `echo $os | grep -i cygwin` ]; then
	os="cygwin"
elif [ `echo $os | grep -i darwin` ]; then
	os="darwin"
elif [ `echo $os | grep -i sunos` ]; then
	os="sunos"
else
	os="unix"
fi
export os

if [ "$MAFFT_BINARIES" ]; then
	prefix="$MAFFT_BINARIES"
else
	prefix=/usr/lib/mafft
fi
export prefix
if [ ! -x "$prefix/tbfast" ]; then
	echo "" 1>&2
	echo "correctly installed?" 1>&2
	echo "mafft binaries have to be installed in \$MAFFT_BINARIES" 1>&2
	echo "or the $prefix directory". 1>&2
	echo "" 1>&2
	exit 1
	er=1
fi

defaultiterate=0
defaultcycle=2
defaultmodel=" -b 62 "
defaultgop="1.53"
defaultaof="0.123"
defaultfft=1
defaultrough=0
defaultdistance="sixtuples"
if [ $progname = "linsi" -o $progname = "mafft-linsi" ]; then
	defaultfft=0
	defaultcycle=1
	defaultiterate=1000
	defaultdistance="local"
elif [ $progname = "ginsi" -o $progname = "mafft-ginsi" ]; then
	defaultfft=1
	defaultcycle=1
	defaultiterate=1000
	defaultdistance="global"
elif [ $progname = "einsi" -o $progname = "mafft-einsi" ]; then
	defaultfft=0
	defaultcycle=1
	defaultiterate=1000
	defaultdistance="genaf"
elif [ $progname = "fftns" -o $progname = "mafft-fftns" ]; then
	defaultfft=1
	defaultcycle=2
elif [ $progname = "fftnsi" -o $progname = "mafft-fftnsi" ]; then
	defaultfft=1
	defaultcycle=2
	defaultiterate=2
elif [ $progname = "nwns" -o $progname = "mafft-nwns" ]; then
	defaultfft=0
	defaultcycle=2
elif [ $progname = "nwnsi" -o $progname = "mafft-nwnsi" ]; then
	defaultfft=0
	defaultcycle=2
	defaultiterate=2
fi
model=$defaultmodel
gop=$defaultgop
aof=$defaultaof
cycle=$defaultcycle
iterate=$defaultiterate
fft=$defaultfft
rough=$defaultrough
distance=$defaultdistance
forcefft=0
memopt="     "
LGOP="-6.00"
LEXP="-0.000"
lgop="-2.00"
lexp="-0.100"
laof="0.100"
seqtype="  "
param_it=" -x "
weighti="2.7"
quiet=0
sw=0
algopt=" -A "
algoptit=" -A "
scorecalcopt=" "
coreout=0
corethr="0.5"
corewin="100"
coreext=" "
outputformat="pir"
outorder="input"
seed="x"
auto=0
groupsize=-1
partsize=50
partdist="sixtuples"
partorderopt=" -x "
treeout=0
seedfiles="/dev/null"
aamatrix="/dev/null"
if [ $# -gt 0 ]; then
	while [ $# -gt 1 ];
	do
		if [ $1 = "--auto" ]; then 
			auto=1
		elif [ $1 = "--clustalout" ]; then 
			outputformat="clustal"
		elif [ $1 = "--reorder" ]; then 
			outorder="aligned"
			partorderopt=" "
		elif [ $1 = "--inputorder" ]; then 
			outorder="input"
			partorderopt=" -x "
		elif [ $1 = "--algq" ]; then 
			algopt=" -Q "
			algoptit=" -QB "
		elif [ $1 = "--groupsize" ]; then 
			shift   
			groupsize=`expr $1 - 0`
		elif [ $1 = "--partsize" ]; then 
			shift   
			partsize=`expr $1 - 0`
		elif [ $1 = "--parttree" ]; then 
			distance="parttree"
			partdist="sixtuples"
		elif [ $1 = "--dpparttree" ]; then 
			distance="parttree"
			partdist="localalign"
		elif [ $1 = "--fastaparttree" ]; then 
			distance="parttree"
			partdist="fasta"
		elif [ $1 = "--treeout" ]; then 
			treeout=1
		elif [ $1 = "--fastswpair" ]; then
			distance="fasta"
			sw=1
		elif [ $1 = "--fastapair" ]; then
			distance="fasta"
			sw=0
		elif [ $1 = "--noscore" ]; then
			scorecalcopt=" -Z "
		elif [ $1 = "--6merpair" ]; then
			distance="sixtuples"
		elif [ $1 = "--blastpair" ]; then
			distance="blast"
		elif [ $1 = "--globalpair" ]; then
			distance="global"
		elif [ $1 = "--localpair" ]; then
			distance="local"
		elif [ $1 = "--genafpair" ]; then
			distance="genaf"
		elif [ $1 = "--memsave" ]; then
			memopt=" -M -B "         # -B (bunkatsunashi no riyu ga omoidasenai)
		elif [ $1 = "--nuc" ]; then 
			seqtype=" -D "
		elif [ $1 = "--amino" ]; then 
			seqtype=" -P "
		elif [ $1 = "--fft" ]; then 
			fft=1
			forcefft=1
		elif [ $1 = "--nofft" ]; then 
			fft=0
		elif [ $1 = "--quiet" ]; then 
			quiet=1
		elif [ $1 = "--coreext" ]; then 
			coreext=" -c "
		elif [ $1 = "--core" ]; then 
			coreout=1
		elif [ $1 = "--maxiterate" ]; then 
			shift   
			iterate=`expr $1 - 0` 
		elif [ $1 = "--retree" ]; then 
			shift   
			cycle=`expr $1 - 0` 
		elif [ $1 = "--aamatrix" ]; then 
			shift   
			model=" -b -1 "
			aamatrix=$1
		elif [ $1 = "--fmodel" ]; then 
			model="-a "$model
		elif [ $1 = "--jtt" ]; then 
			shift   
			model=" -j "$1
		elif [ $1 = "--tm" ]; then 
			shift   
			model=" -m "$1
		elif [ $1 = "--bl" ]; then 
			shift   
			model=" -b "$1
		elif [ $1 = "--weighti" ]; then
			shift   
			weighti=$1
		elif [ $1 = "--op" ]; then 
			shift   
			gop=$1
		elif [ $1 = "--ep" ]; then 
			shift   
			aof=$1
		elif [ $1 = "--lop" ]; then 
			shift   
			lgop=$1
		elif [ $1 = "--LOP" ]; then 
			shift   
			LGOP=$1
		elif [ $1 = "--lep" ]; then 
			shift   
			laof=$1
		elif [ $1 = "--lexp" ]; then 
			shift   
			lexp=$1
		elif [ $1 = "--LEXP" ]; then 
			shift   
			LEXP=$1
		elif [ $1 = "--corethr" ]; then 
			shift   
			corethr=$1
		elif [ $1 = "--corewin" ]; then 
			shift   
			corewin=$1
		elif [ $1 = "--seed" ]; then
			shift
			seed="m"
			seedfiles=$seedfiles" "$1
		elif [ $1 -gt 0 -a $progname = "fftns" -o $1 -gt 0 -a $progname = "nwns" ]; then
			cycle=`expr $1 - 0`
		else
			echo "Unknown option: " $1 1>&2
			er=1;
		fi
		shift   
	done;

#	TMPFILE=/tmp/$progname.$$
	TMPFILE=`mktemp -dt $progname.XXXXXXXXXX`
	umask 077
#	mkdir  $TMPFILE  || er=1
	trap "rm -rf $TMPFILE " 0
	if [ $# -eq 1 ]; then
		if [ -r "$1" -o "$1" = - ]; then
			cat "$1" | tr "\r" "\n" > $TMPFILE/infile 
			cat "$aamatrix" | tr "\r" "\n" > $TMPFILE/_aamtx
#			echo $seedfiles
			infilename="$1"
			seedfilesintmp="/dev/null"
			seednseq="0"
			set $seedfiles > /dev/null
			while [ $# -gt 1 ];
			do
				shift
				cat "$1" | tr "\r" "\n" >  $TMPFILE/seed$#
				seednseq=$seednseq" "`grep -c '^[>|=]' $TMPFILE/seed$#`
				seedfilesintmp=$seedfilesintmp" "seed$#
			done
#			ls $TMPFILE
#			echo $seedfilesintmp
#			echo $seednseq
		else
#			echo "Cannot open $1." 1>&2
			er=1
		fi
	else
		echo '$#'"=$#" 1>&2
		er=1
	fi

	if [ $auto -eq 1 ]; then
		"$prefix/countlen" < $TMPFILE/infile > $TMPFILE/size
		nseq=`awk '{print $1}' $TMPFILE/size`
		nlen=`awk '{print $3}' $TMPFILE/size`
		if [ $nlen -lt 1000 -a $nseq -lt 100 ]; then
			distance="local"
			iterate=1000
		elif [ $nseq -lt 500 ]; then
			distance="sixtuples"
			iterate=2
		else
			distance="sixtuples"
			iterate=0
		fi
		if [ $quiet -eq 0 ]; then
			echo "nseq = " $nseq              1>&2
			echo "nlen = " $nlen              1>&2
			echo "distance = " $distance      1>&2
			echo "iterate = " $iterate        1>&2
		fi
	fi

	if [ $iterate -lt 1 -a $seed != "x" ]; then
		iterate=1
	fi

	if [ $iterate -gt 16 ]; then    #??
		iterate=16
	fi

	if [ $er -eq 1 ]; then
		echo "------------------------------------------------------------------------------" 1>&2
		echo "  MAFFT" $version " Copyright (c) 2006 Kazutaka Katoh" 1>&2
#		echo "" 1>&2
#		echo "  Input format: fasta" 1>&2
#		echo ""  1>&2
#		echo "  Usage: `basename $0` [options] inputfile > outputfile" 1>&2
	    echo "  http://align.bmr.kyushu-u.ac.jp/mafft/software/" 1>&2
		echo "  NAR 30:3059-3066, NAR 33:511-518" 1>&2
#		echo "------------------------------------------------------------------------------" 1>&2
#		echo "  % mafft in > out" 1>&2
		echo "------------------------------------------------------------------------------" 1>&2
#		echo ""  1>&2
		echo "High speed:" 1>&2
		echo "  % mafft in > out" 1>&2
		echo "  % mafft --retree 1 in > out (fastest)" 1>&2
		echo "" 1>&2
		echo "High accuracy (for <~200 sequences x <~2,000 aa/nt):" 1>&2
		echo "  % mafft --maxiterate 1000 --localpair  in > out (% linsi in > out is also ok)" 1>&2
		echo "  % mafft --maxiterate 1000 --genafpair  in > out (% einsi in > out)" 1>&2
		echo "  % mafft --maxiterate 1000 --globalpair in > out (% ginsi in > out)" 1>&2
		echo "" 1>&2
		echo "Medium speed and accuracy:" 1>&2
		echo "  % mafft --maxiterate 2 in > out" 1>&2
		echo "" 1>&2
#		echo "Other options:" 1>&2
		echo "--op # :         Gap opening penalty, default: 1.53" 1>&2
		echo "--ep # :         Offset (works like gap extension penalty), default: 0.123" 1>&2
		echo "--maxiterate # : Maximum number of iterative refinement, default: 0" 1>&2
		echo "--clustalout :   Output: clustal format, default: fasta" 1>&2
		echo "--reorder :      Outorder: aligned, default: input order" 1>&2
		echo "--quiet :        Do not report progress" 1>&2
#		echo "" 1>&2
#		echo " % mafft --maxiterate 1000 --localpair in > out (L-INS-i)" 1>&2
#		echo " most accurate in many cases, assumes only one alignable domain" 1>&2 
#		echo "" 1>&2
#		echo " % mafft --maxiterate 1000 --genafpair in > out (E-INS-i)" 1>&2
#		echo " works well if many unalignable residues exist between alignable domains" 1>&2
#		echo "" 1>&2
#		echo " % mafft --maxiterate 1000 --globalpair in > out (G-INS-i)" 1>&2
#		echo " suitable for globally alignable sequences            " 1>&2
#		echo "" 1>&2
#		echo " % mafft --maxiterate 1000 in > out (FFT-NS-i)" 1>&2
#		echo " accurate and slow, iterative refinement method      " 1>&2
#		echo "" 1>&2
#		echo "If the input sequences are long (~1,000,000nt)," 1>&2
#		echo " % mafft --retree 1 --memsave --fft in > out (FFT-NS-1-memsave, new in v5.8)" 1>&2
#		echo "" 1>&2
#		echo "If many (~5,000) sequences are to be aligned," 1>&2
#		echo "" 1>&2
#		echo " % mafft --retree 1 [--memsave] --nofft in > out (NW-NS-1, new in v5.8)" 1>&2
#		echo "" 1>&2
#		echo " --localpair :      All pairwise local alignment information is included"  1>&2
#		echo "                    to the objective function, default: off"  1>&2
#		echo " --globalpair :     All pairwise global alignment information is included"  1>&2
#		echo "                    to the objective function, default: off"  1>&2
#		echo " --op # :           Gap opening penalty, default: $defaultgop " 1>&2
#		echo " --ep # :           Offset (works like gap extension penalty), default: $defaultaof " 1>&2
#		echo " --bl #, --jtt # :  Scoring matrix, default: BLOSUM62" 1>&2
#		echo "                    Alternatives are BLOSUM (--bl) 30, 45, 62, 80, " 1>&2
#		echo "                    or JTT (--jtt) # PAM. " 1>&2
#		echo " --nuc or --amino : Sequence type, default: auto" 1>&2
#		echo " --retree # :       The number of tree building in progressive method " 1>&2
#		echo "                    (see the paper for detail), default: $defaultcycle " 1>&2
#		echo " --maxiterate # :   Maximum number of iterative refinement, default: $defaultiterate " 1>&2
#		if [ $defaultfft -eq 1 ]; then
#			echo " --fft or --nofft:  FFT is enabled or disabled, default: enabled" 1>&2
#		else
#			echo " --fft or --nofft:  FFT is enabled or disabled, default: disabled" 1>&2
#		fi
#		echo " --memsave:         Memory saving mode" 1>&2
#		echo "                    (for long genomic sequences), default: off" 1>&2
#		echo " --clustalout :     Output: clustal format, default: fasta" 1>&2
#		echo " --reorder :        Outorder: aligned, default: input order" 1>&2
#		echo " --quiet :          Do not report progress" 1>&2
#		echo "-----------------------------------------------------------------------------" 1>&2
		exit 1; 
	fi
	if [ $sw -eq 1 ]; then
		swopt=" -A "
	else
		swopt=" "
	fi

	if [ $distance = "fasta" -o $partdist = "fasta" ]; then
		if [ ! $FASTA_4_MAFFT ]; then
			FASTA_4_MAFFT=`which fasta34`
		fi

		if [ ! -x $FASTA_4_MAFFT ]; then
			echo ""       1>&2
			echo "== Install FASTA ========================================================" 1>&2
			echo "This option requires the fasta34 program (FASTA version x.xx or higher)"   1>&2
			echo "installed in your PATH.  If you have the fasta34 program but have renamed" 1>&2
			echo "(like /usr/local/bin/myfasta), set the FASTA_4_MAFFT environment variable" 1>&2
			echo "to point your fasta34 (like setenv FASTA_4_MAFFT /usr/local/bin/myfasta)." 1>&2
			echo "=========================================================================" 1>&2
			echo "" 1>&2
			exit 1
		fi
	fi

	if [ $treeout -eq 1 ]; then
		parttreeoutopt="-t"
		if [ $cycle -eq 0 ]; then
			treeoutopt="-t -T"
			groupsize=1
		else
			treeoutopt="-t"
		fi
	else
		parttreeoutopt=" "
		treeoutopt=" "
	fi

	nseq=`grep -c '^[>|=]' $TMPFILE/infile | head -1 ` 
	if [ $nseq -eq 2 ]; then
		cycle=1
	fi
	if [ $cycle -gt 3 ]; then
		cycle=3
	fi
	if [ $distance = "sixtuples" -a $seed = "x" ]; then
		localparam=" "
	elif [ $distance = "sixtuples" -a $seed != "x" ]; then
		if [ $cycle -eq 1 ]; then
			cycle=2                # nazeda
		fi
		localparam="-l "$weighti
	elif [ $distance = "parttree" ]; then
		localparam=" "
		if [ $groupsize -gt -1 ]; then
			cycle=1
		fi
	else
		cycle=1
		localparam=" -l "$weighti
	fi
	if [ $distance = "genaf" ]; then
		aof="0.000"
	fi

	if [ $distance = "parttree" ]; then
		if [ $seed != "x" ]; then
			echo "Impossible" 1>&2
			exit 1
		fi
		if [ $iterate -gt 1 ]; then
			echo "Impossible" 1>&2
			exit 1
		fi
		if [ $outorder = "aligned" ]; then
			outorder="input"
		fi
		outorder="input"   # partorder ga kiku
		if [ $partdist = "localalign" ]; then
			splitopt=" -L "    # -L -l -> fast 
		elif [ $partdist = "fasta" ]; then
			splitopt=" -S "
		else
			splitopt="  "
		fi
	fi


	if [ $nseq -gt 1000 ]; then
		fft=0
	fi
	if [ $forcefft -eq 1 ]; then
		param_fft=" -G "
		fft=1
	elif [ $fft -eq 1 ]; then
		param_fft=" -F "
	else
		param_fft=" "
	fi

	if [ $distance = "fasta" -a $sw -eq 0 ]; then
		strategy="F-I"
	elif [ $distance = "fasta" -a $sw -eq 1 ]; then
		strategy="H-I"
	elif [ $distance = "blast" ]; then
		strategy="B-I"
	elif [ $distance = "global" ]; then
		strategy="G-I"
	elif [ $distance = "local" ]; then
		strategy="L-I"
	elif [ $distance = "genaf" ]; then
		strategy="E-I"
	elif [ $fft -eq 1 ]; then
		strategy="FFT-"
	else
		strategy="NW-"
	fi
	strategy=$strategy"NS-"
	if [ $iterate -gt 0 ]; then
		strategy=$strategy"i"
	elif [ $distance = "parttree" ]; then
		if [ $partdist = "fasta" ]; then
			strategy=$strategy"FastaPartTree-"$cycle
		elif [ $partdist = "localalign" ]; then
			strategy=$strategy"DPPartTree-"$cycle
		else
			strategy=$strategy"PartTree-"$cycle
		fi
	else
		strategy=$strategy$cycle
	fi

	explanation='?'
	performance='Not tested.'
	if [ $strategy = "F-INS-i" ]; then
		explanation='Iterative refinement method (<'$iterate') with LOCAL pairwise alignment information'
		performance='Most accurate, but very slow'
	elif [ $strategy = "L-INS-i" ]; then
		explanation='Iterative refinement method (<'$iterate') with LOCAL pairwise alignment information'
		performance='Probably most accurate, very slow'
	elif [ $strategy = "E-INS-i" ]; then
		explanation='Iterative refinement method (<'$iterate') with LOCAL pairwise alignment with generalized affine gap costs (Altschul 1998)'
		performance='Suitable for sequences with long unalignable regions, very slow'
	elif [ $strategy = "G-INS-i" ]; then
		explanation='Iterative refinement method (<'$iterate') with GLOBAL pairwise alignment information'
		performance='Suitable for sequences of similar lengths, very slow'
	elif [ $strategy = "F-INS-1" ]; then
		explanation='Progressive method incorporating LOCAL pairwise alignment information'
	elif [ $strategy = "L-INS-1" ]; then
		explanation='Progressive method incorporating LOCAL pairwise alignment information'
	elif [ $strategy = "G-INS-1" ]; then
		explanation='Progressive method incorporating GLOBAL pairwise alignment information'
	elif [ $strategy = "FFT-NS-i" -o $strategy = "NW-NS-i" ]; then
		explanation='Iterative refinement method (max. '$iterate' iterations)'
		if [ $iterate -gt 2 ]; then
			performance='Accurate but slow'
		else
			performance='Standard'
		fi
	elif [ $strategy = "FFT-NS-2" -o $strategy = "NW-NS-2" ]; then
		explanation='Progressive method (guide trees were built '$cycle' times.)'
		performance='Fast but rough'
	elif [ $strategy = "FFT-NS-1" -o $strategy = "NW-NS-1" ]; then
		explanation='Progressive method (rough guide tree was used.)'
		performance='Very fast but very rough'
	fi

	if [ $outputformat = "clustal" -a $outorder = "aligned" ]; then
		outputopt=" -c $strategy -r order  "
	elif [ $outputformat = "clustal" -a $outorder = "input" ]; then
		outputopt=" -c $strategy  "
	elif [ $outputformat = "pir" -a $outorder = "aligned" ]; then
		outputopt=" -f -r order "
	else
		outputopt="null"
	fi
	
	(
		cd $TMPFILE;
		if [ $quiet -gt 0 ]; then
			if [ $seed != "x" ]; then
				mv infile infile2
				cat /dev/null > infile
				cat /dev/null > hat3.seed
				seedoffset=0
#				echo "seednseq="$seednseq
#				echo "seedoffset="$seedoffset
				set $seednseq > /dev/null
#				echo $#
				while [ $# -gt 1 ]
				do
					shift
#					echo "num="$#
					"$prefix/multi2hat3s" -t $nseq  -o $seedoffset -i seed$# >> infile 2>/dev/null || exit 1
					cat hat3 >> hat3.seed
#					echo $1
					seedoffset=`expr $seedoffset + $1`
#					echo $1
#					echo "seedoffset="$seedoffset
				done;
#				echo "seedoffset="$seedoffset
				cat infile2 >> infile
			else
				cat /dev/null > hat3.seed
			fi
#			cat hat3.seed
			if [ $distance = "fasta" ]; then
				"$prefix/dndfast7" $swopt < infile > /dev/null  2>&1      || exit 1
				cat hat3.seed hat3 > hatx
				mv hatx hat3
				"$prefix/tbfast" $treeoutopt $seqtype $model -f "-"$gop  -h "-"$aof  $param_fft $localparam   $algopt -x < infile   > /dev/null 2>&1 || exit 1
			elif [ $distance = "blast" ]; then
				"$prefix/dndblast" < infile > /dev/null  2>&1      || exit 1
				cat hat3.seed hat3 > hatx
				mv hatx hat3
				"$prefix/tbfast" $treeoutopt $seqtype $model -f "-"$gop  -h "-"$aof  $param_fft $localparam   $algopt -x < infile   > /dev/null 2>&1 || exit 1
			elif [ $distance = "global" ]; then
				"$prefix/pairlocalalign"   $seqtype $model -f "-"$gop  -h "-"$aof  -F < infile > /dev/null  2>&1      || exit 1
				cat hat3.seed hat3 > hatx
				mv hatx hat3
				"$prefix/tbfast" $treeoutopt $seqtype $model -f "-"$gop  -h "-"$aof  $param_fft $localparam   $algopt -x < infile   > /dev/null 2>&1 || exit 1
			elif [ $distance = "local" ]; then
				"$prefix/pairlocalalign"   $seqtype $model  -g $lexp -f $lgop  -h $laof -L < infile > /dev/null  2>&1      || exit 1
				cat hat3.seed hat3 > hatx
				mv hatx hat3
				"$prefix/tbfast" $treeoutopt $seqtype $model -f "-"$gop  -h "-"$aof  $param_fft $localparam   $algopt -x < infile   > /dev/null 2>&1 || exit 1
			elif [ $distance = "genaf" ]; then
				"$prefix/pairlocalalign"   $seqtype $model -g $lexp -f $lgop  -h $laof -O $LGOP -E $LEXP -N < infile > /dev/null  2>&1      || exit 1
				cat hat3.seed hat3 > hatx
				mv hatx hat3
				"$prefix/tbfast" $treeoutopt $seqtype $model -f "-"$gop  -h "-"$aof  $param_fft $localparam   $algopt -x < infile   > /dev/null 2>&1 || exit 1
			elif [ $distance = "parttree" ]; then
				"$prefix/splittbfast" -Q $splitopt $partorderopt $parttreeoutopt $seqtype $model -f "-"$gop  -h "-"$aof  $param_fft -p $partsize -s $groupsize -i infile   > pre 2>/dev/null || exit 1
				mv hat3.seed hat3
			else
				"$prefix/disttbfast" $treeoutopt $memopt $seqtype $model -f "-"$gop  -h "-"$aof  $param_fft $algopt -x < infile   > pre 2>/dev/null || exit 1
				mv hat3.seed hat3
			fi
			while [ $cycle -gt 1 ]
			do
				if [ $distance = "parttree" ]; then
					mv pre infile
					"$prefix/splittbfast" -Z -Q $splitopt $partorderopt $parttreeoutopt $memopt $seqtype $model -f "-"$gop  -h "-"$aof  $param_fft -p $partsize -s $groupsize -i infile   > pre 2>/dev/null || exit 1
				else
					"$prefix/tbfast" $treeoutopt $memopt $seqtype $model  -f "-"$gop  -h "-"$aof $param_fft  $localparam $algopt -Jx < pre > /dev/null 2>&1 || exit 1
				fi
				cycle=`expr $cycle - 1`
			done
			if [ $iterate -gt 0 ]; then
				if [ $distance = "sixtuples" ]; then
				    "$prefix/dndpre"       < pre     > /dev/null 2>&1 || exit 1
				fi
				"$prefix/dvtditr" $memopt $scorecalcopt $localparam -z 50 $seqtype $model -f "-"$gop -h "-"$aof  -c -I $iterate $param_it -F $algoptit  < pre     > /dev/null 2>&1 || exit 1
			fi
		else
			if [ $seed != "x" ]; then
				mv infile infile2
				cat /dev/null > infile
				cat /dev/null > hat3.seed
				seedoffset=0
#				echo "seednseq="$seednseq
#				echo "seedoffset="$seedoffset
				set $seednseq > /dev/null
#				echo $#
				while [ $# -gt 1 ]
				do
					shift
#					echo "num="$#
					"$prefix/multi2hat3s" -t $nseq  -o $seedoffset -i seed$# >> infile  || exit 1
					cat hat3 >> hat3.seed
#					echo $1
					seedoffset=`expr $seedoffset + $1`
#					echo $1
#					echo "seedoffset="$seedoffset
				done;
#				echo "seedoffset="$seedoffset
				cat infile2 >> infile
			else
				cat /dev/null > hat3.seed
			fi
#			cat hat3.seed
			if [ $distance = "fasta" ]; then
				"$prefix/dndfast7" $swopt < infile > /dev/null     || exit 1
				cat hat3.seed hat3 > hatx
				mv hatx hat3
				"$prefix/tbfast" $treeoutopt $seqtype $model -f "-"$gop  -h "-"$aof  $param_fft $localparam  $algopt -x < infile   > /dev/null || exit 1
			elif [ $distance = "blast" ]; then
				"$prefix/dndblast" < infile > /dev/null     || exit 1
				cat hat3.seed hat3 > hatx
				mv hatx hat3
				"$prefix/tbfast" $treeoutopt $seqtype $model -f "-"$gop  -h "-"$aof  $param_fft $localparam  $algopt -x < infile   > /dev/null || exit 1
			elif [ $distance = "global" ]; then
				"$prefix/pairlocalalign"   $seqtype $model -f "-"$gop  -h "-"$aof -F  < infile > /dev/null     || exit 1
				cat hat3.seed hat3 > hatx
				mv hatx hat3
				"$prefix/tbfast" $treeoutopt $seqtype $model -f "-"$gop  -h "-"$aof  $param_fft $localparam  $algopt -x < infile   > /dev/null || exit 1
			elif [ $distance = "local" ]; then
				"$prefix/pairlocalalign"   $seqtype $model  -g $lexp -f $lgop  -h $laof -L  < infile > /dev/null     || exit 1
				cat hat3.seed hat3 > hatx
				mv hatx hat3
				"$prefix/tbfast" $treeoutopt $seqtype $model -f "-"$gop  -h "-"$aof  $param_fft $localparam  $algopt -x < infile   > /dev/null || exit 1
			elif [ $distance = "genaf" ]; then
				"$prefix/pairlocalalign"   $seqtype $model  -g $lexp -f $lgop  -h $laof -O $LGOP -E $LEXP -N  < infile > /dev/null     || exit 1
				cat hat3.seed hat3 > hatx
				mv hatx hat3
				"$prefix/tbfast" $treeoutopt $seqtype $model -f "-"$gop  -h "-"$aof  $param_fft $localparam  $algopt -x < infile   > /dev/null || exit 1
			elif [ $distance = "parttree" ]; then
				"$prefix/splittbfast" -Q $splitopt $partorderopt $parttreeoutopt $memopt $seqtype $model -f "-"$gop  -h "-"$aof  $param_fft -p $partsize -s $groupsize -i infile   > pre || exit 1
				mv hat3.seed hat3
			else
				"$prefix/disttbfast" $treeoutopt $memopt $seqtype $model -f "-"$gop  -h "-"$aof  $param_fft  $algopt -x < infile   > pre || exit 1
				mv hat3.seed hat3
			fi

			while [ $cycle -gt 1 ]
			do
				if [ $distance = "parttree" ]; then
					mv pre infile
					"$prefix/splittbfast" -Z -Q $splitopt $partorderopt $parttreeoutopt $memopt $seqtype $model -f "-"$gop  -h "-"$aof  $param_fft -p $partsize -s $groupsize -i infile  > pre || exit 1
				else
					"$prefix/tbfast" $treeoutopt $memopt $seqtype $model  -f "-"$gop  -h "-"$aof $param_fft $localparam $algopt -Jx < pre > /dev/null || exit 1
				fi
				cycle=`expr $cycle - 1`
			done
			if [ $iterate -gt 0 ]; then
				if [ $distance = "sixtuples" ]; then
			    	"$prefix/dndpre"       < pre     > /dev/null 2>&1 || exit 1
				fi
				"$prefix/dvtditr" $memopt $scorecalcopt $localparam -z 50 $seqtype $model -f "-"$gop -h "-"$aof  -c -I $iterate $param_it -F $algoptit  < pre     > /dev/null || exit 1
			fi
		fi



		if [ $coreout -eq 1 ]; then
			"$prefix/setcore" -w $corewin -i $corethr $coreext < pre > pre2
			mv pre2 pre
		fi
		if [ "$outputopt" = "null" ]; then
			cat < pre || exit 1
		else
			"$prefix/f2cl" $outputopt < pre || exit 1
		fi
	)

	if [ $treeout -eq 1 ]; then
		cp $TMPFILE/infile.tree "$infilename.tree"
	fi

	if [ $quiet -eq 0 ]; then
		echo '' 1>&2
		if [ $distance = "fasta" -o $partdist = "fasta" ]; then
			echo "Pairwise alignments were computed by FASTA" 1>&2
			echo "(Pearson & Lipman, 1988, PNAS 85:2444-2448)" 1>&2
		fi
		if [ $distance = "blast" ]; then
			echo "Pairwise alignments were computed by BLAST" 1>&2
			echo "(Altschul et al., 1997, NAR 25:3389-3402)" 1>&2
		fi
		printf "\n" 1>&2
		echo 'Strategy:' 1>&2
		printf ' '$strategy 1>&2
		echo ' ('$performance')' 1>&2
		echo ' '$explanation 1>&2
		echo '' 1>&2
		echo "If unsure which option to use, try 'mafft --auto input > output'." 1>&2
		echo "If long gaps are expected, try 'mafft --ep 0.0 (other options) input > output'." 1>&2
		echo "For a list of options, type 'mafft --help' or visit the mafft page." 1>&2
		echo '' 1>&2
	fi
	exit 0;
fi

prog="awk"

if [ $os != "darwin" ]; then
	echo "checking nawk" 1>&2
	tmpawk=`which nawk 2>/dev/null | awk '{print $1}'`
	if [ -x $tmpawk ]; then
		prog=$tmpawk
	fi

	echo "checking gawk" 1>&2
	tmpawk=`which gawk 2>/dev/null | awk '{print $1}'`
	if [ -x $tmpawk ]; then
		prog=$tmpawk
	fi
fi

echo "prog="$prog 1>&2

umask 077
export defaultaof
export defaultgop
export defaultfft
export defaultcycle
export defaultiterate
(
$prog '
BEGIN {
	prefix = ENVIRON["prefix"];
	version = ENVIRON["version"];
	myself = ENVIRON["myself"];
	defaultgop = ENVIRON["defaultgop"]
	defaultaof = ENVIRON["defaultaof"]
	defaultfft = ENVIRON["defaultfft"]
	defaultcycle = ENVIRON["defaultcycle"]
	defaultiterate = ENVIRON["defaultiterate"]
	while( 1 )
	{
		options = ""
		printf( "\n" ) > "/dev/tty";
		printf( "---------------------------------------------------------------------\n" )      > "/dev/tty";
		printf( "\n" )                                                                           > "/dev/tty";
		printf( "   MAFFT %s\n", version )                                                       > "/dev/tty";
		printf( "\n" )                                                                           > "/dev/tty";
		printf( "        Copyright (c) 2006 Kazutaka Katoh\n" )                                  > "/dev/tty";
		printf( "        NAR 30:3059-3066, NAR 33:511-518\n" )                                   > "/dev/tty";
		printf( "        http://align.bmr.kyushu-u.ac.jp/mafft/software/\n" )       > "/dev/tty";
		printf( "---------------------------------------------------------------------\n" )      > "/dev/tty";
		printf( "\n" );
	
		while( 1 )
		{
			printf( "\n" ) > "/dev/tty";
			printf( "Input file? (fasta format)\n@ " ) > "/dev/tty";
			res = getline < "/dev/tty";
			close( "/dev/tty" )
			if( res == 0 || NF == 0 )
				continue;
			infile0 = sprintf( "%s", $1 );
			infile = sprintf( "%s", $1 );
	
			res = getline < infile;
			close( infile );
			if( res == -1 )
				printf( "%s: No such file.\n\n", infile );
			else if( res == 0 )
				printf( "%s: Empty.\n", infile );
			else
			{
				printf( "OK. infile = %s\n\n", infile );
				break;
			}
		}
		nseq = 0;
	
		while( 1 )
		{
			printf( "\n" ) > "/dev/tty";
			printf( "Output file?\n" ) > "/dev/tty";
			printf( "@ " ) > "/dev/tty";
			res = getline < "/dev/tty";
			close( "/dev/tty" );
			if( res == 0 || NF == 0 )
				continue;
			else
			{
				outfile = sprintf( "%s", $1 );
				printf( "OK. outfile = %s\n\n", outfile );
				break;
			}
		}
	
	
		while( 1 )
		{
			retree = defaultcycle
			printf( "\n" );
			printf( "Number of tree-rebuilding?\n" ) > "/dev/tty";
			printf( "@ [%d] ", retree ) > "/dev/tty";
			res = getline < "/dev/tty";
			close( "/dev/tty" );
			if( res == 0 )
				continue;
			else if( NF == 0 )
				;
			else
				retree = 0 + $1;
			if( retree < 1 || 10 < retree )
				;
			else
			{
				printf( "OK. %d\n\n", retree );
				break;
			}
		}
	
		while( 1 )
		{
			niterate = defaultiterate;
			printf( "\n" );
			printf( "Maximum number of iterations?\n" ) > "/dev/tty";
			printf( "@ [%d] ", niterate ) > "/dev/tty";
			res = getline < "/dev/tty";
			close( "/dev/tty" );
			if( res == 0 )
				continue;
			else if( NF == 0 )
				;
			else
				niterate = 0 + $1;
			if( niterate < 0 || 1000 < niterate )
				;
			else
			{
				printf( "OK. %d\n\n", niterate );
				break;
			}
		}

		while( 1 )
		{
			fft = defaultfft;
			printf( "\n" );
			printf( "Use fft?\n" ) > "/dev/tty";
			printf( "@ [%s] ", fft?"Yes":"No" ) > "/dev/tty";
			res = getline < "/dev/tty";
			close( "/dev/tty" );
			if( res == 0 )
				continue;
			else if( NF == 0 )
			{
				break;
			}
			else if( NF == 0 || $0 ~ /^[Yy]/ )
			{
				fft = 1;
				break;
			}
			else if( NF == 0 || $0 ~ /^[Nn]/ )
			{
				fft = 0;
				break;
			}
		}
		if( fft )
		{
				printf( "OK. FFT is enabled.\n\n" );
				fftparam = " ";
		}
		else
		{
				printf( "OK. FFT is disabled.\n\n" );
				fftparam = " --nofft ";
		}

		while( 1 )
		{
			scoringmatrix = 3;
			printf( "\n" );
			printf( "Scoring matrix? (ignored when DNA sequence is input.)\n" ) > "/dev/tty";
			printf( " 1. BLOSUM 30\n" ) > "/dev/tty";
			printf( " 2. BLOSUM 45\n" ) > "/dev/tty";
			printf( " 3. BLOSUM 62\n" ) > "/dev/tty";
			printf( " 4. BLOSUM 80\n" ) > "/dev/tty";
			printf( " 5. JTT 200\n" ) > "/dev/tty";
			printf( " 6. JTT 100\n" ) > "/dev/tty";
			printf( "@ [%d] ", scoringmatrix ) > "/dev/tty";
			res = getline < "/dev/tty";
			close( "/dev/tty" );
			if( res == 0 )
				continue;
			else if( NF == 0 )
				;
			else
				scoringmatrix = 0 + $1;
			if( scoringmatrix < 1 || 6 < scoringmatrix )
				;
			else
			{
				break;
			}
		}
		if( scoringmatrix == 1 )
			scoringparam = " --bl 30 ";
		else if( scoringmatrix == 2 )
			scoringparam = " --bl 45 ";
		else if( scoringmatrix == 3 )
			scoringparam = " --bl 62 ";
		else if( scoringmatrix == 4 )
			scoringparam = " --bl 80 ";
		else if( scoringmatrix == 5 )
			scoringparam = " --jtt 200 ";
		else if( scoringmatrix == 6 )
			scoringparam = " --jtt 100 ";
		printf( "OK. %s\n\n",scoringparam );
	
		while( 1 )
		{
			penalty = 0.0 + defaultgop;
			offset = 0.0 + defaultaof;
			printf( "\n" );
			printf( "Parameters (gap opening penalty, offset)?\n", penalty, offset ) > "/dev/tty";
			printf( "@ [%5.3f, %5.3f] ", penalty, offset ) > "/dev/tty";
			res = getline < "/dev/tty";
			close( "/dev/tty" );
			if( res == 0 )
				continue;
			else if( NF == 2 )
			{
				penalty = 0.0 + $1;
				offset = 0.0 + $2;
			}
			else if( NF == 0 )
				;
			else
				continue;
			if( penalty <= 0.0 || 10.0 < penalty )
				;
			else if( offset <= 0.0 || 10.0 < offset )
				;
			else
			{
				printf( "OK. %5.3f %5.3f\n\n", penalty, offset );
				break;
			}
		}
	
		command = sprintf( "%s %s --retree %d --maxiterate %d %s --op %f --ep %f %s > %s", myself, fftparam, retree, niterate, scoringparam, penalty, offset, infile, outfile );
		printf( "%s\n\n", command );
	
	
		while( 1 )
		{
			go = 0;
			printf( "\n" );
			printf( "OK?\n" );
			printf( "@ [Y] " ) > "/dev/tty";
			res = getline < "/dev/tty";
			close( "/dev/tty" );
			if( res == 0 )
				continue;
			else if( NF == 0 || $0 ~ /^[Yy]/ )
			{
				go=1;
				break;
			}
			else
				break;
		}
		if( go ) break;
		printf( "\n" );
		printf( "\n" );
	}
	system( command );
}
'
)
exit 0;
