Пример #1
0
        public void SemiGlobalAlign()
        {
            var scorer = new StringScorer();
            var msa    = new PairwiseAlignmentAlgorithm <string, char>(scorer, "car", "bar", GetChars)
            {
                Mode = AlignmentMode.SemiGlobal
            };

            msa.Compute();
            Alignment <string, char>[] alignments = msa.GetAlignments().ToArray();

            Assert.That(alignments.Length, Is.EqualTo(1));
            AssertAlignmentsEqual(alignments[0], CreateAlignment(
                                      "| c a r |",
                                      "| b a r |"
                                      ));
            Assert.That(alignments[0].NormalizedScore, Is.EqualTo(0.66).Within(0.01));

            msa = new PairwiseAlignmentAlgorithm <string, char>(scorer, "cart", "bar", GetChars)
            {
                Mode = AlignmentMode.SemiGlobal
            };
            msa.Compute();
            alignments = msa.GetAlignments().ToArray();

            Assert.That(alignments.Length, Is.EqualTo(1));
            AssertAlignmentsEqual(alignments[0], CreateAlignment(
                                      "| c a r | t",
                                      "| b a r |  "
                                      ));
            Assert.That(alignments[0].NormalizedScore, Is.EqualTo(0.57).Within(0.01));

            msa = new PairwiseAlignmentAlgorithm <string, char>(scorer, "cart", "art", GetChars)
            {
                Mode = AlignmentMode.SemiGlobal
            };
            msa.Compute();
            alignments = msa.GetAlignments().ToArray();

            Assert.That(alignments.Length, Is.EqualTo(1));
            AssertAlignmentsEqual(alignments[0], CreateAlignment(
                                      "c | a r t |",
                                      "  | a r t |"
                                      ));
            Assert.That(alignments[0].NormalizedScore, Is.EqualTo(0.86).Within(0.01));

            msa = new PairwiseAlignmentAlgorithm <string, char>(scorer, "start", "tan", GetChars)
            {
                Mode = AlignmentMode.SemiGlobal
            };
            msa.Compute();
            alignments = msa.GetAlignments().ToArray();

            Assert.That(alignments.Length, Is.EqualTo(1));
            AssertAlignmentsEqual(alignments[0], CreateAlignment(
                                      "s | t a r | t",
                                      "  | t a n |  "
                                      ));
            Assert.That(alignments[0].NormalizedScore, Is.EqualTo(0.50).Within(0.01));
        }
Пример #2
0
        public void GlobalAlign()
        {
            var scorer = new StringScorer();
            var msa    = new PairwiseAlignmentAlgorithm <string, char>(scorer, "car", "bar", GetChars);

            msa.Compute();
            Alignment <string, char>[] alignments = msa.GetAlignments().ToArray();

            Assert.That(alignments.Length, Is.EqualTo(1));
            AssertAlignmentsEqual(alignments[0], CreateAlignment(
                                      "| c a r |",
                                      "| b a r |"
                                      ));
            Assert.That(alignments[0].NormalizedScore, Is.EqualTo(0.66).Within(0.01));

            msa = new PairwiseAlignmentAlgorithm <string, char>(scorer, "cart", "bar", GetChars);
            msa.Compute();
            alignments = msa.GetAlignments().ToArray();

            Assert.That(alignments.Length, Is.EqualTo(1));
            AssertAlignmentsEqual(alignments[0], CreateAlignment(
                                      "| c a r t |",
                                      "| b a r - |"
                                      ));
            Assert.That(alignments[0].NormalizedScore, Is.EqualTo(0.25).Within(0.01));

            msa = new PairwiseAlignmentAlgorithm <string, char>(scorer, "cart", "art", GetChars);
            msa.Compute();
            alignments = msa.GetAlignments().ToArray();

            Assert.That(alignments.Length, Is.EqualTo(1));
            AssertAlignmentsEqual(alignments[0], CreateAlignment(
                                      "| c a r t |",
                                      "| - a r t |"
                                      ));
            Assert.That(alignments[0].NormalizedScore, Is.EqualTo(0.50).Within(0.01));

            msa = new PairwiseAlignmentAlgorithm <string, char>(scorer, "start", "tan", GetChars);
            msa.Compute();
            alignments = msa.GetAlignments().ToArray();

            Assert.That(alignments.Length, Is.EqualTo(2));
            AssertAlignmentsEqual(alignments[0], CreateAlignment(
                                      "| s t a r t |",
                                      "| - t a - n |"
                                      ));
            Assert.That(alignments[0].NormalizedScore, Is.EqualTo(0.00).Within(0.01));
            AssertAlignmentsEqual(alignments[1], CreateAlignment(
                                      "| s t a r t |",
                                      "| - t a n - |"
                                      ));
            Assert.That(alignments[1].NormalizedScore, Is.EqualTo(0.00).Within(0.01));
        }
Пример #3
0
        public void GlobalAlign_EmptySequence()
        {
            var scorer = new StringScorer();
            var msa    = new PairwiseAlignmentAlgorithm <string, char>(scorer, "", "", GetChars);

            msa.Compute();
            Alignment <string, char>[] alignments = msa.GetAlignments().ToArray();

            Assert.That(alignments.Length, Is.EqualTo(1));
            AssertAlignmentsEqual(alignments[0], CreateAlignment(
                                      "||",
                                      "||"
                                      ));
            Assert.That(alignments[0].NormalizedScore, Is.EqualTo(0));
        }
Пример #4
0
        public void ZeroMaxScore()
        {
            var scorer = new ZeroMaxScoreStringScorer();
            var msa    = new PairwiseAlignmentAlgorithm <string, char>(scorer, "car", "bar", GetChars)
            {
                ExpansionCompressionEnabled = true
            };

            msa.Compute();
            Alignment <string, char>[] alignments = msa.GetAlignments().ToArray();

            Assert.That(alignments.Length, Is.EqualTo(1));
            AssertAlignmentsEqual(alignments[0], CreateAlignment(
                                      "| c a r |",
                                      "| b a r |"
                                      ));
            Assert.That(alignments[0].NormalizedScore, Is.EqualTo(0));
        }
Пример #5
0
        public void Compute()
        {
            var clusterer = new NeighborJoiningClusterer <TSeq>((seq1, seq2) =>
            {
                var pairwiseAligner = new PairwiseAlignmentAlgorithm <TSeq, TItem>(_scorer, seq1, seq2, _itemsSelector);
                pairwiseAligner.Compute();
                return(1.0 - pairwiseAligner.GetAlignments().First().NormalizedScore);
            });
            IUndirectedGraph <Cluster <TSeq>, ClusterEdge <TSeq> >    unrootedTree = clusterer.GenerateClusters(_sequences);
            IBidirectionalGraph <Cluster <TSeq>, ClusterEdge <TSeq> > rootedTree   = unrootedTree.ToRootedTree();

            var            profiles  = new Dictionary <Cluster <TSeq>, Profile <TSeq, TItem> >();
            var            nodeStack = new Stack <Cluster <TSeq> >();
            Cluster <TSeq> root      = rootedTree.Roots().First();
            double         maxWeight = double.MinValue;

            if (root.DataObjects.Count == 1)
            {
                profiles[root] = CreateProfile(root.DataObjects.First(), 0);
                maxWeight      = 0;
            }
            nodeStack.Push(root);
            foreach (ClusterEdge <TSeq> edge in rootedTree.OutEdges(root))
            {
                maxWeight = Math.Max(maxWeight, CalcSequenceWeights(rootedTree, edge, 0, nodeStack, profiles));
            }

            foreach (Profile <TSeq, TItem> profile in profiles.Values)
            {
                profile.Weights[0] += 1.0 - maxWeight;
            }

            var scorer = new ProfileScorer <TSeq, TItem>(_scorer);

            while (nodeStack.Count > 0)
            {
                Cluster <TSeq> node = nodeStack.Pop();

                var curProfiles = new Stack <Profile <TSeq, TItem> >();
                foreach (ClusterEdge <TSeq> childEdge in rootedTree.OutEdges(node))
                {
                    curProfiles.Push(profiles[childEdge.Target]);
                    profiles.Remove(childEdge.Target);
                }
                if (node.DataObjects.Count == 1)
                {
                    curProfiles.Push(profiles[node]);
                    profiles.Remove(node);
                }
                while (curProfiles.Count > 1)
                {
                    Profile <TSeq, TItem> profile1 = curProfiles.Pop();
                    Profile <TSeq, TItem> profile2 = curProfiles.Pop();
                    var profileAligner             = new PairwiseAlignmentAlgorithm <Profile <TSeq, TItem>, AlignmentCell <TItem>[]>(scorer, profile1, profile2, GetProfileItems);
                    profileAligner.Compute();
                    Alignment <Profile <TSeq, TItem>, AlignmentCell <TItem>[]> profileAlignment = profileAligner.GetAlignments().First();
                    var sequences = new List <Tuple <TSeq, AlignmentCell <TItem>, IEnumerable <AlignmentCell <TItem> >, AlignmentCell <TItem> > >();
                    for (int i = 0; i < profile1.Alignment.SequenceCount; i++)
                    {
                        int seq = i;
                        sequences.Add(Tuple.Create(profile1.Alignment.Sequences[i], profile1.Alignment.Prefixes[i], Enumerable.Range(0, profileAlignment.ColumnCount)
                                                   .Select(col => profileAlignment[0, col].IsNull ? new AlignmentCell <TItem>() : profileAlignment[0, col][0][seq]), profile1.Alignment.Suffixes[i]));
                    }
                    for (int j = 0; j < profile2.Alignment.SequenceCount; j++)
                    {
                        int seq = j;
                        sequences.Add(Tuple.Create(profile2.Alignment.Sequences[j], profile2.Alignment.Prefixes[j], Enumerable.Range(0, profileAlignment.ColumnCount)
                                                   .Select(col => profileAlignment[1, col].IsNull ? new AlignmentCell <TItem>() : profileAlignment[1, col][0][seq]), profile2.Alignment.Suffixes[j]));
                    }
                    var newAlignment = new Alignment <TSeq, TItem>(profileAlignment.RawScore, profileAlignment.NormalizedScore, sequences);
                    curProfiles.Push(new Profile <TSeq, TItem>(newAlignment, profile1.Weights.Concat(profile2.Weights)));
                }
                profiles[node] = curProfiles.Pop();
            }

            Alignment <TSeq, TItem> alignment = profiles[root].Alignment;

            if (UseInputOrder)
            {
                var reorderedSequences = new List <Tuple <TSeq, AlignmentCell <TItem>, IEnumerable <AlignmentCell <TItem> >, AlignmentCell <TItem> > >();
                foreach (TSeq sequence in _sequences)
                {
                    for (int i = 0; i < alignment.SequenceCount; i++)
                    {
                        int seq = i;
                        if (sequence.Equals(alignment.Sequences[seq]))
                        {
                            reorderedSequences.Add(Tuple.Create(sequence, alignment.Prefixes[seq], Enumerable.Range(0, alignment.ColumnCount).Select(col => alignment[seq, col]), alignment.Suffixes[seq]));
                            break;
                        }
                    }
                }

                _result = new Alignment <TSeq, TItem>(alignment.RawScore, alignment.NormalizedScore, reorderedSequences);
            }
            else
            {
                _result = alignment;
            }
        }
Пример #6
0
        public void ExpansionCompressionAlign()
        {
            var scorer = new StringScorer();
            var msa    = new PairwiseAlignmentAlgorithm <string, char>(scorer, "car", "bar", GetChars)
            {
                ExpansionCompressionEnabled = true
            };

            msa.Compute();
            Alignment <string, char>[] alignments = msa.GetAlignments().ToArray();

            Assert.That(alignments.Length, Is.EqualTo(1));
            AssertAlignmentsEqual(alignments[0], CreateAlignment(
                                      "| c a r |",
                                      "| b a r |"
                                      ));
            Assert.That(alignments[0].NormalizedScore, Is.EqualTo(0.66).Within(0.01));

            msa = new PairwiseAlignmentAlgorithm <string, char>(scorer, "cart", "bar", GetChars)
            {
                ExpansionCompressionEnabled = true
            };
            msa.Compute();
            alignments = msa.GetAlignments().ToArray();

            Assert.That(alignments.Length, Is.EqualTo(1));
            AssertAlignmentsEqual(alignments[0], CreateAlignment(
                                      "| c a rt |",
                                      "| b a r  |"
                                      ));
            Assert.That(alignments[0].NormalizedScore, Is.EqualTo(0.50).Within(0.01));

            msa = new PairwiseAlignmentAlgorithm <string, char>(scorer, "cart", "art", GetChars)
            {
                ExpansionCompressionEnabled = true
            };
            msa.Compute();
            alignments = msa.GetAlignments().ToArray();

            Assert.That(alignments.Length, Is.EqualTo(1));
            AssertAlignmentsEqual(alignments[0], CreateAlignment(
                                      "| ca r t |",
                                      "| a  r t |"
                                      ));
            Assert.That(alignments[0].NormalizedScore, Is.EqualTo(0.75).Within(0.01));

            msa = new PairwiseAlignmentAlgorithm <string, char>(scorer, "start", "tan", GetChars)
            {
                ExpansionCompressionEnabled = true
            };
            msa.Compute();
            alignments = msa.GetAlignments().ToArray();

            Assert.That(alignments.Length, Is.EqualTo(2));
            AssertAlignmentsEqual(alignments[0], CreateAlignment(
                                      "| st ar t |",
                                      "| t  a  n |"
                                      ));
            Assert.That(alignments[0].NormalizedScore, Is.EqualTo(0.40).Within(0.01));
            AssertAlignmentsEqual(alignments[1], CreateAlignment(
                                      "| st a rt |",
                                      "| t  a n  |"
                                      ));
            Assert.That(alignments[1].NormalizedScore, Is.EqualTo(0.40).Within(0.01));
        }