Beispiel #1
0
        public void TestFormatter()
        {
            string filePath = @"TestUtils\SAM\SeqAlignment1.sam";
            string outputfilePath = "samtest.sam";
            ISequenceAlignmentParser parser = new SAMParser();
            IList<ISequenceAlignment> alignments = parser.Parse(filePath).ToList();

            Assert.IsTrue(alignments != null);
            Assert.AreEqual(alignments.Count, 1);
            Assert.AreEqual(alignments[0].AlignedSequences.Count, 2);

            try
            {
                SAMFormatter formatter = new SAMFormatter();
                formatter.Format(alignments[0], outputfilePath);

                alignments = parser.Parse(outputfilePath).ToList();

                Assert.IsTrue(alignments != null);
                Assert.AreEqual(alignments.Count, 1);
                Assert.AreEqual(alignments[0].AlignedSequences.Count, 2);
            }
            finally
            {
                File.Delete(outputfilePath);
            }
        }
Beispiel #2
0
        /// <summary>
        /// General method to validate SAM Formatter method.
        /// </summary>
        /// <param name="nodeName">xml node name</param>
        /// <param name="parseTypes">enum type to execute different overload</param>
        void ValidateSAMFormatter(string nodeName)
        {
            // Gets the expected sequence from the Xml
            string filePath = utilityObj.xmlUtil.GetTextValue(
                nodeName, Constants.FilePathNode);
            string expectedSequenceFile = utilityObj.xmlUtil.GetTextValue(
                nodeName, Constants.ExpectedSequence);
            SAMParser parser = new SAMParser();
            {
                SequenceAlignmentMap alignments = (SequenceAlignmentMap) parser.ParseOne(filePath);
                SAMFormatter formatter = new SAMFormatter();

                using (var writer =
                            File.Create(Constants.SAMTempFileName))
                {
                    formatter.Format(writer, alignments);
                }

                alignments = parser.ParseOne<SequenceAlignmentMap>(Constants.SAMTempFileName);

                // Get expected sequences
                FastAParser parserObj = new FastAParser();
                {
                    IEnumerable<ISequence> expectedSequences =
                        parserObj.Parse(expectedSequenceFile);

                    IList<ISequence> expectedSequencesList = expectedSequences.ToList();

                    // Validate parsed output with expected output
                    for (int index = 0;
                        index < alignments.QuerySequences.Count;
                        index++)
                    {
                        for (int count = 0;
                            count < alignments.QuerySequences[index].Sequences.Count;
                            count++)
                        {
                            Assert.AreEqual(
                                new string(expectedSequencesList[index].Select(a => (char)a).ToArray()),
                                new string(alignments.QuerySequences[index].Sequences[count].Select(a => (char)a).ToArray()));
                        }
                    }
                }
            }
        }
Beispiel #3
0
 /// <summary>
 /// Writes the SAM object to file in SAM/BAM format.
 /// </summary>
 private void PerformFormat()
 {
     if (_isSAM)
     {
         BAMFormatter format = new BAMFormatter();
         try
         {
             format.Format(_sequenceAlignmentMap, OutputFilename);
         }
         catch (Exception ex)
         {
             throw new InvalidOperationException(Resources.WriteBAM + Environment.NewLine + ex.Message);
         }
     }
     else
     {
         SAMFormatter format = new SAMFormatter();
         try
         {
             format.Format(_sequenceAlignmentMap, OutputFilename);
         }
         catch (Exception ex)
         {
             throw new InvalidOperationException(Resources.WriteSAM + Environment.NewLine + ex.Message);
         }
     }
 }
Beispiel #4
0
        /// <summary>
        /// Validate formatter all format method overloads with filePath\textwriter
        /// </summary>
        /// <param name="nodeName">xml node name</param>
        /// <param name="formatTypes">enum type to execute different overload</param>
        void ValidateSAMFormatter(string nodeName,
            ParseOrFormatTypes formatTypes)
        {
            // Gets the expected sequence from the Xml
            string filePath = utilityObj.xmlUtil.GetTextValue(
                nodeName, Constants.FilePathNode);
            string expectedSequenceFile = utilityObj.xmlUtil.GetTextValue(
                nodeName, Constants.ExpectedSequence);
            ISequenceAlignmentParser parser = new SAMParser();
            try
            {
                IList<ISequenceAlignment> alignments = parser.Parse(filePath).ToList();
                SAMFormatter formatter = new SAMFormatter();
                switch (formatTypes)
                {
                    case ParseOrFormatTypes.ParseOrFormatText:
                        using (var writer = File.Create(Constants.SAMTempFileName))
                        {
                            formatter.Format(writer, alignments[0]);
                        }
                        break;
                    case ParseOrFormatTypes.ParseOrFormatFileName:
                        formatter.Format(alignments[0], Constants.SAMTempFileName);
                        break;
                }
                alignments = parser.Parse(Constants.SAMTempFileName).ToList();

                // Get expected sequences
                FastAParser parserObj = new FastAParser();
                {
                    IEnumerable<ISequence> expectedSequences = parserObj.Parse(expectedSequenceFile);
                    IList<ISequence> expectedSequencesList = expectedSequences.ToList();

                    // Validate parsed output with expected output
                    int count = 0;
                    for (int index = 0; index < alignments.Count; index++)
                    {
                        for (int ialigned = 0; ialigned <
                            alignments[index].AlignedSequences.Count; ialigned++)
                        {
                            for (int iseq = 0; iseq <
                                alignments[index].AlignedSequences[ialigned].Sequences.Count; iseq++)
                            {
                                Assert.AreEqual(new string(expectedSequencesList[count].Select(a => (char)a).ToArray()),
                                    new string(alignments[index].AlignedSequences[ialigned].Sequences[iseq].Select(a => (char)a).ToArray()));
                                count++;
                            }
                        }
                    }
                }
            }
            finally
            {
            }
        }
Beispiel #5
0
 public void ValidateSAMFormatterWithTextWriterAndAlignments()
 {
     // Gets the expected sequence from the Xml
     string filePath = utilityObj.xmlUtil.GetTextValue(
         Constants.SmallSAMFileNode, Constants.FilePathNode);
     ISequenceAlignmentParser parser = new SAMParser();
     IEnumerable<ISequenceAlignment> alignments = parser.Parse(filePath);
     SAMFormatter formatter = new SAMFormatter();
     try
     {
         using (var writer = File.Create(Constants.SAMTempFileName))
         {
             formatter.Format(writer, alignments);
         }
         Assert.Fail();
     }
     catch (NotSupportedException)
     {
         ApplicationLog.WriteLine("SAM Parser BVT : Validated the exception successfully");
     }
 }
Beispiel #6
0
 public void ValidateSAMFormatterWithFileNameAndAlignments()
 {
     // Gets the expected sequence from the Xml
     string filePath = utilityObj.xmlUtil.GetTextValue(
         Constants.SmallSAMFileNode.Replace("\r\n", System.Environment.NewLine), Constants.FilePathNode);
     ISequenceAlignmentParser parser = new SAMParser();
     IEnumerable<ISequenceAlignment> alignments = parser.Parse(filePath);
     SAMFormatter formatter = new SAMFormatter();
     try
     {
         formatter.Format(alignments, Constants.SAMTempFileName);
         Assert.Fail();
     }
     catch (NotSupportedException)
     {
         ApplicationLog.WriteLine("SAM Parser BVT : Validated the exception successfully");
     }
 }
Beispiel #7
0
        public void ValidateBAMToSAMConversion()
        {
            // Get values from xml config file.
            string expectedSamFilePath = this.utilityObj.xmlUtil.GetTextValue(Constants.BAMToSAMConversionNode,
                                                                         Constants.FilePathNode1);
            string bamFilePath = this.utilityObj.xmlUtil.GetTextValue(Constants.BAMToSAMConversionNode,
                                                                 Constants.FilePathNode);

            var samParserObj = new SAMParser();
            SequenceAlignmentMap expextedSamAlignmentObj = samParserObj.ParseOne<SequenceAlignmentMap>(expectedSamFilePath);

            var bamParserObj = new BAMParser();
            SequenceAlignmentMap bamSeqAlignment = bamParserObj.ParseOne<SequenceAlignmentMap>(bamFilePath);

            try
            {
                // Format BAM sequenceAlignment object to SAM file.
                var samFormatterObj = new SAMFormatter();
                samFormatterObj.Format(bamSeqAlignment, Constants.SAMTempFileName);
                SequenceAlignmentMap samSeqAlignment = samParserObj.ParseOne<SequenceAlignmentMap>(Constants.SAMTempFileName);

                Assert.IsTrue(CompareSequencedAlignmentHeader(samSeqAlignment, expextedSamAlignmentObj));
                Assert.IsTrue(CompareAlignedSequences(samSeqAlignment, expextedSamAlignmentObj));
            }
            finally
            {
                // Delete temporary file.
                File.Delete(Constants.SAMTempFileName);
            }
        }
Beispiel #8
0
        /// <summary>
        /// General method to validate BAM to SAM conversion.
        /// </summary>
        /// <param name="nodeName">Different nodeName used for different test cases.</param>
        void ValidateBAMToSAMConversion(string nodeName)
        {
            // Get values from xml config file.
            string expectedSamFilePath = this.utilityObj.xmlUtil.GetTextValue(nodeName,
               Constants.FilePathNode1);
            string BAMStoragePath = this.utilityObj.xmlUtil.GetTextValue(nodeName,
                Constants.FilePathNode);

            BAMParser bamParserObj = new BAMParser();
            SAMParser samParserObj = new SAMParser();
            SAMFormatter samFormatterObj = new SAMFormatter();
            SequenceAlignmentMap samSeqAlignment = null;
            SequenceAlignmentMap bamSeqAlignment = null;

            // Parse expected SAM file.
            SequenceAlignmentMap expectedSamAlignmentObj = samParserObj.ParseOne<SequenceAlignmentMap>(expectedSamFilePath);

            // Parse a BAM file.
            bamSeqAlignment = bamParserObj.ParseOne<SequenceAlignmentMap>(BAMStoragePath);

            try
            {
                // Format BAM sequenceAlignment object to SAM file.
                samFormatterObj.Format(bamSeqAlignment, Constants.SAMTempFileName);

                // Parse a formatted SAM file.
                samSeqAlignment = samParserObj.ParseOne<SequenceAlignmentMap>(Constants.SAMTempFileName);

                // Validate converted SAM file with expected SAM file.
                Assert.IsTrue(CompareSequencedAlignmentHeader(samSeqAlignment,
                    expectedSamAlignmentObj));

                // Validate SAM file aligned sequences.
                Assert.IsTrue(CompareAlignedSequences(samSeqAlignment,
                    expectedSamAlignmentObj));

                // Log message to VSTest GUI.
                ApplicationLog.WriteLine(string.Format((IFormatProvider)null,
                    "BAM Parser P1 : Validated the BAM->SAM conversion successfully"));
            }
            finally
            {
                // Delete temporary file.
                File.Delete(Constants.SAMTempFileName);
                ApplicationLog.WriteLine("Deleted the temp file created.");
            }
        }