Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sam record pair identification depends on required SAM fields #994

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/main/java/htsjdk/samtools/SAMRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,20 @@ private boolean hasReferenceName() {
return hasReferenceName(mReferenceIndex, mReferenceName);
}

/**
* @return {@code true} if records belong to the same pairwise alignment
*/
public boolean isPair(final SAMRecord record) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused about this function, a mate is another record with the same query name...why are you checking everything else, and not the QNAME? I'm sure there's a reason, but it should be explained in the javadoc as it is not obvious at all!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the remark, I think it does matter in SamRecord and I fixed it, but if we implement it in HitsForInsert it doesn't matter because it deals records with the same QNAME only. Here is the logic that produces HitsForInsert.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest to move this to SamPairUtil, because there are already a lot of methods in SAMRecord that may be better to be used within an utility class. Eventually, reads would be accessed by an interface (e.g., #985) and thus this methods may live in an utility class that is implementation-independent.

return record != null
&& Objects.equals(this.getReadName(), record.getReadName())
&& this.getMateAlignmentStart() == record.getAlignmentStart()
&& record.getMateAlignmentStart() == this.getAlignmentStart()
&& !NO_ALIGNMENT_REFERENCE_NAME.equals(this.getMateReferenceName())
&& !NO_ALIGNMENT_REFERENCE_NAME.equals(record.getMateReferenceName())
&& Objects.equals(this.getReferenceName(), record.getMateReferenceName())
&& Objects.equals(record.getReferenceName(), this.getMateReferenceName());
}

/**
* @return true if this SAMRecord has a mate reference, either as a String or index (or both).
*/
Expand Down
49 changes: 49 additions & 0 deletions src/test/java/htsjdk/samtools/SAMRecordUnitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1102,4 +1102,53 @@ private Object[][] hasAttributeTestData() throws IOException {
public void testHasAttribute(final SAMRecord samRecord, final String tag, final boolean expectedHasAttribute) {
Assert.assertEquals(samRecord.hasAttribute(tag), expectedHasAttribute);
}

@Test
public void testRecordsArePairIfTheyLinkEachOtherInMateFields() {
final SAMRecord first = new SAMRecord(new SAMFileHeader(new SAMSequenceDictionary()));
first.setAlignmentStart(42);
first.setReferenceName("chrm1");
final SAMRecord second = new SAMRecord(new SAMFileHeader(new SAMSequenceDictionary()));
second.setAlignmentStart(142);
second.setReferenceName("chrm2");

first.setMateAlignmentStart(second.getAlignmentStart());
first.setMateReferenceName(second.getReferenceName());
second.setMateAlignmentStart(first.getAlignmentStart());
second.setMateReferenceName(first.getReferenceName());

Assert.assertTrue(first.isPair(second));
Assert.assertTrue(second.isPair(first));
}

@Test
public void testRecordsArePairIfTheyHaveNoMateFields() {
final SAMRecord first = new SAMRecord(new SAMFileHeader(new SAMSequenceDictionary()));
first.setReadName("example1");
first.setAlignmentStart(42);
first.setReferenceName("chrm1");
final SAMRecord second = new SAMRecord(new SAMFileHeader(new SAMSequenceDictionary()));
first.setReadName("example1");
second.setAlignmentStart(142);
second.setReferenceName("chrm2");

Assert.assertFalse(first.isPair(second));
Assert.assertFalse(second.isPair(first));
}

@Test
public void testRecordsArePairIdentificationDoesNotThrowNpe() {
final SAMRecord first = new SAMRecord(new SAMFileHeader(new SAMSequenceDictionary()));

Assert.assertFalse(first.isPair(null));
}

@Test
public void testRecordsArePairIdentificationDoesNotThrowNpeIfFieldsAreUndefined() {
final SAMRecord first = new SAMRecord(new SAMFileHeader(new SAMSequenceDictionary()));
final SAMRecord second = new SAMRecord(new SAMFileHeader(new SAMSequenceDictionary()));

Assert.assertFalse(first.isPair(second));
Assert.assertFalse(second.isPair(first));
}
}