Skip to content

Commit

Permalink
SamSequenceRecord implements Locatable (#1459)
Browse files Browse the repository at this point in the history
* Make SamSequenceRecord implement Locatable so it can be used with overlaps checks and in utilities that use locatable.
  • Loading branch information
lindenb authored Feb 19, 2020
1 parent 4e1363f commit 5445b90
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
27 changes: 23 additions & 4 deletions src/main/java/htsjdk/samtools/SAMSequenceRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,20 @@
package htsjdk.samtools;


import htsjdk.variant.variantcontext.VariantContext;

import java.math.BigInteger;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
import htsjdk.samtools.util.Locatable;

/**
* Header information about a reference sequence. Corresponds to @SQ header record in SAM text header.
*/

public class SAMSequenceRecord extends AbstractSAMHeaderRecord implements Cloneable
public class SAMSequenceRecord extends AbstractSAMHeaderRecord implements Cloneable, Locatable
{
public static final long serialVersionUID = 1L; // AbstractSAMHeaderRecord implements Serializable
public final static int UNAVAILABLE_SEQUENCE_INDEX = -1;
Expand Down Expand Up @@ -228,5 +226,26 @@ public String toString() {
public String getSAMString() {
return new SAMTextHeaderCodec().getSQLine(this);
}
/** always returns <code>getSequenceName()</code>
* @see #getSequenceName()
* */
@Override
public final String getContig() {
return this.getSequenceName();
}

/** always returns 1 */
@Override
public final int getStart() {
return 1;
}

/** always returns <code>getSequenceLength()</code>
* @see #getSequenceLength()
* */
@Override
public final int getEnd() {
return this.getSequenceLength();
}
}

12 changes: 12 additions & 0 deletions src/test/java/htsjdk/samtools/SAMSequenceRecordTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package htsjdk.samtools;

import htsjdk.HtsjdkTest;
import htsjdk.samtools.util.Interval;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
Expand All @@ -44,6 +45,17 @@ public void testGetSAMString() {
Assert.assertEquals("@SQ\tSN:chr5_but_without_a_prefix\tLN:271828\tSP:Psephophorus terrypratchetti\tAS:GRCt01\tM5:7a6dd3d307de916b477e7bf304ac22bc", r.getSAMString());
}

@Test
public void testLocatable() {
final SAMSequenceRecord r = new SAMSequenceRecord("1", 100);
Assert.assertTrue(r.overlaps(r));
Assert.assertEquals(r.getStart(),1);
Assert.assertEquals(r.getEnd(),r.getSequenceLength());
Assert.assertEquals(r.getLengthOnReference(),r.getSequenceLength());
Assert.assertTrue(r.overlaps(new Interval(r.getContig(), 50, 150)));
Assert.assertFalse(r.overlaps(new Interval(r.getContig(), 101, 101)));
}

@DataProvider
public Object[][] testIsSameSequenceData() {
final SAMSequenceRecord rec1 = new SAMSequenceRecord("chr1", 100);
Expand Down

0 comments on commit 5445b90

Please sign in to comment.