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); }
public static byte[] GetReadCountsTagData(int?xw, int?xv) { var tagUtils = new TagUtils(); if (xw.HasValue) { tagUtils.AddIntTag("XW", xw.Value); } if (xv.HasValue) { tagUtils.AddIntTag("XV", xv.Value); } return(tagUtils.ToBytes()); }
public static byte[] GetReadCountsTagData(int?x1, int?x2) { var tagUtils = new TagUtils(); if (x1.HasValue) { tagUtils.AddIntTag("X1", x1.Value); } if (x2.HasValue) { tagUtils.AddIntTag("X2", x2.Value); } return(tagUtils.ToBytes()); }
private BamAlignment CreateAlignment(string name, int position, string cigar, string bases, int nm = -1, uint mapq = 1) { var alignment = new BamAlignment(); alignment.Name = name; alignment.Position = position; alignment.RefID = 1; alignment.MapQuality = mapq; alignment.CigarData = new CigarAlignment(cigar); alignment.Bases = bases; alignment.Qualities = new byte[alignment.Bases.Length]; alignment.Qualities = Enumerable.Repeat((byte)30, alignment.Bases.Length).ToArray(); // Generates quality scores of 30 alignment.SetIsProperPair(true); var tagUtils = new TagUtils(); if (nm >= 0) { tagUtils.AddIntTag("NM", nm); } alignment.TagData = tagUtils.ToBytes(); return(alignment); }
public void GetTag_Tests() { // create a tag TagUtils tagUtils = new TagUtils(); tagUtils.AddIntTag("NM", 5); tagUtils.AddStringTag("XU", "ABCD"); tagUtils.AddCharTag("XP", '?'); byte[] tagData = tagUtils.ToBytes(); var alignment = new BamAlignment() { TagData = tagData }; // string tag scenarios Assert.Equal("ABCD", alignment.GetStringTag("XU")); Assert.Equal("?", alignment.GetStringTag("XP")); Assert.Throws <ApplicationException>(() => alignment.GetStringTag("NM")); Assert.Equal(null, alignment.GetStringTag("AB")); }
public static ReadPair GetPair(string cigar1, string cigar2, uint mapq1 = 30, uint mapq2 = 30, PairStatus pairStatus = PairStatus.Paired, bool singleReadOnly = false, int nm = 0, int read2Offset = 0, int?nm2 = null, string name = null, string basesRaw = "AAAGTTTTCCCCCCCCCCCCAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", int read1Position = 99, string read1Bases = null, string read2Bases = null) { int nmRead2 = nm2 ?? nm; var tagUtils = new TagUtils(); if (nm >= 0) { tagUtils.AddIntTag("NM", nm); } var cigarAln1 = new CigarAlignment(cigar1); var qualities1 = new List <byte>(); for (int i = 0; i < cigarAln1.GetReadSpan(); i++) { qualities1.Add(30); } //var basesRaw = "AAAGTTTTCCCCCCCCCCCC"; var alignment = new BamAlignment { Name = name ?? "hi:1:2:3:4:5:6", RefID = 1, Position = read1Position, Bases = read1Bases ?? basesRaw.Substring(0, (int)cigarAln1.GetReadSpan()), CigarData = cigarAln1, Qualities = qualities1.ToArray(), MapQuality = mapq1 }; alignment.TagData = tagUtils.ToBytes(); if (!singleReadOnly) { alignment.SetIsProperPair(true); alignment.MateRefID = 1; } var pair = new ReadPair(alignment); if (!singleReadOnly) { alignment.SetIsMateUnmapped(false); var tagUtils2 = new TagUtils(); if (nmRead2 >= 0) { tagUtils2.AddIntTag("NM", nmRead2); } var cigarAln2 = new CigarAlignment(cigar2); var qualities2 = new List <byte>(); for (int i = 0; i < cigarAln2.GetReadSpan(); i++) { qualities2.Add(30); } var alignment2 = new BamAlignment { Name = "hi:1:2:3:4:5:6", RefID = 1, Position = read1Position + read2Offset, Bases = read2Bases ?? basesRaw.Substring(read2Offset, (int)cigarAln2.GetReadSpan()), CigarData = cigarAln2, Qualities = qualities2.ToArray(), MapQuality = mapq2 }; alignment2.MateRefID = pair.Read1.RefID; alignment2.SetIsProperPair(true); alignment2.SetIsSecondMate(true); alignment2.SetIsReverseStrand(true); alignment2.TagData = tagUtils2.ToBytes(); pair.AddAlignment(alignment2); } pair.PairStatus = pairStatus; return(pair); }
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")); } }