Пример #1
0
        public void BadContent()
        {
            StringBuilder s = new StringBuilder();

            s.AppendLine("AAAA");
            FastaParser parser = null;

            try
            {
                parser = new FastaParser();
                bool exception = false;
                IList <ISequence> sequences = null;
                try
                {
                    sequences = parser.Parse(new StringReader(s.ToString()));
                }
                catch (Exception ex)
                {
                    exception = true;
                    Assert.AreEqual(ex.Message, "Bad input in file [Fasta]");
                }
                Assert.IsTrue(exception);
            }
            finally
            {
                if (parser != null)
                {
                    parser.Dispose();
                }
            }
        }
Пример #2
0
        public void FastaFor186972391()
        {
            string expectedSequence =
                "IFYEPVEILGYDNKSSLVLVKRLITRMYQQKSLISSLNDSNQNEFWGHKNSFSSHFSSQMVSEGFGVILE" +
                "IPFSSRLVSSLEEKRIPKSQNLRSIHSIFPFLEDKLSHLNYVSDLLIPHPIHLEILVQILQCWIKDVPSL" +
                "HLLRLFFHEYHNLNSLITLNKSIYVFSKRKKRFFGFLHNSYVYECEYLFLFIRKKSSYLRSISSGVFLER" +
                "THFYGKIKYLLVVCCNSFQRILWFLKDTFIHYVRYQGKAIMASKGTLILMKKWKFHLVNFWQSYFHFWFQ" +
                "PYRINIKQLPNYSFSFLGYFSSVRKNPLVVRNQMLENSFLINTLTQKLDTIVPAISLIGSLSKAQFCTVL" +
                "GHPISKPIWTDLSDSDILDRFCRICRNLCRYHSGSSKKQVLYRIKYIFRLSCARTLARKHKSTVRTFMRR" +
                "LGSGFLEEFFLEEE";

            string filepath = @"TestUtils\FASTA\186972391.fasta";

            Assert.IsTrue(File.Exists(filepath));

            IList <ISequence> seqs   = null;
            FastaParser       parser = null;

            try
            {
                parser = new FastaParser();
                using (StreamReader reader = File.OpenText(filepath))
                {
                    seqs = parser.Parse(reader);
                }
                Assert.IsNotNull(seqs);
                Assert.AreEqual(1, seqs.Count);
                Sequence seq = (Sequence)seqs[0];
                Assert.IsNotNull(seq);
                Assert.AreEqual(expectedSequence, seq.ToString());
                Assert.AreEqual(expectedSequence.Length, seq.EncodedValues.Length);
                Assert.IsNotNull(seq.Alphabet);
                Assert.AreEqual(seq.Alphabet.Name, "Protein");

                Assert.AreEqual("gi|186972391|gb|ACC99454.1| maturase K [Scaphosepalum rapax]", seq.ID);

                // Try it again with ParseOne, from reader and from filename
                using (StreamReader reader = File.OpenText(filepath))
                {
                    seq = (Sequence)parser.ParseOne(reader);
                }
                Assert.IsNotNull(seq);
                Assert.AreEqual(expectedSequence, seq.ToString());
                Assert.AreEqual(expectedSequence.Length, seq.EncodedValues.Length);
                Assert.IsNotNull(seq.Alphabet);
                Assert.AreEqual(seq.Alphabet.Name, "Protein");

                Assert.AreEqual("gi|186972391|gb|ACC99454.1| maturase K [Scaphosepalum rapax]", seq.ID);

                seq = (Sequence)parser.ParseOne(filepath);

                Assert.IsNotNull(seq);
                Assert.AreEqual(expectedSequence, seq.ToString());
                Assert.AreEqual(expectedSequence.Length, seq.EncodedValues.Length);
                Assert.IsNotNull(seq.Alphabet);
                Assert.AreEqual(seq.Alphabet.Name, "Protein");

                Assert.AreEqual("gi|186972391|gb|ACC99454.1| maturase K [Scaphosepalum rapax]", seq.ID);
            }
            finally
            {
                if (parser != null)
                {
                    parser.Dispose();
                }
            }
        }
Пример #3
0
        public void AllEditableScenarios()
        {
            string filepathOriginal = @"TestUtils\Fasta\5_sequences.fasta";

            Assert.IsTrue(File.Exists(filepathOriginal));

            FastaParser fastaParser = null;

            try
            {
                fastaParser = new FastaParser();
                IList <ISequence> sequences;
                string[]          expectedSequences = new string[] {
                    "KRIPKSQNLRSIHSIFPFLEDKLSHLN",
                    "LNIPSLITLNKSIYVFSKRKKRLSGFLHN",
                    "HEAGAWGHEEHEAGAWGHEEHEAGAWGHEE",
                    "PAWHEAEPAWHEAEPAWHEAEPAWHEAEPAWHEAE",
                    "CGGUCCCGCGGUCCCGCGGUCCCGCGGUCCCG"
                };

                fastaParser.EnforceDataVirtualization = true;

                sequences = fastaParser.Parse(filepathOriginal, true);
                int sequenceCount = sequences.Count;

                for (int i = 0; i < sequenceCount; i++)
                {
                    Sequence actualSequence = sequences[i] as Sequence;
                    actualSequence.IsReadOnly = false;
                    ISequenceItem item = actualSequence[1];

                    actualSequence.Add(item);
                    expectedSequences[i] += item.Symbol;
                    Assert.AreEqual(expectedSequences[i], actualSequence.ToString());

                    actualSequence.Remove(item);
                    int indexOfItem = expectedSequences[i].IndexOf(item.Symbol);
                    expectedSequences[i] = expectedSequences[i].Remove(indexOfItem, 1);
                    Assert.AreEqual(expectedSequences[i], actualSequence.ToString());

                    actualSequence.RemoveAt(0);
                    expectedSequences[i] = expectedSequences[i].Remove(0, 1);
                    Assert.AreEqual(expectedSequences[i], actualSequence.ToString());

                    actualSequence.RemoveRange(2, 5);
                    expectedSequences[i] = expectedSequences[i].Remove(2, 5);
                    Assert.AreEqual(expectedSequences[i], actualSequence.ToString());

                    actualSequence.Replace(0, 'C');
                    expectedSequences[i] = expectedSequences[i].Remove(0, 1);
                    expectedSequences[i] = expectedSequences[i].Insert(0, "C");
                    Assert.AreEqual(expectedSequences[i], actualSequence.ToString());

                    actualSequence.ReplaceRange(3, "GG");
                    expectedSequences[i] = expectedSequences[i].Remove(3, 2);
                    expectedSequences[i] = expectedSequences[i].Insert(3, "GG");
                    Assert.AreEqual(expectedSequences[i], actualSequence.ToString());

                    actualSequence.Insert(3, item);
                    expectedSequences[i] = expectedSequences[i].Insert(3, item.Symbol.ToString());
                    Assert.AreEqual(expectedSequences[i], actualSequence.ToString());

                    actualSequence.InsertRange(2, "CC");
                    expectedSequences[i] = expectedSequences[i].Insert(2, "CC");
                    Assert.AreEqual(expectedSequences[i], actualSequence.ToString());

                    bool actualContainsValue   = actualSequence.Contains(actualSequence[3]);
                    bool expectedContainsValue = expectedSequences[i].Contains(actualSequence[3].Symbol.ToString());
                    Assert.AreEqual(actualContainsValue, expectedContainsValue);
                }
            }
            finally
            {
                if (fastaParser != null)
                {
                    fastaParser.Dispose();
                }
            }
        }
Пример #4
0
        public void FastaFormatter()
        {
            // Test with FASTA file from Simon

            string filepathOriginal = @"TestUtils\FASTA\NC_005213.ffn";

            Assert.IsTrue(File.Exists(filepathOriginal));
            FastaParser parser = null;

            try
            {
                parser = new FastaParser();
                FastaFormatter formatter = new FastaFormatter();

                // Read the original file
                IList <ISequence> seqsOriginal = null;
                parser       = new FastaParser();
                seqsOriginal = parser.Parse(filepathOriginal);
                Assert.IsNotNull(seqsOriginal);

                // Use the formatter to write the original sequences to a temp file
                string filepathTmp = Path.GetTempFileName();
                using (TextWriter writer = new StreamWriter(filepathTmp))
                {
                    foreach (Sequence s in seqsOriginal)
                    {
                        formatter.Format(s, writer);
                    }
                }

                // Read the new file, then compare the sequences
                IList <ISequence> seqsNew = null;
                parser  = new FastaParser();
                seqsNew = parser.Parse(filepathTmp);
                Assert.IsNotNull(seqsOriginal);

                // Now compare the sequences.
                int countOriginal = seqsOriginal.Count();
                int countNew      = seqsNew.Count();
                Assert.AreEqual(countOriginal, countNew);

                int i;
                for (i = 0; i < countOriginal; i++)
                {
                    Assert.AreEqual(seqsOriginal[i].ID, seqsNew[i].ID);
                    string orgSeq = seqsOriginal[i].ToString();
                    string newSeq = seqsNew[i].ToString();
                    Assert.AreEqual(orgSeq, newSeq);
                }
                // Passed all the tests, delete the tmp file. If we failed an Assert,
                // the tmp file will still be there in case we need it for debugging.
                File.Delete(filepathTmp);
            }
            finally
            {
                if (parser != null)
                {
                    parser.Dispose();
                }
            }
        }