def test_label_variant(self): variant = test_utils.make_variant(start=10, alleles=['A', 'C']) tvariant = test_utils.make_variant(start=10, alleles=['A', 'C'], gt=[0, 1]) example = tf_utils.make_example(variant, ['C'], 'foo', self.default_shape, self.default_format) labeler = mock.Mock() labeler.match = mock.Mock(return_value=[True, tvariant]) labeler.match_to_alt_count = mock.Mock(return_value=1) self.processor.labeler = labeler labeled = example_pb2.Example() labeled.CopyFrom(example) self.processor.label_variant(labeled, variant) labeler.match.assert_called_once_with(variant) labeler.match_to_alt_count.assert_called_once_with( variant, tvariant, ['C']) for key, value in example.features.feature.iteritems(): self.assertEqual(value, labeled.features.feature[key]) self.assertEqual(1, tf_utils.example_label(labeled)) self.assertEqual(tvariant, tf_utils.example_truth_variant(labeled))
def create_pileup_examples(self, dv_call): """Creates a tf.Example for DeepVariantCall. This function calls PileupImageCreator.create_pileup_images on dv_call to get raw image tensors for each alt_allele option (see docs for details). These tensors are encoded as pngs, and all of the key information is encoded as a tf.Example via a call to tf_utils.make_example. Args: dv_call: A DeepVariantCall. Returns: A list of tf.Example protos. """ pileup_images = self.pic.create_pileup_images(dv_call) if pileup_images is None: # We cannot build a PileupImage for dv_call, issue a warning. logging.warning('Could not create PileupImage for candidate at %s:%s', dv_call.variant.reference_name, dv_call.variant.start) return [] examples = [] for alt_alleles, image_tensor in pileup_images: encoded_tensor, shape, tensor_format = self._encode_tensor(image_tensor) examples.append( tf_utils.make_example( dv_call.variant, alt_alleles, encoded_tensor, shape=shape, image_format=tensor_format)) return examples
def test_label_variant_raises_for_non_confident_variant(self): variant = test_utils.make_variant(start=10, alleles=['A', 'C'], gt=[0, 1]) self.processor.labeler = mock.Mock() self.processor.labeler.match = mock.Mock(return_value=[False, variant]) example = tf_utils.make_example(variant, ['C'], 'foo', self.default_shape, self.default_format) self.assertFalse(self.processor.label_variant(example, variant))
def testExampleSetLabel(self): example = tf_utils.make_example(self.variant, self.alts, self.encoded_image, self.default_shape, self.default_format) self.assertIsNotAFeature('label', example) for label in [0, 1, 2]: tf_utils.example_set_label(example, label) self.assertEqual(label, tf_utils.example_label(example))
def testMakeExampleMultiAllelic(self): alts = ['AA', 'CC', 'GG'] self.variant.alternate_bases[:] = alts # Providing GG, AA checks that we're sorting the indices. example = tf_utils.make_example(self.variant, ['GG', 'AA'], 'foo', self.default_shape, self.default_format) self.assertEqual([0, 2], tf_utils.example_alt_alleles_indices(example)) self.assertEqual(['AA', 'GG'], tf_utils.example_alt_alleles(example)) self.assertEqual('1:11:C->AA/GG', tf_utils.example_key(example))
def testExampleSetTruthVariant(self): example = tf_utils.make_example(self.variant, self.alts, self.encoded_image, self.default_shape, self.default_format) full_tvariant = variants_pb2.Variant( variant_set_id='variant_set_id', id='id', names=['name1'], created=1234, reference_name='1', start=10, end=11, reference_bases='C', alternate_bases=['A'], filter=['PASS'], quality=1234.5, calls=[ variants_pb2.VariantCall(call_set_id='call_set_id', call_set_name='call_set_name', genotype=[0, 1], phaseset='phaseset', genotype_likelihood=[0.1, 0.2, 0.3]) ]) test_utils.set_list_values(full_tvariant.info['key'], [1]) test_utils.set_list_values(full_tvariant.calls[0].info['key'], [2]) simple_tvariant = variants_pb2.Variant( reference_name='1', start=10, end=11, reference_bases='C', alternate_bases=['A'], filter=['PASS'], quality=1234.5, calls=[ variants_pb2.VariantCall(call_set_name='call_set_name', genotype=[0, 1]) ]) test_utils.set_list_values(simple_tvariant.calls[0].info['key'], [2]) self.assertIsNotAFeature('truth_variant/encoded', example) tf_utils.example_set_truth_variant(example, full_tvariant, simplify=False) self.assertEqual(full_tvariant, tf_utils.example_truth_variant(example)) # Check that reencoding with simplify=True produces the simplified version. tf_utils.example_set_truth_variant(example, full_tvariant, simplify=True) self.assertEqual(simple_tvariant, tf_utils.example_truth_variant(example))
def testMakeExample(self): example = tf_utils.make_example(self.variant, self.alts, self.encoded_image, self.default_shape, self.default_format) self.assertEqual(self.encoded_image, tf_utils.example_encoded_image(example)) self.assertEqual( 'raw', example.features.feature['image/format'].bytes_list.value[0]) self.assertEqual(self.variant, tf_utils.example_variant(example)) self.assertEqual('1:11-11', tf_utils.example_locus(example)) self.assertEqual([0], tf_utils.example_alt_alleles_indices(example)) self.assertEqual('1:11:C->A', tf_utils.example_key(example))
def testAltAllelesWithVariant(self): alts = list(self.variant.alternate_bases) example = tf_utils.make_example(self.variant, alts, six.b('foo'), self.default_shape, self.default_format) self.assertEqual([0], tf_utils.example_alt_alleles_indices(example)) with mock.patch( 'deepvariant.tf_utils.example_variant' ) as mock_ex_variant: # Providing variant directly avoids the call to example_variant(). self.assertEqual( alts, tf_utils.example_alt_alleles(example, variant=self.variant)) mock_ex_variant.assert_not_called() # Checks that we load the variant if needed and that our mock is working. mock_ex_variant.return_value = self.variant self.assertEqual(alts, tf_utils.example_alt_alleles(example)) mock_ex_variant.assert_called_once_with(example)
def testExampleImageShape(self): example = tf_utils.make_example(self.variant, self.alts, self.encoded_image, self.default_shape, self.default_format) self.assertEqual(self.default_shape, tf_utils.example_image_shape(example))
def _example_for_variant(self, variant): return tf_utils.make_example(variant, list(variant.alternate_bases), 'foo', self.default_shape, self.default_format)
def _example_for_variant(self, variant): return tf_utils.make_example(variant, list(variant.alternate_bases), six.b('foo'), self.default_shape, self.default_format)