Esempio n. 1
0
def set_format(variant_call, field_name, value, vcf_object=None):
    """Sets a field of the info map of the `VariantCall` to the given value(s).

  `variant_call.info` is analogous to the FORMAT field of a VCF call.

  Example usage:
  with vcf.VcfReader('/path/to/my.vcf') as vcf_reader:
    for variant in vcf_reader:
      first_call = variant.calls[0]
      # Type can be inferred for reserved VCF fields.
      set_format(first_call, 'AD', 25)
      # Specify the reader explicitly for unknown fields.
      set_format(first_call, 'MYFIELD', 30, vcf_reader)

  Args:
    variant_call: VariantCall proto. The VariantCall to modify.
    field_name: str. The name of the field to set.
    value: A single value or list of values to update the VariantCall with.
      The type of the value is determined by the `vcf_object` if one is given,
      otherwise is looked up based on the reserved FORMAT fields in the VCF
      specification.
    vcf_object: (Optional) A VcfReader or VcfWriter object. If not None, the
      type of the field is inferred from the associated VcfReader or VcfWriter
      based on its name. Otherwise, the type is inferred if it is a reserved
      field.
  """
    if field_name == _GL:
        set_gl(variant_call, value)
        return
    if field_name == _GT:
        set_gt(variant_call, value)
        return

    if vcf_object is None:
        set_field_fn = vcf_constants.reserved_format_field_set_fn(field_name)
    else:
        set_field_fn = vcf_object.field_access_cache.format_field_set_fn(
            field_name)
    set_field_fn(variant_call.info, field_name, value)
Esempio n. 2
0
 def test_invalid_reserved_format_field_set_fn(self, field):
     with self.assertRaisesRegexp(ValueError,
                                  'Unknown reserved FORMAT field:'):
         vcf_constants.reserved_format_field_set_fn(field)
Esempio n. 3
0
 def test_reserved_format_field_set_fn(self, field, expected):
     actual = vcf_constants.reserved_format_field_set_fn(field)
     self.assertIs(actual, expected)