From ef7f119b2a79dad9a19d0dfeb4cb3eb71c6eada7 Mon Sep 17 00:00:00 2001 From: Nils Homer Date: Mon, 29 Jan 2024 17:46:41 -0700 Subject: [PATCH] fix: mapped header records should overwrite unmapped in ZipperBams --- .../scala/com/fulcrumgenomics/bam/ZipperBams.scala | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/main/scala/com/fulcrumgenomics/bam/ZipperBams.scala b/src/main/scala/com/fulcrumgenomics/bam/ZipperBams.scala index e618fcf7a..4ad283c1d 100644 --- a/src/main/scala/com/fulcrumgenomics/bam/ZipperBams.scala +++ b/src/main/scala/com/fulcrumgenomics/bam/ZipperBams.scala @@ -87,11 +87,15 @@ private[bam] object ZipperBams extends LazyLogging { val header = new SAMFileHeader(dict) // Copy over comments, RGs and PGs, letting mapped override unmapped if there are conflicts - Iterator(unmapped, mapped).foreach { old => - old.getComments.iterator().foreach(header.addComment) - old.getReadGroups.iterator().foreach(header.addReadGroup) - old.getProgramRecords.iterator().foreach(header.addProgramRecord) - } + val mappedComments = mapped.getComments.toSet + val mappedReadGroups = mapped.getReadGroups.toSet + val mappedProgramRecords = mapped.getProgramRecords.toSet + unmapped.getComments.filterNot(mappedComments.contains).foreach(header.addComment) + unmapped.getReadGroups.filterNot(mappedReadGroups.contains).foreach(header.addReadGroup) + unmapped.getProgramRecords.filterNot(mappedProgramRecords.contains).foreach(header.addProgramRecord) + mapped.getComments.foreach(header.addComment) + mapped.getReadGroups.foreach(header.addReadGroup) + mapped.getProgramRecords.foreach(header.addProgramRecord) // Set the sort and group order header.setSortOrder(unmapped.getSortOrder)