Exemplo n.º 1
0
        /// <summary>
        ///     returns true if the alignment overlaps with the specified interval
        /// </summary>
        private bool IsOverlap(int begin, int end, BamAlignment alignment)
        {
            int alignmentBegin = alignment.Position;
            int alignmentEnd   = alignment.GetEndPosition();

            return((alignmentEnd >= begin) && (alignmentBegin < end));
        }
Exemplo n.º 2
0
        /// <summary>
        ///     Adds an offset to a specific reference sequence in the index
        /// </summary>
        private static void AddOffset(ref List <ulong> offsets, ref BamAlignment al, ulong offset)
        {
            int beg = al.Position >> BamLidxShift;
            int end = (al.GetEndPosition() - 1) >> BamLidxShift;

            // initialize additional entries if needed
            while (offsets.Count < (end + 1))
            {
                offsets.Add(0);
            }

            if (beg == end)
            {
                if (offsets[beg] == 0)
                {
                    offsets[beg] = offset;
                }
            }
            else
            {
                for (int i = beg; i <= end; i++)
                {
                    if (offsets[i] == 0)
                    {
                        offsets[i] = offset;
                    }
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        ///     returns true if the alignment overlaps with the specified interval [<paramref name="begin"/>,<paramref name="end"/>)
        /// </summary>
        /// <param name="begin">Start of the interval. By default inclusive.</param>
        /// <param name="end">End of the interval. By default exclusive.</param>
        /// <param name="alignment"></param>
        /// <param name="inclusiveEnd">If true, test overlap in the interval [<paramref name="begin"/>,<paramref name="end"/>] </param>
        private bool IsOverlap(int begin, int end, BamAlignment alignment, bool inclusiveEnd = false)
        {
            int alignmentBegin = alignment.Position;
            int alignmentEnd   = alignment.GetEndPosition();

            return((alignmentEnd >= begin) && ((inclusiveEnd && alignmentBegin <= end) || (!inclusiveEnd && alignmentBegin < end)));
        }
Exemplo n.º 4
0
        public void GetEndPosition_Tests()
        {
            var alignment1 = new BamAlignment()
            {
                Position  = 500,
                CigarData = new CigarAlignment("5M7I19M3D")
            };

            Assert.Equal(526, alignment1.GetEndPosition());

            var alignmentWithNoMappingBases = new BamAlignment()
            {
                Position  = 500,
                CigarData = new CigarAlignment("3I")
            };

            //Assert.Equal(500, alignment2.GetEndPosition());
            // Apparently for a degenerate case such as this, we're going to allow start position to be > end position.
            // https://jira.illumina.com/browse/PICS-857
            Assert.Equal(499, alignmentWithNoMappingBases.GetEndPosition());

            alignmentWithNoMappingBases = new BamAlignment()
            {
                Position  = 500,
                CigarData = new CigarAlignment("100S")
            };
            Assert.Equal(499, alignmentWithNoMappingBases.GetEndPosition());

            var alignmentWithNonMappingEnds = new BamAlignment()
            {
                Position  = 500,
                CigarData = new CigarAlignment("5M5I")
            };

            Assert.Equal(504, alignmentWithNonMappingEnds.GetEndPosition());

            alignmentWithNonMappingEnds = new BamAlignment()
            {
                Position  = 500,
                CigarData = new CigarAlignment("3S5M5I3S")
            };
            Assert.Equal(504, alignmentWithNonMappingEnds.GetEndPosition());

            alignmentWithNonMappingEnds = new BamAlignment()
            {
                Position  = 500,
                CigarData = new CigarAlignment("3S5M3S")
            };
            Assert.Equal(504, alignmentWithNonMappingEnds.GetEndPosition());

            alignmentWithNonMappingEnds = new BamAlignment()
            {
                Position  = 500,
                CigarData = new CigarAlignment("3S3M2D3S")
            };
            Assert.Equal(504, alignmentWithNonMappingEnds.GetEndPosition());
        }
Exemplo n.º 5
0
        public void GetEndPosition_Tests()
        {
            var alignment1 = new BamAlignment()
            {
                Position  = 500,
                CigarData = new CigarAlignment("5M7I19M3D")
            };

            Assert.Equal(527, alignment1.GetEndPosition());

            var alignment2 = new BamAlignment()
            {
                Position  = 500,
                CigarData = new CigarAlignment("3I")
            };

            Assert.Equal(500, alignment2.GetEndPosition());
        }
Exemplo n.º 6
0
        /// <summary>
        ///     Adds an offset to a specific reference sequence in the index
        /// </summary>
        private static void AddOffset(ref List<ulong> offsets, ref BamAlignment al, ulong offset)
        {
            int beg = al.Position >> BamLidxShift;
            int end = (al.GetEndPosition() - 1) >> BamLidxShift;

            // initialize additional entries if needed
            while (offsets.Count < (end + 1)) offsets.Add(0);

            if (beg == end)
            {
                if (offsets[beg] == 0) offsets[beg] = offset;
            }
            else
            {
                for (int i = beg; i <= end; i++)
                {
                    if (offsets[i] == 0) offsets[i] = offset;
                }
            }
        }
Exemplo n.º 7
0
		/// <summary>
		///     returns true if the alignment overlaps with the specified interval
		/// </summary>
		private bool IsOverlap(int begin, int end, BamAlignment alignment)
		{
			int alignmentBegin = alignment.Position;
			int alignmentEnd = alignment.GetEndPosition();
			return (alignmentEnd >= begin) && (alignmentBegin < end);
		}
Exemplo n.º 8
0
        private bool JumpIfNeeded(List <Region> chrIntervals, out Region currentRegion)
        {
            var completedIntervals = new List <Region>();

            for (var i = 0; i < chrIntervals.Count; i++)
            {
                var interval = chrIntervals[i];
                if ((_rawAlignment.Position + 1) > interval.EndPosition)  // bam alignment is 0-based, interval is 1-based
                {
                    completedIntervals.Add(interval);
                    _shouldCheckJumpForCurrentInterval = true;
                }
                else
                {
                    break;
                }
            }

            foreach (var completedInterval in completedIntervals)
            {
                chrIntervals.Remove(completedInterval);
            }

            // if done with intervals, jump to next chromosome
            if (!chrIntervals.Any())
            {
                currentRegion = null;
                if (_rawAlignment.RefID == _references.Count - 1)
                {
                    return(false);
                }
                if (_mateFinder == null || _mateFinder.NextMatePosition == null)
                {
                    return(_bamReader.Jump(_rawAlignment.RefID + 1, 0));
                }
                return(true);
            }
            currentRegion = chrIntervals[0];

            // if far from next interval, jump forward.  leave small buffer so we dont accidentally re-read already read alignments if there's not much gap inbetween
            if (_shouldCheckJumpForCurrentInterval)
            {
                var       targetInterval  = currentRegion;
                const int buffer          = 100;
                var       refMaxIndex     = (int)(_references.First(r => r.Index == _rawAlignment.RefID).Length - 1);
                var       jumpToThreshold = Math.Min(Math.Max(0, targetInterval.StartPosition - buffer), refMaxIndex);

                if (_mateFinder != null)
                {
                    var nextMate = _mateFinder.NextMatePosition;
                    if (nextMate != null && nextMate.Value < jumpToThreshold)
                    {
                        jumpToThreshold = nextMate.Value;
                    }
                    else
                    {
                        _shouldCheckJumpForCurrentInterval = false;
                    }
                }
                else
                {
                    _shouldCheckJumpForCurrentInterval = false;
                }

                if ((_rawAlignment.GetEndPosition() - _rawAlignment.CigarData.GetSuffixClip()) < jumpToThreshold)
                {
                    return(_bamReader.JumpForward(_rawAlignment.RefID, Math.Min(Math.Max(0, targetInterval.StartPosition - 1), refMaxIndex)));
                }
            }

            return(true);
        }