Java Code Examples for htsjdk.samtools.SAMRecord#NO_ALIGNMENT_START

The following examples show how to use htsjdk.samtools.SAMRecord#NO_ALIGNMENT_START . 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: ReadsSparkSource.java    From gatk with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Tests if a given SAMRecord overlaps any interval in a collection. This is only used as a fallback option for
 * formats that don't support query-by-interval natively at the Hadoop-BAM layer.
 */
//TODO: use OverlapDetector, see https://github.com/broadinstitute/gatk/issues/1531
private static boolean samRecordOverlaps(final SAMRecord record, final TraversalParameters traversalParameters ) {
    if (traversalParameters == null) {
        return true;
    }
    if (traversalParameters.traverseUnmappedReads() && record.getReadUnmappedFlag() && record.getAlignmentStart() == SAMRecord.NO_ALIGNMENT_START) {
        return true; // include record if unmapped records should be traversed and record is unmapped
    }
    List<SimpleInterval> intervals = traversalParameters.getIntervalsForTraversal();
    if (intervals == null || intervals.isEmpty()) {
        return false; // no intervals means 'no mapped reads'
    }
    for (SimpleInterval interval : intervals) {
        if (record.getReadUnmappedFlag() && record.getAlignmentStart() != SAMRecord.NO_ALIGNMENT_START) {
            // This follows the behavior of htsjdk's SamReader which states that "an unmapped read will be returned
            // by this call if it has a coordinate for the purpose of sorting that is in the query region".
            int start = record.getAlignmentStart();
            return interval.getStart() <= start && interval.getEnd() >= start;
        } else  if (interval.overlaps(record)) {
            return true;
        }
    }
    return false;
}
 
Example 2
Source File: CramSerilization.java    From cramtools with Apache License 2.0 5 votes vote down vote up
private static void updateTracks(final List<SAMRecord> samRecords, final ReferenceTracks tracks) {
	for (final SAMRecord samRecord : samRecords) {
		if (samRecord.getAlignmentStart() != SAMRecord.NO_ALIGNMENT_START
				&& samRecord.getReferenceIndex() == tracks.getSequenceId()) {
			int refPos = samRecord.getAlignmentStart();
			int readPos = 0;
			for (final CigarElement cigarElement : samRecord.getCigar().getCigarElements()) {
				if (cigarElement.getOperator().consumesReferenceBases()) {
					for (int elementIndex = 0; elementIndex < cigarElement.getLength(); elementIndex++)
						tracks.addCoverage(refPos + elementIndex, 1);
				}
				switch (cigarElement.getOperator()) {
				case M:
				case X:
				case EQ:
					for (int pos = readPos; pos < cigarElement.getLength(); pos++) {
						final byte readBase = samRecord.getReadBases()[readPos + pos];
						final byte refBase = tracks.baseAt(refPos + pos);
						if (readBase != refBase)
							tracks.addMismatches(refPos + pos, 1);
					}
					break;

				default:
					break;
				}

				readPos += cigarElement.getOperator().consumesReadBases() ? cigarElement.getLength() : 0;
				refPos += cigarElement.getOperator().consumesReferenceBases() ? cigarElement.getLength() : 0;
			}
		}
	}
}
 
Example 3
Source File: CramNormalizer.java    From cramtools with Apache License 2.0 5 votes vote down vote up
private static void setNextMate(final CramCompressionRecord record, final CramCompressionRecord next) {
	record.mateAlignmentStart = next.alignmentStart;
	record.setMateUnmapped(next.isSegmentUnmapped());
	record.setMateNegativeStrand(next.isNegativeStrand());
	record.mateSequenceID = next.sequenceId;
	if (record.mateSequenceID == SAMRecord.NO_ALIGNMENT_REFERENCE_INDEX)
		record.mateAlignmentStart = SAMRecord.NO_ALIGNMENT_START;
}
 
Example 4
Source File: Cram2Bam.java    From cramtools with Apache License 2.0 5 votes vote down vote up
private static void setNextMate(CramCompressionRecord r, CramCompressionRecord next) {
	r.mateAlignmentStart = next.alignmentStart;
	r.setMateUnmapped(next.isSegmentUnmapped());
	r.setMateNegativeStrand(next.isNegativeStrand());
	r.mateSequenceID = next.sequenceId;
	if (r.mateSequenceID == SAMRecord.NO_ALIGNMENT_REFERENCE_INDEX)
		r.mateAlignmentStart = SAMRecord.NO_ALIGNMENT_START;
}
 
Example 5
Source File: BAMQueryFilteringIterator.java    From cramtools with Apache License 2.0 4 votes vote down vote up
SAMRecord advance() {
	while (true) {
		// Pull next record from stream
		if (!wrappedIterator.hasNext())
			return null;

		final SAMRecord record = wrappedIterator.next();
		// If beyond the end of this reference sequence, end iteration
		final int referenceIndex = record.getReferenceIndex();
		if (referenceIndex != mReferenceIndex) {
			if (referenceIndex < 0 || referenceIndex > mReferenceIndex) {
				return null;
			}
			// If before this reference sequence, continue
			continue;
		}
		if (mRegionStart == 0 && mRegionEnd == Integer.MAX_VALUE) {
			// Quick exit to avoid expensive alignment end calculation
			return record;
		}
		final int alignmentStart = record.getAlignmentStart();
		// If read is unmapped but has a coordinate, return it if the
		// coordinate is within
		// the query region, regardless of whether the mapped mate will
		// be returned.
		final int alignmentEnd;
		if (mQueryType == QueryType.STARTING_AT) {
			alignmentEnd = -1;
		} else {
			alignmentEnd = (record.getAlignmentEnd() != SAMRecord.NO_ALIGNMENT_START ? record.getAlignmentEnd()
					: alignmentStart);
		}

		if (alignmentStart > mRegionEnd) {
			// If scanned beyond target region, end iteration
			return null;
		}
		// Filter for overlap with region
		if (mQueryType == QueryType.CONTAINED) {
			if (alignmentStart >= mRegionStart && alignmentEnd <= mRegionEnd) {
				return record;
			}
		} else if (mQueryType == QueryType.OVERLAPPING) {
			if (alignmentEnd >= mRegionStart && alignmentStart <= mRegionEnd) {
				return record;
			}
		} else {
			if (alignmentStart == mRegionStart) {
				return record;
			}
		}
	}
}