Java Code Examples for htsjdk.samtools.SAMRecord#getReadPositionAtReferencePosition()

The following examples show how to use htsjdk.samtools.SAMRecord#getReadPositionAtReferencePosition() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SamRecordScoring.java    From hmftools with GNU General Public License v3.0 6 votes vote down vote up
@NotNull
private static ReadType getReadType(@NotNull final SAMRecord record, @NotNull final VariantContext variant) {
    final Allele alt = variant.getAlternateAllele(0);
    final Allele ref = variant.getReference();
    final int recordIdxOfVariantStart = record.getReadPositionAtReferencePosition(variant.getStart());
    if (recordIdxOfVariantStart == 0) {
        // Variant position was deleted
        return ReadType.MISSING;

    }
    if (variant.isSNP()) {
        if (record.getReadString().charAt(recordIdxOfVariantStart - 1) == alt.getBaseString().charAt(0)) {
            return ReadType.ALT;
        } else {
            return ReadType.REF;
        }
    }
    if (variant.isSimpleInsertion()) {
        return SAMRecords.containsInsert(record, variant.getStart(), alt.getBaseString()) ? ReadType.ALT : ReadType.REF;
    }
    if (variant.isSimpleDeletion()) {
        return SAMRecords.containsDelete(record, variant.getStart(), ref.getBaseString()) ? ReadType.ALT : ReadType.REF;
    }
    return ReadType.OTHER;
}
 
Example 2
Source File: RawContextCigarHandler.java    From hmftools with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void handleRightSoftClip(@NotNull final SAMRecord record, @NotNull final CigarElement element, final int readIndex,
        final int refPosition) {
    if (result != null) {
        return;
    }

    long refPositionEnd = refPosition + element.getLength() - 1;
    if (refPositionEnd < variant.position()) {
        throw new IllegalStateException("Variant is after record");
    }

    if (variant.position() >= refPosition && variant.position() <= refPositionEnd) {
        int alignmentEnd = record.getAlignmentEnd();
        int actualIndex = record.getReadPositionAtReferencePosition(alignmentEnd) - 1 - alignmentEnd + (int) variant.position();
        result = RawContext.inSoftClip(actualIndex);
    }
}
 
Example 3
Source File: VariantHotspotEvidenceFactory.java    From hmftools with GNU General Public License v3.0 6 votes vote down vote up
private static boolean isStartPartOfLargerMNV(int samOffset, @NotNull final SAMRecord samRecord, @NotNull final String refSequence) {
    int variantStart = samOffset + refSequence.length();

    int startReadPosition = samRecord.getReadPositionAtReferencePosition(variantStart);
    assert (startReadPosition != 0);

    for (int i = 1; i <= refSequence.length(); i++) {
        int bufferReadPosition = samRecord.getReadPositionAtReferencePosition(variantStart - i);

        if (bufferReadPosition == 0) {
            return false; // DEL
        }

        if (samRecord.getReferencePositionAtReadPosition(startReadPosition - i) == 0) {
            return false; // INS
        }

        int refSequenceIndex = refSequence.length() - i;
        if (samRecord.getReadString().charAt(bufferReadPosition - 1) != refSequence.charAt(refSequenceIndex)) {
            return true;
        }

    }

    return false;
}
 
Example 4
Source File: VariantHotspotEvidenceFactory.java    From hmftools with GNU General Public License v3.0 6 votes vote down vote up
private static boolean isEndPartOfLargerMNV(int samOffset, @NotNull final SAMRecord samRecord, @NotNull final String refSequence) {
    int variantEnd = samOffset - 1;

    int endReadPosition = samRecord.getReadPositionAtReferencePosition(variantEnd);
    assert (endReadPosition != 0);

    for (int i = 1; i <= refSequence.length(); i++) {
        int bufferReadPosition = samRecord.getReadPositionAtReferencePosition(variantEnd + i);

        if (bufferReadPosition == 0) {
            return false; // DEL
        }

        if (samRecord.getReferencePositionAtReadPosition(endReadPosition + i) == 0) {
            return false; // INS
        }

        if (samRecord.getReadString().charAt(bufferReadPosition - 1) != refSequence.charAt(i - 1)) {
            return true;
        }

    }

    return false;
}
 
Example 5
Source File: TumorBAFFactory.java    From hmftools with GNU General Public License v3.0 6 votes vote down vote up
void addEvidence(@NotNull final ModifiableTumorBAF evidence, @NotNull final SAMRecord samRecord) {
    int quality = getBaseQuality(evidence, samRecord);
    if (quality >= minBaseQuality) {
        evidence.setTumorReadDepth(evidence.tumorReadDepth() + 1);
        int bafPosition = (int) evidence.position();
        int readPosition = samRecord.getReadPositionAtReferencePosition(bafPosition);
        if (readPosition != 0) {
            if (!indel(bafPosition, readPosition, samRecord)) {
                final String base = String.valueOf(samRecord.getReadString().charAt(readPosition - 1));
                if (base.equals(evidence.ref())) {
                    evidence.setTumorRefSupport(evidence.tumorRefSupport() + 1);
                } else if (base.equals(evidence.alt())) {
                    evidence.setTumorAltSupport(evidence.tumorAltSupport() + 1);
                    evidence.setTumorAltQuality(evidence.tumorAltQuality() + quality);
                }
            } else {
                evidence.setTumorIndelCount(evidence.tumorIndelCount() + 1);
            }
        }
    }
}
 
Example 6
Source File: InframeIndelHotspots.java    From hmftools with GNU General Public License v3.0 6 votes vote down vote up
@NotNull
static Set<VariantHotspot> findInframeIndelsWithIncorrectRefs(@NotNull final SAMRecord record) {
    Set<VariantHotspot> result = Sets.newHashSet();
    if (containsInframeIndel(record)) {
        for (int refPosition = record.getAlignmentStart(); refPosition <= record.getAlignmentEnd(); refPosition++) {
            int readPosition = record.getReadPositionAtReferencePosition(refPosition);

            if (readPosition != 0) {
                int basesInserted = SAMRecords.basesInsertedAfterPosition(record, refPosition);
                if (basesInserted > 0 && basesInserted % 3 == 0) {
                    result.add(createInsert(record, readPosition, refPosition, basesInserted + 1));
                    continue;
                }

                int basesDeleted = SAMRecords.basesDeletedAfterPosition(record, refPosition);
                if (basesDeleted > 0 && basesDeleted % 3 == 0) {
                    result.add(createDelete(record, readPosition, refPosition, basesDeleted + 1));
                }
            }
        }
    }

    return result;
}
 
Example 7
Source File: BaseDepthFactory.java    From hmftools with GNU General Public License v3.0 6 votes vote down vote up
void addEvidence(@NotNull final ModifiableBaseDepth evidence, @NotNull final SAMRecord samRecord) {
    int quality = getBaseQuality(evidence, samRecord);
    if (quality >= minBaseQuality) {
        evidence.setReadDepth(evidence.readDepth() + 1);

        int bafPosition = (int) evidence.position();
        int readPosition = samRecord.getReadPositionAtReferencePosition(bafPosition);
        if (readPosition != 0) {
            if (!indel(bafPosition, readPosition, samRecord)) {
                final char baseChar = samRecord.getReadString().charAt(readPosition - 1);
                final BaseDepth.Base base = BaseDepth.Base.valueOf(String.valueOf(baseChar).toUpperCase());
                if (base.equals(evidence.ref())) {
                    evidence.setRefSupport(evidence.refSupport() + 1);
                } else if (base.equals(evidence.alt())) {
                    evidence.setAltSupport(evidence.altSupport() + 1);
                }
            } else {
                evidence.setIndelCount(evidence.indelCount() + 1);
            }
        }
    }
}
 
Example 8
Source File: SAMRecords.java    From hmftools with GNU General Public License v3.0 5 votes vote down vote up
public static int basesDeletedAfterPosition(@NotNull final SAMRecord record, int position) {
    int startReadPosition = record.getReadPositionAtReferencePosition(position);
    assert (startReadPosition != 0);

    int nextReferencePosition = record.getReferencePositionAtReadPosition(startReadPosition + 1);
    return nextReferencePosition == 0 && startReadPosition == record.getReadLength()
            ? record.getAlignmentEnd() - position
            : Math.max(0, nextReferencePosition - position - 1);
}
 
Example 9
Source File: MNVRegionValidator.java    From hmftools with GNU General Public License v3.0 5 votes vote down vote up
@NotNull
private static Character getReadAtPosition(@NotNull final SAMRecord record, final int position) {
    final int recordPosition = record.getReadPositionAtReferencePosition(position);
    if (recordPosition == 0) {
        return 'N';
    } else {
        return record.getReadString().charAt(recordPosition - 1);
    }
}
 
Example 10
Source File: RawContextCigarHandler.java    From hmftools with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void handleLeftSoftClip(@NotNull final SAMRecord record, @NotNull final CigarElement element) {
    if (variant.position() < record.getAlignmentStart()) {
        int readIndex = record.getReadPositionAtReferencePosition(record.getAlignmentStart()) - 1 - record.getAlignmentStart()
                + (int) variant.position() - variant.alt().length() + variant.ref().length();
        result = RawContext.inSoftClip(readIndex);
    }
}
 
Example 11
Source File: BaseDepthFactory.java    From hmftools with GNU General Public License v3.0 5 votes vote down vote up
static int getBaseQuality(@NotNull final GenomePosition position, @NotNull final SAMRecord samRecord) {
    // Get quality of base after del if necessary
    for (int pos = (int) position.position(); pos <= samRecord.getAlignmentEnd(); pos++) {
        int readPosition = samRecord.getReadPositionAtReferencePosition(pos);
        if (readPosition != 0) {
            return SAMRecords.getBaseQuality(samRecord, readPosition);
        }
    }

    return 0;
}
 
Example 12
Source File: BaseDepthFactory.java    From hmftools with GNU General Public License v3.0 5 votes vote down vote up
static boolean indel(int bafPosition, int readPosition, @NotNull final SAMRecord samRecord) {
    if (samRecord.getAlignmentEnd() > bafPosition) {

        // Delete?
        if (samRecord.getReadPositionAtReferencePosition(bafPosition + 1) == 0) {
            return true;
        }

        // Insert?
        return samRecord.getReferencePositionAtReadPosition(readPosition + 1) != bafPosition + 1;
    }

    return false;
}
 
Example 13
Source File: SAMRecords.java    From hmftools with GNU General Public License v3.0 5 votes vote down vote up
public static int basesInsertedAfterPosition(@NotNull final SAMRecord record, int position) {
    int startReadPosition = record.getReadPositionAtReferencePosition(position);
    assert (startReadPosition != 0);
    int nextReadPosition = record.getReadPositionAtReferencePosition(position + 1);

    return nextReadPosition == 0 && record.getAlignmentEnd() == position
            ? record.getReadString().length() - startReadPosition
            : Math.max(0, nextReadPosition - startReadPosition - 1);
}
 
Example 14
Source File: SAMRecords.java    From hmftools with GNU General Public License v3.0 5 votes vote down vote up
public static boolean containsInsert(@NotNull final SAMRecord record, int position, @NotNull final String alt) {
    int recordIdxOfVariantStart = record.getReadPositionAtReferencePosition(position);

    int insertedBases = basesInsertedAfterPosition(record, position);
    return insertedBases == alt.length() - 1 && record.getReadString()
            .substring(recordIdxOfVariantStart - 1, recordIdxOfVariantStart - 1 + alt.length())
            .equals(alt);
}
 
Example 15
Source File: VariantHotspotEvidenceFactory.java    From hmftools with GNU General Public License v3.0 5 votes vote down vote up
@NotNull
ModifiableVariantHotspotEvidence findEvidenceOfMNV(@NotNull final ModifiableVariantHotspotEvidence builder, int start,
        @NotNull final String refSequence, @NotNull final VariantHotspot hotspot, @NotNull final SAMRecord record) {
    int hotspotStartPosition = (int) hotspot.position();
    int hotspotLength = Math.max(hotspot.ref().length(), hotspot.alt().length());

    int recordStartPosition = record.getReadPositionAtReferencePosition(hotspotStartPosition);
    int recordStartQuality = SAMRecords.getBaseQuality(record, recordStartPosition);

    if (isVariantPartOfLargerMNV(start, refSequence, hotspot, record)) {
        return recordStartQuality < minBaseQuality ? builder : builder.setReadDepth(builder.readDepth() + 1);
    }

    for (int i = 0; i < hotspotLength; i++) {
        int readPosition = record.getReadPositionAtReferencePosition(hotspotStartPosition + i);
        boolean isDeleted = readPosition == 0;
        boolean isInserted = record.getReferencePositionAtReadPosition(recordStartPosition + i) == 0;

        if (isInserted || isDeleted) {
            if (recordStartQuality < minBaseQuality) {
                return builder;
            }
            return builder.setReadDepth(builder.readDepth() + 1);
        }
    }

    final String samBases = record.getReadString().substring(recordStartPosition - 1, recordStartPosition - 1 + hotspotLength);
    if (samBases.equals(hotspot.alt())) {
        int altQuality = getAvgBaseQuality(record, recordStartPosition, hotspotLength);
        if (altQuality < minBaseQuality) {
            return builder;
        }

        builder.setAltQuality(builder.altQuality() + altQuality).setAltSupport(builder.altSupport() + 1);
    } else if (samBases.equals(hotspot.ref())) {
        builder.setRefSupport(builder.refSupport() + 1);
    }

    return builder.setReadDepth(builder.readDepth() + 1);
}
 
Example 16
Source File: VariantHotspotEvidenceFactory.java    From hmftools with GNU General Public License v3.0 5 votes vote down vote up
ModifiableVariantHotspotEvidence findEvidenceOfDelete(@NotNull final ModifiableVariantHotspotEvidence builder,
        @NotNull final VariantHotspot hotspot, @NotNull final SAMRecord record) {
    assert (hotspot.isSimpleDelete());

    int hotspotStartPosition = (int) hotspot.position();
    int recordStartPosition = record.getReadPositionAtReferencePosition(hotspotStartPosition);
    if (recordStartPosition == 0) {
        return builder;
    }

    int recordStartQuality = getBaseQuality(record, recordStartPosition);

    if (containsDelete(record, hotspotStartPosition, hotspot.ref())) {
        int quality =
                record.getReadLength() > recordStartPosition ? getAvgBaseQuality(record, recordStartPosition, 2) : recordStartQuality;
        if (quality < minBaseQuality) {
            return builder;
        }
        return builder.setReadDepth(builder.readDepth() + 1)
                .setIndelSupport(builder.indelSupport() + 1)
                .setAltQuality(builder.altQuality() + quality)
                .setAltSupport(builder.altSupport() + 1);
    }

    if (recordStartQuality < minBaseQuality) {
        return builder;
    }

    int insertedBases = basesInsertedAfterPosition(record, hotspotStartPosition);
    int deletedBases = basesDeletedAfterPosition(record, hotspotStartPosition);
    if (insertedBases == 0 && deletedBases == 0 && record.getReadString().charAt(recordStartPosition - 1) == hotspot.ref().charAt(0)) {
        builder.setRefSupport(builder.refSupport() + 1);
    } else if (insertedBases > 0 || deletedBases > 0) {
        builder.setIndelSupport(builder.indelSupport() + 1);
    }

    return builder.setReadDepth(builder.readDepth() + 1);
}
 
Example 17
Source File: VariantHotspotEvidenceFactory.java    From hmftools with GNU General Public License v3.0 5 votes vote down vote up
@NotNull
ModifiableVariantHotspotEvidence findEvidenceOfInsert(@NotNull final ModifiableVariantHotspotEvidence builder,
        @NotNull final VariantHotspot hotspot, @NotNull final SAMRecord record) {
    assert (hotspot.isSimpleInsert());

    int hotspotStartPosition = (int) hotspot.position();
    int recordStartPosition = record.getReadPositionAtReferencePosition(hotspotStartPosition);
    if (recordStartPosition == 0) {
        return builder;
    }

    int recordStartQuality = getBaseQuality(record, recordStartPosition);
    if (containsInsert(record, hotspotStartPosition, hotspot.alt())) {
        int quality = getAvgBaseQuality(record, recordStartPosition, hotspot.alt().length());
        if (quality < minBaseQuality) {
            return builder;
        }
        return builder.setReadDepth(builder.readDepth() + 1)
                .setIndelSupport(builder.indelSupport() + 1)
                .setAltQuality(builder.altQuality() + quality)
                .setAltSupport(builder.altSupport() + 1);
    }

    if (recordStartQuality < minBaseQuality) {
        return builder;
    }

    int insertedBases = basesInsertedAfterPosition(record, hotspotStartPosition);
    int deletedBases = basesDeletedAfterPosition(record, hotspotStartPosition);
    if (insertedBases == 0 && deletedBases == 0 && record.getReadString().charAt(recordStartPosition - 1) == hotspot.ref().charAt(0)) {
        builder.setRefSupport(builder.refSupport() + 1);
    } else if (insertedBases > 0 || deletedBases > 0) {
        builder.setIndelSupport(builder.indelSupport() + 1);
    }

    return builder.setReadDepth(builder.readDepth() + 1);
}
 
Example 18
Source File: VariantHotspotEvidenceFactory.java    From hmftools with GNU General Public License v3.0 4 votes vote down vote up
private boolean startPositionValid(@NotNull final VariantHotspot hotspot, @NotNull final SAMRecord record) {
    return record.getReadPositionAtReferencePosition((int) hotspot.position()) != 0;
}