#! /bin/sh


er=0;
myself=`dirname "$0"`/`basename "$0"`; export myself
version="v6.864b (2011/11/10)"; 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"
elif [ `echo $os | grep -i linux` ]; then
	os="linux"
else
	os="unix"
fi
export os

if [ "$MAFFT_BINARIES" ]; then
	prefix="$MAFFT_BINARIES"
else
	prefix=/usr/lib/mafft/lib/mafft
fi
export prefix

if [ $# -gt 0 ]; then
	if [ "$1" = "--man" ]; then 
		man "$prefix/mafft.1"
		exit 0;
	fi
fi

if [ -x "$prefix/version" ]; then
		versionbin=`"$prefix/version" | awk '{print $1}'` # for cygwin
	else
		versionbin="0.000"
fi

if ! expr "$version" : v"$versionbin" > /dev/null ; then
	echo "" 1>&2
	echo "v$versionbin != $version" 1>&2
	echo "" 1>&2
	echo "There may be a problem in the configuration." 1>&2
	echo "Check the MAFFT_BINARIES environmental variable." 1>&2
	echo "This variable must be unset in most cases." 1>&2
	echo "" 1>&2
	echo "If it points somewhere, unset it" 1>&2
	echo "$ export MAFFT_BINARIES=" 1>&2
	echo "Or" 1>&2
	echo "% unsetenv MAFFT_BINARIES" 1>&2
	echo "Then retry" 1>&2
	echo "$ mafft input > output" 1>&2
	echo "" 1>&2
	echo "Also delete the MAFFT_BINARIES line from your" 1>&2
	echo "setting file (.bashrc, .profile, .cshrc, etc)." 1>&2
	echo "" 1>&2
	echo "Please send a problem report to kazutaka.katoh@aist.go.jp," 1>&2
	echo "if this problem remains." 1>&2
	echo "" 1>&2
	exit 1
	er=1
fi

defaultiterate=0
defaultcycle=2
defaultgop="1.53"
#defaultaof="0.123"
defaultaof="0.000"
defaultlaof="0.100"
defaultlgop="-2.00"
defaultfft=1
defaultrough=0
defaultdistance="sixtuples"
#defaultdistance="local"
defaultweighti="2.7"
defaultweightr="0.0"
defaultweightm="1.0"
defaultmccaskill=0
defaultcontrafold=0
defaultalgopt="  "
defaultalgoptit="  "
defaultsbstmodel=" -b 62 "
defaultfmodel=" "
defaultkappa=" "
if [ $progname = "xinsi" -o $progname = "mafft-xinsi" ]; then
	defaultfft=1
	defaultcycle=1
	defaultiterate=1000
	defaultdistance="scarna"
	defaultweighti="3.2"
	defaultweightr="8.0"
	defaultweightm="2.0"
	defaultmccaskill=1
	defaultcontrafold=0
	defaultalgopt=" -A "
	defaultalgoptit=" -AB " ## chui
	defaultaof="0.0"
	defaultsbstmodel=" -b 62 "
	defaultkappa=" "
	defaultfmodel=" -a "
elif [ $progname = "qinsi" -o $progname = "mafft-qinsi" ]; then
	defaultfft=1
	defaultcycle=1
	defaultiterate=1000
	defaultdistance="global"
	defaultweighti="3.2"
	defaultweightr="8.0"
	defaultweightm="2.0"
	defaultmccaskill=1
	defaultcontrafold=0
	defaultalgopt=" -A "
	defaultalgoptit=" -AB " ## chui
	defaultaof="0.0"
	defaultsbstmodel=" -b 62 "
	defaultkappa=" "
	defaultfmodel=" -a "
elif [ $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="localgenaf"
elif [ $progname = "fftns" -o $progname = "mafft-fftns" ]; then
	defaultfft=1
	defaultcycle=2
	defaultdistance="sixtuples"
elif [ $progname = "fftnsi" -o $progname = "mafft-fftnsi" ]; then
	defaultfft=1
	defaultcycle=2
	defaultiterate=2
	defaultdistance="sixtuples"
elif [ $progname = "nwns" -o $progname = "mafft-nwns" ]; then
	defaultfft=0
	defaultcycle=2
	defaultdistance="sixtuples"
elif [ $progname = "nwnsi" -o $progname = "mafft-nwnsi" ]; then
	defaultfft=0
	defaultcycle=2
	defaultiterate=2
	defaultdistance="sixtuples"
fi
outputfile=""
namelength=15
anysymbol=0
parallelizationstrategy="BAATARI2"
kappa=$defaultkappa
sbstmodel=$defaultsbstmodel
fmodel=$defaultfmodel
gop=$defaultgop
aof=$defaultaof
cycle=$defaultcycle
iterate=$defaultiterate
fft=$defaultfft
rough=$defaultrough
distance=$defaultdistance
forcefft=0
memopt=" "
weightopt=" "
GGOP="-6.00"
LGOP="-6.00"
LEXP="-0.000"
GEXP="-0.000"
lgop=$defaultlgop
lexp="-0.100"
laof=$defaultlaof
pggop="-2.00"
pgexp="-0.10"
pgaof="0.10"
rgop="-1.530"
rgep="-0.000"
seqtype="  "
weighti=$defaultweighti
weightr=$defaultweightr
weightm=$defaultweightm
rnaalifold=0
mccaskill=$defaultmccaskill
contrafold=$defaultcontrafold
progressfile="/dev/stderr"
debug=0
sw=0
algopt=$defaultalgopt
algoptit=$defaultalgoptit
algspecified=0
scorecalcopt=" "
coreout=0
corethr="0.5"
corewin="100"
coreext=" "
outputformat="pir"
outorder="input"
seed="x"
seedtable="x"
auto=0
groupsize=-1
partsize=50
partdist="sixtuples"
partorderopt=" -x "
treeout=0
distout=0
treein=0
topin=0
treeinopt="  "
seedfiles="/dev/null"
seedtablefile="/dev/null"
aamatrix="/dev/null"
treeinfile="/dev/null"
rnascoremtx=" "
laraparams="/dev/null"
foldalignopt=" "
treealg=" -X "
scoreoutarg=" "
numthreads=0
randomseed=0
addfile="/dev/null"
addarg0=" "
outnum=" "
if [ $# -gt 0 ]; then
	while [ $# -gt 1 ];
	do
		if [ "$1" = "--auto" ]; then 
			auto=1
		elif [ "$1" = "--anysymbol" ]; then 
			anysymbol=1
		elif [ "$1" = "--preservecase" ]; then 
			anysymbol=1
		elif [ "$1" = "--clustalout" ]; then 
			outputformat="clustal"
		elif [ "$1" = "--phylipout" ]; then 
			outputformat="phylip"
		elif [ "$1" = "--reorder" ]; then 
			outorder="aligned"
			partorderopt=" "
		elif [ "$1" = "--inputorder" ]; then 
			outorder="input"
			partorderopt=" -x "
		elif [ "$1" = "--unweight" ]; then 
			weightopt=" -u "
		elif [ "$1" = "--alga" ]; then 
			algopt=" "
			algoptit=" "
			algspecified=1
		elif [ "$1" = "--algq" ]; then 
			algopt=" -Q "
			algoptit=" "
			algspecified=1
		elif [ "$1" = "--namelength" ]; then 
			shift   
			namelength=`expr "$1" - 0`
			if ! expr "$1" : "[0-9]" > /dev/null ; then
				echo "Specify the length of name in clustal format output!" 1>&2
				exit
			fi
		elif [ "$1" = "--groupsize" ]; then 
			shift   
			groupsize=`expr "$1" - 0`
			if ! expr "$1" : "[0-9]" > /dev/null ; then
				echo "Specify groupsize!" 1>&2
				exit
			fi
		elif [ "$1" = "--partsize" ]; then 
			shift   
			partsize=`expr "$1" - 0`
			if ! expr "$1" : "[0-9]" > /dev/null ; then
				echo "Specify partsize!" 1>&2
				exit
			fi
		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" = "--distout" ]; then 
			distout=1
		elif [ "$1" = "--fastswpair" ]; then
			distance="fasta"
			sw=1
		elif [ "$1" = "--fastapair" ]; then
			distance="fasta"
			sw=0
		elif [ "$1" = "--averagelinkage" ]; then
			treealg=" -E "
		elif [ "$1" = "--minimumlinkage" ]; then
			treealg=" -q "
		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" = "--scarnapair" ]; then
			distance="scarna"
		elif [ "$1" = "--larapair" ]; then
			distance="lara"
		elif [ "$1" = "--slarapair" ]; then
			distance="slara"
		elif [ "$1" = "--foldalignpair" ]; then
			distance="foldalignlocal"
		elif [ "$1" = "--foldalignlocalpair" ]; then
			distance="foldalignlocal"
		elif [ "$1" = "--foldalignglobalpair" ]; then
			distance="foldalignglobal"
		elif [ "$1" = "--globalgenafpair" ]; then
			distance="globalgenaf"
		elif [ "$1" = "--localgenafpair" ]; then
			distance="localgenaf"
		elif [ "$1" = "--genafpair" ]; then
			distance="localgenaf"
		elif [ "$1" = "--memsave" ]; then
			memopt=" -M -B "         # -B (bunkatsunashi no riyu ga omoidasenai)
		elif [ "$1" = "--nomemsave" ]; then
			memopt=" -N "
		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 
			progressfile="/dev/null"
		elif [ "$1" = "--debug" ]; then 
			debug=1
		elif [ "$1" = "--coreext" ]; then 
			coreext=" -c "
		elif [ "$1" = "--core" ]; then 
			coreout=1
		elif [ "$1" = "--progress" ]; then 
			shift   
			progressfile="$1"
			if ! ( expr "$progressfile" : "/" > /dev/null || expr "$progressfile" : "[A-Za-z]\:" > /dev/null ) ; then
				echo "Specify a progress file name with the absolute path!" 1>&2
				exit
			fi
		elif [ "$1" = "--out" ]; then 
			shift   
			outputfile="$1"
		elif [ "$1" = "--thread" ]; then 
			shift
			numthreads=`expr "$1" - 0` 
			if ! expr "$1" : "[0-9]" > /dev/null ; then
				echo "Specify the number of threads!" 1>&2
				exit
			fi
		elif [ "$1" = "--randomseed" ]; then 
			shift
			randomseed=`expr "$1" - 0` 
		elif [ "$1" = "--bestfirst" ]; then 
			parallelizationstrategy="BESTFIRST"
		elif [ "$1" = "--adhoc0" ]; then 
			parallelizationstrategy="BAATARI0"
		elif [ "$1" = "--adhoc1" ]; then 
			parallelizationstrategy="BAATARI1"
		elif [ "$1" = "--adhoc2" ]; then 
			parallelizationstrategy="BAATARI2"
		elif [ "$1" = "--simplehillclimbing" ]; then 
			parallelizationstrategy="BAATARI2"
		elif [ "$1" = "--scoreout" ]; then 
			scoreoutarg="-S -B"
		elif [ "$1" = "--outnum" ]; then 
			scoreoutarg="-n"
		elif [ "$1" = "--addprofile" ]; then 
			shift   
			addarg0="-I"
			addfile="$1"
		elif [ "$1" = "--add" ]; then 
			shift   
			addarg0="-K -I"
			addfile="$1"
		elif [ "$1" = "--maxiterate" ]; then 
			shift   
			iterate=`expr "$1" - 0` 
			if ! expr "$1" : "[0-9]" > /dev/null ; then
				echo "Specify the number of iterations!" 1>&2
				exit
			fi
		elif [ "$1" = "--retree" ]; then 
			shift   
			cycle=`expr "$1" - 0`
			if ! expr "$1" : "[0-9]" > /dev/null ; then
				echo "Specify the number of tree rebuilding!" 1>&2
				exit
			fi
		elif [ "$1" = "--aamatrix" ]; then 
			shift   
			sbstmodel=" -b -1 "
			aamatrix="$1"
			if [ ! -e "$aamatrix" ]; then
				echo "Cannot open $aamatrix" 1>&2
				exit
			fi
		elif [ "$1" = "--treein" ]; then 
			shift   
			treeinopt=" -U "
			treein=1
			treeinfile="$1"
			if [ ! -e "$treeinfile" ]; then
				echo "Cannot open $treeinfile" 1>&2
				exit
			fi
		elif [ "$1" = "--topin" ]; then 
			shift   
			treeinopt=" -V "
			treein=1
			treeinfile="$1"
			echo "The --topin option has been disabled." 1>&2
			echo "There was a bug in version < 6.530."   1>&2
			echo "This bug has not yet been fixed."      1>&2
			exit 1
		elif [ "$1" = "--kappa" ]; then 
			shift   
			kappa=" -k $1 "
			if ! expr "$1" : "[0-9]" > /dev/null ; then
				echo "Specify kappa value!" 1>&2
				exit
			fi
		elif [ "$1" = "--fmodel" ]; then 
			fmodel=" -a "
		elif [ "$1" = "--jtt" ]; then 
			shift   
			sbstmodel=" -j $1"
			if ! expr "$1" : "[0-9]" > /dev/null ; then
				echo "Specify pam value!" 1>&2
				exit
			fi
		elif [ "$1" = "--kimura" ]; then 
			shift   
			sbstmodel=" -j $1"
			if ! expr "$1" : "[0-9]" > /dev/null ; then
				echo "Specify pam value!" 1>&2
				exit
			fi
		elif [ "$1" = "--tm" ]; then 
			shift   
			sbstmodel=" -m $1"
			if ! expr "$1" : "[0-9]" > /dev/null ; then
				echo "Specify pam value!" 1>&2
				exit
			fi
		elif [ "$1" = "--bl" ]; then 
			shift   
			sbstmodel=" -b $1"
			if ! expr "$1" : "[0-9]" > /dev/null ; then
				echo "blosum $1?" 1>&2
				exit
			fi
		elif [ "$1" = "--weighti" ]; then
			shift   
			weighti="$1"
			if ! expr "$1" : "[0-9]" > /dev/null ; then
				echo "Specify weighti value!" 1>&2
				exit
			fi
		elif [ "$1" = "--weightr" ]; then
			shift   
			weightr="$1"
			if ! expr "$1" : "[0-9]" > /dev/null ; then
				echo "Specify weightr value!" 1>&2
				exit
			fi
		elif [ "$1" = "--weightm" ]; then
			shift   
			weightm="$1"
			if ! expr "$1" : "[0-9]" > /dev/null ; then
				echo "Specify weightm value!" 1>&2
				exit
			fi
		elif [ "$1" = "--rnaalifold" ]; then
			rnaalifold=1
		elif [ "$1" = "--mccaskill" ]; then
			mccaskill=1
			contrafold=0
		elif [ "$1" = "--contrafold" ]; then
			mccaskill=0
			contrafold=1
		elif [ "$1" = "--ribosum" ]; then
			rnascoremtx=" -s "
		elif [ "$1" = "--op" ]; then 
			shift   
			gop="$1"
			if ! expr "$1" : "[0-9]" > /dev/null ; then
				echo "Specify op!" 1>&2
				exit
			fi
		elif [ "$1" = "--ep" ]; then 
			shift   
			aof="$1"
			if ! expr "$1" : "[0-9]" > /dev/null ; then
				echo "Specify ep!" 1>&2
				exit
			fi
		elif [ "$1" = "--rop" ]; then 
			shift   
			rgop="$1"
# Atode check
		elif [ "$1" = "--rep" ]; then 
			shift   
			rgep="$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" = "--GEXP" ]; then 
			shift   
			GEXP="$1"
		elif [ "$1" = "--GOP" ]; then 
			shift   
			GGOP="$1"
		elif [ "$1" = "--gop" ]; then 
			shift   
			pggop="$1"
		elif [ "$1" = "--gep" ]; then 
			shift   
			pgaof="$1"
		elif [ "$1" = "--gexp" ]; then 
			shift   
			pgexp="$1"
		elif [ "$1" = "--laraparams" ]; then 
			shift   
			laraparams="$1"
		elif [ "$1" = "--corethr" ]; then 
			shift   
			corethr="$1"
		elif [ "$1" = "--corewin" ]; then 
			shift   
			corewin="$1"
		elif [ "$1" = "--seedtable" ]; then
			shift
			seedtable="y"
			seedtablefile="$1"
		elif [ "$1" = "--seed" ]; then
			shift
			seed="m"
			seedfiles="$seedfiles $1"
		elif [ $progname = "fftns" -o  $progname = "nwns" ]; then
			if [ "$1" -gt 0 ]; then
				cycle=`expr "$1" - 0`
			fi
		else
			echo "Unknown option:  $1" 1>&2
			er=1;
		fi
		shift   
	done;



#	TMPFILE=/tmp/$progname.$$
	TMPFILE=`mktemp -dt $progname.XXXXXXXXXX`
	if [ $? -ne 0 ]; then
		echo "mktemp seems to be obsolete. Re-trying without -t" 1>&2
		TMPFILE=`mktemp -d /tmp/$progname.XXXXXXXXXX`
	fi	
	umask 077
#	mkdir  $TMPFILE  || er=1
	if [ $debug -eq 1 ]; then
		trap "tar cfvz debuginfo.tgz $TMPFILE; rm -rf $TMPFILE " 0
	else
		trap "rm -rf $TMPFILE" 0
	fi
	if [ $# -eq 1 ]; then
		if [ -r "$1" -o "$1" = - ]; then

			if [ -r "$addfile" ]; then
				printf '';
			else
				echo "$0": Cannot open "$addfile". 1>&2
				exit 1;
			fi

			cat "$1"              | tr "\r" "\n" > $TMPFILE/infile 
			echo ""                             >> $TMPFILE/infile
			cat "$addfile"        | tr "\r" "\n" | grep -v "^$" >> $TMPFILE/infile
			cat "$addfile"        | tr "\r" "\n" | grep -v "^$" > $TMPFILE/_addfile
			cat "$aamatrix"       | tr "\r" "\n" | grep -v "^$" > $TMPFILE/_aamtx
			cat "$treeinfile"     | tr "\r" "\n" | grep -v "^$" > $TMPFILE/_guidetree
			cat "$seedtablefile"  | tr "\r" "\n" | grep -v "^$" > $TMPFILE/_seedtablefile
			cat "$laraparams"     | tr "\r" "\n" | grep -v "^$" > $TMPFILE/_lara.params
#			echo $seedfiles
			infilename="$1"
			seedfilesintmp="/dev/null"
			seednseq="0"
			set $seedfiles > /dev/null
			while [ $# -gt 1 ];
			do
				shift
				if [ -r "$1" ]; then
					cat "$1" | tr "\r" "\n" >  $TMPFILE/seed$#
				else
					echo "$0": Cannot open "$1". 1>&2
					exit 1;
				fi
				seednseq=$seednseq" "`grep -c '^[>|=]' $TMPFILE/seed$#`
				seedfilesintmp=$seedfilesintmp" "seed$#
			done
#			ls $TMPFILE
#			echo $seedfilesintmp
#			echo $seednseq


		else
			echo "$0": Cannot open "$1". 1>&2
			er=1
#			exit 1;
		fi
	else
		echo '$#'"=$#" 1>&2
		er=1
	fi


	if [ $os != "linux" ]; then
		numthreads=0
	fi

	if [ $numthreads -eq 0 -a $parallelizationstrategy = "BESTFIRST" ]; then
		echo 'Impossible' 1>&2;
		exit 1;
	fi

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

	if [ $parallelizationstrategy = "BESTFIRST" -o  $parallelizationstrategy = "BAATARI0" ]; then
		iteratelimit=254
	else
		iteratelimit=16
	fi
	if [ $iterate -gt $iteratelimit ]; then    #??
		iterate=$iteratelimit
	fi

	if [ $rnaalifold -eq 1 ]; then
		rnaopt=" -e $rgep -o $rgop -c $weightm -r $weightr -R $rnascoremtx "
#		rnaoptit=" -o $rgop -BT -c $weightm -r $weightr -R "
		rnaoptit=" -o $rgop -F -c $weightm -r $weightr -R "
	elif [ $mccaskill -eq 1 -o $contrafold -eq 1 ]; then
		rnaopt=" -o $rgop -c $weightm -r $weightr "
#		rnaoptit=" -e $rgep -o $rgop -BT -c $weightm -r $weightr $rnascoremtx "
		rnaoptit=" -e $rgep -o $rgop -F -c $weightm -r $weightr $rnascoremtx "
	else
		rnaopt="  "
		rnaoptit=" -F "
	fi

	if [ $algspecified -eq 0 ]; then
		if [ $distance = "parttree" ]; then 
			algopt=" -Q "
			algoptit=" "
		else
			algopt=" "
			algoptit=" "
		fi
	fi

	model="$sbstmodel $kappa $fmodel"

	if [ $er -eq 1 ]; then
		echo "------------------------------------------------------------------------------" 1>&2
		echo "  MAFFT" $version 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://mafft.cbrc.jp/alignment/software/" 1>&2
		echo "  NAR 30:3059-3066 (2002), Briefings in Bioinformatics 9:286-298 (2008)"        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 (fast)" 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 "If unsure which option to use:" 1>&2
		echo "  % mafft --auto 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.0" 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 "--thread # :     Number of threads. (# must be <= number of physical cores - 1)" 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 [ $distance = "lara" -o $distance = "slara" ]; then
		if [ ! -x "$prefix/mafft_lara" ]; then
			echo ""       1>&2
			echo "== Install LaRA =========================================================" 1>&2
			echo "This option requires LaRA (Bauer et al. http://www.planet-lisa.net/)."     1>&2
			echo "The executable have to be renamed to 'mafft_lara' and installed into "     1>&2
			echo "the $prefix directory. "                                                   1>&2
			echo "A configuration file of LaRA also have to be given"                        1>&2
			echo "mafft-xinsi --larapair --laraparams parameter_file"                        1>&2
			echo "mafft-xinsi --slarapair --laraparams parameter_file"                       1>&2
			echo "=========================================================================" 1>&2
			echo "" 1>&2
			exit 1
		fi
		if [ ! -s "$laraparams" ]; then
			echo ""       1>&2
			echo "== Configure LaRA =======================================================" 1>&2
			echo "A configuration file of LaRA have to be given"                             1>&2
			echo "mafft-xinsi --larapair --laraparams parameter_file"                        1>&2
			echo "mafft-xinsi --slarapair --laraparams parameter_file"                       1>&2
			echo "=========================================================================" 1>&2
			echo "" 1>&2
			exit 1
		fi
	fi
	if [ $distance = "foldalignlocal" -o $distance = "foldalignglobal" ]; then
	if [ ! -x "$prefix/foldalign210" ]; then
			echo ""       1>&2
			echo "== Install FOLDALIGN ====================================================" 1>&2
			echo "This option requires FOLDALIGN (Havgaard et al. http://foldalign.ku.dk/)." 1>&2
			echo "The executable have to be renamed to 'foldalign210' and installed into "   1>&2
			echo "the $prefix directory. "                                                   1>&2
			echo "=========================================================================" 1>&2
			echo "" 1>&2
			exit 1
		fi
	fi
	if [ $distance = "scarna" ]; then
		if [ ! -x "$prefix/mxscarnamod" ]; then
			echo ""       1>&2
			echo "== Install MXSCARNA ======================================================" 1>&2
			echo "MXSCARNA (Tabei et al. BMC Bioinformatics 2008 9:33) is required."          1>&2
			echo "Please 'make' at the 'extensions' directory of the MAFFT source package,"   1>&2
			echo "which contains the modified version of MXSCARNA."                           1>&2
			echo "http://align.bmr.kyushu-u.ac.jp/mafft/software/source.html "                1>&2
			echo "==========================================================================" 1>&2
			echo "" 1>&2
			exit 1
		fi
	fi
	if [ $mccaskill -eq 1 ]; then
		if [ ! -x "$prefix/mxscarnamod" ]; then
			echo ""       1>&2
			echo "== Install MXSCARNA ======================================================" 1>&2
			echo "MXSCARNA (Tabei et al. BMC Bioinformatics 2008 9:33) is required."          1>&2
			echo "Please 'make' at the 'extensions' directory of the MAFFT source package,"   1>&2
			echo "which contains the modified version of MXSCARNA."                           1>&2
			echo "http://align.bmr.kyushu-u.ac.jp/mafft/software/source.html "                1>&2
			echo "==========================================================================" 1>&2
			echo "" 1>&2
			exit 1
		fi
	fi
	if [ $contrafold -eq 1 ]; then
		if [ ! -x "$prefix/contrafold" ]; then
			echo ""       1>&2
			echo "== Install CONTRAfold ===================================================" 1>&2
			echo "This option requires CONTRAfold"                                           1>&2
			echo "(Do et al. http://contra.stanford.edu/contrafold/)."                       1>&2
			echo "The executable 'contrafold' have to be installed into "                    1>&2
			echo "the $prefix directory. "                                                   1>&2
			echo "=========================================================================" 1>&2
			echo "" 1>&2
			exit 1
		fi
	fi

#old
#	if [ $treeout -eq 1 ]; then
#		parttreeoutopt="-t"
#		if [ $cycle -eq 0 ]; then
#			treeoutopt="-t -T"
#			groupsize=1
#			iterate=0 
#			if [ $distance = "global" -o $distance = "local" -o $distance = "localgenaf" -o $distance = "globalgenaf" ]; then
#				distance="distonly"
#			fi
#		else
#			treeoutopt="-t"
#		fi
#	else
#		parttreeoutopt=" "
#		if [ $cycle -eq 0 ]; then
#			treeoutopt="-t -T"
#			iterate=0 
#			if [ $distance = "global" -o $distance = "local" -o $distance = "localgenaf" -o $distance = "globalgenaf" ]; then
#				distance="distonly"
#			fi
#		else
#			treeoutopt=" "
#		fi
#	fi

#new
	if [ $cycle -eq 0 ]; then
		treeoutopt="-t -T"
		iterate=0 
		if [ $distance = "global" -o $distance = "local" -o $distance = "localgenaf" -o $distance = "globalgenaf" ]; then
			distance="distonly"
		fi
		if [ $treeout -eq 1 ]; then
			parttreeoutopt="-t"
			groupsize=1
		else
			parttreeoutopt=" "
		fi
		if [ $distout -eq 1 ]; then
			distoutopt="-y -T"
		fi
	else
		if [ $treeout -eq 1 ]; then
			parttreeoutopt="-t"
			treeoutopt="-t"
		else
			parttreeoutopt=" "
			treeoutopt=" "
		fi
		if [ $distout -eq 1 ]; then
			distoutopt="-y"
		fi
	fi
#

	formatcheck=`grep -c '^[[:blank:]]\+>' $TMPFILE/infile | head -1 `
	if [ $formatcheck -gt 0 ]; then
		echo "The first character of a description line must be " 1>&2
		echo "the greater-than (>) symbol, not a blank."           1>&2
		echo "Please check the format around the following line(s):"  1>&2
		grep -n '^[[:blank:]]\+>' $TMPFILE/infile  1>&2
		exit 1
	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 [ $nseq -gt 6000 -a $iterate -gt 1 ]; then
		echo "Too many sequences to perform iterative refinement!" 1>&2
		echo "Please use a progressive method." 1>&2
		exit 1
	fi


	if [ $distance = "sixtuples" -a \( $seed = "x" -a $seedtable = "x" \) ]; then
		localparam=" "
	elif [ $distance = "sixtuples" -a \( $seed != "x" -o $seedtable != "x" \) ]; then
		if [ $cycle -lt 2 ]; then
			cycle=2                # nazeda
		fi
		if [ $iterate -lt 2 ]; then
			echo "############################################################################" 1>&2
			echo "# Warning:" 1>&2
			echo "#   Progressive alignment method is incompatible with the --seed option." 1>&2
			echo "#   Automatically switched to the iterative refinement method." 1>&2
			echo "#   " 1>&2
			echo "# Also consider using the '--add' option, which is compatible with" 1>&2
			echo "#   the progressive method and FASTER than the '--seed' option." 1>&2
			echo "#   Usage is:" 1>&2
			echo "#   % mafft --add newSequences existingAlignment > output" 1>&2
			echo "############################################################################" 1>&2
			iterate=2
		fi
		localparam="-l "$weighti
	elif [ $distance = "parttree" ]; then
		localparam=" "
		if [ $groupsize -gt -1 ]; then
			cycle=1
		fi
	else
		localparam=" -l "$weighti
		if [ $cycle -gt 1 ]; then  # 09/01/08
			cycle=1
		fi
	fi

	if [ $distance = "localgenaf" -o $distance = "globalgenaf" ]; then
		aof="0.000"
	fi

	if [ "$memopt" = " -M -B " -a "$distance" != "sixtuples" ]; then
		echo "Impossible" 1>&2
		exit 1
	fi
#exit

	if [ $distance = "parttree" ]; then
		if [ $seed != "x" -o $seedtable != "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 5000 ]; 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 [ $seed != "x" -a $seedtable != "x" ]; then
			echo 'Use either one of seedtable and seed.  Not both.' 1>&2
			exit 1
	fi
#	if [ $seedtable != "x" -a $anysymbol -gt 0 ]; then
#			echo 'The combination of --seedtable and --anysymbol is not supported.' 1>&2
#			exit 1
#	fi

	if [ $treein -eq 1 ]; then
#		if [ $iterate -gt 0 ]; then
#			echo 'Not supported yet.' 1>&2
#			exit 1
#		fi
		cycle=1
	fi

	if [ "$addarg0" != " " ]; then
		"$prefix/countlen" < $TMPFILE/_addfile > $TMPFILE/addsize 2>>"$progressfile"
		nadd=`awk '{print $1}' $TMPFILE/addsize`
		if [ $nadd -eq "0" ]; then
			echo Check $addfile 1>&2
			exit 1;
		fi
		addarg="$addarg0 $nadd"
		cycle=1
		iterate=0
		if [ $seed != "x" -o $seedtable != "x" ]; then
			echo 'Impossible' 1>&2;
			echo 'Use either ONE of --seed, --seedtable, --addprofile and --add.' 1>&2
			exit 1;
		fi
	fi

	if [ $mccaskill -eq 1 -o $rnaalifold -eq 1 -o $contrafold -eq 1 ]; then
		if [ $distance = "sixtuples" ]; then
			echo 'Not supported.' 1>&2
			echo 'Please add --globalpair, --localpair, --scarnapair,' 1>&2
			echo '--larapair, --slarapair, --foldalignlocalpair or --foldalignglobalpair' 1>&2
			exit 1
		fi
	fi

	if [ $mccaskill -eq 1 -o $rnaalifold -eq 1 -o $contrafold -eq 1 ]; then
		if [ $distance = "scarna" -o $distance = "lara" -o $distance = "slara" -o $distance = "foldalignlocal" -o $distance = "foldalignglobal" ]; then
			strategy="X-I"
		elif [ $distance = "global" -o $distance = "local" -o $distance = "localgenaf" -o "globalgenaf" ]; then
			strategy="Q-I"
		fi
	elif [ $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" -o $distance = "distonly" ]; then
		strategy="G-I"
	elif [ $distance = "local" ]; then
		strategy="L-I"
	elif [ $distance = "localgenaf" ]; then
		strategy="E-I"
	elif [ $distance = "globalgenaf" ]; then
		strategy="K-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 = "X-INS-i" ]; then
		explanation='RNA secondary structure information is taken into account.'
		performance='For short RNA sequences only, extremely 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 $TMPFILE/order  "
	elif [ $outputformat = "clustal" -a $outorder = "input" ]; then
		outputopt=" -c $strategy  "
	elif [ $outputformat = "phylip" -a $outorder = "aligned" ]; then
		outputopt=" -y -r $TMPFILE/order "
	elif [ $outputformat = "phylip" -a $outorder = "input" ]; then
		outputopt=" -y "
	elif [ $outputformat = "pir" -a $outorder = "aligned" ]; then
		outputopt=" -f -r $TMPFILE/order "
	else
		outputopt="null"
	fi
	
	(
		cd $TMPFILE;

		echo "" 1>"$progressfile"
		echo "nseq = " $nseq              1>>"$progressfile"
		echo "nlen = " $nlen              1>>"$progressfile"
		echo "distance = " $distance      1>>"$progressfile"
		echo "iterate = " $iterate        1>>"$progressfile"
		echo "cycle = " $cycle            1>>"$progressfile"

		if [ $anysymbol -eq 1 ]; then
			mv infile orig
			"$prefix/replaceu" -i orig > infile 2>>"$progressfile" || exit 1
		fi

		if [ $seed != "x" ]; then
			mv infile infile2
			if [ $anysymbol -eq 1 ]; then
				mv orig orig2
				cat /dev/null > orig
			fi
			cat /dev/null > infile
			cat /dev/null > hat3.seed
			seedoffset=0
#			echo "seednseq="$seednseq
#			echo "seedoffset="$seedoffset
			set $seednseq >> "$progressfile"
#			echo $#
			while [ $# -gt 1 ]
			do
				shift
#				echo "num="$#

				if [ $anysymbol -eq 1 ]; then
					cat seed$# >> orig
					"$prefix/replaceu" -i seed$# -o $seedoffset > clean 2>>"$progressfile" || exit 1
					mv clean seed$#
				fi
				"$prefix/multi2hat3s" -t $nseq -o $seedoffset -i seed$# >> infile 2>>"$progressfile" || exit 1
				cat hat3 >> hat3.seed
#				echo "$1"
				seedoffset=`expr $seedoffset + $1`
#				echo "$1"
#				echo "seedoffset="$seedoffset
			done;
#			echo "seedoffset="$seedoffset
			if [ $anysymbol -eq 1 ]; then
				"$prefix/replaceu" -i orig2 -o $seedoffset >> infile 2>>"$progressfile" || exit 1  # yarinaoshi
				cat orig2 >> orig
			else
				cat infile2 >> infile
			fi
		elif [ $seedtable != "x" ]; then
			cat _seedtablefile > hat3.seed
		else
			cat /dev/null > hat3.seed
		fi
#		cat hat3.seed


		if [ $mccaskill -eq 1 ]; then
			"$prefix/mccaskillwrap" -C $numthreads -d "$prefix" -i infile > hat4 2>>"$progressfile" || exit 1
		elif [ $contrafold -eq 1 ]; then
			"$prefix/contrafoldwrap" -d "$prefix" -i infile > hat4 2>>"$progressfile" || exit 1
		fi
		if [ $distance = "fasta" ]; then
			"$prefix/dndfast7" $swopt < infile > /dev/null  2>>"$progressfile"    || exit 1
			cat hat3.seed hat3 > hatx
			mv hatx hat3
			"$prefix/tbfast" $outnum $addarg -C $numthreads $rnaopt $weightopt $treeinopt $treeoutopt $distoutopt $seqtype $model -f "-"$gop  -h "-"$aof  $param_fft $localparam   $algopt $treealg $scoreoutarg < infile   > /dev/null 2>>"$progressfile" || exit 1
		elif [ $distance = "blast" ]; then
			"$prefix/dndblast" < infile > /dev/null  2>>"$progressfile"      || exit 1
			cat hat3.seed hat3 > hatx
			mv hatx hat3
			"$prefix/tbfast" $outnum $addarg -C $numthreads $rnaopt $weightopt $treeinopt $treeoutopt $distoutopt $seqtype $model -f "-"$gop  -h "-"$aof  $param_fft $localparam   $algopt $treealg $scoreoutarg < infile   > /dev/null 2>>"$progressfile" || exit 1
		elif [ $distance = "foldalignlocal" ]; then
			"$prefix/pairlocalalign" -C $numthreads $seqtype $foldalignopt $model -g $lexp -f $lgop -h $laof -H -d "$prefix" < infile > /dev/null  2>>"$progressfile"      || exit 1
			cat hat3.seed hat3 > hatx
			mv hatx hat3
			"$prefix/tbfast" $outnum $addarg -C $numthreads $rnaopt $weightopt $treeinopt $treeoutopt $distoutopt $seqtype $model -f "-"$gop  -h "-"$aof  $param_fft $localparam   $algopt $treealg $scoreoutarg < infile   > /dev/null 2>>"$progressfile" || exit 1
		elif [ $distance = "foldalignglobal" ]; then
			"$prefix/pairlocalalign" -C $numthreads $seqtype $foldalignopt $model -g $pgexp -f $pggop -h $pgaof -H -o -global -d "$prefix" < infile > /dev/null  2>>"$progressfile"      || exit 1
			cat hat3.seed hat3 > hatx
			mv hatx hat3
			"$prefix/tbfast" $outnum $addarg -C $numthreads $rnaopt $weightopt $treeinopt $treeoutopt $distoutopt $seqtype $model -f "-"$gop  -h "-"$aof  $param_fft $localparam   $algopt $treealg $scoreoutarg < infile   > /dev/null 2>>"$progressfile" || exit 1
		elif [ $distance = "slara" ]; then
			"$prefix/pairlocalalign" -C $numthreads -p $laraparams  $seqtype $model  -f $lgop  -T -d "$prefix" < infile > /dev/null  2>>"$progressfile"      || exit 1
			cat hat3.seed hat3 > hatx
			mv hatx hat3
			"$prefix/tbfast" $outnum $addarg -C $numthreads $rnaopt $weightopt $treeinopt $treeoutopt $distoutopt $seqtype $model -f "-"$gop  -h "-"$aof  $param_fft $localparam   $algopt $treealg $scoreoutarg < infile   > /dev/null 2>>"$progressfile" || exit 1
		elif [ $distance = "lara" ]; then
			"$prefix/pairlocalalign" -C $numthreads -p $laraparams  $seqtype $model  -f $lgop  -B -d "$prefix" < infile > /dev/null  2>>"$progressfile"      || exit 1
			cat hat3.seed hat3 > hatx
			mv hatx hat3
			"$prefix/tbfast" $outnum $addarg -C $numthreads $rnaopt $weightopt $treeinopt $treeoutopt $distoutopt $seqtype $model -f "-"$gop  -h "-"$aof  $param_fft $localparam   $algopt $treealg $scoreoutarg < infile   > /dev/null 2>>"$progressfile" || exit 1
		elif [ $distance = "scarna" ]; then
			"$prefix/pairlocalalign"   -C $numthreads $seqtype $model  -f $pggop  -s -d "$prefix" < infile > /dev/null  2>>"$progressfile"      || exit 1
			cat hat3.seed hat3 > hatx
			mv hatx hat3
			"$prefix/tbfast" $outnum $addarg -C $numthreads $rnaopt $weightopt $treeinopt $treeoutopt $distoutopt $seqtype $model -f "-"$gop  -h "-"$aof  $param_fft $localparam   $algopt $treealg $scoreoutarg < infile   > /dev/null 2>>"$progressfile" || exit 1
		elif [ $distance = "global" ]; then
			"$prefix/pairlocalalign"   -C $numthreads $seqtype $model -g $pgexp -f $pggop  -h $pgaof  -F < infile > /dev/null  2>>"$progressfile"      || exit 1
			cat hat3.seed hat3 > hatx
			mv hatx hat3
			"$prefix/tbfast" $outnum $addarg -C $numthreads $rnaopt $weightopt $treeinopt $treeoutopt $distoutopt $seqtype $model -f "-"$gop  -h "-"$aof  $param_fft $localparam   $algopt $treealg $scoreoutarg < infile   > /dev/null 2>>"$progressfile" || exit 1
		elif [ $distance = "local" ]; then
			"$prefix/pairlocalalign"   -C $numthreads $seqtype $model  -g $lexp -f $lgop  -h $laof -L < infile > /dev/null  2>>"$progressfile"      || exit 1
			cat hat3.seed hat3 > hatx
			mv hatx hat3
			"$prefix/tbfast" $outnum $addarg -C $numthreads $rnaopt $weightopt $treeinopt $treeoutopt $distoutopt $seqtype $model -f "-"$gop  -h "-"$aof  $param_fft $localparam   $algopt $treealg $scoreoutarg < infile   > /dev/null 2>>"$progressfile" || exit 1
		elif [ $distance = "globalgenaf" ]; then
			"$prefix/pairlocalalign"   -C $numthreads $seqtype $model  -g $pgexp -f $pggop  -h $pgaof -O $GGOP -E $GEXP -K  < infile > /dev/null 2>>"$progressfile"    || exit 1
			cat hat3.seed hat3 > hatx
			mv hatx hat3
			"$prefix/tbfast" $outnum $addarg -C $numthreads $rnaopt $weightopt $treeinopt $treeoutopt $distoutopt $seqtype $model -f "-"$gop  -h "-"$aof  $param_fft $localparam   $algopt $treealg $scoreoutarg < infile   > /dev/null 2>>"$progressfile" || exit 1
		elif [ $distance = "localgenaf" ]; then
			"$prefix/pairlocalalign"   -C $numthreads $seqtype $model -g $lexp -f $lgop  -h $laof -O $LGOP -E $LEXP -N < infile > /dev/null  2>>"$progressfile"      || exit 1
			cat hat3.seed hat3 > hatx
			mv hatx hat3
			"$prefix/tbfast" $outnum $addarg -C $numthreads $rnaopt $weightopt $treeinopt $treeoutopt $distoutopt $seqtype $model -f "-"$gop  -h "-"$aof  $param_fft $localparam   $algopt $treealg $scoreoutarg < infile   > /dev/null 2>>"$progressfile" || exit 1
		elif [ $distance = "distonly" ]; then
			"$prefix/pairlocalalign"   -C $numthreads $seqtype $model -g $pgexp -f $pggop  -h $pgaof  -t < infile > /dev/null  2>>"$progressfile"      || exit 1
			"$prefix/tbfast" $outnum $addarg -C $numthreads $rnaopt $weightopt $treeinopt $treeoutopt $distoutopt $seqtype $model -f "-"$gop  -h "-"$aof  $param_fft $localparam   $algopt $treealg $scoreoutarg < infile   > /dev/null 2>>"$progressfile" || exit 1
		elif [ $distance = "parttree" ]; then
			"$prefix/splittbfast" $algopt $splitopt $partorderopt $parttreeoutopt $memopt $seqtype $model -f "-"$gop  -h "-"$aof  $param_fft -p $partsize -s $groupsize $treealg -i infile   > pre 2>>"$progressfile" || exit 1
			mv hat3.seed hat3
		else
			"$prefix/disttbfast" -O $outnum $addarg -C $numthreads $memopt $weightopt $treeinopt $treeoutopt $distoutopt $seqtype $model -f "-"$gop  -h "-"$aof  $param_fft $algopt $treealg $scoreoutarg < infile   > pre 2>>"$progressfile" || exit 1
			mv hat3.seed hat3
		fi
		while [ $cycle -gt 1 ]
		do
			if [ $distance = "parttree" ]; then
				mv pre infile
				"$prefix/splittbfast" -Z $algopt $splitopt $partorderopt $parttreeoutopt $memopt $seqtype $model -f "-"$gop  -h "-"$aof  $param_fft -p $partsize -s $groupsize $treealg -i infile   > pre 2>>"$progressfile" || exit 1
			else
				"$prefix/tbfast" -O $outnum -C $numthreads $rnaopt $weightopt $treeoutopt $distoutopt $memopt $seqtype $model  -f "-"$gop  -h "-"$aof $param_fft  $localparam $algopt -J $treealg $scoreoutarg < pre > /dev/null 2>>"$progressfile" || exit 1
			fi
			cycle=`expr $cycle - 1`
		done
		if [ $iterate -gt 0 ]; then
			if [ $distance = "sixtuples" ]; then
			    "$prefix/dndpre" -C $numthreads < pre     > /dev/null 2>>"$progressfile" || exit 1
			fi
			"$prefix/dvtditr" -C $numthreads -t $randomseed $rnaoptit $memopt $scorecalcopt $localparam -z 50 $seqtype $model -f "-"$gop -h "-"$aof  -I $iterate $weightopt $treeinopt $algoptit $treealg -p $parallelizationstrategy  $scoreoutarg < pre     > /dev/null 2>>"$progressfile" || exit 1
		fi
		if [ $coreout -eq 1 ]; then
			"$prefix/setcore" -w $corewin -i $corethr $coreext < pre > pre2
			mv pre2 pre
		elif [ $anysymbol -eq 1 ]; then
			"$prefix/restoreu" -a pre -i orig > restored || exit 1
			mv restored pre
		fi

		echo '' 1>>"$progressfile"
		if [ $mccaskill -eq 1 ]; then
			echo "RNA base pairing probaility was calculated by the McCaskill algorithm (1)" 1>>"$progressfile"
			echo "implemented in Vienna RNA package (2) and MXSCARNA (3), and then" 1>>"$progressfile"
			echo "incorporated in the iterative alignment process (4)." 1>>"$progressfile"
			echo "(1) McCaskill, 1990, Biopolymers 29:1105-1119" 1>>"$progressfile"
			echo "(2) Hofacker et al., 2002, J. Mol. Biol. 319:3724-3732" 1>>"$progressfile"
			echo "(3) Tabei et al., 2008, BMC Bioinformatics 9:33" 1>>"$progressfile"
			echo "(4) Katoh and Toh, 2008, BMC Bioinformatics 9:212" 1>>"$progressfile"
			echo "" 1>>"$progressfile"
		elif [ $contrafold -eq 1 ]; then
			echo "RNA base pairing probaility was calculated by the CONTRAfold algorithm (1)" 1>>"$progressfile"
			echo "and then incorporated in the iterative alignment process (4)." 1>>"$progressfile"
			echo "(1) Do et al., 2006, Bioinformatics 22:e90-98" 1>>"$progressfile"
			echo "(2) Katoh and Toh, 2008, BMC Bioinformatics 9:212" 1>>"$progressfile"
			echo "" 1>>"$progressfile"
		fi
		if [ $distance = "fasta" -o $partdist = "fasta" ]; then
			echo "Pairwise alignments were computed by FASTA" 1>>"$progressfile"
			echo "(Pearson & Lipman, 1988, PNAS 85:2444-2448)" 1>>"$progressfile"
		fi
		if [ $distance = "blast" ]; then
			echo "Pairwise alignments were computed by BLAST" 1>>"$progressfile"
			echo "(Altschul et al., 1997, NAR 25:3389-3402)" 1>>"$progressfile"
		fi
		if [ $distance = "scarna" ]; then
			echo "Pairwise alignments were computed by MXSCARNA" 1>>"$progressfile"
			echo "(Tabei et al., 2008, BMC Bioinformatics 9:33)." 1>>"$progressfile"
		fi
		if [ $distance = "lara" -o $distance = "slara" ]; then
			echo "Pairwise alignments were computed by LaRA" 1>>"$progressfile"
			echo "(Bauer et al., 2007, BMC Bioinformatics 8:271)." 1>>"$progressfile"
		fi
		if [ $distance = "foldalignlocal" ]; then
			echo "Pairwise alignments were computed by FOLDALIGN (local)" 1>>"$progressfile"
			echo "(Havgaard et al., 2007, PLoS Computational Biology 3:e193)." 1>>"$progressfile"
		fi
		if [ $distance = "foldalignglobal" ]; then
			echo "Pairwise alignments were computed by FOLDALIGN (global)" 1>>"$progressfile"
			echo "(Havgaard et al., 2007, PLoS Computational Biology 3:e193)." 1>>"$progressfile"
		fi
		printf "\n" 1>>"$progressfile"
		echo 'Strategy:' 1>>"$progressfile"
		printf ' '$strategy 1>>"$progressfile"
		echo ' ('$performance')' 1>>"$progressfile"
		echo ' '$explanation 1>>"$progressfile"
		echo '' 1>>"$progressfile"
		echo "If unsure which option to use, try 'mafft --auto input > output'." 1>>"$progressfile"
#		echo "If long gaps are expected, try 'mafft --ep 0.0 --auto input > output'." 1>>"$progressfile"
		echo "If the possibility of long gaps can be excluded, add '--ep 0.123'." 1>>"$progressfile"
		echo "For more information, see 'mafft --help', 'mafft --man' and the mafft page." 1>>"$progressfile"
		echo '' 1>>"$progressfile"
	)

	if [ "$outputfile" = "" ]; then
		if [ "$outputopt" = "null" ]; then
			cat < $TMPFILE/pre || exit 1
		else
			"$prefix/f2cl" -n $namelength $outputopt < $TMPFILE/pre 2>>/dev/null || exit 1
		fi
	else
		if [ "$outputopt" = "null" ]; then
			cat < $TMPFILE/pre > "$outputfile" || exit 1
		else
			"$prefix/f2cl" -n $namelength $outputopt < $TMPFILE/pre > "$outputfile" 2>>/dev/null || exit 1
		fi
	fi

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

	if [ $distout -eq 1 ]; then
		cp $TMPFILE/hat2 "$infilename.hat2"
	fi

	exit 0;
fi

prog="awk"

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

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

#echo "prog="$prog 1>&2

umask 077
(
$prog '
BEGIN {
	prefix = ENVIRON["prefix"];
	version = ENVIRON["version"];
	myself = ENVIRON["myself"];
	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) 2011 Kazutaka Katoh\n" )                                  > "/dev/tty";
		printf( "        NAR 30:3059-3066, NAR 33:511-518\n" )                                   > "/dev/tty";
		printf( "        http://mafft.cbrc.jp/alignment/software/\n" )       > "/dev/tty";
		printf( "---------------------------------------------------------------------\n" )      > "/dev/tty";
		printf( "\n" ) > "/dev/tty";
	
		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;
			infile = sprintf( "%s", $0 );
	
			res = getline < infile;
			close( infile );
			if( res == -1 )
			{
				printf( "%s: No such file.\n\n", infile ) > "/dev/tty";
				printf( "Filename extension (eg., .txt) must be typed, if any.\n\n" ) > "/dev/tty";
			}
			else if( res == 0 )
				printf( "%s: Empty.\n", infile ) > "/dev/tty";
			else
			{
				printf( "OK. infile = %s\n\n", infile ) > "/dev/tty";
				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", $0 );
				printf( "OK. outfile = %s\n\n", outfile ) > "/dev/tty";
				break;
			}
		}
	
		while( 1 )
		{
			outargs = "";
			printf( "\n" ) > "/dev/tty";
			printf( "Output format?\n" ) > "/dev/tty";
			printf( "  1. Clustal format / Sorted\n" ) > "/dev/tty";
			printf( "  2. Clustal format / Input order\n" ) > "/dev/tty";
			printf( "  3. Fasta format   / Sorted\n" ) > "/dev/tty";
			printf( "  4. Fasta format   / Input order\n" ) > "/dev/tty";
			printf( "  5. Phylip format  / Sorted\n" ) > "/dev/tty";
			printf( "  6. Phylip format  / Input order\n" ) > "/dev/tty";
			printf( "@ " ) > "/dev/tty";
			res = getline < "/dev/tty";
			close( "/dev/tty" );
#			printf( "res=%d, NF=%d\n", res, NF );

			resnum = 0 + $1;
#			printf( "resnum=%d\n", resnum );

			if( resnum < 1 || 6 < resnum )
				continue;
			else
			{
				if( resnum == 1 )
					outargs = "--clustalout --reorder";
				else if( resnum == 2 )
					outargs = "--clustalout --inputorder";
				else if( resnum == 3 )
					outargs = "--reorder";
				else if( resnum == 4 )
					outargs = "--inputorder";
				else if( resnum == 5 )
					outargs = "--phylipout --reorder";
				else if( resnum == 6 )
					outargs = "--phylipout --inputorder";
				else
					continue;
				printf( "OK. arguments = %s\n\n", outargs ) > "/dev/tty";
				break;
			}
		}
	
		while( 1 )
		{
			arguments = "";
			printf( "\n" ) > "/dev/tty";
			printf( "Strategy?\n" ) > "/dev/tty";
			printf( "  1. --auto\n" ) > "/dev/tty";
			printf( "  2. FFT-NS-1 (fast)\n" ) > "/dev/tty";
			printf( "  3. FFT-NS-2 (default)\n" ) > "/dev/tty";
			printf( "  4. G-INS-i  (accurate)\n" ) > "/dev/tty";
			printf( "  5. L-INS-i  (accurate)\n" ) > "/dev/tty";
			printf( "  6. E-INS-i  (accurate)\n" ) > "/dev/tty";
			printf( "@ " ) > "/dev/tty";
			res = getline < "/dev/tty";
			close( "/dev/tty" );
#			printf( "res=%d, NF=%d\n", res, NF );

			resnum = 0 + $1;
#			printf( "resnum=%d\n", resnum );

			if( resnum < 1 || 6 < resnum )
				continue;
			else
			{
				if( resnum == 1 )
					arguments = "--auto";
				else if( resnum == 2 )
					arguments = "--retree 1";
				else if( resnum == 3 )
					arguments = "--retree 2";
				else if( resnum == 4 )
					arguments = "--globalpair --maxiterate 16";
				else if( resnum == 5 )
					arguments = "--localpair  --maxiterate 16";
				else if( resnum == 6 )
					arguments = "--genafpair  --maxiterate 16";
				else
					arguments = sprintf( "%s", $0 );
				printf( "OK. arguments = %s %s\n\n", arguments, outargs ) > "/dev/tty";
				break;
			}
		}


		while( 1 )
		{
			printf( "\n" ) > "/dev/tty";
			printf( "Additional arguments? (--ep #, --op #, --kappa #, etc)\n" ) > "/dev/tty";
			printf( "@ " ) > "/dev/tty";
			res = getline < "/dev/tty";
			close( "/dev/tty" );
			if( res == 0 || NF == 0 )
			{
				break;
			}
			else
			{
				addargs = sprintf( "%s", $0 );
				printf( "OK. arguments = %s %s %s\n\n", addargs, arguments, outargs ) > "/dev/tty";
				break;
			}
		}

		arguments = sprintf( "%s %s %s", addargs, arguments, outargs );

		print ""
		command = sprintf( "\"%s\" %s \"%s\" > \"%s\"", myself, arguments, infile, outfile );
		gsub( /\\/, "/", command );


		printf( "command=\n%s\n", command ) > "/dev/tty";
	
	
		while( 1 )
		{
			go = 0;
			printf( "OK?\n" ) > "/dev/tty";
			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" ) > "/dev/tty";
		printf( "\n" ) > "/dev/tty";
	}
	system( command );
	command = sprintf( "less \"%s\"", outfile );
	system( command );
	printf( "Press Enter to exit." ) > "/dev/tty";
	res = getline < "/dev/tty";
}
'
)
exit 0;
