-
Notifications
You must be signed in to change notification settings - Fork 243
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
Adding support for 0-length B arrays in SAM files to conform to 1.6 spec #1194
Merged
Merged
Changes from 18 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
06dd1c1
Adding support for 0-length B arrays in SAM files to conform to 1.6 s…
lbergelson 744d7c6
forgot fai
lbergelson 306c309
add space
lbergelson 8aabba8
space
14ff9e4
space
pshapiro4broad ef8c7a2
lowercase tag
5696fb4
responding to comments
lbergelson 133370a
lowercase
lbergelson f7c46fb
adding more tests and further changes
lbergelson ebf496f
fix space
lbergelson 9b7476f
hopefully tests pass...
lbergelson 37630eb
more htsjdktest
lbergelson 98ceb53
Update src/main/java/htsjdk/samtools/TextTagCodec.java
pshapiro4broad 5d591c3
Update src/main/java/htsjdk/samtools/TextTagCodec.java
pshapiro4broad 1d41109
Update src/main/java/htsjdk/samtools/TextTagCodec.java
pshapiro4broad 2306e3f
more comments
lbergelson fbfdd79
working on comments
lbergelson c1e19ec
more test
lbergelson 8bd2258
Update src/test/java/htsjdk/utils/TestNGUtils.java
lbergelson 31cc9ac
Update src/test/java/htsjdk/utils/TestNGUtilsTest.java
lbergelson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,8 @@ public class TextTagCodec { | |
// 3 fields for non-empty strings 2 fields if the string is empty. | ||
private static final int NUM_TAG_FIELDS = 3; | ||
|
||
private static final String[] EMPTY_STRING_ARRAY = new String[0]; | ||
|
||
/** | ||
* This is really a local variable of decode(), but allocated here to reduce allocations. | ||
*/ | ||
|
@@ -52,7 +54,7 @@ public class TextTagCodec { | |
/** | ||
* Convert in-memory representation of tag to SAM text representation. | ||
* @param tagName Two-character tag name. | ||
* @param value Tag value as approriate Object subclass. | ||
* @param value Tag value as appropriate Object subclass. | ||
* @return SAM text String representation, i.e. name:type:value | ||
*/ | ||
public String encode(final String tagName, Object value) { | ||
|
@@ -71,7 +73,7 @@ public String encode(final String tagName, Object value) { | |
// H should never happen anymore. | ||
value = StringUtil.bytesToHexString((byte[])value); | ||
} else if (tagType == 'B') { | ||
value = getArrayType(value, false) + "," + encodeArrayValue(value); | ||
value = getArrayType(value, false) + encodeArrayValue(value); | ||
} else if (tagType == 'i') { | ||
final long longVal = ((Number) value).longValue(); | ||
// as the spec says: [-2^31, 2^32) | ||
|
@@ -83,7 +85,7 @@ public String encode(final String tagName, Object value) { | |
return sb.toString(); | ||
} | ||
|
||
private char getArrayType(final Object array, final boolean isUnsigned) { | ||
private static char getArrayType(final Object array, final boolean isUnsigned) { | ||
final char type; | ||
final Class<?> componentType = array.getClass().getComponentType(); | ||
if (componentType == Float.TYPE) { | ||
|
@@ -97,18 +99,18 @@ private char getArrayType(final Object array, final boolean isUnsigned) { | |
return (isUnsigned? Character.toUpperCase(type): type); | ||
} | ||
|
||
private String encodeArrayValue(final Object value) { | ||
final StringBuilder ret = new StringBuilder(Array.get(value, 0).toString()); | ||
private static String encodeArrayValue(final Object value) { | ||
final int length = Array.getLength(value); | ||
for (int i = 1; i < length; ++i) { | ||
final StringBuilder ret = new StringBuilder(); | ||
for (int i = 0; i < length; ++i) { | ||
ret.append(','); | ||
ret.append(Array.get(value, i).toString()); | ||
} | ||
return ret.toString(); | ||
|
||
} | ||
|
||
private long[] widenToUnsigned(final Object array) { | ||
private static long[] widenToUnsigned(final Object array) { | ||
final Class<?> componentType = array.getClass().getComponentType(); | ||
final long mask; | ||
if (componentType == Byte.TYPE) mask = 0xffL; | ||
|
@@ -127,7 +129,7 @@ String encodeUnsignedArray(final String tagName, final Object array) { | |
throw new IllegalArgumentException("Non-array passed to encodeUnsignedArray: " + array.getClass()); | ||
} | ||
final long[] widened = widenToUnsigned(array); | ||
return tagName + ":B:" + getArrayType(array, true) + "," + encodeArrayValue(widened); | ||
return tagName + ":B:" + getArrayType(array, true) + encodeArrayValue(widened); | ||
} | ||
|
||
/** | ||
|
@@ -175,7 +177,7 @@ public Object setValue(final Object o) { | |
}; | ||
} | ||
|
||
private Object convertStringToObject(final String type, final String stringVal) { | ||
private static Object convertStringToObject(final String type, final String stringVal) { | ||
if (type.equals("Z")) { | ||
return stringVal; | ||
} else if (type.equals("A")) { | ||
|
@@ -219,17 +221,18 @@ else if (SAMUtils.isValidUnsignedIntegerAttribute(lValue)) { | |
} | ||
} | ||
|
||
private Object covertStringArrayToObject(final String stringVal) { | ||
private static Object covertStringArrayToObject(final String stringVal) { | ||
final String[] elementTypeAndValue = new String[2]; | ||
if (StringUtil.splitConcatenateExcessTokens(stringVal, elementTypeAndValue, ',') != 2) { | ||
throw new SAMFormatException("Tag of type B should have an element type followed by comma"); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove NL |
||
final int numberOfTokens = StringUtil.splitConcatenateExcessTokens(stringVal, elementTypeAndValue, ','); | ||
|
||
if (elementTypeAndValue[0].length() != 1) { | ||
throw new SAMFormatException("Unrecognized element type for array tag value: " + elementTypeAndValue[0]); | ||
} | ||
|
||
final char elementType = elementTypeAndValue[0].charAt(0); | ||
final String[] stringValues = elementTypeAndValue[1].split(","); | ||
if (stringValues.length == 0) throw new SAMFormatException("Tag of type B should have at least one element"); | ||
|
||
final String[] stringValues = elementTypeAndValue[1] != null ? elementTypeAndValue[1].split(",") : EMPTY_STRING_ARRAY; | ||
if (elementType == 'f') { | ||
final float[] ret = new float[stringValues.length]; | ||
for (int i = 0; i < stringValues.length; ++i) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,7 +34,7 @@ | |
|
||
public class SAMTextWriterTest extends HtsjdkTest { | ||
|
||
private SAMRecordSetBuilder getSAMReader(final boolean sortForMe, final SAMFileHeader.SortOrder sortOrder) { | ||
private SAMRecordSetBuilder getSamRecordSet(final boolean sortForMe, final SAMFileHeader.SortOrder sortOrder) { | ||
final SAMRecordSetBuilder ret = new SAMRecordSetBuilder(sortForMe, sortOrder); | ||
ret.addPair("readB", 20, 200, 300); | ||
ret.addPair("readA", 20, 100, 150); | ||
|
@@ -45,7 +45,7 @@ private SAMRecordSetBuilder getSAMReader(final boolean sortForMe, final SAMFileH | |
|
||
@Test | ||
public void testNullHeader() throws Exception { | ||
final SAMRecordSetBuilder recordSetBuilder = getSAMReader(true, SAMFileHeader.SortOrder.coordinate); | ||
final SAMRecordSetBuilder recordSetBuilder = getSamRecordSet(true, SAMFileHeader.SortOrder.coordinate); | ||
for (final SAMRecord rec : recordSetBuilder.getRecords()) { | ||
rec.setHeader(null); | ||
} | ||
|
@@ -77,7 +77,7 @@ private void doTest(final SAMRecordSetBuilder recordSetBuilder) throws Exception | |
} | ||
|
||
private void doTest(final SamFlagField samFlagField) throws Exception { | ||
doTest(getSAMReader(true, SAMFileHeader.SortOrder.coordinate), samFlagField); | ||
doTest(getSamRecordSet(true, SAMFileHeader.SortOrder.coordinate), samFlagField); | ||
} | ||
|
||
private void doTest(final SAMRecordSetBuilder recordSetBuilder, final SamFlagField samFlagField) throws Exception { | ||
|
@@ -128,4 +128,12 @@ private void doTest(final SAMRecordSetBuilder recordSetBuilder, final SamFlagFie | |
Assert.assertFalse(newSAMIt.hasNext()); | ||
inputSAM.close(); | ||
} | ||
|
||
@Test | ||
public void testEmptyArrayAttributeHasNoCommaWhenWrittenToSAM(){ | ||
final SAMFileHeader header = new SAMFileHeader(); | ||
final SAMRecord record = new SAMRecord(header); | ||
record.setAttribute("xa", new int[0]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ARRAY_TAG? |
||
Assert.assertTrue(record.getSAMString().endsWith("xa:B:i\n")); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove nl