/** * Returns 1-based index of first base in read that corresponds to M in CIGAR string. * Note that first is relative to 5' end, so that for reverse-strand alignment, the index of * the last base aligned is computed relative to the end of the read. */ int getIndexOfFirstAlignedBase(final SAMRecord rec) { final List<AlignmentBlock> alignmentBlocks = rec.getAlignmentBlocks(); if (rec.getReadNegativeStrandFlag()) { final AlignmentBlock alignmentBlock = alignmentBlocks.get(alignmentBlocks.size() - 1); return rec.getReadLength() - CoordMath.getEnd(alignmentBlock.getReadStart(), alignmentBlock.getLength()) + 1; } else { return alignmentBlocks.get(0).getReadStart(); } } }
/** * Returns 1-based index of first base in read that corresponds to M in CIGAR string. * Note that first is relative to 5' end, so that for reverse-strand alignment, the index of * the last base aligned is computed relative to the end of the read. */ int getIndexOfFirstAlignedBase(final SAMRecord rec) { final List<AlignmentBlock> alignmentBlocks = rec.getAlignmentBlocks(); if (rec.getReadNegativeStrandFlag()) { final AlignmentBlock alignmentBlock = alignmentBlocks.get(alignmentBlocks.size() - 1); return rec.getReadLength() - CoordMath.getEnd(alignmentBlock.getReadStart(), alignmentBlock.getLength()) + 1; } else { return alignmentBlocks.get(0).getReadStart(); } } }
/** Get the the number of bases in the given alignment block and record that have base quality greater or equal to the minimum */ public static int getNumBasesPassingMinimumBaseQuality(final SAMRecord record, final AlignmentBlock block, final int minimumBaseQuality) { int basesInBlockAtMinimumQuality = 0; final byte[] baseQualities = record.getBaseQualities(); for (int idx = block.getReadStart(); idx <= CoordMath.getEnd(block.getLength(), block.getReadStart()); idx++) { // idx is one-based if (minimumBaseQuality <= baseQualities[idx-1]) basesInBlockAtMinimumQuality++; } return basesInBlockAtMinimumQuality; }
/** Get the the number of bases in the given alignment block and record that have base quality greater or equal to the minimum */ public static int getNumBasesPassingMinimumBaseQuality(final SAMRecord record, final AlignmentBlock block, final int minimumBaseQuality) { int basesInBlockAtMinimumQuality = 0; final byte[] baseQualities = record.getBaseQualities(); for (int idx = block.getReadStart(); idx <= CoordMath.getEnd(block.getLength(), block.getReadStart()); idx++) { // idx is one-based if (minimumBaseQuality <= baseQualities[idx-1]) basesInBlockAtMinimumQuality++; } return basesInBlockAtMinimumQuality; }
/** * This method uses the MateCigar value as determined from the attribute MC. It must be non-null. * @param rec the SAM record * @return 1-based inclusive rightmost position of the clipped mate sequence, or 0 read if unmapped. */ public static int getMateAlignmentEnd(final SAMRecord rec) { if (rec.getMateUnmappedFlag()) { throw new RuntimeException("getMateAlignmentEnd called on an unmapped mate."); } final Cigar mateCigar = SAMUtils.getMateCigar(rec); if (mateCigar == null) { throw new SAMException("Mate CIGAR (Tag MC) not found."); } return CoordMath.getEnd(rec.getMateAlignmentStart(), mateCigar.getReferenceLength()); }
/** * This method uses the MateCigar value as determined from the attribute MC. It must be non-null. * * @param rec the SAM record * @return 1-based inclusive rightmost position of the clipped mate sequence, or 0 read if unmapped. */ public static int getMateAlignmentEnd(final SAMRecord rec) { if (rec.getMateUnmappedFlag()) { throw new RuntimeException("getMateAlignmentEnd called on an unmapped mate: " + rec); } final Cigar mateCigar = SAMUtils.getMateCigar(rec); if (mateCigar == null) { throw new SAMException("Mate CIGAR (Tag MC) not found:" + rec); } return CoordMath.getEnd(rec.getMateAlignmentStart(), mateCigar.getReferenceLength()); }
/** * This method uses the MateCigar value as determined from the attribute MC. It must be non-null. * * @param rec the SAM record * @return 1-based inclusive rightmost position of the clipped mate sequence, or 0 read if unmapped. */ public static int getMateAlignmentEnd(final SAMRecord rec) { if (rec.getMateUnmappedFlag()) { throw new RuntimeException("getMateAlignmentEnd called on an unmapped mate: " + rec); } final Cigar mateCigar = SAMUtils.getMateCigar(rec); if (mateCigar == null) { throw new SAMException("Mate CIGAR (Tag MC) not found:" + rec); } return CoordMath.getEnd(rec.getMateAlignmentStart(), mateCigar.getReferenceLength()); }
/** * This method uses the MateCigar value as determined from the attribute MC. It must be non-null. * * @param rec the SAM record * @return 1-based inclusive rightmost position of the clipped mate sequence, or 0 read if unmapped. */ public static int getMateAlignmentEnd(final SAMRecord rec) { if (rec.getMateUnmappedFlag()) { throw new RuntimeException("getMateAlignmentEnd called on an unmapped mate: " + rec); } final Cigar mateCigar = SAMUtils.getMateCigar(rec); if (mateCigar == null) { throw new SAMException("Mate CIGAR (Tag MC) not found:" + rec); } return CoordMath.getEnd(rec.getMateAlignmentStart(), mateCigar.getReferenceLength()); }
/** * Write into locusFunctions the function of each position from start to start + locusFunctions.length * relative to this transcript. Does not overwrite an existing value in locusFunctions that is stronger * than the function for that locus in this transcript. * @param start 1-based genomic coordinate of the first position in locusFunctions. * @param locusFunctions */ public void assignLocusFunctionForRange(final int start, final LocusFunction[] locusFunctions) { for (int i = Math.max(start, transcriptionStart); i <= Math.min(transcriptionEnd, CoordMath.getEnd(start, locusFunctions.length)); ++i) { if (locusFunctions[i - start].ordinal() > LocusFunction.CODING.ordinal()) continue; final LocusFunction locusFunction; if (inExon(i)) { if (utr(i)) locusFunction = LocusFunction.UTR; else locusFunction = LocusFunction.CODING; } else locusFunction = LocusFunction.INTRONIC; if (locusFunction.ordinal() > locusFunctions[i - start].ordinal()) { locusFunctions[i - start] = locusFunction; } } }
/** * Write into locusFunctions the function of each position from start to start + locusFunctions.length * relative to this transcript. Does not overwrite an existing value in locusFunctions that is stronger * than the function for that locus in this transcript. * @param start 1-based genomic coordinate of the first position in locusFunctions. * @param locusFunctions */ public void assignLocusFunctionForRange(final int start, final LocusFunction[] locusFunctions) { for (int i = Math.max(start, transcriptionStart); i <= Math.min(transcriptionEnd, CoordMath.getEnd(start, locusFunctions.length)); ++i) { if (locusFunctions[i - start].ordinal() > LocusFunction.CODING.ordinal()) continue; final LocusFunction locusFunction; if (inExon(i)) { if (utr(i)) locusFunction = LocusFunction.UTR; else locusFunction = LocusFunction.CODING; } else locusFunction = LocusFunction.INTRONIC; if (locusFunction.ordinal() > locusFunctions[i - start].ordinal()) { locusFunctions[i - start] = locusFunction; } } }
@Override List<Bait> design(final BaitDesigner designer, final Interval target, final ReferenceSequence reference) { final List<Bait> baits = new LinkedList<Bait>(); final int baitSize = designer.BAIT_SIZE; final int baitOffset = designer.BAIT_OFFSET; final int lastPossibleBaitStart = Math.min(target.getEnd(), reference.length() - baitSize); final int baitCount = 1 + (int) Math.floor((lastPossibleBaitStart - target.getStart()) / (double) baitOffset); int i = 0; for (int start = target.getStart(); start < lastPossibleBaitStart; start += baitOffset) { final Bait bait = new Bait(target.getContig(), start, CoordMath.getEnd(start, baitSize), target.isNegativeStrand(), designer.makeBaitName(target.getName(), ++i, baitCount)); bait.addBases(reference, designer.DESIGN_ON_TARGET_STRAND); baits.add(bait); } return baits; } };
@Override List<Bait> design(final BaitDesigner designer, final Interval target, final ReferenceSequence reference) { final List<Bait> baits = new LinkedList<Bait>(); final int baitSize = designer.BAIT_SIZE; final int baitOffset = designer.BAIT_OFFSET; final int lastPossibleBaitStart = Math.min(target.getEnd(), reference.length() - baitSize); final int baitCount = 1 + (int) Math.floor((lastPossibleBaitStart - target.getStart()) / (double) baitOffset); int i = 0; for (int start = target.getStart(); start < lastPossibleBaitStart; start += baitOffset) { final Bait bait = new Bait(target.getContig(), start, CoordMath.getEnd(start, baitSize), target.isNegativeStrand(), designer.makeBaitName(target.getName(), ++i, baitCount)); bait.addBases(reference, designer.DESIGN_ON_TARGET_STRAND); baits.add(bait); } return baits; } };
final Bait bait = new Bait(target.getContig(), baitStart, CoordMath.getEnd(baitStart, baitSize), target.isNegativeStrand(), designer.makeBaitName(target.getName(), 1, 1)); int start = firstBaitStart; while (start <= lastBaitStart) { final int end = CoordMath.getEnd(start, baitSize); final Bait bait = new Bait(target.getContig(), start,
final Bait bait = new Bait(target.getContig(), baitStart, CoordMath.getEnd(baitStart, baitSize), target.isNegativeStrand(), designer.makeBaitName(target.getName(), 1, 1)); int start = firstBaitStart; while (start <= lastBaitStart) { final int end = CoordMath.getEnd(start, baitSize); final Bait bait = new Bait(target.getContig(), start,
/** * @return 1-based inclusive reference position of the unclipped sequence at a given offset, * or 0 if there is no position. * For example, given the sequence NNNAAACCCGGG, cigar 3S9M, and an alignment start of 1, * and a (1-based)offset 10 (start of GGG) it returns 7 (1-based offset starting after the soft clip. * For example: given the sequence AAACCCGGGTTT, cigar 4M1D6M, an alignment start of 1, * an offset of 4 returns reference position 4, an offset of 5 returns reference position 6. * Another example: given the sequence AAACCCGGGTTT, cigar 4M1I6M, an alignment start of 1, * an offset of 4 returns reference position 4, an offset of 5 returns 0. * @offset 1-based location within the unclipped sequence */ public int getReferencePositionAtReadPosition(final int offset) { if (offset == 0) return 0; for (final AlignmentBlock alignmentBlock : getAlignmentBlocks()) { if (CoordMath.getEnd(alignmentBlock.getReadStart(), alignmentBlock.getLength()) < offset) { continue; } else if (offset < alignmentBlock.getReadStart()) { return 0; } else { return alignmentBlock.getReferenceStart() + offset - alignmentBlock.getReadStart(); } } return 0; // offset not located in an alignment block }
if (CoordMath.getEnd(alignmentBlock.getReadStart(), alignmentBlock.getLength()) < position) { continue; } else if (position < alignmentBlock.getReadStart()) {
if (CoordMath.getEnd(alignmentBlock.getReadStart(), alignmentBlock.getLength()) < position) { continue; } else if (position < alignmentBlock.getReadStart()) {
/** * Since EdgeReadIterator does not support emitting uncovered loci, this test just check that * iterator return correctly aligned objects for start and end of a read. */ @Override @Test public void testEmitUncoveredLoci() { final SAMRecordSetBuilder builder = getRecordBuilder(); // add records up to coverage for the test in that position final int startPosition = 165; for (int i = 0; i < coverage; i++) { // add a negative-strand fragment mapped on chrM with base quality of 10 builder.addFrag("record" + i, 0, startPosition, true, false, "36M", null, 10); } final int coveredEnd = CoordMath.getEnd(startPosition, readLength) + 1; final EdgeReadIterator sli = new EdgeReadIterator(builder.getSamReader()); int pos = 1; final int coveredStart = 165; for (final AbstractLocusInfo li : sli) { assertEquals(li.getPosition(), pos++); final int expectedReads; if (li.getPosition() == coveredStart || li.getPosition() == coveredEnd) { expectedReads = 2; } else { expectedReads = 0; } assertEquals(li.getRecordAndOffsets().size(), expectedReads); } assertEquals(pos, 100001); }
final int coveredEnd = CoordMath.getEnd(startPosition, readLength);
if (CoordMath.getEnd(alignmentBlock.getReferenceStart(), alignmentBlock.getLength()) >= pos) { if (pos < alignmentBlock.getReferenceStart()) {