예제 #1
0
        public static BamAlignment StitchifyBamAlignment(ReadPair pair, Read read, char read1dir, char read2dir)
        {
            var alignment = new BamAlignment(read.BamAlignment);

            alignment.SetIsFirstMate(false);
            alignment.SetIsProperPair(false);

            var tagUtils = new TagUtils();

            if (read.StitchedCigar != null)
            {
                alignment.CigarData = read.StitchedCigar;
            }

            if (read.CigarDirections != null)
            {
                tagUtils.AddStringTag("XD", read.CigarDirections.ToString());
            }

            // if the original reads had UMIs and were collapsed, they will have XU(Z), XV(i), XW(i)
            // these need to be copied to correctly populate some fields in the called variants
            if (pair.Read1.TagData != null && pair.Read1.TagData.Length > 0)
            {
                var xu = pair.Read1.GetStringTag("XU");
                if (xu != null)
                {
                    tagUtils.AddStringTag("XU", xu);
                }
                var xv = pair.Read1.GetIntTag("XV");
                if (xv.HasValue)
                {
                    tagUtils.AddIntTag("XV", xv.Value);
                }
                var xw = pair.Read1.GetIntTag("XW");
                if (xw.HasValue)
                {
                    tagUtils.AddIntTag("XW", xw.Value);
                }
            }

            var xr = string.Format("{0}{1}", read1dir, read2dir);

            tagUtils.AddStringTag("XR", xr);
            var tagData = tagUtils.ToBytes();

            var existingTags = alignment.TagData;

            if (existingTags == null)
            {
                alignment.TagData = tagData;
            }
            else
            {
                alignment.AppendTagData(tagData);
            }
            return(alignment);
        }
예제 #2
0
 private void SetPairedEndAttributes(BamAlignment read1, BamAlignment read2)
 {
     read1.SetIsFirstMate(true);
     read1.SetIsSecondMate(false);
     read2.SetIsFirstMate(false);
     read2.SetIsSecondMate(true);
     read1.SetIsProperPair(true);
     read2.SetIsProperPair(true);
     read2.SetIsReverseStrand(true);
     read2.MatePosition = read1.Position;
     read1.MatePosition = read2.Position;
 }
예제 #3
0
        // Copied from AlignmentSourceTests.cs (modified to add custom CigarData)
        private Read CreateRead(string chr, string sequence, int position, string name, CigarAlignment cigar = null, bool isMapped = true,
                                bool isPrimaryAlignment = true, bool isProperPair = true, bool isDuplicate = false, int mapQuality = 10, bool addCigarData = true,
                                bool read2 = false, int matePosition = 0)
        {
            var alignment = new BamAlignment()
            {
                Bases = sequence, Position = position, Name = name, MapQuality = (uint)mapQuality
            };

            alignment.SetIsUnmapped(!isMapped);
            alignment.SetIsSecondaryAlignment(!isPrimaryAlignment);
            alignment.SetIsDuplicate(isDuplicate);
            alignment.SetIsProperPair(isProperPair);
            alignment.SetIsFirstMate(!read2);
            alignment.MatePosition = matePosition;
            alignment.CigarData    = cigar ?? new CigarAlignment(sequence.Length + "M");

            return(new Read(chr, alignment));
        }
예제 #4
0
        public static BamAlignment CreateBamAlignment(string sequence, int position,
                                                      int matePosition, byte qualityForAll, bool isReverseMapped, uint mapQ = 30, byte[] qualities = null, CigarAlignment cigar = null, string name = null, bool isFirstMate = true)
        {
            var bamAlignment = new BamAlignment
            {
                Bases        = sequence,
                Position     = position - 1,
                CigarData    = cigar ?? new CigarAlignment(sequence.Length + "M"),
                Qualities    = qualities ?? Enumerable.Repeat(qualityForAll, sequence.Length).ToArray(),
                MatePosition = matePosition - 1,
                TagData      = new byte[0],
                RefID        = 1,
                MateRefID    = 1,
                Name         = name ?? "Alignment"
            };

            bamAlignment.SetIsFirstMate(isFirstMate);
            bamAlignment.MapQuality = mapQ;
            bamAlignment.SetIsReverseStrand(isReverseMapped);
            bamAlignment.SetIsMateReverseStrand(!isReverseMapped);
            return(bamAlignment);
        }
예제 #5
0
        public void BamAlignmentFlag_Setter_Tests()
        {
            var alignment = new BamAlignment();

            //Set Failed QC
            alignment.SetIsFailedQC(true);
            Assert.Equal((uint)512, alignment.AlignmentFlag);
            alignment.SetIsFailedQC(false);
            Assert.Equal((uint)0, alignment.AlignmentFlag);

            //Set Mate Unmapped
            alignment.SetIsMateUnmapped(true);
            Assert.Equal((uint)8, alignment.AlignmentFlag);
            alignment.SetIsMateUnmapped(false);
            Assert.Equal((uint)0, alignment.AlignmentFlag);

            //Set Mate Reverse Strand
            alignment.SetIsMateReverseStrand(true);
            Assert.Equal((uint)32, alignment.AlignmentFlag);
            alignment.SetIsMateReverseStrand(false);
            Assert.Equal((uint)0, alignment.AlignmentFlag);

            //Set Is Paired
            alignment.SetIsPaired(true);
            Assert.Equal((uint)1, alignment.AlignmentFlag);
            alignment.SetIsPaired(false);
            Assert.Equal((uint)0, alignment.AlignmentFlag);

            //Set Duplicate
            alignment.SetIsDuplicate(true);
            Assert.Equal((uint)1024, alignment.AlignmentFlag);
            alignment.SetIsDuplicate(false);
            Assert.Equal((uint)0, alignment.AlignmentFlag);

            //Set First Mate
            alignment.SetIsFirstMate(true);
            Assert.Equal((uint)64, alignment.AlignmentFlag);
            alignment.SetIsFirstMate(false);
            Assert.Equal((uint)0, alignment.AlignmentFlag);

            //Set Proper Pair
            alignment.SetIsProperPair(true);
            Assert.Equal((uint)2, alignment.AlignmentFlag);
            alignment.SetIsProperPair(false);
            Assert.Equal((uint)0, alignment.AlignmentFlag);

            //Set Reverse Strand
            alignment.SetIsReverseStrand(true);
            Assert.Equal((uint)16, alignment.AlignmentFlag);
            alignment.SetIsReverseStrand(false);
            Assert.Equal((uint)0, alignment.AlignmentFlag);

            //Set Secondary Alignment
            alignment.SetIsSecondaryAlignment(true);
            Assert.Equal((uint)256, alignment.AlignmentFlag);
            alignment.SetIsSecondaryAlignment(false);
            Assert.Equal((uint)0, alignment.AlignmentFlag);

            //Set Second Mate
            alignment.SetIsSecondMate(true);
            Assert.Equal((uint)128, alignment.AlignmentFlag);
            alignment.SetIsSecondMate(false);
            Assert.Equal((uint)0, alignment.AlignmentFlag);

            //Set Unmapped
            alignment.SetIsUnmapped(true);
            Assert.Equal((uint)4, alignment.AlignmentFlag);
            alignment.SetIsUnmapped(false);
            Assert.Equal((uint)0, alignment.AlignmentFlag);
        }
예제 #6
0
        private void ExtractReadsStrand(bool r1Reverse, bool r2Reverse, string expectedXRTag)
        {
            var refIdMapping = new Dictionary <int, string>()
            {
                { 1, "chr1" }
            };

            var stitcher = StitcherTestHelpers.GetStitcher(10, false);

            var readStatusCounter = new ReadStatusCounter();

            var pairHandler = new PairHandler(refIdMapping, stitcher, readStatusCounter, filterUnstitchablePairs: true);

            var alignment1 = new BamAlignment()
            {
                AlignmentFlag  = 0,
                Bases          = "ABCF",
                Bin            = 4,
                CigarData      = new CigarAlignment("2S2M"),
                FragmentLength = 42,
                MapQuality     = 30,
                MatePosition   = 2,
                MateRefID      = 43,
                Name           = "Read1",
                Position       = 1,
                Qualities      = new byte[4],
                RefID          = 1,
                TagData        = new byte[0]
            };

            alignment1.SetIsFirstMate(true);
            alignment1.SetIsReverseStrand(r1Reverse);
            var alignment2 = new BamAlignment()
            {
                AlignmentFlag  = 0,
                Bases          = "ABCF",
                Bin            = 4,
                CigarData      = new CigarAlignment("2S2M"),
                FragmentLength = 42,
                MapQuality     = 30,
                MatePosition   = 2,
                MateRefID      = 43,
                Name           = "Read1",
                Position       = 1,
                Qualities      = new byte[4],
                RefID          = 1,
                TagData        = new byte[0]
            };

            alignment2.SetIsSecondMate(true);
            alignment2.SetIsReverseStrand(r2Reverse);
            var readPair = new ReadPair(alignment1);

            readPair.AddAlignment(alignment2);

            var alignmentResults = pairHandler.ExtractReads(readPair);

            Assert.Equal(1, alignmentResults.Count);
            var alignment = alignmentResults[0];
            var XD        = alignment.GetStringTag("XR");

            Assert.Equal(expectedXRTag, alignment.GetStringTag("XR"));
        }
예제 #7
0
        public List <BamAlignment> ExtractReads(ReadPair pair)
        {
            var reads = new List <BamAlignment>();

            var chrom1 = _refIdMapping[pair.Read1.RefID];
            var chrom2 = _refIdMapping[pair.Read2.RefID];

            var alignmentSet = new AlignmentSet(
                new Read(chrom1, pair.Read1),
                new Read(chrom2, pair.Read2),
                false);

            var stitched = _stitcher.TryStitch(alignmentSet);

            if (stitched)
            {
                if (alignmentSet.ReadsForProcessing.Count > 1)
                {
                    throw new Exception("AlignmentSets for stitched reads should only have one ReadsForProcessing.");
                }
                foreach (var read in alignmentSet.ReadsForProcessing)
                {
                    var alignment = new BamAlignment(read.BamAlignment);

                    alignment.SetIsFirstMate(false);
                    alignment.SetIsProperPair(false);

                    var tagUtils = new TagUtils();
                    if (read.StitchedCigar != null)
                    {
                        alignment.CigarData = read.StitchedCigar;
                    }
                    if (read.CigarDirections != null)
                    {
                        tagUtils.AddStringTag("XD", read.CigarDirections.ToString());
                    }
                    var tagData = tagUtils.ToBytes();

                    var existingTags = alignment.TagData;
                    if (existingTags == null)
                    {
                        alignment.TagData = tagData;
                    }
                    else
                    {
                        alignment.AppendTagData(tagData);
                    }

                    reads.Add(alignment);
                }
            }
            else
            {
                if (!_filterUnstitchablePairs)
                {
                    _statusCounter.AddStatusCount("Unstitchable Pairs Kept");
                    reads.Add(new BamAlignment(alignmentSet.PartnerRead1.BamAlignment));
                    reads.Add(new BamAlignment(alignmentSet.PartnerRead2.BamAlignment));
                }
                else
                {
                    _statusCounter.AddStatusCount("Unstitchable Pairs Filtered");
                }
            }

            return(reads);
        }