Ejemplo n.º 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);
        }
Ejemplo n.º 2
0
        public void AppendTagData_Test()
        {
            var alignment = new BamAlignment()
            {
                TagData = new byte[] { 10, 10, 10 }
            };

            alignment.AppendTagData(new byte[] { 10, 10, 10 });
            Assert.Equal(6, alignment.TagData.Count());
            foreach (var item in alignment.TagData)
            {
                Assert.Equal(item, (byte)10);
            }
        }
Ejemplo n.º 3
0
        private void ExtractReadsStrandXRValidation()
        {
            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  = 99,
                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]
            };
            var tagUtils = new TagUtils();

            tagUtils.AddStringTag("XR", "BLABLA"); //start with random XR tag and confirm whether it is overwritten by read direction
            alignment1.AppendTagData(tagUtils.ToBytes());

            var alignment2 = new BamAlignment()
            {
                AlignmentFlag  = 144,
                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]
            };

            var tagUtils2 = new TagUtils();

            tagUtils.AddStringTag("XR", "BLABLA");  //start with random XR tag and confirm whether it is overwritten by read direction
            alignment2.AppendTagData(tagUtils.ToBytes());

            var readPair = new ReadPair(alignment1);

            readPair.AddAlignment(alignment2);

            var alignmentResults = pairHandler.ExtractReads(readPair);

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

            Assert.Equal("FR", alignment.GetStringTag("XR"));
        }
Ejemplo n.º 4
0
        private void ExtractReads(bool addUmiTags)
        {
            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]
            };

            var tagUtils = new TagUtils();

            tagUtils.AddStringTag("BC", "14");
            tagUtils.AddIntTag("SM", 40);
            if (addUmiTags)
            {
                tagUtils.AddIntTag("XV", 1);
                tagUtils.AddIntTag("XW", 2);
                tagUtils.AddStringTag("XU", "ABBA-ZABBA");
            }

            alignment1.AppendTagData(tagUtils.ToBytes());

            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]
            };

            var tagUtils2 = new TagUtils();

            tagUtils2.AddIntTag("NM", 5);
            tagUtils2.AddStringTag("BC", "14");
            tagUtils2.AddIntTag("SM", 20);
            if (addUmiTags)
            {
                tagUtils2.AddIntTag("XV", 1);
                tagUtils2.AddIntTag("XW", 2);
                tagUtils2.AddStringTag("XU", "ABBA-ZABBA");
            }
            alignment2.AppendTagData(tagUtils2.ToBytes());

            var readPair = new ReadPair(alignment1);

            readPair.AddAlignment(alignment2);

            var alignmentResults = pairHandler.ExtractReads(readPair);

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


            Assert.NotNull(alignment.GetStringTag("XD"));
            Assert.Null(alignment.GetIntTag("NM"));
            Assert.Null(alignment.GetStringTag("BC"));
            Assert.Null(alignment.GetIntTag("SM"));
            if (addUmiTags)
            {
                Assert.Equal("ABBA-ZABBA", alignment.GetStringTag("XU"));
                Assert.Equal(1, alignment.GetIntTag("XV"));
                Assert.Equal(2, alignment.GetIntTag("XW"));
            }
        }
Ejemplo n.º 5
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);
        }