Skip to content

Commit

Permalink
Gff3Writer provide a hook for providing a custom way to escape strings (
Browse files Browse the repository at this point in the history
#1528)

The current implementation is valid according to the spec, but some downstream tools don't recognize UTF8 correctly.  This adds an overridable method that allows custom subclasses to do their own string escaping to deal with incompatible tools.
  • Loading branch information
lindenb authored Jan 21, 2021
1 parent cc399e5 commit 2e3da45
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/main/java/htsjdk/tribble/gff/Gff3Writer.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ private void tryToWrite(final String string) {

private void writeFirstEightFields(final Gff3Feature feature) throws IOException {
writeJoinedByDelimiter(Gff3Constants.FIELD_DELIMITER, this::tryToWrite, Arrays.asList(
encodeString(feature.getContig()),
encodeString(feature.getSource()),
encodeString(feature.getType()),
escapeString(feature.getContig()),
escapeString(feature.getSource()),
escapeString(feature.getType()),
Integer.toString(feature.getStart()),
Integer.toString(feature.getEnd()),
feature.getScore() < 0 ? Gff3Constants.UNDEFINED_FIELD_VALUE : Double.toString(feature.getScore()),
Expand All @@ -92,7 +92,7 @@ void writeKeyValuePair(final String key, final List<String> values) {
try {
tryToWrite(key);
out.write(Gff3Constants.KEY_VALUE_SEPARATOR);
writeJoinedByDelimiter(Gff3Constants.VALUE_DELIMITER, v -> tryToWrite(encodeString(v)), values);
writeJoinedByDelimiter(Gff3Constants.VALUE_DELIMITER, v -> tryToWrite(escapeString(v)), values);
} catch (final IOException ex) {
throw new TribbleException("error writing out key value pair " + key + " " + values);
}
Expand Down Expand Up @@ -123,6 +123,16 @@ public void addFeature(final Gff3Feature feature) throws IOException {
out.write(Gff3Constants.END_OF_LINE_CHARACTER);
}

/***
* escape a String.
* Default behavior is to call {@link #encodeString(String)}
* @param s the string to be escaped
* @return the escaped string
*/
protected String escapeString(final String s) {
return encodeString(s);
}

static String encodeString(final String s) {
try {
//URLEncoder.encode is hardcoded to change all spaces to +, but we want spaces left unchanged so have to do this
Expand Down

0 comments on commit 2e3da45

Please sign in to comment.