Esempio n. 1
0
        static void Main(string[] args)
        {
            char[] seq1 = null;
            char[] seq2 = null;

            try
            {
                using (StreamReader sr = new StreamReader("sequence1.txt"))
                {
                    String line = sr.ReadToEnd();
                    seq1 = line.ToCharArray();
                }
                using (StreamReader sr = new StreamReader("sequence2.txt"))
                {
                    String line = sr.ReadToEnd();
                    seq2 = line.ToCharArray();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(e.Message);
            }

            //sample sequences
            //char[] s11 = { 'G', 'C', 'A', 'T', 'G','C','U' };
            //char[] s22 = { 'G', 'A', 'T', 'T', 'A','C','A' };

            char[] s1 = new char[seq1.Length + 1];
            char[] s2 = new char[seq2.Length + 1];
            s1[0] = ' ';
            s2[0] = ' ';
            seq1.CopyTo(s1, 1);
            seq2.CopyTo(s2, 1);


            Console.WriteLine("Needleman-Wunsch algorithm with gap penalty");
            AlignmentWithPenalty withPenalty = new AlignmentWithPenalty();

            int[,] arr = withPenalty.GetSimilarityMatrix(s1, s2);
            withPenalty.GetBacktrace(arr, s1, s2);

            Console.WriteLine("\nHirschberg algorithm without gap penalty");
            Hirschberg hirschberg = new Hirschberg();

            hirschberg.GetAlignment(seq1, seq2);
        }
Esempio n. 2
0
        public void GetAlignment(char[] s1, char[] s2)
        {
            StringBuilder Z = new StringBuilder();
            StringBuilder W = new StringBuilder();

            if (s1.Length == 0)
            {
                for (int i = 1; i < s2.Length - 1; i++)
                {
                    Z.Append("-");
                    W.Append(s2[i]);
                }
            }
            else if (s2.Length == 0)
            {
                for (int i = 1; i < s2.Length - 1; i++)
                {
                    Z.Append(s1[i]);
                    W.Append("-");
                }
            }
            else if (s1.Length == 1 || s2.Length == 1)
            {
                AlignmentWithPenalty withPenalty = new AlignmentWithPenalty();
                int[,] arr = withPenalty.GetSimilarityMatrix(s1, s2);
                withPenalty.GetBacktrace(arr, s1, s2);
            }
            else
            {
                int xlen = s1.Length;
                int ylen = s2.Length;
                int xmid = xlen / 2;

                int[] prefScore = GetNWScore(s1, s2, xmid, "pref");
                int[] suffScore = GetNWScore(s1, s2, xmid, "suff");
                int[] score     = new int[prefScore.Length];

                for (int i = 0; i < s2.Length; i++)
                {
                    score[i] = prefScore[i] + suffScore[i];
                }

                int max  = score.Max();
                int ymid = Array.IndexOf(score, max);

                char[] pref_s1 = new char[xmid];
                char[] suff_s1 = new char[s1.Length - xmid];
                char[] pref_s2 = new char[ymid];
                char[] suff_s2 = new char[s2.Length - ymid];

                Array.Copy(s1, 0, pref_s1, 0, xmid);
                Array.Copy(s2, 0, pref_s2, 0, ymid);
                Array.Copy(s1, xmid, suff_s1, 0, s1.Length - xmid);
                Array.Copy(s1, ymid, suff_s2, 0, s2.Length - ymid);

                GetAlignment(pref_s1, pref_s2);
                if (ymid != 0)
                {
                    GetAlignment(suff_s1, suff_s2);
                }
            }
        }