public void MotifTestMatchAlternatives ()
        {
            Sequence seq = new Sequence(AlphabetType.DNA, "atgc");
            Motif motif = new Motif("test", AlphabetType.DNA,"[ga]tg[c]", 0.0);
            Match match = motif.Match(seq, 1);
            Assert.AreEqual(1.0, match.Similarity, 1e-2);
        }
        public void TestMatch() {
            Sequence  seq   = new Sequence(AlphabetType.DNA,"tttaagaacaagttt");
            Motif     motif = new Motif("motif", AlphabetType.DNA,"aag", 0.5);
            Iteration iteration;
            Match     match;

            iteration = new Iteration("test",motif,1,3,0.0);
            match     = iteration.Match(seq, 4);
            Assert.AreEqual(4, match.Start);
            Assert.AreEqual(9, match.Length);
            Assert.AreEqual(1, match.Strand);
            Assert.AreEqual("aagaacaag", match.Letters());
            Assert.AreEqual(seq, match.BaseSequence);
            Assert.AreEqual(0.888, match.Similarity, 1e-3);
            
            iteration = new Iteration("test",motif,1,1,0.0);
            match     = iteration.Match(seq, 4);
            Assert.AreEqual("aag", match.Letters());
            Assert.AreEqual(1.0, match.Similarity, 1e-3);
            
            iteration = new Iteration("test",motif,1,2,0.0);
            match     = iteration.Match(seq, 4);
            Assert.AreEqual("aagaac", match.Letters());
            Assert.AreEqual(0.833, match.Similarity, 1e-3);
              
            iteration = new Iteration("test",motif,4,5,0.0);
            Assert.AreEqual(null, iteration.Match(seq, 4));
        }
 public void Setup()
 {
     series = new SeriesBest("series", 0.0);
     motif = new Motif("motif", AlphabetType.DNA, "accg", 1.0);
     gap = new Gap("gap", 3, 3);
     gap.Impact = 0.0;
     series.Add(motif);
     series.Add(gap);
 }
		/** Tests the getter of patterns */
		public void TestGet () {
			ProfileProxy pf = new ProfileProxy();
			Motif pat1 = new Motif( "motif1", AlphabetType.DNA, "ac", 0 );
			Motif pat2 = new Motif( "motif2", AlphabetType.DNA, "ag", 0 );
			pf.Add( pat1 );
			pf.Add( pat2 );
			Assert.AreEqual( pat1, pf.Pattern( 0 ) );
			Assert.AreEqual( pat2, pf.Pattern( 1 ) );
		}
		public void TestAddGetPattern () {
			SetBest set = new SetBest();
			Assert.AreEqual( 0, set.Count );

			Motif motif = new Motif( "motif1", AlphabetType.DNA, "ACTG", 0.7 );
			set.Add( motif );
			Assert.AreEqual( 1, set.Count );
			Assert.AreEqual( motif, set[0] );
		}
        public void TestConstructor()
        {
            Motif motif = new Motif("motif", AlphabetType.DNA, "tgn", 0.0);
            Iteration iteration = new Iteration("test", motif, 3, 4, 0.0);

            Assert.AreEqual("test", iteration.Name);
            Assert.AreEqual(motif, iteration.Pattern);
            Assert.AreEqual(3, iteration.Minimum);
            Assert.AreEqual(4, iteration.Maximum);
        }
		public void MotifTestMatchAtStart () {
			Sequence seq = new Sequence( AlphabetType.DNA, "aTtgattaca" );
			Motif motif = new Motif( "test", AlphabetType.DNA, "attg", 0.0 );
			Match match = motif.Match( seq, 1 );

			Assert.AreEqual( 1, match.Start );
			Assert.AreEqual( 4, match.Length );
			Assert.AreEqual( 1, match.Strand );
			Assert.AreEqual( 1.0, match.Similarity, 1e-2 );
			Assert.AreEqual( "attg", match.Letters() );
		}
		public void MotifTestMatch () {
			Sequence seq = new Sequence( AlphabetType.DNA, "aTtga" );
			Motif motif = new Motif( "test", AlphabetType.DNA, "tgn", 0.0 );
			Match match = motif.Match( seq, 3 );

			Assert.AreEqual( 3, match.Start );
			Assert.AreEqual( 3, match.Length );
			Assert.AreEqual( 1, match.Strand );
			Assert.AreEqual( 1.0, match.Similarity, 1e-2 );

			match = motif.Match( seq, 2 );
			Assert.AreEqual( 2, match.Start );
			Assert.AreEqual( 3, match.Length );
			Assert.AreEqual( 1, match.Strand );
			Assert.AreEqual( 0.66, match.Similarity, 1e-2 );
		}
		/** Tests the match method of a sequence of patterns */
		public void TestMatch1 () {
			Sequence seq = new Sequence( AlphabetType.DNA, "baaacc" );
			ProfileAll pf = new ProfileAll();
			Motif motif;
			ProfileElement element;
			FeatureList matches;

			motif = new Motif( "motif1", AlphabetType.DNA, "aa", 0.5 );
			element = pf.Add( motif );
			matches = seq.Search( 1, seq.Length, pf );
			Assert.AreEqual( 4, matches.Count );
			Assert.AreEqual( "ba", matches[ 0 ].Letters() );
			Assert.AreEqual( 1, matches[ 0 ].Start );
			Assert.AreEqual( "aa", matches[ 1 ].Letters() );
			Assert.AreEqual( 2, matches[ 1 ].Start );
			Assert.AreEqual( "aa", matches[ 2 ].Letters() );
			Assert.AreEqual( 3, matches[ 2 ].Start );
			Assert.AreEqual( "ac", matches[ 3 ].Letters() );
			Assert.AreEqual( 4, matches[ 3 ].Start );

			motif = new Motif( "motif2", AlphabetType.DNA, "acc", 0.5 );
			element = pf.Add( element, ProfileElement.AlignmentType.END, 0, 1, motif );
			matches = seq.Search( 1, seq.Length, pf );
			Assert.AreEqual( 3, matches.Count );
			Assert.AreEqual( "baaac", matches[ 0 ].Letters() );
			Assert.AreEqual( 1, matches[ 0 ].Start );
			Assert.AreEqual( "baaacc", matches[ 1 ].Letters() );
			Assert.AreEqual( 1, matches[ 1 ].Start );
			Assert.AreEqual( "aaacc", matches[ 2 ].Letters() );
			Assert.AreEqual( 2, matches[ 2 ].Start );


			motif = new Motif( "motif3", AlphabetType.DNA, "cc", 1.0 );
			element = pf.Add( element, ProfileElement.AlignmentType.END, -2, 1, motif );
			matches = seq.Search( 1, seq.Length, pf );
			Assert.AreEqual( 3, matches.Count );
			Assert.AreEqual( "baaacc", matches[ 0 ].Letters() );
			Assert.AreEqual( 1, matches[ 0 ].Start );
			Assert.AreEqual( "baaacc", matches[ 1 ].Letters() );
			Assert.AreEqual( 1, matches[ 1 ].Start );
			Assert.AreEqual( "aaacc", matches[ 2 ].Letters() );
			Assert.AreEqual( 2, matches[ 2 ].Start );
		}
		/** Tests the adding of patterns to a profile */
		public void TestAdd () {
			IPattern pattern1, pattern2;
			ProfileElement element1, element2;
			ProfileProxy pf = new ProfileProxy();
			Assert.AreEqual( 0, pf.Count );

			pattern1 = new Motif( "motif1", AlphabetType.DNA, "ac", 0 );
			element1 = pf.Add( null, ProfileElement.AlignmentType.NONE, 0, 0, pattern1 );
			Assert.AreEqual( 1, pf.Count );
			Assert.AreEqual( pattern1, element1.Pattern );

			pattern2 = new Motif( "motif2", AlphabetType.DNA, "tg", 0 );
			element2 = pf.Add( element1, ProfileElement.AlignmentType.START, -2, -1, pattern2 );
			Assert.AreEqual( 2, pf.Count );
			Assert.AreEqual( ProfileElement.AlignmentType.START, element2.Alignment );
			Assert.AreEqual( -2, element2.MinGap );
			Assert.AreEqual( -1, element2.MaxGap );
			Assert.AreEqual( ProfileElement.AlignmentType.START, element2.Alignment );
			Assert.AreEqual( element1, element2.RefElement );
		}
		/** Tests the match method for a hierarchical pattern */
		public void TestMatch3 () {
			Sequence seq = new Sequence( AlphabetType.DNA, "baaacc" );
			ProfileAll pf1 = new ProfileAll();
			ProfileAll pf2 = new ProfileAll();
			Motif motif;
			ProfileElement element;
			FeatureList matches;

			motif = new Motif( "motif1", AlphabetType.DNA, "aa", 1.0 );
			element = pf1.Add( motif );
			motif = new Motif( "motif2", AlphabetType.DNA, "aa", 1.0 );
			element = pf1.Add( element, ProfileElement.AlignmentType.START, 0, 1, motif );
			matches = seq.Search( 1, seq.Length, pf1 );
			Assert.AreEqual( 3, matches.Count );
			Assert.AreEqual( "aa", matches[ 0 ].Letters() );
			Assert.AreEqual( "aaa", matches[ 1 ].Letters() );
			Assert.AreEqual( "aa", matches[ 2 ].Letters() );

			motif = new Motif( "motif3", AlphabetType.DNA, "aa", 1.0 );
			element = pf2.Add( motif );
			motif = new Motif( "motif4", AlphabetType.DNA, "cc", 1.0 );
			element = pf2.Add( element, ProfileElement.AlignmentType.CENTER, 0, 2, motif );
			matches = seq.Search( 1, seq.Length, pf2 );
			Assert.AreEqual( 2, matches.Count );
			Assert.AreEqual( "aaacc", matches[ 0 ].Letters() );
			Assert.AreEqual( "aacc", matches[ 1 ].Letters() );

			pf1.Add( pf1[0], ProfileElement.AlignmentType.END, -1, 0, pf2 );
			pf2.Threshold = ( 1.0 );
			matches = seq.Search( 1, seq.Length, pf1 );
			Assert.AreEqual( 2, matches.Count );
			Assert.AreEqual( "aaacc", matches[ 0 ].Letters() );
			Assert.AreEqual( "aaacc", matches[ 1 ].Letters() );
		}
		public void SetUp () {
			seq = new Sequence( AlphabetType.DNA, "atgcatgc" );
			series = new SeriesBest( "series", 1.0 );
			motif1 = new Motif( "motif1", AlphabetType.DNA, "tgc", 1.0 );
			motif2 = new Motif( "motif2", AlphabetType.DNA, "gca", 1.0 );
		}
		public void MotifTestMatchSimiliarity ()
        {
            Sequence seq = new Sequence(AlphabetType.DNA, "aCtG", true);

            Assert.AreEqual(2, (new Motif("test", AlphabetType.DNA, "ct", 0)).Match(seq, 1).Length);
            Assert.AreEqual(3, (new Motif("test", AlphabetType.DNA, "ct", 0)).Match(seq, 3).Start);
            Assert.AreEqual(0.0, (new Motif("test", AlphabetType.DNA, "ct", 0)).Match(seq, 1).Similarity);

            Assert.AreEqual(0.5, (new Motif("test", AlphabetType.DNA, "cc", 0)).Match(seq, 1).Similarity, 1e-3);
            Assert.AreEqual(1.0, (new Motif("test", AlphabetType.DNA, "ct", 0)).Match(seq, 2).Similarity, 1e-3);
            Assert.AreEqual(0.0, (new Motif("test", AlphabetType.DNA, "ct", 0)).Match(seq, 3).Similarity, 1e-3);

            Assert.AreEqual(1.0, (new Motif("test", AlphabetType.DNA, "ctga", 0)).Match(seq, 2).Similarity, 1e-3);

            Motif seq1 = new Motif("test", AlphabetType.DNA, "cc", 0.6);
            Assert.AreEqual(null, seq1.Match(seq, 1));
        }
        /// <summary>
        /// 
        /// Reads a pattern from a starting specified node. This method
        /// recursivly calls the reading methods of the different patterns.
        /// </summary>
        /// <param name="node">Node of the pattern the reading starts with.</param>
        /// <param name="definition">Pattern definition which pattern list will be extended 
        /// with the pattern and all its sub-patterns read.
        /// </param>
        /// <returns>The read pattern or null if there is no pattern to read.</returns>
        /// <exception cref="System.SystemException">Thrown when unknown pattern was found</exception>
        public static IPattern ReadPattern
                    (XmlNode node, Definition definition)
        {
            while (node != null
                    && node.NodeType != XmlNodeType.Element)

                node = node.NextSibling; //Iterate thru the list of nodes until it is an element

            IPattern pattern = null;
            String mode = XMLHelper.GetAttrValueString(node, "mode");

            switch (node.Name)
            {
                case "Any":
                    pattern = new Any();
                    break;

                case "Alignment":
                    pattern = new Alignment();
                    break;

                case "Composition" :
                    pattern = new Composition();
                    break;

                case "Constraint":
                    pattern = new Constraint();
                    break;

                case "Iteration":
                    pattern = new Iteration();
                    break;

                case "Logic":
                    pattern = new Logic();
                    break;

                case "Motif":
                    pattern = new Motif();
                    break;

                case "PWM":
                    pattern = new PWM();
                    break;

                case "Regex":
                    pattern = new RegularExp();
                    break;

                case "Prosite":
                    pattern = new Prosite();
                    break;

                case "Block":
                    pattern = new Block();
                    break;

                case "Gap":
                    pattern = new Gap();
                    break;

                case "Repeat":
                    pattern = new Repeat();
                    break;

                case "Series":
                    pattern = mode.Equals("ALL") ? 
                        pattern = new SeriesAll() : pattern = new SeriesBest();
                    break;

                case "Set":
                    pattern = mode.Equals("ALL") ?
                       pattern = new SetAll() : pattern = new SetBest();
                    break;

                case "Void":
                    pattern = new VoidPattern();
                    break;

                case "Use":
                    pattern = new Use();
                    break;

                throw new SystemException
                    ("Unknown pattern found: " + node.Name);
 
            }

 
            pattern.ReadNode(node, definition);      // read the node data and initialize the pattern
            definition.Patterns.Add
                            (0, pattern); //Always adding the element to last index
 
            return pattern;
        }
		public void TestSearch () {
			Sequence seq   = new Sequence( AlphabetType.DNA, "acttacttagttaac" );
			Motif  motif = new Motif( "motif1", AlphabetType.DNA, "act", 0.5 );

			FeatureList list = seq.Search( 0, 0, motif );
			Assert.AreEqual( 3, list.Count() );
			Assert.AreEqual( 1, list[ 0 ].Start );
			Assert.AreEqual( 1.0, ( (Match) list[ 0 ] ).Similarity, 1e-1 );
			Assert.AreEqual( 5, list[ 1 ].Start );
			Assert.AreEqual( 1.0, ( (Match) list[ 1 ] ).Similarity, 1e-1 );
			Assert.AreEqual( 9, list[ 2 ].Start );
			Assert.AreEqual( 0.6, ( (Match) list[ 2 ] ).Similarity, 1e-1 );

			list = seq.Search( 11, seq.Length + 1, motif );
			Assert.AreEqual( 1, list.Count() );
			Assert.AreEqual( 14, list[ 0 ].Start );
			Assert.AreEqual( 0.6, ( (Match) list[ 0 ] ).Similarity, 1e-1 );


		}
		/** Test for best match searching within a sequence */
		public void TestSearchBest () {
			Sequence seq = new Sequence( AlphabetType.DNA, "acttacttagttaatt" );
			Motif motif = new Motif( "motif1", AlphabetType.DNA, "taga", 0.0 );
			Match match = seq.SearchBest( 1, -1, motif );
			Assert.AreEqual( "tagt", match.Letters() );
			Assert.AreEqual( 8, match.Start );
			Assert.AreEqual( 4, match.Length );
			Assert.AreEqual( 0.75, match.Similarity, 1e-3 );
			motif = new Motif( "motif2", AlphabetType.DNA, "aatt", 0.0 );
			match = seq.SearchBest( 1, -1, motif );
			Assert.AreEqual( 13, match.Start );
			match = seq.SearchBest( 1, seq.Length, motif );
			Assert.AreEqual( 13, match.Start );
			match = seq.SearchBest( 1, seq.Length - 1, motif );
			Assert.AreEqual( 1, match.Start );
		}
		/** Tests the match method for a pattern which matches all the time */
		public void TestMatch5 () {
			Sequence seq = new Sequence( AlphabetType.DNA, "bacgt" );
			ProfileAll pf = new ProfileAll();
			Motif motif;
			FeatureList matches;

			motif = new Motif( "motif1", AlphabetType.DNA, "aa", 0.0 );
			pf.Add( motif );

			matches = seq.Search( 1, seq.Length, pf );
			Assert.AreEqual( 4, matches.Count );
			Assert.AreEqual( "ba", matches[ 0 ].Letters() );
			Assert.AreEqual( 1, matches[ 0 ].Start );
			Assert.AreEqual( "ac", matches[ 1 ].Letters() );
			Assert.AreEqual( 2, matches[ 1 ].Start );
		}
        public void TestMatchSubMatches()
        {
            Sequence seq = new Sequence(AlphabetType.DNA, "tttaagaacaagttt");

            Motif motif = new Motif("motif", AlphabetType.DNA, "aag", 0.5);
            Iteration iteration = new Iteration("test", motif, 1, 3, 0.0);
            Match match = seq.SearchBest(0, 0, iteration);

            Assert.AreEqual("aag", match.Letters());
            Assert.AreEqual(10, match.Start);
            Assert.AreEqual(1.0, match.Similarity);
            Assert.AreEqual("aag", match.SubMatches[0].Letters()); //sub match to properties?

            FeatureList matches = seq.Search(0, 0, iteration);
            Assert.AreEqual(3, matches.Count);
            Assert.AreEqual("aagaacaag", matches[0].Letters());
            Assert.AreEqual(0.88, ((Match)matches[0]).Similarity, 1e-2);
            Assert.AreEqual("aacaag", matches[1].Letters());
            Assert.AreEqual(0.83, ((Match)matches[1]).Similarity, 1e-2);
            Assert.AreEqual("aag", matches[2].Letters());
            Assert.AreEqual(1.00, ((Match)matches[2]).Similarity, 1e-2);
        }
		public void MotifTestLetters ()
        {
            Motif motif = new Motif("test", AlphabetType.DNA, "tgn[at]C[TG]", 0.0);
            Assert.AreEqual("tg[atcg][at]c[tg]", motif.Letters);
        }