예제 #1
0
        /// <summary>
        /// Writes the specified QualitativeSequence in FastQ format to the file.
        /// </summary>
        /// <param name="qualitativeSequence">QualitativeSequence to write.</param>
        public void Write(QualitativeSequence qualitativeSequence)
        {
            if (qualitativeSequence == null)
            {
                throw new ArgumentNullException("qualitativeSequence");
            }

            if (this.streamWriter == null)
            {
                throw new InvalidOperationException(Properties.Resource.FileNotOpened);
            }

            string header    = qualitativeSequence.ID;
            string lengthStr = " length=";

            if (qualitativeSequence.ID.Contains(lengthStr))
            {
                int startIndex = qualitativeSequence.ID.LastIndexOf(lengthStr, StringComparison.OrdinalIgnoreCase);
                header = header.Substring(0, startIndex + 8) + qualitativeSequence.Count;
            }

            // Write to stream.
            this.streamWriter.WriteLine("@" + header);
            byte[] sequenceBytes = qualitativeSequence.ToArray();
            this.streamWriter.WriteLine(UTF8Encoding.UTF8.GetString(sequenceBytes, 0, sequenceBytes.Length));
            this.streamWriter.WriteLine("+" + header);
            byte[] qualityValues = QualitativeSequence.ConvertEncodedQualityScore(qualitativeSequence.FormatType, this.FormatType, qualitativeSequence.GetEncodedQualityScores());
            this.streamWriter.WriteLine(UTF8Encoding.UTF8.GetString(qualityValues, 0, qualityValues.Length));

            if (this.AutoFlush)
            {
                this.streamWriter.Flush();
            }
        }
예제 #2
0
        public void ConvertIlluminaToSangerAndSolexa()
        {
            // Gets the actual sequence and the Qual score from the Xml
            string illuminaSequence = utilityObj.xmlUtil.GetTextValue(
                Constants.IlluminaToSangerAndSolexaNode, Constants.IlluminaSequence);
            string illuminaQualScore = utilityObj.xmlUtil.GetTextValue(
                Constants.IlluminaToSangerAndSolexaNode, Constants.IlluminaQualScore);
            string expectedSangerQualScore = utilityObj.xmlUtil.GetTextValue(
                Constants.IlluminaToSangerAndSolexaNode, Constants.SangerQualScore);
            string expectedSolexaQualScore = utilityObj.xmlUtil.GetTextValue(
                Constants.IlluminaToSangerAndSolexaNode, Constants.SolexaQualScore);

            byte[] illuminaSequenceinBytes  = Encoding.UTF8.GetBytes(illuminaSequence);
            byte[] illuminaQualScoreinBytes = Encoding.UTF8.GetBytes(illuminaQualScore);
            byte[] sangerQualScore          = QualitativeSequence.ConvertEncodedQualityScore(FastQFormatType.Illumina_v1_3, FastQFormatType.Sanger, illuminaQualScoreinBytes);

            string qualSequenceSanger = new string(Encoding.UTF8.GetChars(sangerQualScore));

            // Validate converted sanger score.
            Assert.AreEqual(expectedSangerQualScore, qualSequenceSanger);

            ApplicationLog.WriteLine(string.Format((IFormatProvider)null,
                                                   "Qualitative Sequence BVT:Qualitative Sanger score type {0} is as expected.",
                                                   sangerQualScore));
            byte[] solexaQualScore = QualitativeSequence.ConvertEncodedQualityScore(FastQFormatType.Illumina_v1_3, FastQFormatType.Solexa_Illumina_v1_0, illuminaQualScoreinBytes);

            string qualSequenceSolexa = new string(Encoding.UTF8.GetChars(solexaQualScore));

            // Validate converted illumina score.
            Assert.AreEqual(expectedSolexaQualScore, qualSequenceSolexa);
            ApplicationLog.WriteLine(string.Format((IFormatProvider)null,
                                                   "Qualitative Sequence BVT:Qualitative Solexa format type {0} is as expected.",
                                                   illuminaQualScore));
        }
예제 #3
0
 public void TestConvertFromSolexa_Illumina_v1_0ToSanger()
 {
     byte[] sangerScores;
     byte[] solexaScores = new byte[1];
     solexaScores[0] = (byte)59;
     sangerScores    = QualitativeSequence.ConvertEncodedQualityScore(FastQFormatType.Solexa_Illumina_v1_0, FastQFormatType.Sanger, solexaScores);
     Assert.IsNotNull((object)sangerScores);
     Assert.AreEqual(1, sangerScores.Length);
     Assert.AreEqual((byte)33, sangerScores[0]);
 }
예제 #4
0
 public void TestConvertFromIllumina_v1_3ToSanger()
 {
     byte[] sangerScores;
     byte[] illuminaScores = new byte[2];
     illuminaScores[0] = (byte)64;
     illuminaScores[1] = (byte)64;
     sangerScores      = QualitativeSequence.ConvertEncodedQualityScore(FastQFormatType.Illumina_v1_3, FastQFormatType.Sanger, illuminaScores);
     Assert.IsNotNull((object)sangerScores);
     Assert.AreEqual(2, sangerScores.Length);
     Assert.AreEqual((byte)33, sangerScores[0]);
     Assert.AreEqual((byte)33, sangerScores[1]);
 }
예제 #5
0
 public void TestConvertFromIllumina_v1_3ToSolexa_Illumina_v1_0()
 {
     byte[] solexaScores;
     byte[] illuminaScores = new byte[2];
     illuminaScores[0] = (byte)65;
     illuminaScores[1] = (byte)65;
     solexaScores      = QualitativeSequence.ConvertEncodedQualityScore(FastQFormatType.Illumina_v1_3, FastQFormatType.Solexa_Illumina_v1_0, illuminaScores);
     Assert.IsNotNull((object)solexaScores);
     Assert.AreEqual(2, solexaScores.Length);
     Assert.AreEqual((byte)59, solexaScores[0]);
     Assert.AreEqual((byte)59, solexaScores[1]);
 }
예제 #6
0
 public void TestConvertFromSangerToIllumina_v1_3()
 {
     byte[] illuminaScores;
     byte[] sangerScores = new byte[2];
     sangerScores[0] = (byte)33;
     sangerScores[1] = (byte)33;
     illuminaScores  = QualitativeSequence.ConvertEncodedQualityScore(FastQFormatType.Sanger, FastQFormatType.Illumina_v1_3, sangerScores);
     Assert.IsNotNull((object)illuminaScores);
     Assert.AreEqual <int>(2, illuminaScores.Length);
     Assert.AreEqual <byte>((byte)64, illuminaScores[0]);
     Assert.AreEqual <byte>((byte)64, illuminaScores[1]);
 }
예제 #7
0
 public void TestConvertFromSangerToSolexa_Illumina_v1_0()
 {
     byte[] solexaScores;
     byte[] sangerScores = new byte[2];
     sangerScores[0] = (byte)34;
     sangerScores[1] = (byte)34;
     solexaScores    = QualitativeSequence.ConvertEncodedQualityScore(FastQFormatType.Sanger, FastQFormatType.Solexa_Illumina_v1_0, sangerScores);
     Assert.IsNotNull((object)solexaScores);
     Assert.AreEqual <int>(2, solexaScores.Length);
     Assert.AreEqual <byte>((byte)59, solexaScores[0]);
     Assert.AreEqual <byte>((byte)59, solexaScores[1]);
 }
예제 #8
0
        /// <summary>
        /// Write out a sequence to the given stream writer.
        /// </summary>
        /// <param name="writer"></param>
        /// <param name="qualitativeSequence"></param>
        private void Format(StreamWriter writer, IQualitativeSequence qualitativeSequence)
        {
            string       header    = qualitativeSequence.ID;
            const string LengthStr = " length=";

            if (qualitativeSequence.ID.Contains(LengthStr))
            {
                int startIndex = qualitativeSequence.ID.LastIndexOf(LengthStr, StringComparison.OrdinalIgnoreCase);
                header = header.Substring(0, startIndex + 8) + qualitativeSequence.Count;
            }

            // Write to stream.
            writer.WriteLine("@" + header);
            byte[] sequenceBytes = qualitativeSequence.ToArray();
            writer.WriteLine(Encoding.UTF8.GetString(sequenceBytes, 0, sequenceBytes.Length));
            writer.WriteLine("+" + header);
            byte[] qualityValues = QualitativeSequence.ConvertEncodedQualityScore(qualitativeSequence.FormatType, this.FormatType, qualitativeSequence.GetEncodedQualityScores());
            writer.WriteLine(Encoding.UTF8.GetString(qualityValues, 0, qualityValues.Length));

            if (this.AutoFlush)
            {
                writer.Flush();
            }
        }
예제 #9
0
        /// <summary>
        /// Writes SAMAlignedSequence to specified text writer.
        /// </summary>
        /// <param name="alignedSequence">SAM aligned sequence to write</param>
        /// <param name="writer">Text writer.</param>
        public static void WriteSAMAlignedSequence(IAlignedSequence alignedSequence, TextWriter writer)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }

            if (alignedSequence == null)
            {
                throw new ArgumentNullException("alignedSequence");
            }

            SAMAlignedSequenceHeader alignedHeader = alignedSequence.Metadata[Helper.SAMAlignedSequenceHeaderKey] as SAMAlignedSequenceHeader;

            if (alignedHeader == null)
            {
                throw new ArgumentException(Properties.Resource.SAM_AlignedSequenceHeaderMissing);
            }

            ISequence sequence = alignedSequence.Sequences[0];

            if (!(sequence.Alphabet is DnaAlphabet))
            {
                throw new ArgumentException(Properties.Resource.SAMFormatterSupportsDNAOnly);
            }

            string seq = "*";

            if (sequence.Count > 0)
            {
                char[] symbols = new char[sequence.Count];
                for (int i = 0; i < sequence.Count; i++)
                {
                    symbols[i] = (char)sequence[i];
                }

                seq = new string(symbols);
            }

            string qualValues = "*";

            QualitativeSequence qualSeq = sequence as QualitativeSequence;

            if (qualSeq != null)
            {
                byte[] bytes = qualSeq.GetEncodedQualityScores();

                // if FormatType is not sanger then convert the quality scores to sanger.
                if (qualSeq.FormatType != FastQFormatType.Sanger)
                {
                    bytes = QualitativeSequence.ConvertEncodedQualityScore(qualSeq.FormatType, FastQFormatType.Sanger, bytes);
                }

                qualValues = System.Text.ASCIIEncoding.ASCII.GetString(bytes);
            }

            writer.Write(AlignedSequenceFormat,
                         alignedHeader.QName, (int)alignedHeader.Flag, alignedHeader.RName,
                         alignedHeader.Pos, alignedHeader.MapQ, alignedHeader.CIGAR,
                         alignedHeader.MRNM.Equals(alignedHeader.RName) ? "=" : alignedHeader.MRNM,
                         alignedHeader.MPos, alignedHeader.ISize, seq, qualValues);

            foreach (var j in alignedHeader.OptionalFields)
            {
                writer.Write(OptionalFieldFormat, j.Tag,
                             j.VType, j.Value);
            }

            writer.WriteLine();
        }
예제 #10
0
        public void ConvertSangerToSolexaAndIllumina()
        {
            // Gets the actual sequence and the Qual score from the Xml
            string sangerSequence = utilityObj.xmlUtil.GetTextValue(
                Constants.SangerToSolexaAndIlluminaNode, Constants.SangerSequence);
            string expectedSolexaSequence = utilityObj.xmlUtil.GetTextValue(
                Constants.SangerToSolexaAndIlluminaNode, Constants.SolexaSequence);
            string expectedIlluminaSequence = utilityObj.xmlUtil.GetTextValue(
                Constants.SangerToSolexaAndIlluminaNode, Constants.IlluminaSequence);
            string sangerQualScore = utilityObj.xmlUtil.GetTextValue(
                Constants.SangerToSolexaAndIlluminaNode, Constants.SangerQualScore);
            string expectedSolexaQualScore = utilityObj.xmlUtil.GetTextValue(
                Constants.SangerToSolexaAndIlluminaNode, Constants.SolexaQualScore);
            string expectedIlluminaQualScore = utilityObj.xmlUtil.GetTextValue(
                Constants.SangerToSolexaAndIlluminaNode, Constants.IlluminaQualScore);

            string solexaQualScore   = null;
            string illuminaQualScore = null;

            byte[] scoreValue = Encoding.UTF8.GetBytes(sangerQualScore);

            // Create a Sanger qualitative sequence.
            QualitativeSequence sangerQualSequence = new QualitativeSequence(Alphabets.DNA,
                                                                             FastQFormatType.Sanger, sangerSequence, sangerQualScore);

            // Convert Sanger to Solexa.
            QualitativeSequence solexaQualSequence = sangerQualSequence.ConvertTo(
                FastQFormatType.Solexa_Illumina_v1_0);

            byte[] sangerToSolexa = QualitativeSequence.ConvertEncodedQualityScore(FastQFormatType.Sanger, FastQFormatType.Solexa_Illumina_v1_0, scoreValue);
            var    scores         = solexaQualSequence.GetEncodedQualityScores();

            solexaQualScore = Encoding.UTF8.GetString(scores, 0, scores.Length);

            // Validate converted solexa score.

            string qualSequence = new string(Encoding.UTF8.GetChars(sangerToSolexa));

            Assert.AreEqual(expectedSolexaQualScore, qualSequence);
            Assert.AreEqual(solexaQualScore, expectedSolexaQualScore);

            string solexaQualString = new string(solexaQualSequence.Select(a => (char)a).ToArray());

            Assert.AreEqual(solexaQualString, expectedSolexaSequence);

            ApplicationLog.WriteLine(string.Format(null,
                                                   "Qualitative Sequence BVT:Qualitative Solexa score type {0} is as expected.",
                                                   solexaQualScore));

            // Convert Sanger to Illumina.
            QualitativeSequence illuminaQualSequence = sangerQualSequence.ConvertTo(
                FastQFormatType.Illumina_v1_3);

            scores            = illuminaQualSequence.GetEncodedQualityScores();
            illuminaQualScore = Encoding.UTF8.GetString(scores, 0, scores.Length);

            byte[] sangerToIllumina = QualitativeSequence.ConvertEncodedQualityScore(FastQFormatType.Sanger, FastQFormatType.Illumina_v1_3, scoreValue);
            string illuminaQualSeq  = new string(illuminaQualSequence.Select(a => (char)a).ToArray());

            //// Validate converted illumina score.
            Assert.AreEqual(illuminaQualScore, expectedIlluminaQualScore);
            Assert.AreEqual(illuminaQualSeq, expectedIlluminaSequence);

            string sangerToIlluminaString = new string(Encoding.UTF8.GetChars(sangerToIllumina));

            Assert.AreEqual(expectedIlluminaQualScore, sangerToIlluminaString);

            ApplicationLog.WriteLine(string.Format((IFormatProvider)null,
                                                   "Qualitative Sequence BVT:Qualitative Illumina score type {0} is as expected.",
                                                   illuminaQualScore));
        }