Ejemplo n.º 1
0
        public void FastQFormatter()
        {
            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
                using (FastQFormatter formatter = new FastQFormatter(filepathTmp))
                {
                    foreach (QualitativeSequence s in seqsOriginal)
                    {
                        formatter.Write(s);
                    }
                }
            }

            // 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);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// General method to validate FastQ Formatter on a Stream.
        /// <param name="nodeName">xml node name.</param>
        /// </summary>
        void ValidateFastQFormatterOnAStream(string nodeName)
        {
            // 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 parsedValue   = string.Empty;
            string parsedID      = string.Empty;
            IEnumerable <QualitativeSequence> qualSequence = null;

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

                // New Sequence after formatting file.
                IEnumerable <QualitativeSequence> newQualSeq = null;
                using (StreamWriter writer = new StreamWriter(tempFileName1))
                {
                    using (FastQFormatter fastQFormatter = new FastQFormatter())
                    {
                        fastQFormatter.Open(writer);
                        fastQFormatter.Write(qualSequence.ElementAt(0));
                    }
                }

                using (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);
                }

                // 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 VSTest 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;
                File.Delete(tempFileName1);
            }
        }
Ejemplo n.º 3
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;
                }
            }
        }
Ejemplo n.º 4
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();
        }
Ejemplo n.º 5
0
        private 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 tempFileName = Path.GetTempFileName();

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

                // New Sequence after formatting file.
                IEnumerable <QualitativeSequence> newQualSeq = null;
                string parsedValue = null;
                string parsedID    = null;

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

                    break;

                case FastQFileParameters.Fq:
                    using (var fastQFormatterFq = new FastQFormatter(tempFileName))
                    {
                        fastQFormatterFq.Write(qualSequence.ElementAt(0));
                    }
                    using (var fastQParserObjTemp1 = new FastQParser(tempFileName))
                    {
                        newQualSeq  = fastQParserObjTemp1.Parse();
                        parsedValue = new string(newQualSeq.ElementAt(0).Select(a => (char)a).ToArray());
                        parsedID    = newQualSeq.ElementAt(0).ID.ToString(null);
                    }
                    break;

                default:
                    break;
                }

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

                qualSequence = null;
                File.Delete(tempFileName);
            }
        }
Ejemplo n.º 6
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);
            }
        }