Skip to content

Commit

Permalink
Modernizing SAMTag and SAMTagUtil
Browse files Browse the repository at this point in the history
* Making methods in SAMTagUtil final and deprecating the use of the getSingleton() method.
* This is a breaking change to SAMTagUtil, it will break any subclasses, all other uses should be compatible
  • Loading branch information
lbergelson committed Oct 26, 2018
1 parent 04c05ac commit 3d1a410
Show file tree
Hide file tree
Showing 20 changed files with 171 additions and 104 deletions.
4 changes: 2 additions & 2 deletions src/main/java/htsjdk/samtools/BAMRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ static boolean isSentinelCigar(final Cigar cigar, final int readLength) {
* extracts the CIGAR from the CG tag and places it into the (in memory) cigar.
*/
private void extractCigarFromCGAttribute(final Cigar sentinelCigar) throws IllegalStateException {
final int[] cigarFromCG = (int[]) getAttribute(SAMTagUtil.getSingleton().CG);
final int[] cigarFromCG = (int[]) getAttribute(SAMTagUtil.CG);

if (cigarFromCG == null) return;

Expand Down Expand Up @@ -353,7 +353,7 @@ private void extractCigarFromCGAttribute(final Cigar sentinelCigar) throws Illeg
initializeCigar(decodedCigar);

// remove CG attribute.
setAttribute(SAMTagUtil.getSingleton().CG, null);
setAttribute(SAMTagUtil.CG, null);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public SAMBinaryTagAndUnsignedArrayValue(final short tag, final Object value) {
if (!value.getClass().isArray() || value instanceof float[]) {
throw new IllegalArgumentException("Attribute type " + value.getClass() +
" cannot be encoded as an unsigned array. Tag: " +
SAMTagUtil.getSingleton().makeStringTag(tag));
SAMTagUtil.makeStringTag(tag));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/htsjdk/samtools/SAMBinaryTagAndValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public SAMBinaryTagAndValue(final short tag, final Object value) {
}
if (!isAllowedAttributeValue(value)) {
throw new IllegalArgumentException("Attribute type " + value.getClass() + " not supported. Tag: " +
SAMTagUtil.getSingleton().makeStringTag(tag));
SAMTagUtil.makeStringTag(tag));
}
this.tag = tag;
this.value = value;
Expand Down
33 changes: 17 additions & 16 deletions src/main/java/htsjdk/samtools/SAMRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ public class SAMRecord implements Cloneable, Locatable, Serializable {
/**
* Tags that are known to need the reverse complement if the read is reverse complemented.
*/
@SuppressWarnings("deprecated")
public static List<String> TAGS_TO_REVERSE_COMPLEMENT = Arrays.asList(SAMTag.E2.name(), SAMTag.SQ.name());

/**
Expand Down Expand Up @@ -868,7 +869,7 @@ protected void initializeCigar(final Cigar cigar) {
* @throws ClassCastException if RG tag does not have a String value.
*/
public SAMReadGroupRecord getReadGroup() {
final String rgId = (String)getAttribute(SAMTagUtil.getSingleton().RG);
final String rgId = (String)getAttribute(SAMTagUtil.RG);
if (rgId == null || getHeader() == null) {
return null;
} else {
Expand Down Expand Up @@ -1162,7 +1163,7 @@ public boolean hasAttribute(final String tag) {
* @return Appropriately typed tag value, or null if the requested tag is not present.
*/
public Object getAttribute(final String tag) {
return getAttribute(SAMTagUtil.getSingleton().makeBinaryTag(tag));
return getAttribute(SAMTagUtil.makeBinaryTag(tag));
}

/**
Expand Down Expand Up @@ -1196,7 +1197,7 @@ public Integer getIntegerAttribute(final String tag) {
* @throws {@link htsjdk.samtools.SAMException} if the value is out of range for a 32-bit unsigned value, or not a Number
*/
public Long getUnsignedIntegerAttribute(final String tag) throws SAMException {
return getUnsignedIntegerAttribute(SAMTagUtil.getSingleton().makeBinaryTag(tag));
return getUnsignedIntegerAttribute(SAMTagUtil.makeBinaryTag(tag));
}

/**
Expand All @@ -1219,11 +1220,11 @@ public Long getUnsignedIntegerAttribute(final short tag) throws SAMException {
return lValue;
} else {
throw new SAMException("Unsigned integer value of tag " +
SAMTagUtil.getSingleton().makeStringTag(tag) + " is out of bounds for a 32-bit unsigned integer: " + lValue);
SAMTagUtil.makeStringTag(tag) + " is out of bounds for a 32-bit unsigned integer: " + lValue);
}
} else {
throw new SAMException("Unexpected attribute value data type " + value.getClass() + " for tag " +
SAMTagUtil.getSingleton().makeStringTag(tag));
SAMTagUtil.makeStringTag(tag));
}
}

Expand Down Expand Up @@ -1374,7 +1375,7 @@ public float[] getFloatArrayAttribute(final String tag) {
* @throws SAMException if the tag is not present.
*/
public boolean isUnsignedArrayAttribute(final String tag) {
final SAMBinaryTagAndValue tmp = this.mAttributes.find(SAMTagUtil.getSingleton().makeBinaryTag(tag));
final SAMBinaryTagAndValue tmp = this.mAttributes.find(SAMTagUtil.makeBinaryTag(tag));
if (tmp != null) return tmp.isUnsignedArray();
throw new SAMException("Tag " + tag + " is not present in this SAMRecord");
}
Expand Down Expand Up @@ -1422,7 +1423,7 @@ public void setAttribute(final String tag, final Object value) {
if (value != null && value.getClass().isArray() && Array.getLength(value) == 0) {
throw new IllegalArgumentException("Empty value passed for tag " + tag);
}
setAttribute(SAMTagUtil.getSingleton().makeBinaryTag(tag), value);
setAttribute(SAMTagUtil.makeBinaryTag(tag), value);
}

/**
Expand All @@ -1438,7 +1439,7 @@ public void setUnsignedArrayAttribute(final String tag, final Object value) {
if (Array.getLength(value) == 0) {
throw new IllegalArgumentException("Empty array passed to setUnsignedArrayAttribute for tag " + tag);
}
setAttribute(SAMTagUtil.getSingleton().makeBinaryTag(tag), value, true);
setAttribute(SAMTagUtil.makeBinaryTag(tag), value, true);
}

/**
Expand Down Expand Up @@ -1559,8 +1560,8 @@ public List<SAMTagAndValue> getAttributes() {
SAMBinaryTagAndValue binaryAttributes = getBinaryAttributes();
final List<SAMTagAndValue> ret = new ArrayList<>();
while (binaryAttributes != null) {
ret.add(new SAMTagAndValue(SAMTagUtil.getSingleton().makeStringTag(binaryAttributes.tag),
binaryAttributes.value));
ret.add(new SAMTagAndValue(SAMTagUtil.makeStringTag(binaryAttributes.tag),
binaryAttributes.value));
binaryAttributes = binaryAttributes.getNext();
}
return ret;
Expand Down Expand Up @@ -1772,8 +1773,8 @@ private void addField(final StringBuilder buffer, final String field) {
buffer.append(field);
}

private String formatTagValue(final short tag, final Object value) {
final String tagString = SAMTagUtil.getSingleton().makeStringTag(tag);
private static String formatTagValue(final short tag, final Object value) {
final String tagString = SAMTagUtil.makeStringTag(tag);
if (value == null || value instanceof String) {
return tagString + ":Z:" + value;
} else if (value instanceof Integer || value instanceof Long ||
Expand Down Expand Up @@ -2065,7 +2066,7 @@ public List<SAMValidationError> isValid(final boolean firstOnly) {
*/
}
// Validate the RG ID is found in header
final String rgId = (String)getAttribute(SAMTagUtil.getSingleton().RG);
final String rgId = (String)getAttribute(SAMTagUtil.RG);
if (rgId != null && getHeader() != null && getHeader().getReadGroup(rgId) == null) {
if (ret == null) ret = new ArrayList<>();
ret.add(new SAMValidationError(SAMValidationError.Type.READ_GROUP_NOT_FOUND,
Expand All @@ -2080,10 +2081,10 @@ public List<SAMValidationError> isValid(final boolean firstOnly) {
}
// TODO(mccowan): Is this asking "is this the primary alignment"?
if (this.getReadLength() == 0 && !this.isSecondaryAlignment()) {
final Object fz = getAttribute(SAMTagUtil.getSingleton().FZ);
final Object fz = getAttribute(SAMTagUtil.FZ);
if (fz == null) {
final String cq = (String)getAttribute(SAMTagUtil.getSingleton().CQ);
final String cs = (String)getAttribute(SAMTagUtil.getSingleton().CS);
final String cq = (String)getAttribute(SAMTagUtil.CQ);
final String cs = (String)getAttribute(SAMTagUtil.CS);
if (cq == null || cq.isEmpty() || cs == null || cs.isEmpty()) {
if (ret == null) ret = new ArrayList<>();
ret.add(new SAMValidationError(SAMValidationError.Type.EMPTY_READ,
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/htsjdk/samtools/SAMRecordSetBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public void setHeader(final SAMFileHeader header) {
*/
public void addRecord(final SAMRecord record) {
if (record.getReadPairedFlag() && !record.getMateUnmappedFlag() &&
null == record.getAttribute(SAMTagUtil.getSingleton().MC)) {
null == record.getAttribute(SAMTagUtil.MC)) {
throw new SAMException("Mate Cigar tag (MC) not found in: " + record.getReadName());
}
this.records.add(record);
Expand Down
38 changes: 30 additions & 8 deletions src/main/java/htsjdk/samtools/SAMTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,25 @@ public enum SAMTag {
FS,
FT,
FZ,
GC, // for backwards compatibility
/** for backwards compatibility only */
@Deprecated
GC,
/** @deprecated for backwards compatibility only */
@Deprecated
GS, // for backwards compatibility
GQ, // for backwards compatibility
/** @deprecated for backwards compatibility only */
@Deprecated
GQ,
LB,
H0,
H1,
H2,
HI,
IH,
MC,
MF, // for backwards compatibility
/** @deprecated for backwards compatibility only */
@Deprecated
MF,
MI,
MD,
MQ,
Expand All @@ -79,19 +87,33 @@ public enum SAMTag {
QX,
R2,
RG,

/**
* @deprecated use BC instead
* @deprecated use BC instead, for backwards compatibilty only
*/
@Deprecated
RT,

RX,
/** @deprecated for backwards compatibility only */
@Deprecated
S2, // for backwards compatibility
SA,
SM,
/** @deprecated for backwards compatibility only */
@Deprecated
SQ, // for backwards compatibility
TC,
U2,
UQ,
}
UQ;

private final short shortValue = SAMTagUtil.makeBinaryTag(this.name());

/**
* Get the binary representation of this tag name.
* @see SAMTagUtil#makeBinaryTag(String)
*
* @return the binary representation of this tag name
*/
public short getBinaryTag(){
return this.shortValue;
}
}
Loading

0 comments on commit 3d1a410

Please sign in to comment.