@Override public BCF2Type getDynamicType(final Object value) { return value == null ? BCF2Type.INT8 : BCF2Utils.determineIntegerType((int[])value); }
@Override public BCF2Type getDynamicType(final Object value) { return value == null ? BCF2Type.INT8 : BCF2Utils.determineIntegerType((Integer)value); }
public static BCF2Type determineIntegerType(final int[] values) { // find the min and max values in the array int max = 0, min = 0; for ( final int v : values ) { if ( v > max ) max = v; if ( v < min ) min = v; } final BCF2Type maxType = determineIntegerType(max); final BCF2Type minType = determineIntegerType(min); // INT8 < INT16 < INT32 so this returns the larger of the two return maxType.compareTo(minType) >= 0 ? maxType : minType; }
public static BCF2Type determineIntegerType(final int[] values) { // find the min and max values in the array int max = 0, min = 0; for ( final int v : values ) { if ( v > max ) max = v; if ( v < min ) min = v; } final BCF2Type maxType = determineIntegerType(max); final BCF2Type minType = determineIntegerType(min); // INT8 < INT16 < INT32 so this returns the larger of the two return maxType.compareTo(minType) >= 0 ? maxType : minType; }
private final BCF2Type determineBCFType(final Object arg) { final Object toType = arg instanceof List ? ((List)arg).get(0) : arg; if ( toType instanceof Integer ) return BCF2Utils.determineIntegerType((Integer) toType); else if ( toType instanceof String ) return BCF2Type.CHAR; else if ( toType instanceof Double ) return BCF2Type.FLOAT; else throw new IllegalArgumentException("No native encoding for Object of type " + arg.getClass().getSimpleName()); }
private final BCF2Type determineBCFType(final Object arg) { final Object toType = arg instanceof List ? ((List)arg).get(0) : arg; if ( toType instanceof Integer ) return BCF2Utils.determineIntegerType((Integer) toType); else if ( toType instanceof String ) return BCF2Type.CHAR; else if ( toType instanceof Double ) return BCF2Type.FLOAT; else throw new IllegalArgumentException("No native encoding for Object of type " + arg.getClass().getSimpleName()); }
private BCF2Type encodeStringsByRef(final Collection<String> strings) throws IOException { final List<Integer> offsets = new ArrayList<Integer>(strings.size()); // iterate over strings until we find one that needs 16 bits, and break for ( final String string : strings ) { final Integer got = stringDictionaryMap.get(string); if ( got == null ) throw new IllegalStateException("Format error: could not find string " + string + " in header as required by BCF"); final int offset = got; offsets.add(offset); } final BCF2Type type = BCF2Utils.determineIntegerType(offsets); encoder.encodeTyped(offsets, type); return type; }
private BCF2Type encodeStringsByRef(final Collection<String> strings) throws IOException { final List<Integer> offsets = new ArrayList<Integer>(strings.size()); // iterate over strings until we find one that needs 16 bits, and break for ( final String string : strings ) { final Integer got = stringDictionaryMap.get(string); if ( got == null ) throw new IllegalStateException("Format error: could not find string " + string + " in header as required by BCF"); final int offset = got; offsets.add(offset); } final BCF2Type type = BCF2Utils.determineIntegerType(offsets); encoder.encodeTyped(offsets, type); return type; }
@Ensures("result.isIntegerType()") public static BCF2Type determineIntegerType(final int[] values) { // find the min and max values in the array int max = 0, min = 0; for ( final int v : values ) { if ( v > max ) max = v; if ( v < min ) min = v; } final BCF2Type maxType = determineIntegerType(max); final BCF2Type minType = determineIntegerType(min); // INT8 < INT16 < INT32 so this returns the larger of the two return maxType.compareTo(minType) >= 0 ? maxType : minType; }
public static BCF2Type determineIntegerType(final List<Integer> values) { BCF2Type maxType = BCF2Type.INT8; for ( final int value : values ) { final BCF2Type type1 = determineIntegerType(value); switch ( type1 ) { case INT8: break; case INT16: maxType = BCF2Type.INT16; break; case INT32: return BCF2Type.INT32; // fast path for largest possible value default: throw new TribbleException("Unexpected integer type " + type1 ); } } return maxType; }
@Requires("arg != null") private final BCF2Type determineBCFType(final Object arg) { final Object toType = arg instanceof List ? ((List)arg).get(0) : arg; if ( toType instanceof Integer ) return BCF2Utils.determineIntegerType((Integer) toType); else if ( toType instanceof String ) return BCF2Type.CHAR; else if ( toType instanceof Double ) return BCF2Type.FLOAT; else throw new IllegalArgumentException("No native encoding for Object of type " + arg.getClass().getSimpleName()); }
public static BCF2Type determineIntegerType(final List<Integer> values) { BCF2Type maxType = BCF2Type.INT8; for ( final int value : values ) { final BCF2Type type1 = determineIntegerType(value); switch ( type1 ) { case INT8: break; case INT16: maxType = BCF2Type.INT16; break; case INT32: return BCF2Type.INT32; // fast path for largest possible value default: throw new TribbleException("Unexpected integer type " + type1 ); } } return maxType; }
public final void encodeTypedInt(final int v) throws IOException { final BCF2Type type = BCF2Utils.determineIntegerType(v); encodeTypedInt(v, type); }
private BCF2FieldEncoder(final VCFCompoundHeaderLine headerLine, final Map<String, Integer> dict, final BCF2Type staticType) { this.headerLine = headerLine; this.staticType = staticType; final Integer offset = dict.get(getField()); if ( offset == null ) throw new IllegalStateException("Format error: could not find string " + getField() + " in header as required by BCF"); this.dictionaryOffset = offset; dictionaryOffsetType = BCF2Utils.determineIntegerType(offset); }
private BCF2FieldEncoder(final VCFCompoundHeaderLine headerLine, final Map<String, Integer> dict, final BCF2Type staticType) { this.headerLine = headerLine; this.staticType = staticType; final Integer offset = dict.get(getField()); if ( offset == null ) throw new IllegalStateException("Format error: could not find string " + getField() + " in header as required by BCF"); this.dictionaryOffset = offset; dictionaryOffsetType = BCF2Utils.determineIntegerType(offset); }
@Ensures("encodeStream.size() > old(encodeStream.size())") public final void encodeTypedInt(final int v) throws IOException { final BCF2Type type = BCF2Utils.determineIntegerType(v); encodeTypedInt(v, type); }
@Requires({"headerLine != null", "dict != null"}) private BCF2FieldEncoder(final VCFCompoundHeaderLine headerLine, final Map<String, Integer> dict, final BCF2Type staticType) { this.headerLine = headerLine; this.staticType = staticType; final Integer offset = dict.get(getField()); if ( offset == null ) throw new IllegalStateException("Format error: could not find string " + getField() + " in header as required by BCF"); this.dictionaryOffset = offset; dictionaryOffsetType = BCF2Utils.determineIntegerType(offset); }
@Test(dataProvider = "BestIntTypeTests") public void determineBestEncoding(final List<Integer> ints, final BCF2Type expectedType) throws IOException { BCF2Encoder encoder = new BCF2Encoder(); Assert.assertEquals(BCF2Utils.determineIntegerType(ints), expectedType); Assert.assertEquals(BCF2Utils.determineIntegerType(toPrimitive(ints.toArray(new Integer[0]))), expectedType); }
@Override public void start(final BCF2Encoder encoder, final VariantContext vc) throws IOException { // the only value that is dynamic are integers final List<Integer> values = new ArrayList<Integer>(vc.getNSamples()); for ( final Genotype g : vc.getGenotypes() ) { for ( final Integer i : BCF2Utils.toList(Integer.class, g.getExtendedAttribute(getField(), null)) ) { if ( i != null ) values.add(i); } } encodingType = BCF2Utils.determineIntegerType(values); super.start(encoder, vc); } }
@Override public void start(final BCF2Encoder encoder, final VariantContext vc) throws IOException { // the only value that is dynamic are integers final List<Integer> values = new ArrayList<Integer>(vc.getNSamples()); for ( final Genotype g : vc.getGenotypes() ) { for ( final Object i : BCF2Utils.toList(g.getExtendedAttribute(getField(), null)) ) { if ( i != null ) values.add((Integer)i); // we know they are all integers } } encodingType = BCF2Utils.determineIntegerType(values); super.start(encoder, vc); } }