Skip to content

Commit 3a74fd2

Browse files
authored
ZipperBams to produce mate score ("ms") for samtools markdup (#952)
* ZipperBams to produce mate score ("ms") for samtools markdup Fixes: #951
1 parent 0a03aa9 commit 3a74fd2

File tree

2 files changed

+35
-16
lines changed

2 files changed

+35
-16
lines changed

src/main/scala/com/fulcrumgenomics/bam/Bams.scala

+6-1
Original file line numberDiff line numberDiff line change
@@ -105,16 +105,21 @@ case class Template(r1: Option[SamRecord],
105105
Template(x1, x2)
106106
}
107107

108-
/** Fixes mate information and sets mate cigar on all primary and supplementary (but not secondary) records. */
108+
/** Fixes mate information and sets mate cigar and mate score on all primary and supplementary (but not secondary) records. */
109109
def fixMateInfo(): Unit = {
110+
// Developer note: the mate score ("ms") tag is used by samtools markdup
110111
for (primary <- r1; supp <- r2Supplementals) {
111112
SamPairUtil.setMateInformationOnSupplementalAlignment(supp.asSam, primary.asSam, true)
113+
primary.get[Int]("AS").foreach(supp("ms") = _)
112114
}
113115
for (primary <- r2; supp <- r1Supplementals) {
114116
SamPairUtil.setMateInformationOnSupplementalAlignment(supp.asSam, primary.asSam, true)
117+
primary.get[Int]("AS").foreach(supp("ms") = _)
115118
}
116119
for (first <- r1; second <- r2) {
117120
SamPairUtil.setMateInfo(first.asSam, second.asSam, true)
121+
first.get[Int]("AS").foreach(second("ms") = _)
122+
second.get[Int]("AS").foreach(first("ms") = _)
118123
}
119124
}
120125

src/test/scala/com/fulcrumgenomics/bam/BamsTest.scala

+29-15
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,7 @@ class BamsTest extends UnitSpec {
473473
builder.addPair(name="q1", contig=1, contig2=Some(2), start1=100, start2=200, cigar1="50M50S", cigar2="50S50M", mapq1=51, mapq2=52)
474474
builder.addPair(name="q1", contig=3, contig2=Some(4), start1=300, start2=400, cigar1="50S50M", cigar2="50M50S", mapq1=21, mapq2=22)
475475
.foreach(_.supplementary = true)
476+
builder.addPair(name="q2", contig=1, contig2=Some(2), start1=100, start2=200, cigar1="50M50S", cigar2="50S50M", mapq1=51, mapq2=52, attrs=Map(("ms", 2)))
476477

477478
val recs = builder.toIndexedSeq.tapEach { r =>
478479
r.mateMapped = false
@@ -482,21 +483,34 @@ class BamsTest extends UnitSpec {
482483
r.remove("MQ")
483484
}
484485

485-
val template = Template(recs.iterator)
486-
template.fixMateInfo()
487-
488-
template.allReads.foreach { r =>
489-
r.mateMapped shouldBe true
490-
491-
if (r.firstOfPair) {
492-
r.mateRefIndex shouldBe 2
493-
r.mateStart shouldBe 200
494-
r.mateCigar.value.toString() shouldBe "50S50M"
495-
}
496-
else {
497-
r.mateRefIndex shouldBe 1
498-
r.mateStart shouldBe 100
499-
r.mateCigar.value.toString() shouldBe "50M50S"
486+
val q1Template = Template(recs.iterator.filter(_.name == "q1"))
487+
val q2Template = Template(recs.iterator.filter(_.name == "q2"))
488+
val templates = Seq(q1Template, q2Template)
489+
490+
templates.foreach { template =>
491+
template.fixMateInfo()
492+
493+
template.allReads.foreach { r =>
494+
r.mateMapped shouldBe true
495+
496+
if (r.firstOfPair) {
497+
r.mateRefIndex shouldBe 2
498+
r.mateStart shouldBe 200
499+
r.mateCigar.value.toString() shouldBe "50S50M"
500+
}
501+
else {
502+
r.mateRefIndex shouldBe 1
503+
r.mateStart shouldBe 100
504+
r.mateCigar.value.toString() shouldBe "50M50S"
505+
}
506+
507+
if (r.name == "q1") {
508+
r.get[Int]("ms").isEmpty shouldBe true
509+
}
510+
else {
511+
r.name shouldBe "q2"
512+
r.get[Int]("ms").value shouldBe 2
513+
}
500514
}
501515
}
502516
}

0 commit comments

Comments
 (0)