/// <summary>
        /// The implementation ensures that
        /// a match fails for a given position if there is no match. Otherwise the
        /// matcher might return a match at a different position.
        /// <see cref="QUT.Bio.BioPatML.Patterns.IPattern">IPattern Match(Sequence, int) method</see>
        /// </summary>
        /// <param name="sequence"> The sequence for comparing</param>
        /// <param name="position"> Matching position</param>
        /// <returns></returns>
        public override Match Match(BioPatML.Sequences.Sequence sequence, int position)
        {
            while (Index < base.Count)
            {
                IPattern pattern = this[Index++];
                Match match = pattern.Match(sequence, position);

                // store minimum increment
                int inc = pattern.Increment;
                if (inc < Min_inc)
                    Min_inc = inc;

                if (match != null && (match.Similarity >= Threshold))
                {
                    increment = 0;
                    return match;
                }

            }

            Index = 0;
            increment = Min_inc;
            Min_inc = int.MaxValue;

            return null;
        }
        /// <summary>
        /// The implementation ensures that
        /// a match fails for a given position if there is no match. Otherwise the
        /// matcher might return a match at a different position.
        /// <see cref="QUT.Bio.BioPatML.Patterns.IPattern">IPattern Match(Sequence, int) method</see>
        /// </summary>
        /// <param name="sequence"> The sequence for comparing</param>
        /// <param name="position"> Matching position</param>
        /// <returns></returns>
        public override Match Match(BioPatML.Sequences.Sequence sequence, int position)
        {
            Match match = Matched;
            Match bestMatch = match.Clone();
            Match nextMatch = null;
            int patternNumber = Patterns.Count;
            int index = 0;

            bestMatch.Similarity = -1.0;


            while (index >= 0)
            {
                IPattern pattern = this[index];

                position = index > 0 ? match.GetSubMatch(index - 1).End + 1 : position;
                nextMatch = pattern.Match(sequence, position);

                if (nextMatch == null)
                {
                    while (--index >= 0 && this[index].Increment != 0) ;
                    continue;
                }

                pattern.Matched.Set(nextMatch);

                index++;

                if (index == patternNumber)
                {
                    while (--index >= 0 && this[index].Increment != 0) ;
                    match.CalcSimilarity();
                    if (match.Similarity >= Threshold
                            && match.Similarity > bestMatch.Similarity)
                        bestMatch.Set(match);
                }
            }

            if (bestMatch.Similarity < 0)
                return (null);

            match.Set(bestMatch);
            match.CalcStartEnd();
            match.Strand = sequence.Strand;
            match.SetSequence(sequence);

            return (match);
        }
        /// <summary>
        /// The implementation ensures that
        /// a match fails for a given position if there is no match. Otherwise the
        /// matcher might return a match at a different position.
        /// <see cref="QUT.Bio.BioPatML.Patterns.IPattern">IPattern Match(Sequence, int) method</see>
        /// </summary>
        /// <param name="sequence"> the sequence for matching</param>
        /// <param name="position"> position used for matching</param>
        /// <returns>The matched</returns>
        public override Match Match(BioPatML.Sequences.Sequence sequence, int position)
        {
            Match match = Matched;
            Match nextMatch = null;
            int patternNumber = Patterns.Count;

            while (Index >= 0)
            {
                IPattern pattern = this[Index];

                position = Index > 0 ? match.GetSubMatch(Index - 1).End + 1 : position;
                nextMatch = pattern.Match(sequence, position);

                if (nextMatch == null)
                {
                    while (--Index >= 0 && this[Index].Increment > 0) ;
                    continue;
                }
                pattern.Matched.Set(nextMatch);

                Index++;
                if (Index == patternNumber)
                {
                    while (--Index >= 0 && this[Index].Increment > 0) ;

                    increment = 0;

                    match.CalcSimilarity();

                    if (match.Similarity < Threshold)
                        return null;

                    match.CalcStartEnd();
                    match.Strand = sequence.Strand;
                    match.SetSequence(sequence);

                    return (match);
                }
            }

            increment = base.Increment;
            Index = 0;
            return (null);
        }
        /// <summary>
        /// Implementation of the pattern interface. A match is successful if
        /// every pattern of the profile matches successfully and the mean
        /// similarity is higher or equal than the profile similarity threshold.
        /// This matching method trys to match all possibilities which are 
        /// allowed be the profile patterns and the gaps in between.
        /// <see cref="QUT.Bio.BioPatML.Patterns.IMatcher">IMatcher Match Method</see>
        /// </summary>
        /// <param name="sequence"></param>
        /// <param name="position"></param>
        /// <returns></returns>
        public override Match Match(BioPatML.Sequences.Sequence sequence, int position)
        {
            Match match = Matched;
            Match nextMatch = null;
            int patternNumber = Count; //patternList.Count;

            increment = 0;
            while (Index >= 0)
            {
                ProfileElement element = this[Index];

                position = Index > 0 ? element.GapStart : position;

                nextMatch = NextMatch(element, sequence, position);
                if (nextMatch == null)
                {
                    element.ResetGap();
                    Index--;
                    continue;
                }

                element.Pattern.Matched.Set(nextMatch);

                Index++;

                if (Index >= patternNumber)
                {
                    Index = patternNumber - 1;

                    match.CalcSimilarity();
                    if (match.Similarity < Threshold)
                        return (null);

                    match.CalcStartEnd();
                    match.Strand = sequence.Strand;
                    match.SetSequence(sequence);

                    return (match);
                }
            }

            increment = base.Increment;
            Index = 0;
            return (null);
        }
        /// <summary>
        /// Implementation of the pattern interface. A match is successful if
        /// every pattern of the profile matches successfully and the mean
        /// similarity is higher or equal than the profile similarity threshold
        /// <see cref="QUT.Bio.BioPatML.Patterns.IMatcher">IMatcher Match Method</see>
        /// </summary>
        /// <param name="sequence"></param>
        /// <param name="position"></param>
        /// <returns></returns>
        public override Match Match(BioPatML.Sequences.Sequence sequence, int position)
        {
            Match match = Matched;
            Match maxMatch = null;
            int patternNumber = patternList.Count;

            for (int index = 0; index < patternNumber; index++)
            {
                ProfileElement element = this[index];
                position = index > 0 ? element.GapStart : position;
                maxMatch = MaxMatch(element, sequence, position);
                if (maxMatch == null)
                    return (null);
                element.Pattern.Matched.Set(maxMatch);
            }

            match.CalcSimilarity();
            if (match.Similarity < Threshold)
                return (null);

            match.CalcStartEnd();
            match.Strand = sequence.Strand;
            match.SetSequence(sequence);

            return (match);
        }