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

Fixes edge case where soft-cliped portion of a read ends up at a nega… #1513

Merged
merged 6 commits into from
May 21, 2020
Merged
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
10 changes: 6 additions & 4 deletions src/main/java/picard/sam/AbstractAlignmentMerger.java
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,7 @@ protected static void clipForOverlappingReads(final SAMRecord read1, final SAMRe
}
}

private static int getReadPositionAtReferencePositionIgnoreSoftClips(final SAMRecord rec, final int pos) {
static int getReadPositionAtReferencePositionIgnoreSoftClips(final SAMRecord rec, final int pos) {
final int readPosition;
final Cigar oldCigar = rec.getCigar();
final int oldStart = rec.getAlignmentStart();
Expand Down Expand Up @@ -819,10 +819,12 @@ private static int getReadPositionAtReferencePositionIgnoreSoftClips(final SAMRe

// Temporarily use the newCigar that has SOFT_CLIPs replaced with MATCH_OR_MISMATCH to get read position at reference, but ignore existence of soft-clips
rec.setCigar(newCigar);
readPosition = SAMRecord.getReadPositionAtReferencePosition(rec, pos, false);
// Since the read effectively got shifted forward by turning the clips into matches, the query position needs
// also to be moved forward bye posShift so that it's still querying the same base.
readPosition = SAMRecord.getReadPositionAtReferencePosition(rec, pos + posShift, false);
Copy link
Contributor

Choose a reason for hiding this comment

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

add a comment that since the read effectively got shifted forward by turning the clips into matches, the query position needs also to be moved forward so that it's still querying the same base

rec.setCigar(oldCigar);
// instead of setting back the position of the read by posShift, which could create a negative start position, we add posShift the final position in the read.
return readPosition + posShift;

return readPosition;
}

private static void clip3PrimeEndOfRead(final SAMRecord rec, final int clipFrom, final boolean useHardClipping) {
Expand Down
24 changes: 24 additions & 0 deletions src/test/java/picard/sam/AbstractAlignmentMergerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -250,5 +250,29 @@ private static File newTempSamFile(final String filename) throws IOException {
file.deleteOnExit();
return file;
}
@DataProvider(name = "readPositionIgnoringSoftClips")
public Object[][] readPositionIgnoringSoftClips() {
return new Object[][] {
{"26S58M62S", 3688, 3827, 0}, // This is from the read that made us aware of a bug
{"26S58M62S", 3688, 3665, 4},
{"26S58M62S", 3688, 3660, 0}, // Before soft clip
{"10S100M2S", 5, 10, 16},
{"10S100M2S", 5, 3, 9},
{"10S100M2S", 10, 12, 13},
{"10S100M2S", 5, 107, 0}
};
}
@Test(dataProvider = "readPositionIgnoringSoftClips")
public void testGetReadPositionIgnoringSoftClips(final String cigarString, final int startPosition, final int queryPosition, final int expectedReadPosititon) {
final SAMFileHeader newHeader = SAMRecordSetBuilder.makeDefaultHeader(SAMFileHeader.SortOrder.queryname, 100000,false);
final SAMRecord rec = new SAMRecord(newHeader);

rec.setCigarString(cigarString);
rec.setAlignmentStart(startPosition);

final int readPosition = AbstractAlignmentMerger.getReadPositionAtReferencePositionIgnoreSoftClips(rec, queryPosition);

Assert.assertEquals(readPosition, expectedReadPosititon);
}
}