public void IntervalJumping_Ends()
        {
            var smallBam     = Path.Combine(UnitTestPaths.TestDataDirectory, "Ins-L3-var12_S12.bam");
            var intervals    = new Dictionary <string, List <Region> >();
            var chrIntervals = new List <Region>
            {
                new Region(28608100, 28608100),  // interval in the middle of coverage
                new Region(29608700, 29608800)   // interval out in the boonies where there's no data
            };

            intervals.Add("chr13", chrIntervals);
            var extractor = new BamFileAlignmentExtractor(smallBam, bamIntervals: intervals);

            var read                = new Read();
            var numReadsLessThan    = 0;
            var numReadsGreaterThan = 0;

            // verify we are always moving forward and not backwards (not re-reading alignments)
            while (extractor.GetNextAlignment(read))
            {
                if (read.EndPosition + 1 < 28608100) // bam reader is off by one, see note in BamFileExtractor.Jump
                {
                    numReadsLessThan++;
                }
                else if (read.Position > 28608100)
                {
                    numReadsGreaterThan++;
                }
            }

            Assert.Equal(1, numReadsLessThan);  // this should be just the first read (before we figure out we're not in range)
            Assert.Equal(0, numReadsGreaterThan);
        }
        private void ReadFileTest(string bamfile, int expectedReads, bool bamHasXc)
        {
            var extractor = new BamFileAlignmentExtractor(bamfile);

            var read         = new Read();
            var lastPosition = -1;
            var numReads     = 0;

            bool hasAnyStitchedCigars = false;

            while (extractor.GetNextAlignment(read))
            {
                Assert.True(read.Position >= lastPosition); // make sure reads are read in order
                Assert.False(string.IsNullOrEmpty(read.Name));
                Assert.False(string.IsNullOrEmpty(read.Chromosome));

                if (!bamHasXc)
                {
                    Assert.Equal(null, read.StitchedCigar);
                }
                if (read.StitchedCigar != null && read.StitchedCigar.Count > 0)
                {
                    hasAnyStitchedCigars = true;
                }
                lastPosition = read.Position;
                numReads++;
            }

            if (bamHasXc)
            {
                Assert.True(hasAnyStitchedCigars);
            }
            Assert.Equal(expectedReads, numReads);
            extractor.Dispose();

            // make sure can't read after dispose
            Assert.Throws <IOException>(() => extractor.GetNextAlignment(read));
        }
        public void UnalignedReads()
        {
            var extractor = new BamFileAlignmentExtractor(Path.Combine(TestPaths.LocalTestDataDirectory, "unaligned.bam"));

            var read  = new Read();
            var count = 0;

            while (extractor.GetNextAlignment(read))
            {
                count++;
            }

            Assert.Equal(138826, count);
            Assert.Equal(null, read.Chromosome); // last reads are unaligned
        }
        public void ReadFileAsStitched()
        {
            var smallBam = Path.Combine(TestPaths.LocalTestDataDirectory, "small.bam");

            //we claim its not stitched
            var extractor = new BamFileAlignmentExtractor(smallBam, false);
            var read      = new Read();

            extractor.GetNextAlignment(read);
            Assert.False(extractor.SourceIsStitched);


            //we claim its stitched
            extractor = new BamFileAlignmentExtractor(smallBam, true);
            read      = new Read();
            extractor.GetNextAlignment(read);
            Assert.True(extractor.SourceIsStitched);
        }
        public void IntervalJumping_Boundaries()
        {
            var smallBam     = Path.Combine(UnitTestPaths.TestDataDirectory, "Ins-L3-var12_S12.bam");
            var intervals    = new Dictionary <string, List <Region> >();
            var chrIntervals = new List <Region>
            {
                new Region(115169880, 115169880)  // feeding in an interval that's past reference max shouldnt cause it to blow up
            };

            intervals.Add("chr13", chrIntervals);
            var extractor = new BamFileAlignmentExtractor(smallBam, bamIntervals: intervals);

            var read = new Read();

            while (extractor.GetNextAlignment(read))
            {
            }
        }
        public void IntervalJumping_Middle()
        {
            var smallBam     = Path.Combine(UnitTestPaths.TestDataDirectory, "Ins-L3-var12_S12.bam");
            var intervals    = new Dictionary <string, List <Region> >();
            var chrIntervals = new List <Region>
            {
                new Region(28607838, 28607838),
                new Region(28608631, 28608631)
            };

            intervals.Add("chr13", chrIntervals);
            var extractor = new BamFileAlignmentExtractor(smallBam, bamIntervals: intervals);

            var read = new Read();

            // verify we skip over the middle
            while (extractor.GetNextAlignment(read))
            {
                Assert.True(read.Position < 28607840 || read.BamAlignment.GetEndPosition() >= 28608631);
            }
        }
        public void IntervalJumping_SmallIntervals()
        {
            var smallBam     = Path.Combine(UnitTestPaths.TestDataDirectory, "Ins-L3-var12_S12.bam");
            var intervals    = new Dictionary <string, List <Region> >();
            var chrIntervals = new List <Region>
            {
                new Region(28607838, 28607838),
                new Region(28607908, 28607908),
                new Region(28608631, 28608631)
            };

            intervals.Add("chr13", chrIntervals);
            var extractor = new BamFileAlignmentExtractor(smallBam, bamIntervals: intervals);

            var read         = new Read();
            var lastPosition = 0;

            // verify we are always moving forward and not backwards (not re-reading alignments)
            while (extractor.GetNextAlignment(read))
            {
                Assert.True(read.Position >= lastPosition);
                lastPosition = read.Position;
            }
        }