Basic sequence class. Stores alphabet and building.
Наследование: AbstractChain
        /// <summary>
        /// Reorganizes source chain.
        /// </summary>
        /// <param name="source">
        /// Source chain.
        /// </param>
        /// <returns>
        /// <see cref="AbstractChain"/>.
        /// </returns>
        /// <exception cref="InvalidOperationException">
        /// Thrown if level is less than 0.
        /// </exception>
        public override AbstractChain Reorganize(AbstractChain source)
        {
            if (level < 0)
            {
                throw new InvalidOperationException("Markov chain level can't be less than 0");
            }

            if (level == 0)
            {
                return source;
            }

            var result = new BaseChain();
            result.ClearAndSetNewLength(source.GetLength() + level);
            for (int i = 0; i < source.GetLength(); i++)
            {
                result[i] = source[i];
            }

            var iterator = new IteratorStart(source, level, 1);
            iterator.Reset();
            iterator.Next();
            AbstractChain addition = iterator.Current();
            for (int i = 0; i < addition.GetLength(); i++)
            {
                result[source.GetLength() + i] = addition[i];
            }

            return result;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="TestObject"/> class.
        /// </summary>
        public TestObject()
        {
            chain = new BaseChain(10);
            chain2 = new BaseChain(5);

            alpha.Add((ValueString)'a');
            alpha.Add((ValueString)'b');
            alpha.Add((ValueString)'c');
            pm1 = new ValuePhantom { alpha[2], alpha[1] };
            pm2 = new ValuePhantom { alpha[0] };

            chain.Set(PhantomMessageBc, 0);
            chain.Set(PhantomMessageA, 1);
            chain.Set(PhantomMessageA, 2);
            chain.Set(PhantomMessageBc, 3);
            chain.Set(PhantomMessageA, 4);
            chain.Set(PhantomMessageBc, 5);
            chain.Set(PhantomMessageA, 6);
            chain.Set(PhantomMessageBc, 7);
            chain.Set(PhantomMessageA, 8);
            chain.Set(PhantomMessageA, 9);

            chain2.Set(alpha[1], 0);
            chain2.Set(PhantomMessageA, 1);
            chain2.Set(PhantomMessageBc, 2);
            chain2.Set(alpha[0], 3);
            chain2.Set(PhantomMessageBc, 4);
        }
        public void FourthTest()
        {
            var resultChain = new BaseChain(10);
            var iterator = new IteratorWritableStart(resultChain);
            iterator.Reset();
            while (iterator.Next())
            {
                iterator.WriteValue(mother.PhantomMessageBc);
            }

            var gen = new PhantomChainGenerator(resultChain, new SimpleGenerator());
            List<BaseChain> res = gen.Generate(1000);
            int counter = 0;
            for (int i = 0; i < 999; i++)
            {
                for (int j = i + 1; j < 1000; j++)
                {
                    if (res[i].Equals(res[j]))
                    {
                        counter++;
                    }
                }
            }

            Assert.AreEqual(0, counter);
        }
        /// <summary>
        /// Create data sequence and matter.
        /// </summary>
        /// <param name="sequence">
        /// The common sequence.
        /// </param>
        /// <param name="sequenceStream">
        /// The sequence stream.
        /// </param>
        /// <param name="precision">
        /// Precision of data sequence.
        /// </param>
        public void Create(CommonSequence sequence, Stream sequenceStream, int precision)
        {
            var stringSequence = FileHelper.ReadSequenceFromStream(sequenceStream);

            string[] text = stringSequence.Split(new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);

            var cleanedSequence = text.Where(t => !t.Equals("\"volume\"") && !string.IsNullOrEmpty(t) && !string.IsNullOrWhiteSpace(t)).ToList();

            var elements = new List<IBaseObject>();

            for (int i = 0; i < cleanedSequence.Count; i++)
            {
                var element = cleanedSequence[i];
                if (element.Substring(element.Length - 2, 2).Equals(".0"))
                {
                    cleanedSequence[i] = cleanedSequence[i].Substring(0, cleanedSequence[i].Length - 2);
                }

                int intElement = int.Parse(cleanedSequence[i]);
                int multiplier = (int)Math.Pow(10, precision);
                intElement /= multiplier;
                intElement *= multiplier;

                elements.Add(new ValueInt(intElement));
            }

            var chain = new BaseChain(elements);

            MatterRepository.CreateMatterFromSequence(sequence);

            var alphabet = ElementRepository.ToDbElements(chain.Alphabet, sequence.NotationId, true);
            Create(sequence, alphabet, chain.Building);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="PhantomTable"/> class.
        /// </summary>
        /// <param name="source">
        /// Phantom chain.
        /// </param>
        public PhantomTable(BaseChain source)
        {
            BaseChain internalChain = source;
            ulong v = 1;
            StartPositions.Add(0);
            for (int j = 0; j < internalChain.GetLength(); j++)
            {
                if ((((ValuePhantom)internalChain[j])[0] is ValueString)
                    || (((ValuePhantom)internalChain[j])[0] is BaseChain))
                {
                    StartPositions.Add(StartPositions[j] + ((ValuePhantom)internalChain[j])[0].ToString().Length);
                }
                else
                {
                    StartPositions.Add(StartPositions[j] + 1);
                }

                table.Add(null);
            }

            table.Add(null);
            for (int i = internalChain.GetLength(); i > 0; i--)
            {
                var temp = (ValuePhantom)internalChain[i - 1];
                table[i] = new Record(temp, v);
                v *= (uint)temp.Cardinality;
            }

            // tree root is associated with phantom message
            var t = new ValuePhantom { NullValue.Instance() };
            table[0] = new Record(t, v);
        }
        /// <summary>
        /// Reorganizes <see cref="AbstractChain"/> into <see cref="AbstractChain"/>.
        /// </summary>
        /// <param name="source">
        /// Source chain.
        /// </param>
        /// <returns>
        /// The <see cref="AbstractChain"/>.
        /// </returns>
        public override AbstractChain Reorganize(AbstractChain source)
        {
            var result = source.Clone() as AbstractChain;
            if (result != null)
            {
                return result;
            }

            result = new BaseChain();
            result.ClearAndSetNewLength(source.GetLength());

            var iteratorRead = new IteratorSimpleStart(source, 1);
            var iteratorWrite = new IteratorWritableStart(result);
            iteratorRead.Reset();
            iteratorWrite.Reset();
            iteratorRead.Next();
            iteratorWrite.Next();

            for (int i = 0; i < source.GetLength(); i++)
            {
                iteratorWrite.WriteValue(iteratorRead.Current());
                iteratorRead.Next();
                iteratorWrite.Next();
            }

            return result;
        }
 public void ToStringTest()
 {
     var stringExpected = "abcabccc";
     var chain = new Chain(stringExpected);
     Assert.AreEqual(stringExpected, chain.ToString());
     var baseChain = new BaseChain(stringExpected);
     Assert.AreEqual(stringExpected, baseChain.ToString());
 }
        public void CloneTest()
        {
            chain = new BaseChain("123456789A");

            var itsClone = (BaseChain)chain.Clone();
            Assert.AreEqual(chain, itsClone);
            Assert.AreNotSame(chain, itsClone);
        }
        /// <summary>
        /// Addes chain to alphabet.
        /// </summary>
        /// <param name="item">
        /// The item.
        /// </param>
        /// <returns>
        /// <see cref="int"/>.
        /// </returns>
        public override int Add(IBaseObject item)
        {
            var temp = item as BaseChain;
            if (temp == null)
            {
                temp = new BaseChain(1);
                temp.Set(item, 0);
            }

            return base.Add(temp);
        }
        /// <summary>
        /// Reorganizes <see cref="AbstractChain"/> into <see cref="AbstractChain"/>.
        /// </summary>
        /// <param name="source">
        /// Source chain.
        /// </param>
        /// <returns>
        /// The <see cref="AbstractChain"/>.
        /// </returns>
        public override AbstractChain Reorganize(AbstractChain source)
        {
            var resent = new BaseChain();
            resent.ClearAndSetNewLength(source.GetLength());
            for (int i = 0; i < source.GetLength(); i++)
            {
                var phantom = source[i] as ValuePhantom;
                resent.Set(phantom != null ? phantom[0] : source[i], i);
            }

            return resent;
        }
        /// <summary>
        /// The teach.
        /// </summary>
        /// <param name="chain">
        /// The chain.
        /// </param>
        /// <param name="method">
        /// The method.
        /// </param>
        public override void Teach(BaseChain chain, TeachingMethod method)
        {
            var builder = new MatrixBuilder();
            var absoluteMatrix = (IAbsoluteMatrix)builder.Create(chain.Alphabet.Cardinality, Rank);
            for (int i = 0; i < chain.Alphabet.Cardinality; i++)
            {
                int[] temp = new int[1];
                temp[0] = chain.Alphabet.IndexOf(chain.Alphabet[i]);

                absoluteMatrix.Add(temp);
            }

            ProbabilityMatrixes[0] = absoluteMatrix.ProbabilityMatrix();
        }
        /// <summary>
        /// Generates sequence.
        /// </summary>
        /// <param name="length">
        /// Length of generated sequence.
        /// </param>
        /// <param name="chainRank">
        /// Rank of used markov chain.
        /// </param>
        /// <returns>
        /// Generated sequence as <see cref="BaseChain"/>.
        /// </returns>
        public override BaseChain Generate(int length, int chainRank)
        {
            var temp = new BaseChain();
            temp.ClearAndSetNewLength(length);
            var read = Rank > 1 ? new IteratorStart(temp, Rank - 1, 1) : null;
            var write = new IteratorWritableStart(temp);
            if (read != null)
            {
                read.Reset();
                read.Next();
            }

            write.Reset();
            Generator.Reset();

            int m = 0;
            for (int j = 0; j < length; j++)
            {
                if (m == HeterogeneityRank + 1)
                {
                    m = 0;
                }

                m += 1;

                write.Next();

                if (j >= Rank)
                {
                    if (read != null)
                    {
                        read.Next();
                    }
                }

                if (read != null)
                {
                    BaseChain chain = (BaseChain)read.Current();
                    var indexedChain = new int[chain.GetLength()];
                    for (int k = 0; k < chain.GetLength(); k++)
                    {
                        indexedChain[k] = Alphabet.IndexOf(chain[k]);
                    }

                    write.WriteValue(GetObject(ProbabilityMatrixes[m - 1].GetProbabilityVector(Alphabet, indexedChain)));
                }
            }

            return temp;
        }
 public void Initialize()
 {
     chainBase = new BaseChain(10);
     chainBase[0] = new ValueString('1');
     chainBase[1] = new ValueString('2');
     chainBase[2] = new ValueString('3');
     chainBase[3] = new ValueString('4');
     chainBase[4] = new ValueString('5');
     chainBase[5] = new ValueString('6');
     chainBase[6] = new ValueString('7');
     chainBase[7] = new ValueString('8');
     chainBase[8] = new ValueString('9');
     chainBase[9] = new ValueString('A');
 }
        public void ToStringDelimiterTest()
        {
            var source = "abcabccc";
            var chain = new Chain(source);
            var baseChain = new BaseChain(source);

            var expected = "a b c a b c c c";
            Assert.AreEqual(expected, chain.ToString(" "));
            Assert.AreEqual(expected, baseChain.ToString(" "));

            expected = "acbcccacbcccccc";
            Assert.AreEqual(expected, chain.ToString("c"));
            Assert.AreEqual(expected, baseChain.ToString("c"));
        }
        public void ToStringLongDelimiterTest()
        {
            var source = "abcabccc";
            var chain = new Chain(source);
            var baseChain = new BaseChain(source);

            var expected = "a - b - c - a - b - c - c - c";
            Assert.AreEqual(expected, chain.ToString(" - "));
            Assert.AreEqual(expected, baseChain.ToString(" - "));

            expected = "a, b, c, a, b, c, c, c";
            Assert.AreEqual(expected, chain.ToString(", "));
            Assert.AreEqual(expected, baseChain.ToString(", "));
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="PhantomChainGenerator"/> class.
        /// </summary>
        /// <param name="chain">
        /// The chain.
        /// </param>
        /// <param name="generator">
        /// The gen.
        /// </param>
        public PhantomChainGenerator(BaseChain chain, IGenerator generator)
        {
            this.generator = generator;
            var reorganizer = new SpacePhantomReorganizer();
            var internalChain = (BaseChain)reorganizer.Reorganize(chain);
            for (int w = 0; w < internalChain.GetLength(); w++)
            {
                totalLength += ((ValuePhantom)internalChain[w])[0].ToString().Length;
            }

            ulong tempVariants = 1;
            int counter = 0;
            for (int k = 0; k < (int)Math.Ceiling((double)internalChain.GetLength() / BasicChainLength); k++)
            {
                tempChains.Add(new BaseChain());
                tempChains[k].ClearAndSetNewLength(BasicChainLength);
                tree.Add(null);
            }

            // variants count calculation cycle
            for (int i = 0; i < tempChains.Count; i++)
            {
                for (int j = 0; j < tempChains[i].GetLength(); j++)
                {
                    ValuePhantom tempMessage;
                    if (counter < internalChain.GetLength())
                    {
                        tempMessage = (ValuePhantom)internalChain[counter];
                        tempChains[i][j] = tempMessage;
                    }
                    else
                    {
                        tempMessage = new ValuePhantom { new ValueString('a') };
                        tempChains[i][j] = tempMessage;
                    }

                    tempVariants *= (uint)tempMessage.Cardinality;
                    counter++;
                }

                if ((i != tempChains.Count - 1) || (tempChains.Count == 1))
                {
                    Variants = Math.Min(Variants, tempVariants);
                }

                tempVariants = 1;
                tree[i] = new TreeTop(tempChains[i], generator);
            }
        }
        public void ContentTest()
        {
            var m1 = new ValuePhantom { new ValueString('1'), new ValueString('2'), new ValueString('3') };
            var m2 = new ValuePhantom { new ValueString('4'), new ValueString('3') };

            var test = new BaseChain(3);
            test.Set(m1, 0);
            test.Set(m2, 1);
            test.Set(m2, 2);

            var table = new PhantomTable(test);
            Assert.AreEqual(m1, table[1].Content);
            Assert.AreEqual(m2, table[2].Content);
            Assert.AreEqual(m2, table[3].Content);
        }
        /// <summary>
        /// Method that creates chain of "first occurrences of different elements".
        /// </summary>
        /// <param name="source">
        /// Source chain.
        /// </param>
        /// <returns>/
        /// Dissimilar chain.
        /// </returns>
        public static Chain Create(BaseChain source)
        {
            var result = new Chain(source.GetLength());
            Alphabet sourceAlphabet = source.Alphabet;
            var entries = new int[sourceAlphabet.Cardinality];

            for (int i = 0; i < source.GetLength(); i++)
            {
                int elementIndex = sourceAlphabet.IndexOf(source[i]);
                int entry = ++entries[elementIndex];
                result.Set(new ValueInt(entry), i);
            }

            return result;
        }
Пример #19
0
        /// <summary>
        /// The divide.
        /// </summary>
        /// <param name="chain">
        /// The chain.
        /// </param>
        /// <returns>
        /// The <see cref="BaseChain"/>.
        /// </returns>
        public BaseChain Divide(BaseChain chain)
        {
            var firstChain = new ActualChain(chain);
            var stack = new Stack();
            stack.Push(firstChain);
            ArrayList list = alphabet.GetLengthList();
            do
            {
                var actChain = (ActualChain)stack.Pop();
                BaseChain chain4Check = actChain.Source;

                for (int i = list.Count - 1; i >= 0; i--)
                {
                    BaseChain word;
                    if (chain4Check.GetLength() >= (int)list[i])
                    {
                        var it = new IteratorStart(chain4Check, (int)list[i], 1);
                        it.Next();
                        word = (BaseChain)it.Current();
                    }
                    else
                    {
                        continue;
                    }

                    if (alphabet.Contains(word))
                    {
                        // solution is found
                        if (chain4Check.GetLength() == (int)list[i])
                        {
                            actChain.RemoveCharacter((int)list[i]);
                            return actChain.GetResult();
                        }

                        var newChain = (ActualChain)actChain.Clone();
                        newChain.RemoveCharacter((int)list[i]);
                        stack.Push(newChain);
                    }
                }
            }
            while (stack.Count > 0);
            return null;
        }
        public void FirstTest()
        {
            var resultChain = new BaseChain(10);
            resultChain.Set(mother.PhantomMessageBc[1], 0);
            resultChain.Set(mother.PhantomMessageA[0], 1);
            resultChain.Set(mother.PhantomMessageA[0], 2);
            resultChain.Set(mother.PhantomMessageBc[1], 3);
            resultChain.Set(mother.PhantomMessageA[0], 4);
            resultChain.Set(mother.PhantomMessageBc[1], 5);
            resultChain.Set(mother.PhantomMessageA[0], 6);
            resultChain.Set(mother.PhantomMessageBc[1], 7);
            resultChain.Set(mother.PhantomMessageA[0], 8);
            resultChain.Set(mother.PhantomMessageA[0], 9);

            var gen = new PhantomChainGenerator(mother.SourceChain, new MockGenerator());
            List<BaseChain> res = gen.Generate(1);
            Assert.AreEqual(res.Count, 1);
            Assert.AreEqual(resultChain, res[0]);
        }
        public void VolumeTest()
        {
            var m3 = new ValuePhantom { new ValueString('a') };
            var m1 = new ValuePhantom { new ValueString('1'), new ValueString('2'), new ValueString('3') };
            var m2 = new ValuePhantom { new ValueString('4'), new ValueString('3') };

            var test = new BaseChain(4);
            test.Set(m1, 0);
            test.Set(m2, 1);
            test.Set(m2, 2);
            test.Set(m3, 3);

            var table = new PhantomTable(test);
            Assert.AreEqual(12, table[0].Volume);
            Assert.AreEqual(4, table[1].Volume);
            Assert.AreEqual(2, table[2].Volume);
            Assert.AreEqual(1, table[3].Volume);
            Assert.AreEqual(1, table[4].Volume);
        }
        public void AddLengthMoreThanChainRankTest()
        {
            alphabet.Add(a);
            alphabet.Add(b);
            alphabet.Add(c);
            alphabet.Add(d);

            baseChain = new BaseChain(3);

            baseChain.Set(a, 0);
            baseChain.Set(b, 1);
            baseChain.Set(b, 2);

            matrix = new Matrix(alphabet.Cardinality, 2);
            var arrayCh = new int[baseChain.GetLength()];
            arrayCh[0] = alphabet.IndexOf(baseChain[0]);
            arrayCh[1] = alphabet.IndexOf(baseChain[1]);
            arrayCh[2] = alphabet.IndexOf(baseChain[2]);
            Assert.Throws<ArgumentException>(() => matrix.Add(arrayCh));
        }
Пример #23
0
        /// <summary>
        /// Initializes a new instance of the <see cref="TreeTop"/> class,
        /// representing root element of the tree.
        /// </summary>
        /// <param name="source">
        /// The input chain.
        /// </param>
        /// <param name="generator">
        /// The gen.
        /// </param>
        public TreeTop(BaseChain source, IGenerator generator)
        {
            table = new PhantomTable(source);
            this.generator = generator;
            StartPosition = 0;
            Volume = table[0].Volume;
            Level = -1;
            if ((table[1].Content[0] is ValueString) || (table[1].Content[0] is BaseChain))
            {
                isString = true;
            }

            if ((source != null) && (source.GetLength() != 0))
            {
                var temp = (ValuePhantom)source[0];
                for (int i = 0; i < temp.Cardinality; i++)
                {
                    Children.Add(new TreeNode(this, temp[i], table));
                }
            }
        }
Пример #24
0
        public void TreeVolumeTest()
        {
            var m1 = new ValuePhantom { new ValueString('1'), new ValueString('2'), new ValueString('3') };
            var m2 = new ValuePhantom { new ValueString('4'), new ValueString('3') };
            var m3 = new ValuePhantom { new ValueString('a') };

            var test = new BaseChain(4);
            test.Set(m1, 0);
            test.Set(m2, 1);
            test.Set(m2, 2);
            test.Set(m3, 3);

            IGenerator gen = new SimpleGenerator();
            var tree = new TreeTop(test, gen);
            Assert.AreEqual(12, tree.Volume);
            var ch1 = tree.GetChild(0);
            Assert.AreEqual(4, ch1.Volume);
            var ch2 = tree.GetChild(1);
            Assert.AreEqual(4, ch2.Volume);
            var ch3 = tree.GetChild(2);
            Assert.AreEqual(4, ch3.Volume);
        }
        /// <summary>
        /// Creates literature sequence in database.
        /// </summary>
        /// <param name="commonSequence">
        /// The common sequence.
        /// </param>
        /// <param name="sequenceStream">
        /// The sequence stream.
        /// </param>
        /// <param name="languageId">
        /// The language id.
        /// </param>
        /// <param name="original">
        /// The original.
        /// </param>
        /// <param name="translatorId">
        /// The translator id.
        /// </param>
        public void Create(CommonSequence commonSequence, Stream sequenceStream, int languageId, bool original, int? translatorId)
        {
            var stringSequence = FileHelper.ReadSequenceFromStream(sequenceStream);
            string[] text = stringSequence.Split('\n');
            for (int l = 0; l < text.Length - 1; l++)
            {
                // removing "\r"
                text[l] = text[l].Substring(0, text[l].Length - 1);
            }

            var chain = new BaseChain(text.Length - 1);

            // file always contains empty string at the end
            // TODO: rewrite this, add empty string check at the end or write a normal trim
            for (int i = 0; i < text.Length - 1; i++)
            {
                chain.Set(new ValueString(text[i]), i);
            }

            MatterRepository.CreateMatterFromSequence(commonSequence);

            var alphabet = ElementRepository.ToDbElements(chain.Alphabet, commonSequence.NotationId, true);
            Create(commonSequence, original, languageId, translatorId, alphabet, chain.Building);
        }
        public void SortListTest()
        {
            var temp = new ChainsAlphabet();
            var a = new BaseChain(2);
            for (int i = 0; i < a.GetLength(); i++)
            {
                a.Set((ValueInt)i, i);
            }

            temp.Add(a);
            var b = new BaseChain(1);
            for (int i = 0; i < b.GetLength(); i++)
            {
                b.Set((ValueInt)i, i);
            }

            temp.Add(b);
            var c = new BaseChain(5);
            for (int i = 0; i < c.GetLength(); i++)
            {
                c.Set((ValueInt)i, i);
            }

            temp.Add(c);
            var d = new BaseChain(2);
            for (int i = 0; i < d.GetLength(); i++)
            {
                d.Set((ValueInt)(i + 1), i);
            }

            temp.Add(d);
            ArrayList list = temp.GetLengthList();
            Assert.AreEqual(5, list[2]);
            Assert.AreEqual(2, list[1]);
            Assert.AreEqual(1, list[0]);
        }
        /// <summary>
        /// The create DNA sequence.
        /// </summary>
        /// <param name="sequence">
        /// The common sequence.
        /// </param>
        /// <param name="fastaSequence">
        /// Sequence as <see cref="ISequence"/>>.
        /// </param>
        /// <param name="partial">
        /// The partial.
        /// </param>
        /// <exception cref="Exception">
        /// Thrown if at least one element of new sequence is missing in db
        /// or if sequence is empty or invalid.
        /// </exception>
        public void Create(CommonSequence sequence, ISequence fastaSequence, bool partial)
        {
            // TODO: fix it for batch import or remove fasta headers completely
            string fastaHeader = ">" + fastaSequence.ID;

            if (fastaHeader.Contains("Resource temporarily unavailable"))
            {
                throw new Exception("Sequence is empty or invalid (probably ncbi is not responding).");
            }

            string stringSequence = fastaSequence.ConvertToString().ToUpper();

            var chain = new BaseChain(stringSequence);

            if (!ElementRepository.ElementsInDb(chain.Alphabet, sequence.NotationId))
            {
                throw new Exception("At least one element of new sequence is invalid (not A, C, T, G or U).");
            }

            MatterRepository.CreateMatterFromSequence(sequence);

            var alphabet = ElementRepository.ToDbElements(chain.Alphabet, sequence.NotationId, false);
            Create(sequence, partial, alphabet, chain.Building);
        }
 public void ConstructorTest()
 {
     chain = new Chain(100);
     Assert.AreEqual(100, chain.GetLength());
 }
 public void Initialization()
 {
     chain = new BaseChain(10);
 }
 public void Initialization()
 {
     chain = new Chain("adbaacbbaaca");
     baseChain = new BaseChain("adbaacbbaaca");
 }