Exemplo n.º 1
0
        void InValidateFastQFormatter(FastQFormatParameters param)
        {
            // Gets the expected sequence from the Xml
            string filepath = utilityObj.xmlUtil.GetTextValue(
                Constants.MultiSeqSangerRnaProNode, Constants.FilePathNode);

            // Parse a FastQ file.
            using (FastQParser fastQParser = new FastQParser(filepath))
            {
                fastQParser.AutoDetectFastQFormat = true;
                IEnumerable <QualitativeSequence> sequence = null;
                FastQFormatter fastQFormatter = null;

                switch (param)
                {
                case FastQFormatParameters.Sequence:
                    try
                    {
                        fastQFormatter = new FastQFormatter(filepath);
                        fastQFormatter.Write(null as ISequence);
                        Assert.Fail();
                    }
                    catch (ArgumentNullException)
                    {
                        fastQFormatter.Close();
                        ApplicationLog.WriteLine(
                            "FastQ Parser P2 : Successfully validated the exception");
                        Console.WriteLine(
                            "FastQ Parser P2 : Successfully validated the exception");
                    }
                    break;

                default:
                    try
                    {
                        sequence       = fastQParser.Parse();
                        fastQFormatter = new FastQFormatter(Constants.FastQTempFileName);
                        fastQFormatter.Write(sequence as QualitativeSequence);
                        Assert.Fail();
                    }
                    catch (ArgumentNullException)
                    {
                        fastQFormatter.Close();
                        ApplicationLog.WriteLine(
                            "FastQ Parser P2 : Successfully validated the exception");
                        Console.WriteLine(
                            "FastQ Parser P2 : Successfully validated the exception");
                    }
                    break;
                }
            }
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            FastAParser    fap = new FastAParser(@"D:\TestMixing\CRS.rn.fasta");
            FastQFormatter faq = new FastQFormatter(@"D:\TestMixing\RefSeq.fastq");
            var            seq = fap.Parse().First() as Sequence;

            seq = seq.GetSubSequence(1000, 300) as Sequence;
            int coverage = 50;
            int len      = 75;

            byte[] QualScores = Enumerable.Range(0, len).Select(x => (byte)(33 + 35)).ToArray();
            for (int i = 0; i < (seq.Count - len); i++)
            {
                //string s=seq.GetSubSequence(i,len).ToString();
                string s  = (seq as Sequence).ConvertToString(i, len);
                int    hh = s.Length;
                byte[] sb = Encoding.UTF8.GetBytes(s);
                for (int j = 0; j < coverage; j++)
                {
                    QualitativeSequence qs = new QualitativeSequence(Alphabets.DNA, FastQFormatType.Illumina_v1_8, sb, QualScores);
                    qs.ID = "REF-" + i.ToString();
                    faq.Write(qs);
                }
            }
            faq.Close();
            fap.Close();
            byte[] newSeq = new byte[seq.Count];
            seq.CopyTo(newSeq, 0, seq.Count);
            byte oldBase = seq[seq.Count / 2];

            newSeq[newSeq.Length / 2] = 65;
            //now mutate one base and go again
            Sequence nonRef = new Sequence(Alphabets.DNA, newSeq, true);

            faq = new FastQFormatter(@"D:\TestMixing\NonRefSeq.fastq");
            for (int i = 0; i < (seq.Count - len); i++)
            {
                //string s=seq.GetSubSequence(i,len).ToString();
                string s  = (nonRef as Sequence).ConvertToString(i, len);
                int    hh = s.Length;
                byte[] sb = Encoding.UTF8.GetBytes(s);
                for (int j = 0; j < coverage; j++)
                {
                    QualitativeSequence qs = new QualitativeSequence(Alphabets.DNA, FastQFormatType.Illumina_v1_8, sb, QualScores);
                    qs.ID = "NONREF-" + i.ToString();
                    faq.Write(qs);
                }
            }
            faq.Close();
        }
Exemplo n.º 3
0
        private void InValidateFastQFormatter(FastQFormatParameters param)
        {
            // Gets the expected sequence from the Xml
            string filepath = utilityObj.xmlUtil.GetTextValue(
                Constants.MultiSeqSangerRnaProNode, Constants.FilePathNode);

            // Parse a FastQ file.
            var fastQParser = new FastQParser();

            using (fastQParser.Open(filepath))
            {
                FastQFormatter fastQFormatter = null;

                switch (param)
                {
                case FastQFormatParameters.Sequence:
                    try
                    {
                        fastQFormatter = new FastQFormatter();
                        fastQFormatter.Format(null as ISequence, filepath);
                        Assert.Fail();
                    }
                    catch (ArgumentNullException)
                    {
                        fastQFormatter.Close();
                        ApplicationLog.WriteLine(
                            "FastQ Parser P2 : Successfully validated the exception");
                    }
                    break;

                default:
                    try
                    {
                        IEnumerable <IQualitativeSequence> sequence = fastQParser.Parse();
                        fastQFormatter = new FastQFormatter();
                        fastQFormatter.Format(sequence, null);
                        Assert.Fail();
                    }
                    catch (ArgumentNullException)
                    {
                        ApplicationLog.WriteLine("FastQ Parser P2 : Successfully validated the exception");
                    }
                    break;
                }
            }
        }
Exemplo n.º 4
0
        private void InValidateFastQFormatter(FastQFormatParameters param)
        {
            // Gets the expected sequence from the Xml
            string filepath = utilityObj.xmlUtil.GetTextValue(
                Constants.MultiSeqSangerRnaProNode, Constants.FilePathNode);

            // Parse a FastQ file.
            var fastQParser = new FastQParser();
            using (fastQParser.Open(filepath))
            {
                FastQFormatter fastQFormatter = null;

                switch (param)
                {
                    case FastQFormatParameters.Sequence:
                        try
                        {
                            fastQFormatter = new FastQFormatter();
                            fastQFormatter.Format(null as ISequence, filepath);
                            Assert.Fail();
                        }
                        catch (ArgumentNullException)
                        {
                            fastQFormatter.Close();
                            ApplicationLog.WriteLine(
                                "FastQ Parser P2 : Successfully validated the exception");
                        }
                        break;
                    default:
                        try
                        {
                            IEnumerable<IQualitativeSequence> sequence = fastQParser.Parse();
                            fastQFormatter = new FastQFormatter();
                            fastQFormatter.Format(sequence, null);
                            Assert.Fail();
                        }
                        catch (ArgumentNullException)
                        {
                            ApplicationLog.WriteLine("FastQ Parser P2 : Successfully validated the exception");
                        }
                        break;
                }
            }
        }
Exemplo n.º 5
0
        public void FastQFormatterUsingInterface()
        {
            string filepathOriginal = @"TestUtils\FASTQ\SRR002012_5.fastq";

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

            IList <QualitativeSequence> seqsOriginal = null;
            string filepathTmp = Path.GetTempFileName();

            using (FastQParser parser = new FastQParser())
            {
                parser.Open(filepathOriginal);

                // Read the original file
                seqsOriginal = parser.Parse().ToList();
                Assert.IsNotNull(seqsOriginal);

                // Use the formatter to write the original sequences to a temp file
                ISequenceFormatter formatter = null;
                try
                {
                    formatter = new FastQFormatter();
                    formatter.Open(filepathTmp);
                    foreach (ISequence s in seqsOriginal)
                    {
                        formatter.Write(s);
                    }

                    formatter.Close();
                }
                finally
                {
                    if (formatter != null)
                    {
                        ((FastQFormatter)formatter).Dispose();
                    }
                }
            }

            // Read the new file, then compare the sequences
            IList <QualitativeSequence> seqsNew = null;

            using (FastQParser parser = new FastQParser(filepathTmp))
            {
                seqsNew = parser.Parse().ToList();
                Assert.IsNotNull(seqsNew);

                // 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    = ASCIIEncoding.ASCII.GetString(seqsOriginal[i].ToArray());
                    string newSeq    = ASCIIEncoding.ASCII.GetString(seqsNew[i].ToArray());
                    string orgscores = ASCIIEncoding.ASCII.GetString(seqsOriginal[i].GetEncodedQualityScores());
                    string newscores = ASCIIEncoding.ASCII.GetString(seqsNew[i].GetEncodedQualityScores());
                    Assert.AreEqual(orgSeq, newSeq);
                    Assert.AreEqual(orgscores, newscores);
                }

                // 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);
            }
        }
Exemplo n.º 6
0
        /// <summary>
        ///     General method to validate FastQ formatting
        ///     Qualitative Sequence by passing TextWriter as a parameter
        ///     <param name="nodeName">xml node name.</param>
        /// </summary>
        private void ValidateFastQFormatByFormattingQualSeqeunce(string nodeName)
        {
            // Gets the actual sequence and the alphabet from the Xml
            IAlphabet alphabet = Utility.GetAlphabet(utilityObj.xmlUtil.GetTextValue(nodeName, Constants.AlphabetNameNodeV2));
            FastQFormatType expectedFormatType = Utility.GetFastQFormatType(utilityObj.xmlUtil.GetTextValue(nodeName, Constants.FastQFormatType));
            string qualSequence = utilityObj.xmlUtil.GetTextValue(nodeName, Constants.ExpectedSequenceNode);
            string expectedQualitativeSequence = utilityObj.xmlUtil.GetTextValue(nodeName, Constants.ExpectedSequenceNode);
            string qualityScores = "";
            int i;

            for (i = 0; i < qualSequence.Length; i++)
                qualityScores = qualityScores + "}";

            byte[] seq = Encoding.UTF8.GetBytes(qualSequence);
            byte[] qScore = Encoding.UTF8.GetBytes(qualityScores);
            string tempFileName = Path.GetTempFileName();

            // Create a Qualitative Sequence.
            var qualSeq = new QualitativeSequence(alphabet, expectedFormatType, seq, qScore);

            var formatter = new FastQFormatter();
            using (formatter.Open(tempFileName))
            {
                formatter.Format(qualSeq);
                formatter.Close();

                var fastQParserObj = new FastQParser();
                using (fastQParserObj.Open(tempFileName))
                {
                    // Read the new file and validate Sequences.
                    var seqsNew = fastQParserObj.Parse();
                    var firstSequence = seqsNew.First();

                    // Validate qualitative Sequence upon parsing FastQ file.
                    Assert.AreEqual(expectedQualitativeSequence, firstSequence.ConvertToString());
                    Assert.IsTrue(string.IsNullOrEmpty(firstSequence.ID));

                    ApplicationLog.WriteLine(string.Format("FastQ Parser P1: The FASTQ sequence '{0}' validation after Parse() is found to be as expected.", firstSequence));
                }

                File.Delete(tempFileName);
            }
        }
Exemplo n.º 7
0
        void ValidateFastQFormatter(string nodeName,
                                    FastQFileParameters fileExtension)
        {
            // Gets the expected sequence from the Xml
            string filePath = utilityObj.xmlUtil.GetTextValue(
                nodeName, Constants.FilePathNode);
            string expectedQualitativeSequence = utilityObj.xmlUtil.GetTextValue(
                nodeName, Constants.ExpectedSequenceNode);
            string expectedSequenceId = utilityObj.xmlUtil.GetTextValue(
                nodeName, Constants.SequenceIdNode);
            string tempFileName1 = System.IO.Path.GetTempFileName();
            string tempFileName2 = System.IO.Path.GetTempFileName();

            // Parse a FastQ file using parseOne method.
            using (FastQParser fastQParserObj = new FastQParser(filePath))
            {
                fastQParserObj.AutoDetectFastQFormat = false;
                IEnumerable <ISequence> qualSequence = null;
                qualSequence = fastQParserObj.Parse();

                // New Sequence after formatting file.
                IEnumerable <ISequence> newQualSeq       = null;
                FastQFormatter          fastQFormatter   = new FastQFormatter(tempFileName1);
                FastQFormatter          fastQFormatterFq = new FastQFormatter(tempFileName2);
                string parsedValue = null;
                string parsedID    = null;

                // Format Parsed Sequence to temp file with different extension.
                switch (fileExtension)
                {
                case FastQFileParameters.FastQ:
                    fastQFormatter.Write(qualSequence.ElementAt(0));
                    fastQFormatter.Close();
                    FastQParser fastQParserObjTemp = new FastQParser(tempFileName1);
                    newQualSeq  = fastQParserObjTemp.Parse();
                    parsedValue = new string(newQualSeq.ElementAt(0).Select(a => (char)a).ToArray());
                    parsedID    = newQualSeq.ElementAt(0).ID.ToString((IFormatProvider)null);
                    fastQParserObjTemp.Dispose();
                    break;

                case FastQFileParameters.Fq:
                    fastQFormatterFq.Write(qualSequence.ElementAt(0));
                    fastQFormatterFq.Close();
                    FastQParser fastQParserObjTemp1 = new FastQParser(tempFileName2);
                    newQualSeq  = fastQParserObjTemp1.Parse();
                    parsedValue = new string(newQualSeq.ElementAt(0).Select(a => (char)a).ToArray());
                    parsedID    = newQualSeq.ElementAt(0).ID.ToString((IFormatProvider)null);
                    break;

                default:
                    break;
                }

                // Validate qualitative parsing temporary file.
                Assert.AreEqual(parsedValue, expectedQualitativeSequence);
                Assert.AreEqual(parsedID, expectedSequenceId);
                ApplicationLog.WriteLine(string.Format((IFormatProvider)null,
                                                       "FastQ Formatter BVT: The FASTQ sequence '{0}' validation after Write() and Parse() is found to be as expected.",
                                                       parsedValue));

                // Logs to the NUnit GUI (Console.Out) window
                Console.WriteLine(string.Format((IFormatProvider)null,
                                                "FastQ Formatter BVT: The FASTQ sequence '{0}' validation after Write() and Parse() is found to be as expected.",
                                                parsedValue));
                Console.WriteLine(string.Format((IFormatProvider)null,
                                                "FastQ Formatter BVT: The FASTQ sequence '{0}' validation after Write() and Parse() is found to be as expected.",
                                                parsedID));

                qualSequence     = null;
                fastQFormatter   = null;
                fastQFormatterFq = null;
                GC.Collect();
                GC.WaitForPendingFinalizers();
                File.Delete(tempFileName1);
                File.Delete(tempFileName2);
            }
        }