Skip to content

Commit

Permalink
Add ReferenceSequenceFile.getSubsequenceAt(final Locatable locatable ) (
Browse files Browse the repository at this point in the history
  • Loading branch information
lindenb authored Oct 28, 2024
1 parent a37ffa6 commit fd56496
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 <code>getSubsequenceAt(locatable.getContig(), locatable.getStart(), locatable.getEnd());</code>
* @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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit fd56496

Please sign in to comment.