def label_variant(self, example, variant): """Adds the truth variant and label for variant to example. This function uses VariantLabeler to find a match for variant and writes in the correspond truth variant and derived label to our example proto. Args: example: A tf.Example proto. We will write truth_variant and label into this proto. variant: A learning.genomics.v1.Variant proto. This is the variant we'll use to call our VariantLabeler.match to get our truth variant. Returns: True if the variant was in the confident region (meaning that it could be given a label) and False otherwise. """ is_confident, truth_variant = self.labeler.match(variant) if not is_confident: return False alt_alleles = tf_utils.example_alt_alleles(example, variant=variant) if variantutils.is_ref(variant): label = 0 else: label = self.labeler.match_to_alt_count(variant, truth_variant, alt_alleles) tf_utils.example_set_label(example, label) tf_utils.example_set_truth_variant(example, truth_variant) return True
def test_create_pileup_examples(self): self.processor.pic = mock.Mock() self.add_mock('_encode_tensor', side_effect=[ ('tensor1', self.default_shape, self.default_format), ('tensor2', self.default_shape, self.default_format) ]) dv_call = mock.Mock() dv_call.variant = test_utils.make_variant(start=10, alleles=['A', 'C', 'G']) ex = mock.Mock() alt1, alt2 = ['C'], ['G'] self.processor.pic.create_pileup_images.return_value = [ (alt1, 'tensor1'), (alt2, 'tensor2') ] actual = self.processor.create_pileup_examples(dv_call) self.processor.pic.create_pileup_images.assert_called_once_with( dv_call) self.assertEquals(len(actual), 2) for ex, (alt, img) in zip(actual, [(alt1, 'tensor1'), (alt2, 'tensor2')]): self.assertEqual(tf_utils.example_alt_alleles(ex), alt) self.assertEqual(tf_utils.example_variant(ex), dv_call.variant) self.assertEqual(tf_utils.example_encoded_image(ex), img) self.assertEqual(tf_utils.example_image_shape(ex), self.default_shape) self.assertEqual(tf_utils.example_image_format(ex), self.default_format)
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 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 test_create_pileup_examples(self): self.processor.pic = mock.Mock() self.add_mock( '_encode_tensor', side_effect=[('tensor1', self.default_shape, self.default_format), ('tensor2', self.default_shape, self.default_format)]) dv_call = mock.Mock() dv_call.variant = test_utils.make_variant(start=10, alleles=['A', 'C', 'G']) ex = mock.Mock() alt1, alt2 = ['C'], ['G'] self.processor.pic.create_pileup_images.return_value = [(alt1, 'tensor1'), (alt2, 'tensor2')] actual = self.processor.create_pileup_examples(dv_call) self.processor.pic.create_pileup_images.assert_called_once_with(dv_call) self.assertEquals(len(actual), 2) for ex, (alt, img) in zip(actual, [(alt1, 'tensor1'), (alt2, 'tensor2')]): self.assertEqual(tf_utils.example_alt_alleles(ex), alt) self.assertEqual(tf_utils.example_variant(ex), dv_call.variant) self.assertEqual(tf_utils.example_encoded_image(ex), img) self.assertEqual(tf_utils.example_image_shape(ex), self.default_shape) self.assertEqual(tf_utils.example_image_format(ex), self.default_format)
def test_create_pileup_examples(self): self.processor.pic = mock.Mock() self.processor.pic.get_reads.return_value = [] self.add_mock('_encode_tensor', side_effect=[(six.b('tensor1'), self.default_shape, self.default_format), (six.b('tensor2'), self.default_shape, self.default_format)]) dv_call = mock.Mock() dv_call.variant = test_utils.make_variant(start=10, alleles=['A', 'C', 'G']) ex = mock.Mock() alt1, alt2 = ['C'], ['G'] self.processor.pic.create_pileup_images.return_value = [ (alt1, six.b('tensor1')), (alt2, six.b('tensor2')) ] actual = self.processor.create_pileup_examples(dv_call) self.processor.pic.create_pileup_images.assert_called_once_with( dv_call=dv_call, reads_for_samples=[[]], haplotype_alignments_for_samples=None, haplotype_sequences=None, sample_order=None) self.assertLen(actual, 2) for ex, (alt, img) in zip(actual, [(alt1, six.b('tensor1')), (alt2, six.b('tensor2'))]): self.assertEqual(tf_utils.example_alt_alleles(ex), alt) self.assertEqual(tf_utils.example_variant(ex), dv_call.variant) self.assertEqual(tf_utils.example_encoded_image(ex), img) self.assertEqual(tf_utils.example_image_shape(ex), self.default_shape) self.assertEqual(tf_utils.example_image_format(ex), six.b(self.default_format))