Java 类com.google.protobuf.Value 实例源码

项目:curiostack    文件:WellKnownTypeMarshaller.java   
@Override
public void doMerge(JsonParser parser, int currentDepth, Message.Builder messageBuilder)
    throws IOException {
  Value.Builder builder = (Value.Builder) messageBuilder;
  JsonToken token = parser.currentToken();
  if (token.isBoolean()) {
    builder.setBoolValue(ParseSupport.parseBool(parser));
  } else if (token.isNumeric()) {
    builder.setNumberValue(ParseSupport.parseDouble(parser));
  } else if (token == JsonToken.VALUE_NULL) {
    builder.setNullValue(NullValue.NULL_VALUE);
  } else if (token.isScalarValue()) {
    builder.setStringValue(ParseSupport.parseString(parser));
  } else if (token == JsonToken.START_OBJECT) {
    Struct.Builder structBuilder = builder.getStructValueBuilder();
    StructMarshaller.INSTANCE.mergeValue(parser, currentDepth + 1, structBuilder);
  } else if (token == JsonToken.START_ARRAY) {
    ListValue.Builder listValueBuilder = builder.getListValueBuilder();
    ListValueMarshaller.INSTANCE.mergeValue(parser, currentDepth + 1, listValueBuilder);
  } else {
    throw new IllegalStateException("Unexpected json data: " + parser.getText());
  }
}
项目:curiostack    文件:WellKnownTypeMarshaller.java   
@Override
public void doWrite(Value message, JsonGenerator gen) throws IOException {
  switch (message.getKindCase()) {
    case NULL_VALUE:
      SerializeSupport.printNull(0, gen);
      break;
    case NUMBER_VALUE:
      SerializeSupport.printDouble(message.getNumberValue(), gen);
      break;
    case STRING_VALUE:
      SerializeSupport.printString(message.getStringValue(), gen);
      break;
    case BOOL_VALUE:
      SerializeSupport.printBool(message.getBoolValue(), gen);
      break;
    case STRUCT_VALUE:
      StructMarshaller.INSTANCE.writeValue(message.getStructValue(), gen);
      break;
    case LIST_VALUE:
      ListValueMarshaller.INSTANCE.writeValue(message.getListValue(), gen);
      break;
    case KIND_NOT_SET:
      SerializeSupport.printNull(0, gen);
      break;
  }
}
项目:curiostack    文件:MessageMarshallerTest.java   
@Test
public void anyInMaps() throws Exception {
  TestAny.Builder testAny = TestAny.newBuilder();
  testAny.putAnyMap("int32_wrapper", Any.pack(Int32Value.newBuilder().setValue(123).build()));
  testAny.putAnyMap("int64_wrapper", Any.pack(Int64Value.newBuilder().setValue(456).build()));
  testAny.putAnyMap("timestamp", Any.pack(Timestamps.parse("1969-12-31T23:59:59Z")));
  testAny.putAnyMap("duration", Any.pack(Durations.parse("12345.1s")));
  testAny.putAnyMap("field_mask", Any.pack(FieldMaskUtil.fromString("foo.bar,baz")));
  Value numberValue = Value.newBuilder().setNumberValue(1.125).build();
  Struct.Builder struct = Struct.newBuilder();
  struct.putFields("number", numberValue);
  testAny.putAnyMap("struct", Any.pack(struct.build()));
  Value nullValue = Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build();
  testAny.putAnyMap(
      "list_value",
      Any.pack(ListValue.newBuilder().addValues(numberValue).addValues(nullValue).build()));
  testAny.putAnyMap("number_value", Any.pack(numberValue));
  testAny.putAnyMap("any_value_number", Any.pack(Any.pack(numberValue)));
  testAny.putAnyMap("any_value_default", Any.pack(Any.getDefaultInstance()));
  testAny.putAnyMap("default", Any.getDefaultInstance());

  assertMatchesUpstream(testAny.build(), TestAllTypes.getDefaultInstance());
}
项目:endpoints-management-java    文件:ReportRequestInfoTest.java   
private static LogEntry.Builder newTestLogEntry(String name, int responseCode) {
  Value.Builder vb = Value.newBuilder();
  Map<String, Value> values = Maps.newHashMap();
  values.put("api_key", vb.setStringValue(TEST_API_KEY).build());
  values.put("http_method", vb.setStringValue("GET").build());
  values.put("timestamp",
      vb.setNumberValue(TEST_CLOCK.currentTimeMillis()).build());
  values.put("http_response_code", vb.setNumberValue(responseCode).build());
  values.put("response_size", vb.setNumberValue(TEST_SIZE).build());
  values.put("request_size", vb.setNumberValue(TEST_SIZE).build());
  if (responseCode >= 400) {
    values.put("error_cause", vb.setStringValue("internal").build());
  }
  return LogEntry
      .newBuilder()
      .setStructPayload(Struct.newBuilder().putAllFields(values))
      .setName(name)
      .setSeverity(responseCode >= 400 ? LogSeverity.ERROR : LogSeverity.INFO)
      .setTimestamp(REALLY_EARLY);
}
项目:genomewarp    文件:VcfToVariant.java   
private static Value createTypedValue(VCFHeaderLineType type, Object value) {
  if (type == VCFHeaderLineType.Flag) {
    return Value.newBuilder().setBoolValue((Boolean) value).build();
  }

  // Booleans are given as Boolean objects. Strangely, Floats and Integers
  // are given as String objects by HTSJDK.
  if (!(value instanceof String)) {
    throw new IllegalStateException("Received non-Boolean, non-List type in non-String format. "
        + "This is most likely due to a change in htsjdk's library.");
  }

  String stringValue = (String) value;
  boolean isNumeric = stringValue.matches("[-+]?\\d+(\\.\\d+)?");

  if (type == VCFHeaderLineType.Integer && isNumeric) {
    return Value.newBuilder().setNumberValue(Integer.parseInt(stringValue)).build();
  }

  if (type == VCFHeaderLineType.Float && isNumeric) {
    return Value.newBuilder().setNumberValue(Double.parseDouble(stringValue)).build();
  }


  return Value.newBuilder().setStringValue(stringValue).build();
}
项目:genomewarp    文件:VcfToVariant.java   
private static boolean parseInlineGenotypeFields(String field, VariantCall.Builder vcBuilder,
    ListValue.Builder lvBuilder, IntGenotypeFieldAccessors.Accessor accessor, Genotype g) {

  final int[] intValues = accessor.getValues(g);
  if (intValues == null || intValues.length == 0) {
    return false;
  }

  if (field.equals(VCFConstants.GENOTYPE_PL_KEY)) {
    // HTSJDK folds GL's into PL's. We only use PL's to store genotype likelihood.
    for (int i = 0; i < intValues.length; i++) {
      // We add 0.0 to remove the possiblity of getting -0.0.
      vcBuilder.addGenotypeLikelihood(-(double) intValues[i] / 10.0 + 0.0);
    }
    return false;
  }

  for (int i = 0; i < intValues.length; i++) {
    lvBuilder.addValues(Value.newBuilder().setNumberValue(intValues[i]));
  }
  return true;
}
项目:dataflow-java    文件:VerifyBamIdTest.java   
@Test
public void testGetAlleleFreq() throws Exception {
  DoFnTester<Variant, KV<Position, AlleleFreq>> getAlleleFreq = DoFnTester.of(
      new GetAlleleFreq());
  Position pos = Position.newBuilder()
      .setReferenceName("1")
      .setPosition(123L)
      .build();
  Variant.Builder vBuild = Variant.newBuilder()
      .setReferenceName("1")
      .setStart(123L)
      .setReferenceBases("C")
      .addAlternateBases("T");
  vBuild.getMutableInfo().put("AF", ListValue.newBuilder()
      .addValues(Value.newBuilder().setStringValue("0.25").build()).build());
  AlleleFreq af = new AlleleFreq();
  af.setAltBases(Lists.newArrayList("T"));
  af.setRefBases("C");
  af.setRefFreq(0.25);
  Assert.assertThat(getAlleleFreq.processBundle(vBuild.build()),
      CoreMatchers.hasItems(KV.of(pos, af)));
}
项目:rejoiner    文件:GraphQlStreamObserver.java   
@Override
public void onNext(T value) {
  List<Value> path =
      dataFetchingEnvironment
          .getFieldTypeInfo()
          .getPath()
          .toList()
          .stream()
          .map(
              p ->
                  p instanceof Number
                      ? Value.newBuilder()
                          .setNumberValue(Double.parseDouble(p.toString()))
                          .build()
                      : Value.newBuilder().setStringValue(p.toString()).build())
          .collect(ImmutableList.toImmutableList());

  ListValue pathListVale =
      ListValue.newBuilder()
          .addAllValues(path)
          .addValues(Value.newBuilder().setNumberValue(pathIndex.incrementAndGet()))
          .build();

  R graphQlResponse = getData(value, pathListVale);

  rejoinerStreamingContext.responseStreamObserver().onNext(graphQlResponse);

  try {
    System.out.println(
        "Streaming response as Json: " + JsonFormat.printer().print(graphQlResponse));
  } catch (InvalidProtocolBufferException e) {
    throw new RuntimeException(e);
  }
}
项目:curiostack    文件:WellKnownTypeMarshaller.java   
@Override
protected void doWrite(Struct message, JsonGenerator gen) throws IOException {
  for (Map.Entry<String, Value> entry : message.getFieldsMap().entrySet()) {
    gen.writeFieldName(entry.getKey());
    ValueMarshaller.INSTANCE.writeValue(entry.getValue(), gen);
  }
}
项目:curiostack    文件:WellKnownTypeMarshaller.java   
@Override
public void doMerge(JsonParser parser, int currentDepth, Message.Builder messageBuilder)
    throws IOException {
  JsonToken token = parser.currentToken();
  if (token != JsonToken.START_ARRAY) {
    throw new InvalidProtocolBufferException("Expect an array but found: " + parser.getText());
  }
  ListValue.Builder builder = (ListValue.Builder) messageBuilder;
  while (parser.nextValue() != JsonToken.END_ARRAY) {
    Value.Builder valueBuilder = builder.addValuesBuilder();
    ValueMarshaller.INSTANCE.mergeValue(parser, currentDepth + 1, valueBuilder);
  }
}
项目:curiostack    文件:WellKnownTypeMarshaller.java   
@Override
public void doWrite(ListValue message, JsonGenerator gen) throws IOException {
  List<Value> values = message.getValuesList();
  int numElements = values.size();
  gen.writeStartArray(numElements);
  for (int i = 0; i < numElements; i++) {
    ValueMarshaller.INSTANCE.writeValue(values.get(i), gen);
  }
  gen.writeEndArray();
}
项目:curiostack    文件:DoParse.java   
/**
 * Determines whether we skip processing of the field if it is null. We usually skip null values
 * in the JSON to treat them as default, but must actually process the null for {@link Value} and
 * {@link NullValue} because it means their value must be set.
 */
private boolean mustSkipNull(FieldDescriptor field) {
  if (field.isRepeated()) {
    return true;
  }
  if (field.getJavaType() == JavaType.MESSAGE
      && field.getMessageType() == Value.getDescriptor()) {
    return false;
  }
  if (field.getJavaType() == JavaType.ENUM && field.getEnumType() == NullValue.getDescriptor()) {
    return false;
  }
  return true;
}
项目:curiostack    文件:MessageMarshallerTest.java   
@Test
public void struct() throws Exception {
  // Build a struct with all possible values.
  TestStruct.Builder builder = TestStruct.newBuilder();
  Struct.Builder structBuilder = builder.getStructValueBuilder();
  structBuilder.putFields("null_value", Value.newBuilder().setNullValueValue(0).build());
  structBuilder.putFields("number_value", Value.newBuilder().setNumberValue(1.25).build());
  structBuilder.putFields("string_value", Value.newBuilder().setStringValue("hello").build());
  Struct.Builder subStructBuilder = Struct.newBuilder();
  subStructBuilder.putFields("number_value", Value.newBuilder().setNumberValue(1234).build());
  structBuilder.putFields(
      "struct_value", Value.newBuilder().setStructValue(subStructBuilder.build()).build());
  ListValue.Builder listBuilder = ListValue.newBuilder();
  listBuilder.addValues(Value.newBuilder().setNumberValue(1.125).build());
  listBuilder.addValues(Value.newBuilder().setNullValueValue(0).build());
  structBuilder.putFields(
      "list_value", Value.newBuilder().setListValue(listBuilder.build()).build());
  TestStruct message = builder.build();
  assertMatchesUpstream(message);

  builder = TestStruct.newBuilder();
  builder.setValue(Value.newBuilder().setNullValueValue(0).build());
  message = builder.build();
  assertMatchesUpstream(message);

  builder = TestStruct.newBuilder();
  listBuilder = builder.getListValueBuilder();
  listBuilder.addValues(Value.newBuilder().setNumberValue(31831.125).build());
  listBuilder.addValues(Value.newBuilder().setNullValueValue(0).build());
  message = builder.build();
  assertMatchesUpstream(message);
}
项目:seldon-core    文件:PredictorUtils.java   
public static DefaultData tensorToNDArray(DefaultData data){
    if (data.getDataOneofCase() == DataOneofCase.TENSOR)
    {
        List<Double> valuesList = data.getTensor().getValuesList();
        List<Integer> shapeList = data.getTensor().getShapeList();

        DefaultData.Builder dataBuilder = DefaultData.newBuilder();

        int index=0;
        for (Iterator<String> i = data.getNamesList().iterator(); i.hasNext();){
            dataBuilder.setNames(index, i.next());
            index++;
        }

        ListValue.Builder b1 = ListValue.newBuilder();
        for (int i = 0; i < shapeList.get(0); ++i) {
            ListValue.Builder b2 = ListValue.newBuilder();
            for (int j = 0; j < shapeList.get(1); j++){
                b2.addValues(Value.newBuilder().setNumberValue(valuesList.get(i*shapeList.get(1))+j));
            }
            b1.addValues(Value.newBuilder().setListValue(b2.build()));
        }
        dataBuilder.setNdarray(b1.build());

        return dataBuilder.build();

    }
    else if (data.getDataOneofCase() == DataOneofCase.NDARRAY)
    {
        return data;
    }
    return null;
}
项目:seldon-core    文件:PredictorUtils.java   
public static DefaultData updateData(DefaultData oldData, INDArray newData){
        DefaultData.Builder dataBuilder = DefaultData.newBuilder();

        dataBuilder.addAllNames(oldData.getNamesList());

//      int index=0;
//      for (Iterator<String> i = oldData.getFeaturesList().iterator(); i.hasNext();){
//          dataBuilder.setFeatures(index, i.next());
//          index++;
//      }

        if (oldData.getDataOneofCase() == DataOneofCase.TENSOR){
            Tensor.Builder tBuilder = Tensor.newBuilder();
            List<Integer> shapeList = Arrays.stream(newData.shape()).boxed().collect(Collectors.toList());
            tBuilder.addAllShape(shapeList);

            for (int i=0; i<shapeList.get(0); ++i){
                for (int j=0; j<shapeList.get(1); ++j){
                    tBuilder.addValues(newData.getDouble(i,j));
                }
            }
            dataBuilder.setTensor(tBuilder);
            return dataBuilder.build();
        }
        else if (oldData.getDataOneofCase() == DataOneofCase.NDARRAY){
            ListValue.Builder b1 = ListValue.newBuilder();
            for (int i = 0; i < newData.shape()[0]; ++i) {
                ListValue.Builder b2 = ListValue.newBuilder();
                for (int j = 0; j < newData.shape()[1]; j++){
                    b2.addValues(Value.newBuilder().setNumberValue(newData.getDouble(i,j)));
                }
                b1.addValues(Value.newBuilder().setListValue(b2.build()));
            }
            dataBuilder.setNdarray(b1.build());
            return dataBuilder.build();
        }
        return null;

    }
项目:seldon-core    文件:TestPredictionProto.java   
@Test
public void defaultRequest() throws InvalidProtocolBufferException
{
    String[] features = {"a","b"};
    Double[] values = {1.0,2.0,1.5,2.2};
    DefaultData.Builder defB = DefaultData.newBuilder();
    defB.addAllNames( Arrays.asList(features) );
    defB.setTensor(Tensor.newBuilder().addShape(1).addShape(values.length).addAllValues(Arrays.asList(values)).build());
    SeldonMessage.Builder b = SeldonMessage.newBuilder();
    Value v;
    Value v1 = Value.newBuilder().setNumberValue(1.0).build();

    b.setData(defB.build()).setMeta(Meta.newBuilder().putTags("key", v1).build());
    SeldonMessage request = b.build();

    String json = ProtoBufUtils.toJson(request);

    System.out.println(json);

    SeldonMessage.Builder b2 = SeldonMessage.newBuilder();
    ProtoBufUtils.updateMessageBuilderFromJson(b2, json);

    SeldonMessage request2 = b2.build();

    String json2 = ProtoBufUtils.toJson(request2);

    System.out.println(json2);

    Assert.assertEquals(json, json2);

}
项目:seldon-core    文件:AverageCombinerTest.java   
@Test
public void testSimpleNDArrayCase() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException {
    List<SeldonMessage> predictorReturns = new ArrayList<>();
    String[] names = {"c","d"};

    Double[] values1 = {1.0,1.0};
    predictorReturns.add(SeldonMessage.newBuilder().setStatus(Status.newBuilder().setStatus(Status.StatusFlag.SUCCESS).build())
            .setData(DefaultData.newBuilder().addAllNames(Arrays.asList(names))
                    .setNdarray(ListValue.newBuilder().addValues(Value.newBuilder().setListValue(ListValue.newBuilder()
                            .addValues(Value.newBuilder().setNumberValue(values1[0]))
                            .addValues(Value.newBuilder().setNumberValue(values1[1])).build())).build()).build()).build());

    Double[] values2 = {1.0,0.5};
    predictorReturns.add(SeldonMessage.newBuilder().setStatus(Status.newBuilder().setStatus(Status.StatusFlag.SUCCESS).build())
            .setData(DefaultData.newBuilder().addAllNames(Arrays.asList(names))
                    .setNdarray(ListValue.newBuilder().addValues(Value.newBuilder().setListValue(ListValue.newBuilder()
                            .addValues(Value.newBuilder().setNumberValue(values2[0]))
                            .addValues(Value.newBuilder().setNumberValue(values2[1])).build())).build()).build()).build());

    Double[] values3 = {2.2,0.9};
    predictorReturns.add(SeldonMessage.newBuilder().setStatus(Status.newBuilder().setStatus(Status.StatusFlag.SUCCESS).build())
            .setData(DefaultData.newBuilder().addAllNames(Arrays.asList(names))
                    .setNdarray(ListValue.newBuilder().addValues(Value.newBuilder().setListValue(ListValue.newBuilder()
                            .addValues(Value.newBuilder().setNumberValue(values3[0]))
                            .addValues(Value.newBuilder().setNumberValue(values3[1])).build())).build()).build()).build());

    AverageCombinerUnit averageCombinerUnit = new AverageCombinerUnit();

    SeldonMessage average = averageCombinerUnit.aggregate(predictorReturns, null);

    Assert.assertThat(average.getData().getNamesList().get(0),is(names[0]));

    Double[][] expected_values = {{(1.0+1.0+2.2)/3,(1.0+0.5+0.9)/3}};
    Assert.assertEquals(expected_values[0][0],average.getData().getNdarray().getValues(0).getListValue().getValues(0).getNumberValue(),1e-7);
}
项目:api-compiler    文件:TypesBuilderFromDescriptor.java   
/**
 * Creates additional types (Value, Struct and ListValue) to be added to the Service config.
 * TODO (guptasu): Fix this hack. Find a better way to add the predefined types.
 * TODO (guptasu): Add them only when required and not in all cases.
 */

static Iterable<Type> createAdditionalServiceTypes() {
  Map<String, DescriptorProto> additionalMessages = Maps.newHashMap();
  additionalMessages.put(Struct.getDescriptor().getFullName(),
      Struct.getDescriptor().toProto());
  additionalMessages.put(Value.getDescriptor().getFullName(),
      Value.getDescriptor().toProto());
  additionalMessages.put(ListValue.getDescriptor().getFullName(),
      ListValue.getDescriptor().toProto());
  additionalMessages.put(Empty.getDescriptor().getFullName(),
      Empty.getDescriptor().toProto());
  additionalMessages.put(Int32Value.getDescriptor().getFullName(),
      Int32Value.getDescriptor().toProto());
  additionalMessages.put(DoubleValue.getDescriptor().getFullName(),
      DoubleValue.getDescriptor().toProto());
  additionalMessages.put(BoolValue.getDescriptor().getFullName(),
      BoolValue.getDescriptor().toProto());
  additionalMessages.put(StringValue.getDescriptor().getFullName(),
      StringValue.getDescriptor().toProto());

  for (Descriptor descriptor : Struct.getDescriptor().getNestedTypes()) {
    additionalMessages.put(descriptor.getFullName(), descriptor.toProto());
  }

  // TODO (guptasu): Remove this hard coding. Without this, creation of Model from Service throws.
  // Needs investigation.
  String fileName = "struct.proto";
  List<Type> additionalTypes = Lists.newArrayList();
  for (String typeName : additionalMessages.keySet()) {
    additionalTypes.add(TypesBuilderFromDescriptor.createType(typeName,
        additionalMessages.get(typeName), fileName));
  }
  return additionalTypes;
}
项目:genomewarp    文件:VariantToVcf.java   
private static String getKind(Value.KindCase kind) {
  switch (kind) {
      case NULL_VALUE: return "NULL_VALUE";
      case NUMBER_VALUE: return "NUMBER_VALUE";
      case STRING_VALUE: return "STRING_VALUE";
      case BOOL_VALUE: return "BOOL_VALUE";
      case STRUCT_VALUE: return "STRUCT_VALUE";
      case LIST_VALUE: return "LIST_VALUE";
      default: return "KIND_NOT_SET";
  }
}
项目:genomewarp    文件:VariantToVcf.java   
private static Object parseObject(String key, Value in, VCFHeaderLineType type) {
  // Case on type
  switch (in.getKindCase()) {
      case NULL_VALUE: throw new IllegalStateException(String.format("field %s contained "
          + "a null value", key));
      case NUMBER_VALUE: return getNumberValue(in.getNumberValue(), type);
      case STRING_VALUE: return in.getStringValue();
      case BOOL_VALUE: return (Boolean) in.getBoolValue();
      default: throw new IllegalStateException(String.format("field %s contained a %s type, which"
          + " is not supported by VariantToVcf", key, getKind(in.getKindCase())));
  }
}
项目:genomewarp    文件:GenomeWarpTestUtils.java   
private static Value valueFromObject(Object v) {
  if (v instanceof Boolean) {
    return Value.newBuilder().setBoolValue((Boolean) v).build();
  }
  if (v instanceof Integer) {
    return Value.newBuilder().setNumberValue((Integer) v).build();
  }
  if (v instanceof Double) {
    return Value.newBuilder().setNumberValue((Double) v).build();
  }

  return Value.newBuilder().setStringValue((String) v).build();
}
项目:codelabs    文件:TransformNonVariantSegmentDataTest.java   
@Test
public void testFilterVariantCallsFn_AllCallsRemoved() throws Exception {

  Map<String, ListValue> passingFilter = new HashMap<String, ListValue>();
  passingFilter.put(
      TransformNonVariantSegmentData.FILTER_FIELD,
      ListValue.newBuilder()
          .addValues(Value.newBuilder().setStringValue(TransformNonVariantSegmentData.PASSING_FILTER).build())
          .build());
  VariantCall passingCall = VariantCall.newBuilder().putAllInfo(passingFilter).build();

  Map<String, ListValue> failingFilter = new HashMap<String, ListValue>();
  failingFilter.put(
      TransformNonVariantSegmentData.FILTER_FIELD,
      ListValue.newBuilder()
          .addValues(Value.newBuilder().setStringValue("VQSRTrancheSNP99.90to100.00").build())
          .build());
  VariantCall failingCall = VariantCall.newBuilder().putAllInfo(failingFilter).build();

  DoFnTester<Variant, Variant> filterCallsFn = DoFnTester.of(new FilterCallsFn(true));
  List<Variant> filteredVariants = filterCallsFn.processBundle(
      Variant.newBuilder().setReferenceBases("T").addAlternateBases("A").addAllCalls(Arrays.asList(failingCall)).build(),
      Variant.newBuilder().setReferenceBases("G").addAlternateBases("C").addAllCalls(Arrays.asList(passingCall)).build());
  assertEquals(1, filteredVariants.size());

  // Non-variant segments are not filtered.
  filteredVariants = filterCallsFn.processBundle(
      Variant.newBuilder().setReferenceBases("T").addAllCalls(Arrays.asList(failingCall)).build(),
      Variant.newBuilder().setReferenceBases("G").addAlternateBases(VariantUtils.GATK_NON_VARIANT_SEGMENT_ALT)
        .addAllCalls(Arrays.asList(failingCall)).build());
  assertEquals(2, filteredVariants.size());
}
项目:codelabs    文件:TransformNonVariantSegmentDataTest.java   
@Test
public void testFormatCalls() throws Exception {

  Map depthInfo = new HashMap<String, List<String>>();
  depthInfo.put(
      TransformNonVariantSegmentData.DEPTH_FIELD,
      ListValue.newBuilder()
          .addValues(Value.newBuilder().setStringValue("30").build())
          .build());

  Map dotDepthInfo = new HashMap<String, List<String>>();
  dotDepthInfo.put(
      TransformNonVariantSegmentData.DEPTH_FIELD,
      ListValue.newBuilder()
          .addValues(Value.newBuilder().setStringValue(".").build())
          .build());

  VariantCall callWithValidDepth = VariantCall.newBuilder()
      .setCallSetName("hetAlt").addAllGenotype(Arrays.asList(0, 1))
      .putAllInfo(depthInfo)
      .build();

  VariantCall callWithDotDepth = VariantCall.newBuilder()
      .setCallSetName("homAlt").addAllGenotype(Arrays.asList(1, 1))
      .putAllInfo(dotDepthInfo)
      .build();

  Variant variant = Variant.newBuilder()
      .putAllInfo(FlagVariantsWithAmbiguousCallsFn.NO_AMBIGUOUS_CALLS_INFO)
      .addAllCalls(Arrays.asList(callWithValidDepth, callWithDotDepth))
      .build();

  DoFnTester<Variant, TableRow> formatVariantsFn = DoFnTester.of(new TransformNonVariantSegmentData.FormatVariantsFn(true, false, cohortMap));
  List<TableRow> rows = formatVariantsFn.processBundle(variant);
  assertEquals(1, rows.size());

  assertEquals("[{call_set_name=hetAlt, phaseset=, genotype=[0, 1], genotype_likelihood=[], FILTER=[], DP=30},"
      + " {call_set_name=homAlt, phaseset=, genotype=[1, 1], genotype_likelihood=[], FILTER=[], DP=null}]",
      rows.get(0).get("call").toString());
}
项目:curiostack    文件:WellKnownTypeMarshaller.java   
ValueMarshaller() {
  super(Value.getDefaultInstance());
}
项目:curiostack    文件:MessageMarshallerTest.java   
@Test
public void anyFields() throws Exception {
  TestAllTypes content = TestAllTypes.newBuilder().setOptionalInt32(1234).build();
  TestAny message = TestAny.newBuilder().setAnyValue(Any.pack(content)).build();
  assertMatchesUpstream(message, TestAllTypes.getDefaultInstance());

  TestAny messageWithDefaultAnyValue =
      TestAny.newBuilder().setAnyValue(Any.getDefaultInstance()).build();
  assertMatchesUpstream(messageWithDefaultAnyValue);

  // Well-known types have a special formatting when embedded in Any.
  //
  // 1. Any in Any.
  Any anyMessage = Any.pack(Any.pack(content));
  assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());

  // 2. Wrappers in Any.
  anyMessage = Any.pack(Int32Value.newBuilder().setValue(12345).build());
  assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());
  anyMessage = Any.pack(UInt32Value.newBuilder().setValue(12345).build());
  assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());
  anyMessage = Any.pack(Int64Value.newBuilder().setValue(12345).build());
  assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());
  anyMessage = Any.pack(UInt64Value.newBuilder().setValue(12345).build());
  assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());
  anyMessage = Any.pack(FloatValue.newBuilder().setValue(12345).build());
  assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());
  anyMessage = Any.pack(DoubleValue.newBuilder().setValue(12345).build());
  assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());
  anyMessage = Any.pack(BoolValue.newBuilder().setValue(true).build());
  assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());
  anyMessage = Any.pack(StringValue.newBuilder().setValue("Hello").build());
  assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());
  anyMessage =
      Any.pack(BytesValue.newBuilder().setValue(ByteString.copyFrom(new byte[] {1, 2})).build());
  assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());

  // 3. Timestamp in Any.
  anyMessage = Any.pack(Timestamps.parse("1969-12-31T23:59:59Z"));
  assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());

  // 4. Duration in Any
  anyMessage = Any.pack(Durations.parse("12345.10s"));
  assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());

  // 5. FieldMask in Any
  anyMessage = Any.pack(FieldMaskUtil.fromString("foo.bar,baz"));
  assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());

  // 6. Struct in Any
  Struct.Builder structBuilder = Struct.newBuilder();
  structBuilder.putFields("number", Value.newBuilder().setNumberValue(1.125).build());
  anyMessage = Any.pack(structBuilder.build());
  assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());

  // 7. Value (number type) in Any
  Value.Builder valueBuilder = Value.newBuilder();
  valueBuilder.setNumberValue(1);
  anyMessage = Any.pack(valueBuilder.build());
  assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());

  // 8. Value (null type) in Any
  anyMessage = Any.pack(Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build());
  assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());
}
项目:seldon-core    文件:JsonFormat.java   
private Object parseFieldValue(FieldDescriptor field, JsonElement json, Message.Builder builder)
    throws InvalidProtocolBufferException {
  if (json instanceof JsonNull) {
    if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE
        && field.getMessageType().getFullName().equals(Value.getDescriptor().getFullName())) {
      // For every other type, "null" means absence, but for the special
      // Value message, it means the "null_value" field has been set.
      Value value = Value.newBuilder().setNullValueValue(0).build();
      return builder.newBuilderForField(field).mergeFrom(value.toByteString()).build();
    } else if (field.getJavaType() == FieldDescriptor.JavaType.ENUM
        && field.getEnumType().getFullName().equals(NullValue.getDescriptor().getFullName())) {
      // If the type of the field is a NullValue, then the value should be explicitly set.
      return field.getEnumType().findValueByNumber(0);
    }
    return null;
  }
  switch (field.getType()) {
    case INT32:
    case SINT32:
    case SFIXED32:
      return parseInt32(json);

    case INT64:
    case SINT64:
    case SFIXED64:
      return parseInt64(json);

    case BOOL:
      return parseBool(json);

    case FLOAT:
      return parseFloat(json);

    case DOUBLE:
      return parseDouble(json);

    case UINT32:
    case FIXED32:
      return parseUint32(json);

    case UINT64:
    case FIXED64:
      return parseUint64(json);

    case STRING:
      return parseString(json);

    case BYTES:
      return parseBytes(json);

    case ENUM:
      return parseEnum(field.getEnumType(), json);

    case MESSAGE:
    case GROUP:
      if (currentDepth >= recursionLimit) {
        throw new InvalidProtocolBufferException("Hit recursion limit.");
      }
      ++currentDepth;
      Message.Builder subBuilder = builder.newBuilderForField(field);
      merge(json, subBuilder);
      --currentDepth;
      return subBuilder.build();

    default:
      throw new InvalidProtocolBufferException("Invalid field type: " + field.getType());
  }
}
项目:seldon-core    文件:JsonFormat.java   
private Object parseFieldValue(FieldDescriptor field, JsonElement json, Message.Builder builder)
    throws InvalidProtocolBufferException {
  if (json instanceof JsonNull) {
    if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE
        && field.getMessageType().getFullName().equals(Value.getDescriptor().getFullName())) {
      // For every other type, "null" means absence, but for the special
      // Value message, it means the "null_value" field has been set.
      Value value = Value.newBuilder().setNullValueValue(0).build();
      return builder.newBuilderForField(field).mergeFrom(value.toByteString()).build();
    } else if (field.getJavaType() == FieldDescriptor.JavaType.ENUM
        && field.getEnumType().getFullName().equals(NullValue.getDescriptor().getFullName())) {
      // If the type of the field is a NullValue, then the value should be explicitly set.
      return field.getEnumType().findValueByNumber(0);
    }
    return null;
  }
  switch (field.getType()) {
    case INT32:
    case SINT32:
    case SFIXED32:
      return parseInt32(json);

    case INT64:
    case SINT64:
    case SFIXED64:
      return parseInt64(json);

    case BOOL:
      return parseBool(json);

    case FLOAT:
      return parseFloat(json);

    case DOUBLE:
      return parseDouble(json);

    case UINT32:
    case FIXED32:
      return parseUint32(json);

    case UINT64:
    case FIXED64:
      return parseUint64(json);

    case STRING:
      return parseString(json);

    case BYTES:
      return parseBytes(json);

    case ENUM:
      return parseEnum(field.getEnumType(), json);

    case MESSAGE:
    case GROUP:
      if (currentDepth >= recursionLimit) {
        throw new InvalidProtocolBufferException("Hit recursion limit.");
      }
      ++currentDepth;
      Message.Builder subBuilder = builder.newBuilderForField(field);
      merge(json, subBuilder);
      --currentDepth;
      return subBuilder.build();

    default:
      throw new InvalidProtocolBufferException("Invalid field type: " + field.getType());
  }
}
项目:seldon-core    文件:JsonFormat.java   
private Object parseFieldValue(FieldDescriptor field, JsonElement json, Message.Builder builder)
    throws InvalidProtocolBufferException {
  if (json instanceof JsonNull) {
    if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE
        && field.getMessageType().getFullName().equals(Value.getDescriptor().getFullName())) {
      // For every other type, "null" means absence, but for the special
      // Value message, it means the "null_value" field has been set.
      Value value = Value.newBuilder().setNullValueValue(0).build();
      return builder.newBuilderForField(field).mergeFrom(value.toByteString()).build();
    } else if (field.getJavaType() == FieldDescriptor.JavaType.ENUM
        && field.getEnumType().getFullName().equals(NullValue.getDescriptor().getFullName())) {
      // If the type of the field is a NullValue, then the value should be explicitly set.
      return field.getEnumType().findValueByNumber(0);
    }
    return null;
  }
  switch (field.getType()) {
    case INT32:
    case SINT32:
    case SFIXED32:
      return parseInt32(json);

    case INT64:
    case SINT64:
    case SFIXED64:
      return parseInt64(json);

    case BOOL:
      return parseBool(json);

    case FLOAT:
      return parseFloat(json);

    case DOUBLE:
      return parseDouble(json);

    case UINT32:
    case FIXED32:
      return parseUint32(json);

    case UINT64:
    case FIXED64:
      return parseUint64(json);

    case STRING:
      return parseString(json);

    case BYTES:
      return parseBytes(json);

    case ENUM:
      return parseEnum(field.getEnumType(), json);

    case MESSAGE:
    case GROUP:
      if (currentDepth >= recursionLimit) {
        throw new InvalidProtocolBufferException("Hit recursion limit.");
      }
      ++currentDepth;
      Message.Builder subBuilder = builder.newBuilderForField(field);
      merge(json, subBuilder);
      --currentDepth;
      return subBuilder.build();

    default:
      throw new InvalidProtocolBufferException("Invalid field type: " + field.getType());
  }
}
项目:endpoints-management-java    文件:ReportRequestInfo.java   
/**
 * Make a {@code LogEntry} from the instance.
 *
 * @param name the name of log
 * @param timestampMillis the timestamp of the log in milliseconds
 * @return the corresponding {@code LogEntry.Builder}
 */
public LogEntry.Builder asLogEntry(String name, long timestampMillis) {
  Value.Builder vb = Value.newBuilder();
  Map<String, Value> values = Maps.newHashMap();
  values.put("http_response_code", vb.setNumberValue(getResponseCode()).build());
  values.put("timestamp", vb.setNumberValue(timestampMillis).build());
  if (getRequestSize() > 0) {
    values.put("request_size", vb.setNumberValue(getRequestSize()).build());
  }
  if (getResponseSize() > 0) {
    values.put("response_size", vb.setNumberValue(getResponseSize()).build());
  }
  if (!Strings.isNullOrEmpty(getMethod())) {
    values.put("http_method", vb.setStringValue(getMethod()).build());
  }
  if (!Strings.isNullOrEmpty(getApiName())) {
    values.put("api_name", vb.setStringValue(getApiName()).build());
  }
  if (!Strings.isNullOrEmpty(getApiMethod())) {
    values.put("api_method", vb.setStringValue(getApiMethod()).build());
  }
  if (!Strings.isNullOrEmpty(getApiKey())) {
    values.put("api_key", vb.setStringValue(getApiKey()).build());
  }
  if (!Strings.isNullOrEmpty(getProducerProjectId())) {
    values.put("producer_project_id", vb.setStringValue(getProducerProjectId()).build());
  }
  if (!Strings.isNullOrEmpty(getReferer())) {
    values.put("referer", vb.setStringValue(getReferer()).build());
  }
  if (!Strings.isNullOrEmpty(getLocation())) {
    values.put("location", vb.setStringValue(getLocation()).build());
  }
  if (!Strings.isNullOrEmpty(getLogMessage())) {
    values.put("log_message", vb.setStringValue(getLogMessage()).build());
  }
  if (!Strings.isNullOrEmpty(getUrl())) {
    values.put("url", vb.setStringValue(getUrl()).build());
  }
  LogSeverity severity = LogSeverity.INFO;
  if (getResponseCode() >= 400) {
    values.put("error_cause", vb.setStringValue(getErrorCause().name()).build());
    severity = LogSeverity.ERROR;
  }
  Struct.Builder theStruct = Struct.newBuilder().putAllFields(values);
  return LogEntry
      .newBuilder()
      .setTimestamp(Timestamps.fromEpoch(timestampMillis))
      .setStructPayload(theStruct)
      .setName(name)
      .setSeverity(severity);
}
项目:genomewarp    文件:VcfToVariant.java   
@VisibleForTesting
static List<VariantCall> getCalls(VariantContext vc, VCFHeader header) {
  List<VariantCall> toReturn = new ArrayList<>();

  for (String currSample : header.getGenotypeSamples()) {
    if (!vc.hasGenotype(currSample)) {
      continue;
    }

    Genotype currGenotype = vc.getGenotype(currSample);
    VariantCall.Builder vcBuilder = VariantCall.newBuilder();
    vcBuilder.setCallSetName(currSample);

    // Get GT info.
    final Map<Allele, Integer> alleleStrings = buildAlleleMap(vc);
    vcBuilder.addGenotype(alleleStrings.get(currGenotype.getAllele(0)));
    for (int i = 1; i < currGenotype.getPloidy(); i++) {
      vcBuilder.addGenotype(alleleStrings.get(currGenotype.getAllele(i)));
    }

    // Set phasing (not applicable to haploid).
    if (currGenotype.isPhased() && currGenotype.getPloidy() > 1) {
      vcBuilder.setPhaseset("*");
    }

    // Get rest of the genotype info.
    Map<String, ListValue> genotypeInfo = new HashMap<>();

    // Set filters
    if (currGenotype.isFiltered()) {
      genotypeInfo.put(VCFConstants.GENOTYPE_FILTER_KEY, ListValue.newBuilder()
          .addValues(Value.newBuilder().setStringValue(currGenotype.getFilters()).build())
          .build());
    }

    for (final String field : vc.calcVCFGenotypeKeys(header)) {
      // We've already handled genotype
      if (field.equals(VCFConstants.GENOTYPE_KEY)) {
        continue;
      }

      ListValue.Builder listValueBuilder = ListValue.newBuilder();
      if (field.equals(VCFConstants.GENOTYPE_FILTER_KEY)) {
        // This field has already been dealt with
        continue;
      } else {
        final IntGenotypeFieldAccessors.Accessor accessor =
            GENOTYPE_FIELD_ACCESSORS.getAccessor(field);

        if (accessor != null) {
          // The field is a default inline field.
          if (!parseInlineGenotypeFields(field, vcBuilder, listValueBuilder, accessor,
              currGenotype)) {
            continue;
          }
        } else {
          // Other field, we'll get type/other info from header.
          if (!parseOtherGenotypeFields(field, vc, listValueBuilder, currGenotype,
              header)) {
            continue;
          }
        }
      }

      genotypeInfo.put(field, listValueBuilder.build());
    }

    vcBuilder.putAllInfo(genotypeInfo);
    toReturn.add(vcBuilder.build());
  }

  return toReturn;
}
项目:core-java    文件:InvalidCommandException.java   
@Override
protected Map<String, Value> getMessageTypeAttribute(Message commandMessage) {
    return commandTypeAttribute(commandMessage);
}
项目:core-java    文件:InvalidEventException.java   
@Override
protected Map<String, Value> getMessageTypeAttribute(Message message) {
    return eventTypeAttribute(message);
}
项目:bazel    文件:JsonFormat.java   
private Object parseFieldValue(FieldDescriptor field, JsonElement json, Message.Builder builder)
    throws InvalidProtocolBufferException {
  if (json instanceof JsonNull) {
    if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE
        && field.getMessageType().getFullName().equals(Value.getDescriptor().getFullName())) {
      // For every other type, "null" means absence, but for the special
      // Value message, it means the "null_value" field has been set.
      Value value = Value.newBuilder().setNullValueValue(0).build();
      return builder.newBuilderForField(field).mergeFrom(value.toByteString()).build();
    } else if (field.getJavaType() == FieldDescriptor.JavaType.ENUM
        && field.getEnumType().getFullName().equals(NullValue.getDescriptor().getFullName())) {
      // If the type of the field is a NullValue, then the value should be explicitly set.
      return field.getEnumType().findValueByNumber(0);
    }
    return null;
  }
  switch (field.getType()) {
    case INT32:
    case SINT32:
    case SFIXED32:
      return parseInt32(json);

    case INT64:
    case SINT64:
    case SFIXED64:
      return parseInt64(json);

    case BOOL:
      return parseBool(json);

    case FLOAT:
      return parseFloat(json);

    case DOUBLE:
      return parseDouble(json);

    case UINT32:
    case FIXED32:
      return parseUint32(json);

    case UINT64:
    case FIXED64:
      return parseUint64(json);

    case STRING:
      return parseString(json);

    case BYTES:
      return parseBytes(json);

    case ENUM:
      return parseEnum(field.getEnumType(), json);

    case MESSAGE:
    case GROUP:
      if (currentDepth >= recursionLimit) {
        throw new InvalidProtocolBufferException("Hit recursion limit.");
      }
      ++currentDepth;
      Message.Builder subBuilder = builder.newBuilderForField(field);
      merge(json, subBuilder);
      --currentDepth;
      return subBuilder.build();

    default:
      throw new InvalidProtocolBufferException("Invalid field type: " + field.getType());
  }
}
项目:bazel    文件:JsonFormatTest.java   
public void testStruct() throws Exception {
  // Build a struct with all possible values.
  TestStruct.Builder builder = TestStruct.newBuilder();
  Struct.Builder structBuilder = builder.getStructValueBuilder();
  structBuilder.putFields("null_value", Value.newBuilder().setNullValueValue(0).build());
  structBuilder.putFields("number_value", Value.newBuilder().setNumberValue(1.25).build());
  structBuilder.putFields("string_value", Value.newBuilder().setStringValue("hello").build());
  Struct.Builder subStructBuilder = Struct.newBuilder();
  subStructBuilder.putFields("number_value", Value.newBuilder().setNumberValue(1234).build());
  structBuilder.putFields(
      "struct_value", Value.newBuilder().setStructValue(subStructBuilder.build()).build());
  ListValue.Builder listBuilder = ListValue.newBuilder();
  listBuilder.addValues(Value.newBuilder().setNumberValue(1.125).build());
  listBuilder.addValues(Value.newBuilder().setNullValueValue(0).build());
  structBuilder.putFields(
      "list_value", Value.newBuilder().setListValue(listBuilder.build()).build());
  TestStruct message = builder.build();

  assertEquals(
      "{\n"
          + "  \"structValue\": {\n"
          + "    \"null_value\": null,\n"
          + "    \"number_value\": 1.25,\n"
          + "    \"string_value\": \"hello\",\n"
          + "    \"struct_value\": {\n"
          + "      \"number_value\": 1234.0\n"
          + "    },\n"
          + "    \"list_value\": [1.125, null]\n"
          + "  }\n"
          + "}",
      toJsonString(message));
  assertRoundTripEquals(message);

  builder = TestStruct.newBuilder();
  builder.setValue(Value.newBuilder().setNullValueValue(0).build());
  message = builder.build();
  assertEquals("{\n" + "  \"value\": null\n" + "}", toJsonString(message));
  assertRoundTripEquals(message);

  builder = TestStruct.newBuilder();
  listBuilder = builder.getListValueBuilder();
  listBuilder.addValues(Value.newBuilder().setNumberValue(31831.125).build());
  listBuilder.addValues(Value.newBuilder().setNullValueValue(0).build());
  message = builder.build();
  assertEquals("{\n" + "  \"listValue\": [31831.125, null]\n" + "}", toJsonString(message));
  assertRoundTripEquals(message);
}
项目:bazel    文件:JsonFormatTest.java   
public void testAnyInMaps() throws Exception {
  JsonFormat.TypeRegistry registry =
      JsonFormat.TypeRegistry.newBuilder().add(TestAllTypes.getDescriptor()).build();
  JsonFormat.Printer printer = JsonFormat.printer().usingTypeRegistry(registry);

  TestAny.Builder testAny = TestAny.newBuilder();
  testAny.putAnyMap("int32_wrapper", Any.pack(Int32Value.newBuilder().setValue(123).build()));
  testAny.putAnyMap("int64_wrapper", Any.pack(Int64Value.newBuilder().setValue(456).build()));
  testAny.putAnyMap("timestamp", Any.pack(Timestamps.parse("1969-12-31T23:59:59Z")));
  testAny.putAnyMap("duration", Any.pack(Durations.parse("12345.1s")));
  testAny.putAnyMap("field_mask", Any.pack(FieldMaskUtil.fromString("foo.bar,baz")));
  Value numberValue = Value.newBuilder().setNumberValue(1.125).build();
  Struct.Builder struct = Struct.newBuilder();
  struct.putFields("number", numberValue);
  testAny.putAnyMap("struct", Any.pack(struct.build()));
  Value nullValue = Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build();
  testAny.putAnyMap(
      "list_value",
      Any.pack(ListValue.newBuilder().addValues(numberValue).addValues(nullValue).build()));
  testAny.putAnyMap("number_value", Any.pack(numberValue));
  testAny.putAnyMap("any_value_number", Any.pack(Any.pack(numberValue)));
  testAny.putAnyMap("any_value_default", Any.pack(Any.getDefaultInstance()));
  testAny.putAnyMap("default", Any.getDefaultInstance());

  assertEquals(
      "{\n"
          + "  \"anyMap\": {\n"
          + "    \"int32_wrapper\": {\n"
          + "      \"@type\": \"type.googleapis.com/google.protobuf.Int32Value\",\n"
          + "      \"value\": 123\n"
          + "    },\n"
          + "    \"int64_wrapper\": {\n"
          + "      \"@type\": \"type.googleapis.com/google.protobuf.Int64Value\",\n"
          + "      \"value\": \"456\"\n"
          + "    },\n"
          + "    \"timestamp\": {\n"
          + "      \"@type\": \"type.googleapis.com/google.protobuf.Timestamp\",\n"
          + "      \"value\": \"1969-12-31T23:59:59Z\"\n"
          + "    },\n"
          + "    \"duration\": {\n"
          + "      \"@type\": \"type.googleapis.com/google.protobuf.Duration\",\n"
          + "      \"value\": \"12345.100s\"\n"
          + "    },\n"
          + "    \"field_mask\": {\n"
          + "      \"@type\": \"type.googleapis.com/google.protobuf.FieldMask\",\n"
          + "      \"value\": \"foo.bar,baz\"\n"
          + "    },\n"
          + "    \"struct\": {\n"
          + "      \"@type\": \"type.googleapis.com/google.protobuf.Struct\",\n"
          + "      \"value\": {\n"
          + "        \"number\": 1.125\n"
          + "      }\n"
          + "    },\n"
          + "    \"list_value\": {\n"
          + "      \"@type\": \"type.googleapis.com/google.protobuf.ListValue\",\n"
          + "      \"value\": [1.125, null]\n"
          + "    },\n"
          + "    \"number_value\": {\n"
          + "      \"@type\": \"type.googleapis.com/google.protobuf.Value\",\n"
          + "      \"value\": 1.125\n"
          + "    },\n"
          + "    \"any_value_number\": {\n"
          + "      \"@type\": \"type.googleapis.com/google.protobuf.Any\",\n"
          + "      \"value\": {\n"
          + "        \"@type\": \"type.googleapis.com/google.protobuf.Value\",\n"
          + "        \"value\": 1.125\n"
          + "      }\n"
          + "    },\n"
          + "    \"any_value_default\": {\n"
          + "      \"@type\": \"type.googleapis.com/google.protobuf.Any\",\n"
          + "      \"value\": {}\n"
          + "    },\n"
          + "    \"default\": {}\n"
          + "  }\n"
          + "}",
      printer.print(testAny.build()));
  assertRoundTripEquals(testAny.build(), registry);
}
项目:codelabs    文件:TransformNonVariantSegmentData.java   
@ProcessElement
public void processElement(ProcessContext context) {
  Variant variant = context.element();

  // We may have variants without calls if any callsets were deleted from the variant set and/or
  // a filtering step removed all calls. Omit these variants from our output.
  if (null == variant.getCallsList() || variant.getCallsList().isEmpty()) {
    return;
  }

  // Gather calls together for the same callSetName.
  ListMultimap<String, VariantCall> indexedCalls =
      Multimaps.index(variant.getCallsList(), new Function<VariantCall, String>() {
        @Override
        public String apply(final VariantCall c) {
          return c.getCallSetName();
        }
      });

  // Identify and count variants with multiple calls per callSetName.
  boolean isAmbiguous = false;
  for (Entry<String, Collection<VariantCall>> entry : indexedCalls.asMap().entrySet()) {
    if (1 < entry.getValue().size()) {
      LOG.warning("Variant " + variant.getId() + " contains ambiguous calls for at least one genome: "
          + entry.getValue().iterator().next().getCallSetName());
      isAmbiguous = true;
      Metrics.counter(TransformNonVariantSegmentData.class, "Number of variants containing ambiguous calls").inc();
      break;  // No need to look for additional ambiguity; one instance is enough to warrant the flag.
    }
  }

  // Also check the overlapping calls.
  if (null != variant.getInfo().get(MergeAllVariantsAtSameSite.OVERLAPPING_CALLSETS_FIELD)) {
    Set<String> callSetNames = new HashSet<String>();
    for (Value value : variant.getInfo().get(MergeAllVariantsAtSameSite.OVERLAPPING_CALLSETS_FIELD).getValuesList()) {
      callSetNames.add(value.getStringValue());
    }
    callSetNames.retainAll(indexedCalls.keySet());
    if (0 != callSetNames.size()) {
      LOG.warning("Variant " + variant.getId() + " contains ambiguous calls for a variant that overlaps this variant: " + callSetNames);
      isAmbiguous = true;
      Metrics.counter(TransformNonVariantSegmentData.class, "Number of variants containing ambiguous calls").inc();
    }
  }

  // Add the flag to the variant.
  context.output(Variant.newBuilder(variant)
      .putAllInfo(isAmbiguous ? HAS_AMBIGUOUS_CALLS_INFO : NO_AMBIGUOUS_CALLS_INFO)
      .build());
}
项目:codelabs    文件:TransformNonVariantSegmentDataTest.java   
@Test
public void testFilterVariantCallsFn() throws Exception {
  DoFnTester<Variant, Variant> filterCallsFn = DoFnTester.of(new FilterCallsFn(true));

  Map<String, ListValue> passingFilter = new HashMap<String, ListValue>();
  passingFilter.put(
      TransformNonVariantSegmentData.FILTER_FIELD,
      ListValue.newBuilder()
          .addValues(Value.newBuilder().setStringValue(TransformNonVariantSegmentData.PASSING_FILTER).build())
          .build());
  VariantCall call1 = VariantCall.newBuilder().putAllInfo(passingFilter).build();

  Map<String, ListValue> failingFilter = new HashMap<String, ListValue>();
  failingFilter.put(
      TransformNonVariantSegmentData.FILTER_FIELD,
      ListValue.newBuilder()
          .addValues(Value.newBuilder().setStringValue("VQSRTrancheSNP99.90to100.00").build())
          .build());
  VariantCall call2 = VariantCall.newBuilder().putAllInfo(failingFilter).build();

  Map<String, ListValue> ambiguousFilter = new HashMap<String, ListValue>();
  ambiguousFilter.put(
      TransformNonVariantSegmentData.FILTER_FIELD,
      ListValue.newBuilder()
          .addValues(Value.newBuilder().setStringValue("VQSRTrancheSNP99.90to100.00").build())
          .addValues(Value.newBuilder().setStringValue(TransformNonVariantSegmentData.PASSING_FILTER).build())
          .build());
  VariantCall call3 = VariantCall.newBuilder().putAllInfo(ambiguousFilter).build();

  // Test a variant.
  Variant inputVariant = Variant.newBuilder()
      .setReferenceBases("A")
      .addAlternateBases("T")
      .addAllCalls(Arrays.asList(call1, call2, call3))
      .build();

  Variant expectedVariant = Variant.newBuilder()
      .setReferenceBases("A")
      .addAlternateBases("T")
      .addAllCalls(Arrays.asList(call1, call3))
      .build();

  Iterator<Variant> filtered1 = filterCallsFn.processBundle(inputVariant).iterator();
  assertEquals(filtered1.next(), expectedVariant);
  assertFalse(filtered1.hasNext());

  // Also test a non-variant segment.  These are not filtered.
  Variant inputBlockRecord = Variant.newBuilder()
      .setReferenceBases("A")
      .addAllCalls(Arrays.asList(call1, call2, call3))
      .build();

  Variant expectedBlockRecord = Variant.newBuilder()
      .setReferenceBases("A")
      .addAllCalls(Arrays.asList(call1, call2, call3))
      .build();

  Iterator<Variant> filtered2 = filterCallsFn.processBundle(inputBlockRecord).iterator();
  assertEquals(filtered2.next(), expectedBlockRecord);
  assertFalse(filtered2.hasNext());
}
项目:codelabs    文件:TransformNonVariantSegmentDataTest.java   
@Test
public void testAmbiguousOverlappingVariantCallsFn() throws Exception {

  DoFnTester<Variant, Variant> flagVariantsFn =
      DoFnTester.of(new TransformNonVariantSegmentData.FlagVariantsWithAmbiguousCallsFn());
  DoFnTester<Variant, TableRow> formatVariantsFn =
      DoFnTester.of(new TransformNonVariantSegmentData.FormatVariantsFn(true, false, cohortMap));

  VariantCall call1 =
      VariantCall.newBuilder().setCallSetName("sample1").addAllGenotype(Arrays.asList(0, 1))
          .build();
  VariantCall call2 =
      VariantCall.newBuilder().setCallSetName("sample2").addAllGenotype(Arrays.asList(0, 1))
          .build();

  Map<String, ListValue> info = new HashMap<String, ListValue>();
  info.put(MergeAllVariantsAtSameSite.OVERLAPPING_CALLSETS_FIELD,
      ListValue.newBuilder().addValues(Value.newBuilder().setStringValue("sampleN")).build());
  Variant inputVariant = Variant.newBuilder()
      .addAllCalls(Arrays.asList(call1, call2))
      .putAllInfo(info)
      .build();

  Map<String, ListValue> ambiguousInfo = new HashMap<String, ListValue>();
  ambiguousInfo.put(MergeAllVariantsAtSameSite.OVERLAPPING_CALLSETS_FIELD,
      ListValue.newBuilder().addValues(Value.newBuilder().setStringValue("sample2")).build());
  Variant ambiguousInputVariant = Variant.newBuilder()
      .addAllCalls(Arrays.asList(call1, call2))
      .putAllInfo(ambiguousInfo)
      .build();

  Map<String, ListValue> expectedInfo = new HashMap<String, ListValue>();
  expectedInfo.put(MergeAllVariantsAtSameSite.OVERLAPPING_CALLSETS_FIELD,
      ListValue.newBuilder().addValues(Value.newBuilder().setStringValue("sampleN")).build());
  expectedInfo.put(
      TransformNonVariantSegmentData.HAS_AMBIGUOUS_CALLS_FIELD,
      ListValue.newBuilder()
          .addValues(Value.newBuilder().setStringValue(Boolean.toString(Boolean.FALSE)).build())
          .build());
  Variant expectedVariant = Variant.newBuilder()
      .addAllCalls(Arrays.asList(call1, call2))
      .putAllInfo(expectedInfo)
      .build();

  Map<String, ListValue> expectedAmbiguousInfo = new HashMap<String, ListValue>();
  expectedAmbiguousInfo.put(MergeAllVariantsAtSameSite.OVERLAPPING_CALLSETS_FIELD,
      ListValue.newBuilder().addValues(Value.newBuilder().setStringValue("sample2")).build());
  expectedAmbiguousInfo.put(
      TransformNonVariantSegmentData.HAS_AMBIGUOUS_CALLS_FIELD,
      ListValue.newBuilder()
          .addValues(Value.newBuilder().setStringValue(Boolean.toString(Boolean.TRUE)).build())
          .build());
  Variant ambiguousExpectedVariant = Variant.newBuilder()
      .addAllCalls(Arrays.asList(call1, call2))
      .putAllInfo(expectedAmbiguousInfo)
      .build();

  List<Variant> flaggedVariants = flagVariantsFn.processBundle(inputVariant, ambiguousInputVariant);
  assertEquals(expectedVariant, flaggedVariants.get(0));
  assertEquals(ambiguousExpectedVariant, flaggedVariants.get(1));

  List<TableRow> rows = formatVariantsFn.processBundle(flaggedVariants);
  assertEquals(2, rows.size());
  assertEquals("false", rows.get(0).get(TransformNonVariantSegmentData.HAS_AMBIGUOUS_CALLS_FIELD).toString());
  assertEquals("true", rows.get(1).get(TransformNonVariantSegmentData.HAS_AMBIGUOUS_CALLS_FIELD).toString());

  assertEquals("[sampleN]", rows.get(0).get(TransformNonVariantSegmentData.OVERLAPPING_CALLSETS_FIELD).toString());
  assertEquals("[sample2]", rows.get(1).get(TransformNonVariantSegmentData.OVERLAPPING_CALLSETS_FIELD).toString());
}
项目:utils-java    文件:MergeAllVariantsAtSameSite.java   
@Override
public Value apply(VariantCall call) {
  return Value.newBuilder().setStringValue(call.getCallSetName()).build();
}
项目:utils-java    文件:ReadUtilsTest.java   
private void testGetReferenceSequenceHelper(final String seq, final String cigar, final String md,
    final String expectedReference) throws IOException {
  LinearAlignment.Builder alignment = LinearAlignment.newBuilder();
  Cigar cigars = TextCigarCodec.decode(cigar);
  for (int i = 0; i < cigars.numCigarElements(); i++) {
    CigarElement c = cigars.getCigarElement(i);
    CigarUnit.Builder unit = CigarUnit.newBuilder().setOperationLength(c.getLength());
    switch (c.getOperator()) {
      case M:
        unit.setOperation(CigarUnit.Operation.ALIGNMENT_MATCH);
        break;
      case I:
        unit.setOperation(CigarUnit.Operation.INSERT);
        break;
      case D:
        unit.setOperation(CigarUnit.Operation.DELETE);
        break;
      case N:
        unit.setOperation(CigarUnit.Operation.SKIP);
        break;
      case S:
        unit.setOperation(CigarUnit.Operation.CLIP_SOFT);
        break;
      case H:
        unit.setOperation(CigarUnit.Operation.CLIP_HARD);
        break;
      case P:
        unit.setOperation(CigarUnit.Operation.PAD);
        break;
      case EQ:
        unit.setOperation(CigarUnit.Operation.SEQUENCE_MATCH);
        break;
      case X:
        unit.setOperation(CigarUnit.Operation.SEQUENCE_MISMATCH);
        break;
    }
    alignment.addCigar(unit.build());
  }
  final Read.Builder rec = Read.newBuilder()
      .setFragmentName("test")
      .setAlignedSequence(seq)
      .setAlignment(alignment.build());
  rec.getMutableInfo().put("MD",
      ListValue.newBuilder().addValues(0, Value.newBuilder().setStringValue(md).build()).build());
  final String refBases = ReadUtils.inferReferenceSequenceByParsingMdFlag(rec.build());
  assertEquals(refBases, expectedReference);
}