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

Deprecate 'not-primary' in favor of secondary along HTSJDK #977

Merged
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
32 changes: 5 additions & 27 deletions src/main/java/htsjdk/samtools/NotPrimarySkippingIterator.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,39 +24,17 @@
package htsjdk.samtools;

import htsjdk.samtools.util.CloseableIterator;
import htsjdk.samtools.util.PeekIterator;

/**
* Wrapper around SAMRecord iterator that skips over non-primary elements.
* This iterator conflates a filtering iterator and a peekable iterator. It would be cleaner to
* handle those concerns separately.
* @deprecated use {@link SecondaryAlignmentSkippingIterator} instead.
*/
public class NotPrimarySkippingIterator {
private final PeekIterator<SAMRecord> it;
@Deprecated
public class NotPrimarySkippingIterator extends SecondaryAlignmentSkippingIterator {

public NotPrimarySkippingIterator(final CloseableIterator<SAMRecord> underlyingIt) {
it = new PeekIterator<SAMRecord>(underlyingIt);
skipAnyNotprimary();
}

public boolean hasCurrent() {
return it.hasNext();
}

public SAMRecord getCurrent() {
assert(hasCurrent());
return it.peek();
}

public boolean advance() {
it.next();
skipAnyNotprimary();
return hasCurrent();
}

private void skipAnyNotprimary() {
while (it.hasNext() && it.peek().getNotPrimaryAlignmentFlag()) {
it.next();
}
public NotPrimarySkippingIterator(CloseableIterator<SAMRecord> underlyingIt) {
super(underlyingIt);
}
}
3 changes: 3 additions & 0 deletions src/main/java/htsjdk/samtools/SAMFlag.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public enum SAMFlag {
MATE_REVERSE_STRAND( 0x20, "SEQ of the next segment in the template being reverse complemented"),
FIRST_OF_PAIR( 0x40, "The first segment in the template"),
SECOND_OF_PAIR( 0x80, "The last segment in the template"),
SECONDARY_ALIGNMENT( 0x100, "Secondary alignment"),
/** @deprecated use {@link #SECONDARY_ALIGNMENT} instead. */
@Deprecated
NOT_PRIMARY_ALIGNMENT( 0x100, "Secondary alignment"),
READ_FAILS_VENDOR_QUALITY_CHECK(0x200, "Not passing quality controls"),
DUPLICATE_READ( 0x400, "PCR or optical duplicate"),
Expand Down
30 changes: 24 additions & 6 deletions src/main/java/htsjdk/samtools/SAMRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -967,9 +967,18 @@ private boolean getSecondOfPairFlagUnchecked() {

/**
* the alignment is not primary (a read having split hits may have multiple primary alignment records).
* @deprecated use {@link #isSecondaryAlignment()} instead.
*/
@Deprecated
public boolean getNotPrimaryAlignmentFlag() {
return (mFlags & SAMFlag.NOT_PRIMARY_ALIGNMENT.flag) != 0;
return isSecondaryAlignment();
}

/**
* the alignment is secondary (a read having slipt hits have multiple alignment records).
*/
public boolean isSecondaryAlignment() {
return (mFlags & SAMFlag.SECONDARY_ALIGNMENT.flag) != 0;
}

/**
Expand Down Expand Up @@ -1063,9 +1072,18 @@ public void setSecondOfPairFlag(final boolean flag) {

/**
* the alignment is not primary (a read having split hits may have multiple primary alignment records).
* @deprecated use {@link #setSecondaryAlignment(boolean)} instead.
*/
@Deprecated
public void setNotPrimaryAlignmentFlag(final boolean flag) {
setFlag(flag, SAMFlag.NOT_PRIMARY_ALIGNMENT.flag);
setSecondaryAlignment(flag);
}

/**
* the alignment is secondary (a read having slipt hits have multiple alignment records).
*/
public void setSecondaryAlignment(final boolean flag) {
setFlag(flag, SAMFlag.SECONDARY_ALIGNMENT.flag);
}

/**
Expand Down Expand Up @@ -1094,7 +1112,7 @@ public void setDuplicateReadFlag(final boolean flag) {
* equivalent to {@code (getNotPrimaryAlignmentFlag() || getSupplementaryAlignmentFlag())}.
*/
public boolean isSecondaryOrSupplementary() {
return getNotPrimaryAlignmentFlag() || getSupplementaryAlignmentFlag();
return isSecondaryAlignment() || getSupplementaryAlignmentFlag();
}

private void setFlag(final boolean flag, final int bit) {
Expand Down Expand Up @@ -1954,9 +1972,9 @@ public List<SAMValidationError> isValid(final boolean firstOnly) {
if (firstOnly) return ret;
}
if (getReadUnmappedFlag()) {
if (getNotPrimaryAlignmentFlag()) {
if (isSecondaryAlignment()) {
if (ret == null) ret = new ArrayList<>();
ret.add(new SAMValidationError(SAMValidationError.Type.INVALID_FLAG_NOT_PRIM_ALIGNMENT, "Not primary alignment flag should not be set for unmapped read.", getReadName()));
ret.add(new SAMValidationError(SAMValidationError.Type.INVALID_FLAG_NOT_PRIM_ALIGNMENT, "Secondary alignment flag should not be set for unmapped read.", getReadName()));
if (firstOnly) return ret;
}
if (getSupplementaryAlignmentFlag()) {
Expand Down Expand Up @@ -2030,7 +2048,7 @@ public List<SAMValidationError> isValid(final boolean firstOnly) {
if (firstOnly) return ret;
}
// TODO(mccowan): Is this asking "is this the primary alignment"?
if (this.getReadLength() == 0 && !this.getNotPrimaryAlignmentFlag()) {
if (this.getReadLength() == 0 && !this.isSecondaryAlignment()) {
final Object fz = getAttribute(SAMTagUtil.getSingleton().FZ);
if (fz == null) {
final String cq = (String)getAttribute(SAMTagUtil.getSingleton().CQ);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public int compare(final SAMRecord samRecord1, final SAMRecord samRecord2) {
if (samRecord1.getReadNegativeStrandFlag() != samRecord2.getReadNegativeStrandFlag()) {
return (samRecord1.getReadNegativeStrandFlag()? 1: -1);
}
if (samRecord1.getNotPrimaryAlignmentFlag() != samRecord2.getNotPrimaryAlignmentFlag()) {
return samRecord2.getNotPrimaryAlignmentFlag()? -1: 1;
if (samRecord1.isSecondaryAlignment() != samRecord2.isSecondaryAlignment()) {
return samRecord2.isSecondaryAlignment()? -1: 1;
}
if (samRecord1.getSupplementaryAlignmentFlag() != samRecord2.getSupplementaryAlignmentFlag()) {
return samRecord2.getSupplementaryAlignmentFlag() ? -1 : 1;
Expand Down
13 changes: 6 additions & 7 deletions src/main/java/htsjdk/samtools/SAMRecordSetBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

import htsjdk.samtools.DuplicateScoringStrategy.ScoringStrategy;
import htsjdk.samtools.util.CloseableIterator;
import htsjdk.samtools.util.CoordMath;
import htsjdk.samtools.util.RuntimeIOException;
import htsjdk.samtools.util.SequenceUtil;

Expand Down Expand Up @@ -287,7 +286,7 @@ public SAMRecord addFrag(final String name, final int contig, final int start, f
final boolean recordUnmapped, final String cigar, final String qualityString,
final int defaultQuality, final boolean isSecondary) throws SAMException {
final htsjdk.samtools.SAMRecord rec = createReadNoFlag(name, contig, start, negativeStrand, recordUnmapped, cigar, qualityString, defaultQuality);
if (isSecondary) rec.setNotPrimaryAlignmentFlag(true);
if (isSecondary) rec.setSecondaryAlignment(true);
this.records.add(rec);
return rec;
}
Expand All @@ -300,7 +299,7 @@ public SAMRecord addFrag(final String name, final int contig, final int start, f
final boolean recordUnmapped, final String cigar, final String qualityString,
final int defaultQuality, final boolean isSecondary, final boolean isSupplementary) throws SAMException {
final htsjdk.samtools.SAMRecord rec = createReadNoFlag(name, contig, start, negativeStrand, recordUnmapped, cigar, qualityString, defaultQuality);
if (isSecondary) rec.setNotPrimaryAlignmentFlag(true);
if (isSecondary) rec.setSecondaryAlignment(true);
if (isSupplementary) rec.setSupplementaryAlignmentFlag(true);
this.records.add(rec);
return rec;
Expand Down Expand Up @@ -434,11 +433,11 @@ public List<SAMRecord> addPair(final String name, final int contig1, final int c
end2.setReadPairedFlag(true);
end2.setSecondOfPairFlag(true);

if (record1NonPrimary) end1.setNotPrimaryAlignmentFlag(true);
if (record2NonPrimary) end2.setNotPrimaryAlignmentFlag(true);
if (record1NonPrimary) end1.setSecondaryAlignment(true);
if (record2NonPrimary) end2.setSecondaryAlignment(true);

if (record1NonPrimary) end1.setNotPrimaryAlignmentFlag(true);
if (record2NonPrimary) end2.setNotPrimaryAlignmentFlag(true);
if (record1NonPrimary) end1.setSecondaryAlignment(true);
if (record2NonPrimary) end2.setSecondaryAlignment(true);

// set mate info
SamPairUtil.setMateInfo(end1, end2, true);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/htsjdk/samtools/SAMUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ public static void makeReadUnmapped(final SAMRecord rec) {
rec.setCigarString(SAMRecord.NO_ALIGNMENT_CIGAR);
rec.setMappingQuality(SAMRecord.NO_MAPPING_QUALITY);
rec.setInferredInsertSize(0);
rec.setNotPrimaryAlignmentFlag(false);
rec.setSecondaryAlignment(false);
rec.setSupplementaryAlignmentFlag(false);
rec.setProperPairFlag(false);
rec.setReadUnmappedFlag(true);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/htsjdk/samtools/SamFlagField.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public String format(final int flag) {
if ((flag & SAMFlag.FIRST_OF_PAIR.flag) != 0) value.append('1');
if ((flag & SAMFlag.SECOND_OF_PAIR.flag) != 0) value.append('2');

if ((flag & SAMFlag.NOT_PRIMARY_ALIGNMENT.flag) != 0) value.append('s');
if ((flag & SAMFlag.SECONDARY_ALIGNMENT.flag) != 0) value.append('s');
if ((flag & SAMFlag.SUPPLEMENTARY_ALIGNMENT.flag) != 0) value.append('S');
if ((flag & SAMFlag.READ_FAILS_VENDOR_QUALITY_CHECK.flag) != 0) value.append('x');
if ((flag & SAMFlag.DUPLICATE_READ.flag) != 0) value.append('d');
Expand All @@ -138,7 +138,7 @@ protected int parseWithoutValidation(final String flag) {
case 'R': value |= SAMFlag.MATE_REVERSE_STRAND.flag; break;
case '1': value |= SAMFlag.FIRST_OF_PAIR.flag; break;
case '2': value |= SAMFlag.SECOND_OF_PAIR.flag; break;
case 's': value |= SAMFlag.NOT_PRIMARY_ALIGNMENT.flag; break;
case 's': value |= SAMFlag.SECONDARY_ALIGNMENT.flag; break;
case 'x': value |= SAMFlag.READ_FAILS_VENDOR_QUALITY_CHECK.flag; break;
case 'd': value |= SAMFlag.DUPLICATE_READ.flag; break;
case 'S': value |= SAMFlag.SUPPLEMENTARY_ALIGNMENT.flag; break;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* The MIT License
*
* Copyright (c) 2009 The Broad Institute
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package htsjdk.samtools;

import htsjdk.samtools.util.CloseableIterator;
import htsjdk.samtools.util.PeekIterator;

/**
* Wrapper around SAMRecord iterator that skips over secondary elements.
* This iterator conflates a filtering iterator and a peekable iterator. It would be cleaner to
* handle those concerns separately.
*/
public class SecondaryAlignmentSkippingIterator {
private final PeekIterator<SAMRecord> it;

public SecondaryAlignmentSkippingIterator(final CloseableIterator<SAMRecord> underlyingIt) {
it = new PeekIterator<>(underlyingIt);
skipAnySecondary();
}

public boolean hasCurrent() {
return it.hasNext();
}

public SAMRecord getCurrent() {
assert(hasCurrent());
return it.peek();
}

public boolean advance() {
it.next();
skipAnySecondary();
return hasCurrent();
}

private void skipAnySecondary() {
while (it.hasNext() && it.peek().isSecondaryAlignment()) {
it.next();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/**
* Wrapper around SAMRecord iterator that skips over secondary and supplementary elements.
* This iterator conflates a filtering iterator and a peekable iterator. It would be cleaner to
* handle those concerns separately. This class should be viewed as a replacement for NotPrimarySkippingIterator,
* handle those concerns separately. This class should be viewed as a replacement for SecondaryAlignmentSkippingIterator,
* in that we did not want to change the functionality of NPSI to no longer match its name
*/
public class SecondaryOrSupplementarySkippingIterator {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private static void copyFlags(final CramCompressionRecord cramRecord, final SAMR
samRecord.setReadNegativeStrandFlag(cramRecord.isNegativeStrand());
samRecord.setFirstOfPairFlag(cramRecord.isFirstSegment());
samRecord.setSecondOfPairFlag(cramRecord.isLastSegment());
samRecord.setNotPrimaryAlignmentFlag(cramRecord.isSecondaryAlignment());
samRecord.setSecondaryAlignment(cramRecord.isSecondaryAlignment());
samRecord.setReadFailsVendorQualityCheckFlag(cramRecord.isVendorFiltered());
samRecord.setDuplicateReadFlag(cramRecord.isDuplicate());
samRecord.setSupplementaryAlignmentFlag(cramRecord.isSupplementary());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public CramCompressionRecord createCramRecord(final SAMRecord record) {
cramRecord.setNegativeStrand(record.getReadNegativeStrandFlag());
cramRecord.setFirstSegment(record.getReadPairedFlag() && record.getFirstOfPairFlag());
cramRecord.setLastSegment(record.getReadPairedFlag() && record.getSecondOfPairFlag());
cramRecord.setSecondaryAlignment(record.getNotPrimaryAlignmentFlag());
cramRecord.setSecondaryAlignment(record.isSecondaryAlignment());
cramRecord.setVendorFiltered(record.getReadFailsVendorQualityCheckFlag());
cramRecord.setDuplicate(record.getDuplicateReadFlag());
cramRecord.setSupplementary(record.getSupplementaryAlignmentFlag());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,34 +23,13 @@
*/
package htsjdk.samtools.filter;

import htsjdk.samtools.SAMRecord;

/**
* Filter out SAMRecords with NotPrimaryAlignment flag set
*
* $Id$
* @deprecated use {@link SecondaryAlignmentFilter} instead.
*/
public class NotPrimaryAlignmentFilter implements SamRecordFilter {
/**
* @param record the SAMRecord to evaluate
* @return true if the SAMRecord matches the filter, otherwise false
*/
@Override
public boolean filterOut(final SAMRecord record) {
return record.getNotPrimaryAlignmentFlag();
}
@Deprecated
public class NotPrimaryAlignmentFilter extends SecondaryAlignmentFilter {

/**
* Determines whether a pair of SAMRecord matches this filter
*
* @param first the first SAMRecord to evaluate
* @param second the second SAMRecord to evaluate
*
* @return true if the SAMRecords matches the filter, otherwise false
*/
@Override
public boolean filterOut(final SAMRecord first, final SAMRecord second) {
// if either fails, exclude them both
return (first.getNotPrimaryAlignmentFlag() || second.getNotPrimaryAlignmentFlag());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ public class SecondaryAlignmentFilter implements SamRecordFilter {
* Returns true if the read is marked as secondary.
*/
@Override
public boolean filterOut(final SAMRecord record) { return record.getNotPrimaryAlignmentFlag(); }
public boolean filterOut(final SAMRecord record) { return record.isSecondaryAlignment(); }

/**
* Returns true if either read is marked as secondary.
*/
@Override
public boolean filterOut(final SAMRecord first, final SAMRecord second) {
return first.getNotPrimaryAlignmentFlag() || second.getNotPrimaryAlignmentFlag();
return first.isSecondaryAlignment() || second.isSecondaryAlignment();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import htsjdk.samtools.SAMRecord;

/**
* Filter out SAMRecords with NotPrimaryAlignment or Supplementary flag set
* This class should be viewed as a replacement for NotPrimarySkippingIterator,
* Filter out SAMRecords with Secondary or Supplementary flag set
* This class should be viewed as a replacement for {@link htsjdk.samtools.NotPrimarySkippingIterator},
* in that we did not want to change the functionality of NPSI to no longer match its name
* $Id$
*/
Expand Down
Loading