Skip to content

Commit

Permalink
feat: add isConsensus method
Browse files Browse the repository at this point in the history
  • Loading branch information
znorgaard committed Jul 1, 2024
1 parent 8d31cf3 commit 85f9691
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/main/scala/com/fulcrumgenomics/bam/api/SamRecord.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ package com.fulcrumgenomics.bam.api

import com.fulcrumgenomics.FgBioDef._
import com.fulcrumgenomics.alignment.Cigar
import com.fulcrumgenomics.umi.ConsensusTags.PerRead.AllPerReadTags
import htsjdk.samtools
import htsjdk.samtools.SamPairUtil.PairOrientation
import htsjdk.samtools._
Expand Down Expand Up @@ -269,6 +270,10 @@ trait SamRecord {
SamPairUtil.getPairOrientation(this) == PairOrientation.FR
}

def isConsensus: Boolean = {
AllPerReadTags.exists(this.contains)
}

/** Clone method that does a "reasonably deep" clone. The bases and quals are cloned as is the attributes map,
* though not the values in the attributes map. */
override def clone(): SamRecord = {
Expand Down
14 changes: 14 additions & 0 deletions src/test/scala/com/fulcrumgenomics/bam/api/SamRecordTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ package com.fulcrumgenomics.bam.api
import com.fulcrumgenomics.alignment.Cigar
import com.fulcrumgenomics.testing.SamBuilder.{Minus, Plus}
import com.fulcrumgenomics.testing.{SamBuilder, UnitSpec}
import com.fulcrumgenomics.umi.ConsensusTags.PerRead.AllPerReadTags
import htsjdk.samtools.TextCigarCodec
import org.scalatest.OptionValues

Expand Down Expand Up @@ -289,4 +290,17 @@ class SamRecordTest extends UnitSpec with OptionValues {
rec1.matesOverlap shouldBe None // Mate's start is not enclosed by rec, and mate's end cannot be determined
rec2.matesOverlap.value shouldBe true // Mate's start is enclosed by rec, regardless of where mate end is
}

"SamRecord.isConsensus" should "return false for reads without consensus tags" in {
val builder = new SamBuilder(sort=Some(SamOrder.Coordinate), readLength=10, baseQuality=20)
builder.addFrag(start=100).exists(_.isConsensus) shouldBe false
builder.addPair(start1=100, start2=100, unmapped2=true).exists(_.isConsensus) shouldBe false
}

it should "return true for reads with consensus tags" in {
val builder = new SamBuilder(sort=Some(SamOrder.Coordinate), readLength=10, baseQuality=20)
AllPerReadTags.forall(
tag => builder.addFrag(start=10, attrs=Map(tag -> 123)).exists(_.isConsensus)
) shouldBe true
}
}

0 comments on commit 85f9691

Please sign in to comment.