Exemplo n.º 1
0
        /// <summary>
        /// Writes sequence alignment to specified stream.
        /// </summary>
        /// <param name="sequenceAlignment">Sequence alignment object</param>
        /// <param name="writer">Stream to write.</param>
        /// <param name="indexFile">BAMIndex file.</param>
        private void WriteSequenceAlignment(ISequenceAlignment sequenceAlignment, Stream writer, BAMIndexFile indexFile)
        {
            // validate sequenceAlignment.
            SequenceAlignmentMap sequenceAlignmentMap = ValidateAlignment(sequenceAlignment);

            string tempFilename = Path.GetTempFileName();

            // write bam struct to temp file.
            using (FileStream fstemp = new FileStream(tempFilename, FileMode.Create, FileAccess.ReadWrite))
            {
                WriteUncompressed(sequenceAlignmentMap, fstemp);

                fstemp.Seek(0, SeekOrigin.Begin);

                // Compress and write to the specified stream.
                CompressBAMFile(fstemp, writer);

                // if index file need to be created.
                if (indexFile != null)
                {
                    writer.Seek(0, SeekOrigin.Begin);
                    CreateIndexFile(writer, indexFile);
                }
            }

            // delete the temp file.
            File.Delete(tempFilename);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Write out the given SequenceAlignmentMap to the file
        /// </summary>
        /// <param name="formatter">BAMFormatter</param>
        /// <param name="sam">SequenceAlignmentMap</param>
        /// <param name="filename">File to write to</param>
        public static void Format(this BAMFormatter formatter, SequenceAlignmentMap sam, string filename)
        {
            if (formatter == null)
            {
                throw new ArgumentNullException("formatter");
            }
            if (sam == null)
            {
                throw new ArgumentNullException("sam");
            }
            if (string.IsNullOrWhiteSpace(filename))
            {
                throw new ArgumentNullException("filename");
            }

            using (var fs = File.Create(filename))
            {
                // Create the IndexFile if necessary
                if (formatter.CreateIndexFile)
                {
                    using (var bamIndexFile = new BAMIndexStorage(
                               File.Create(filename + Properties.Resource.BAM_INDEXFILEEXTENSION)))
                    {
                        formatter.Format(fs, bamIndexFile, sam);
                    }
                }
                else
                {
                    formatter.Format(fs, sam);
                }
            }
        }
Exemplo n.º 3
0
        public void ValidateVirtualSAMAlignedSequenceListContains()
        {
            // Get values from XML node.
            string filePath = _utilityObj._xmlUtil.GetTextValue(Constants.SAMFileWithAllFieldsNode,
                                                                Constants.FilePathNode1);

            // Parse a SAM file.
            using (SAMParser samParserObj = new SAMParser())
            {
                samParserObj.EnforceDataVirtualization = true;

                SequenceAlignmentMap       alignedSeqList = samParserObj.Parse(filePath);
                IList <SAMAlignedSequence> samAlignedList = alignedSeqList.QuerySequences;

                VirtualAlignedSequenceList <SAMAlignedSequence> virtualASeqList =
                    GetSAMAlignedSequence(Constants.SAMFileWithAllFieldsNode);

                // Validate contains.
                Assert.IsTrue(virtualASeqList.Contains(virtualASeqList.First(
                                                           Q => Q.QuerySequence.ToString().Equals(
                                                               samAlignedList[0].QuerySequence.ToString()))));

                // Log to Nunit GUI.
                ApplicationLog.WriteLine(string.Format((IFormatProvider)null,
                                                       "Virtual AlignedSequenceList P1 : VAS {0} is present in the virtualAlignedSequence List",
                                                       virtualASeqList[10]));
                Console.WriteLine(string.Format((IFormatProvider)null,
                                                "Virtual AlignedSequenceList P1 : VAS {0} is present in the virtualAlignedSequence List",
                                                virtualASeqList[10]));
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Writes specified alignment object to a file. The output is formatted according to the BAM specification.
        /// Also creates index file in the same location that of the specified filename.
        /// If the specified filename is sample.bam then the index file name will be sample.bam.bai.
        /// </summary>
        /// <param name="sequenceAlignmentMap">SequenceAlignmentMap object.</param>
        /// <param name="filename">BAM file name to write BAM data.</param>
        public void Format(SequenceAlignmentMap sequenceAlignmentMap, string filename)
        {
            if (sequenceAlignmentMap == null)
            {
                throw new ArgumentNullException("sequenceAlignmentMap");
            }

            if (string.IsNullOrWhiteSpace(filename))
            {
                throw new ArgumentNullException("filename");
            }

            using (FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.ReadWrite))
            {
                if (CreateIndexFile)
                {
                    using (BAMIndexFile bamIndexFile = new BAMIndexFile(filename + Resource.BAM_INDEXFILEEXTENSION, FileMode.Create, FileAccess.Write))
                    {
                        WriteSequenceAlignment(sequenceAlignmentMap, fs, bamIndexFile);
                    }
                }
                else
                {
                    WriteSequenceAlignment(sequenceAlignmentMap, fs, null);
                }
            }
        }
Exemplo n.º 5
0
        public void TestFormatterWithSort()
        {
            string               inputFilePath   = @"TestData\BAM\SeqAlignment.bam";
            string               outputFilePath1 = "output1.bam";
            string               outputFilePath2 = "output2.bam";
            BAMParser            parser          = new BAMParser();
            BAMFormatter         formatter       = new BAMFormatter();
            SequenceAlignmentMap alignmentMap    = parser.Parse(inputFilePath);

            Assert.IsTrue(alignmentMap != null);
            Assert.AreEqual(alignmentMap.Header.GetReferenceSequences().Count, 1);
            Assert.AreEqual(alignmentMap.QuerySequences.Count, 2);

            formatter.CreateSortedBAMFile = true;
            formatter.SortType            = BAMSortByFields.ChromosomeCoordinates;
            formatter.Format(alignmentMap, outputFilePath1);

            parser.EnforceDataVirtualization = true;
            alignmentMap = parser.Parse(inputFilePath);
            formatter.Format(alignmentMap, outputFilePath2);

            Assert.IsTrue(File.Exists(outputFilePath1));
            Assert.IsTrue(File.Exists(outputFilePath2));

            Assert.AreEqual(true, FileCompare(outputFilePath1, outputFilePath2));
        }
Exemplo n.º 6
0
        /// <summary>
        /// Dispalys the headers present in the BAM file
        /// </summary>
        /// <param name="seqAlignmentMap">SeqAlignment map</param>
        private void DisplayHeader(SequenceAlignmentMap seqAlignmentMap)
        {
            // Get Header
            SAMAlignmentHeader     header       = seqAlignmentMap.Header;
            IList <SAMRecordField> recordField  = header.RecordFields;
            IList <string>         commenstList = header.Comments;

            if (recordField.Count > 0)
            {
                Console.WriteLine("MetaData:");

                // Read Header Lines
                for (int i = 0; i < recordField.Count; i++)
                {
                    Console.Write("\n@{0}", recordField[i].Typecode);
                    for (int tags = 0; tags < recordField[i].Tags.Count; tags++)
                    {
                        Console.Write("\t{0}:{1}", recordField[i].Tags[tags].Tag,
                                      recordField[i].Tags[tags].Value);
                    }
                }
            }

            // Displays the comments if any
            if (commenstList.Count > 0)
            {
                for (int i = 0; i < commenstList.Count; i++)
                {
                    Console.Write("\n@CO\t{0}\n", commenstList[i].ToString());
                }
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Writes specified alignment object to a file.
        /// The output is formatted according to the BAM specification.
        /// </summary>
        /// <param name="sequenceAlignmentMap">SequenceAlignmentMap object.</param>
        /// <param name="bamFilename">BAM filename to write BAM data.</param>
        /// <param name="indexFilename">BAM index filename to write index data.</param>
        public void Format(SequenceAlignmentMap sequenceAlignmentMap, string bamFilename, string indexFilename)
        {
            if (sequenceAlignmentMap == null)
            {
                throw new ArgumentNullException("sequenceAlignmentMap");
            }

            if (string.IsNullOrWhiteSpace(bamFilename))
            {
                throw new ArgumentNullException("bamFilename");
            }

            if (string.IsNullOrWhiteSpace(indexFilename))
            {
                throw new ArgumentNullException("indexFilename");
            }

            if (bamFilename.Equals(indexFilename))
            {
                throw new ArgumentException(Resource.BAM_BAMFileNIndexFileContbeSame);
            }

            using (FileStream fs = new FileStream(bamFilename, FileMode.Create, FileAccess.ReadWrite))
            {
                using (BAMIndexFile bamIndexFile = new BAMIndexFile(indexFilename, FileMode.Create, FileAccess.Write))
                {
                    WriteSequenceAlignment(sequenceAlignmentMap, fs, bamIndexFile);
                }
            }
        }
Exemplo n.º 8
0
 /// <summary>
 /// Parse SAM or BAM file based on user input.
 /// </summary>
 private void DoParse()
 {
     if (!SAMInput)
     {
         BAMParser parse = new BAMParser();
         parse.EnforceDataVirtualization = true;
         try
         {
             _seqAlignmentMap = parse.Parse(InputFilePath);
         }
         catch
         {
             throw new InvalidOperationException(Resources.InvalidBAMFile);
         }
     }
     else
     {
         SAMParser parse = new SAMParser();
         parse.EnforceDataVirtualization = true;
         try
         {
             _seqAlignmentMap = parse.Parse(InputFilePath);
         }
         catch
         {
             throw new InvalidOperationException(Resources.InvalidSAMFile);
         }
     }
 }
Exemplo n.º 9
0
        /// <summary>
        /// Writes specified sequence alignment to stream.
        /// The output is formatted according to the BAM structure.
        /// </summary>
        /// <param name="sequenceAlignmentMap">SequenceAlignmentMap object.</param>
        /// <param name="writer">Stream to write.</param>
        /// <param name="createSortedFile">If this flag is true output file will be sorted.</param>
        private void WriteUncompressed(SequenceAlignmentMap sequenceAlignmentMap, Stream writer, bool createSortedFile)
        {
            SAMAlignmentHeader header = sequenceAlignmentMap.Header;

            if (createSortedFile && SortType == BAMSortByFields.ChromosomeNameAndCoordinates)
            {
                header            = GetHeaderWithSortedSQFields(header, true);
                this.refSequences = header.GetReferenceSequenceRanges();
            }

            if (this.refSequences == null)
            {
                this.refSequences = header.GetReferenceSequenceRanges();
            }

            WriteHeader(header, writer);
            writer.Flush();
            if (createSortedFile)
            {
                WriteUncompressedSortedBAM(sequenceAlignmentMap, writer);
            }
            else
            {
                foreach (SAMAlignedSequence seq in sequenceAlignmentMap.QuerySequences)
                {
                    SAMAlignedSequence alignedSeq = seq;
                    this.ValidateSQHeader(alignedSeq.RName);
                    this.WriteAlignedSequence(alignedSeq, writer);
                    writer.Flush();
                }
            }

            writer.Flush();
        }
Exemplo n.º 10
0
        /// <summary>
        /// Compare two SAM objects
        /// </summary>
        /// <param name="map">First SAM object.</param>
        /// <param name="map1">Second DAM object.</param>
        /// <returns>Whether two objects are equal or not.</returns>
        private static bool CompareSAM(SequenceAlignmentMap map, SequenceAlignmentMap map1)
        {
            bool comparison = false;

            if (map.Header.RecordFields.Count == map1.Header.RecordFields.Count &&
                map.Header.Comments.Count == map1.Header.Comments.Count)
            {
                if (map.Header.RecordFields.All(
                        a => map1.Header.RecordFields.Where(
                            b => a.Tags.All(
                                c => b.Tags.Where(
                                    d => c.Tag.Equals(d.Tag) && c.Value.Equals(d.Value)).ToList().Count > 0)).ToList().Count > 0))
                {
                    if (map.QuerySequences.Count == map1.QuerySequences.Count)
                    {
                        if (map.QuerySequences.AsParallel().All(e => map1.QuerySequences.Where(f => e.Bin == f.Bin && e.CIGAR == f.CIGAR).ToList().Count > 0))
                        {
                            comparison = true;
                        }
                    }
                }
            }

            return(comparison);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Validate BAM file Header fields.
        /// </summary>
        /// <param name="nodeName">XML nodename used for different test cases</param>
        /// <param name="seqAlignment">seqAlignment object</param>
        void ValidateBAMHeaderRecords(string nodeName,
                                      SequenceAlignmentMap seqAlignment)
        {
            string expectedHeaderTagValues = _utilityObj._xmlUtil.GetTextValue(
                nodeName, Constants.RecordTagValuesNode);
            string expectedHeaderTagKeys = _utilityObj._xmlUtil.GetTextValue(
                nodeName, Constants.RecordTagKeysNode);
            string expectedHeaderTypes = _utilityObj._xmlUtil.GetTextValue(
                nodeName, Constants.HeaderTyepsNodes);

            string[] expectedHeaderTagsValues = expectedHeaderTagValues.Split(',');
            string[] expectedHeaderKeys       = expectedHeaderTagKeys.Split(',');
            string[] expectedHeaders          = expectedHeaderTypes.Split(',');

            SAMAlignmentHeader     header       = seqAlignment.Header;
            IList <SAMRecordField> recordFields = header.RecordFields;
            int tagKeysCount   = 0;
            int tagValuesCount = 0;

            for (int index = 0; index < recordFields.Count; index++)
            {
                Assert.AreEqual(expectedHeaders[index].Replace("/", ""),
                                recordFields[index].Typecode.ToString((IFormatProvider)null).Replace("/", ""));
                for (int tags = 0; tags < recordFields[index].Tags.Count; tags++)
                {
                    Assert.AreEqual(expectedHeaderKeys[tagKeysCount].Replace("/", ""),
                                    recordFields[index].Tags[tags].Tag.ToString((IFormatProvider)null).Replace("/", ""));
                    Assert.AreEqual(expectedHeaderTagsValues[tagValuesCount].Replace("/", ""),
                                    recordFields[index].Tags[tags].Value.ToString((IFormatProvider)null).Replace("/", "").Replace("\r", "").Replace("\n", ""));
                    tagKeysCount++;
                    tagValuesCount++;
                }
            }
        }
Exemplo n.º 12
0
        /// <summary>
        /// General method to validate SAM parser method.
        /// </summary>
        /// <param name="nodeName">xml node name</param>
        /// <param name="parseTypes">enum type to execute different overload</param>
        void ValidateSAMParser(string nodeName)
        {
            // Gets the expected sequence from the Xml
            string filePath = Utility._xmlUtil.GetTextValue(
                nodeName, Constants.FilePathNode);
            string expectedSequenceFile = Utility._xmlUtil.GetTextValue(
                nodeName, Constants.ExpectedSequence);
            SAMParser            parser     = new SAMParser();
            SequenceAlignmentMap alignments = null;

            // Parse SAM File
            using (TextReader reader = new StreamReader(filePath))
            {
                alignments = parser.Parse(reader);
            }

            // Get expected sequences
            FastaParser       parserObj         = new FastaParser();
            IList <ISequence> expectedSequences =
                parserObj.Parse(expectedSequenceFile);

            // 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(expectedSequences[index].ToString(),
                                    alignments.QuerySequences[index].Sequences[count].ToString());
                }
            }
        }
Exemplo n.º 13
0
        public void PerformBAMParserPerf()
        {
            // Get BAM file path.
            string bamFilePath =
                Utility._xmlUtil.GetTextValue(Constants.BAMParserNode,
                                              Constants.FilePathNode);

            // Create a List for input files.
            List <string> lstInputFiles = new List <string>();

            lstInputFiles.Add(bamFilePath);

            // Create a BAM Parser object
            BAMParser parseBam = new BAMParser();

            // Enable DV.
            parseBam.EnforceDataVirtualization = true;

            _watchObj = new Stopwatch();
            _watchObj.Reset();
            _watchObj.Start();
            long memoryStart           = GC.GetTotalMemory(false);
            SequenceAlignmentMap align = parseBam.Parse(bamFilePath);

            _watchObj.Stop();
            long memoryEnd = GC.GetTotalMemory(false);

            string memoryUsed = (memoryEnd - memoryStart).ToString();

            // Display BAMParser perf test case execution details.
            DisplayTestCaseHeader(lstInputFiles, _watchObj, memoryUsed,
                                  null);
        }
Exemplo n.º 14
0
 public void TestSNPDetectionUsingBAMFile()
 {
     using (BAMParser parser = new BAMParser())
     {
         SequenceAlignmentMap map = parser.Parse(@"TestUtils\BAM\PairedReadsTest.bam");
         TestCoverage("chr1", map, "true");
     }
 }
Exemplo n.º 15
0
 public void TestCoverageProfileUsingSAMFile()
 {
     using (SAMParser parser = new SAMParser())
     {
         SequenceAlignmentMap map = parser.Parse(@"TestUtils\SAM\PairedReadsTest.sam");
         TestCoverage("chr1", map, "true");
     }
 }
Exemplo n.º 16
0
 public void ValidateSAMParserWithEmptyAlignmentMap()
 {
     SAMParser parser = new SAMParser();
     {
         SequenceAlignmentMap alignment = parser.ParseOne <SequenceAlignmentMap>(utilityObj.xmlUtil.GetTextValue(Constants.EmptySamFileNode, Constants.FilePathNode));
         Assert.IsNotNull(alignment);
     }
 }
Exemplo n.º 17
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 ValidateSAMFormatterSeqAlign(
            string nodeName,
            ParseOrFormatTypes parseTypes)
        {
            // Gets the expected sequence from the Xml
            string filePath = _utilityObj._xmlUtil.GetTextValue(
                nodeName, Constants.FilePathNode);
            string expectedSequenceFile = _utilityObj._xmlUtil.GetTextValue(
                nodeName, Constants.ExpectedSequence);

            using (SAMParser parser = new SAMParser())
            {
                SequenceAlignmentMap alignments = parser.Parse(filePath);
                SAMFormatter         formatter  = new SAMFormatter();
                switch (parseTypes)
                {
                case ParseOrFormatTypes.ParseOrFormatText:
                    using (TextWriter writer =
                               new StreamWriter(Constants.SAMTempFileName))
                    {
                        formatter.Format(alignments, writer);
                    }
                    break;

                case ParseOrFormatTypes.ParseOrFormatFileName:
                    formatter.Format(alignments, Constants.SAMTempFileName);
                    break;

                case ParseOrFormatTypes.ParseOrFormatFileNameWithFlag:
                    formatter.Format(alignments, Constants.SAMTempFileName);
                    break;
                }

                alignments = parser.Parse(Constants.SAMTempFileName);

                // Get expected sequences
                using (FastaParser parserObj = new FastaParser())
                {
                    IList <ISequence> expectedSequences =
                        parserObj.Parse(expectedSequenceFile);

                    // 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(expectedSequences[index].ToString(),
                                            alignments.QuerySequences[index].Sequences[count].ToString());
                        }
                    }
                }
            }
        }
Exemplo n.º 18
0
        public void TestParser()
        {
            string               filePath     = @"TestData\BAM\SeqAlignment.bam";
            BAMParser            parser       = new BAMParser();
            SequenceAlignmentMap alignmentMap = parser.Parse(filePath);

            Assert.IsTrue(alignmentMap != null);
            Assert.AreEqual(alignmentMap.Header.GetReferenceSequences().Count, 1);
            Assert.AreEqual(alignmentMap.QuerySequences.Count, 2);
        }
Exemplo n.º 19
0
        public void TestOrphanRegionssUsingSAMFile()
        {
            string samFilename = @"TestUtils\SAM\PairedReadsTest.sam";

            using (SAMParser bamParser = new SAMParser())
            {
                SequenceAlignmentMap alignmentMapobj = bamParser.Parse(samFilename);
                TestOrphanRegions(alignmentMapobj);
            }
        }
Exemplo n.º 20
0
        public void TestOrphansRegionsUsingBAMFile()
        {
            string bamFilename = @"TestUtils\BAM\PairedReadsTest.bam";

            using (BAMParser bamParser = new BAMParser())
            {
                SequenceAlignmentMap alignmentMapobj = bamParser.Parse(bamFilename);
                TestOrphanRegions(alignmentMapobj);
            }
        }
Exemplo n.º 21
0
        public void TestLengthAnomaliesUsingBAMfile()
        {
            string bamFilename = @"TestUtils\BAM\PairedReadsTest.bam";

            using (BAMParser bamParser = new BAMParser())
            {
                SequenceAlignmentMap alignmentMapobj = bamParser.Parse(bamFilename);
                TestLengthAnomalies(alignmentMapobj);
            }
        }
Exemplo n.º 22
0
        public void TestChimeraDataUsingSAMfile()
        {
            string samFilename = @"TestUtils\SAM\PairedReadsTest.sam";

            using (SAMParser bamParser = new SAMParser())
            {
                SequenceAlignmentMap alignmentMapobj = bamParser.Parse(samFilename);
                TestChimeraData(alignmentMapobj);
            }
        }
Exemplo n.º 23
0
        public void ValidateSAMParserWithEmptyAlignmentMap()
        {
            SAMParser            parser    = new SAMParser();
            SequenceAlignmentMap alignment =
                parser.Parse(Utility._xmlUtil.GetTextValue(
                                 Constants.EmptySamFileNode,
                                 Constants.FilePathNode));

            Assert.AreEqual(null, alignment);
        }
Exemplo n.º 24
0
        public void TestALLtypePairedReadsInSAMFile()
        {
            string samfilePath = @"TestUtils\SAM\PairedReadsTest.sam";

            using (SAMParser parser = new SAMParser())
            {
                SequenceAlignmentMap map = parser.Parse(samfilePath);

                int TotalReadsCount       = 77;
                int unpairedReadsCount    = 2;
                int multipleHitsPairCount = 2;
                int multipleHitReadsCount = 8;
                int normalPairCount       = 4;
                int normalreadsCount      = 2 * normalPairCount;
                int orphanPairsCount      = 3;
                int orphanreadsCount      = 5;
                int chimerapaircount      = 15;
                int chimerareadsCount     = 2 * chimerapaircount;
                int strucAnomPairCount    = 3;
                int strucAnomReadsCount   = 2 * strucAnomPairCount;
                int lenAnomPairCount      = 9;
                int LenAnomReadsCount     = 2 * lenAnomPairCount;

                int total = unpairedReadsCount + multipleHitReadsCount + normalreadsCount + orphanreadsCount + chimerareadsCount + strucAnomReadsCount + LenAnomReadsCount;
                Assert.AreEqual(TotalReadsCount, total);

                IList <PairedRead> reads        = map.GetPairedReads(200, 50);
                IList <PairedRead> multipleHits = reads.Where(PE => PE.PairedType == PairedReadType.MultipleHits).ToList();
                IList <PairedRead> normal       = reads.Where(PE => PE.PairedType == PairedReadType.Normal).ToList();
                IList <PairedRead> orphan       = reads.Where(PE => PE.PairedType == PairedReadType.Orphan).ToList();
                IList <PairedRead> chimera      = reads.Where(PE => PE.PairedType == PairedReadType.Chimera).ToList();
                IList <PairedRead> strucAnom    = reads.Where(PE => PE.PairedType == PairedReadType.StructuralAnomaly).ToList();
                IList <PairedRead> lenAnom      = reads.Where(PE => PE.PairedType == PairedReadType.LengthAnomaly).ToList();

                Assert.AreEqual(TotalReadsCount, map.QuerySequences.Count);

                Assert.AreEqual(multipleHitsPairCount, multipleHits.Count());
                Assert.AreEqual(multipleHitReadsCount, multipleHits.Sum(PE => PE.Reads.Count));

                Assert.AreEqual(normalPairCount, normal.Count());
                Assert.AreEqual(normalreadsCount, normal.Sum(PE => PE.Reads.Count));

                Assert.AreEqual(orphanPairsCount, orphan.Count());
                Assert.AreEqual(orphanreadsCount, orphan.Sum(PE => PE.Reads.Count));

                Assert.AreEqual(chimerapaircount, chimera.Count());
                Assert.AreEqual(chimerareadsCount, chimera.Sum(PE => PE.Reads.Count));

                Assert.AreEqual(strucAnomPairCount, strucAnom.Count());
                Assert.AreEqual(strucAnomReadsCount, strucAnom.Sum(PE => PE.Reads.Count));

                Assert.AreEqual(lenAnomPairCount, lenAnom.Count());
                Assert.AreEqual(LenAnomReadsCount, lenAnom.Sum(PE => PE.Reads.Count));
            }
        }
Exemplo n.º 25
0
        public void TestFormatter()
        {
            string    filePath       = @"TestUtils\BAM\SeqAlignment.bam";
            string    outputfilePath = "BAMTests123.bam";
            BAMParser parser         = null;

            try
            {
                parser = new BAMParser();
                BAMFormatter         formatter    = new BAMFormatter();
                SequenceAlignmentMap alignmentMap = parser.Parse(filePath);

                Assert.IsTrue(alignmentMap != null);
                Assert.AreEqual(alignmentMap.Header.GetReferenceSequencesInfoFromSQHeader().Count, 1);
                Assert.AreEqual(alignmentMap.Header.ReferenceSequences.Count, 1);
                Assert.AreEqual(alignmentMap.QuerySequences.Count, 2);

                formatter.Format(alignmentMap, outputfilePath);

                formatter.CreateSortedBAMFile = true;
                formatter.CreateIndexFile     = true;
                alignmentMap = parser.Parse(filePath);
                formatter.Format(alignmentMap, outputfilePath);

                Assert.IsTrue(File.Exists("BAMTests123.bam.bai"));

                alignmentMap = parser.Parse(outputfilePath);

                Assert.IsTrue(alignmentMap != null);
                Assert.AreEqual(alignmentMap.Header.GetReferenceSequencesInfoFromSQHeader().Count, 1);
                Assert.AreEqual(alignmentMap.Header.ReferenceSequences.Count, 1);
                Assert.AreEqual(alignmentMap.QuerySequences.Count, 2);

                alignmentMap = parser.ParseRange("BAMTests123.bam", new SequenceRange("chr20", 0, int.MaxValue));

                Assert.IsTrue(alignmentMap != null);
                Assert.AreEqual(alignmentMap.Header.GetReferenceSequencesInfoFromSQHeader().Count, 1);
                Assert.AreEqual(alignmentMap.Header.ReferenceSequences.Count, 1);
                Assert.AreEqual(alignmentMap.QuerySequences.Count, 2);

                alignmentMap = parser.ParseRange("BAMTests123.bam", new SequenceRange("chr20", 0, 28833));

                Assert.IsTrue(alignmentMap != null);
                Assert.AreEqual(alignmentMap.Header.GetReferenceSequencesInfoFromSQHeader().Count, 1);
                Assert.AreEqual(alignmentMap.Header.ReferenceSequences.Count, 1);
                Assert.AreEqual(alignmentMap.QuerySequences.Count, 1);
            }
            finally
            {
                if (parser != null)
                {
                    parser.Dispose();
                }
            }
        }
Exemplo n.º 26
0
        // Validates the alignment.
        private SequenceAlignmentMap ValidateAlignment(ISequenceAlignment sequenceAlignment)
        {
            SequenceAlignmentMap seqAlignmentMap = sequenceAlignment as SequenceAlignmentMap;

            if (seqAlignmentMap != null)
            {
                ValidateAlignmentHeader(seqAlignmentMap.Header);
                if (CreateSortedBAMFile && SortType == BAMSortByFields.ChromosomeNameAndCoordinates)
                {
                    this.refSequences = SortSequenceRanges(seqAlignmentMap.Header.GetReferenceSequenceRanges());
                }
                else
                {
                    this.refSequences = seqAlignmentMap.Header.GetReferenceSequenceRanges();
                }

                return(seqAlignmentMap);
            }

            SAMAlignmentHeader header = sequenceAlignment.Metadata[Helper.SAMAlignmentHeaderKey] as SAMAlignmentHeader;

            if (header == null)
            {
                throw new ArgumentException(Properties.Resource.SAMAlignmentHeaderNotFound);
            }

            ValidateAlignmentHeader(header);

            seqAlignmentMap = new SequenceAlignmentMap(header);
            if (CreateSortedBAMFile && SortType == BAMSortByFields.ChromosomeNameAndCoordinates)
            {
                this.refSequences = SortSequenceRanges(seqAlignmentMap.Header.GetReferenceSequenceRanges());
            }
            else
            {
                this.refSequences = seqAlignmentMap.Header.GetReferenceSequenceRanges();
            }

            foreach (IAlignedSequence alignedSeq in sequenceAlignment.AlignedSequences)
            {
                SAMAlignedSequenceHeader alignedHeader = alignedSeq.Metadata[Helper.SAMAlignedSequenceHeaderKey] as SAMAlignedSequenceHeader;
                if (alignedHeader == null)
                {
                    throw new ArgumentException(Properties.Resource.SAMAlignedSequenceHeaderNotFound);
                }

                SAMAlignedSequence samAlignedSeq = new SAMAlignedSequence(alignedHeader);
                samAlignedSeq.QuerySequence = alignedSeq.Sequences[0];
                seqAlignmentMap.QuerySequences.Add(samAlignedSeq);
            }

            return(seqAlignmentMap);
        }
Exemplo n.º 27
0
        /// <summary>
        /// General method to validate SAM parser method.
        /// </summary>
        /// <param name="nodeName">xml node name</param>
        /// <param name="parseTypes">enum type to execute different overload</param>
        void ValidateSAMParserSeqAlign(
            string nodeName,
            ParseOrFormatTypes method)
        {
            // Gets the expected sequence from the Xml
            string filePath = utilityObj.xmlUtil.GetTextValue(
                nodeName, Constants.FilePathNode);
            string expectedSequenceFile = utilityObj.xmlUtil.GetTextValue(
                nodeName, Constants.ExpectedSequence);

            using (SAMParser parser = new SAMParser())
            {
                SequenceAlignmentMap alignments = null;

                // Parse SAM File
                switch (method)
                {
                case ParseOrFormatTypes.ParseOrFormatText:
                    using (TextReader reader = new StreamReader(filePath))
                    {
                        alignments = parser.Parse(reader);
                    }
                    break;

                case ParseOrFormatTypes.ParseOrFormatFileName:
                    alignments = parser.Parse(filePath);
                    break;
                }

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

                    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()));
                        }
                    }
                }
            }
        }
Exemplo n.º 28
0
        // Validates the alignment.
        private SequenceAlignmentMap ValidateAlignment(ISequenceAlignment sequenceAlignment)
        {
            SequenceAlignmentMap seqAlignmentMap = sequenceAlignment as SequenceAlignmentMap;

            if (seqAlignmentMap != null)
            {
                ValidateAlignmentHeader(seqAlignmentMap.Header);
                _refSequences = SortSequenceRanges(seqAlignmentMap.Header.GetReferenceSequenceRanges());

                foreach (SAMAlignedSequence alignedSequence in seqAlignmentMap.QuerySequences)
                {
                    string message = alignedSequence.IsValidHeader();
                    if (!string.IsNullOrEmpty(message))
                    {
                        throw new ArgumentException(message);
                    }

                    ValidateSQHeader(alignedSequence.RName);
                }

                return(seqAlignmentMap);
            }

            SAMAlignmentHeader header = sequenceAlignment.Metadata[Helper.SAMAlignmentHeaderKey] as SAMAlignmentHeader;

            if (header == null)
            {
                throw new ArgumentException(Resource.SAMAlignmentHeaderNotFound);
            }

            ValidateAlignmentHeader(header);

            seqAlignmentMap = new SequenceAlignmentMap(header);
            _refSequences   = SortSequenceRanges(seqAlignmentMap.Header.GetReferenceSequenceRanges());

            foreach (IAlignedSequence alignedSeq in sequenceAlignment.AlignedSequences)
            {
                SAMAlignedSequenceHeader alignedHeader = alignedSeq.Metadata[Helper.SAMAlignedSequenceHeaderKey] as SAMAlignedSequenceHeader;
                if (alignedHeader == null)
                {
                    throw new ArgumentException(Resource.SAMAlignedSequenceHeaderNotFound);
                }

                ValidateAlignedSequenceHeader(alignedHeader);
                ValidateSQHeader(alignedHeader.RName);

                SAMAlignedSequence samAlignedSeq = new SAMAlignedSequence(alignedHeader);
                samAlignedSeq.QuerySequence = alignedSeq.Sequences[0];
            }

            return(seqAlignmentMap);
        }
Exemplo n.º 29
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 ValidateSAMFormatterSeqAlign(
            string nodeName,
            ParseOrFormatTypes parseTypes)
        {
            // Gets the expected sequence from the Xml
            string filePath = utilityObj.xmlUtil.GetTextValue(
                nodeName, Constants.FilePathNode).TestDir();
            string expectedSequenceFile = utilityObj.xmlUtil.GetTextValue(
                nodeName, Constants.ExpectedSequence).TestDir();
            SAMParser parser = new SAMParser();
            {
                SequenceAlignmentMap alignments = parser.ParseOne <SequenceAlignmentMap>(filePath);
                SAMFormatter         formatter  = new SAMFormatter();
                switch (parseTypes)
                {
                case ParseOrFormatTypes.ParseOrFormatText:
                    using (var writer =
                               File.Create(Constants.SAMTempFileName))
                    {
                        formatter.Format(writer, alignments);
                    }
                    break;

                case ParseOrFormatTypes.ParseOrFormatFileName:
                    formatter.Format(alignments, Constants.SAMTempFileName);
                    break;
                }

                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()));
                        }
                    }
                }
            }
        }
Exemplo n.º 30
0
        /// <summary>
        /// Writes specified alignment object to a stream.
        /// The output is formatted according to the BAM specification.
        /// Note that this method does not create index file.
        /// </summary>
        /// <param name="sequenceAlignmentMap">SequenceAlignmentMap object.</param>
        /// <param name="writer">Stream to write BAM data.</param>
        public void Format(SequenceAlignmentMap sequenceAlignmentMap, Stream writer)
        {
            if (sequenceAlignmentMap == null)
            {
                throw new ArgumentNullException("sequenceAlignmentMap");
            }

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

            WriteSequenceAlignment(sequenceAlignmentMap, writer, null);
        }