From fd56496666211ffc4516271d5667dde8e0733483 Mon Sep 17 00:00:00 2001 From: Pierre Lindenbaum <33838+lindenb@users.noreply.github.com> Date: Mon, 28 Oct 2024 21:56:22 +0100 Subject: [PATCH] Add ReferenceSequenceFile.getSubsequenceAt(final Locatable locatable ) (#1725) --- .../reference/ReferenceSequenceFile.java | 13 ++++++++++++- .../AbstractIndexedFastaSequenceFileTest.java | 16 ++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/main/java/htsjdk/samtools/reference/ReferenceSequenceFile.java b/src/main/java/htsjdk/samtools/reference/ReferenceSequenceFile.java index 49f526cbc7..89afa12753 100644 --- a/src/main/java/htsjdk/samtools/reference/ReferenceSequenceFile.java +++ b/src/main/java/htsjdk/samtools/reference/ReferenceSequenceFile.java @@ -25,6 +25,7 @@ package htsjdk.samtools.reference; import htsjdk.samtools.SAMSequenceDictionary; +import htsjdk.samtools.util.Locatable; import java.io.Closeable; import java.io.IOException; @@ -80,7 +81,17 @@ public interface ReferenceSequenceFile extends Closeable { * @throws UnsupportedOperationException if !sIndexed. */ public ReferenceSequence getSubsequenceAt( String contig, long start, long stop ); - + + /** + * Gets the subsequence of the contig in the locatable. Shortcut to getSubsequenceAt(locatable.getContig(), locatable.getStart(), locatable.getEnd()); + * @param locatable the interval to retrieve. + * @return The partial reference sequence associated with this location. + * @throws UnsupportedOperationException if !sIndexed. + */ + public default ReferenceSequence getSubsequenceAt(final Locatable locatable ) { + return getSubsequenceAt(locatable.getContig(), locatable.getStart(), locatable.getEnd()); + } + /** * @return Reference name, file name, or something other human-readable representation. */ diff --git a/src/test/java/htsjdk/samtools/reference/AbstractIndexedFastaSequenceFileTest.java b/src/test/java/htsjdk/samtools/reference/AbstractIndexedFastaSequenceFileTest.java index ce2d23fa5b..3d53639ae9 100644 --- a/src/test/java/htsjdk/samtools/reference/AbstractIndexedFastaSequenceFileTest.java +++ b/src/test/java/htsjdk/samtools/reference/AbstractIndexedFastaSequenceFileTest.java @@ -30,6 +30,7 @@ import htsjdk.samtools.util.CloserUtil; import htsjdk.samtools.util.GZIIndex; import htsjdk.samtools.util.IOUtil; +import htsjdk.samtools.util.Interval; import htsjdk.samtools.util.RuntimeIOException; import htsjdk.samtools.util.StringUtil; import org.testng.Assert; @@ -138,6 +139,21 @@ public void testFirstSequence(AbstractIndexedFastaSequenceFile sequenceFile) { System.err.printf("testFirstSequence runtime: %dms%n", (endTime - startTime)) ; } + @Test(dataProvider="homosapiens") + public void testSubsequenceAtLocatable(AbstractIndexedFastaSequenceFile sequenceFile) { + long startTime = System.currentTimeMillis(); + ReferenceSequence sequence = sequenceFile.getSubsequenceAt(new Interval("chrM",1,firstBasesOfChrM.length())); + long endTime = System.currentTimeMillis(); + + Assert.assertEquals(sequence.getName(),"chrM","Sequence contig is not correct"); + Assert.assertEquals(sequence.getContigIndex(),0,"Sequence contig index is not correct"); + Assert.assertEquals(StringUtil.bytesToString(sequence.getBases()),firstBasesOfChrM,"First n bases of chrM are incorrect"); + + CloserUtil.close(sequenceFile); + + System.err.printf("testSubsequenceAtLocatable runtime: %dms%n", (endTime - startTime)) ; + } + @Test(dataProvider="homosapiens") public void testFirstSequenceExtended(AbstractIndexedFastaSequenceFile sequenceFile) { long startTime = System.currentTimeMillis();